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