55 Provenance(std::size_t offset) : offset_{offset} { CHECK(offset > 0); }
56 Provenance(
const Provenance &that) =
default;
57 Provenance(Provenance &&that) =
default;
58 Provenance &operator=(
const Provenance &that) =
default;
59 Provenance &operator=(Provenance &&that) =
default;
61 std::size_t offset()
const {
return offset_; }
63 Provenance operator+(ptrdiff_t n)
const {
64 CHECK(n > -
static_cast<ptrdiff_t
>(offset_));
65 return {offset_ +
static_cast<std::size_t
>(n)};
67 Provenance operator+(std::size_t n)
const {
return {offset_ + n}; }
68 std::size_t operator-(Provenance that)
const {
70 return offset_ - that.offset_;
72 bool operator<(Provenance that)
const {
return offset_ < that.offset_; }
73 bool operator<=(Provenance that)
const {
return !(that < *
this); }
74 bool operator==(Provenance that)
const {
return offset_ == that.offset_; }
75 bool operator!=(Provenance that)
const {
return !(*
this == that); }
78 std::size_t offset_{0};
88class ProvenanceRangeToOffsetMappings {
90 ProvenanceRangeToOffsetMappings();
91 ~ProvenanceRangeToOffsetMappings();
92 bool empty()
const {
return map_.empty(); }
93 void Put(ProvenanceRange, std::size_t offset);
94 std::optional<std::size_t> Map(ProvenanceRange)
const;
95 llvm::raw_ostream &Dump(llvm::raw_ostream &)
const;
101 struct WhollyPrecedes {
102 bool operator()(ProvenanceRange, ProvenanceRange)
const;
105 std::multimap<ProvenanceRange, std::size_t, WhollyPrecedes> map_;
112class OffsetToProvenanceMappings {
114 OffsetToProvenanceMappings() {}
116 void swap(OffsetToProvenanceMappings &);
117 void shrink_to_fit();
118 std::size_t SizeInBytes()
const;
119 void Put(ProvenanceRange);
120 void Put(
const OffsetToProvenanceMappings &);
121 ProvenanceRange Map(std::size_t at)
const;
122 void RemoveLastBytes(std::size_t);
124 llvm::raw_ostream &Dump(llvm::raw_ostream &)
const;
127 struct ContiguousProvenanceMapping {
129 ProvenanceRange range;
134 std::vector<ContiguousProvenanceMapping> provenanceMap_;
144 std::size_t size()
const {
return range_.size(); }
146 Encoding encoding()
const {
return encoding_; }
147 AllSources &set_encoding(Encoding e) {
152 void ClearSearchPath();
153 void AppendSearchPathDirectory(std::string);
154 const SourceFile *OpenPath(std::string path, llvm::raw_ostream &error);
155 const SourceFile *Open(std::string path, llvm::raw_ostream &error,
156 std::optional<std::string> &&prependPath = std::nullopt);
157 const SourceFile *ReadStandardInput(llvm::raw_ostream &error);
159 ProvenanceRange AddIncludedFile(
160 const SourceFile &, ProvenanceRange,
bool isModule =
false);
161 ProvenanceRange AddMacroCall(
162 ProvenanceRange def, ProvenanceRange use,
const std::string &expansion);
163 ProvenanceRange AddCompilerInsertion(std::string);
169 bool IsValid(
Provenance at)
const {
return range_.Contains(at); }
170 bool IsValid(ProvenanceRange range)
const {
171 return range.size() > 0 && range_.Contains(range);
173 void setShowColors(
bool showColors) { showColors_ = showColors; }
174 bool getShowColors()
const {
return showColors_; }
175 std::optional<ProvenanceRange> GetInclusionInfo(
176 const std::optional<ProvenanceRange> &)
const;
177 void EmitMessage(llvm::raw_ostream &,
const std::optional<ProvenanceRange> &,
178 const std::string &message,
const std::string &prefix,
179 llvm::raw_ostream::Colors color,
bool echoSourceLine =
false)
const;
181 Provenance, std::size_t *offset =
nullptr,
bool topLevel =
false)
const;
182 const char *GetSource(ProvenanceRange)
const;
183 std::optional<SourcePosition> GetSourcePosition(
Provenance)
const;
184 std::optional<ProvenanceRange> GetFirstFileProvenance()
const;
185 std::string GetPath(
Provenance,
bool topLevel =
false)
const;
187 Provenance CompilerInsertionProvenance(
char ch);
188 ProvenanceRange IntersectionWithSourceFiles(ProvenanceRange)
const;
189 llvm::raw_ostream &Dump(llvm::raw_ostream &)
const;
194 bool isModule{
false};
197 ProvenanceRange definition;
198 std::string expansion;
200 struct CompilerInsertion {
206 Origin(ProvenanceRange,
const SourceFile &, ProvenanceRange,
207 bool isModule =
false);
208 Origin(ProvenanceRange, ProvenanceRange def, ProvenanceRange use,
209 const std::string &expansion);
210 Origin(ProvenanceRange,
const std::string &);
212 const char &operator[](std::size_t)
const;
214 std::variant<Inclusion, Macro, CompilerInsertion> u;
215 ProvenanceRange covers, replaces;
221 std::vector<Origin> origin_;
222 ProvenanceRange range_;
223 std::map<char, Provenance> compilerInsertionProvenance_;
224 std::vector<std::unique_ptr<SourceFile>> ownedSourceFiles_;
225 std::list<std::string> searchPath_;
226 Encoding encoding_{Encoding::UTF_8};
227 bool showColors_{
false};
235 explicit CookedSource(
AllSources &allSources) : allSources_{allSources} {};
237 int number()
const {
return number_; }
238 void set_number(
int n) { number_ = n; }
241 std::optional<ProvenanceRange> GetProvenanceRange(
CharBlock)
const;
242 std::optional<CharBlock> GetCharBlock(ProvenanceRange)
const;
246 std::size_t Put(
const char *data, std::size_t bytes) {
247 return buffer_.Put(data, bytes);
249 std::size_t Put(
const std::string &s) {
return buffer_.Put(s); }
250 std::size_t Put(
char ch) {
return buffer_.Put(&ch, 1); }
252 provenanceMap_.Put(ProvenanceRange{p, 1});
253 return buffer_.Put(&ch, 1);
256 void PutProvenance(
Provenance p) { provenanceMap_.Put(ProvenanceRange{p}); }
257 void PutProvenance(ProvenanceRange pr) { provenanceMap_.Put(pr); }
259 provenanceMap_.Put(pm);
262 void MarkPossibleFixedFormContinuation() {
263 possibleFixedFormContinuations_.push_back(BufferedBytes());
266 std::size_t BufferedBytes()
const;
268 void CompileProvenanceRangeToOffsetMappings(
AllSources &);
269 llvm::raw_ostream &Dump(llvm::raw_ostream &)
const;
278 std::list<std::size_t> possibleFixedFormContinuations_;
281class AllCookedSources {
286 AllSources &allSources() {
return allSources_; }
287 const AllSources &allSources()
const {
return allSources_; }
294 bool IsValid(ProvenanceRange r)
const {
return allSources_.IsValid(r); }
296 std::optional<ProvenanceRange> GetProvenanceRange(
CharBlock)
const;
297 std::optional<CharBlock> GetCharBlockFromLineAndColumns(
298 int line,
int startColumn,
int endColumn)
const;
299 std::optional<std::pair<SourcePosition, SourcePosition>>
301 std::optional<CharBlock> GetCharBlock(ProvenanceRange)
const;
302 void Dump(llvm::raw_ostream &)
const;
312 std::list<CookedSource> cooked_;
313 std::map<CharBlock, const CookedSource &, CharBlockPointerComparator> index_;