21#ifndef LIB_SOURCE_FILE_H_
22#define LIB_SOURCE_FILE_H_
29#include "absl/strings/str_format.h"
34#ifdef P4C_GTEST_ENABLED
35#include "gtest/gtest_prod.h"
66 return columnNumber == rhs.columnNumber && lineNumber == rhs.lineNumber;
68 inline bool operator!=(
const SourcePosition &rhs)
const {
return !this->operator==(rhs); }
71 return (lineNumber < rhs.lineNumber) ||
72 (lineNumber == rhs.lineNumber && columnNumber < rhs.columnNumber);
74 inline bool operator>(
const SourcePosition &rhs)
const {
return rhs.operator<(*this); }
75 inline bool operator<=(
const SourcePosition &rhs)
const {
return !this->operator>(rhs); }
76 inline bool operator>=(
const SourcePosition &rhs)
const {
return !this->operator<(rhs); }
80 if (columnNumber > 0) columnNumber--;
90 if (this->
operator<(rhs))
return *
this;
95 if (this->
operator>(rhs))
return *
this;
101 bool isValid()
const {
return lineNumber != 0; }
103 unsigned getLineNumber()
const {
return lineNumber; }
105 unsigned getColumnNumber()
const {
return columnNumber; }
107 template <
typename Sink>
109 absl::Format(&sink,
"%d:%d", p.lineNumber, p.columnNumber);
114 unsigned lineNumber = 0;
115 unsigned columnNumber = 0;
138 this->filename = filename;
140 this->column = column;
141 this->srcBrief = srcBrief;
148 : sources(sources), start(point), end(point) {}
160 if (!this->isValid())
return rhs;
161 if (!rhs.isValid())
return *
this;
169 }
else if (rhs.isValid()) {
170 start = start.min(rhs.start);
171 end = end.max(rhs.end);
176 bool operator==(
const SourceInfo &rhs)
const {
return start == rhs.start && end == rhs.end; }
180 void dbprint(std::ostream &out)
const { out << this->toString(); }
190 cstring toBriefSourceFragment()
const;
191 cstring toPositionString()
const;
192 cstring toSourcePositionData(
unsigned *outLineNumber,
unsigned *outColumnNumber)
const;
193 SourceFileLine toPosition()
const;
195 bool isValid()
const {
return this->start.isValid(); }
196 explicit operator bool()
const {
return isValid(); }
201 const SourcePosition &getStart()
const {
return this->start; }
203 const SourcePosition &getEnd()
const {
return this->end; }
211 if (!rhs.isValid())
return false;
212 if (!isValid())
return true;
213 return this->start < rhs.start;
215 inline bool operator>(
const SourceInfo &rhs)
const {
return rhs.operator<(*this); }
216 inline bool operator<=(
const SourceInfo &rhs)
const {
return !this->operator>(rhs); }
219 friend std::ostream &operator<<(std::ostream &os,
const SourceInfo &
info);
222 const InputSources *sources =
nullptr;
223 SourcePosition start = SourcePosition();
224 SourcePosition end = SourcePosition();
230 virtual cstring toString()
const = 0;
235template <
class,
class =
void>
264 : srcInfo(srcInfo), singleLine(singleLine), body(body) {}
265 cstring toString()
const override {
266 std::stringstream str;
270 void dbprint(std::ostream &out)
const override {
276 if (!singleLine) out <<
"*/";
294#ifdef P4C_GTEST_ENABLED
300 std::string_view getLine(
unsigned lineNumber)
const;
304 unsigned lineCount()
const;
306 unsigned getCurrentLineNumber()
const;
317 void mapLine(std::string_view file,
unsigned originalSourceLineNo);
336 void appendToLastLine(std::string_view text);
338 void appendNewline(std::string_view newline);
343 std::map<unsigned, SourceFileLine> line_file_map;
346 std::vector<std::string> contents;
348 std::vector<Comment *> comments;
Definition stringify.h:33
Definition source_file.h:227
Definition source_file.h:131
SourceInfo(const InputSources *sources, SourcePosition point)
Creates a SourceInfo for a 'point' in the source, or invalid.
Definition source_file.h:147
bool operator<(const SourceInfo &rhs) const
Definition source_file.h:210
SourceInfo operator+(const SourceInfo &rhs) const
Definition source_file.h:159
SourceInfo()=default
Creates an "invalid" SourceInfo.
cstring toSourceFragment(int trimWidth=-1, bool useMarker=true) const
Definition source_file.cpp:317
Definition source_file.h:55
SourcePosition & operator--()
Move one column back. This never moves one line back.
Definition source_file.h:79
SourcePosition()=default
Creates an invalid source position.
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:148
Definition source_file.h:246
cstring fileName
an empty filename indicates stdin
Definition source_file.h:248
SFINAE helper to check if given class has a getSourceInfo method.
Definition source_file.h:236