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