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);
162 std::vector<std::string> GetIncludedFilePaths()
const;
164 ProvenanceRange AddIncludedFile(
165 const SourceFile &, ProvenanceRange,
bool isModule =
false);
166 ProvenanceRange AddMacroCall(
167 ProvenanceRange def, ProvenanceRange use,
const std::string &expansion);
168 ProvenanceRange AddCompilerInsertion(std::string);
174 bool IsValid(
Provenance at)
const {
return range_.Contains(at); }
175 bool IsValid(ProvenanceRange range)
const {
176 return range.size() > 0 && range_.Contains(range);
178 void setShowColors(
bool showColors) { showColors_ = showColors; }
179 bool getShowColors()
const {
return showColors_; }
180 std::optional<ProvenanceRange> GetInclusionInfo(
181 const std::optional<ProvenanceRange> &)
const;
182 void EmitMessage(llvm::raw_ostream &,
const std::optional<ProvenanceRange> &,
183 const std::string &message,
const std::string &prefix,
184 llvm::raw_ostream::Colors color,
bool echoSourceLine =
false)
const;
186 Provenance, std::size_t *offset =
nullptr,
bool topLevel =
false)
const;
187 const char *GetSource(ProvenanceRange)
const;
188 std::optional<SourcePosition> GetSourcePosition(
Provenance)
const;
189 std::optional<ProvenanceRange> GetFirstFileProvenance()
const;
190 std::string GetPath(
Provenance,
bool topLevel =
false)
const;
192 Provenance CompilerInsertionProvenance(
char ch);
193 ProvenanceRange IntersectionWithSourceFiles(ProvenanceRange)
const;
194 llvm::raw_ostream &Dump(llvm::raw_ostream &)
const;
199 bool isModule{
false};
202 ProvenanceRange definition;
203 std::string expansion;
205 struct CompilerInsertion {
211 Origin(ProvenanceRange,
const SourceFile &, ProvenanceRange,
212 bool isModule =
false);
213 Origin(ProvenanceRange, ProvenanceRange def, ProvenanceRange use,
214 const std::string &expansion);
215 Origin(ProvenanceRange,
const std::string &);
217 const char &operator[](std::size_t)
const;
219 std::variant<Inclusion, Macro, CompilerInsertion> u;
220 ProvenanceRange covers, replaces;
226 std::vector<Origin> origin_;
227 ProvenanceRange range_;
228 std::map<char, Provenance> compilerInsertionProvenance_;
229 std::vector<std::unique_ptr<SourceFile>> ownedSourceFiles_;
230 std::list<std::string> searchPath_;
231 Encoding encoding_{Encoding::UTF_8};
232 bool showColors_{
false};
240 explicit CookedSource(
AllSources &allSources) : allSources_{allSources} {};
242 int number()
const {
return number_; }
243 void set_number(
int n) { number_ = n; }
246 std::optional<ProvenanceRange> GetProvenanceRange(
CharBlock)
const;
247 std::optional<CharBlock> GetCharBlock(ProvenanceRange)
const;
251 std::size_t Put(
const char *data, std::size_t bytes) {
252 return buffer_.Put(data, bytes);
254 std::size_t Put(
const std::string &s) {
return buffer_.Put(s); }
255 std::size_t Put(
char ch) {
return buffer_.Put(&ch, 1); }
257 provenanceMap_.Put(ProvenanceRange{p, 1});
258 return buffer_.Put(&ch, 1);
261 void PutProvenance(
Provenance p) { provenanceMap_.Put(ProvenanceRange{p}); }
262 void PutProvenance(ProvenanceRange pr) { provenanceMap_.Put(pr); }
264 provenanceMap_.Put(pm);
267 void MarkPossibleFixedFormContinuation() {
268 possibleFixedFormContinuations_.push_back(BufferedBytes());
271 std::size_t BufferedBytes()
const;
273 void CompileProvenanceRangeToOffsetMappings(
AllSources &);
274 llvm::raw_ostream &Dump(llvm::raw_ostream &)
const;
283 std::list<std::size_t> possibleFixedFormContinuations_;
286class AllCookedSources {
291 AllSources &allSources() {
return allSources_; }
292 const AllSources &allSources()
const {
return allSources_; }
299 bool IsValid(ProvenanceRange r)
const {
return allSources_.IsValid(r); }
301 std::optional<ProvenanceRange> GetProvenanceRange(
CharBlock)
const;
302 std::optional<CharBlock> GetCharBlockFromLineAndColumns(
303 int line,
int startColumn,
int endColumn)
const;
304 std::optional<std::pair<SourcePosition, SourcePosition>>
306 std::optional<CharBlock> GetCharBlock(ProvenanceRange)
const;
307 void Dump(llvm::raw_ostream &)
const;
317 std::list<CookedSource> cooked_;
318 std::map<CharBlock, const CookedSource &, CharBlockPointerComparator> index_;