P4C
The P4 Compiler
Loading...
Searching...
No Matches
source_file.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17/* -*-c++-*- */
18
19/* Source-level information for a P4 program */
20
21#ifndef LIB_SOURCE_FILE_H_
22#define LIB_SOURCE_FILE_H_
23
24#include <map>
25#include <sstream>
26#include <string_view>
27#include <vector>
28
29#include "absl/strings/str_format.h"
30#include "config.h"
31#include "cstring.h"
32#include "stringify.h"
33
34// GTest
35#ifdef P4C_GTEST_ENABLED
36#include "gtest/gtest_prod.h"
37#endif
38
39namespace P4::Test {
40class UtilSourceFile;
41}
42
43namespace P4::Util {
44using namespace P4::literals;
45
46struct SourceFileLine;
56class SourcePosition final {
57 public:
59 SourcePosition() = default;
60
61 SourcePosition(unsigned lineNumber, unsigned columnNumber);
62
63 SourcePosition(const SourcePosition &other) = default;
64 SourcePosition &operator=(const SourcePosition &) = default;
65
66 inline bool operator==(const SourcePosition &rhs) const {
67 return columnNumber == rhs.columnNumber && lineNumber == rhs.lineNumber;
68 }
69 inline bool operator!=(const SourcePosition &rhs) const { return !this->operator==(rhs); }
70
71 inline bool operator<(const SourcePosition &rhs) const {
72 return (lineNumber < rhs.lineNumber) ||
73 (lineNumber == rhs.lineNumber && columnNumber < rhs.columnNumber);
74 }
75 inline bool operator>(const SourcePosition &rhs) const { return rhs.operator<(*this); }
76 inline bool operator<=(const SourcePosition &rhs) const { return !this->operator>(rhs); }
77 inline bool operator>=(const SourcePosition &rhs) const { return !this->operator<(rhs); }
78
81 if (columnNumber > 0) columnNumber--;
82 return *this;
83 }
85 SourcePosition tmp(*this);
86 this->operator--();
87 return tmp;
88 }
89
90 inline const SourcePosition &min(const SourcePosition &rhs) const {
91 if (this->operator<(rhs)) return *this;
92 return rhs;
93 }
94
95 inline const SourcePosition &max(const SourcePosition &rhs) const {
96 if (this->operator>(rhs)) return *this;
97 return rhs;
98 }
99
100 cstring toString() const;
101
102 bool isValid() const { return lineNumber != 0; }
103
104 unsigned getLineNumber() const { return lineNumber; }
105
106 unsigned getColumnNumber() const { return columnNumber; }
107
108 template <typename Sink>
109 friend void AbslStringify(Sink &sink, const SourcePosition &p) {
110 absl::Format(&sink, "%d:%d", p.lineNumber, p.columnNumber);
111 }
112
113 private:
114 // Input sources where this character position is interpreted.
115 unsigned lineNumber = 0;
116 unsigned columnNumber = 0;
117};
118
119class InputSources;
120class Comment;
121
132class SourceInfo final {
133 public:
134 cstring filename = ""_cs;
135 int line = -1;
136 int column = -1;
137 cstring srcBrief = ""_cs;
138 SourceInfo(cstring filename, int line, int column, cstring srcBrief);
140 SourceInfo() = default;
141
143 SourceInfo(const InputSources *sources, SourcePosition point);
144
145 SourceInfo(const InputSources *sources, SourcePosition start, SourcePosition end);
146
147 SourceInfo(const SourceInfo &other) = default;
148 SourceInfo &operator=(const SourceInfo &other) = default;
149 ~SourceInfo() = default;
150
154 SourceInfo operator+(const SourceInfo &rhs) const {
155 if (!this->isValid()) return rhs;
156 if (!rhs.isValid()) return *this;
157 SourcePosition s = start.min(rhs.start);
158 SourcePosition e = end.max(rhs.end);
159 return SourceInfo(sources, s, e);
160 }
161 SourceInfo &operator+=(const SourceInfo &rhs) {
162 if (!isValid()) {
163 *this = rhs;
164 } else if (rhs.isValid()) {
165 start = start.min(rhs.start);
166 end = end.max(rhs.end);
167 }
168 return *this;
169 }
170
171 bool operator==(const SourceInfo &rhs) const { return start == rhs.start && end == rhs.end; }
172
173 cstring toString() const;
174
175 void dbprint(std::ostream &out) const { out << this->toString(); }
176
183 cstring toSourceFragment(int trimWidth = -1, bool useMarker = true) const;
184 cstring toSourceFragment(bool useMarker) const { return toSourceFragment(-1, useMarker); }
185 cstring toBriefSourceFragment() const;
186 cstring toPositionString() const;
187 cstring toSourcePositionData(unsigned *outLineNumber, unsigned *outColumnNumber) const;
188 SourceFileLine toPosition() const;
189 SourceFileLine toPositionEnd() const;
190
191 bool isValid() const { return this->start.isValid(); }
192 explicit operator bool() const { return isValid(); }
193
194 cstring getSourceFile() const;
195 cstring getLineNum() const;
196
197 const SourcePosition &getStart() const { return this->start; }
198
199 const SourcePosition &getEnd() const { return this->end; }
200
206 bool operator<(const SourceInfo &rhs) const {
207 if (!rhs.isValid()) return false;
208 if (!isValid()) return true;
209 return this->start < rhs.start;
210 }
211 inline bool operator>(const SourceInfo &rhs) const { return rhs.operator<(*this); }
212 inline bool operator<=(const SourceInfo &rhs) const { return !this->operator>(rhs); }
213 inline bool operator>=(const SourceInfo &rhs) const { return !this->operator<(rhs); }
214
215 friend std::ostream &operator<<(std::ostream &os, const SourceInfo &info);
216
217 private:
218 const InputSources *sources = nullptr;
219 SourcePosition start = SourcePosition();
220 SourcePosition end = SourcePosition();
221};
222
224 public:
225 virtual SourceInfo getSourceInfo() const = 0;
226 virtual cstring toString() const = 0;
227 virtual ~IHasSourceInfo() {}
228};
229
231template <class, class = void>
232struct has_SourceInfo : std::false_type {};
233
234template <class T>
235struct has_SourceInfo<T, std::void_t<decltype(std::declval<T>().getSourceInfo())>>
236 : std::true_type {};
237
238template <class T>
239inline constexpr bool has_SourceInfo_v = has_SourceInfo<T>::value;
240
242struct SourceFileLine {
245 unsigned sourceLine;
246
247 SourceFileLine(std::string_view file, unsigned line) : fileName(file), sourceLine(line) {}
248
249 cstring toString() const;
250};
251
252class Comment final : IHasDbPrint, IHasSourceInfo {
253 private:
254 SourceInfo srcInfo;
255 bool singleLine;
256 cstring body;
257
258 public:
259 Comment(SourceInfo srcInfo, bool singleLine, cstring body)
260 : srcInfo(srcInfo), singleLine(singleLine), body(body) {}
261 cstring toString() const override {
262 std::stringstream str;
263 dbprint(str);
264 return {str};
265 }
266 void dbprint(std::ostream &out) const override {
267 if (singleLine)
268 out << "//";
269 else
270 out << "/*";
271 out << body;
272 if (!singleLine) out << "*/";
273 }
274
276 [[nodiscard]] SourceInfo getSourceInfo() const override { return srcInfo; }
277};
278
289class InputSources final {
290#ifdef P4C_GTEST_ENABLED
291 FRIEND_TEST(UtilSourceFile, InputSources);
292#endif
293
294 public:
295 InputSources();
296 std::string_view getLine(unsigned lineNumber) const;
298 SourceFileLine getSourceLine(unsigned line) const;
299
300 unsigned lineCount() const;
301 SourcePosition getCurrentPosition() const;
302 unsigned getCurrentLineNumber() const;
303
305 void seal();
306
308 void appendText(const char *text);
309
313 void mapLine(std::string_view file, unsigned originalSourceLineNo);
314
320 cstring getSourceFragment(const SourcePosition &position, int trimWidth, bool useMarker) const;
321 cstring getSourceFragment(const SourceInfo &position, int trimWidth, bool useMarker) const;
322 cstring getBriefSourceFragment(const SourceInfo &position) const;
323
324 cstring toDebugString() const;
325 void addComment(SourceInfo srcInfo, bool singleLine, cstring body);
326
328 const std::vector<Comment *> &getAllComments() const;
329
330 private:
332 void appendToLastLine(std::string_view text);
334 void appendNewline(std::string_view newline);
335
337 bool sealed;
338
339 std::map<unsigned, SourceFileLine> line_file_map;
340
342 std::vector<std::string> contents;
344 std::vector<Comment *> comments;
345};
346
347} // namespace P4::Util
348
349namespace P4 {
350
351void dbprint(const IHasDbPrint *o);
352
353} // namespace P4
354
355#endif /* LIB_SOURCE_FILE_H_ */
Definition stringify.h:33
Definition source_file.h:252
SourceInfo getSourceInfo() const override
Retrieve the source position associated with this comment.
Definition source_file.h:276
Definition source_file.h:223
Definition source_file.h:289
void appendText(const char *text)
Append this text; it is either a newline or a text with no newlines.
Definition source_file.cpp:132
cstring getSourceFragment(const SourcePosition &position, int trimWidth, bool useMarker) const
Definition source_file.cpp:209
const std::vector< Comment * > & getAllComments() const
Returns a list of all the comments found in the file, stored as a part of InputSources
Definition source_file.cpp:95
void mapLine(std::string_view file, unsigned originalSourceLineNo)
Definition source_file.cpp:174
SourceFileLine getSourceLine(unsigned line) const
Original source line that produced the line with the specified number.
Definition source_file.cpp:180
void seal()
Prevents further changes; currently not used.
Definition source_file.cpp:98
Definition source_file.h:132
bool operator<(const SourceInfo &rhs) const
Definition source_file.h:206
SourceInfo operator+(const SourceInfo &rhs) const
Definition source_file.h:154
SourceInfo()=default
Creates an "invalid" SourceInfo.
cstring toSourceFragment(int trimWidth=-1, bool useMarker=true) const
Definition source_file.cpp:329
Definition source_file.h:56
SourcePosition & operator--()
Move one column back. This never moves one line back.
Definition source_file.h:80
SourcePosition()=default
Creates an invalid source position.
Definition cstring.h:85
Definition lib/json.h:35
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
void info(const int kind, const char *format, const T *node, Args &&...args)
Report info messages of type kind. Requires that the node argument have source info.
Definition lib/error.h:167
STL namespace.
Definition source_file.h:242
cstring fileName
an empty filename indicates stdin
Definition source_file.h:244
SFINAE helper to check if given class has a getSourceInfo method.
Definition source_file.h:232