provenance.cpp
18.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
//===-- lib/Parser/provenance.cpp -----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "flang/Parser/provenance.h"
#include "flang/Common/idioms.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <utility>
namespace Fortran::parser {
ProvenanceRangeToOffsetMappings::ProvenanceRangeToOffsetMappings() {}
ProvenanceRangeToOffsetMappings::~ProvenanceRangeToOffsetMappings() {}
void ProvenanceRangeToOffsetMappings::Put(
ProvenanceRange range, std::size_t offset) {
auto fromTo{map_.equal_range(range)};
for (auto iter{fromTo.first}; iter != fromTo.second; ++iter) {
if (range == iter->first) {
iter->second = std::min(offset, iter->second);
return;
}
}
if (fromTo.second != map_.end()) {
map_.emplace_hint(fromTo.second, range, offset);
} else {
map_.emplace(range, offset);
}
}
std::optional<std::size_t> ProvenanceRangeToOffsetMappings::Map(
ProvenanceRange range) const {
auto fromTo{map_.equal_range(range)};
std::optional<std::size_t> result;
for (auto iter{fromTo.first}; iter != fromTo.second; ++iter) {
ProvenanceRange that{iter->first};
if (that.Contains(range)) {
std::size_t offset{iter->second + that.MemberOffset(range.start())};
if (!result || offset < *result) {
result = offset;
}
}
}
return result;
}
bool ProvenanceRangeToOffsetMappings::WhollyPrecedes::operator()(
ProvenanceRange before, ProvenanceRange after) const {
return before.start() + before.size() <= after.start();
}
void OffsetToProvenanceMappings::clear() { provenanceMap_.clear(); }
void OffsetToProvenanceMappings::swap(OffsetToProvenanceMappings &that) {
provenanceMap_.swap(that.provenanceMap_);
}
void OffsetToProvenanceMappings::shrink_to_fit() {
provenanceMap_.shrink_to_fit();
}
std::size_t OffsetToProvenanceMappings::SizeInBytes() const {
if (provenanceMap_.empty()) {
return 0;
} else {
const ContiguousProvenanceMapping &last{provenanceMap_.back()};
return last.start + last.range.size();
}
}
void OffsetToProvenanceMappings::Put(ProvenanceRange range) {
if (provenanceMap_.empty()) {
provenanceMap_.push_back({0, range});
} else {
ContiguousProvenanceMapping &last{provenanceMap_.back()};
if (!last.range.AnnexIfPredecessor(range)) {
provenanceMap_.push_back({last.start + last.range.size(), range});
}
}
}
void OffsetToProvenanceMappings::Put(const OffsetToProvenanceMappings &that) {
for (const auto &map : that.provenanceMap_) {
Put(map.range);
}
}
ProvenanceRange OffsetToProvenanceMappings::Map(std::size_t at) const {
// CHECK(!provenanceMap_.empty());
std::size_t low{0}, count{provenanceMap_.size()};
while (count > 1) {
std::size_t mid{low + (count >> 1)};
if (provenanceMap_[mid].start > at) {
count = mid - low;
} else {
count -= mid - low;
low = mid;
}
}
std::size_t offset{at - provenanceMap_[low].start};
return provenanceMap_[low].range.Suffix(offset);
}
void OffsetToProvenanceMappings::RemoveLastBytes(std::size_t bytes) {
for (; bytes > 0; provenanceMap_.pop_back()) {
CHECK(!provenanceMap_.empty());
ContiguousProvenanceMapping &last{provenanceMap_.back()};
std::size_t chunk{last.range.size()};
if (bytes < chunk) {
last.range = last.range.Prefix(chunk - bytes);
break;
}
bytes -= chunk;
}
}
ProvenanceRangeToOffsetMappings OffsetToProvenanceMappings::Invert(
const AllSources &allSources) const {
ProvenanceRangeToOffsetMappings result;
for (const auto &contig : provenanceMap_) {
ProvenanceRange range{contig.range};
while (!range.empty()) {
ProvenanceRange source{allSources.IntersectionWithSourceFiles(range)};
if (source.empty()) {
break;
}
result.Put(
source, contig.start + contig.range.MemberOffset(source.start()));
Provenance after{source.NextAfter()};
if (range.Contains(after)) {
range = range.Suffix(range.MemberOffset(after));
} else {
break;
}
}
}
return result;
}
AllSources::AllSources() : range_{1, 1} {
// Start the origin_ array with a dummy entry that has a forced provenance,
// so that provenance offset 0 remains reserved as an uninitialized
// value.
origin_.emplace_back(range_, std::string{'?'});
}
AllSources::~AllSources() {}
const char &AllSources::operator[](Provenance at) const {
const Origin &origin{MapToOrigin(at)};
return origin[origin.covers.MemberOffset(at)];
}
void AllSources::PushSearchPathDirectory(std::string directory) {
// gfortran and ifort append to current path, PGI prepends
searchPath_.push_back(directory);
}
std::string AllSources::PopSearchPathDirectory() {
std::string directory{searchPath_.back()};
searchPath_.pop_back();
return directory;
}
const SourceFile *AllSources::Open(std::string path, llvm::raw_ostream &error) {
std::unique_ptr<SourceFile> source{std::make_unique<SourceFile>(encoding_)};
if (source->Open(LocateSourceFile(path, searchPath_), error)) {
return ownedSourceFiles_.emplace_back(std::move(source)).get();
} else {
return nullptr;
}
}
const SourceFile *AllSources::ReadStandardInput(llvm::raw_ostream &error) {
std::unique_ptr<SourceFile> source{std::make_unique<SourceFile>(encoding_)};
if (source->ReadStandardInput(error)) {
return ownedSourceFiles_.emplace_back(std::move(source)).get();
}
return nullptr;
}
ProvenanceRange AllSources::AddIncludedFile(
const SourceFile &source, ProvenanceRange from, bool isModule) {
ProvenanceRange covers{range_.NextAfter(), source.bytes()};
CHECK(range_.AnnexIfPredecessor(covers));
CHECK(origin_.back().covers.ImmediatelyPrecedes(covers));
origin_.emplace_back(covers, source, from, isModule);
return covers;
}
ProvenanceRange AllSources::AddMacroCall(
ProvenanceRange def, ProvenanceRange use, const std::string &expansion) {
ProvenanceRange covers{range_.NextAfter(), expansion.size()};
CHECK(range_.AnnexIfPredecessor(covers));
CHECK(origin_.back().covers.ImmediatelyPrecedes(covers));
origin_.emplace_back(covers, def, use, expansion);
return covers;
}
ProvenanceRange AllSources::AddCompilerInsertion(std::string text) {
ProvenanceRange covers{range_.NextAfter(), text.size()};
CHECK(range_.AnnexIfPredecessor(covers));
CHECK(origin_.back().covers.ImmediatelyPrecedes(covers));
origin_.emplace_back(covers, text);
return covers;
}
void AllSources::EmitMessage(llvm::raw_ostream &o,
const std::optional<ProvenanceRange> &range, const std::string &message,
bool echoSourceLine) const {
if (!range) {
o << message << '\n';
return;
}
CHECK(IsValid(*range));
const Origin &origin{MapToOrigin(range->start())};
std::visit(
common::visitors{
[&](const Inclusion &inc) {
o << inc.source.path();
std::size_t offset{origin.covers.MemberOffset(range->start())};
SourcePosition pos{inc.source.FindOffsetLineAndColumn(offset)};
o << ':' << pos.line << ':' << pos.column;
o << ": " << message << '\n';
if (echoSourceLine) {
const char *text{inc.source.content().data() +
inc.source.GetLineStartOffset(pos.line)};
o << " ";
for (const char *p{text}; *p != '\n'; ++p) {
o << *p;
}
o << "\n ";
for (int j{1}; j < pos.column; ++j) {
char ch{text[j - 1]};
o << (ch == '\t' ? '\t' : ' ');
}
o << '^';
if (range->size() > 1) {
auto last{range->start() + range->size() - 1};
if (&MapToOrigin(last) == &origin) {
auto endOffset{origin.covers.MemberOffset(last)};
auto endPos{inc.source.FindOffsetLineAndColumn(endOffset)};
if (pos.line == endPos.line) {
for (int j{pos.column}; j < endPos.column; ++j) {
o << '^';
}
}
}
}
o << '\n';
}
if (IsValid(origin.replaces)) {
EmitMessage(o, origin.replaces,
inc.isModule ? "used here"s : "included here"s,
echoSourceLine);
}
},
[&](const Macro &mac) {
EmitMessage(o, origin.replaces, message, echoSourceLine);
EmitMessage(
o, mac.definition, "in a macro defined here", echoSourceLine);
if (echoSourceLine) {
o << "that expanded to:\n " << mac.expansion << "\n ";
for (std::size_t j{0};
origin.covers.OffsetMember(j) < range->start(); ++j) {
o << (mac.expansion[j] == '\t' ? '\t' : ' ');
}
o << "^\n";
}
},
[&](const CompilerInsertion &) { o << message << '\n'; },
},
origin.u);
}
const SourceFile *AllSources::GetSourceFile(
Provenance at, std::size_t *offset) const {
const Origin &origin{MapToOrigin(at)};
return std::visit(common::visitors{
[&](const Inclusion &inc) {
if (offset) {
*offset = origin.covers.MemberOffset(at);
}
return &inc.source;
},
[&](const Macro &) {
return GetSourceFile(origin.replaces.start(), offset);
},
[offset](const CompilerInsertion &) {
if (offset) {
*offset = 0;
}
return static_cast<const SourceFile *>(nullptr);
},
},
origin.u);
}
const char *AllSources::GetSource(ProvenanceRange range) const {
Provenance start{range.start()};
const Origin &origin{MapToOrigin(start)};
return origin.covers.Contains(range)
? &origin[origin.covers.MemberOffset(start)]
: nullptr;
}
std::optional<SourcePosition> AllSources::GetSourcePosition(
Provenance prov) const {
const Origin &origin{MapToOrigin(prov)};
if (const auto *inc{std::get_if<Inclusion>(&origin.u)}) {
std::size_t offset{origin.covers.MemberOffset(prov)};
return inc->source.FindOffsetLineAndColumn(offset);
} else {
return std::nullopt;
}
}
std::optional<ProvenanceRange> AllSources::GetFirstFileProvenance() const {
for (const auto &origin : origin_) {
if (std::holds_alternative<Inclusion>(origin.u)) {
return origin.covers;
}
}
return std::nullopt;
}
std::string AllSources::GetPath(Provenance at) const {
const SourceFile *source{GetSourceFile(at)};
return source ? source->path() : ""s;
}
int AllSources::GetLineNumber(Provenance at) const {
std::size_t offset{0};
const SourceFile *source{GetSourceFile(at, &offset)};
return source ? source->FindOffsetLineAndColumn(offset).line : 0;
}
Provenance AllSources::CompilerInsertionProvenance(char ch) {
auto iter{compilerInsertionProvenance_.find(ch)};
if (iter != compilerInsertionProvenance_.end()) {
return iter->second;
}
ProvenanceRange newCharRange{AddCompilerInsertion(std::string{ch})};
Provenance newCharProvenance{newCharRange.start()};
compilerInsertionProvenance_.insert(std::make_pair(ch, newCharProvenance));
return newCharProvenance;
}
ProvenanceRange AllSources::IntersectionWithSourceFiles(
ProvenanceRange range) const {
if (range.empty()) {
return {};
} else {
const Origin &origin{MapToOrigin(range.start())};
if (std::holds_alternative<Inclusion>(origin.u)) {
return range.Intersection(origin.covers);
} else {
auto skip{
origin.covers.size() - origin.covers.MemberOffset(range.start())};
return IntersectionWithSourceFiles(range.Suffix(skip));
}
}
}
AllSources::Origin::Origin(ProvenanceRange r, const SourceFile &source)
: u{Inclusion{source}}, covers{r} {}
AllSources::Origin::Origin(ProvenanceRange r, const SourceFile &included,
ProvenanceRange from, bool isModule)
: u{Inclusion{included, isModule}}, covers{r}, replaces{from} {}
AllSources::Origin::Origin(ProvenanceRange r, ProvenanceRange def,
ProvenanceRange use, const std::string &expansion)
: u{Macro{def, expansion}}, covers{r}, replaces{use} {}
AllSources::Origin::Origin(ProvenanceRange r, const std::string &text)
: u{CompilerInsertion{text}}, covers{r} {}
const char &AllSources::Origin::operator[](std::size_t n) const {
return std::visit(
common::visitors{
[n](const Inclusion &inc) -> const char & {
return inc.source.content()[n];
},
[n](const Macro &mac) -> const char & { return mac.expansion[n]; },
[n](const CompilerInsertion &ins) -> const char & {
return ins.text[n];
},
},
u);
}
const AllSources::Origin &AllSources::MapToOrigin(Provenance at) const {
CHECK(range_.Contains(at));
std::size_t low{0}, count{origin_.size()};
while (count > 1) {
std::size_t mid{low + (count >> 1)};
if (at < origin_[mid].covers.start()) {
count = mid - low;
} else {
count -= mid - low;
low = mid;
}
}
CHECK(origin_[low].covers.Contains(at));
return origin_[low];
}
std::optional<ProvenanceRange> CookedSource::GetProvenanceRange(
CharBlock cookedRange) const {
if (!AsCharBlock().Contains(cookedRange)) {
return std::nullopt;
}
ProvenanceRange first{provenanceMap_.Map(cookedRange.begin() - &data_[0])};
if (cookedRange.size() <= first.size()) {
return first.Prefix(cookedRange.size());
}
ProvenanceRange last{provenanceMap_.Map(cookedRange.end() - &data_[0])};
return {ProvenanceRange{first.start(), last.start() - first.start()}};
}
std::optional<CharBlock> CookedSource::GetCharBlock(
ProvenanceRange range) const {
CHECK(!invertedMap_.empty() &&
"CompileProvenanceRangeToOffsetMappings not called");
if (auto to{invertedMap_.Map(range)}) {
return CharBlock{data_.c_str() + *to, range.size()};
} else {
return std::nullopt;
}
}
std::size_t CookedSource::BufferedBytes() const { return buffer_.bytes(); }
void CookedSource::Marshal(AllSources &allSources) {
CHECK(provenanceMap_.SizeInBytes() == buffer_.bytes());
provenanceMap_.Put(allSources.AddCompilerInsertion("(after end of source)"));
data_ = buffer_.Marshal();
buffer_.clear();
}
void CookedSource::CompileProvenanceRangeToOffsetMappings(
AllSources &allSources) {
if (invertedMap_.empty()) {
invertedMap_ = provenanceMap_.Invert(allSources);
}
}
static void DumpRange(llvm::raw_ostream &o, const ProvenanceRange &r) {
o << "[" << r.start().offset() << ".." << r.Last().offset() << "] ("
<< r.size() << " bytes)";
}
llvm::raw_ostream &ProvenanceRangeToOffsetMappings::Dump(
llvm::raw_ostream &o) const {
for (const auto &m : map_) {
o << "provenances ";
DumpRange(o, m.first);
o << " -> offsets [" << m.second << ".." << (m.second + m.first.size() - 1)
<< "]\n";
}
return o;
}
llvm::raw_ostream &OffsetToProvenanceMappings::Dump(
llvm::raw_ostream &o) const {
for (const ContiguousProvenanceMapping &m : provenanceMap_) {
std::size_t n{m.range.size()};
o << "offsets [" << m.start << ".." << (m.start + n - 1)
<< "] -> provenances ";
DumpRange(o, m.range);
o << '\n';
}
return o;
}
llvm::raw_ostream &AllSources::Dump(llvm::raw_ostream &o) const {
o << "AllSources range_ ";
DumpRange(o, range_);
o << '\n';
for (const Origin &m : origin_) {
o << " ";
DumpRange(o, m.covers);
o << " -> ";
std::visit(common::visitors{
[&](const Inclusion &inc) {
if (inc.isModule) {
o << "module ";
}
o << "file " << inc.source.path();
},
[&](const Macro &mac) { o << "macro " << mac.expansion; },
[&](const CompilerInsertion &ins) {
o << "compiler '" << ins.text << '\'';
if (ins.text.length() == 1) {
int ch = ins.text[0];
o << "(0x";
o.write_hex(ch & 0xff) << ")";
}
},
},
m.u);
if (IsValid(m.replaces)) {
o << " replaces ";
DumpRange(o, m.replaces);
}
o << '\n';
}
return o;
}
llvm::raw_ostream &CookedSource::Dump(llvm::raw_ostream &o) const {
o << "CookedSource::provenanceMap_:\n";
provenanceMap_.Dump(o);
o << "CookedSource::invertedMap_:\n";
invertedMap_.Dump(o);
return o;
}
AllCookedSources::AllCookedSources(AllSources &s) : allSources_{s} {}
AllCookedSources::~AllCookedSources() {}
CookedSource &AllCookedSources::NewCookedSource() {
return cooked_.emplace_back();
}
std::optional<ProvenanceRange> AllCookedSources::GetProvenanceRange(
CharBlock cb) const {
if (const CookedSource * c{Find(cb)}) {
return c->GetProvenanceRange(cb);
} else {
return std::nullopt;
}
}
std::optional<CharBlock> AllCookedSources::GetCharBlockFromLineAndColumns(
int line, int startColumn, int endColumn) const {
// 2nd column is exclusive, meaning it is target column + 1.
CHECK(line > 0 && startColumn > 0 && endColumn > 0);
CHECK(startColumn < endColumn);
auto provenanceStart{allSources_.GetFirstFileProvenance().value().start()};
if (auto sourceFile{allSources_.GetSourceFile(provenanceStart)}) {
CHECK(line <= static_cast<int>(sourceFile->lines()));
return GetCharBlock(ProvenanceRange(sourceFile->GetLineStartOffset(line) +
provenanceStart.offset() + startColumn - 1,
endColumn - startColumn));
}
return std::nullopt;
}
std::optional<std::pair<SourcePosition, SourcePosition>>
AllCookedSources::GetSourcePositionRange(CharBlock cookedRange) const {
if (auto range{GetProvenanceRange(cookedRange)}) {
if (auto firstOffset{allSources_.GetSourcePosition(range->start())}) {
if (auto secondOffset{
allSources_.GetSourcePosition(range->start() + range->size())}) {
return std::pair{*firstOffset, *secondOffset};
}
}
}
return std::nullopt;
}
std::optional<CharBlock> AllCookedSources::GetCharBlock(
ProvenanceRange range) const {
for (const auto &c : cooked_) {
if (auto result{c.GetCharBlock(range)}) {
return result;
}
}
return nullptr;
}
void AllCookedSources::Dump(llvm::raw_ostream &o) const {
o << "AllSources:\n";
allSources_.Dump(o);
for (const auto &c : cooked_) {
c.Dump(o);
}
}
} // namespace Fortran::parser