21#ifndef LIB_SOURCE_FILE_H_
22#define LIB_SOURCE_FILE_H_
33#ifdef P4C_GTEST_ENABLED
34#include "gtest/gtest_prod.h"
65 return columnNumber == rhs.columnNumber && lineNumber == rhs.lineNumber;
67 inline bool operator!=(
const SourcePosition &rhs)
const {
return !this->operator==(rhs); }
70 return (lineNumber < rhs.lineNumber) ||
71 (lineNumber == rhs.lineNumber && columnNumber < rhs.columnNumber);
73 inline bool operator>(
const SourcePosition &rhs)
const {
return rhs.operator<(*this); }
74 inline bool operator<=(
const SourcePosition &rhs)
const {
return !this->operator>(rhs); }
75 inline bool operator>=(
const SourcePosition &rhs)
const {
return !this->operator<(rhs); }
79 if (columnNumber > 0) columnNumber--;
89 if (this->
operator<(rhs))
return *
this;
94 if (this->
operator>(rhs))
return *
this;
100 bool isValid()
const {
return lineNumber != 0; }
102 unsigned getLineNumber()
const {
return lineNumber; }
104 unsigned getColumnNumber()
const {
return columnNumber; }
108 unsigned lineNumber = 0;
109 unsigned columnNumber = 0;
131 this->filename = filename;
133 this->column = column;
134 this->srcBrief = srcBrief;
141 : sources(sources), start(point), end(point) {}
153 if (!this->isValid())
return rhs;
154 if (!rhs.isValid())
return *
this;
162 }
else if (rhs.isValid()) {
163 start = start.min(rhs.start);
164 end = end.max(rhs.end);
169 bool operator==(
const SourceInfo &rhs)
const {
return start == rhs.start && end == rhs.end; }
173 void dbprint(std::ostream &out)
const { out << this->toString(); }
183 cstring toBriefSourceFragment()
const;
184 cstring toPositionString()
const;
185 cstring toSourcePositionData(
unsigned *outLineNumber,
unsigned *outColumnNumber)
const;
186 SourceFileLine toPosition()
const;
188 bool isValid()
const {
return this->start.isValid(); }
189 explicit operator bool()
const {
return isValid(); }
194 const SourcePosition &getStart()
const {
return this->start; }
196 const SourcePosition &getEnd()
const {
return this->end; }
204 if (!rhs.isValid())
return false;
205 if (!isValid())
return true;
206 return this->start < rhs.start;
208 inline bool operator>(
const SourceInfo &rhs)
const {
return rhs.operator<(*this); }
209 inline bool operator<=(
const SourceInfo &rhs)
const {
return !this->operator>(rhs); }
212 friend std::ostream &operator<<(std::ostream &os,
const SourceInfo &
info);
215 const InputSources *sources =
nullptr;
216 SourcePosition start = SourcePosition();
217 SourcePosition end = SourcePosition();
223 virtual cstring toString()
const = 0;
228template <
class,
class =
void>
257 : srcInfo(srcInfo), singleLine(singleLine), body(body) {}
259 std::stringstream str;
263 void dbprint(std::ostream &out)
const override {
269 if (!singleLine) out <<
"*/";
284#ifdef P4C_GTEST_ENABLED
291 std::string_view getLine(
unsigned lineNumber)
const;
295 unsigned lineCount()
const;
297 unsigned getCurrentLineNumber()
const;
308 void mapLine(std::string_view file,
unsigned originalSourceLineNo);
324 void appendToLastLine(std::string_view text);
326 void appendNewline(std::string_view newline);
331 std::map<unsigned, SourceFileLine> line_file_map;
334 std::vector<std::string> contents;
336 std::vector<Comment *> comments;
Definition stringify.h:33
Definition source_file.h:220
Definition source_file.h:124
SourceInfo(const InputSources *sources, SourcePosition point)
Creates a SourceInfo for a 'point' in the source, or invalid.
Definition source_file.h:140
bool operator<(const SourceInfo &rhs) const
Definition source_file.h:203
SourceInfo operator+(const SourceInfo &rhs) const
Definition source_file.h:152
SourceInfo()=default
Creates an "invalid" SourceInfo.
cstring toSourceFragment(int trimWidth=-1, bool useMarker=true) const
Definition source_file.cpp:318
Definition source_file.h:54
SourcePosition & operator--()
Move one column back. This never moves one line back.
Definition source_file.h:78
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 error.h:148
Definition source_file.h:239
cstring fileName
an empty filename indicates stdin
Definition source_file.h:241
SFINAE helper to check if given class has a getSourceInfo method.
Definition source_file.h:229