21#ifndef LIB_SOURCE_FILE_H_
22#define LIB_SOURCE_FILE_H_
32#ifdef P4C_GTEST_ENABLED
33#include "gtest/gtest_prod.h"
64 return columnNumber == rhs.columnNumber && lineNumber == rhs.lineNumber;
66 inline bool operator!=(
const SourcePosition &rhs)
const {
return !this->operator==(rhs); }
69 return (lineNumber < rhs.lineNumber) ||
70 (lineNumber == rhs.lineNumber && columnNumber < rhs.columnNumber);
72 inline bool operator>(
const SourcePosition &rhs)
const {
return rhs.operator<(*this); }
73 inline bool operator<=(
const SourcePosition &rhs)
const {
return !this->operator>(rhs); }
74 inline bool operator>=(
const SourcePosition &rhs)
const {
return !this->operator<(rhs); }
78 if (columnNumber > 0) columnNumber--;
88 if (this->
operator<(rhs))
return *
this;
93 if (this->
operator>(rhs))
return *
this;
99 bool isValid()
const {
return lineNumber != 0; }
101 unsigned getLineNumber()
const {
return lineNumber; }
103 unsigned getColumnNumber()
const {
return columnNumber; }
107 unsigned lineNumber = 0;
108 unsigned columnNumber = 0;
130 this->filename = filename;
132 this->column = column;
133 this->srcBrief = srcBrief;
140 : sources(sources), start(point), end(point) {}
152 if (!this->isValid())
return rhs;
153 if (!rhs.isValid())
return *
this;
161 }
else if (rhs.isValid()) {
162 start = start.min(rhs.start);
163 end = end.max(rhs.end);
168 bool operator==(
const SourceInfo &rhs)
const {
return start == rhs.start && end == rhs.end; }
172 void dbprint(std::ostream &out)
const { out << this->toString(); }
174 cstring toSourceFragment(
bool useMarker =
true)
const;
175 cstring toBriefSourceFragment()
const;
176 cstring toPositionString()
const;
177 cstring toSourcePositionData(
unsigned *outLineNumber,
unsigned *outColumnNumber)
const;
178 SourceFileLine toPosition()
const;
180 bool isValid()
const {
return this->start.isValid(); }
181 explicit operator bool()
const {
return isValid(); }
186 const SourcePosition &getStart()
const {
return this->start; }
188 const SourcePosition &getEnd()
const {
return this->end; }
196 if (!rhs.isValid())
return false;
197 if (!isValid())
return true;
198 return this->start < rhs.start;
200 inline bool operator>(
const SourceInfo &rhs)
const {
return rhs.operator<(*this); }
201 inline bool operator<=(
const SourceInfo &rhs)
const {
return !this->operator>(rhs); }
205 const InputSources *sources =
nullptr;
206 SourcePosition start = SourcePosition();
207 SourcePosition end = SourcePosition();
213 virtual cstring toString()
const = 0;
218template <
class,
class =
void>
247 : srcInfo(srcInfo), singleLine(singleLine), body(body) {}
255 if (!singleLine) result +=
"*/";
258 void dbprint(std::ostream &out)
const { out << toString(); }
272#ifdef P4C_GTEST_ENABLED
279 std::string_view getLine(
unsigned lineNumber)
const;
283 unsigned lineCount()
const;
285 unsigned getCurrentLineNumber()
const;
296 void mapLine(std::string_view file,
unsigned originalSourceLineNo);
312 void appendToLastLine(std::string_view text);
314 void appendNewline(std::string_view newline);
319 std::map<unsigned, SourceFileLine> line_file_map;
322 std::vector<std::string> contents;
324 std::vector<Comment *> comments;
Definition stringify.h:31
Definition source_file.h:210
Definition source_file.h:123
bool operator<(const SourceInfo &rhs) const
Definition source_file.h:195
SourceInfo()=default
Creates an "invalid" SourceInfo.
SourceInfo(const InputSources *sources, SourcePosition point)
Creates a SourceInfo for a 'point' in the source, or invalid.
Definition source_file.h:139
SourceInfo operator+(const SourceInfo &rhs) const
Definition source_file.h:151
Definition source_file.h:53
SourcePosition()=default
Creates an invalid source position.
SourcePosition & operator--()
Move one column back. This never moves one line back.
Definition source_file.h:77
Definition source_file.h:229
cstring fileName
an empty filename indicates stdin
Definition source_file.h:231
SFINAE helper to check if given class has a getSourceInfo method.
Definition source_file.h:219