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;
132 this->filename = filename;
134 this->column = column;
135 this->srcBrief = srcBrief;
142 : sources(sources), start(point), end(point) {}
154 if (!this->isValid())
return rhs;
155 if (!rhs.isValid())
return *
this;
163 }
else if (rhs.isValid()) {
164 start = start.min(rhs.start);
165 end = end.max(rhs.end);
170 bool operator==(
const SourceInfo &rhs)
const {
return start == rhs.start && end == rhs.end; }
174 void dbprint(std::ostream &out)
const { out << this->toString(); }
184 cstring toBriefSourceFragment()
const;
185 cstring toPositionString()
const;
186 cstring toSourcePositionData(
unsigned *outLineNumber,
unsigned *outColumnNumber)
const;
187 SourceFileLine toPosition()
const;
189 bool isValid()
const {
return this->start.isValid(); }
190 explicit operator bool()
const {
return isValid(); }
195 const SourcePosition &getStart()
const {
return this->start; }
197 const SourcePosition &getEnd()
const {
return this->end; }
205 if (!rhs.isValid())
return false;
206 if (!isValid())
return true;
207 return this->start < rhs.start;
209 inline bool operator>(
const SourceInfo &rhs)
const {
return rhs.operator<(*this); }
210 inline bool operator<=(
const SourceInfo &rhs)
const {
return !this->operator>(rhs); }
213 friend std::ostream &operator<<(std::ostream &os,
const SourceInfo &
info);
216 const InputSources *sources =
nullptr;
217 SourcePosition start = SourcePosition();
218 SourcePosition end = SourcePosition();
224 virtual cstring toString()
const = 0;
229template <
class,
class =
void>
258 : srcInfo(srcInfo), singleLine(singleLine), body(body) {}
259 cstring toString()
const override {
260 std::stringstream str;
264 void dbprint(std::ostream &out)
const override {
270 if (!singleLine) out <<
"*/";
288#ifdef P4C_GTEST_ENABLED
294 std::string_view getLine(
unsigned lineNumber)
const;
298 unsigned lineCount()
const;
300 unsigned getCurrentLineNumber()
const;
311 void mapLine(std::string_view file,
unsigned originalSourceLineNo);
330 void appendToLastLine(std::string_view text);
332 void appendNewline(std::string_view newline);
337 std::map<unsigned, SourceFileLine> line_file_map;
340 std::vector<std::string> contents;
342 std::vector<Comment *> comments;
Definition stringify.h:33
Definition source_file.h:221
Definition source_file.h:125
SourceInfo(const InputSources *sources, SourcePosition point)
Creates a SourceInfo for a 'point' in the source, or invalid.
Definition source_file.h:141
bool operator<(const SourceInfo &rhs) const
Definition source_file.h:204
SourceInfo operator+(const SourceInfo &rhs) const
Definition source_file.h:153
SourceInfo()=default
Creates an "invalid" SourceInfo.
cstring toSourceFragment(int trimWidth=-1, bool useMarker=true) const
Definition source_file.cpp:320
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 lib/error.h:148
Definition source_file.h:240
cstring fileName
an empty filename indicates stdin
Definition source_file.h:242
SFINAE helper to check if given class has a getSourceInfo method.
Definition source_file.h:230