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
196 bool isValid() const { return this->start.isValid(); }
197 explicit operator bool() const { return isValid(); }
198
199 cstring getSourceFile() const;
200 cstring getLineNum() const;
201
202 const SourcePosition &getStart() const { return this->start; }
203
204 const SourcePosition &getEnd() const { return this->end; }
205
211 bool operator<(const SourceInfo &rhs) const {
212 if (!rhs.isValid()) return false;
213 if (!isValid()) return true;
214 return this->start < rhs.start;
215 }
216 inline bool operator>(const SourceInfo &rhs) const { return rhs.operator<(*this); }
217 inline bool operator<=(const SourceInfo &rhs) const { return !this->operator>(rhs); }
218 inline bool operator>=(const SourceInfo &rhs) const { return !this->operator<(rhs); }
219
220 friend std::ostream &operator<<(std::ostream &os, const SourceInfo &info);
221
222 private:
223 const InputSources *sources = nullptr;
224 SourcePosition start = SourcePosition();
225 SourcePosition end = SourcePosition();
226};
227
229 public:
230 virtual SourceInfo getSourceInfo() const = 0;
231 virtual cstring toString() const = 0;
232 virtual ~IHasSourceInfo() {}
233};
234
236template <class, class = void>
237struct has_SourceInfo : std::false_type {};
238
239template <class T>
240struct has_SourceInfo<T, std::void_t<decltype(std::declval<T>().getSourceInfo())>>
241 : std::true_type {};
242
243template <class T>
244inline constexpr bool has_SourceInfo_v = has_SourceInfo<T>::value;
245
247struct SourceFileLine {
250 unsigned sourceLine;
251
252 SourceFileLine(std::string_view file, unsigned line) : fileName(file), sourceLine(line) {}
253
254 cstring toString() const;
255};
256
257class Comment final : IHasDbPrint, IHasSourceInfo {
258 private:
259 SourceInfo srcInfo;
260 bool singleLine;
261 cstring body;
262
263 public:
264 Comment(SourceInfo srcInfo, bool singleLine, cstring body)
265 : srcInfo(srcInfo), singleLine(singleLine), body(body) {}
266 cstring toString() const override {
267 std::stringstream str;
268 dbprint(str);
269 return {str};
270 }
271 void dbprint(std::ostream &out) const override {
272 if (singleLine)
273 out << "//";
274 else
275 out << "/*";
276 out << body;
277 if (!singleLine) out << "*/";
278 }
279
281 [[nodiscard]] SourceInfo getSourceInfo() const override { return srcInfo; }
282};
283
294class InputSources final {
295#ifdef P4C_GTEST_ENABLED
296 FRIEND_TEST(UtilSourceFile, InputSources);
297#endif
298
299 public:
300 InputSources();
301 std::string_view getLine(unsigned lineNumber) const;
303 SourceFileLine getSourceLine(unsigned line) const;
304
305 unsigned lineCount() const;
306 SourcePosition getCurrentPosition() const;
307 unsigned getCurrentLineNumber() const;
308
310 void seal();
311
313 void appendText(const char *text);
314
318 void mapLine(std::string_view file, unsigned originalSourceLineNo);
319
325 cstring getSourceFragment(const SourcePosition &position, int trimWidth, bool useMarker) const;
326 cstring getSourceFragment(const SourceInfo &position, int trimWidth, bool useMarker) const;
327 cstring getBriefSourceFragment(const SourceInfo &position) const;
328
329 cstring toDebugString() const;
330 void addComment(SourceInfo srcInfo, bool singleLine, cstring body);
331
333 const std::vector<Comment *> &getAllComments() const;
334
335 private:
337 void appendToLastLine(std::string_view text);
339 void appendNewline(std::string_view newline);
340
342 bool sealed;
343
344 std::map<unsigned, SourceFileLine> line_file_map;
345
347 std::vector<std::string> contents;
349 std::vector<Comment *> comments;
350};
351
352} // namespace P4::Util
353
354namespace P4 {
355
356void dbprint(const IHasDbPrint *o);
357
358} // namespace P4
359
360#endif /* LIB_SOURCE_FILE_H_ */
Definition stringify.h:33
Definition source_file.h:257
SourceInfo getSourceInfo() const override
Retrieve the source position associated with this comment.
Definition source_file.h:281
Definition source_file.h:228
Definition source_file.h:294
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:211
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 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:155
STL namespace.
Definition source_file.h:247
cstring fileName
an empty filename indicates stdin
Definition source_file.h:249
SFINAE helper to check if given class has a getSourceInfo method.
Definition source_file.h:237