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) {
139 this->filename = filename;
140 this->line = line;
141 this->column = column;
142 this->srcBrief = srcBrief;
143 }
145 SourceInfo() = default;
146
149 : sources(sources), start(point), end(point) {}
150
151 SourceInfo(const InputSources *sources, SourcePosition start, SourcePosition end);
152
153 SourceInfo(const SourceInfo &other) = default;
154 SourceInfo &operator=(const SourceInfo &other) = default;
155 ~SourceInfo() = default;
156
160 SourceInfo operator+(const SourceInfo &rhs) const {
161 if (!this->isValid()) return rhs;
162 if (!rhs.isValid()) return *this;
163 SourcePosition s = start.min(rhs.start);
164 SourcePosition e = end.max(rhs.end);
165 return SourceInfo(sources, s, e);
166 }
167 SourceInfo &operator+=(const SourceInfo &rhs) {
168 if (!isValid()) {
169 *this = rhs;
170 } else if (rhs.isValid()) {
171 start = start.min(rhs.start);
172 end = end.max(rhs.end);
173 }
174 return *this;
175 }
176
177 bool operator==(const SourceInfo &rhs) const { return start == rhs.start && end == rhs.end; }
178
179 cstring toString() const;
180
181 void dbprint(std::ostream &out) const { out << this->toString(); }
182
189 cstring toSourceFragment(int trimWidth = -1, bool useMarker = true) const;
190 cstring toSourceFragment(bool useMarker) const { return toSourceFragment(-1, useMarker); }
191 cstring toBriefSourceFragment() const;
192 cstring toPositionString() const;
193 cstring toSourcePositionData(unsigned *outLineNumber, unsigned *outColumnNumber) const;
194 SourceFileLine toPosition() const;
195 SourceFileLine toPositionEnd() const;
196
197 bool isValid() const { return this->start.isValid(); }
198 explicit operator bool() const { return isValid(); }
199
200 cstring getSourceFile() const;
201 cstring getLineNum() const;
202
203 const SourcePosition &getStart() const { return this->start; }
204
205 const SourcePosition &getEnd() const { return this->end; }
206
212 bool operator<(const SourceInfo &rhs) const {
213 if (!rhs.isValid()) return false;
214 if (!isValid()) return true;
215 return this->start < rhs.start;
216 }
217 inline bool operator>(const SourceInfo &rhs) const { return rhs.operator<(*this); }
218 inline bool operator<=(const SourceInfo &rhs) const { return !this->operator>(rhs); }
219 inline bool operator>=(const SourceInfo &rhs) const { return !this->operator<(rhs); }
220
221 friend std::ostream &operator<<(std::ostream &os, const SourceInfo &info);
222
223 private:
224 const InputSources *sources = nullptr;
225 SourcePosition start = SourcePosition();
226 SourcePosition end = SourcePosition();
227};
228
230 public:
231 virtual SourceInfo getSourceInfo() const = 0;
232 virtual cstring toString() const = 0;
233 virtual ~IHasSourceInfo() {}
234};
235
237template <class, class = void>
238struct has_SourceInfo : std::false_type {};
239
240template <class T>
241struct has_SourceInfo<T, std::void_t<decltype(std::declval<T>().getSourceInfo())>>
242 : std::true_type {};
243
244template <class T>
245inline constexpr bool has_SourceInfo_v = has_SourceInfo<T>::value;
246
248struct SourceFileLine {
251 unsigned sourceLine;
252
253 SourceFileLine(std::string_view file, unsigned line) : fileName(file), sourceLine(line) {}
254
255 cstring toString() const;
256};
257
258class Comment final : IHasDbPrint, IHasSourceInfo {
259 private:
260 SourceInfo srcInfo;
261 bool singleLine;
262 cstring body;
263
264 public:
265 Comment(SourceInfo srcInfo, bool singleLine, cstring body)
266 : srcInfo(srcInfo), singleLine(singleLine), body(body) {}
267 cstring toString() const override {
268 std::stringstream str;
269 dbprint(str);
270 return {str};
271 }
272 void dbprint(std::ostream &out) const override {
273 if (singleLine)
274 out << "//";
275 else
276 out << "/*";
277 out << body;
278 if (!singleLine) out << "*/";
279 }
280
282 [[nodiscard]] SourceInfo getSourceInfo() const override { return srcInfo; }
283};
284
295class InputSources final {
296#ifdef P4C_GTEST_ENABLED
297 FRIEND_TEST(UtilSourceFile, InputSources);
298#endif
299
300 public:
301 InputSources();
302 std::string_view getLine(unsigned lineNumber) const;
304 SourceFileLine getSourceLine(unsigned line) const;
305
306 unsigned lineCount() const;
307 SourcePosition getCurrentPosition() const;
308 unsigned getCurrentLineNumber() const;
309
311 void seal();
312
314 void appendText(const char *text);
315
319 void mapLine(std::string_view file, unsigned originalSourceLineNo);
320
326 cstring getSourceFragment(const SourcePosition &position, int trimWidth, bool useMarker) const;
327 cstring getSourceFragment(const SourceInfo &position, int trimWidth, bool useMarker) const;
328 cstring getBriefSourceFragment(const SourceInfo &position) const;
329
330 cstring toDebugString() const;
331 void addComment(SourceInfo srcInfo, bool singleLine, cstring body);
332
334 const std::vector<Comment *> &getAllComments() const;
335
336 private:
338 void appendToLastLine(std::string_view text);
340 void appendNewline(std::string_view newline);
341
343 bool sealed;
344
345 std::map<unsigned, SourceFileLine> line_file_map;
346
348 std::vector<std::string> contents;
350 std::vector<Comment *> comments;
351};
352
353} // namespace P4::Util
354
355namespace P4 {
356
357void dbprint(const IHasDbPrint *o);
358
359} // namespace P4
360
361#endif /* LIB_SOURCE_FILE_H_ */
Definition stringify.h:33
Definition source_file.h:258
SourceInfo getSourceInfo() const override
Retrieve the source position associated with this comment.
Definition source_file.h:282
Definition source_file.h:229
Definition source_file.h:295
void appendText(const char *text)
Append this text; it is either a newline or a text with no newlines.
Definition source_file.cpp:120
cstring getSourceFragment(const SourcePosition &position, int trimWidth, bool useMarker) const
Definition source_file.cpp:197
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:83
void mapLine(std::string_view file, unsigned originalSourceLineNo)
Definition source_file.cpp:162
SourceFileLine getSourceLine(unsigned line) const
Original source line that produced the line with the specified number.
Definition source_file.cpp:168
void seal()
Prevents further changes; currently not used.
Definition source_file.cpp:86
Definition source_file.h:132
SourceInfo(const InputSources *sources, SourcePosition point)
Creates a SourceInfo for a 'point' in the source, or invalid.
Definition source_file.h:148
bool operator<(const SourceInfo &rhs) const
Definition source_file.h:212
SourceInfo operator+(const SourceInfo &rhs) const
Definition source_file.h:160
SourceInfo()=default
Creates an "invalid" SourceInfo.
cstring toSourceFragment(int trimWidth=-1, bool useMarker=true) const
Definition source_file.cpp:317
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:248
cstring fileName
an empty filename indicates stdin
Definition source_file.h:250
SFINAE helper to check if given class has a getSourceInfo method.
Definition source_file.h:238