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() + 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( 285 common::visitors{ 286 [&](const Inclusion &inc) { 287 if (offset) { 288 *offset = origin.covers.MemberOffset(at); 289 } 290 return &inc.source; 291 }, 292 [&](const Macro &) { 293 return GetSourceFile(origin.replaces.start(), offset); 294 }, 295 [offset](const CompilerInsertion &) { 296 if (offset) { 297 *offset = 0; 298 } 299 return static_cast<const SourceFile *>(nullptr); 300 }, 301 }, 302 origin.u); 303 } 304 305 std::optional<SourcePosition> AllSources::GetSourcePosition( 306 Provenance prov) const { 307 const Origin &origin{MapToOrigin(prov)}; 308 if (const auto *inc{std::get_if<Inclusion>(&origin.u)}) { 309 std::size_t offset{origin.covers.MemberOffset(prov)}; 310 return inc->source.FindOffsetLineAndColumn(offset); 311 } else { 312 return std::nullopt; 313 } 314 } 315 316 std::optional<ProvenanceRange> AllSources::GetFirstFileProvenance() const { 317 for (const auto &origin : origin_) { 318 if (std::holds_alternative<Inclusion>(origin.u)) { 319 return origin.covers; 320 } 321 } 322 return std::nullopt; 323 } 324 325 std::string AllSources::GetPath(Provenance at) const { 326 const SourceFile *source{GetSourceFile(at)}; 327 return source ? source->path() : ""s; 328 } 329 330 int AllSources::GetLineNumber(Provenance at) const { 331 std::size_t offset{0}; 332 const SourceFile *source{GetSourceFile(at, &offset)}; 333 return source ? source->FindOffsetLineAndColumn(offset).line : 0; 334 } 335 336 Provenance AllSources::CompilerInsertionProvenance(char ch) { 337 auto iter{compilerInsertionProvenance_.find(ch)}; 338 if (iter != compilerInsertionProvenance_.end()) { 339 return iter->second; 340 } 341 ProvenanceRange newCharRange{AddCompilerInsertion(std::string{ch})}; 342 Provenance newCharProvenance{newCharRange.start()}; 343 compilerInsertionProvenance_.insert(std::make_pair(ch, newCharProvenance)); 344 return newCharProvenance; 345 } 346 347 ProvenanceRange AllSources::IntersectionWithSourceFiles( 348 ProvenanceRange range) const { 349 if (range.empty()) { 350 return {}; 351 } else { 352 const Origin &origin{MapToOrigin(range.start())}; 353 if (std::holds_alternative<Inclusion>(origin.u)) { 354 return range.Intersection(origin.covers); 355 } else { 356 auto skip{ 357 origin.covers.size() - origin.covers.MemberOffset(range.start())}; 358 return IntersectionWithSourceFiles(range.Suffix(skip)); 359 } 360 } 361 } 362 363 AllSources::Origin::Origin(ProvenanceRange r, const SourceFile &source) 364 : u{Inclusion{source}}, covers{r} {} 365 AllSources::Origin::Origin(ProvenanceRange r, const SourceFile &included, 366 ProvenanceRange from, bool isModule) 367 : u{Inclusion{included, isModule}}, covers{r}, replaces{from} {} 368 AllSources::Origin::Origin(ProvenanceRange r, ProvenanceRange def, 369 ProvenanceRange use, const std::string &expansion) 370 : u{Macro{def, expansion}}, covers{r}, replaces{use} {} 371 AllSources::Origin::Origin(ProvenanceRange r, const std::string &text) 372 : u{CompilerInsertion{text}}, covers{r} {} 373 374 const char &AllSources::Origin::operator[](std::size_t n) const { 375 return std::visit( 376 common::visitors{ 377 [n](const Inclusion &inc) -> const char & { 378 return inc.source.content()[n]; 379 }, 380 [n](const Macro &mac) -> const char & { return mac.expansion[n]; }, 381 [n](const CompilerInsertion &ins) -> const char & { 382 return ins.text[n]; 383 }, 384 }, 385 u); 386 } 387 388 const AllSources::Origin &AllSources::MapToOrigin(Provenance at) const { 389 CHECK(range_.Contains(at)); 390 std::size_t low{0}, count{origin_.size()}; 391 while (count > 1) { 392 std::size_t mid{low + (count >> 1)}; 393 if (at < origin_[mid].covers.start()) { 394 count = mid - low; 395 } else { 396 count -= mid - low; 397 low = mid; 398 } 399 } 400 CHECK(origin_[low].covers.Contains(at)); 401 return origin_[low]; 402 } 403 404 CookedSource::CookedSource(AllSources &s) : allSources_{s} {} 405 CookedSource::~CookedSource() {} 406 407 std::optional<ProvenanceRange> CookedSource::GetProvenanceRange( 408 CharBlock cookedRange) const { 409 if (!IsValid(cookedRange)) { 410 return std::nullopt; 411 } 412 ProvenanceRange first{provenanceMap_.Map(cookedRange.begin() - &data_[0])}; 413 if (cookedRange.size() <= first.size()) { 414 return first.Prefix(cookedRange.size()); 415 } 416 ProvenanceRange last{provenanceMap_.Map(cookedRange.end() - &data_[0])}; 417 return {ProvenanceRange{first.start(), last.start() - first.start()}}; 418 } 419 420 std::optional<CharBlock> CookedSource::GetCharBlockFromLineAndColumns( 421 int line, int startColumn, int endColumn) const { 422 // 2nd column is exclusive, meaning it is target column + 1. 423 CHECK(line > 0 && startColumn > 0 && endColumn > 0); 424 CHECK(startColumn < endColumn); 425 auto provenanceStart{allSources_.GetFirstFileProvenance().value().start()}; 426 if (auto sourceFile{allSources_.GetSourceFile(provenanceStart)}) { 427 CHECK(line <= static_cast<int>(sourceFile->lines())); 428 return GetCharBlock(ProvenanceRange(sourceFile->GetLineStartOffset(line) + 429 provenanceStart.offset() + startColumn - 1, 430 endColumn - startColumn)); 431 } 432 return std::nullopt; 433 } 434 435 std::optional<std::pair<SourcePosition, SourcePosition>> 436 CookedSource::GetSourcePositionRange(CharBlock cookedRange) const { 437 if (auto range{GetProvenanceRange(cookedRange)}) { 438 if (auto firstOffset{allSources_.GetSourcePosition(range->start())}) { 439 if (auto secondOffset{ 440 allSources_.GetSourcePosition(range->start() + range->size())}) { 441 return std::pair{*firstOffset, *secondOffset}; 442 } 443 } 444 } 445 return std::nullopt; 446 } 447 448 std::optional<CharBlock> CookedSource::GetCharBlock( 449 ProvenanceRange range) const { 450 CHECK(!invertedMap_.empty() && 451 "CompileProvenanceRangeToOffsetMappings not called"); 452 if (auto to{invertedMap_.Map(range)}) { 453 return CharBlock{data_.c_str() + *to, range.size()}; 454 } else { 455 return std::nullopt; 456 } 457 } 458 459 std::size_t CookedSource::BufferedBytes() const { return buffer_.bytes(); } 460 461 void CookedSource::Marshal() { 462 CHECK(provenanceMap_.SizeInBytes() == buffer_.bytes()); 463 provenanceMap_.Put(allSources_.AddCompilerInsertion("(after end of source)")); 464 data_ = buffer_.Marshal(); 465 buffer_.clear(); 466 } 467 468 void CookedSource::CompileProvenanceRangeToOffsetMappings() { 469 if (invertedMap_.empty()) { 470 invertedMap_ = provenanceMap_.Invert(allSources_); 471 } 472 } 473 474 static void DumpRange(llvm::raw_ostream &o, const ProvenanceRange &r) { 475 o << "[" << r.start().offset() << ".." << r.Last().offset() << "] (" 476 << r.size() << " bytes)"; 477 } 478 479 llvm::raw_ostream &ProvenanceRangeToOffsetMappings::Dump( 480 llvm::raw_ostream &o) const { 481 for (const auto &m : map_) { 482 o << "provenances "; 483 DumpRange(o, m.first); 484 o << " -> offsets [" << m.second << ".." << (m.second + m.first.size() - 1) 485 << "]\n"; 486 } 487 return o; 488 } 489 490 llvm::raw_ostream &OffsetToProvenanceMappings::Dump( 491 llvm::raw_ostream &o) const { 492 for (const ContiguousProvenanceMapping &m : provenanceMap_) { 493 std::size_t n{m.range.size()}; 494 o << "offsets [" << m.start << ".." << (m.start + n - 1) 495 << "] -> provenances "; 496 DumpRange(o, m.range); 497 o << '\n'; 498 } 499 return o; 500 } 501 502 llvm::raw_ostream &AllSources::Dump(llvm::raw_ostream &o) const { 503 o << "AllSources range_ "; 504 DumpRange(o, range_); 505 o << '\n'; 506 for (const Origin &m : origin_) { 507 o << " "; 508 DumpRange(o, m.covers); 509 o << " -> "; 510 std::visit( 511 common::visitors{ 512 [&](const Inclusion &inc) { 513 if (inc.isModule) { 514 o << "module "; 515 } 516 o << "file " << inc.source.path(); 517 }, 518 [&](const Macro &mac) { o << "macro " << mac.expansion; }, 519 [&](const CompilerInsertion &ins) { 520 o << "compiler '" << ins.text << '\''; 521 if (ins.text.length() == 1) { 522 int ch = ins.text[0]; 523 o << "(0x"; 524 o.write_hex(ch & 0xff) << ")"; 525 } 526 }, 527 }, 528 m.u); 529 if (IsValid(m.replaces)) { 530 o << " replaces "; 531 DumpRange(o, m.replaces); 532 } 533 o << '\n'; 534 } 535 return o; 536 } 537 538 llvm::raw_ostream &CookedSource::Dump(llvm::raw_ostream &o) const { 539 o << "CookedSource:\n"; 540 allSources_.Dump(o); 541 o << "CookedSource::provenanceMap_:\n"; 542 provenanceMap_.Dump(o); 543 o << "CookedSource::invertedMap_:\n"; 544 invertedMap_.Dump(o); 545 return o; 546 } 547 } 548