1 //===-- ASTReader.cpp - AST File Reader ----------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the ASTReader class, which reads AST files. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/Serialization/ASTReader.h" 15 #include "ASTCommon.h" 16 #include "ASTReaderInternals.h" 17 #include "clang/AST/ASTConsumer.h" 18 #include "clang/AST/ASTContext.h" 19 #include "clang/AST/DeclTemplate.h" 20 #include "clang/AST/Expr.h" 21 #include "clang/AST/ExprCXX.h" 22 #include "clang/Frontend/PCHContainerOperations.h" 23 #include "clang/AST/ASTMutationListener.h" 24 #include "clang/AST/NestedNameSpecifier.h" 25 #include "clang/AST/Type.h" 26 #include "clang/AST/TypeLocVisitor.h" 27 #include "clang/Basic/DiagnosticOptions.h" 28 #include "clang/Basic/FileManager.h" 29 #include "clang/Basic/SourceManager.h" 30 #include "clang/Basic/SourceManagerInternals.h" 31 #include "clang/Basic/TargetInfo.h" 32 #include "clang/Basic/TargetOptions.h" 33 #include "clang/Basic/Version.h" 34 #include "clang/Basic/VersionTuple.h" 35 #include "clang/Frontend/Utils.h" 36 #include "clang/Lex/HeaderSearch.h" 37 #include "clang/Lex/HeaderSearchOptions.h" 38 #include "clang/Lex/MacroInfo.h" 39 #include "clang/Lex/PreprocessingRecord.h" 40 #include "clang/Lex/Preprocessor.h" 41 #include "clang/Lex/PreprocessorOptions.h" 42 #include "clang/Sema/Scope.h" 43 #include "clang/Sema/Sema.h" 44 #include "clang/Serialization/ASTDeserializationListener.h" 45 #include "clang/Serialization/GlobalModuleIndex.h" 46 #include "clang/Serialization/ModuleManager.h" 47 #include "clang/Serialization/SerializationDiagnostic.h" 48 #include "llvm/ADT/Hashing.h" 49 #include "llvm/ADT/StringExtras.h" 50 #include "llvm/Bitcode/BitstreamReader.h" 51 #include "llvm/Support/Compression.h" 52 #include "llvm/Support/ErrorHandling.h" 53 #include "llvm/Support/FileSystem.h" 54 #include "llvm/Support/MemoryBuffer.h" 55 #include "llvm/Support/Path.h" 56 #include "llvm/Support/SaveAndRestore.h" 57 #include "llvm/Support/raw_ostream.h" 58 #include <algorithm> 59 #include <cstdio> 60 #include <iterator> 61 #include <system_error> 62 63 using namespace clang; 64 using namespace clang::serialization; 65 using namespace clang::serialization::reader; 66 using llvm::BitstreamCursor; 67 68 69 //===----------------------------------------------------------------------===// 70 // ChainedASTReaderListener implementation 71 //===----------------------------------------------------------------------===// 72 73 bool 74 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) { 75 return First->ReadFullVersionInformation(FullVersion) || 76 Second->ReadFullVersionInformation(FullVersion); 77 } 78 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) { 79 First->ReadModuleName(ModuleName); 80 Second->ReadModuleName(ModuleName); 81 } 82 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) { 83 First->ReadModuleMapFile(ModuleMapPath); 84 Second->ReadModuleMapFile(ModuleMapPath); 85 } 86 bool 87 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts, 88 bool Complain, 89 bool AllowCompatibleDifferences) { 90 return First->ReadLanguageOptions(LangOpts, Complain, 91 AllowCompatibleDifferences) || 92 Second->ReadLanguageOptions(LangOpts, Complain, 93 AllowCompatibleDifferences); 94 } 95 bool ChainedASTReaderListener::ReadTargetOptions( 96 const TargetOptions &TargetOpts, bool Complain, 97 bool AllowCompatibleDifferences) { 98 return First->ReadTargetOptions(TargetOpts, Complain, 99 AllowCompatibleDifferences) || 100 Second->ReadTargetOptions(TargetOpts, Complain, 101 AllowCompatibleDifferences); 102 } 103 bool ChainedASTReaderListener::ReadDiagnosticOptions( 104 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 105 return First->ReadDiagnosticOptions(DiagOpts, Complain) || 106 Second->ReadDiagnosticOptions(DiagOpts, Complain); 107 } 108 bool 109 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts, 110 bool Complain) { 111 return First->ReadFileSystemOptions(FSOpts, Complain) || 112 Second->ReadFileSystemOptions(FSOpts, Complain); 113 } 114 115 bool ChainedASTReaderListener::ReadHeaderSearchOptions( 116 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath, 117 bool Complain) { 118 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 119 Complain) || 120 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 121 Complain); 122 } 123 bool ChainedASTReaderListener::ReadPreprocessorOptions( 124 const PreprocessorOptions &PPOpts, bool Complain, 125 std::string &SuggestedPredefines) { 126 return First->ReadPreprocessorOptions(PPOpts, Complain, 127 SuggestedPredefines) || 128 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines); 129 } 130 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M, 131 unsigned Value) { 132 First->ReadCounter(M, Value); 133 Second->ReadCounter(M, Value); 134 } 135 bool ChainedASTReaderListener::needsInputFileVisitation() { 136 return First->needsInputFileVisitation() || 137 Second->needsInputFileVisitation(); 138 } 139 bool ChainedASTReaderListener::needsSystemInputFileVisitation() { 140 return First->needsSystemInputFileVisitation() || 141 Second->needsSystemInputFileVisitation(); 142 } 143 void ChainedASTReaderListener::visitModuleFile(StringRef Filename, 144 ModuleKind Kind) { 145 First->visitModuleFile(Filename, Kind); 146 Second->visitModuleFile(Filename, Kind); 147 } 148 bool ChainedASTReaderListener::visitInputFile(StringRef Filename, 149 bool isSystem, 150 bool isOverridden, 151 bool isExplicitModule) { 152 bool Continue = false; 153 if (First->needsInputFileVisitation() && 154 (!isSystem || First->needsSystemInputFileVisitation())) 155 Continue |= First->visitInputFile(Filename, isSystem, isOverridden, 156 isExplicitModule); 157 if (Second->needsInputFileVisitation() && 158 (!isSystem || Second->needsSystemInputFileVisitation())) 159 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden, 160 isExplicitModule); 161 return Continue; 162 } 163 164 void ChainedASTReaderListener::readModuleFileExtension( 165 const ModuleFileExtensionMetadata &Metadata) { 166 First->readModuleFileExtension(Metadata); 167 Second->readModuleFileExtension(Metadata); 168 } 169 170 //===----------------------------------------------------------------------===// 171 // PCH validator implementation 172 //===----------------------------------------------------------------------===// 173 174 ASTReaderListener::~ASTReaderListener() {} 175 176 /// \brief Compare the given set of language options against an existing set of 177 /// language options. 178 /// 179 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 180 /// \param AllowCompatibleDifferences If true, differences between compatible 181 /// language options will be permitted. 182 /// 183 /// \returns true if the languagae options mis-match, false otherwise. 184 static bool checkLanguageOptions(const LangOptions &LangOpts, 185 const LangOptions &ExistingLangOpts, 186 DiagnosticsEngine *Diags, 187 bool AllowCompatibleDifferences = true) { 188 #define LANGOPT(Name, Bits, Default, Description) \ 189 if (ExistingLangOpts.Name != LangOpts.Name) { \ 190 if (Diags) \ 191 Diags->Report(diag::err_pch_langopt_mismatch) \ 192 << Description << LangOpts.Name << ExistingLangOpts.Name; \ 193 return true; \ 194 } 195 196 #define VALUE_LANGOPT(Name, Bits, Default, Description) \ 197 if (ExistingLangOpts.Name != LangOpts.Name) { \ 198 if (Diags) \ 199 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 200 << Description; \ 201 return true; \ 202 } 203 204 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 205 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \ 206 if (Diags) \ 207 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 208 << Description; \ 209 return true; \ 210 } 211 212 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \ 213 if (!AllowCompatibleDifferences) \ 214 LANGOPT(Name, Bits, Default, Description) 215 216 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \ 217 if (!AllowCompatibleDifferences) \ 218 ENUM_LANGOPT(Name, Bits, Default, Description) 219 220 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \ 221 if (!AllowCompatibleDifferences) \ 222 VALUE_LANGOPT(Name, Bits, Default, Description) 223 224 #define BENIGN_LANGOPT(Name, Bits, Default, Description) 225 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) 226 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description) 227 #include "clang/Basic/LangOptions.def" 228 229 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) { 230 if (Diags) 231 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features"; 232 return true; 233 } 234 235 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) { 236 if (Diags) 237 Diags->Report(diag::err_pch_langopt_value_mismatch) 238 << "target Objective-C runtime"; 239 return true; 240 } 241 242 if (ExistingLangOpts.CommentOpts.BlockCommandNames != 243 LangOpts.CommentOpts.BlockCommandNames) { 244 if (Diags) 245 Diags->Report(diag::err_pch_langopt_value_mismatch) 246 << "block command names"; 247 return true; 248 } 249 250 return false; 251 } 252 253 /// \brief Compare the given set of target options against an existing set of 254 /// target options. 255 /// 256 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 257 /// 258 /// \returns true if the target options mis-match, false otherwise. 259 static bool checkTargetOptions(const TargetOptions &TargetOpts, 260 const TargetOptions &ExistingTargetOpts, 261 DiagnosticsEngine *Diags, 262 bool AllowCompatibleDifferences = true) { 263 #define CHECK_TARGET_OPT(Field, Name) \ 264 if (TargetOpts.Field != ExistingTargetOpts.Field) { \ 265 if (Diags) \ 266 Diags->Report(diag::err_pch_targetopt_mismatch) \ 267 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \ 268 return true; \ 269 } 270 271 // The triple and ABI must match exactly. 272 CHECK_TARGET_OPT(Triple, "target"); 273 CHECK_TARGET_OPT(ABI, "target ABI"); 274 275 // We can tolerate different CPUs in many cases, notably when one CPU 276 // supports a strict superset of another. When allowing compatible 277 // differences skip this check. 278 if (!AllowCompatibleDifferences) 279 CHECK_TARGET_OPT(CPU, "target CPU"); 280 281 #undef CHECK_TARGET_OPT 282 283 // Compare feature sets. 284 SmallVector<StringRef, 4> ExistingFeatures( 285 ExistingTargetOpts.FeaturesAsWritten.begin(), 286 ExistingTargetOpts.FeaturesAsWritten.end()); 287 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(), 288 TargetOpts.FeaturesAsWritten.end()); 289 std::sort(ExistingFeatures.begin(), ExistingFeatures.end()); 290 std::sort(ReadFeatures.begin(), ReadFeatures.end()); 291 292 // We compute the set difference in both directions explicitly so that we can 293 // diagnose the differences differently. 294 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures; 295 std::set_difference( 296 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(), 297 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures)); 298 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(), 299 ExistingFeatures.begin(), ExistingFeatures.end(), 300 std::back_inserter(UnmatchedReadFeatures)); 301 302 // If we are allowing compatible differences and the read feature set is 303 // a strict subset of the existing feature set, there is nothing to diagnose. 304 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty()) 305 return false; 306 307 if (Diags) { 308 for (StringRef Feature : UnmatchedReadFeatures) 309 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 310 << /* is-existing-feature */ false << Feature; 311 for (StringRef Feature : UnmatchedExistingFeatures) 312 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 313 << /* is-existing-feature */ true << Feature; 314 } 315 316 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty(); 317 } 318 319 bool 320 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts, 321 bool Complain, 322 bool AllowCompatibleDifferences) { 323 const LangOptions &ExistingLangOpts = PP.getLangOpts(); 324 return checkLanguageOptions(LangOpts, ExistingLangOpts, 325 Complain ? &Reader.Diags : nullptr, 326 AllowCompatibleDifferences); 327 } 328 329 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts, 330 bool Complain, 331 bool AllowCompatibleDifferences) { 332 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts(); 333 return checkTargetOptions(TargetOpts, ExistingTargetOpts, 334 Complain ? &Reader.Diags : nullptr, 335 AllowCompatibleDifferences); 336 } 337 338 namespace { 339 typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> > 340 MacroDefinitionsMap; 341 typedef llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > 342 DeclsMap; 343 } 344 345 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags, 346 DiagnosticsEngine &Diags, 347 bool Complain) { 348 typedef DiagnosticsEngine::Level Level; 349 350 // Check current mappings for new -Werror mappings, and the stored mappings 351 // for cases that were explicitly mapped to *not* be errors that are now 352 // errors because of options like -Werror. 353 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags }; 354 355 for (DiagnosticsEngine *MappingSource : MappingSources) { 356 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) { 357 diag::kind DiagID = DiagIDMappingPair.first; 358 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation()); 359 if (CurLevel < DiagnosticsEngine::Error) 360 continue; // not significant 361 Level StoredLevel = 362 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation()); 363 if (StoredLevel < DiagnosticsEngine::Error) { 364 if (Complain) 365 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" + 366 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str(); 367 return true; 368 } 369 } 370 } 371 372 return false; 373 } 374 375 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) { 376 diag::Severity Ext = Diags.getExtensionHandlingBehavior(); 377 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors()) 378 return true; 379 return Ext >= diag::Severity::Error; 380 } 381 382 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags, 383 DiagnosticsEngine &Diags, 384 bool IsSystem, bool Complain) { 385 // Top-level options 386 if (IsSystem) { 387 if (Diags.getSuppressSystemWarnings()) 388 return false; 389 // If -Wsystem-headers was not enabled before, be conservative 390 if (StoredDiags.getSuppressSystemWarnings()) { 391 if (Complain) 392 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers"; 393 return true; 394 } 395 } 396 397 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) { 398 if (Complain) 399 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror"; 400 return true; 401 } 402 403 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() && 404 !StoredDiags.getEnableAllWarnings()) { 405 if (Complain) 406 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror"; 407 return true; 408 } 409 410 if (isExtHandlingFromDiagsError(Diags) && 411 !isExtHandlingFromDiagsError(StoredDiags)) { 412 if (Complain) 413 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors"; 414 return true; 415 } 416 417 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain); 418 } 419 420 bool PCHValidator::ReadDiagnosticOptions( 421 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 422 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics(); 423 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs()); 424 IntrusiveRefCntPtr<DiagnosticsEngine> Diags( 425 new DiagnosticsEngine(DiagIDs, DiagOpts.get())); 426 // This should never fail, because we would have processed these options 427 // before writing them to an ASTFile. 428 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false); 429 430 ModuleManager &ModuleMgr = Reader.getModuleManager(); 431 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then"); 432 433 // If the original import came from a file explicitly generated by the user, 434 // don't check the diagnostic mappings. 435 // FIXME: currently this is approximated by checking whether this is not a 436 // module import of an implicitly-loaded module file. 437 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in 438 // the transitive closure of its imports, since unrelated modules cannot be 439 // imported until after this module finishes validation. 440 ModuleFile *TopImport = *ModuleMgr.rbegin(); 441 while (!TopImport->ImportedBy.empty()) 442 TopImport = TopImport->ImportedBy[0]; 443 if (TopImport->Kind != MK_ImplicitModule) 444 return false; 445 446 StringRef ModuleName = TopImport->ModuleName; 447 assert(!ModuleName.empty() && "diagnostic options read before module name"); 448 449 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName); 450 assert(M && "missing module"); 451 452 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that 453 // contains the union of their flags. 454 return checkDiagnosticMappings(*Diags, ExistingDiags, M->IsSystem, Complain); 455 } 456 457 /// \brief Collect the macro definitions provided by the given preprocessor 458 /// options. 459 static void 460 collectMacroDefinitions(const PreprocessorOptions &PPOpts, 461 MacroDefinitionsMap &Macros, 462 SmallVectorImpl<StringRef> *MacroNames = nullptr) { 463 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) { 464 StringRef Macro = PPOpts.Macros[I].first; 465 bool IsUndef = PPOpts.Macros[I].second; 466 467 std::pair<StringRef, StringRef> MacroPair = Macro.split('='); 468 StringRef MacroName = MacroPair.first; 469 StringRef MacroBody = MacroPair.second; 470 471 // For an #undef'd macro, we only care about the name. 472 if (IsUndef) { 473 if (MacroNames && !Macros.count(MacroName)) 474 MacroNames->push_back(MacroName); 475 476 Macros[MacroName] = std::make_pair("", true); 477 continue; 478 } 479 480 // For a #define'd macro, figure out the actual definition. 481 if (MacroName.size() == Macro.size()) 482 MacroBody = "1"; 483 else { 484 // Note: GCC drops anything following an end-of-line character. 485 StringRef::size_type End = MacroBody.find_first_of("\n\r"); 486 MacroBody = MacroBody.substr(0, End); 487 } 488 489 if (MacroNames && !Macros.count(MacroName)) 490 MacroNames->push_back(MacroName); 491 Macros[MacroName] = std::make_pair(MacroBody, false); 492 } 493 } 494 495 /// \brief Check the preprocessor options deserialized from the control block 496 /// against the preprocessor options in an existing preprocessor. 497 /// 498 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 499 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts, 500 const PreprocessorOptions &ExistingPPOpts, 501 DiagnosticsEngine *Diags, 502 FileManager &FileMgr, 503 std::string &SuggestedPredefines, 504 const LangOptions &LangOpts) { 505 // Check macro definitions. 506 MacroDefinitionsMap ASTFileMacros; 507 collectMacroDefinitions(PPOpts, ASTFileMacros); 508 MacroDefinitionsMap ExistingMacros; 509 SmallVector<StringRef, 4> ExistingMacroNames; 510 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames); 511 512 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) { 513 // Dig out the macro definition in the existing preprocessor options. 514 StringRef MacroName = ExistingMacroNames[I]; 515 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName]; 516 517 // Check whether we know anything about this macro name or not. 518 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known 519 = ASTFileMacros.find(MacroName); 520 if (Known == ASTFileMacros.end()) { 521 // FIXME: Check whether this identifier was referenced anywhere in the 522 // AST file. If so, we should reject the AST file. Unfortunately, this 523 // information isn't in the control block. What shall we do about it? 524 525 if (Existing.second) { 526 SuggestedPredefines += "#undef "; 527 SuggestedPredefines += MacroName.str(); 528 SuggestedPredefines += '\n'; 529 } else { 530 SuggestedPredefines += "#define "; 531 SuggestedPredefines += MacroName.str(); 532 SuggestedPredefines += ' '; 533 SuggestedPredefines += Existing.first.str(); 534 SuggestedPredefines += '\n'; 535 } 536 continue; 537 } 538 539 // If the macro was defined in one but undef'd in the other, we have a 540 // conflict. 541 if (Existing.second != Known->second.second) { 542 if (Diags) { 543 Diags->Report(diag::err_pch_macro_def_undef) 544 << MacroName << Known->second.second; 545 } 546 return true; 547 } 548 549 // If the macro was #undef'd in both, or if the macro bodies are identical, 550 // it's fine. 551 if (Existing.second || Existing.first == Known->second.first) 552 continue; 553 554 // The macro bodies differ; complain. 555 if (Diags) { 556 Diags->Report(diag::err_pch_macro_def_conflict) 557 << MacroName << Known->second.first << Existing.first; 558 } 559 return true; 560 } 561 562 // Check whether we're using predefines. 563 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) { 564 if (Diags) { 565 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines; 566 } 567 return true; 568 } 569 570 // Detailed record is important since it is used for the module cache hash. 571 if (LangOpts.Modules && 572 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord) { 573 if (Diags) { 574 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord; 575 } 576 return true; 577 } 578 579 // Compute the #include and #include_macros lines we need. 580 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) { 581 StringRef File = ExistingPPOpts.Includes[I]; 582 if (File == ExistingPPOpts.ImplicitPCHInclude) 583 continue; 584 585 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File) 586 != PPOpts.Includes.end()) 587 continue; 588 589 SuggestedPredefines += "#include \""; 590 SuggestedPredefines += File; 591 SuggestedPredefines += "\"\n"; 592 } 593 594 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) { 595 StringRef File = ExistingPPOpts.MacroIncludes[I]; 596 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(), 597 File) 598 != PPOpts.MacroIncludes.end()) 599 continue; 600 601 SuggestedPredefines += "#__include_macros \""; 602 SuggestedPredefines += File; 603 SuggestedPredefines += "\"\n##\n"; 604 } 605 606 return false; 607 } 608 609 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 610 bool Complain, 611 std::string &SuggestedPredefines) { 612 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts(); 613 614 return checkPreprocessorOptions(PPOpts, ExistingPPOpts, 615 Complain? &Reader.Diags : nullptr, 616 PP.getFileManager(), 617 SuggestedPredefines, 618 PP.getLangOpts()); 619 } 620 621 /// Check the header search options deserialized from the control block 622 /// against the header search options in an existing preprocessor. 623 /// 624 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 625 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 626 StringRef SpecificModuleCachePath, 627 StringRef ExistingModuleCachePath, 628 DiagnosticsEngine *Diags, 629 const LangOptions &LangOpts) { 630 if (LangOpts.Modules) { 631 if (SpecificModuleCachePath != ExistingModuleCachePath) { 632 if (Diags) 633 Diags->Report(diag::err_pch_modulecache_mismatch) 634 << SpecificModuleCachePath << ExistingModuleCachePath; 635 return true; 636 } 637 } 638 639 return false; 640 } 641 642 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 643 StringRef SpecificModuleCachePath, 644 bool Complain) { 645 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 646 PP.getHeaderSearchInfo().getModuleCachePath(), 647 Complain ? &Reader.Diags : nullptr, 648 PP.getLangOpts()); 649 } 650 651 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) { 652 PP.setCounterValue(Value); 653 } 654 655 //===----------------------------------------------------------------------===// 656 // AST reader implementation 657 //===----------------------------------------------------------------------===// 658 659 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener, 660 bool TakeOwnership) { 661 DeserializationListener = Listener; 662 OwnsDeserializationListener = TakeOwnership; 663 } 664 665 666 667 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) { 668 return serialization::ComputeHash(Sel); 669 } 670 671 672 std::pair<unsigned, unsigned> 673 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 674 using namespace llvm::support; 675 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 676 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 677 return std::make_pair(KeyLen, DataLen); 678 } 679 680 ASTSelectorLookupTrait::internal_key_type 681 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) { 682 using namespace llvm::support; 683 SelectorTable &SelTable = Reader.getContext().Selectors; 684 unsigned N = endian::readNext<uint16_t, little, unaligned>(d); 685 IdentifierInfo *FirstII = Reader.getLocalIdentifier( 686 F, endian::readNext<uint32_t, little, unaligned>(d)); 687 if (N == 0) 688 return SelTable.getNullarySelector(FirstII); 689 else if (N == 1) 690 return SelTable.getUnarySelector(FirstII); 691 692 SmallVector<IdentifierInfo *, 16> Args; 693 Args.push_back(FirstII); 694 for (unsigned I = 1; I != N; ++I) 695 Args.push_back(Reader.getLocalIdentifier( 696 F, endian::readNext<uint32_t, little, unaligned>(d))); 697 698 return SelTable.getSelector(N, Args.data()); 699 } 700 701 ASTSelectorLookupTrait::data_type 702 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d, 703 unsigned DataLen) { 704 using namespace llvm::support; 705 706 data_type Result; 707 708 Result.ID = Reader.getGlobalSelectorID( 709 F, endian::readNext<uint32_t, little, unaligned>(d)); 710 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d); 711 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d); 712 Result.InstanceBits = FullInstanceBits & 0x3; 713 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1; 714 Result.FactoryBits = FullFactoryBits & 0x3; 715 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1; 716 unsigned NumInstanceMethods = FullInstanceBits >> 3; 717 unsigned NumFactoryMethods = FullFactoryBits >> 3; 718 719 // Load instance methods 720 for (unsigned I = 0; I != NumInstanceMethods; ++I) { 721 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 722 F, endian::readNext<uint32_t, little, unaligned>(d))) 723 Result.Instance.push_back(Method); 724 } 725 726 // Load factory methods 727 for (unsigned I = 0; I != NumFactoryMethods; ++I) { 728 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 729 F, endian::readNext<uint32_t, little, unaligned>(d))) 730 Result.Factory.push_back(Method); 731 } 732 733 return Result; 734 } 735 736 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) { 737 return llvm::HashString(a); 738 } 739 740 std::pair<unsigned, unsigned> 741 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) { 742 using namespace llvm::support; 743 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 744 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 745 return std::make_pair(KeyLen, DataLen); 746 } 747 748 ASTIdentifierLookupTraitBase::internal_key_type 749 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) { 750 assert(n >= 2 && d[n-1] == '\0'); 751 return StringRef((const char*) d, n-1); 752 } 753 754 /// \brief Whether the given identifier is "interesting". 755 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II, 756 bool IsModule) { 757 return II.hadMacroDefinition() || 758 II.isPoisoned() || 759 (IsModule ? II.hasRevertedBuiltin() : II.getObjCOrBuiltinID()) || 760 II.hasRevertedTokenIDToIdentifier() || 761 (!(IsModule && Reader.getContext().getLangOpts().CPlusPlus) && 762 II.getFETokenInfo<void>()); 763 } 764 765 static bool readBit(unsigned &Bits) { 766 bool Value = Bits & 0x1; 767 Bits >>= 1; 768 return Value; 769 } 770 771 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) { 772 using namespace llvm::support; 773 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 774 return Reader.getGlobalIdentifierID(F, RawID >> 1); 775 } 776 777 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) { 778 if (!II.isFromAST()) { 779 II.setIsFromAST(); 780 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr; 781 if (isInterestingIdentifier(Reader, II, IsModule)) 782 II.setChangedSinceDeserialization(); 783 } 784 } 785 786 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k, 787 const unsigned char* d, 788 unsigned DataLen) { 789 using namespace llvm::support; 790 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 791 bool IsInteresting = RawID & 0x01; 792 793 // Wipe out the "is interesting" bit. 794 RawID = RawID >> 1; 795 796 // Build the IdentifierInfo and link the identifier ID with it. 797 IdentifierInfo *II = KnownII; 798 if (!II) { 799 II = &Reader.getIdentifierTable().getOwn(k); 800 KnownII = II; 801 } 802 markIdentifierFromAST(Reader, *II); 803 Reader.markIdentifierUpToDate(II); 804 805 IdentID ID = Reader.getGlobalIdentifierID(F, RawID); 806 if (!IsInteresting) { 807 // For uninteresting identifiers, there's nothing else to do. Just notify 808 // the reader that we've finished loading this identifier. 809 Reader.SetIdentifierInfo(ID, II); 810 return II; 811 } 812 813 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d); 814 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d); 815 bool CPlusPlusOperatorKeyword = readBit(Bits); 816 bool HasRevertedTokenIDToIdentifier = readBit(Bits); 817 bool HasRevertedBuiltin = readBit(Bits); 818 bool Poisoned = readBit(Bits); 819 bool ExtensionToken = readBit(Bits); 820 bool HadMacroDefinition = readBit(Bits); 821 822 assert(Bits == 0 && "Extra bits in the identifier?"); 823 DataLen -= 8; 824 825 // Set or check the various bits in the IdentifierInfo structure. 826 // Token IDs are read-only. 827 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier) 828 II->revertTokenIDToIdentifier(); 829 if (!F.isModule()) 830 II->setObjCOrBuiltinID(ObjCOrBuiltinID); 831 else if (HasRevertedBuiltin && II->getBuiltinID()) { 832 II->revertBuiltin(); 833 assert((II->hasRevertedBuiltin() || 834 II->getObjCOrBuiltinID() == ObjCOrBuiltinID) && 835 "Incorrect ObjC keyword or builtin ID"); 836 } 837 assert(II->isExtensionToken() == ExtensionToken && 838 "Incorrect extension token flag"); 839 (void)ExtensionToken; 840 if (Poisoned) 841 II->setIsPoisoned(true); 842 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword && 843 "Incorrect C++ operator keyword flag"); 844 (void)CPlusPlusOperatorKeyword; 845 846 // If this identifier is a macro, deserialize the macro 847 // definition. 848 if (HadMacroDefinition) { 849 uint32_t MacroDirectivesOffset = 850 endian::readNext<uint32_t, little, unaligned>(d); 851 DataLen -= 4; 852 853 Reader.addPendingMacro(II, &F, MacroDirectivesOffset); 854 } 855 856 Reader.SetIdentifierInfo(ID, II); 857 858 // Read all of the declarations visible at global scope with this 859 // name. 860 if (DataLen > 0) { 861 SmallVector<uint32_t, 4> DeclIDs; 862 for (; DataLen > 0; DataLen -= 4) 863 DeclIDs.push_back(Reader.getGlobalDeclID( 864 F, endian::readNext<uint32_t, little, unaligned>(d))); 865 Reader.SetGloballyVisibleDecls(II, DeclIDs); 866 } 867 868 return II; 869 } 870 871 DeclarationNameKey::DeclarationNameKey(DeclarationName Name) 872 : Kind(Name.getNameKind()) { 873 switch (Kind) { 874 case DeclarationName::Identifier: 875 Data = (uint64_t)Name.getAsIdentifierInfo(); 876 break; 877 case DeclarationName::ObjCZeroArgSelector: 878 case DeclarationName::ObjCOneArgSelector: 879 case DeclarationName::ObjCMultiArgSelector: 880 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr(); 881 break; 882 case DeclarationName::CXXOperatorName: 883 Data = Name.getCXXOverloadedOperator(); 884 break; 885 case DeclarationName::CXXLiteralOperatorName: 886 Data = (uint64_t)Name.getCXXLiteralIdentifier(); 887 break; 888 case DeclarationName::CXXConstructorName: 889 case DeclarationName::CXXDestructorName: 890 case DeclarationName::CXXConversionFunctionName: 891 case DeclarationName::CXXUsingDirective: 892 Data = 0; 893 break; 894 } 895 } 896 897 unsigned DeclarationNameKey::getHash() const { 898 llvm::FoldingSetNodeID ID; 899 ID.AddInteger(Kind); 900 901 switch (Kind) { 902 case DeclarationName::Identifier: 903 case DeclarationName::CXXLiteralOperatorName: 904 ID.AddString(((IdentifierInfo*)Data)->getName()); 905 break; 906 case DeclarationName::ObjCZeroArgSelector: 907 case DeclarationName::ObjCOneArgSelector: 908 case DeclarationName::ObjCMultiArgSelector: 909 ID.AddInteger(serialization::ComputeHash(Selector(Data))); 910 break; 911 case DeclarationName::CXXOperatorName: 912 ID.AddInteger((OverloadedOperatorKind)Data); 913 break; 914 case DeclarationName::CXXConstructorName: 915 case DeclarationName::CXXDestructorName: 916 case DeclarationName::CXXConversionFunctionName: 917 case DeclarationName::CXXUsingDirective: 918 break; 919 } 920 921 return ID.ComputeHash(); 922 } 923 924 ModuleFile * 925 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) { 926 using namespace llvm::support; 927 uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d); 928 return Reader.getLocalModuleFile(F, ModuleFileID); 929 } 930 931 std::pair<unsigned, unsigned> 932 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) { 933 using namespace llvm::support; 934 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 935 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 936 return std::make_pair(KeyLen, DataLen); 937 } 938 939 ASTDeclContextNameLookupTrait::internal_key_type 940 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) { 941 using namespace llvm::support; 942 943 auto Kind = (DeclarationName::NameKind)*d++; 944 uint64_t Data; 945 switch (Kind) { 946 case DeclarationName::Identifier: 947 Data = (uint64_t)Reader.getLocalIdentifier( 948 F, endian::readNext<uint32_t, little, unaligned>(d)); 949 break; 950 case DeclarationName::ObjCZeroArgSelector: 951 case DeclarationName::ObjCOneArgSelector: 952 case DeclarationName::ObjCMultiArgSelector: 953 Data = 954 (uint64_t)Reader.getLocalSelector( 955 F, endian::readNext<uint32_t, little, unaligned>( 956 d)).getAsOpaquePtr(); 957 break; 958 case DeclarationName::CXXOperatorName: 959 Data = *d++; // OverloadedOperatorKind 960 break; 961 case DeclarationName::CXXLiteralOperatorName: 962 Data = (uint64_t)Reader.getLocalIdentifier( 963 F, endian::readNext<uint32_t, little, unaligned>(d)); 964 break; 965 case DeclarationName::CXXConstructorName: 966 case DeclarationName::CXXDestructorName: 967 case DeclarationName::CXXConversionFunctionName: 968 case DeclarationName::CXXUsingDirective: 969 Data = 0; 970 break; 971 } 972 973 return DeclarationNameKey(Kind, Data); 974 } 975 976 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type, 977 const unsigned char *d, 978 unsigned DataLen, 979 data_type_builder &Val) { 980 using namespace llvm::support; 981 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) { 982 uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d); 983 Val.insert(Reader.getGlobalDeclID(F, LocalID)); 984 } 985 } 986 987 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M, 988 BitstreamCursor &Cursor, 989 uint64_t Offset, 990 DeclContext *DC) { 991 assert(Offset != 0); 992 993 SavedStreamPosition SavedPosition(Cursor); 994 Cursor.JumpToBit(Offset); 995 996 RecordData Record; 997 StringRef Blob; 998 unsigned Code = Cursor.ReadCode(); 999 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob); 1000 if (RecCode != DECL_CONTEXT_LEXICAL) { 1001 Error("Expected lexical block"); 1002 return true; 1003 } 1004 1005 assert(!isa<TranslationUnitDecl>(DC) && 1006 "expected a TU_UPDATE_LEXICAL record for TU"); 1007 // If we are handling a C++ class template instantiation, we can see multiple 1008 // lexical updates for the same record. It's important that we select only one 1009 // of them, so that field numbering works properly. Just pick the first one we 1010 // see. 1011 auto &Lex = LexicalDecls[DC]; 1012 if (!Lex.first) { 1013 Lex = std::make_pair( 1014 &M, llvm::makeArrayRef( 1015 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 1016 Blob.data()), 1017 Blob.size() / 4)); 1018 } 1019 DC->setHasExternalLexicalStorage(true); 1020 return false; 1021 } 1022 1023 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M, 1024 BitstreamCursor &Cursor, 1025 uint64_t Offset, 1026 DeclID ID) { 1027 assert(Offset != 0); 1028 1029 SavedStreamPosition SavedPosition(Cursor); 1030 Cursor.JumpToBit(Offset); 1031 1032 RecordData Record; 1033 StringRef Blob; 1034 unsigned Code = Cursor.ReadCode(); 1035 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob); 1036 if (RecCode != DECL_CONTEXT_VISIBLE) { 1037 Error("Expected visible lookup table block"); 1038 return true; 1039 } 1040 1041 // We can't safely determine the primary context yet, so delay attaching the 1042 // lookup table until we're done with recursive deserialization. 1043 auto *Data = (const unsigned char*)Blob.data(); 1044 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data}); 1045 return false; 1046 } 1047 1048 void ASTReader::Error(StringRef Msg) { 1049 Error(diag::err_fe_pch_malformed, Msg); 1050 if (Context.getLangOpts().Modules && !Diags.isDiagnosticInFlight() && 1051 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) { 1052 Diag(diag::note_module_cache_path) 1053 << PP.getHeaderSearchInfo().getModuleCachePath(); 1054 } 1055 } 1056 1057 void ASTReader::Error(unsigned DiagID, 1058 StringRef Arg1, StringRef Arg2) { 1059 if (Diags.isDiagnosticInFlight()) 1060 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2); 1061 else 1062 Diag(DiagID) << Arg1 << Arg2; 1063 } 1064 1065 //===----------------------------------------------------------------------===// 1066 // Source Manager Deserialization 1067 //===----------------------------------------------------------------------===// 1068 1069 /// \brief Read the line table in the source manager block. 1070 /// \returns true if there was an error. 1071 bool ASTReader::ParseLineTable(ModuleFile &F, 1072 const RecordData &Record) { 1073 unsigned Idx = 0; 1074 LineTableInfo &LineTable = SourceMgr.getLineTable(); 1075 1076 // Parse the file names 1077 std::map<int, int> FileIDs; 1078 for (unsigned I = 0; Record[Idx]; ++I) { 1079 // Extract the file name 1080 auto Filename = ReadPath(F, Record, Idx); 1081 FileIDs[I] = LineTable.getLineTableFilenameID(Filename); 1082 } 1083 ++Idx; 1084 1085 // Parse the line entries 1086 std::vector<LineEntry> Entries; 1087 while (Idx < Record.size()) { 1088 int FID = Record[Idx++]; 1089 assert(FID >= 0 && "Serialized line entries for non-local file."); 1090 // Remap FileID from 1-based old view. 1091 FID += F.SLocEntryBaseID - 1; 1092 1093 // Extract the line entries 1094 unsigned NumEntries = Record[Idx++]; 1095 assert(NumEntries && "no line entries for file ID"); 1096 Entries.clear(); 1097 Entries.reserve(NumEntries); 1098 for (unsigned I = 0; I != NumEntries; ++I) { 1099 unsigned FileOffset = Record[Idx++]; 1100 unsigned LineNo = Record[Idx++]; 1101 int FilenameID = FileIDs[Record[Idx++]]; 1102 SrcMgr::CharacteristicKind FileKind 1103 = (SrcMgr::CharacteristicKind)Record[Idx++]; 1104 unsigned IncludeOffset = Record[Idx++]; 1105 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, 1106 FileKind, IncludeOffset)); 1107 } 1108 LineTable.AddEntry(FileID::get(FID), Entries); 1109 } 1110 1111 return false; 1112 } 1113 1114 /// \brief Read a source manager block 1115 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) { 1116 using namespace SrcMgr; 1117 1118 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor; 1119 1120 // Set the source-location entry cursor to the current position in 1121 // the stream. This cursor will be used to read the contents of the 1122 // source manager block initially, and then lazily read 1123 // source-location entries as needed. 1124 SLocEntryCursor = F.Stream; 1125 1126 // The stream itself is going to skip over the source manager block. 1127 if (F.Stream.SkipBlock()) { 1128 Error("malformed block record in AST file"); 1129 return true; 1130 } 1131 1132 // Enter the source manager block. 1133 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) { 1134 Error("malformed source manager block record in AST file"); 1135 return true; 1136 } 1137 1138 RecordData Record; 1139 while (true) { 1140 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks(); 1141 1142 switch (E.Kind) { 1143 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1144 case llvm::BitstreamEntry::Error: 1145 Error("malformed block record in AST file"); 1146 return true; 1147 case llvm::BitstreamEntry::EndBlock: 1148 return false; 1149 case llvm::BitstreamEntry::Record: 1150 // The interesting case. 1151 break; 1152 } 1153 1154 // Read a record. 1155 Record.clear(); 1156 StringRef Blob; 1157 switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) { 1158 default: // Default behavior: ignore. 1159 break; 1160 1161 case SM_SLOC_FILE_ENTRY: 1162 case SM_SLOC_BUFFER_ENTRY: 1163 case SM_SLOC_EXPANSION_ENTRY: 1164 // Once we hit one of the source location entries, we're done. 1165 return false; 1166 } 1167 } 1168 } 1169 1170 /// \brief If a header file is not found at the path that we expect it to be 1171 /// and the PCH file was moved from its original location, try to resolve the 1172 /// file by assuming that header+PCH were moved together and the header is in 1173 /// the same place relative to the PCH. 1174 static std::string 1175 resolveFileRelativeToOriginalDir(const std::string &Filename, 1176 const std::string &OriginalDir, 1177 const std::string &CurrDir) { 1178 assert(OriginalDir != CurrDir && 1179 "No point trying to resolve the file if the PCH dir didn't change"); 1180 using namespace llvm::sys; 1181 SmallString<128> filePath(Filename); 1182 fs::make_absolute(filePath); 1183 assert(path::is_absolute(OriginalDir)); 1184 SmallString<128> currPCHPath(CurrDir); 1185 1186 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)), 1187 fileDirE = path::end(path::parent_path(filePath)); 1188 path::const_iterator origDirI = path::begin(OriginalDir), 1189 origDirE = path::end(OriginalDir); 1190 // Skip the common path components from filePath and OriginalDir. 1191 while (fileDirI != fileDirE && origDirI != origDirE && 1192 *fileDirI == *origDirI) { 1193 ++fileDirI; 1194 ++origDirI; 1195 } 1196 for (; origDirI != origDirE; ++origDirI) 1197 path::append(currPCHPath, ".."); 1198 path::append(currPCHPath, fileDirI, fileDirE); 1199 path::append(currPCHPath, path::filename(Filename)); 1200 return currPCHPath.str(); 1201 } 1202 1203 bool ASTReader::ReadSLocEntry(int ID) { 1204 if (ID == 0) 1205 return false; 1206 1207 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1208 Error("source location entry ID out-of-range for AST file"); 1209 return true; 1210 } 1211 1212 // Local helper to read the (possibly-compressed) buffer data following the 1213 // entry record. 1214 auto ReadBuffer = [this]( 1215 BitstreamCursor &SLocEntryCursor, 1216 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> { 1217 RecordData Record; 1218 StringRef Blob; 1219 unsigned Code = SLocEntryCursor.ReadCode(); 1220 unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob); 1221 1222 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) { 1223 SmallString<0> Uncompressed; 1224 if (llvm::zlib::uncompress(Blob, Uncompressed, Record[0]) != 1225 llvm::zlib::StatusOK) { 1226 Error("could not decompress embedded file contents"); 1227 return nullptr; 1228 } 1229 return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name); 1230 } else if (RecCode == SM_SLOC_BUFFER_BLOB) { 1231 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true); 1232 } else { 1233 Error("AST record has invalid code"); 1234 return nullptr; 1235 } 1236 }; 1237 1238 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second; 1239 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]); 1240 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor; 1241 unsigned BaseOffset = F->SLocEntryBaseOffset; 1242 1243 ++NumSLocEntriesRead; 1244 llvm::BitstreamEntry Entry = SLocEntryCursor.advance(); 1245 if (Entry.Kind != llvm::BitstreamEntry::Record) { 1246 Error("incorrectly-formatted source location entry in AST file"); 1247 return true; 1248 } 1249 1250 RecordData Record; 1251 StringRef Blob; 1252 switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) { 1253 default: 1254 Error("incorrectly-formatted source location entry in AST file"); 1255 return true; 1256 1257 case SM_SLOC_FILE_ENTRY: { 1258 // We will detect whether a file changed and return 'Failure' for it, but 1259 // we will also try to fail gracefully by setting up the SLocEntry. 1260 unsigned InputID = Record[4]; 1261 InputFile IF = getInputFile(*F, InputID); 1262 const FileEntry *File = IF.getFile(); 1263 bool OverriddenBuffer = IF.isOverridden(); 1264 1265 // Note that we only check if a File was returned. If it was out-of-date 1266 // we have complained but we will continue creating a FileID to recover 1267 // gracefully. 1268 if (!File) 1269 return true; 1270 1271 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1272 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) { 1273 // This is the module's main file. 1274 IncludeLoc = getImportLocation(F); 1275 } 1276 SrcMgr::CharacteristicKind 1277 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1278 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter, 1279 ID, BaseOffset + Record[0]); 1280 SrcMgr::FileInfo &FileInfo = 1281 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()); 1282 FileInfo.NumCreatedFIDs = Record[5]; 1283 if (Record[3]) 1284 FileInfo.setHasLineDirectives(); 1285 1286 const DeclID *FirstDecl = F->FileSortedDecls + Record[6]; 1287 unsigned NumFileDecls = Record[7]; 1288 if (NumFileDecls) { 1289 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?"); 1290 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl, 1291 NumFileDecls)); 1292 } 1293 1294 const SrcMgr::ContentCache *ContentCache 1295 = SourceMgr.getOrCreateContentCache(File, 1296 /*isSystemFile=*/FileCharacter != SrcMgr::C_User); 1297 if (OverriddenBuffer && !ContentCache->BufferOverridden && 1298 ContentCache->ContentsEntry == ContentCache->OrigEntry && 1299 !ContentCache->getRawBuffer()) { 1300 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName()); 1301 if (!Buffer) 1302 return true; 1303 SourceMgr.overrideFileContents(File, std::move(Buffer)); 1304 } 1305 1306 break; 1307 } 1308 1309 case SM_SLOC_BUFFER_ENTRY: { 1310 const char *Name = Blob.data(); 1311 unsigned Offset = Record[0]; 1312 SrcMgr::CharacteristicKind 1313 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1314 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1315 if (IncludeLoc.isInvalid() && 1316 (F->Kind == MK_ImplicitModule || F->Kind == MK_ExplicitModule)) { 1317 IncludeLoc = getImportLocation(F); 1318 } 1319 1320 auto Buffer = ReadBuffer(SLocEntryCursor, Name); 1321 if (!Buffer) 1322 return true; 1323 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID, 1324 BaseOffset + Offset, IncludeLoc); 1325 break; 1326 } 1327 1328 case SM_SLOC_EXPANSION_ENTRY: { 1329 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]); 1330 SourceMgr.createExpansionLoc(SpellingLoc, 1331 ReadSourceLocation(*F, Record[2]), 1332 ReadSourceLocation(*F, Record[3]), 1333 Record[4], 1334 ID, 1335 BaseOffset + Record[0]); 1336 break; 1337 } 1338 } 1339 1340 return false; 1341 } 1342 1343 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) { 1344 if (ID == 0) 1345 return std::make_pair(SourceLocation(), ""); 1346 1347 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1348 Error("source location entry ID out-of-range for AST file"); 1349 return std::make_pair(SourceLocation(), ""); 1350 } 1351 1352 // Find which module file this entry lands in. 1353 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second; 1354 if (M->Kind != MK_ImplicitModule && M->Kind != MK_ExplicitModule) 1355 return std::make_pair(SourceLocation(), ""); 1356 1357 // FIXME: Can we map this down to a particular submodule? That would be 1358 // ideal. 1359 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName)); 1360 } 1361 1362 /// \brief Find the location where the module F is imported. 1363 SourceLocation ASTReader::getImportLocation(ModuleFile *F) { 1364 if (F->ImportLoc.isValid()) 1365 return F->ImportLoc; 1366 1367 // Otherwise we have a PCH. It's considered to be "imported" at the first 1368 // location of its includer. 1369 if (F->ImportedBy.empty() || !F->ImportedBy[0]) { 1370 // Main file is the importer. 1371 assert(SourceMgr.getMainFileID().isValid() && "missing main file"); 1372 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); 1373 } 1374 return F->ImportedBy[0]->FirstLoc; 1375 } 1376 1377 /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the 1378 /// specified cursor. Read the abbreviations that are at the top of the block 1379 /// and then leave the cursor pointing into the block. 1380 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) { 1381 if (Cursor.EnterSubBlock(BlockID)) 1382 return true; 1383 1384 while (true) { 1385 uint64_t Offset = Cursor.GetCurrentBitNo(); 1386 unsigned Code = Cursor.ReadCode(); 1387 1388 // We expect all abbrevs to be at the start of the block. 1389 if (Code != llvm::bitc::DEFINE_ABBREV) { 1390 Cursor.JumpToBit(Offset); 1391 return false; 1392 } 1393 Cursor.ReadAbbrevRecord(); 1394 } 1395 } 1396 1397 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record, 1398 unsigned &Idx) { 1399 Token Tok; 1400 Tok.startToken(); 1401 Tok.setLocation(ReadSourceLocation(F, Record, Idx)); 1402 Tok.setLength(Record[Idx++]); 1403 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++])) 1404 Tok.setIdentifierInfo(II); 1405 Tok.setKind((tok::TokenKind)Record[Idx++]); 1406 Tok.setFlag((Token::TokenFlags)Record[Idx++]); 1407 return Tok; 1408 } 1409 1410 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) { 1411 BitstreamCursor &Stream = F.MacroCursor; 1412 1413 // Keep track of where we are in the stream, then jump back there 1414 // after reading this macro. 1415 SavedStreamPosition SavedPosition(Stream); 1416 1417 Stream.JumpToBit(Offset); 1418 RecordData Record; 1419 SmallVector<IdentifierInfo*, 16> MacroArgs; 1420 MacroInfo *Macro = nullptr; 1421 1422 while (true) { 1423 // Advance to the next record, but if we get to the end of the block, don't 1424 // pop it (removing all the abbreviations from the cursor) since we want to 1425 // be able to reseek within the block and read entries. 1426 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd; 1427 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags); 1428 1429 switch (Entry.Kind) { 1430 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1431 case llvm::BitstreamEntry::Error: 1432 Error("malformed block record in AST file"); 1433 return Macro; 1434 case llvm::BitstreamEntry::EndBlock: 1435 return Macro; 1436 case llvm::BitstreamEntry::Record: 1437 // The interesting case. 1438 break; 1439 } 1440 1441 // Read a record. 1442 Record.clear(); 1443 PreprocessorRecordTypes RecType = 1444 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record); 1445 switch (RecType) { 1446 case PP_MODULE_MACRO: 1447 case PP_MACRO_DIRECTIVE_HISTORY: 1448 return Macro; 1449 1450 case PP_MACRO_OBJECT_LIKE: 1451 case PP_MACRO_FUNCTION_LIKE: { 1452 // If we already have a macro, that means that we've hit the end 1453 // of the definition of the macro we were looking for. We're 1454 // done. 1455 if (Macro) 1456 return Macro; 1457 1458 unsigned NextIndex = 1; // Skip identifier ID. 1459 SubmoduleID SubModID = getGlobalSubmoduleID(F, Record[NextIndex++]); 1460 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex); 1461 MacroInfo *MI = PP.AllocateDeserializedMacroInfo(Loc, SubModID); 1462 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex)); 1463 MI->setIsUsed(Record[NextIndex++]); 1464 MI->setUsedForHeaderGuard(Record[NextIndex++]); 1465 1466 if (RecType == PP_MACRO_FUNCTION_LIKE) { 1467 // Decode function-like macro info. 1468 bool isC99VarArgs = Record[NextIndex++]; 1469 bool isGNUVarArgs = Record[NextIndex++]; 1470 bool hasCommaPasting = Record[NextIndex++]; 1471 MacroArgs.clear(); 1472 unsigned NumArgs = Record[NextIndex++]; 1473 for (unsigned i = 0; i != NumArgs; ++i) 1474 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++])); 1475 1476 // Install function-like macro info. 1477 MI->setIsFunctionLike(); 1478 if (isC99VarArgs) MI->setIsC99Varargs(); 1479 if (isGNUVarArgs) MI->setIsGNUVarargs(); 1480 if (hasCommaPasting) MI->setHasCommaPasting(); 1481 MI->setArgumentList(MacroArgs, PP.getPreprocessorAllocator()); 1482 } 1483 1484 // Remember that we saw this macro last so that we add the tokens that 1485 // form its body to it. 1486 Macro = MI; 1487 1488 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() && 1489 Record[NextIndex]) { 1490 // We have a macro definition. Register the association 1491 PreprocessedEntityID 1492 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]); 1493 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 1494 PreprocessingRecord::PPEntityID PPID = 1495 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true); 1496 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>( 1497 PPRec.getPreprocessedEntity(PPID)); 1498 if (PPDef) 1499 PPRec.RegisterMacroDefinition(Macro, PPDef); 1500 } 1501 1502 ++NumMacrosRead; 1503 break; 1504 } 1505 1506 case PP_TOKEN: { 1507 // If we see a TOKEN before a PP_MACRO_*, then the file is 1508 // erroneous, just pretend we didn't see this. 1509 if (!Macro) break; 1510 1511 unsigned Idx = 0; 1512 Token Tok = ReadToken(F, Record, Idx); 1513 Macro->AddTokenToBody(Tok); 1514 break; 1515 } 1516 } 1517 } 1518 } 1519 1520 PreprocessedEntityID 1521 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const { 1522 ContinuousRangeMap<uint32_t, int, 2>::const_iterator 1523 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS); 1524 assert(I != M.PreprocessedEntityRemap.end() 1525 && "Invalid index into preprocessed entity index remap"); 1526 1527 return LocalID + I->second; 1528 } 1529 1530 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) { 1531 return llvm::hash_combine(ikey.Size, ikey.ModTime); 1532 } 1533 1534 HeaderFileInfoTrait::internal_key_type 1535 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) { 1536 internal_key_type ikey = {FE->getSize(), 1537 M.HasTimestamps ? FE->getModificationTime() : 0, 1538 FE->getName(), /*Imported*/ false}; 1539 return ikey; 1540 } 1541 1542 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) { 1543 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime)) 1544 return false; 1545 1546 if (llvm::sys::path::is_absolute(a.Filename) && 1547 strcmp(a.Filename, b.Filename) == 0) 1548 return true; 1549 1550 // Determine whether the actual files are equivalent. 1551 FileManager &FileMgr = Reader.getFileManager(); 1552 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* { 1553 if (!Key.Imported) 1554 return FileMgr.getFile(Key.Filename); 1555 1556 std::string Resolved = Key.Filename; 1557 Reader.ResolveImportedPath(M, Resolved); 1558 return FileMgr.getFile(Resolved); 1559 }; 1560 1561 const FileEntry *FEA = GetFile(a); 1562 const FileEntry *FEB = GetFile(b); 1563 return FEA && FEA == FEB; 1564 } 1565 1566 std::pair<unsigned, unsigned> 1567 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) { 1568 using namespace llvm::support; 1569 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d); 1570 unsigned DataLen = (unsigned) *d++; 1571 return std::make_pair(KeyLen, DataLen); 1572 } 1573 1574 HeaderFileInfoTrait::internal_key_type 1575 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) { 1576 using namespace llvm::support; 1577 internal_key_type ikey; 1578 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d)); 1579 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d)); 1580 ikey.Filename = (const char *)d; 1581 ikey.Imported = true; 1582 return ikey; 1583 } 1584 1585 HeaderFileInfoTrait::data_type 1586 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d, 1587 unsigned DataLen) { 1588 const unsigned char *End = d + DataLen; 1589 using namespace llvm::support; 1590 HeaderFileInfo HFI; 1591 unsigned Flags = *d++; 1592 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp. 1593 HFI.isImport |= (Flags >> 4) & 0x01; 1594 HFI.isPragmaOnce |= (Flags >> 3) & 0x01; 1595 HFI.DirInfo = (Flags >> 1) & 0x03; 1596 HFI.IndexHeaderMapHeader = Flags & 0x01; 1597 // FIXME: Find a better way to handle this. Maybe just store a 1598 // "has been included" flag? 1599 HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d), 1600 HFI.NumIncludes); 1601 HFI.ControllingMacroID = Reader.getGlobalIdentifierID( 1602 M, endian::readNext<uint32_t, little, unaligned>(d)); 1603 if (unsigned FrameworkOffset = 1604 endian::readNext<uint32_t, little, unaligned>(d)) { 1605 // The framework offset is 1 greater than the actual offset, 1606 // since 0 is used as an indicator for "no framework name". 1607 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1); 1608 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName); 1609 } 1610 1611 assert((End - d) % 4 == 0 && 1612 "Wrong data length in HeaderFileInfo deserialization"); 1613 while (d != End) { 1614 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d); 1615 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3); 1616 LocalSMID >>= 2; 1617 1618 // This header is part of a module. Associate it with the module to enable 1619 // implicit module import. 1620 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID); 1621 Module *Mod = Reader.getSubmodule(GlobalSMID); 1622 FileManager &FileMgr = Reader.getFileManager(); 1623 ModuleMap &ModMap = 1624 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap(); 1625 1626 std::string Filename = key.Filename; 1627 if (key.Imported) 1628 Reader.ResolveImportedPath(M, Filename); 1629 // FIXME: This is not always the right filename-as-written, but we're not 1630 // going to use this information to rebuild the module, so it doesn't make 1631 // a lot of difference. 1632 Module::Header H = { key.Filename, FileMgr.getFile(Filename) }; 1633 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true); 1634 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader); 1635 } 1636 1637 // This HeaderFileInfo was externally loaded. 1638 HFI.External = true; 1639 HFI.IsValid = true; 1640 return HFI; 1641 } 1642 1643 void ASTReader::addPendingMacro(IdentifierInfo *II, 1644 ModuleFile *M, 1645 uint64_t MacroDirectivesOffset) { 1646 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard"); 1647 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset)); 1648 } 1649 1650 void ASTReader::ReadDefinedMacros() { 1651 // Note that we are loading defined macros. 1652 Deserializing Macros(this); 1653 1654 for (auto &I : llvm::reverse(ModuleMgr)) { 1655 BitstreamCursor &MacroCursor = I->MacroCursor; 1656 1657 // If there was no preprocessor block, skip this file. 1658 if (!MacroCursor.getBitStreamReader()) 1659 continue; 1660 1661 BitstreamCursor Cursor = MacroCursor; 1662 Cursor.JumpToBit(I->MacroStartOffset); 1663 1664 RecordData Record; 1665 while (true) { 1666 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks(); 1667 1668 switch (E.Kind) { 1669 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1670 case llvm::BitstreamEntry::Error: 1671 Error("malformed block record in AST file"); 1672 return; 1673 case llvm::BitstreamEntry::EndBlock: 1674 goto NextCursor; 1675 1676 case llvm::BitstreamEntry::Record: 1677 Record.clear(); 1678 switch (Cursor.readRecord(E.ID, Record)) { 1679 default: // Default behavior: ignore. 1680 break; 1681 1682 case PP_MACRO_OBJECT_LIKE: 1683 case PP_MACRO_FUNCTION_LIKE: { 1684 IdentifierInfo *II = getLocalIdentifier(*I, Record[0]); 1685 if (II->isOutOfDate()) 1686 updateOutOfDateIdentifier(*II); 1687 break; 1688 } 1689 1690 case PP_TOKEN: 1691 // Ignore tokens. 1692 break; 1693 } 1694 break; 1695 } 1696 } 1697 NextCursor: ; 1698 } 1699 } 1700 1701 namespace { 1702 /// \brief Visitor class used to look up identifirs in an AST file. 1703 class IdentifierLookupVisitor { 1704 StringRef Name; 1705 unsigned NameHash; 1706 unsigned PriorGeneration; 1707 unsigned &NumIdentifierLookups; 1708 unsigned &NumIdentifierLookupHits; 1709 IdentifierInfo *Found; 1710 1711 public: 1712 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration, 1713 unsigned &NumIdentifierLookups, 1714 unsigned &NumIdentifierLookupHits) 1715 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)), 1716 PriorGeneration(PriorGeneration), 1717 NumIdentifierLookups(NumIdentifierLookups), 1718 NumIdentifierLookupHits(NumIdentifierLookupHits), 1719 Found() 1720 { 1721 } 1722 1723 bool operator()(ModuleFile &M) { 1724 // If we've already searched this module file, skip it now. 1725 if (M.Generation <= PriorGeneration) 1726 return true; 1727 1728 ASTIdentifierLookupTable *IdTable 1729 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable; 1730 if (!IdTable) 1731 return false; 1732 1733 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M, 1734 Found); 1735 ++NumIdentifierLookups; 1736 ASTIdentifierLookupTable::iterator Pos = 1737 IdTable->find_hashed(Name, NameHash, &Trait); 1738 if (Pos == IdTable->end()) 1739 return false; 1740 1741 // Dereferencing the iterator has the effect of building the 1742 // IdentifierInfo node and populating it with the various 1743 // declarations it needs. 1744 ++NumIdentifierLookupHits; 1745 Found = *Pos; 1746 return true; 1747 } 1748 1749 // \brief Retrieve the identifier info found within the module 1750 // files. 1751 IdentifierInfo *getIdentifierInfo() const { return Found; } 1752 }; 1753 } 1754 1755 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) { 1756 // Note that we are loading an identifier. 1757 Deserializing AnIdentifier(this); 1758 1759 unsigned PriorGeneration = 0; 1760 if (getContext().getLangOpts().Modules) 1761 PriorGeneration = IdentifierGeneration[&II]; 1762 1763 // If there is a global index, look there first to determine which modules 1764 // provably do not have any results for this identifier. 1765 GlobalModuleIndex::HitSet Hits; 1766 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 1767 if (!loadGlobalIndex()) { 1768 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) { 1769 HitsPtr = &Hits; 1770 } 1771 } 1772 1773 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration, 1774 NumIdentifierLookups, 1775 NumIdentifierLookupHits); 1776 ModuleMgr.visit(Visitor, HitsPtr); 1777 markIdentifierUpToDate(&II); 1778 } 1779 1780 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) { 1781 if (!II) 1782 return; 1783 1784 II->setOutOfDate(false); 1785 1786 // Update the generation for this identifier. 1787 if (getContext().getLangOpts().Modules) 1788 IdentifierGeneration[II] = getGeneration(); 1789 } 1790 1791 void ASTReader::resolvePendingMacro(IdentifierInfo *II, 1792 const PendingMacroInfo &PMInfo) { 1793 ModuleFile &M = *PMInfo.M; 1794 1795 BitstreamCursor &Cursor = M.MacroCursor; 1796 SavedStreamPosition SavedPosition(Cursor); 1797 Cursor.JumpToBit(PMInfo.MacroDirectivesOffset); 1798 1799 struct ModuleMacroRecord { 1800 SubmoduleID SubModID; 1801 MacroInfo *MI; 1802 SmallVector<SubmoduleID, 8> Overrides; 1803 }; 1804 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros; 1805 1806 // We expect to see a sequence of PP_MODULE_MACRO records listing exported 1807 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete 1808 // macro histroy. 1809 RecordData Record; 1810 while (true) { 1811 llvm::BitstreamEntry Entry = 1812 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 1813 if (Entry.Kind != llvm::BitstreamEntry::Record) { 1814 Error("malformed block record in AST file"); 1815 return; 1816 } 1817 1818 Record.clear(); 1819 switch ((PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record)) { 1820 case PP_MACRO_DIRECTIVE_HISTORY: 1821 break; 1822 1823 case PP_MODULE_MACRO: { 1824 ModuleMacros.push_back(ModuleMacroRecord()); 1825 auto &Info = ModuleMacros.back(); 1826 Info.SubModID = getGlobalSubmoduleID(M, Record[0]); 1827 Info.MI = getMacro(getGlobalMacroID(M, Record[1])); 1828 for (int I = 2, N = Record.size(); I != N; ++I) 1829 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I])); 1830 continue; 1831 } 1832 1833 default: 1834 Error("malformed block record in AST file"); 1835 return; 1836 } 1837 1838 // We found the macro directive history; that's the last record 1839 // for this macro. 1840 break; 1841 } 1842 1843 // Module macros are listed in reverse dependency order. 1844 { 1845 std::reverse(ModuleMacros.begin(), ModuleMacros.end()); 1846 llvm::SmallVector<ModuleMacro*, 8> Overrides; 1847 for (auto &MMR : ModuleMacros) { 1848 Overrides.clear(); 1849 for (unsigned ModID : MMR.Overrides) { 1850 Module *Mod = getSubmodule(ModID); 1851 auto *Macro = PP.getModuleMacro(Mod, II); 1852 assert(Macro && "missing definition for overridden macro"); 1853 Overrides.push_back(Macro); 1854 } 1855 1856 bool Inserted = false; 1857 Module *Owner = getSubmodule(MMR.SubModID); 1858 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted); 1859 } 1860 } 1861 1862 // Don't read the directive history for a module; we don't have anywhere 1863 // to put it. 1864 if (M.Kind == MK_ImplicitModule || M.Kind == MK_ExplicitModule) 1865 return; 1866 1867 // Deserialize the macro directives history in reverse source-order. 1868 MacroDirective *Latest = nullptr, *Earliest = nullptr; 1869 unsigned Idx = 0, N = Record.size(); 1870 while (Idx < N) { 1871 MacroDirective *MD = nullptr; 1872 SourceLocation Loc = ReadSourceLocation(M, Record, Idx); 1873 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++]; 1874 switch (K) { 1875 case MacroDirective::MD_Define: { 1876 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++])); 1877 MD = PP.AllocateDefMacroDirective(MI, Loc); 1878 break; 1879 } 1880 case MacroDirective::MD_Undefine: { 1881 MD = PP.AllocateUndefMacroDirective(Loc); 1882 break; 1883 } 1884 case MacroDirective::MD_Visibility: 1885 bool isPublic = Record[Idx++]; 1886 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic); 1887 break; 1888 } 1889 1890 if (!Latest) 1891 Latest = MD; 1892 if (Earliest) 1893 Earliest->setPrevious(MD); 1894 Earliest = MD; 1895 } 1896 1897 if (Latest) 1898 PP.setLoadedMacroDirective(II, Latest); 1899 } 1900 1901 ASTReader::InputFileInfo 1902 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) { 1903 // Go find this input file. 1904 BitstreamCursor &Cursor = F.InputFilesCursor; 1905 SavedStreamPosition SavedPosition(Cursor); 1906 Cursor.JumpToBit(F.InputFileOffsets[ID-1]); 1907 1908 unsigned Code = Cursor.ReadCode(); 1909 RecordData Record; 1910 StringRef Blob; 1911 1912 unsigned Result = Cursor.readRecord(Code, Record, &Blob); 1913 assert(static_cast<InputFileRecordTypes>(Result) == INPUT_FILE && 1914 "invalid record type for input file"); 1915 (void)Result; 1916 1917 assert(Record[0] == ID && "Bogus stored ID or offset"); 1918 InputFileInfo R; 1919 R.StoredSize = static_cast<off_t>(Record[1]); 1920 R.StoredTime = static_cast<time_t>(Record[2]); 1921 R.Overridden = static_cast<bool>(Record[3]); 1922 R.Transient = static_cast<bool>(Record[4]); 1923 R.Filename = Blob; 1924 ResolveImportedPath(F, R.Filename); 1925 return R; 1926 } 1927 1928 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) { 1929 // If this ID is bogus, just return an empty input file. 1930 if (ID == 0 || ID > F.InputFilesLoaded.size()) 1931 return InputFile(); 1932 1933 // If we've already loaded this input file, return it. 1934 if (F.InputFilesLoaded[ID-1].getFile()) 1935 return F.InputFilesLoaded[ID-1]; 1936 1937 if (F.InputFilesLoaded[ID-1].isNotFound()) 1938 return InputFile(); 1939 1940 // Go find this input file. 1941 BitstreamCursor &Cursor = F.InputFilesCursor; 1942 SavedStreamPosition SavedPosition(Cursor); 1943 Cursor.JumpToBit(F.InputFileOffsets[ID-1]); 1944 1945 InputFileInfo FI = readInputFileInfo(F, ID); 1946 off_t StoredSize = FI.StoredSize; 1947 time_t StoredTime = FI.StoredTime; 1948 bool Overridden = FI.Overridden; 1949 bool Transient = FI.Transient; 1950 StringRef Filename = FI.Filename; 1951 1952 const FileEntry *File = FileMgr.getFile(Filename, /*OpenFile=*/false); 1953 1954 // If we didn't find the file, resolve it relative to the 1955 // original directory from which this AST file was created. 1956 if (File == nullptr && !F.OriginalDir.empty() && !CurrentDir.empty() && 1957 F.OriginalDir != CurrentDir) { 1958 std::string Resolved = resolveFileRelativeToOriginalDir(Filename, 1959 F.OriginalDir, 1960 CurrentDir); 1961 if (!Resolved.empty()) 1962 File = FileMgr.getFile(Resolved); 1963 } 1964 1965 // For an overridden file, create a virtual file with the stored 1966 // size/timestamp. 1967 if ((Overridden || Transient) && File == nullptr) 1968 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime); 1969 1970 if (File == nullptr) { 1971 if (Complain) { 1972 std::string ErrorStr = "could not find file '"; 1973 ErrorStr += Filename; 1974 ErrorStr += "' referenced by AST file '"; 1975 ErrorStr += F.FileName; 1976 ErrorStr += "'"; 1977 Error(ErrorStr.c_str()); 1978 } 1979 // Record that we didn't find the file. 1980 F.InputFilesLoaded[ID-1] = InputFile::getNotFound(); 1981 return InputFile(); 1982 } 1983 1984 // Check if there was a request to override the contents of the file 1985 // that was part of the precompiled header. Overridding such a file 1986 // can lead to problems when lexing using the source locations from the 1987 // PCH. 1988 SourceManager &SM = getSourceManager(); 1989 // FIXME: Reject if the overrides are different. 1990 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) { 1991 if (Complain) 1992 Error(diag::err_fe_pch_file_overridden, Filename); 1993 // After emitting the diagnostic, recover by disabling the override so 1994 // that the original file will be used. 1995 // 1996 // FIXME: This recovery is just as broken as the original state; there may 1997 // be another precompiled module that's using the overridden contents, or 1998 // we might be half way through parsing it. Instead, we should treat the 1999 // overridden contents as belonging to a separate FileEntry. 2000 SM.disableFileContentsOverride(File); 2001 // The FileEntry is a virtual file entry with the size of the contents 2002 // that would override the original contents. Set it to the original's 2003 // size/time. 2004 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File), 2005 StoredSize, StoredTime); 2006 } 2007 2008 bool IsOutOfDate = false; 2009 2010 // For an overridden file, there is nothing to validate. 2011 if (!Overridden && // 2012 (StoredSize != File->getSize() || 2013 (StoredTime && StoredTime != File->getModificationTime() && 2014 !DisableValidation) 2015 )) { 2016 if (Complain) { 2017 // Build a list of the PCH imports that got us here (in reverse). 2018 SmallVector<ModuleFile *, 4> ImportStack(1, &F); 2019 while (ImportStack.back()->ImportedBy.size() > 0) 2020 ImportStack.push_back(ImportStack.back()->ImportedBy[0]); 2021 2022 // The top-level PCH is stale. 2023 StringRef TopLevelPCHName(ImportStack.back()->FileName); 2024 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName); 2025 2026 // Print the import stack. 2027 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) { 2028 Diag(diag::note_pch_required_by) 2029 << Filename << ImportStack[0]->FileName; 2030 for (unsigned I = 1; I < ImportStack.size(); ++I) 2031 Diag(diag::note_pch_required_by) 2032 << ImportStack[I-1]->FileName << ImportStack[I]->FileName; 2033 } 2034 2035 if (!Diags.isDiagnosticInFlight()) 2036 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName; 2037 } 2038 2039 IsOutOfDate = true; 2040 } 2041 // FIXME: If the file is overridden and we've already opened it, 2042 // issue an error (or split it into a separate FileEntry). 2043 2044 InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate); 2045 2046 // Note that we've loaded this input file. 2047 F.InputFilesLoaded[ID-1] = IF; 2048 return IF; 2049 } 2050 2051 /// \brief If we are loading a relocatable PCH or module file, and the filename 2052 /// is not an absolute path, add the system or module root to the beginning of 2053 /// the file name. 2054 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) { 2055 // Resolve relative to the base directory, if we have one. 2056 if (!M.BaseDirectory.empty()) 2057 return ResolveImportedPath(Filename, M.BaseDirectory); 2058 } 2059 2060 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) { 2061 if (Filename.empty() || llvm::sys::path::is_absolute(Filename)) 2062 return; 2063 2064 SmallString<128> Buffer; 2065 llvm::sys::path::append(Buffer, Prefix, Filename); 2066 Filename.assign(Buffer.begin(), Buffer.end()); 2067 } 2068 2069 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) { 2070 switch (ARR) { 2071 case ASTReader::Failure: return true; 2072 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing); 2073 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate); 2074 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch); 2075 case ASTReader::ConfigurationMismatch: 2076 return !(Caps & ASTReader::ARR_ConfigurationMismatch); 2077 case ASTReader::HadErrors: return true; 2078 case ASTReader::Success: return false; 2079 } 2080 2081 llvm_unreachable("unknown ASTReadResult"); 2082 } 2083 2084 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock( 2085 BitstreamCursor &Stream, unsigned ClientLoadCapabilities, 2086 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener, 2087 std::string &SuggestedPredefines, bool ValidateDiagnosticOptions) { 2088 if (Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) 2089 return Failure; 2090 2091 // Read all of the records in the options block. 2092 RecordData Record; 2093 ASTReadResult Result = Success; 2094 while (1) { 2095 llvm::BitstreamEntry Entry = Stream.advance(); 2096 2097 switch (Entry.Kind) { 2098 case llvm::BitstreamEntry::Error: 2099 case llvm::BitstreamEntry::SubBlock: 2100 return Failure; 2101 2102 case llvm::BitstreamEntry::EndBlock: 2103 return Result; 2104 2105 case llvm::BitstreamEntry::Record: 2106 // The interesting case. 2107 break; 2108 } 2109 2110 // Read and process a record. 2111 Record.clear(); 2112 switch ((OptionsRecordTypes)Stream.readRecord(Entry.ID, Record)) { 2113 case LANGUAGE_OPTIONS: { 2114 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2115 if (ParseLanguageOptions(Record, Complain, Listener, 2116 AllowCompatibleConfigurationMismatch)) 2117 Result = ConfigurationMismatch; 2118 break; 2119 } 2120 2121 case TARGET_OPTIONS: { 2122 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2123 if (ParseTargetOptions(Record, Complain, Listener, 2124 AllowCompatibleConfigurationMismatch)) 2125 Result = ConfigurationMismatch; 2126 break; 2127 } 2128 2129 case DIAGNOSTIC_OPTIONS: { 2130 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 2131 if (ValidateDiagnosticOptions && 2132 !AllowCompatibleConfigurationMismatch && 2133 ParseDiagnosticOptions(Record, Complain, Listener)) 2134 return OutOfDate; 2135 break; 2136 } 2137 2138 case FILE_SYSTEM_OPTIONS: { 2139 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2140 if (!AllowCompatibleConfigurationMismatch && 2141 ParseFileSystemOptions(Record, Complain, Listener)) 2142 Result = ConfigurationMismatch; 2143 break; 2144 } 2145 2146 case HEADER_SEARCH_OPTIONS: { 2147 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2148 if (!AllowCompatibleConfigurationMismatch && 2149 ParseHeaderSearchOptions(Record, Complain, Listener)) 2150 Result = ConfigurationMismatch; 2151 break; 2152 } 2153 2154 case PREPROCESSOR_OPTIONS: 2155 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2156 if (!AllowCompatibleConfigurationMismatch && 2157 ParsePreprocessorOptions(Record, Complain, Listener, 2158 SuggestedPredefines)) 2159 Result = ConfigurationMismatch; 2160 break; 2161 } 2162 } 2163 } 2164 2165 ASTReader::ASTReadResult 2166 ASTReader::ReadControlBlock(ModuleFile &F, 2167 SmallVectorImpl<ImportedModule> &Loaded, 2168 const ModuleFile *ImportedBy, 2169 unsigned ClientLoadCapabilities) { 2170 BitstreamCursor &Stream = F.Stream; 2171 ASTReadResult Result = Success; 2172 2173 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) { 2174 Error("malformed block record in AST file"); 2175 return Failure; 2176 } 2177 2178 // Read all of the records and blocks in the control block. 2179 RecordData Record; 2180 unsigned NumInputs = 0; 2181 unsigned NumUserInputs = 0; 2182 while (1) { 2183 llvm::BitstreamEntry Entry = Stream.advance(); 2184 2185 switch (Entry.Kind) { 2186 case llvm::BitstreamEntry::Error: 2187 Error("malformed block record in AST file"); 2188 return Failure; 2189 case llvm::BitstreamEntry::EndBlock: { 2190 // Validate input files. 2191 const HeaderSearchOptions &HSOpts = 2192 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 2193 2194 // All user input files reside at the index range [0, NumUserInputs), and 2195 // system input files reside at [NumUserInputs, NumInputs). For explicitly 2196 // loaded module files, ignore missing inputs. 2197 if (!DisableValidation && F.Kind != MK_ExplicitModule) { 2198 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 2199 2200 // If we are reading a module, we will create a verification timestamp, 2201 // so we verify all input files. Otherwise, verify only user input 2202 // files. 2203 2204 unsigned N = NumUserInputs; 2205 if (ValidateSystemInputs || 2206 (HSOpts.ModulesValidateOncePerBuildSession && 2207 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp && 2208 F.Kind == MK_ImplicitModule)) 2209 N = NumInputs; 2210 2211 for (unsigned I = 0; I < N; ++I) { 2212 InputFile IF = getInputFile(F, I+1, Complain); 2213 if (!IF.getFile() || IF.isOutOfDate()) 2214 return OutOfDate; 2215 } 2216 } 2217 2218 if (Listener) 2219 Listener->visitModuleFile(F.FileName, F.Kind); 2220 2221 if (Listener && Listener->needsInputFileVisitation()) { 2222 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs 2223 : NumUserInputs; 2224 for (unsigned I = 0; I < N; ++I) { 2225 bool IsSystem = I >= NumUserInputs; 2226 InputFileInfo FI = readInputFileInfo(F, I+1); 2227 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden, 2228 F.Kind == MK_ExplicitModule); 2229 } 2230 } 2231 2232 return Result; 2233 } 2234 2235 case llvm::BitstreamEntry::SubBlock: 2236 switch (Entry.ID) { 2237 case INPUT_FILES_BLOCK_ID: 2238 F.InputFilesCursor = Stream; 2239 if (Stream.SkipBlock() || // Skip with the main cursor 2240 // Read the abbreviations 2241 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) { 2242 Error("malformed block record in AST file"); 2243 return Failure; 2244 } 2245 continue; 2246 2247 case OPTIONS_BLOCK_ID: 2248 // If we're reading the first module for this group, check its options 2249 // are compatible with ours. For modules it imports, no further checking 2250 // is required, because we checked them when we built it. 2251 if (Listener && !ImportedBy) { 2252 // Should we allow the configuration of the module file to differ from 2253 // the configuration of the current translation unit in a compatible 2254 // way? 2255 // 2256 // FIXME: Allow this for files explicitly specified with -include-pch. 2257 bool AllowCompatibleConfigurationMismatch = 2258 F.Kind == MK_ExplicitModule; 2259 const HeaderSearchOptions &HSOpts = 2260 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 2261 2262 Result = ReadOptionsBlock(Stream, ClientLoadCapabilities, 2263 AllowCompatibleConfigurationMismatch, 2264 *Listener, SuggestedPredefines, 2265 HSOpts.ModulesValidateDiagnosticOptions); 2266 if (Result == Failure) { 2267 Error("malformed block record in AST file"); 2268 return Result; 2269 } 2270 2271 if (DisableValidation || 2272 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 2273 Result = Success; 2274 2275 // If we can't load the module, exit early since we likely 2276 // will rebuild the module anyway. The stream may be in the 2277 // middle of a block. 2278 if (Result != Success) 2279 return Result; 2280 } else if (Stream.SkipBlock()) { 2281 Error("malformed block record in AST file"); 2282 return Failure; 2283 } 2284 continue; 2285 2286 default: 2287 if (Stream.SkipBlock()) { 2288 Error("malformed block record in AST file"); 2289 return Failure; 2290 } 2291 continue; 2292 } 2293 2294 case llvm::BitstreamEntry::Record: 2295 // The interesting case. 2296 break; 2297 } 2298 2299 // Read and process a record. 2300 Record.clear(); 2301 StringRef Blob; 2302 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) { 2303 case METADATA: { 2304 if (Record[0] != VERSION_MAJOR && !DisableValidation) { 2305 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2306 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old 2307 : diag::err_pch_version_too_new); 2308 return VersionMismatch; 2309 } 2310 2311 bool hasErrors = Record[6]; 2312 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) { 2313 Diag(diag::err_pch_with_compiler_errors); 2314 return HadErrors; 2315 } 2316 if (hasErrors) { 2317 Diags.ErrorOccurred = true; 2318 Diags.UncompilableErrorOccurred = true; 2319 Diags.UnrecoverableErrorOccurred = true; 2320 } 2321 2322 F.RelocatablePCH = Record[4]; 2323 // Relative paths in a relocatable PCH are relative to our sysroot. 2324 if (F.RelocatablePCH) 2325 F.BaseDirectory = isysroot.empty() ? "/" : isysroot; 2326 2327 F.HasTimestamps = Record[5]; 2328 2329 const std::string &CurBranch = getClangFullRepositoryVersion(); 2330 StringRef ASTBranch = Blob; 2331 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) { 2332 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2333 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch; 2334 return VersionMismatch; 2335 } 2336 break; 2337 } 2338 2339 case SIGNATURE: 2340 assert((!F.Signature || F.Signature == Record[0]) && "signature changed"); 2341 F.Signature = Record[0]; 2342 break; 2343 2344 case IMPORTS: { 2345 // Load each of the imported PCH files. 2346 unsigned Idx = 0, N = Record.size(); 2347 while (Idx < N) { 2348 // Read information about the AST file. 2349 ModuleKind ImportedKind = (ModuleKind)Record[Idx++]; 2350 // The import location will be the local one for now; we will adjust 2351 // all import locations of module imports after the global source 2352 // location info are setup, in ReadAST. 2353 SourceLocation ImportLoc = 2354 ReadUntranslatedSourceLocation(Record[Idx++]); 2355 off_t StoredSize = (off_t)Record[Idx++]; 2356 time_t StoredModTime = (time_t)Record[Idx++]; 2357 ASTFileSignature StoredSignature = Record[Idx++]; 2358 auto ImportedFile = ReadPath(F, Record, Idx); 2359 2360 // If our client can't cope with us being out of date, we can't cope with 2361 // our dependency being missing. 2362 unsigned Capabilities = ClientLoadCapabilities; 2363 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 2364 Capabilities &= ~ARR_Missing; 2365 2366 // Load the AST file. 2367 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, 2368 Loaded, StoredSize, StoredModTime, 2369 StoredSignature, Capabilities); 2370 2371 // If we diagnosed a problem, produce a backtrace. 2372 if (isDiagnosedResult(Result, Capabilities)) 2373 Diag(diag::note_module_file_imported_by) 2374 << F.FileName << !F.ModuleName.empty() << F.ModuleName; 2375 2376 switch (Result) { 2377 case Failure: return Failure; 2378 // If we have to ignore the dependency, we'll have to ignore this too. 2379 case Missing: 2380 case OutOfDate: return OutOfDate; 2381 case VersionMismatch: return VersionMismatch; 2382 case ConfigurationMismatch: return ConfigurationMismatch; 2383 case HadErrors: return HadErrors; 2384 case Success: break; 2385 } 2386 } 2387 break; 2388 } 2389 2390 case ORIGINAL_FILE: 2391 F.OriginalSourceFileID = FileID::get(Record[0]); 2392 F.ActualOriginalSourceFileName = Blob; 2393 F.OriginalSourceFileName = F.ActualOriginalSourceFileName; 2394 ResolveImportedPath(F, F.OriginalSourceFileName); 2395 break; 2396 2397 case ORIGINAL_FILE_ID: 2398 F.OriginalSourceFileID = FileID::get(Record[0]); 2399 break; 2400 2401 case ORIGINAL_PCH_DIR: 2402 F.OriginalDir = Blob; 2403 break; 2404 2405 case MODULE_NAME: 2406 F.ModuleName = Blob; 2407 if (Listener) 2408 Listener->ReadModuleName(F.ModuleName); 2409 break; 2410 2411 case MODULE_DIRECTORY: { 2412 assert(!F.ModuleName.empty() && 2413 "MODULE_DIRECTORY found before MODULE_NAME"); 2414 // If we've already loaded a module map file covering this module, we may 2415 // have a better path for it (relative to the current build). 2416 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName); 2417 if (M && M->Directory) { 2418 // If we're implicitly loading a module, the base directory can't 2419 // change between the build and use. 2420 if (F.Kind != MK_ExplicitModule) { 2421 const DirectoryEntry *BuildDir = 2422 PP.getFileManager().getDirectory(Blob); 2423 if (!BuildDir || BuildDir != M->Directory) { 2424 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 2425 Diag(diag::err_imported_module_relocated) 2426 << F.ModuleName << Blob << M->Directory->getName(); 2427 return OutOfDate; 2428 } 2429 } 2430 F.BaseDirectory = M->Directory->getName(); 2431 } else { 2432 F.BaseDirectory = Blob; 2433 } 2434 break; 2435 } 2436 2437 case MODULE_MAP_FILE: 2438 if (ASTReadResult Result = 2439 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities)) 2440 return Result; 2441 break; 2442 2443 case INPUT_FILE_OFFSETS: 2444 NumInputs = Record[0]; 2445 NumUserInputs = Record[1]; 2446 F.InputFileOffsets = 2447 (const llvm::support::unaligned_uint64_t *)Blob.data(); 2448 F.InputFilesLoaded.resize(NumInputs); 2449 break; 2450 } 2451 } 2452 } 2453 2454 ASTReader::ASTReadResult 2455 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) { 2456 BitstreamCursor &Stream = F.Stream; 2457 2458 if (Stream.EnterSubBlock(AST_BLOCK_ID)) { 2459 Error("malformed block record in AST file"); 2460 return Failure; 2461 } 2462 2463 // Read all of the records and blocks for the AST file. 2464 RecordData Record; 2465 while (1) { 2466 llvm::BitstreamEntry Entry = Stream.advance(); 2467 2468 switch (Entry.Kind) { 2469 case llvm::BitstreamEntry::Error: 2470 Error("error at end of module block in AST file"); 2471 return Failure; 2472 case llvm::BitstreamEntry::EndBlock: { 2473 // Outside of C++, we do not store a lookup map for the translation unit. 2474 // Instead, mark it as needing a lookup map to be built if this module 2475 // contains any declarations lexically within it (which it always does!). 2476 // This usually has no cost, since we very rarely need the lookup map for 2477 // the translation unit outside C++. 2478 DeclContext *DC = Context.getTranslationUnitDecl(); 2479 if (DC->hasExternalLexicalStorage() && 2480 !getContext().getLangOpts().CPlusPlus) 2481 DC->setMustBuildLookupTable(); 2482 2483 return Success; 2484 } 2485 case llvm::BitstreamEntry::SubBlock: 2486 switch (Entry.ID) { 2487 case DECLTYPES_BLOCK_ID: 2488 // We lazily load the decls block, but we want to set up the 2489 // DeclsCursor cursor to point into it. Clone our current bitcode 2490 // cursor to it, enter the block and read the abbrevs in that block. 2491 // With the main cursor, we just skip over it. 2492 F.DeclsCursor = Stream; 2493 if (Stream.SkipBlock() || // Skip with the main cursor. 2494 // Read the abbrevs. 2495 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) { 2496 Error("malformed block record in AST file"); 2497 return Failure; 2498 } 2499 break; 2500 2501 case PREPROCESSOR_BLOCK_ID: 2502 F.MacroCursor = Stream; 2503 if (!PP.getExternalSource()) 2504 PP.setExternalSource(this); 2505 2506 if (Stream.SkipBlock() || 2507 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) { 2508 Error("malformed block record in AST file"); 2509 return Failure; 2510 } 2511 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo(); 2512 break; 2513 2514 case PREPROCESSOR_DETAIL_BLOCK_ID: 2515 F.PreprocessorDetailCursor = Stream; 2516 if (Stream.SkipBlock() || 2517 ReadBlockAbbrevs(F.PreprocessorDetailCursor, 2518 PREPROCESSOR_DETAIL_BLOCK_ID)) { 2519 Error("malformed preprocessor detail record in AST file"); 2520 return Failure; 2521 } 2522 F.PreprocessorDetailStartOffset 2523 = F.PreprocessorDetailCursor.GetCurrentBitNo(); 2524 2525 if (!PP.getPreprocessingRecord()) 2526 PP.createPreprocessingRecord(); 2527 if (!PP.getPreprocessingRecord()->getExternalSource()) 2528 PP.getPreprocessingRecord()->SetExternalSource(*this); 2529 break; 2530 2531 case SOURCE_MANAGER_BLOCK_ID: 2532 if (ReadSourceManagerBlock(F)) 2533 return Failure; 2534 break; 2535 2536 case SUBMODULE_BLOCK_ID: 2537 if (ASTReadResult Result = ReadSubmoduleBlock(F, ClientLoadCapabilities)) 2538 return Result; 2539 break; 2540 2541 case COMMENTS_BLOCK_ID: { 2542 BitstreamCursor C = Stream; 2543 if (Stream.SkipBlock() || 2544 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) { 2545 Error("malformed comments block in AST file"); 2546 return Failure; 2547 } 2548 CommentsCursors.push_back(std::make_pair(C, &F)); 2549 break; 2550 } 2551 2552 default: 2553 if (Stream.SkipBlock()) { 2554 Error("malformed block record in AST file"); 2555 return Failure; 2556 } 2557 break; 2558 } 2559 continue; 2560 2561 case llvm::BitstreamEntry::Record: 2562 // The interesting case. 2563 break; 2564 } 2565 2566 // Read and process a record. 2567 Record.clear(); 2568 StringRef Blob; 2569 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) { 2570 default: // Default behavior: ignore. 2571 break; 2572 2573 case TYPE_OFFSET: { 2574 if (F.LocalNumTypes != 0) { 2575 Error("duplicate TYPE_OFFSET record in AST file"); 2576 return Failure; 2577 } 2578 F.TypeOffsets = (const uint32_t *)Blob.data(); 2579 F.LocalNumTypes = Record[0]; 2580 unsigned LocalBaseTypeIndex = Record[1]; 2581 F.BaseTypeIndex = getTotalNumTypes(); 2582 2583 if (F.LocalNumTypes > 0) { 2584 // Introduce the global -> local mapping for types within this module. 2585 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F)); 2586 2587 // Introduce the local -> global mapping for types within this module. 2588 F.TypeRemap.insertOrReplace( 2589 std::make_pair(LocalBaseTypeIndex, 2590 F.BaseTypeIndex - LocalBaseTypeIndex)); 2591 2592 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes); 2593 } 2594 break; 2595 } 2596 2597 case DECL_OFFSET: { 2598 if (F.LocalNumDecls != 0) { 2599 Error("duplicate DECL_OFFSET record in AST file"); 2600 return Failure; 2601 } 2602 F.DeclOffsets = (const DeclOffset *)Blob.data(); 2603 F.LocalNumDecls = Record[0]; 2604 unsigned LocalBaseDeclID = Record[1]; 2605 F.BaseDeclID = getTotalNumDecls(); 2606 2607 if (F.LocalNumDecls > 0) { 2608 // Introduce the global -> local mapping for declarations within this 2609 // module. 2610 GlobalDeclMap.insert( 2611 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F)); 2612 2613 // Introduce the local -> global mapping for declarations within this 2614 // module. 2615 F.DeclRemap.insertOrReplace( 2616 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID)); 2617 2618 // Introduce the global -> local mapping for declarations within this 2619 // module. 2620 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID; 2621 2622 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls); 2623 } 2624 break; 2625 } 2626 2627 case TU_UPDATE_LEXICAL: { 2628 DeclContext *TU = Context.getTranslationUnitDecl(); 2629 LexicalContents Contents( 2630 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 2631 Blob.data()), 2632 static_cast<unsigned int>(Blob.size() / 4)); 2633 TULexicalDecls.push_back(std::make_pair(&F, Contents)); 2634 TU->setHasExternalLexicalStorage(true); 2635 break; 2636 } 2637 2638 case UPDATE_VISIBLE: { 2639 unsigned Idx = 0; 2640 serialization::DeclID ID = ReadDeclID(F, Record, Idx); 2641 auto *Data = (const unsigned char*)Blob.data(); 2642 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data}); 2643 // If we've already loaded the decl, perform the updates when we finish 2644 // loading this block. 2645 if (Decl *D = GetExistingDecl(ID)) 2646 PendingUpdateRecords.push_back(std::make_pair(ID, D)); 2647 break; 2648 } 2649 2650 case IDENTIFIER_TABLE: 2651 F.IdentifierTableData = Blob.data(); 2652 if (Record[0]) { 2653 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create( 2654 (const unsigned char *)F.IdentifierTableData + Record[0], 2655 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t), 2656 (const unsigned char *)F.IdentifierTableData, 2657 ASTIdentifierLookupTrait(*this, F)); 2658 2659 PP.getIdentifierTable().setExternalIdentifierLookup(this); 2660 } 2661 break; 2662 2663 case IDENTIFIER_OFFSET: { 2664 if (F.LocalNumIdentifiers != 0) { 2665 Error("duplicate IDENTIFIER_OFFSET record in AST file"); 2666 return Failure; 2667 } 2668 F.IdentifierOffsets = (const uint32_t *)Blob.data(); 2669 F.LocalNumIdentifiers = Record[0]; 2670 unsigned LocalBaseIdentifierID = Record[1]; 2671 F.BaseIdentifierID = getTotalNumIdentifiers(); 2672 2673 if (F.LocalNumIdentifiers > 0) { 2674 // Introduce the global -> local mapping for identifiers within this 2675 // module. 2676 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1, 2677 &F)); 2678 2679 // Introduce the local -> global mapping for identifiers within this 2680 // module. 2681 F.IdentifierRemap.insertOrReplace( 2682 std::make_pair(LocalBaseIdentifierID, 2683 F.BaseIdentifierID - LocalBaseIdentifierID)); 2684 2685 IdentifiersLoaded.resize(IdentifiersLoaded.size() 2686 + F.LocalNumIdentifiers); 2687 } 2688 break; 2689 } 2690 2691 case INTERESTING_IDENTIFIERS: 2692 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end()); 2693 break; 2694 2695 case EAGERLY_DESERIALIZED_DECLS: 2696 // FIXME: Skip reading this record if our ASTConsumer doesn't care 2697 // about "interesting" decls (for instance, if we're building a module). 2698 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2699 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 2700 break; 2701 2702 case SPECIAL_TYPES: 2703 if (SpecialTypes.empty()) { 2704 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2705 SpecialTypes.push_back(getGlobalTypeID(F, Record[I])); 2706 break; 2707 } 2708 2709 if (SpecialTypes.size() != Record.size()) { 2710 Error("invalid special-types record"); 2711 return Failure; 2712 } 2713 2714 for (unsigned I = 0, N = Record.size(); I != N; ++I) { 2715 serialization::TypeID ID = getGlobalTypeID(F, Record[I]); 2716 if (!SpecialTypes[I]) 2717 SpecialTypes[I] = ID; 2718 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate 2719 // merge step? 2720 } 2721 break; 2722 2723 case STATISTICS: 2724 TotalNumStatements += Record[0]; 2725 TotalNumMacros += Record[1]; 2726 TotalLexicalDeclContexts += Record[2]; 2727 TotalVisibleDeclContexts += Record[3]; 2728 break; 2729 2730 case UNUSED_FILESCOPED_DECLS: 2731 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2732 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I])); 2733 break; 2734 2735 case DELEGATING_CTORS: 2736 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2737 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I])); 2738 break; 2739 2740 case WEAK_UNDECLARED_IDENTIFIERS: 2741 if (Record.size() % 4 != 0) { 2742 Error("invalid weak identifiers record"); 2743 return Failure; 2744 } 2745 2746 // FIXME: Ignore weak undeclared identifiers from non-original PCH 2747 // files. This isn't the way to do it :) 2748 WeakUndeclaredIdentifiers.clear(); 2749 2750 // Translate the weak, undeclared identifiers into global IDs. 2751 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) { 2752 WeakUndeclaredIdentifiers.push_back( 2753 getGlobalIdentifierID(F, Record[I++])); 2754 WeakUndeclaredIdentifiers.push_back( 2755 getGlobalIdentifierID(F, Record[I++])); 2756 WeakUndeclaredIdentifiers.push_back( 2757 ReadSourceLocation(F, Record, I).getRawEncoding()); 2758 WeakUndeclaredIdentifiers.push_back(Record[I++]); 2759 } 2760 break; 2761 2762 case SELECTOR_OFFSETS: { 2763 F.SelectorOffsets = (const uint32_t *)Blob.data(); 2764 F.LocalNumSelectors = Record[0]; 2765 unsigned LocalBaseSelectorID = Record[1]; 2766 F.BaseSelectorID = getTotalNumSelectors(); 2767 2768 if (F.LocalNumSelectors > 0) { 2769 // Introduce the global -> local mapping for selectors within this 2770 // module. 2771 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F)); 2772 2773 // Introduce the local -> global mapping for selectors within this 2774 // module. 2775 F.SelectorRemap.insertOrReplace( 2776 std::make_pair(LocalBaseSelectorID, 2777 F.BaseSelectorID - LocalBaseSelectorID)); 2778 2779 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors); 2780 } 2781 break; 2782 } 2783 2784 case METHOD_POOL: 2785 F.SelectorLookupTableData = (const unsigned char *)Blob.data(); 2786 if (Record[0]) 2787 F.SelectorLookupTable 2788 = ASTSelectorLookupTable::Create( 2789 F.SelectorLookupTableData + Record[0], 2790 F.SelectorLookupTableData, 2791 ASTSelectorLookupTrait(*this, F)); 2792 TotalNumMethodPoolEntries += Record[1]; 2793 break; 2794 2795 case REFERENCED_SELECTOR_POOL: 2796 if (!Record.empty()) { 2797 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) { 2798 ReferencedSelectorsData.push_back(getGlobalSelectorID(F, 2799 Record[Idx++])); 2800 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx). 2801 getRawEncoding()); 2802 } 2803 } 2804 break; 2805 2806 case PP_COUNTER_VALUE: 2807 if (!Record.empty() && Listener) 2808 Listener->ReadCounter(F, Record[0]); 2809 break; 2810 2811 case FILE_SORTED_DECLS: 2812 F.FileSortedDecls = (const DeclID *)Blob.data(); 2813 F.NumFileSortedDecls = Record[0]; 2814 break; 2815 2816 case SOURCE_LOCATION_OFFSETS: { 2817 F.SLocEntryOffsets = (const uint32_t *)Blob.data(); 2818 F.LocalNumSLocEntries = Record[0]; 2819 unsigned SLocSpaceSize = Record[1]; 2820 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) = 2821 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, 2822 SLocSpaceSize); 2823 if (!F.SLocEntryBaseID) { 2824 Error("ran out of source locations"); 2825 break; 2826 } 2827 // Make our entry in the range map. BaseID is negative and growing, so 2828 // we invert it. Because we invert it, though, we need the other end of 2829 // the range. 2830 unsigned RangeStart = 2831 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1; 2832 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F)); 2833 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset); 2834 2835 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing. 2836 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0); 2837 GlobalSLocOffsetMap.insert( 2838 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset 2839 - SLocSpaceSize,&F)); 2840 2841 // Initialize the remapping table. 2842 // Invalid stays invalid. 2843 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0)); 2844 // This module. Base was 2 when being compiled. 2845 F.SLocRemap.insertOrReplace(std::make_pair(2U, 2846 static_cast<int>(F.SLocEntryBaseOffset - 2))); 2847 2848 TotalNumSLocEntries += F.LocalNumSLocEntries; 2849 break; 2850 } 2851 2852 case MODULE_OFFSET_MAP: { 2853 // Additional remapping information. 2854 const unsigned char *Data = (const unsigned char*)Blob.data(); 2855 const unsigned char *DataEnd = Data + Blob.size(); 2856 2857 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders. 2858 if (F.SLocRemap.find(0) == F.SLocRemap.end()) { 2859 F.SLocRemap.insert(std::make_pair(0U, 0)); 2860 F.SLocRemap.insert(std::make_pair(2U, 1)); 2861 } 2862 2863 // Continuous range maps we may be updating in our module. 2864 typedef ContinuousRangeMap<uint32_t, int, 2>::Builder 2865 RemapBuilder; 2866 RemapBuilder SLocRemap(F.SLocRemap); 2867 RemapBuilder IdentifierRemap(F.IdentifierRemap); 2868 RemapBuilder MacroRemap(F.MacroRemap); 2869 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap); 2870 RemapBuilder SubmoduleRemap(F.SubmoduleRemap); 2871 RemapBuilder SelectorRemap(F.SelectorRemap); 2872 RemapBuilder DeclRemap(F.DeclRemap); 2873 RemapBuilder TypeRemap(F.TypeRemap); 2874 2875 while (Data < DataEnd) { 2876 using namespace llvm::support; 2877 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data); 2878 StringRef Name = StringRef((const char*)Data, Len); 2879 Data += Len; 2880 ModuleFile *OM = ModuleMgr.lookup(Name); 2881 if (!OM) { 2882 Error("SourceLocation remap refers to unknown module"); 2883 return Failure; 2884 } 2885 2886 uint32_t SLocOffset = 2887 endian::readNext<uint32_t, little, unaligned>(Data); 2888 uint32_t IdentifierIDOffset = 2889 endian::readNext<uint32_t, little, unaligned>(Data); 2890 uint32_t MacroIDOffset = 2891 endian::readNext<uint32_t, little, unaligned>(Data); 2892 uint32_t PreprocessedEntityIDOffset = 2893 endian::readNext<uint32_t, little, unaligned>(Data); 2894 uint32_t SubmoduleIDOffset = 2895 endian::readNext<uint32_t, little, unaligned>(Data); 2896 uint32_t SelectorIDOffset = 2897 endian::readNext<uint32_t, little, unaligned>(Data); 2898 uint32_t DeclIDOffset = 2899 endian::readNext<uint32_t, little, unaligned>(Data); 2900 uint32_t TypeIndexOffset = 2901 endian::readNext<uint32_t, little, unaligned>(Data); 2902 2903 uint32_t None = std::numeric_limits<uint32_t>::max(); 2904 2905 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset, 2906 RemapBuilder &Remap) { 2907 if (Offset != None) 2908 Remap.insert(std::make_pair(Offset, 2909 static_cast<int>(BaseOffset - Offset))); 2910 }; 2911 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap); 2912 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap); 2913 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap); 2914 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID, 2915 PreprocessedEntityRemap); 2916 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap); 2917 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap); 2918 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap); 2919 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap); 2920 2921 // Global -> local mappings. 2922 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset; 2923 } 2924 break; 2925 } 2926 2927 case SOURCE_MANAGER_LINE_TABLE: 2928 if (ParseLineTable(F, Record)) 2929 return Failure; 2930 break; 2931 2932 case SOURCE_LOCATION_PRELOADS: { 2933 // Need to transform from the local view (1-based IDs) to the global view, 2934 // which is based off F.SLocEntryBaseID. 2935 if (!F.PreloadSLocEntries.empty()) { 2936 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file"); 2937 return Failure; 2938 } 2939 2940 F.PreloadSLocEntries.swap(Record); 2941 break; 2942 } 2943 2944 case EXT_VECTOR_DECLS: 2945 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2946 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I])); 2947 break; 2948 2949 case VTABLE_USES: 2950 if (Record.size() % 3 != 0) { 2951 Error("Invalid VTABLE_USES record"); 2952 return Failure; 2953 } 2954 2955 // Later tables overwrite earlier ones. 2956 // FIXME: Modules will have some trouble with this. This is clearly not 2957 // the right way to do this. 2958 VTableUses.clear(); 2959 2960 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) { 2961 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++])); 2962 VTableUses.push_back( 2963 ReadSourceLocation(F, Record, Idx).getRawEncoding()); 2964 VTableUses.push_back(Record[Idx++]); 2965 } 2966 break; 2967 2968 case PENDING_IMPLICIT_INSTANTIATIONS: 2969 if (PendingInstantiations.size() % 2 != 0) { 2970 Error("Invalid existing PendingInstantiations"); 2971 return Failure; 2972 } 2973 2974 if (Record.size() % 2 != 0) { 2975 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block"); 2976 return Failure; 2977 } 2978 2979 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 2980 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++])); 2981 PendingInstantiations.push_back( 2982 ReadSourceLocation(F, Record, I).getRawEncoding()); 2983 } 2984 break; 2985 2986 case SEMA_DECL_REFS: 2987 if (Record.size() != 2) { 2988 Error("Invalid SEMA_DECL_REFS block"); 2989 return Failure; 2990 } 2991 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2992 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 2993 break; 2994 2995 case PPD_ENTITIES_OFFSETS: { 2996 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data(); 2997 assert(Blob.size() % sizeof(PPEntityOffset) == 0); 2998 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset); 2999 3000 unsigned LocalBasePreprocessedEntityID = Record[0]; 3001 3002 unsigned StartingID; 3003 if (!PP.getPreprocessingRecord()) 3004 PP.createPreprocessingRecord(); 3005 if (!PP.getPreprocessingRecord()->getExternalSource()) 3006 PP.getPreprocessingRecord()->SetExternalSource(*this); 3007 StartingID 3008 = PP.getPreprocessingRecord() 3009 ->allocateLoadedEntities(F.NumPreprocessedEntities); 3010 F.BasePreprocessedEntityID = StartingID; 3011 3012 if (F.NumPreprocessedEntities > 0) { 3013 // Introduce the global -> local mapping for preprocessed entities in 3014 // this module. 3015 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F)); 3016 3017 // Introduce the local -> global mapping for preprocessed entities in 3018 // this module. 3019 F.PreprocessedEntityRemap.insertOrReplace( 3020 std::make_pair(LocalBasePreprocessedEntityID, 3021 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID)); 3022 } 3023 3024 break; 3025 } 3026 3027 case DECL_UPDATE_OFFSETS: { 3028 if (Record.size() % 2 != 0) { 3029 Error("invalid DECL_UPDATE_OFFSETS block in AST file"); 3030 return Failure; 3031 } 3032 for (unsigned I = 0, N = Record.size(); I != N; I += 2) { 3033 GlobalDeclID ID = getGlobalDeclID(F, Record[I]); 3034 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1])); 3035 3036 // If we've already loaded the decl, perform the updates when we finish 3037 // loading this block. 3038 if (Decl *D = GetExistingDecl(ID)) 3039 PendingUpdateRecords.push_back(std::make_pair(ID, D)); 3040 } 3041 break; 3042 } 3043 3044 case OBJC_CATEGORIES_MAP: { 3045 if (F.LocalNumObjCCategoriesInMap != 0) { 3046 Error("duplicate OBJC_CATEGORIES_MAP record in AST file"); 3047 return Failure; 3048 } 3049 3050 F.LocalNumObjCCategoriesInMap = Record[0]; 3051 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data(); 3052 break; 3053 } 3054 3055 case OBJC_CATEGORIES: 3056 F.ObjCCategories.swap(Record); 3057 break; 3058 3059 case DIAG_PRAGMA_MAPPINGS: 3060 if (F.PragmaDiagMappings.empty()) 3061 F.PragmaDiagMappings.swap(Record); 3062 else 3063 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(), 3064 Record.begin(), Record.end()); 3065 break; 3066 3067 case CUDA_SPECIAL_DECL_REFS: 3068 // Later tables overwrite earlier ones. 3069 // FIXME: Modules will have trouble with this. 3070 CUDASpecialDeclRefs.clear(); 3071 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3072 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3073 break; 3074 3075 case HEADER_SEARCH_TABLE: { 3076 F.HeaderFileInfoTableData = Blob.data(); 3077 F.LocalNumHeaderFileInfos = Record[1]; 3078 if (Record[0]) { 3079 F.HeaderFileInfoTable 3080 = HeaderFileInfoLookupTable::Create( 3081 (const unsigned char *)F.HeaderFileInfoTableData + Record[0], 3082 (const unsigned char *)F.HeaderFileInfoTableData, 3083 HeaderFileInfoTrait(*this, F, 3084 &PP.getHeaderSearchInfo(), 3085 Blob.data() + Record[2])); 3086 3087 PP.getHeaderSearchInfo().SetExternalSource(this); 3088 if (!PP.getHeaderSearchInfo().getExternalLookup()) 3089 PP.getHeaderSearchInfo().SetExternalLookup(this); 3090 } 3091 break; 3092 } 3093 3094 case FP_PRAGMA_OPTIONS: 3095 // Later tables overwrite earlier ones. 3096 FPPragmaOptions.swap(Record); 3097 break; 3098 3099 case OPENCL_EXTENSIONS: 3100 // Later tables overwrite earlier ones. 3101 OpenCLExtensions.swap(Record); 3102 break; 3103 3104 case TENTATIVE_DEFINITIONS: 3105 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3106 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I])); 3107 break; 3108 3109 case KNOWN_NAMESPACES: 3110 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3111 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I])); 3112 break; 3113 3114 case UNDEFINED_BUT_USED: 3115 if (UndefinedButUsed.size() % 2 != 0) { 3116 Error("Invalid existing UndefinedButUsed"); 3117 return Failure; 3118 } 3119 3120 if (Record.size() % 2 != 0) { 3121 Error("invalid undefined-but-used record"); 3122 return Failure; 3123 } 3124 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3125 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++])); 3126 UndefinedButUsed.push_back( 3127 ReadSourceLocation(F, Record, I).getRawEncoding()); 3128 } 3129 break; 3130 case DELETE_EXPRS_TO_ANALYZE: 3131 for (unsigned I = 0, N = Record.size(); I != N;) { 3132 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++])); 3133 const uint64_t Count = Record[I++]; 3134 DelayedDeleteExprs.push_back(Count); 3135 for (uint64_t C = 0; C < Count; ++C) { 3136 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding()); 3137 bool IsArrayForm = Record[I++] == 1; 3138 DelayedDeleteExprs.push_back(IsArrayForm); 3139 } 3140 } 3141 break; 3142 3143 case IMPORTED_MODULES: { 3144 if (F.Kind != MK_ImplicitModule && F.Kind != MK_ExplicitModule) { 3145 // If we aren't loading a module (which has its own exports), make 3146 // all of the imported modules visible. 3147 // FIXME: Deal with macros-only imports. 3148 for (unsigned I = 0, N = Record.size(); I != N; /**/) { 3149 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]); 3150 SourceLocation Loc = ReadSourceLocation(F, Record, I); 3151 if (GlobalID) 3152 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc)); 3153 } 3154 } 3155 break; 3156 } 3157 3158 case MACRO_OFFSET: { 3159 if (F.LocalNumMacros != 0) { 3160 Error("duplicate MACRO_OFFSET record in AST file"); 3161 return Failure; 3162 } 3163 F.MacroOffsets = (const uint32_t *)Blob.data(); 3164 F.LocalNumMacros = Record[0]; 3165 unsigned LocalBaseMacroID = Record[1]; 3166 F.BaseMacroID = getTotalNumMacros(); 3167 3168 if (F.LocalNumMacros > 0) { 3169 // Introduce the global -> local mapping for macros within this module. 3170 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F)); 3171 3172 // Introduce the local -> global mapping for macros within this module. 3173 F.MacroRemap.insertOrReplace( 3174 std::make_pair(LocalBaseMacroID, 3175 F.BaseMacroID - LocalBaseMacroID)); 3176 3177 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros); 3178 } 3179 break; 3180 } 3181 3182 case LATE_PARSED_TEMPLATE: { 3183 LateParsedTemplates.append(Record.begin(), Record.end()); 3184 break; 3185 } 3186 3187 case OPTIMIZE_PRAGMA_OPTIONS: 3188 if (Record.size() != 1) { 3189 Error("invalid pragma optimize record"); 3190 return Failure; 3191 } 3192 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]); 3193 break; 3194 3195 case MSSTRUCT_PRAGMA_OPTIONS: 3196 if (Record.size() != 1) { 3197 Error("invalid pragma ms_struct record"); 3198 return Failure; 3199 } 3200 PragmaMSStructState = Record[0]; 3201 break; 3202 3203 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS: 3204 if (Record.size() != 2) { 3205 Error("invalid pragma ms_struct record"); 3206 return Failure; 3207 } 3208 PragmaMSPointersToMembersState = Record[0]; 3209 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]); 3210 break; 3211 3212 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES: 3213 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3214 UnusedLocalTypedefNameCandidates.push_back( 3215 getGlobalDeclID(F, Record[I])); 3216 break; 3217 } 3218 } 3219 } 3220 3221 ASTReader::ASTReadResult 3222 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F, 3223 const ModuleFile *ImportedBy, 3224 unsigned ClientLoadCapabilities) { 3225 unsigned Idx = 0; 3226 F.ModuleMapPath = ReadPath(F, Record, Idx); 3227 3228 if (F.Kind == MK_ExplicitModule) { 3229 // For an explicitly-loaded module, we don't care whether the original 3230 // module map file exists or matches. 3231 return Success; 3232 } 3233 3234 // Try to resolve ModuleName in the current header search context and 3235 // verify that it is found in the same module map file as we saved. If the 3236 // top-level AST file is a main file, skip this check because there is no 3237 // usable header search context. 3238 assert(!F.ModuleName.empty() && 3239 "MODULE_NAME should come before MODULE_MAP_FILE"); 3240 if (F.Kind == MK_ImplicitModule && 3241 (*ModuleMgr.begin())->Kind != MK_MainFile) { 3242 // An implicitly-loaded module file should have its module listed in some 3243 // module map file that we've already loaded. 3244 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName); 3245 auto &Map = PP.getHeaderSearchInfo().getModuleMap(); 3246 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr; 3247 if (!ModMap) { 3248 assert(ImportedBy && "top-level import should be verified"); 3249 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) { 3250 if (auto *ASTFE = M ? M->getASTFile() : nullptr) 3251 // This module was defined by an imported (explicit) module. 3252 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName 3253 << ASTFE->getName(); 3254 else 3255 // This module was built with a different module map. 3256 Diag(diag::err_imported_module_not_found) 3257 << F.ModuleName << F.FileName << ImportedBy->FileName 3258 << F.ModuleMapPath; 3259 } 3260 return OutOfDate; 3261 } 3262 3263 assert(M->Name == F.ModuleName && "found module with different name"); 3264 3265 // Check the primary module map file. 3266 const FileEntry *StoredModMap = FileMgr.getFile(F.ModuleMapPath); 3267 if (StoredModMap == nullptr || StoredModMap != ModMap) { 3268 assert(ModMap && "found module is missing module map file"); 3269 assert(ImportedBy && "top-level import should be verified"); 3270 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3271 Diag(diag::err_imported_module_modmap_changed) 3272 << F.ModuleName << ImportedBy->FileName 3273 << ModMap->getName() << F.ModuleMapPath; 3274 return OutOfDate; 3275 } 3276 3277 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps; 3278 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) { 3279 // FIXME: we should use input files rather than storing names. 3280 std::string Filename = ReadPath(F, Record, Idx); 3281 const FileEntry *F = 3282 FileMgr.getFile(Filename, false, false); 3283 if (F == nullptr) { 3284 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3285 Error("could not find file '" + Filename +"' referenced by AST file"); 3286 return OutOfDate; 3287 } 3288 AdditionalStoredMaps.insert(F); 3289 } 3290 3291 // Check any additional module map files (e.g. module.private.modulemap) 3292 // that are not in the pcm. 3293 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) { 3294 for (const FileEntry *ModMap : *AdditionalModuleMaps) { 3295 // Remove files that match 3296 // Note: SmallPtrSet::erase is really remove 3297 if (!AdditionalStoredMaps.erase(ModMap)) { 3298 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3299 Diag(diag::err_module_different_modmap) 3300 << F.ModuleName << /*new*/0 << ModMap->getName(); 3301 return OutOfDate; 3302 } 3303 } 3304 } 3305 3306 // Check any additional module map files that are in the pcm, but not 3307 // found in header search. Cases that match are already removed. 3308 for (const FileEntry *ModMap : AdditionalStoredMaps) { 3309 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3310 Diag(diag::err_module_different_modmap) 3311 << F.ModuleName << /*not new*/1 << ModMap->getName(); 3312 return OutOfDate; 3313 } 3314 } 3315 3316 if (Listener) 3317 Listener->ReadModuleMapFile(F.ModuleMapPath); 3318 return Success; 3319 } 3320 3321 3322 /// \brief Move the given method to the back of the global list of methods. 3323 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) { 3324 // Find the entry for this selector in the method pool. 3325 Sema::GlobalMethodPool::iterator Known 3326 = S.MethodPool.find(Method->getSelector()); 3327 if (Known == S.MethodPool.end()) 3328 return; 3329 3330 // Retrieve the appropriate method list. 3331 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first 3332 : Known->second.second; 3333 bool Found = false; 3334 for (ObjCMethodList *List = &Start; List; List = List->getNext()) { 3335 if (!Found) { 3336 if (List->getMethod() == Method) { 3337 Found = true; 3338 } else { 3339 // Keep searching. 3340 continue; 3341 } 3342 } 3343 3344 if (List->getNext()) 3345 List->setMethod(List->getNext()->getMethod()); 3346 else 3347 List->setMethod(Method); 3348 } 3349 } 3350 3351 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) { 3352 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?"); 3353 for (Decl *D : Names) { 3354 bool wasHidden = D->Hidden; 3355 D->Hidden = false; 3356 3357 if (wasHidden && SemaObj) { 3358 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) { 3359 moveMethodToBackOfGlobalList(*SemaObj, Method); 3360 } 3361 } 3362 } 3363 } 3364 3365 void ASTReader::makeModuleVisible(Module *Mod, 3366 Module::NameVisibilityKind NameVisibility, 3367 SourceLocation ImportLoc) { 3368 llvm::SmallPtrSet<Module *, 4> Visited; 3369 SmallVector<Module *, 4> Stack; 3370 Stack.push_back(Mod); 3371 while (!Stack.empty()) { 3372 Mod = Stack.pop_back_val(); 3373 3374 if (NameVisibility <= Mod->NameVisibility) { 3375 // This module already has this level of visibility (or greater), so 3376 // there is nothing more to do. 3377 continue; 3378 } 3379 3380 if (!Mod->isAvailable()) { 3381 // Modules that aren't available cannot be made visible. 3382 continue; 3383 } 3384 3385 // Update the module's name visibility. 3386 Mod->NameVisibility = NameVisibility; 3387 3388 // If we've already deserialized any names from this module, 3389 // mark them as visible. 3390 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod); 3391 if (Hidden != HiddenNamesMap.end()) { 3392 auto HiddenNames = std::move(*Hidden); 3393 HiddenNamesMap.erase(Hidden); 3394 makeNamesVisible(HiddenNames.second, HiddenNames.first); 3395 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() && 3396 "making names visible added hidden names"); 3397 } 3398 3399 // Push any exported modules onto the stack to be marked as visible. 3400 SmallVector<Module *, 16> Exports; 3401 Mod->getExportedModules(Exports); 3402 for (SmallVectorImpl<Module *>::iterator 3403 I = Exports.begin(), E = Exports.end(); I != E; ++I) { 3404 Module *Exported = *I; 3405 if (Visited.insert(Exported).second) 3406 Stack.push_back(Exported); 3407 } 3408 } 3409 } 3410 3411 bool ASTReader::loadGlobalIndex() { 3412 if (GlobalIndex) 3413 return false; 3414 3415 if (TriedLoadingGlobalIndex || !UseGlobalIndex || 3416 !Context.getLangOpts().Modules) 3417 return true; 3418 3419 // Try to load the global index. 3420 TriedLoadingGlobalIndex = true; 3421 StringRef ModuleCachePath 3422 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath(); 3423 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result 3424 = GlobalModuleIndex::readIndex(ModuleCachePath); 3425 if (!Result.first) 3426 return true; 3427 3428 GlobalIndex.reset(Result.first); 3429 ModuleMgr.setGlobalIndex(GlobalIndex.get()); 3430 return false; 3431 } 3432 3433 bool ASTReader::isGlobalIndexUnavailable() const { 3434 return Context.getLangOpts().Modules && UseGlobalIndex && 3435 !hasGlobalIndex() && TriedLoadingGlobalIndex; 3436 } 3437 3438 static void updateModuleTimestamp(ModuleFile &MF) { 3439 // Overwrite the timestamp file contents so that file's mtime changes. 3440 std::string TimestampFilename = MF.getTimestampFilename(); 3441 std::error_code EC; 3442 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::F_Text); 3443 if (EC) 3444 return; 3445 OS << "Timestamp file\n"; 3446 } 3447 3448 /// \brief Given a cursor at the start of an AST file, scan ahead and drop the 3449 /// cursor into the start of the given block ID, returning false on success and 3450 /// true on failure. 3451 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) { 3452 while (1) { 3453 llvm::BitstreamEntry Entry = Cursor.advance(); 3454 switch (Entry.Kind) { 3455 case llvm::BitstreamEntry::Error: 3456 case llvm::BitstreamEntry::EndBlock: 3457 return true; 3458 3459 case llvm::BitstreamEntry::Record: 3460 // Ignore top-level records. 3461 Cursor.skipRecord(Entry.ID); 3462 break; 3463 3464 case llvm::BitstreamEntry::SubBlock: 3465 if (Entry.ID == BlockID) { 3466 if (Cursor.EnterSubBlock(BlockID)) 3467 return true; 3468 // Found it! 3469 return false; 3470 } 3471 3472 if (Cursor.SkipBlock()) 3473 return true; 3474 } 3475 } 3476 } 3477 3478 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, 3479 ModuleKind Type, 3480 SourceLocation ImportLoc, 3481 unsigned ClientLoadCapabilities) { 3482 llvm::SaveAndRestore<SourceLocation> 3483 SetCurImportLocRAII(CurrentImportLoc, ImportLoc); 3484 3485 // Defer any pending actions until we get to the end of reading the AST file. 3486 Deserializing AnASTFile(this); 3487 3488 // Bump the generation number. 3489 unsigned PreviousGeneration = incrementGeneration(Context); 3490 3491 unsigned NumModules = ModuleMgr.size(); 3492 SmallVector<ImportedModule, 4> Loaded; 3493 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc, 3494 /*ImportedBy=*/nullptr, Loaded, 3495 0, 0, 0, 3496 ClientLoadCapabilities)) { 3497 case Failure: 3498 case Missing: 3499 case OutOfDate: 3500 case VersionMismatch: 3501 case ConfigurationMismatch: 3502 case HadErrors: { 3503 llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet; 3504 for (const ImportedModule &IM : Loaded) 3505 LoadedSet.insert(IM.Mod); 3506 3507 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(), 3508 LoadedSet, 3509 Context.getLangOpts().Modules 3510 ? &PP.getHeaderSearchInfo().getModuleMap() 3511 : nullptr); 3512 3513 // If we find that any modules are unusable, the global index is going 3514 // to be out-of-date. Just remove it. 3515 GlobalIndex.reset(); 3516 ModuleMgr.setGlobalIndex(nullptr); 3517 return ReadResult; 3518 } 3519 case Success: 3520 break; 3521 } 3522 3523 // Here comes stuff that we only do once the entire chain is loaded. 3524 3525 // Load the AST blocks of all of the modules that we loaded. 3526 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(), 3527 MEnd = Loaded.end(); 3528 M != MEnd; ++M) { 3529 ModuleFile &F = *M->Mod; 3530 3531 // Read the AST block. 3532 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities)) 3533 return Result; 3534 3535 // Read the extension blocks. 3536 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) { 3537 if (ASTReadResult Result = ReadExtensionBlock(F)) 3538 return Result; 3539 } 3540 3541 // Once read, set the ModuleFile bit base offset and update the size in 3542 // bits of all files we've seen. 3543 F.GlobalBitOffset = TotalModulesSizeInBits; 3544 TotalModulesSizeInBits += F.SizeInBits; 3545 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F)); 3546 3547 // Preload SLocEntries. 3548 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) { 3549 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID; 3550 // Load it through the SourceManager and don't call ReadSLocEntry() 3551 // directly because the entry may have already been loaded in which case 3552 // calling ReadSLocEntry() directly would trigger an assertion in 3553 // SourceManager. 3554 SourceMgr.getLoadedSLocEntryByID(Index); 3555 } 3556 3557 // Preload all the pending interesting identifiers by marking them out of 3558 // date. 3559 for (auto Offset : F.PreloadIdentifierOffsets) { 3560 const unsigned char *Data = reinterpret_cast<const unsigned char *>( 3561 F.IdentifierTableData + Offset); 3562 3563 ASTIdentifierLookupTrait Trait(*this, F); 3564 auto KeyDataLen = Trait.ReadKeyDataLength(Data); 3565 auto Key = Trait.ReadKey(Data, KeyDataLen.first); 3566 auto &II = PP.getIdentifierTable().getOwn(Key); 3567 II.setOutOfDate(true); 3568 3569 // Mark this identifier as being from an AST file so that we can track 3570 // whether we need to serialize it. 3571 markIdentifierFromAST(*this, II); 3572 3573 // Associate the ID with the identifier so that the writer can reuse it. 3574 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first); 3575 SetIdentifierInfo(ID, &II); 3576 } 3577 } 3578 3579 // Setup the import locations and notify the module manager that we've 3580 // committed to these module files. 3581 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(), 3582 MEnd = Loaded.end(); 3583 M != MEnd; ++M) { 3584 ModuleFile &F = *M->Mod; 3585 3586 ModuleMgr.moduleFileAccepted(&F); 3587 3588 // Set the import location. 3589 F.DirectImportLoc = ImportLoc; 3590 // FIXME: We assume that locations from PCH / preamble do not need 3591 // any translation. 3592 if (!M->ImportedBy) 3593 F.ImportLoc = M->ImportLoc; 3594 else 3595 F.ImportLoc = TranslateSourceLocation(*M->ImportedBy, M->ImportLoc); 3596 } 3597 3598 if (!Context.getLangOpts().CPlusPlus || 3599 (Type != MK_ImplicitModule && Type != MK_ExplicitModule)) { 3600 // Mark all of the identifiers in the identifier table as being out of date, 3601 // so that various accessors know to check the loaded modules when the 3602 // identifier is used. 3603 // 3604 // For C++ modules, we don't need information on many identifiers (just 3605 // those that provide macros or are poisoned), so we mark all of 3606 // the interesting ones via PreloadIdentifierOffsets. 3607 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(), 3608 IdEnd = PP.getIdentifierTable().end(); 3609 Id != IdEnd; ++Id) 3610 Id->second->setOutOfDate(true); 3611 } 3612 // Mark selectors as out of date. 3613 for (auto Sel : SelectorGeneration) 3614 SelectorOutOfDate[Sel.first] = true; 3615 3616 // Resolve any unresolved module exports. 3617 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) { 3618 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I]; 3619 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID); 3620 Module *ResolvedMod = getSubmodule(GlobalID); 3621 3622 switch (Unresolved.Kind) { 3623 case UnresolvedModuleRef::Conflict: 3624 if (ResolvedMod) { 3625 Module::Conflict Conflict; 3626 Conflict.Other = ResolvedMod; 3627 Conflict.Message = Unresolved.String.str(); 3628 Unresolved.Mod->Conflicts.push_back(Conflict); 3629 } 3630 continue; 3631 3632 case UnresolvedModuleRef::Import: 3633 if (ResolvedMod) 3634 Unresolved.Mod->Imports.insert(ResolvedMod); 3635 continue; 3636 3637 case UnresolvedModuleRef::Export: 3638 if (ResolvedMod || Unresolved.IsWildcard) 3639 Unresolved.Mod->Exports.push_back( 3640 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard)); 3641 continue; 3642 } 3643 } 3644 UnresolvedModuleRefs.clear(); 3645 3646 // FIXME: How do we load the 'use'd modules? They may not be submodules. 3647 // Might be unnecessary as use declarations are only used to build the 3648 // module itself. 3649 3650 InitializeContext(); 3651 3652 if (SemaObj) 3653 UpdateSema(); 3654 3655 if (DeserializationListener) 3656 DeserializationListener->ReaderInitialized(this); 3657 3658 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule(); 3659 if (PrimaryModule.OriginalSourceFileID.isValid()) { 3660 PrimaryModule.OriginalSourceFileID 3661 = FileID::get(PrimaryModule.SLocEntryBaseID 3662 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1); 3663 3664 // If this AST file is a precompiled preamble, then set the 3665 // preamble file ID of the source manager to the file source file 3666 // from which the preamble was built. 3667 if (Type == MK_Preamble) { 3668 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID); 3669 } else if (Type == MK_MainFile) { 3670 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID); 3671 } 3672 } 3673 3674 // For any Objective-C class definitions we have already loaded, make sure 3675 // that we load any additional categories. 3676 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) { 3677 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(), 3678 ObjCClassesLoaded[I], 3679 PreviousGeneration); 3680 } 3681 3682 if (PP.getHeaderSearchInfo() 3683 .getHeaderSearchOpts() 3684 .ModulesValidateOncePerBuildSession) { 3685 // Now we are certain that the module and all modules it depends on are 3686 // up to date. Create or update timestamp files for modules that are 3687 // located in the module cache (not for PCH files that could be anywhere 3688 // in the filesystem). 3689 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) { 3690 ImportedModule &M = Loaded[I]; 3691 if (M.Mod->Kind == MK_ImplicitModule) { 3692 updateModuleTimestamp(*M.Mod); 3693 } 3694 } 3695 } 3696 3697 return Success; 3698 } 3699 3700 static ASTFileSignature readASTFileSignature(llvm::BitstreamReader &StreamFile); 3701 3702 /// \brief Whether \p Stream starts with the AST/PCH file magic number 'CPCH'. 3703 static bool startsWithASTFileMagic(BitstreamCursor &Stream) { 3704 return Stream.Read(8) == 'C' && 3705 Stream.Read(8) == 'P' && 3706 Stream.Read(8) == 'C' && 3707 Stream.Read(8) == 'H'; 3708 } 3709 3710 static unsigned moduleKindForDiagnostic(ModuleKind Kind) { 3711 switch (Kind) { 3712 case MK_PCH: 3713 return 0; // PCH 3714 case MK_ImplicitModule: 3715 case MK_ExplicitModule: 3716 return 1; // module 3717 case MK_MainFile: 3718 case MK_Preamble: 3719 return 2; // main source file 3720 } 3721 llvm_unreachable("unknown module kind"); 3722 } 3723 3724 ASTReader::ASTReadResult 3725 ASTReader::ReadASTCore(StringRef FileName, 3726 ModuleKind Type, 3727 SourceLocation ImportLoc, 3728 ModuleFile *ImportedBy, 3729 SmallVectorImpl<ImportedModule> &Loaded, 3730 off_t ExpectedSize, time_t ExpectedModTime, 3731 ASTFileSignature ExpectedSignature, 3732 unsigned ClientLoadCapabilities) { 3733 ModuleFile *M; 3734 std::string ErrorStr; 3735 ModuleManager::AddModuleResult AddResult 3736 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy, 3737 getGeneration(), ExpectedSize, ExpectedModTime, 3738 ExpectedSignature, readASTFileSignature, 3739 M, ErrorStr); 3740 3741 switch (AddResult) { 3742 case ModuleManager::AlreadyLoaded: 3743 return Success; 3744 3745 case ModuleManager::NewlyLoaded: 3746 // Load module file below. 3747 break; 3748 3749 case ModuleManager::Missing: 3750 // The module file was missing; if the client can handle that, return 3751 // it. 3752 if (ClientLoadCapabilities & ARR_Missing) 3753 return Missing; 3754 3755 // Otherwise, return an error. 3756 Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type) 3757 << FileName << ErrorStr.empty() 3758 << ErrorStr; 3759 return Failure; 3760 3761 case ModuleManager::OutOfDate: 3762 // We couldn't load the module file because it is out-of-date. If the 3763 // client can handle out-of-date, return it. 3764 if (ClientLoadCapabilities & ARR_OutOfDate) 3765 return OutOfDate; 3766 3767 // Otherwise, return an error. 3768 Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type) 3769 << FileName << ErrorStr.empty() 3770 << ErrorStr; 3771 return Failure; 3772 } 3773 3774 assert(M && "Missing module file"); 3775 3776 // FIXME: This seems rather a hack. Should CurrentDir be part of the 3777 // module? 3778 if (FileName != "-") { 3779 CurrentDir = llvm::sys::path::parent_path(FileName); 3780 if (CurrentDir.empty()) CurrentDir = "."; 3781 } 3782 3783 ModuleFile &F = *M; 3784 BitstreamCursor &Stream = F.Stream; 3785 PCHContainerRdr.ExtractPCH(F.Buffer->getMemBufferRef(), F.StreamFile); 3786 Stream.init(&F.StreamFile); 3787 F.SizeInBits = F.Buffer->getBufferSize() * 8; 3788 3789 // Sniff for the signature. 3790 if (!startsWithASTFileMagic(Stream)) { 3791 Diag(diag::err_module_file_invalid) << moduleKindForDiagnostic(Type) 3792 << FileName; 3793 return Failure; 3794 } 3795 3796 // This is used for compatibility with older PCH formats. 3797 bool HaveReadControlBlock = false; 3798 while (1) { 3799 llvm::BitstreamEntry Entry = Stream.advance(); 3800 3801 switch (Entry.Kind) { 3802 case llvm::BitstreamEntry::Error: 3803 case llvm::BitstreamEntry::Record: 3804 case llvm::BitstreamEntry::EndBlock: 3805 Error("invalid record at top-level of AST file"); 3806 return Failure; 3807 3808 case llvm::BitstreamEntry::SubBlock: 3809 break; 3810 } 3811 3812 switch (Entry.ID) { 3813 case CONTROL_BLOCK_ID: 3814 HaveReadControlBlock = true; 3815 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) { 3816 case Success: 3817 // Check that we didn't try to load a non-module AST file as a module. 3818 // 3819 // FIXME: Should we also perform the converse check? Loading a module as 3820 // a PCH file sort of works, but it's a bit wonky. 3821 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule) && 3822 F.ModuleName.empty()) { 3823 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure; 3824 if (Result != OutOfDate || 3825 (ClientLoadCapabilities & ARR_OutOfDate) == 0) 3826 Diag(diag::err_module_file_not_module) << FileName; 3827 return Result; 3828 } 3829 break; 3830 3831 case Failure: return Failure; 3832 case Missing: return Missing; 3833 case OutOfDate: return OutOfDate; 3834 case VersionMismatch: return VersionMismatch; 3835 case ConfigurationMismatch: return ConfigurationMismatch; 3836 case HadErrors: return HadErrors; 3837 } 3838 break; 3839 3840 case AST_BLOCK_ID: 3841 if (!HaveReadControlBlock) { 3842 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 3843 Diag(diag::err_pch_version_too_old); 3844 return VersionMismatch; 3845 } 3846 3847 // Record that we've loaded this module. 3848 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc)); 3849 return Success; 3850 3851 default: 3852 if (Stream.SkipBlock()) { 3853 Error("malformed block record in AST file"); 3854 return Failure; 3855 } 3856 break; 3857 } 3858 } 3859 3860 return Success; 3861 } 3862 3863 /// Parse a record and blob containing module file extension metadata. 3864 static bool parseModuleFileExtensionMetadata( 3865 const SmallVectorImpl<uint64_t> &Record, 3866 StringRef Blob, 3867 ModuleFileExtensionMetadata &Metadata) { 3868 if (Record.size() < 4) return true; 3869 3870 Metadata.MajorVersion = Record[0]; 3871 Metadata.MinorVersion = Record[1]; 3872 3873 unsigned BlockNameLen = Record[2]; 3874 unsigned UserInfoLen = Record[3]; 3875 3876 if (BlockNameLen + UserInfoLen > Blob.size()) return true; 3877 3878 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen); 3879 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen, 3880 Blob.data() + BlockNameLen + UserInfoLen); 3881 return false; 3882 } 3883 3884 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) { 3885 BitstreamCursor &Stream = F.Stream; 3886 3887 RecordData Record; 3888 while (true) { 3889 llvm::BitstreamEntry Entry = Stream.advance(); 3890 switch (Entry.Kind) { 3891 case llvm::BitstreamEntry::SubBlock: 3892 if (Stream.SkipBlock()) 3893 return Failure; 3894 3895 continue; 3896 3897 case llvm::BitstreamEntry::EndBlock: 3898 return Success; 3899 3900 case llvm::BitstreamEntry::Error: 3901 return HadErrors; 3902 3903 case llvm::BitstreamEntry::Record: 3904 break; 3905 } 3906 3907 Record.clear(); 3908 StringRef Blob; 3909 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob); 3910 switch (RecCode) { 3911 case EXTENSION_METADATA: { 3912 ModuleFileExtensionMetadata Metadata; 3913 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) 3914 return Failure; 3915 3916 // Find a module file extension with this block name. 3917 auto Known = ModuleFileExtensions.find(Metadata.BlockName); 3918 if (Known == ModuleFileExtensions.end()) break; 3919 3920 // Form a reader. 3921 if (auto Reader = Known->second->createExtensionReader(Metadata, *this, 3922 F, Stream)) { 3923 F.ExtensionReaders.push_back(std::move(Reader)); 3924 } 3925 3926 break; 3927 } 3928 } 3929 } 3930 3931 return Success; 3932 } 3933 3934 void ASTReader::InitializeContext() { 3935 // If there's a listener, notify them that we "read" the translation unit. 3936 if (DeserializationListener) 3937 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, 3938 Context.getTranslationUnitDecl()); 3939 3940 // FIXME: Find a better way to deal with collisions between these 3941 // built-in types. Right now, we just ignore the problem. 3942 3943 // Load the special types. 3944 if (SpecialTypes.size() >= NumSpecialTypeIDs) { 3945 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) { 3946 if (!Context.CFConstantStringTypeDecl) 3947 Context.setCFConstantStringType(GetType(String)); 3948 } 3949 3950 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) { 3951 QualType FileType = GetType(File); 3952 if (FileType.isNull()) { 3953 Error("FILE type is NULL"); 3954 return; 3955 } 3956 3957 if (!Context.FILEDecl) { 3958 if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) 3959 Context.setFILEDecl(Typedef->getDecl()); 3960 else { 3961 const TagType *Tag = FileType->getAs<TagType>(); 3962 if (!Tag) { 3963 Error("Invalid FILE type in AST file"); 3964 return; 3965 } 3966 Context.setFILEDecl(Tag->getDecl()); 3967 } 3968 } 3969 } 3970 3971 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) { 3972 QualType Jmp_bufType = GetType(Jmp_buf); 3973 if (Jmp_bufType.isNull()) { 3974 Error("jmp_buf type is NULL"); 3975 return; 3976 } 3977 3978 if (!Context.jmp_bufDecl) { 3979 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) 3980 Context.setjmp_bufDecl(Typedef->getDecl()); 3981 else { 3982 const TagType *Tag = Jmp_bufType->getAs<TagType>(); 3983 if (!Tag) { 3984 Error("Invalid jmp_buf type in AST file"); 3985 return; 3986 } 3987 Context.setjmp_bufDecl(Tag->getDecl()); 3988 } 3989 } 3990 } 3991 3992 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) { 3993 QualType Sigjmp_bufType = GetType(Sigjmp_buf); 3994 if (Sigjmp_bufType.isNull()) { 3995 Error("sigjmp_buf type is NULL"); 3996 return; 3997 } 3998 3999 if (!Context.sigjmp_bufDecl) { 4000 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) 4001 Context.setsigjmp_bufDecl(Typedef->getDecl()); 4002 else { 4003 const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); 4004 assert(Tag && "Invalid sigjmp_buf type in AST file"); 4005 Context.setsigjmp_bufDecl(Tag->getDecl()); 4006 } 4007 } 4008 } 4009 4010 if (unsigned ObjCIdRedef 4011 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) { 4012 if (Context.ObjCIdRedefinitionType.isNull()) 4013 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef); 4014 } 4015 4016 if (unsigned ObjCClassRedef 4017 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) { 4018 if (Context.ObjCClassRedefinitionType.isNull()) 4019 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef); 4020 } 4021 4022 if (unsigned ObjCSelRedef 4023 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) { 4024 if (Context.ObjCSelRedefinitionType.isNull()) 4025 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef); 4026 } 4027 4028 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) { 4029 QualType Ucontext_tType = GetType(Ucontext_t); 4030 if (Ucontext_tType.isNull()) { 4031 Error("ucontext_t type is NULL"); 4032 return; 4033 } 4034 4035 if (!Context.ucontext_tDecl) { 4036 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>()) 4037 Context.setucontext_tDecl(Typedef->getDecl()); 4038 else { 4039 const TagType *Tag = Ucontext_tType->getAs<TagType>(); 4040 assert(Tag && "Invalid ucontext_t type in AST file"); 4041 Context.setucontext_tDecl(Tag->getDecl()); 4042 } 4043 } 4044 } 4045 } 4046 4047 ReadPragmaDiagnosticMappings(Context.getDiagnostics()); 4048 4049 // If there were any CUDA special declarations, deserialize them. 4050 if (!CUDASpecialDeclRefs.empty()) { 4051 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!"); 4052 Context.setcudaConfigureCallDecl( 4053 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0]))); 4054 } 4055 4056 // Re-export any modules that were imported by a non-module AST file. 4057 // FIXME: This does not make macro-only imports visible again. 4058 for (auto &Import : ImportedModules) { 4059 if (Module *Imported = getSubmodule(Import.ID)) { 4060 makeModuleVisible(Imported, Module::AllVisible, 4061 /*ImportLoc=*/Import.ImportLoc); 4062 if (Import.ImportLoc.isValid()) 4063 PP.makeModuleVisible(Imported, Import.ImportLoc); 4064 // FIXME: should we tell Sema to make the module visible too? 4065 } 4066 } 4067 ImportedModules.clear(); 4068 } 4069 4070 void ASTReader::finalizeForWriting() { 4071 // Nothing to do for now. 4072 } 4073 4074 /// \brief Reads and return the signature record from \p StreamFile's control 4075 /// block, or else returns 0. 4076 static ASTFileSignature readASTFileSignature(llvm::BitstreamReader &StreamFile){ 4077 BitstreamCursor Stream(StreamFile); 4078 if (!startsWithASTFileMagic(Stream)) 4079 return 0; 4080 4081 // Scan for the CONTROL_BLOCK_ID block. 4082 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) 4083 return 0; 4084 4085 // Scan for SIGNATURE inside the control block. 4086 ASTReader::RecordData Record; 4087 while (1) { 4088 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 4089 if (Entry.Kind == llvm::BitstreamEntry::EndBlock || 4090 Entry.Kind != llvm::BitstreamEntry::Record) 4091 return 0; 4092 4093 Record.clear(); 4094 StringRef Blob; 4095 if (SIGNATURE == Stream.readRecord(Entry.ID, Record, &Blob)) 4096 return Record[0]; 4097 } 4098 } 4099 4100 /// \brief Retrieve the name of the original source file name 4101 /// directly from the AST file, without actually loading the AST 4102 /// file. 4103 std::string ASTReader::getOriginalSourceFile( 4104 const std::string &ASTFileName, FileManager &FileMgr, 4105 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) { 4106 // Open the AST file. 4107 auto Buffer = FileMgr.getBufferForFile(ASTFileName); 4108 if (!Buffer) { 4109 Diags.Report(diag::err_fe_unable_to_read_pch_file) 4110 << ASTFileName << Buffer.getError().message(); 4111 return std::string(); 4112 } 4113 4114 // Initialize the stream 4115 llvm::BitstreamReader StreamFile; 4116 PCHContainerRdr.ExtractPCH((*Buffer)->getMemBufferRef(), StreamFile); 4117 BitstreamCursor Stream(StreamFile); 4118 4119 // Sniff for the signature. 4120 if (!startsWithASTFileMagic(Stream)) { 4121 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName; 4122 return std::string(); 4123 } 4124 4125 // Scan for the CONTROL_BLOCK_ID block. 4126 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) { 4127 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 4128 return std::string(); 4129 } 4130 4131 // Scan for ORIGINAL_FILE inside the control block. 4132 RecordData Record; 4133 while (1) { 4134 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 4135 if (Entry.Kind == llvm::BitstreamEntry::EndBlock) 4136 return std::string(); 4137 4138 if (Entry.Kind != llvm::BitstreamEntry::Record) { 4139 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 4140 return std::string(); 4141 } 4142 4143 Record.clear(); 4144 StringRef Blob; 4145 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE) 4146 return Blob.str(); 4147 } 4148 } 4149 4150 namespace { 4151 class SimplePCHValidator : public ASTReaderListener { 4152 const LangOptions &ExistingLangOpts; 4153 const TargetOptions &ExistingTargetOpts; 4154 const PreprocessorOptions &ExistingPPOpts; 4155 std::string ExistingModuleCachePath; 4156 FileManager &FileMgr; 4157 4158 public: 4159 SimplePCHValidator(const LangOptions &ExistingLangOpts, 4160 const TargetOptions &ExistingTargetOpts, 4161 const PreprocessorOptions &ExistingPPOpts, 4162 StringRef ExistingModuleCachePath, 4163 FileManager &FileMgr) 4164 : ExistingLangOpts(ExistingLangOpts), 4165 ExistingTargetOpts(ExistingTargetOpts), 4166 ExistingPPOpts(ExistingPPOpts), 4167 ExistingModuleCachePath(ExistingModuleCachePath), 4168 FileMgr(FileMgr) 4169 { 4170 } 4171 4172 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain, 4173 bool AllowCompatibleDifferences) override { 4174 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr, 4175 AllowCompatibleDifferences); 4176 } 4177 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain, 4178 bool AllowCompatibleDifferences) override { 4179 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr, 4180 AllowCompatibleDifferences); 4181 } 4182 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 4183 StringRef SpecificModuleCachePath, 4184 bool Complain) override { 4185 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 4186 ExistingModuleCachePath, 4187 nullptr, ExistingLangOpts); 4188 } 4189 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 4190 bool Complain, 4191 std::string &SuggestedPredefines) override { 4192 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr, 4193 SuggestedPredefines, ExistingLangOpts); 4194 } 4195 }; 4196 } 4197 4198 bool ASTReader::readASTFileControlBlock( 4199 StringRef Filename, FileManager &FileMgr, 4200 const PCHContainerReader &PCHContainerRdr, 4201 bool FindModuleFileExtensions, 4202 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) { 4203 // Open the AST file. 4204 // FIXME: This allows use of the VFS; we do not allow use of the 4205 // VFS when actually loading a module. 4206 auto Buffer = FileMgr.getBufferForFile(Filename); 4207 if (!Buffer) { 4208 return true; 4209 } 4210 4211 // Initialize the stream 4212 llvm::BitstreamReader StreamFile; 4213 PCHContainerRdr.ExtractPCH((*Buffer)->getMemBufferRef(), StreamFile); 4214 BitstreamCursor Stream(StreamFile); 4215 4216 // Sniff for the signature. 4217 if (!startsWithASTFileMagic(Stream)) 4218 return true; 4219 4220 // Scan for the CONTROL_BLOCK_ID block. 4221 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) 4222 return true; 4223 4224 bool NeedsInputFiles = Listener.needsInputFileVisitation(); 4225 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation(); 4226 bool NeedsImports = Listener.needsImportVisitation(); 4227 BitstreamCursor InputFilesCursor; 4228 4229 RecordData Record; 4230 std::string ModuleDir; 4231 bool DoneWithControlBlock = false; 4232 while (!DoneWithControlBlock) { 4233 llvm::BitstreamEntry Entry = Stream.advance(); 4234 4235 switch (Entry.Kind) { 4236 case llvm::BitstreamEntry::SubBlock: { 4237 switch (Entry.ID) { 4238 case OPTIONS_BLOCK_ID: { 4239 std::string IgnoredSuggestedPredefines; 4240 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate, 4241 /*AllowCompatibleConfigurationMismatch*/ false, 4242 Listener, IgnoredSuggestedPredefines, 4243 ValidateDiagnosticOptions) != Success) 4244 return true; 4245 break; 4246 } 4247 4248 case INPUT_FILES_BLOCK_ID: 4249 InputFilesCursor = Stream; 4250 if (Stream.SkipBlock() || 4251 (NeedsInputFiles && 4252 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))) 4253 return true; 4254 break; 4255 4256 default: 4257 if (Stream.SkipBlock()) 4258 return true; 4259 break; 4260 } 4261 4262 continue; 4263 } 4264 4265 case llvm::BitstreamEntry::EndBlock: 4266 DoneWithControlBlock = true; 4267 break; 4268 4269 case llvm::BitstreamEntry::Error: 4270 return true; 4271 4272 case llvm::BitstreamEntry::Record: 4273 break; 4274 } 4275 4276 if (DoneWithControlBlock) break; 4277 4278 Record.clear(); 4279 StringRef Blob; 4280 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob); 4281 switch ((ControlRecordTypes)RecCode) { 4282 case METADATA: { 4283 if (Record[0] != VERSION_MAJOR) 4284 return true; 4285 4286 if (Listener.ReadFullVersionInformation(Blob)) 4287 return true; 4288 4289 break; 4290 } 4291 case MODULE_NAME: 4292 Listener.ReadModuleName(Blob); 4293 break; 4294 case MODULE_DIRECTORY: 4295 ModuleDir = Blob; 4296 break; 4297 case MODULE_MAP_FILE: { 4298 unsigned Idx = 0; 4299 auto Path = ReadString(Record, Idx); 4300 ResolveImportedPath(Path, ModuleDir); 4301 Listener.ReadModuleMapFile(Path); 4302 break; 4303 } 4304 case INPUT_FILE_OFFSETS: { 4305 if (!NeedsInputFiles) 4306 break; 4307 4308 unsigned NumInputFiles = Record[0]; 4309 unsigned NumUserFiles = Record[1]; 4310 const uint64_t *InputFileOffs = (const uint64_t *)Blob.data(); 4311 for (unsigned I = 0; I != NumInputFiles; ++I) { 4312 // Go find this input file. 4313 bool isSystemFile = I >= NumUserFiles; 4314 4315 if (isSystemFile && !NeedsSystemInputFiles) 4316 break; // the rest are system input files 4317 4318 BitstreamCursor &Cursor = InputFilesCursor; 4319 SavedStreamPosition SavedPosition(Cursor); 4320 Cursor.JumpToBit(InputFileOffs[I]); 4321 4322 unsigned Code = Cursor.ReadCode(); 4323 RecordData Record; 4324 StringRef Blob; 4325 bool shouldContinue = false; 4326 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) { 4327 case INPUT_FILE: 4328 bool Overridden = static_cast<bool>(Record[3]); 4329 std::string Filename = Blob; 4330 ResolveImportedPath(Filename, ModuleDir); 4331 shouldContinue = Listener.visitInputFile( 4332 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false); 4333 break; 4334 } 4335 if (!shouldContinue) 4336 break; 4337 } 4338 break; 4339 } 4340 4341 case IMPORTS: { 4342 if (!NeedsImports) 4343 break; 4344 4345 unsigned Idx = 0, N = Record.size(); 4346 while (Idx < N) { 4347 // Read information about the AST file. 4348 Idx += 5; // ImportLoc, Size, ModTime, Signature 4349 std::string Filename = ReadString(Record, Idx); 4350 ResolveImportedPath(Filename, ModuleDir); 4351 Listener.visitImport(Filename); 4352 } 4353 break; 4354 } 4355 4356 default: 4357 // No other validation to perform. 4358 break; 4359 } 4360 } 4361 4362 // Look for module file extension blocks, if requested. 4363 if (FindModuleFileExtensions) { 4364 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) { 4365 bool DoneWithExtensionBlock = false; 4366 while (!DoneWithExtensionBlock) { 4367 llvm::BitstreamEntry Entry = Stream.advance(); 4368 4369 switch (Entry.Kind) { 4370 case llvm::BitstreamEntry::SubBlock: 4371 if (Stream.SkipBlock()) 4372 return true; 4373 4374 continue; 4375 4376 case llvm::BitstreamEntry::EndBlock: 4377 DoneWithExtensionBlock = true; 4378 continue; 4379 4380 case llvm::BitstreamEntry::Error: 4381 return true; 4382 4383 case llvm::BitstreamEntry::Record: 4384 break; 4385 } 4386 4387 Record.clear(); 4388 StringRef Blob; 4389 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob); 4390 switch (RecCode) { 4391 case EXTENSION_METADATA: { 4392 ModuleFileExtensionMetadata Metadata; 4393 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) 4394 return true; 4395 4396 Listener.readModuleFileExtension(Metadata); 4397 break; 4398 } 4399 } 4400 } 4401 } 4402 } 4403 4404 return false; 4405 } 4406 4407 bool ASTReader::isAcceptableASTFile( 4408 StringRef Filename, FileManager &FileMgr, 4409 const PCHContainerReader &PCHContainerRdr, const LangOptions &LangOpts, 4410 const TargetOptions &TargetOpts, const PreprocessorOptions &PPOpts, 4411 std::string ExistingModuleCachePath) { 4412 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, 4413 ExistingModuleCachePath, FileMgr); 4414 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr, 4415 /*FindModuleFileExtensions=*/false, 4416 validator, 4417 /*ValidateDiagnosticOptions=*/true); 4418 } 4419 4420 ASTReader::ASTReadResult 4421 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) { 4422 // Enter the submodule block. 4423 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) { 4424 Error("malformed submodule block record in AST file"); 4425 return Failure; 4426 } 4427 4428 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap(); 4429 bool First = true; 4430 Module *CurrentModule = nullptr; 4431 RecordData Record; 4432 while (true) { 4433 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks(); 4434 4435 switch (Entry.Kind) { 4436 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 4437 case llvm::BitstreamEntry::Error: 4438 Error("malformed block record in AST file"); 4439 return Failure; 4440 case llvm::BitstreamEntry::EndBlock: 4441 return Success; 4442 case llvm::BitstreamEntry::Record: 4443 // The interesting case. 4444 break; 4445 } 4446 4447 // Read a record. 4448 StringRef Blob; 4449 Record.clear(); 4450 auto Kind = F.Stream.readRecord(Entry.ID, Record, &Blob); 4451 4452 if ((Kind == SUBMODULE_METADATA) != First) { 4453 Error("submodule metadata record should be at beginning of block"); 4454 return Failure; 4455 } 4456 First = false; 4457 4458 // Submodule information is only valid if we have a current module. 4459 // FIXME: Should we error on these cases? 4460 if (!CurrentModule && Kind != SUBMODULE_METADATA && 4461 Kind != SUBMODULE_DEFINITION) 4462 continue; 4463 4464 switch (Kind) { 4465 default: // Default behavior: ignore. 4466 break; 4467 4468 case SUBMODULE_DEFINITION: { 4469 if (Record.size() < 8) { 4470 Error("malformed module definition"); 4471 return Failure; 4472 } 4473 4474 StringRef Name = Blob; 4475 unsigned Idx = 0; 4476 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]); 4477 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]); 4478 bool IsFramework = Record[Idx++]; 4479 bool IsExplicit = Record[Idx++]; 4480 bool IsSystem = Record[Idx++]; 4481 bool IsExternC = Record[Idx++]; 4482 bool InferSubmodules = Record[Idx++]; 4483 bool InferExplicitSubmodules = Record[Idx++]; 4484 bool InferExportWildcard = Record[Idx++]; 4485 bool ConfigMacrosExhaustive = Record[Idx++]; 4486 4487 Module *ParentModule = nullptr; 4488 if (Parent) 4489 ParentModule = getSubmodule(Parent); 4490 4491 // Retrieve this (sub)module from the module map, creating it if 4492 // necessary. 4493 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule, IsFramework, 4494 IsExplicit).first; 4495 4496 // FIXME: set the definition loc for CurrentModule, or call 4497 // ModMap.setInferredModuleAllowedBy() 4498 4499 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS; 4500 if (GlobalIndex >= SubmodulesLoaded.size() || 4501 SubmodulesLoaded[GlobalIndex]) { 4502 Error("too many submodules"); 4503 return Failure; 4504 } 4505 4506 if (!ParentModule) { 4507 if (const FileEntry *CurFile = CurrentModule->getASTFile()) { 4508 if (CurFile != F.File) { 4509 if (!Diags.isDiagnosticInFlight()) { 4510 Diag(diag::err_module_file_conflict) 4511 << CurrentModule->getTopLevelModuleName() 4512 << CurFile->getName() 4513 << F.File->getName(); 4514 } 4515 return Failure; 4516 } 4517 } 4518 4519 CurrentModule->setASTFile(F.File); 4520 } 4521 4522 CurrentModule->Signature = F.Signature; 4523 CurrentModule->IsFromModuleFile = true; 4524 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem; 4525 CurrentModule->IsExternC = IsExternC; 4526 CurrentModule->InferSubmodules = InferSubmodules; 4527 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules; 4528 CurrentModule->InferExportWildcard = InferExportWildcard; 4529 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive; 4530 if (DeserializationListener) 4531 DeserializationListener->ModuleRead(GlobalID, CurrentModule); 4532 4533 SubmodulesLoaded[GlobalIndex] = CurrentModule; 4534 4535 // Clear out data that will be replaced by what is in the module file. 4536 CurrentModule->LinkLibraries.clear(); 4537 CurrentModule->ConfigMacros.clear(); 4538 CurrentModule->UnresolvedConflicts.clear(); 4539 CurrentModule->Conflicts.clear(); 4540 4541 // The module is available unless it's missing a requirement; relevant 4542 // requirements will be (re-)added by SUBMODULE_REQUIRES records. 4543 // Missing headers that were present when the module was built do not 4544 // make it unavailable -- if we got this far, this must be an explicitly 4545 // imported module file. 4546 CurrentModule->Requirements.clear(); 4547 CurrentModule->MissingHeaders.clear(); 4548 CurrentModule->IsMissingRequirement = 4549 ParentModule && ParentModule->IsMissingRequirement; 4550 CurrentModule->IsAvailable = !CurrentModule->IsMissingRequirement; 4551 break; 4552 } 4553 4554 case SUBMODULE_UMBRELLA_HEADER: { 4555 std::string Filename = Blob; 4556 ResolveImportedPath(F, Filename); 4557 if (auto *Umbrella = PP.getFileManager().getFile(Filename)) { 4558 if (!CurrentModule->getUmbrellaHeader()) 4559 ModMap.setUmbrellaHeader(CurrentModule, Umbrella, Blob); 4560 else if (CurrentModule->getUmbrellaHeader().Entry != Umbrella) { 4561 // This can be a spurious difference caused by changing the VFS to 4562 // point to a different copy of the file, and it is too late to 4563 // to rebuild safely. 4564 // FIXME: If we wrote the virtual paths instead of the 'real' paths, 4565 // after input file validation only real problems would remain and we 4566 // could just error. For now, assume it's okay. 4567 break; 4568 } 4569 } 4570 break; 4571 } 4572 4573 case SUBMODULE_HEADER: 4574 case SUBMODULE_EXCLUDED_HEADER: 4575 case SUBMODULE_PRIVATE_HEADER: 4576 // We lazily associate headers with their modules via the HeaderInfo table. 4577 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead 4578 // of complete filenames or remove it entirely. 4579 break; 4580 4581 case SUBMODULE_TEXTUAL_HEADER: 4582 case SUBMODULE_PRIVATE_TEXTUAL_HEADER: 4583 // FIXME: Textual headers are not marked in the HeaderInfo table. Load 4584 // them here. 4585 break; 4586 4587 case SUBMODULE_TOPHEADER: { 4588 CurrentModule->addTopHeaderFilename(Blob); 4589 break; 4590 } 4591 4592 case SUBMODULE_UMBRELLA_DIR: { 4593 std::string Dirname = Blob; 4594 ResolveImportedPath(F, Dirname); 4595 if (auto *Umbrella = PP.getFileManager().getDirectory(Dirname)) { 4596 if (!CurrentModule->getUmbrellaDir()) 4597 ModMap.setUmbrellaDir(CurrentModule, Umbrella, Blob); 4598 else if (CurrentModule->getUmbrellaDir().Entry != Umbrella) { 4599 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 4600 Error("mismatched umbrella directories in submodule"); 4601 return OutOfDate; 4602 } 4603 } 4604 break; 4605 } 4606 4607 case SUBMODULE_METADATA: { 4608 F.BaseSubmoduleID = getTotalNumSubmodules(); 4609 F.LocalNumSubmodules = Record[0]; 4610 unsigned LocalBaseSubmoduleID = Record[1]; 4611 if (F.LocalNumSubmodules > 0) { 4612 // Introduce the global -> local mapping for submodules within this 4613 // module. 4614 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F)); 4615 4616 // Introduce the local -> global mapping for submodules within this 4617 // module. 4618 F.SubmoduleRemap.insertOrReplace( 4619 std::make_pair(LocalBaseSubmoduleID, 4620 F.BaseSubmoduleID - LocalBaseSubmoduleID)); 4621 4622 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules); 4623 } 4624 break; 4625 } 4626 4627 case SUBMODULE_IMPORTS: { 4628 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) { 4629 UnresolvedModuleRef Unresolved; 4630 Unresolved.File = &F; 4631 Unresolved.Mod = CurrentModule; 4632 Unresolved.ID = Record[Idx]; 4633 Unresolved.Kind = UnresolvedModuleRef::Import; 4634 Unresolved.IsWildcard = false; 4635 UnresolvedModuleRefs.push_back(Unresolved); 4636 } 4637 break; 4638 } 4639 4640 case SUBMODULE_EXPORTS: { 4641 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) { 4642 UnresolvedModuleRef Unresolved; 4643 Unresolved.File = &F; 4644 Unresolved.Mod = CurrentModule; 4645 Unresolved.ID = Record[Idx]; 4646 Unresolved.Kind = UnresolvedModuleRef::Export; 4647 Unresolved.IsWildcard = Record[Idx + 1]; 4648 UnresolvedModuleRefs.push_back(Unresolved); 4649 } 4650 4651 // Once we've loaded the set of exports, there's no reason to keep 4652 // the parsed, unresolved exports around. 4653 CurrentModule->UnresolvedExports.clear(); 4654 break; 4655 } 4656 case SUBMODULE_REQUIRES: { 4657 CurrentModule->addRequirement(Blob, Record[0], Context.getLangOpts(), 4658 Context.getTargetInfo()); 4659 break; 4660 } 4661 4662 case SUBMODULE_LINK_LIBRARY: 4663 CurrentModule->LinkLibraries.push_back( 4664 Module::LinkLibrary(Blob, Record[0])); 4665 break; 4666 4667 case SUBMODULE_CONFIG_MACRO: 4668 CurrentModule->ConfigMacros.push_back(Blob.str()); 4669 break; 4670 4671 case SUBMODULE_CONFLICT: { 4672 UnresolvedModuleRef Unresolved; 4673 Unresolved.File = &F; 4674 Unresolved.Mod = CurrentModule; 4675 Unresolved.ID = Record[0]; 4676 Unresolved.Kind = UnresolvedModuleRef::Conflict; 4677 Unresolved.IsWildcard = false; 4678 Unresolved.String = Blob; 4679 UnresolvedModuleRefs.push_back(Unresolved); 4680 break; 4681 } 4682 4683 case SUBMODULE_INITIALIZERS: 4684 SmallVector<uint32_t, 16> Inits; 4685 for (auto &ID : Record) 4686 Inits.push_back(getGlobalDeclID(F, ID)); 4687 Context.addLazyModuleInitializers(CurrentModule, Inits); 4688 break; 4689 } 4690 } 4691 } 4692 4693 /// \brief Parse the record that corresponds to a LangOptions data 4694 /// structure. 4695 /// 4696 /// This routine parses the language options from the AST file and then gives 4697 /// them to the AST listener if one is set. 4698 /// 4699 /// \returns true if the listener deems the file unacceptable, false otherwise. 4700 bool ASTReader::ParseLanguageOptions(const RecordData &Record, 4701 bool Complain, 4702 ASTReaderListener &Listener, 4703 bool AllowCompatibleDifferences) { 4704 LangOptions LangOpts; 4705 unsigned Idx = 0; 4706 #define LANGOPT(Name, Bits, Default, Description) \ 4707 LangOpts.Name = Record[Idx++]; 4708 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 4709 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++])); 4710 #include "clang/Basic/LangOptions.def" 4711 #define SANITIZER(NAME, ID) \ 4712 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]); 4713 #include "clang/Basic/Sanitizers.def" 4714 4715 for (unsigned N = Record[Idx++]; N; --N) 4716 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx)); 4717 4718 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++]; 4719 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx); 4720 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion); 4721 4722 LangOpts.CurrentModule = ReadString(Record, Idx); 4723 4724 // Comment options. 4725 for (unsigned N = Record[Idx++]; N; --N) { 4726 LangOpts.CommentOpts.BlockCommandNames.push_back( 4727 ReadString(Record, Idx)); 4728 } 4729 LangOpts.CommentOpts.ParseAllComments = Record[Idx++]; 4730 4731 // OpenMP offloading options. 4732 for (unsigned N = Record[Idx++]; N; --N) { 4733 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx))); 4734 } 4735 4736 LangOpts.OMPHostIRFile = ReadString(Record, Idx); 4737 4738 return Listener.ReadLanguageOptions(LangOpts, Complain, 4739 AllowCompatibleDifferences); 4740 } 4741 4742 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain, 4743 ASTReaderListener &Listener, 4744 bool AllowCompatibleDifferences) { 4745 unsigned Idx = 0; 4746 TargetOptions TargetOpts; 4747 TargetOpts.Triple = ReadString(Record, Idx); 4748 TargetOpts.CPU = ReadString(Record, Idx); 4749 TargetOpts.ABI = ReadString(Record, Idx); 4750 for (unsigned N = Record[Idx++]; N; --N) { 4751 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx)); 4752 } 4753 for (unsigned N = Record[Idx++]; N; --N) { 4754 TargetOpts.Features.push_back(ReadString(Record, Idx)); 4755 } 4756 4757 return Listener.ReadTargetOptions(TargetOpts, Complain, 4758 AllowCompatibleDifferences); 4759 } 4760 4761 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain, 4762 ASTReaderListener &Listener) { 4763 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions); 4764 unsigned Idx = 0; 4765 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++]; 4766 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \ 4767 DiagOpts->set##Name(static_cast<Type>(Record[Idx++])); 4768 #include "clang/Basic/DiagnosticOptions.def" 4769 4770 for (unsigned N = Record[Idx++]; N; --N) 4771 DiagOpts->Warnings.push_back(ReadString(Record, Idx)); 4772 for (unsigned N = Record[Idx++]; N; --N) 4773 DiagOpts->Remarks.push_back(ReadString(Record, Idx)); 4774 4775 return Listener.ReadDiagnosticOptions(DiagOpts, Complain); 4776 } 4777 4778 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain, 4779 ASTReaderListener &Listener) { 4780 FileSystemOptions FSOpts; 4781 unsigned Idx = 0; 4782 FSOpts.WorkingDir = ReadString(Record, Idx); 4783 return Listener.ReadFileSystemOptions(FSOpts, Complain); 4784 } 4785 4786 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record, 4787 bool Complain, 4788 ASTReaderListener &Listener) { 4789 HeaderSearchOptions HSOpts; 4790 unsigned Idx = 0; 4791 HSOpts.Sysroot = ReadString(Record, Idx); 4792 4793 // Include entries. 4794 for (unsigned N = Record[Idx++]; N; --N) { 4795 std::string Path = ReadString(Record, Idx); 4796 frontend::IncludeDirGroup Group 4797 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]); 4798 bool IsFramework = Record[Idx++]; 4799 bool IgnoreSysRoot = Record[Idx++]; 4800 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework, 4801 IgnoreSysRoot); 4802 } 4803 4804 // System header prefixes. 4805 for (unsigned N = Record[Idx++]; N; --N) { 4806 std::string Prefix = ReadString(Record, Idx); 4807 bool IsSystemHeader = Record[Idx++]; 4808 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader); 4809 } 4810 4811 HSOpts.ResourceDir = ReadString(Record, Idx); 4812 HSOpts.ModuleCachePath = ReadString(Record, Idx); 4813 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx); 4814 HSOpts.DisableModuleHash = Record[Idx++]; 4815 HSOpts.UseBuiltinIncludes = Record[Idx++]; 4816 HSOpts.UseStandardSystemIncludes = Record[Idx++]; 4817 HSOpts.UseStandardCXXIncludes = Record[Idx++]; 4818 HSOpts.UseLibcxx = Record[Idx++]; 4819 std::string SpecificModuleCachePath = ReadString(Record, Idx); 4820 4821 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 4822 Complain); 4823 } 4824 4825 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record, 4826 bool Complain, 4827 ASTReaderListener &Listener, 4828 std::string &SuggestedPredefines) { 4829 PreprocessorOptions PPOpts; 4830 unsigned Idx = 0; 4831 4832 // Macro definitions/undefs 4833 for (unsigned N = Record[Idx++]; N; --N) { 4834 std::string Macro = ReadString(Record, Idx); 4835 bool IsUndef = Record[Idx++]; 4836 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef)); 4837 } 4838 4839 // Includes 4840 for (unsigned N = Record[Idx++]; N; --N) { 4841 PPOpts.Includes.push_back(ReadString(Record, Idx)); 4842 } 4843 4844 // Macro Includes 4845 for (unsigned N = Record[Idx++]; N; --N) { 4846 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx)); 4847 } 4848 4849 PPOpts.UsePredefines = Record[Idx++]; 4850 PPOpts.DetailedRecord = Record[Idx++]; 4851 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx); 4852 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx); 4853 PPOpts.ObjCXXARCStandardLibrary = 4854 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]); 4855 SuggestedPredefines.clear(); 4856 return Listener.ReadPreprocessorOptions(PPOpts, Complain, 4857 SuggestedPredefines); 4858 } 4859 4860 std::pair<ModuleFile *, unsigned> 4861 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) { 4862 GlobalPreprocessedEntityMapType::iterator 4863 I = GlobalPreprocessedEntityMap.find(GlobalIndex); 4864 assert(I != GlobalPreprocessedEntityMap.end() && 4865 "Corrupted global preprocessed entity map"); 4866 ModuleFile *M = I->second; 4867 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID; 4868 return std::make_pair(M, LocalIndex); 4869 } 4870 4871 llvm::iterator_range<PreprocessingRecord::iterator> 4872 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const { 4873 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord()) 4874 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID, 4875 Mod.NumPreprocessedEntities); 4876 4877 return llvm::make_range(PreprocessingRecord::iterator(), 4878 PreprocessingRecord::iterator()); 4879 } 4880 4881 llvm::iterator_range<ASTReader::ModuleDeclIterator> 4882 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) { 4883 return llvm::make_range( 4884 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls), 4885 ModuleDeclIterator(this, &Mod, 4886 Mod.FileSortedDecls + Mod.NumFileSortedDecls)); 4887 } 4888 4889 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) { 4890 PreprocessedEntityID PPID = Index+1; 4891 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 4892 ModuleFile &M = *PPInfo.first; 4893 unsigned LocalIndex = PPInfo.second; 4894 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 4895 4896 if (!PP.getPreprocessingRecord()) { 4897 Error("no preprocessing record"); 4898 return nullptr; 4899 } 4900 4901 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor); 4902 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset); 4903 4904 llvm::BitstreamEntry Entry = 4905 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 4906 if (Entry.Kind != llvm::BitstreamEntry::Record) 4907 return nullptr; 4908 4909 // Read the record. 4910 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()), 4911 TranslateSourceLocation(M, PPOffs.getEnd())); 4912 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 4913 StringRef Blob; 4914 RecordData Record; 4915 PreprocessorDetailRecordTypes RecType = 4916 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord( 4917 Entry.ID, Record, &Blob); 4918 switch (RecType) { 4919 case PPD_MACRO_EXPANSION: { 4920 bool isBuiltin = Record[0]; 4921 IdentifierInfo *Name = nullptr; 4922 MacroDefinitionRecord *Def = nullptr; 4923 if (isBuiltin) 4924 Name = getLocalIdentifier(M, Record[1]); 4925 else { 4926 PreprocessedEntityID GlobalID = 4927 getGlobalPreprocessedEntityID(M, Record[1]); 4928 Def = cast<MacroDefinitionRecord>( 4929 PPRec.getLoadedPreprocessedEntity(GlobalID - 1)); 4930 } 4931 4932 MacroExpansion *ME; 4933 if (isBuiltin) 4934 ME = new (PPRec) MacroExpansion(Name, Range); 4935 else 4936 ME = new (PPRec) MacroExpansion(Def, Range); 4937 4938 return ME; 4939 } 4940 4941 case PPD_MACRO_DEFINITION: { 4942 // Decode the identifier info and then check again; if the macro is 4943 // still defined and associated with the identifier, 4944 IdentifierInfo *II = getLocalIdentifier(M, Record[0]); 4945 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range); 4946 4947 if (DeserializationListener) 4948 DeserializationListener->MacroDefinitionRead(PPID, MD); 4949 4950 return MD; 4951 } 4952 4953 case PPD_INCLUSION_DIRECTIVE: { 4954 const char *FullFileNameStart = Blob.data() + Record[0]; 4955 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]); 4956 const FileEntry *File = nullptr; 4957 if (!FullFileName.empty()) 4958 File = PP.getFileManager().getFile(FullFileName); 4959 4960 // FIXME: Stable encoding 4961 InclusionDirective::InclusionKind Kind 4962 = static_cast<InclusionDirective::InclusionKind>(Record[2]); 4963 InclusionDirective *ID 4964 = new (PPRec) InclusionDirective(PPRec, Kind, 4965 StringRef(Blob.data(), Record[0]), 4966 Record[1], Record[3], 4967 File, 4968 Range); 4969 return ID; 4970 } 4971 } 4972 4973 llvm_unreachable("Invalid PreprocessorDetailRecordTypes"); 4974 } 4975 4976 /// \brief \arg SLocMapI points at a chunk of a module that contains no 4977 /// preprocessed entities or the entities it contains are not the ones we are 4978 /// looking for. Find the next module that contains entities and return the ID 4979 /// of the first entry. 4980 PreprocessedEntityID ASTReader::findNextPreprocessedEntity( 4981 GlobalSLocOffsetMapType::const_iterator SLocMapI) const { 4982 ++SLocMapI; 4983 for (GlobalSLocOffsetMapType::const_iterator 4984 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) { 4985 ModuleFile &M = *SLocMapI->second; 4986 if (M.NumPreprocessedEntities) 4987 return M.BasePreprocessedEntityID; 4988 } 4989 4990 return getTotalNumPreprocessedEntities(); 4991 } 4992 4993 namespace { 4994 4995 struct PPEntityComp { 4996 const ASTReader &Reader; 4997 ModuleFile &M; 4998 4999 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { } 5000 5001 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const { 5002 SourceLocation LHS = getLoc(L); 5003 SourceLocation RHS = getLoc(R); 5004 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 5005 } 5006 5007 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const { 5008 SourceLocation LHS = getLoc(L); 5009 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 5010 } 5011 5012 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const { 5013 SourceLocation RHS = getLoc(R); 5014 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 5015 } 5016 5017 SourceLocation getLoc(const PPEntityOffset &PPE) const { 5018 return Reader.TranslateSourceLocation(M, PPE.getBegin()); 5019 } 5020 }; 5021 5022 } 5023 5024 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc, 5025 bool EndsAfter) const { 5026 if (SourceMgr.isLocalSourceLocation(Loc)) 5027 return getTotalNumPreprocessedEntities(); 5028 5029 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find( 5030 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1); 5031 assert(SLocMapI != GlobalSLocOffsetMap.end() && 5032 "Corrupted global sloc offset map"); 5033 5034 if (SLocMapI->second->NumPreprocessedEntities == 0) 5035 return findNextPreprocessedEntity(SLocMapI); 5036 5037 ModuleFile &M = *SLocMapI->second; 5038 typedef const PPEntityOffset *pp_iterator; 5039 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 5040 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 5041 5042 size_t Count = M.NumPreprocessedEntities; 5043 size_t Half; 5044 pp_iterator First = pp_begin; 5045 pp_iterator PPI; 5046 5047 if (EndsAfter) { 5048 PPI = std::upper_bound(pp_begin, pp_end, Loc, 5049 PPEntityComp(*this, M)); 5050 } else { 5051 // Do a binary search manually instead of using std::lower_bound because 5052 // The end locations of entities may be unordered (when a macro expansion 5053 // is inside another macro argument), but for this case it is not important 5054 // whether we get the first macro expansion or its containing macro. 5055 while (Count > 0) { 5056 Half = Count / 2; 5057 PPI = First; 5058 std::advance(PPI, Half); 5059 if (SourceMgr.isBeforeInTranslationUnit( 5060 TranslateSourceLocation(M, PPI->getEnd()), Loc)) { 5061 First = PPI; 5062 ++First; 5063 Count = Count - Half - 1; 5064 } else 5065 Count = Half; 5066 } 5067 } 5068 5069 if (PPI == pp_end) 5070 return findNextPreprocessedEntity(SLocMapI); 5071 5072 return M.BasePreprocessedEntityID + (PPI - pp_begin); 5073 } 5074 5075 /// \brief Returns a pair of [Begin, End) indices of preallocated 5076 /// preprocessed entities that \arg Range encompasses. 5077 std::pair<unsigned, unsigned> 5078 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) { 5079 if (Range.isInvalid()) 5080 return std::make_pair(0,0); 5081 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); 5082 5083 PreprocessedEntityID BeginID = 5084 findPreprocessedEntity(Range.getBegin(), false); 5085 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true); 5086 return std::make_pair(BeginID, EndID); 5087 } 5088 5089 /// \brief Optionally returns true or false if the preallocated preprocessed 5090 /// entity with index \arg Index came from file \arg FID. 5091 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index, 5092 FileID FID) { 5093 if (FID.isInvalid()) 5094 return false; 5095 5096 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 5097 ModuleFile &M = *PPInfo.first; 5098 unsigned LocalIndex = PPInfo.second; 5099 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 5100 5101 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin()); 5102 if (Loc.isInvalid()) 5103 return false; 5104 5105 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID)) 5106 return true; 5107 else 5108 return false; 5109 } 5110 5111 namespace { 5112 /// \brief Visitor used to search for information about a header file. 5113 class HeaderFileInfoVisitor { 5114 const FileEntry *FE; 5115 5116 Optional<HeaderFileInfo> HFI; 5117 5118 public: 5119 explicit HeaderFileInfoVisitor(const FileEntry *FE) 5120 : FE(FE) { } 5121 5122 bool operator()(ModuleFile &M) { 5123 HeaderFileInfoLookupTable *Table 5124 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable); 5125 if (!Table) 5126 return false; 5127 5128 // Look in the on-disk hash table for an entry for this file name. 5129 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE); 5130 if (Pos == Table->end()) 5131 return false; 5132 5133 HFI = *Pos; 5134 return true; 5135 } 5136 5137 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; } 5138 }; 5139 } 5140 5141 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) { 5142 HeaderFileInfoVisitor Visitor(FE); 5143 ModuleMgr.visit(Visitor); 5144 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) 5145 return *HFI; 5146 5147 return HeaderFileInfo(); 5148 } 5149 5150 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) { 5151 // FIXME: Make it work properly with modules. 5152 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates; 5153 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) { 5154 ModuleFile &F = *(*I); 5155 unsigned Idx = 0; 5156 DiagStates.clear(); 5157 assert(!Diag.DiagStates.empty()); 5158 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one. 5159 while (Idx < F.PragmaDiagMappings.size()) { 5160 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]); 5161 unsigned DiagStateID = F.PragmaDiagMappings[Idx++]; 5162 if (DiagStateID != 0) { 5163 Diag.DiagStatePoints.push_back( 5164 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1], 5165 FullSourceLoc(Loc, SourceMgr))); 5166 continue; 5167 } 5168 5169 assert(DiagStateID == 0); 5170 // A new DiagState was created here. 5171 Diag.DiagStates.push_back(*Diag.GetCurDiagState()); 5172 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back(); 5173 DiagStates.push_back(NewState); 5174 Diag.DiagStatePoints.push_back( 5175 DiagnosticsEngine::DiagStatePoint(NewState, 5176 FullSourceLoc(Loc, SourceMgr))); 5177 while (1) { 5178 assert(Idx < F.PragmaDiagMappings.size() && 5179 "Invalid data, didn't find '-1' marking end of diag/map pairs"); 5180 if (Idx >= F.PragmaDiagMappings.size()) { 5181 break; // Something is messed up but at least avoid infinite loop in 5182 // release build. 5183 } 5184 unsigned DiagID = F.PragmaDiagMappings[Idx++]; 5185 if (DiagID == (unsigned)-1) { 5186 break; // no more diag/map pairs for this location. 5187 } 5188 diag::Severity Map = (diag::Severity)F.PragmaDiagMappings[Idx++]; 5189 DiagnosticMapping Mapping = Diag.makeUserMapping(Map, Loc); 5190 Diag.GetCurDiagState()->setMapping(DiagID, Mapping); 5191 } 5192 } 5193 } 5194 } 5195 5196 /// \brief Get the correct cursor and offset for loading a type. 5197 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) { 5198 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index); 5199 assert(I != GlobalTypeMap.end() && "Corrupted global type map"); 5200 ModuleFile *M = I->second; 5201 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]); 5202 } 5203 5204 /// \brief Read and return the type with the given index.. 5205 /// 5206 /// The index is the type ID, shifted and minus the number of predefs. This 5207 /// routine actually reads the record corresponding to the type at the given 5208 /// location. It is a helper routine for GetType, which deals with reading type 5209 /// IDs. 5210 QualType ASTReader::readTypeRecord(unsigned Index) { 5211 RecordLocation Loc = TypeCursorForIndex(Index); 5212 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 5213 5214 // Keep track of where we are in the stream, then jump back there 5215 // after reading this type. 5216 SavedStreamPosition SavedPosition(DeclsCursor); 5217 5218 ReadingKindTracker ReadingKind(Read_Type, *this); 5219 5220 // Note that we are loading a type record. 5221 Deserializing AType(this); 5222 5223 unsigned Idx = 0; 5224 DeclsCursor.JumpToBit(Loc.Offset); 5225 RecordData Record; 5226 unsigned Code = DeclsCursor.ReadCode(); 5227 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) { 5228 case TYPE_EXT_QUAL: { 5229 if (Record.size() != 2) { 5230 Error("Incorrect encoding of extended qualifier type"); 5231 return QualType(); 5232 } 5233 QualType Base = readType(*Loc.F, Record, Idx); 5234 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]); 5235 return Context.getQualifiedType(Base, Quals); 5236 } 5237 5238 case TYPE_COMPLEX: { 5239 if (Record.size() != 1) { 5240 Error("Incorrect encoding of complex type"); 5241 return QualType(); 5242 } 5243 QualType ElemType = readType(*Loc.F, Record, Idx); 5244 return Context.getComplexType(ElemType); 5245 } 5246 5247 case TYPE_POINTER: { 5248 if (Record.size() != 1) { 5249 Error("Incorrect encoding of pointer type"); 5250 return QualType(); 5251 } 5252 QualType PointeeType = readType(*Loc.F, Record, Idx); 5253 return Context.getPointerType(PointeeType); 5254 } 5255 5256 case TYPE_DECAYED: { 5257 if (Record.size() != 1) { 5258 Error("Incorrect encoding of decayed type"); 5259 return QualType(); 5260 } 5261 QualType OriginalType = readType(*Loc.F, Record, Idx); 5262 QualType DT = Context.getAdjustedParameterType(OriginalType); 5263 if (!isa<DecayedType>(DT)) 5264 Error("Decayed type does not decay"); 5265 return DT; 5266 } 5267 5268 case TYPE_ADJUSTED: { 5269 if (Record.size() != 2) { 5270 Error("Incorrect encoding of adjusted type"); 5271 return QualType(); 5272 } 5273 QualType OriginalTy = readType(*Loc.F, Record, Idx); 5274 QualType AdjustedTy = readType(*Loc.F, Record, Idx); 5275 return Context.getAdjustedType(OriginalTy, AdjustedTy); 5276 } 5277 5278 case TYPE_BLOCK_POINTER: { 5279 if (Record.size() != 1) { 5280 Error("Incorrect encoding of block pointer type"); 5281 return QualType(); 5282 } 5283 QualType PointeeType = readType(*Loc.F, Record, Idx); 5284 return Context.getBlockPointerType(PointeeType); 5285 } 5286 5287 case TYPE_LVALUE_REFERENCE: { 5288 if (Record.size() != 2) { 5289 Error("Incorrect encoding of lvalue reference type"); 5290 return QualType(); 5291 } 5292 QualType PointeeType = readType(*Loc.F, Record, Idx); 5293 return Context.getLValueReferenceType(PointeeType, Record[1]); 5294 } 5295 5296 case TYPE_RVALUE_REFERENCE: { 5297 if (Record.size() != 1) { 5298 Error("Incorrect encoding of rvalue reference type"); 5299 return QualType(); 5300 } 5301 QualType PointeeType = readType(*Loc.F, Record, Idx); 5302 return Context.getRValueReferenceType(PointeeType); 5303 } 5304 5305 case TYPE_MEMBER_POINTER: { 5306 if (Record.size() != 2) { 5307 Error("Incorrect encoding of member pointer type"); 5308 return QualType(); 5309 } 5310 QualType PointeeType = readType(*Loc.F, Record, Idx); 5311 QualType ClassType = readType(*Loc.F, Record, Idx); 5312 if (PointeeType.isNull() || ClassType.isNull()) 5313 return QualType(); 5314 5315 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr()); 5316 } 5317 5318 case TYPE_CONSTANT_ARRAY: { 5319 QualType ElementType = readType(*Loc.F, Record, Idx); 5320 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 5321 unsigned IndexTypeQuals = Record[2]; 5322 unsigned Idx = 3; 5323 llvm::APInt Size = ReadAPInt(Record, Idx); 5324 return Context.getConstantArrayType(ElementType, Size, 5325 ASM, IndexTypeQuals); 5326 } 5327 5328 case TYPE_INCOMPLETE_ARRAY: { 5329 QualType ElementType = readType(*Loc.F, Record, Idx); 5330 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 5331 unsigned IndexTypeQuals = Record[2]; 5332 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals); 5333 } 5334 5335 case TYPE_VARIABLE_ARRAY: { 5336 QualType ElementType = readType(*Loc.F, Record, Idx); 5337 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 5338 unsigned IndexTypeQuals = Record[2]; 5339 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]); 5340 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]); 5341 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F), 5342 ASM, IndexTypeQuals, 5343 SourceRange(LBLoc, RBLoc)); 5344 } 5345 5346 case TYPE_VECTOR: { 5347 if (Record.size() != 3) { 5348 Error("incorrect encoding of vector type in AST file"); 5349 return QualType(); 5350 } 5351 5352 QualType ElementType = readType(*Loc.F, Record, Idx); 5353 unsigned NumElements = Record[1]; 5354 unsigned VecKind = Record[2]; 5355 return Context.getVectorType(ElementType, NumElements, 5356 (VectorType::VectorKind)VecKind); 5357 } 5358 5359 case TYPE_EXT_VECTOR: { 5360 if (Record.size() != 3) { 5361 Error("incorrect encoding of extended vector type in AST file"); 5362 return QualType(); 5363 } 5364 5365 QualType ElementType = readType(*Loc.F, Record, Idx); 5366 unsigned NumElements = Record[1]; 5367 return Context.getExtVectorType(ElementType, NumElements); 5368 } 5369 5370 case TYPE_FUNCTION_NO_PROTO: { 5371 if (Record.size() != 6) { 5372 Error("incorrect encoding of no-proto function type"); 5373 return QualType(); 5374 } 5375 QualType ResultType = readType(*Loc.F, Record, Idx); 5376 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3], 5377 (CallingConv)Record[4], Record[5]); 5378 return Context.getFunctionNoProtoType(ResultType, Info); 5379 } 5380 5381 case TYPE_FUNCTION_PROTO: { 5382 QualType ResultType = readType(*Loc.F, Record, Idx); 5383 5384 FunctionProtoType::ExtProtoInfo EPI; 5385 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1], 5386 /*hasregparm*/ Record[2], 5387 /*regparm*/ Record[3], 5388 static_cast<CallingConv>(Record[4]), 5389 /*produces*/ Record[5]); 5390 5391 unsigned Idx = 6; 5392 5393 EPI.Variadic = Record[Idx++]; 5394 EPI.HasTrailingReturn = Record[Idx++]; 5395 EPI.TypeQuals = Record[Idx++]; 5396 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]); 5397 SmallVector<QualType, 8> ExceptionStorage; 5398 readExceptionSpec(*Loc.F, ExceptionStorage, EPI.ExceptionSpec, Record, Idx); 5399 5400 unsigned NumParams = Record[Idx++]; 5401 SmallVector<QualType, 16> ParamTypes; 5402 for (unsigned I = 0; I != NumParams; ++I) 5403 ParamTypes.push_back(readType(*Loc.F, Record, Idx)); 5404 5405 SmallVector<FunctionProtoType::ExtParameterInfo, 4> ExtParameterInfos; 5406 if (Idx != Record.size()) { 5407 for (unsigned I = 0; I != NumParams; ++I) 5408 ExtParameterInfos.push_back( 5409 FunctionProtoType::ExtParameterInfo 5410 ::getFromOpaqueValue(Record[Idx++])); 5411 EPI.ExtParameterInfos = ExtParameterInfos.data(); 5412 } 5413 5414 assert(Idx == Record.size()); 5415 5416 return Context.getFunctionType(ResultType, ParamTypes, EPI); 5417 } 5418 5419 case TYPE_UNRESOLVED_USING: { 5420 unsigned Idx = 0; 5421 return Context.getTypeDeclType( 5422 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx)); 5423 } 5424 5425 case TYPE_TYPEDEF: { 5426 if (Record.size() != 2) { 5427 Error("incorrect encoding of typedef type"); 5428 return QualType(); 5429 } 5430 unsigned Idx = 0; 5431 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx); 5432 QualType Canonical = readType(*Loc.F, Record, Idx); 5433 if (!Canonical.isNull()) 5434 Canonical = Context.getCanonicalType(Canonical); 5435 return Context.getTypedefType(Decl, Canonical); 5436 } 5437 5438 case TYPE_TYPEOF_EXPR: 5439 return Context.getTypeOfExprType(ReadExpr(*Loc.F)); 5440 5441 case TYPE_TYPEOF: { 5442 if (Record.size() != 1) { 5443 Error("incorrect encoding of typeof(type) in AST file"); 5444 return QualType(); 5445 } 5446 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 5447 return Context.getTypeOfType(UnderlyingType); 5448 } 5449 5450 case TYPE_DECLTYPE: { 5451 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 5452 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType); 5453 } 5454 5455 case TYPE_UNARY_TRANSFORM: { 5456 QualType BaseType = readType(*Loc.F, Record, Idx); 5457 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 5458 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2]; 5459 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind); 5460 } 5461 5462 case TYPE_AUTO: { 5463 QualType Deduced = readType(*Loc.F, Record, Idx); 5464 AutoTypeKeyword Keyword = (AutoTypeKeyword)Record[Idx++]; 5465 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false; 5466 return Context.getAutoType(Deduced, Keyword, IsDependent); 5467 } 5468 5469 case TYPE_RECORD: { 5470 if (Record.size() != 2) { 5471 Error("incorrect encoding of record type"); 5472 return QualType(); 5473 } 5474 unsigned Idx = 0; 5475 bool IsDependent = Record[Idx++]; 5476 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx); 5477 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl()); 5478 QualType T = Context.getRecordType(RD); 5479 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 5480 return T; 5481 } 5482 5483 case TYPE_ENUM: { 5484 if (Record.size() != 2) { 5485 Error("incorrect encoding of enum type"); 5486 return QualType(); 5487 } 5488 unsigned Idx = 0; 5489 bool IsDependent = Record[Idx++]; 5490 QualType T 5491 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx)); 5492 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 5493 return T; 5494 } 5495 5496 case TYPE_ATTRIBUTED: { 5497 if (Record.size() != 3) { 5498 Error("incorrect encoding of attributed type"); 5499 return QualType(); 5500 } 5501 QualType modifiedType = readType(*Loc.F, Record, Idx); 5502 QualType equivalentType = readType(*Loc.F, Record, Idx); 5503 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]); 5504 return Context.getAttributedType(kind, modifiedType, equivalentType); 5505 } 5506 5507 case TYPE_PAREN: { 5508 if (Record.size() != 1) { 5509 Error("incorrect encoding of paren type"); 5510 return QualType(); 5511 } 5512 QualType InnerType = readType(*Loc.F, Record, Idx); 5513 return Context.getParenType(InnerType); 5514 } 5515 5516 case TYPE_PACK_EXPANSION: { 5517 if (Record.size() != 2) { 5518 Error("incorrect encoding of pack expansion type"); 5519 return QualType(); 5520 } 5521 QualType Pattern = readType(*Loc.F, Record, Idx); 5522 if (Pattern.isNull()) 5523 return QualType(); 5524 Optional<unsigned> NumExpansions; 5525 if (Record[1]) 5526 NumExpansions = Record[1] - 1; 5527 return Context.getPackExpansionType(Pattern, NumExpansions); 5528 } 5529 5530 case TYPE_ELABORATED: { 5531 unsigned Idx = 0; 5532 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 5533 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 5534 QualType NamedType = readType(*Loc.F, Record, Idx); 5535 return Context.getElaboratedType(Keyword, NNS, NamedType); 5536 } 5537 5538 case TYPE_OBJC_INTERFACE: { 5539 unsigned Idx = 0; 5540 ObjCInterfaceDecl *ItfD 5541 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx); 5542 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl()); 5543 } 5544 5545 case TYPE_OBJC_OBJECT: { 5546 unsigned Idx = 0; 5547 QualType Base = readType(*Loc.F, Record, Idx); 5548 unsigned NumTypeArgs = Record[Idx++]; 5549 SmallVector<QualType, 4> TypeArgs; 5550 for (unsigned I = 0; I != NumTypeArgs; ++I) 5551 TypeArgs.push_back(readType(*Loc.F, Record, Idx)); 5552 unsigned NumProtos = Record[Idx++]; 5553 SmallVector<ObjCProtocolDecl*, 4> Protos; 5554 for (unsigned I = 0; I != NumProtos; ++I) 5555 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx)); 5556 bool IsKindOf = Record[Idx++]; 5557 return Context.getObjCObjectType(Base, TypeArgs, Protos, IsKindOf); 5558 } 5559 5560 case TYPE_OBJC_OBJECT_POINTER: { 5561 unsigned Idx = 0; 5562 QualType Pointee = readType(*Loc.F, Record, Idx); 5563 return Context.getObjCObjectPointerType(Pointee); 5564 } 5565 5566 case TYPE_SUBST_TEMPLATE_TYPE_PARM: { 5567 unsigned Idx = 0; 5568 QualType Parm = readType(*Loc.F, Record, Idx); 5569 QualType Replacement = readType(*Loc.F, Record, Idx); 5570 return Context.getSubstTemplateTypeParmType( 5571 cast<TemplateTypeParmType>(Parm), 5572 Context.getCanonicalType(Replacement)); 5573 } 5574 5575 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: { 5576 unsigned Idx = 0; 5577 QualType Parm = readType(*Loc.F, Record, Idx); 5578 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx); 5579 return Context.getSubstTemplateTypeParmPackType( 5580 cast<TemplateTypeParmType>(Parm), 5581 ArgPack); 5582 } 5583 5584 case TYPE_INJECTED_CLASS_NAME: { 5585 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx); 5586 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable 5587 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable 5588 // for AST reading, too much interdependencies. 5589 const Type *T = nullptr; 5590 for (auto *DI = D; DI; DI = DI->getPreviousDecl()) { 5591 if (const Type *Existing = DI->getTypeForDecl()) { 5592 T = Existing; 5593 break; 5594 } 5595 } 5596 if (!T) { 5597 T = new (Context, TypeAlignment) InjectedClassNameType(D, TST); 5598 for (auto *DI = D; DI; DI = DI->getPreviousDecl()) 5599 DI->setTypeForDecl(T); 5600 } 5601 return QualType(T, 0); 5602 } 5603 5604 case TYPE_TEMPLATE_TYPE_PARM: { 5605 unsigned Idx = 0; 5606 unsigned Depth = Record[Idx++]; 5607 unsigned Index = Record[Idx++]; 5608 bool Pack = Record[Idx++]; 5609 TemplateTypeParmDecl *D 5610 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx); 5611 return Context.getTemplateTypeParmType(Depth, Index, Pack, D); 5612 } 5613 5614 case TYPE_DEPENDENT_NAME: { 5615 unsigned Idx = 0; 5616 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 5617 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 5618 const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx); 5619 QualType Canon = readType(*Loc.F, Record, Idx); 5620 if (!Canon.isNull()) 5621 Canon = Context.getCanonicalType(Canon); 5622 return Context.getDependentNameType(Keyword, NNS, Name, Canon); 5623 } 5624 5625 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: { 5626 unsigned Idx = 0; 5627 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 5628 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 5629 const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx); 5630 unsigned NumArgs = Record[Idx++]; 5631 SmallVector<TemplateArgument, 8> Args; 5632 Args.reserve(NumArgs); 5633 while (NumArgs--) 5634 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx)); 5635 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name, 5636 Args); 5637 } 5638 5639 case TYPE_DEPENDENT_SIZED_ARRAY: { 5640 unsigned Idx = 0; 5641 5642 // ArrayType 5643 QualType ElementType = readType(*Loc.F, Record, Idx); 5644 ArrayType::ArraySizeModifier ASM 5645 = (ArrayType::ArraySizeModifier)Record[Idx++]; 5646 unsigned IndexTypeQuals = Record[Idx++]; 5647 5648 // DependentSizedArrayType 5649 Expr *NumElts = ReadExpr(*Loc.F); 5650 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx); 5651 5652 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM, 5653 IndexTypeQuals, Brackets); 5654 } 5655 5656 case TYPE_TEMPLATE_SPECIALIZATION: { 5657 unsigned Idx = 0; 5658 bool IsDependent = Record[Idx++]; 5659 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx); 5660 SmallVector<TemplateArgument, 8> Args; 5661 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx); 5662 QualType Underlying = readType(*Loc.F, Record, Idx); 5663 QualType T; 5664 if (Underlying.isNull()) 5665 T = Context.getCanonicalTemplateSpecializationType(Name, Args); 5666 else 5667 T = Context.getTemplateSpecializationType(Name, Args, Underlying); 5668 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 5669 return T; 5670 } 5671 5672 case TYPE_ATOMIC: { 5673 if (Record.size() != 1) { 5674 Error("Incorrect encoding of atomic type"); 5675 return QualType(); 5676 } 5677 QualType ValueType = readType(*Loc.F, Record, Idx); 5678 return Context.getAtomicType(ValueType); 5679 } 5680 5681 case TYPE_PIPE: { 5682 if (Record.size() != 1) { 5683 Error("Incorrect encoding of pipe type"); 5684 return QualType(); 5685 } 5686 5687 // Reading the pipe element type. 5688 QualType ElementType = readType(*Loc.F, Record, Idx); 5689 return Context.getPipeType(ElementType); 5690 } 5691 } 5692 llvm_unreachable("Invalid TypeCode!"); 5693 } 5694 5695 void ASTReader::readExceptionSpec(ModuleFile &ModuleFile, 5696 SmallVectorImpl<QualType> &Exceptions, 5697 FunctionProtoType::ExceptionSpecInfo &ESI, 5698 const RecordData &Record, unsigned &Idx) { 5699 ExceptionSpecificationType EST = 5700 static_cast<ExceptionSpecificationType>(Record[Idx++]); 5701 ESI.Type = EST; 5702 if (EST == EST_Dynamic) { 5703 for (unsigned I = 0, N = Record[Idx++]; I != N; ++I) 5704 Exceptions.push_back(readType(ModuleFile, Record, Idx)); 5705 ESI.Exceptions = Exceptions; 5706 } else if (EST == EST_ComputedNoexcept) { 5707 ESI.NoexceptExpr = ReadExpr(ModuleFile); 5708 } else if (EST == EST_Uninstantiated) { 5709 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx); 5710 ESI.SourceTemplate = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx); 5711 } else if (EST == EST_Unevaluated) { 5712 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx); 5713 } 5714 } 5715 5716 class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> { 5717 ASTReader &Reader; 5718 ModuleFile &F; 5719 const ASTReader::RecordData &Record; 5720 unsigned &Idx; 5721 5722 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R, 5723 unsigned &I) { 5724 return Reader.ReadSourceLocation(F, R, I); 5725 } 5726 5727 template<typename T> 5728 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) { 5729 return Reader.ReadDeclAs<T>(F, Record, Idx); 5730 } 5731 5732 public: 5733 TypeLocReader(ASTReader &Reader, ModuleFile &F, 5734 const ASTReader::RecordData &Record, unsigned &Idx) 5735 : Reader(Reader), F(F), Record(Record), Idx(Idx) 5736 { } 5737 5738 // We want compile-time assurance that we've enumerated all of 5739 // these, so unfortunately we have to declare them first, then 5740 // define them out-of-line. 5741 #define ABSTRACT_TYPELOC(CLASS, PARENT) 5742 #define TYPELOC(CLASS, PARENT) \ 5743 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 5744 #include "clang/AST/TypeLocNodes.def" 5745 5746 void VisitFunctionTypeLoc(FunctionTypeLoc); 5747 void VisitArrayTypeLoc(ArrayTypeLoc); 5748 }; 5749 5750 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 5751 // nothing to do 5752 } 5753 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 5754 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx)); 5755 if (TL.needsExtraLocalData()) { 5756 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++])); 5757 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++])); 5758 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++])); 5759 TL.setModeAttr(Record[Idx++]); 5760 } 5761 } 5762 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { 5763 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5764 } 5765 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { 5766 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 5767 } 5768 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) { 5769 // nothing to do 5770 } 5771 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { 5772 // nothing to do 5773 } 5774 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 5775 TL.setCaretLoc(ReadSourceLocation(Record, Idx)); 5776 } 5777 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 5778 TL.setAmpLoc(ReadSourceLocation(Record, Idx)); 5779 } 5780 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 5781 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx)); 5782 } 5783 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 5784 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 5785 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx)); 5786 } 5787 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { 5788 TL.setLBracketLoc(ReadSourceLocation(Record, Idx)); 5789 TL.setRBracketLoc(ReadSourceLocation(Record, Idx)); 5790 if (Record[Idx++]) 5791 TL.setSizeExpr(Reader.ReadExpr(F)); 5792 else 5793 TL.setSizeExpr(nullptr); 5794 } 5795 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 5796 VisitArrayTypeLoc(TL); 5797 } 5798 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 5799 VisitArrayTypeLoc(TL); 5800 } 5801 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 5802 VisitArrayTypeLoc(TL); 5803 } 5804 void TypeLocReader::VisitDependentSizedArrayTypeLoc( 5805 DependentSizedArrayTypeLoc TL) { 5806 VisitArrayTypeLoc(TL); 5807 } 5808 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( 5809 DependentSizedExtVectorTypeLoc TL) { 5810 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5811 } 5812 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { 5813 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5814 } 5815 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 5816 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5817 } 5818 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 5819 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx)); 5820 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 5821 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 5822 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx)); 5823 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) { 5824 TL.setParam(i, ReadDeclAs<ParmVarDecl>(Record, Idx)); 5825 } 5826 } 5827 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 5828 VisitFunctionTypeLoc(TL); 5829 } 5830 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 5831 VisitFunctionTypeLoc(TL); 5832 } 5833 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 5834 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5835 } 5836 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 5837 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5838 } 5839 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 5840 TL.setTypeofLoc(ReadSourceLocation(Record, Idx)); 5841 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 5842 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 5843 } 5844 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 5845 TL.setTypeofLoc(ReadSourceLocation(Record, Idx)); 5846 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 5847 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 5848 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx)); 5849 } 5850 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 5851 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5852 } 5853 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 5854 TL.setKWLoc(ReadSourceLocation(Record, Idx)); 5855 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 5856 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 5857 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx)); 5858 } 5859 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) { 5860 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5861 } 5862 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { 5863 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5864 } 5865 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { 5866 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5867 } 5868 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 5869 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx)); 5870 if (TL.hasAttrOperand()) { 5871 SourceRange range; 5872 range.setBegin(ReadSourceLocation(Record, Idx)); 5873 range.setEnd(ReadSourceLocation(Record, Idx)); 5874 TL.setAttrOperandParensRange(range); 5875 } 5876 if (TL.hasAttrExprOperand()) { 5877 if (Record[Idx++]) 5878 TL.setAttrExprOperand(Reader.ReadExpr(F)); 5879 else 5880 TL.setAttrExprOperand(nullptr); 5881 } else if (TL.hasAttrEnumOperand()) 5882 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx)); 5883 } 5884 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 5885 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5886 } 5887 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( 5888 SubstTemplateTypeParmTypeLoc TL) { 5889 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5890 } 5891 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc( 5892 SubstTemplateTypeParmPackTypeLoc TL) { 5893 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5894 } 5895 void TypeLocReader::VisitTemplateSpecializationTypeLoc( 5896 TemplateSpecializationTypeLoc TL) { 5897 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx)); 5898 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx)); 5899 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 5900 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 5901 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 5902 TL.setArgLocInfo(i, 5903 Reader.GetTemplateArgumentLocInfo(F, 5904 TL.getTypePtr()->getArg(i).getKind(), 5905 Record, Idx)); 5906 } 5907 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) { 5908 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 5909 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 5910 } 5911 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 5912 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx)); 5913 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx)); 5914 } 5915 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 5916 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5917 } 5918 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 5919 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx)); 5920 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx)); 5921 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5922 } 5923 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( 5924 DependentTemplateSpecializationTypeLoc TL) { 5925 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx)); 5926 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx)); 5927 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx)); 5928 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx)); 5929 TL.setLAngleLoc(ReadSourceLocation(Record, Idx)); 5930 TL.setRAngleLoc(ReadSourceLocation(Record, Idx)); 5931 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 5932 TL.setArgLocInfo(I, 5933 Reader.GetTemplateArgumentLocInfo(F, 5934 TL.getTypePtr()->getArg(I).getKind(), 5935 Record, Idx)); 5936 } 5937 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 5938 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx)); 5939 } 5940 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 5941 TL.setNameLoc(ReadSourceLocation(Record, Idx)); 5942 } 5943 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 5944 TL.setHasBaseTypeAsWritten(Record[Idx++]); 5945 TL.setTypeArgsLAngleLoc(ReadSourceLocation(Record, Idx)); 5946 TL.setTypeArgsRAngleLoc(ReadSourceLocation(Record, Idx)); 5947 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i) 5948 TL.setTypeArgTInfo(i, Reader.GetTypeSourceInfo(F, Record, Idx)); 5949 TL.setProtocolLAngleLoc(ReadSourceLocation(Record, Idx)); 5950 TL.setProtocolRAngleLoc(ReadSourceLocation(Record, Idx)); 5951 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 5952 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx)); 5953 } 5954 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 5955 TL.setStarLoc(ReadSourceLocation(Record, Idx)); 5956 } 5957 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) { 5958 TL.setKWLoc(ReadSourceLocation(Record, Idx)); 5959 TL.setLParenLoc(ReadSourceLocation(Record, Idx)); 5960 TL.setRParenLoc(ReadSourceLocation(Record, Idx)); 5961 } 5962 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) { 5963 TL.setKWLoc(ReadSourceLocation(Record, Idx)); 5964 } 5965 5966 TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F, 5967 const RecordData &Record, 5968 unsigned &Idx) { 5969 QualType InfoTy = readType(F, Record, Idx); 5970 if (InfoTy.isNull()) 5971 return nullptr; 5972 5973 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy); 5974 TypeLocReader TLR(*this, F, Record, Idx); 5975 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc()) 5976 TLR.Visit(TL); 5977 return TInfo; 5978 } 5979 5980 QualType ASTReader::GetType(TypeID ID) { 5981 unsigned FastQuals = ID & Qualifiers::FastMask; 5982 unsigned Index = ID >> Qualifiers::FastWidth; 5983 5984 if (Index < NUM_PREDEF_TYPE_IDS) { 5985 QualType T; 5986 switch ((PredefinedTypeIDs)Index) { 5987 case PREDEF_TYPE_NULL_ID: 5988 return QualType(); 5989 case PREDEF_TYPE_VOID_ID: 5990 T = Context.VoidTy; 5991 break; 5992 case PREDEF_TYPE_BOOL_ID: 5993 T = Context.BoolTy; 5994 break; 5995 5996 case PREDEF_TYPE_CHAR_U_ID: 5997 case PREDEF_TYPE_CHAR_S_ID: 5998 // FIXME: Check that the signedness of CharTy is correct! 5999 T = Context.CharTy; 6000 break; 6001 6002 case PREDEF_TYPE_UCHAR_ID: 6003 T = Context.UnsignedCharTy; 6004 break; 6005 case PREDEF_TYPE_USHORT_ID: 6006 T = Context.UnsignedShortTy; 6007 break; 6008 case PREDEF_TYPE_UINT_ID: 6009 T = Context.UnsignedIntTy; 6010 break; 6011 case PREDEF_TYPE_ULONG_ID: 6012 T = Context.UnsignedLongTy; 6013 break; 6014 case PREDEF_TYPE_ULONGLONG_ID: 6015 T = Context.UnsignedLongLongTy; 6016 break; 6017 case PREDEF_TYPE_UINT128_ID: 6018 T = Context.UnsignedInt128Ty; 6019 break; 6020 case PREDEF_TYPE_SCHAR_ID: 6021 T = Context.SignedCharTy; 6022 break; 6023 case PREDEF_TYPE_WCHAR_ID: 6024 T = Context.WCharTy; 6025 break; 6026 case PREDEF_TYPE_SHORT_ID: 6027 T = Context.ShortTy; 6028 break; 6029 case PREDEF_TYPE_INT_ID: 6030 T = Context.IntTy; 6031 break; 6032 case PREDEF_TYPE_LONG_ID: 6033 T = Context.LongTy; 6034 break; 6035 case PREDEF_TYPE_LONGLONG_ID: 6036 T = Context.LongLongTy; 6037 break; 6038 case PREDEF_TYPE_INT128_ID: 6039 T = Context.Int128Ty; 6040 break; 6041 case PREDEF_TYPE_HALF_ID: 6042 T = Context.HalfTy; 6043 break; 6044 case PREDEF_TYPE_FLOAT_ID: 6045 T = Context.FloatTy; 6046 break; 6047 case PREDEF_TYPE_DOUBLE_ID: 6048 T = Context.DoubleTy; 6049 break; 6050 case PREDEF_TYPE_LONGDOUBLE_ID: 6051 T = Context.LongDoubleTy; 6052 break; 6053 case PREDEF_TYPE_FLOAT128_ID: 6054 T = Context.Float128Ty; 6055 break; 6056 case PREDEF_TYPE_OVERLOAD_ID: 6057 T = Context.OverloadTy; 6058 break; 6059 case PREDEF_TYPE_BOUND_MEMBER: 6060 T = Context.BoundMemberTy; 6061 break; 6062 case PREDEF_TYPE_PSEUDO_OBJECT: 6063 T = Context.PseudoObjectTy; 6064 break; 6065 case PREDEF_TYPE_DEPENDENT_ID: 6066 T = Context.DependentTy; 6067 break; 6068 case PREDEF_TYPE_UNKNOWN_ANY: 6069 T = Context.UnknownAnyTy; 6070 break; 6071 case PREDEF_TYPE_NULLPTR_ID: 6072 T = Context.NullPtrTy; 6073 break; 6074 case PREDEF_TYPE_CHAR16_ID: 6075 T = Context.Char16Ty; 6076 break; 6077 case PREDEF_TYPE_CHAR32_ID: 6078 T = Context.Char32Ty; 6079 break; 6080 case PREDEF_TYPE_OBJC_ID: 6081 T = Context.ObjCBuiltinIdTy; 6082 break; 6083 case PREDEF_TYPE_OBJC_CLASS: 6084 T = Context.ObjCBuiltinClassTy; 6085 break; 6086 case PREDEF_TYPE_OBJC_SEL: 6087 T = Context.ObjCBuiltinSelTy; 6088 break; 6089 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 6090 case PREDEF_TYPE_##Id##_ID: \ 6091 T = Context.SingletonId; \ 6092 break; 6093 #include "clang/Basic/OpenCLImageTypes.def" 6094 case PREDEF_TYPE_SAMPLER_ID: 6095 T = Context.OCLSamplerTy; 6096 break; 6097 case PREDEF_TYPE_EVENT_ID: 6098 T = Context.OCLEventTy; 6099 break; 6100 case PREDEF_TYPE_CLK_EVENT_ID: 6101 T = Context.OCLClkEventTy; 6102 break; 6103 case PREDEF_TYPE_QUEUE_ID: 6104 T = Context.OCLQueueTy; 6105 break; 6106 case PREDEF_TYPE_NDRANGE_ID: 6107 T = Context.OCLNDRangeTy; 6108 break; 6109 case PREDEF_TYPE_RESERVE_ID_ID: 6110 T = Context.OCLReserveIDTy; 6111 break; 6112 case PREDEF_TYPE_AUTO_DEDUCT: 6113 T = Context.getAutoDeductType(); 6114 break; 6115 6116 case PREDEF_TYPE_AUTO_RREF_DEDUCT: 6117 T = Context.getAutoRRefDeductType(); 6118 break; 6119 6120 case PREDEF_TYPE_ARC_UNBRIDGED_CAST: 6121 T = Context.ARCUnbridgedCastTy; 6122 break; 6123 6124 case PREDEF_TYPE_BUILTIN_FN: 6125 T = Context.BuiltinFnTy; 6126 break; 6127 6128 case PREDEF_TYPE_OMP_ARRAY_SECTION: 6129 T = Context.OMPArraySectionTy; 6130 break; 6131 } 6132 6133 assert(!T.isNull() && "Unknown predefined type"); 6134 return T.withFastQualifiers(FastQuals); 6135 } 6136 6137 Index -= NUM_PREDEF_TYPE_IDS; 6138 assert(Index < TypesLoaded.size() && "Type index out-of-range"); 6139 if (TypesLoaded[Index].isNull()) { 6140 TypesLoaded[Index] = readTypeRecord(Index); 6141 if (TypesLoaded[Index].isNull()) 6142 return QualType(); 6143 6144 TypesLoaded[Index]->setFromAST(); 6145 if (DeserializationListener) 6146 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID), 6147 TypesLoaded[Index]); 6148 } 6149 6150 return TypesLoaded[Index].withFastQualifiers(FastQuals); 6151 } 6152 6153 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) { 6154 return GetType(getGlobalTypeID(F, LocalID)); 6155 } 6156 6157 serialization::TypeID 6158 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const { 6159 unsigned FastQuals = LocalID & Qualifiers::FastMask; 6160 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth; 6161 6162 if (LocalIndex < NUM_PREDEF_TYPE_IDS) 6163 return LocalID; 6164 6165 ContinuousRangeMap<uint32_t, int, 2>::iterator I 6166 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS); 6167 assert(I != F.TypeRemap.end() && "Invalid index into type index remap"); 6168 6169 unsigned GlobalIndex = LocalIndex + I->second; 6170 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals; 6171 } 6172 6173 TemplateArgumentLocInfo 6174 ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F, 6175 TemplateArgument::ArgKind Kind, 6176 const RecordData &Record, 6177 unsigned &Index) { 6178 switch (Kind) { 6179 case TemplateArgument::Expression: 6180 return ReadExpr(F); 6181 case TemplateArgument::Type: 6182 return GetTypeSourceInfo(F, Record, Index); 6183 case TemplateArgument::Template: { 6184 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, 6185 Index); 6186 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index); 6187 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 6188 SourceLocation()); 6189 } 6190 case TemplateArgument::TemplateExpansion: { 6191 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, 6192 Index); 6193 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index); 6194 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index); 6195 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 6196 EllipsisLoc); 6197 } 6198 case TemplateArgument::Null: 6199 case TemplateArgument::Integral: 6200 case TemplateArgument::Declaration: 6201 case TemplateArgument::NullPtr: 6202 case TemplateArgument::Pack: 6203 // FIXME: Is this right? 6204 return TemplateArgumentLocInfo(); 6205 } 6206 llvm_unreachable("unexpected template argument loc"); 6207 } 6208 6209 TemplateArgumentLoc 6210 ASTReader::ReadTemplateArgumentLoc(ModuleFile &F, 6211 const RecordData &Record, unsigned &Index) { 6212 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index); 6213 6214 if (Arg.getKind() == TemplateArgument::Expression) { 6215 if (Record[Index++]) // bool InfoHasSameExpr. 6216 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); 6217 } 6218 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(), 6219 Record, Index)); 6220 } 6221 6222 const ASTTemplateArgumentListInfo* 6223 ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F, 6224 const RecordData &Record, 6225 unsigned &Index) { 6226 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index); 6227 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index); 6228 unsigned NumArgsAsWritten = Record[Index++]; 6229 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc); 6230 for (unsigned i = 0; i != NumArgsAsWritten; ++i) 6231 TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index)); 6232 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo); 6233 } 6234 6235 Decl *ASTReader::GetExternalDecl(uint32_t ID) { 6236 return GetDecl(ID); 6237 } 6238 6239 template<typename TemplateSpecializationDecl> 6240 static void completeRedeclChainForTemplateSpecialization(Decl *D) { 6241 if (auto *TSD = dyn_cast<TemplateSpecializationDecl>(D)) 6242 TSD->getSpecializedTemplate()->LoadLazySpecializations(); 6243 } 6244 6245 void ASTReader::CompleteRedeclChain(const Decl *D) { 6246 if (NumCurrentElementsDeserializing) { 6247 // We arrange to not care about the complete redeclaration chain while we're 6248 // deserializing. Just remember that the AST has marked this one as complete 6249 // but that it's not actually complete yet, so we know we still need to 6250 // complete it later. 6251 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D)); 6252 return; 6253 } 6254 6255 const DeclContext *DC = D->getDeclContext()->getRedeclContext(); 6256 6257 // If this is a named declaration, complete it by looking it up 6258 // within its context. 6259 // 6260 // FIXME: Merging a function definition should merge 6261 // all mergeable entities within it. 6262 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) || 6263 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) { 6264 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) { 6265 if (!getContext().getLangOpts().CPlusPlus && 6266 isa<TranslationUnitDecl>(DC)) { 6267 // Outside of C++, we don't have a lookup table for the TU, so update 6268 // the identifier instead. (For C++ modules, we don't store decls 6269 // in the serialized identifier table, so we do the lookup in the TU.) 6270 auto *II = Name.getAsIdentifierInfo(); 6271 assert(II && "non-identifier name in C?"); 6272 if (II->isOutOfDate()) 6273 updateOutOfDateIdentifier(*II); 6274 } else 6275 DC->lookup(Name); 6276 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) { 6277 // Find all declarations of this kind from the relevant context. 6278 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) { 6279 auto *DC = cast<DeclContext>(DCDecl); 6280 SmallVector<Decl*, 8> Decls; 6281 FindExternalLexicalDecls( 6282 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls); 6283 } 6284 } 6285 } 6286 6287 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) 6288 CTSD->getSpecializedTemplate()->LoadLazySpecializations(); 6289 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) 6290 VTSD->getSpecializedTemplate()->LoadLazySpecializations(); 6291 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 6292 if (auto *Template = FD->getPrimaryTemplate()) 6293 Template->LoadLazySpecializations(); 6294 } 6295 } 6296 6297 CXXCtorInitializer ** 6298 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) { 6299 RecordLocation Loc = getLocalBitOffset(Offset); 6300 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 6301 SavedStreamPosition SavedPosition(Cursor); 6302 Cursor.JumpToBit(Loc.Offset); 6303 ReadingKindTracker ReadingKind(Read_Decl, *this); 6304 6305 RecordData Record; 6306 unsigned Code = Cursor.ReadCode(); 6307 unsigned RecCode = Cursor.readRecord(Code, Record); 6308 if (RecCode != DECL_CXX_CTOR_INITIALIZERS) { 6309 Error("malformed AST file: missing C++ ctor initializers"); 6310 return nullptr; 6311 } 6312 6313 unsigned Idx = 0; 6314 return ReadCXXCtorInitializers(*Loc.F, Record, Idx); 6315 } 6316 6317 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) { 6318 RecordLocation Loc = getLocalBitOffset(Offset); 6319 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 6320 SavedStreamPosition SavedPosition(Cursor); 6321 Cursor.JumpToBit(Loc.Offset); 6322 ReadingKindTracker ReadingKind(Read_Decl, *this); 6323 RecordData Record; 6324 unsigned Code = Cursor.ReadCode(); 6325 unsigned RecCode = Cursor.readRecord(Code, Record); 6326 if (RecCode != DECL_CXX_BASE_SPECIFIERS) { 6327 Error("malformed AST file: missing C++ base specifiers"); 6328 return nullptr; 6329 } 6330 6331 unsigned Idx = 0; 6332 unsigned NumBases = Record[Idx++]; 6333 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases); 6334 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases]; 6335 for (unsigned I = 0; I != NumBases; ++I) 6336 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx); 6337 return Bases; 6338 } 6339 6340 serialization::DeclID 6341 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const { 6342 if (LocalID < NUM_PREDEF_DECL_IDS) 6343 return LocalID; 6344 6345 ContinuousRangeMap<uint32_t, int, 2>::iterator I 6346 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS); 6347 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap"); 6348 6349 return LocalID + I->second; 6350 } 6351 6352 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID, 6353 ModuleFile &M) const { 6354 // Predefined decls aren't from any module. 6355 if (ID < NUM_PREDEF_DECL_IDS) 6356 return false; 6357 6358 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID && 6359 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls; 6360 } 6361 6362 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) { 6363 if (!D->isFromASTFile()) 6364 return nullptr; 6365 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID()); 6366 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 6367 return I->second; 6368 } 6369 6370 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) { 6371 if (ID < NUM_PREDEF_DECL_IDS) 6372 return SourceLocation(); 6373 6374 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 6375 6376 if (Index > DeclsLoaded.size()) { 6377 Error("declaration ID out-of-range for AST file"); 6378 return SourceLocation(); 6379 } 6380 6381 if (Decl *D = DeclsLoaded[Index]) 6382 return D->getLocation(); 6383 6384 SourceLocation Loc; 6385 DeclCursorForID(ID, Loc); 6386 return Loc; 6387 } 6388 6389 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) { 6390 switch (ID) { 6391 case PREDEF_DECL_NULL_ID: 6392 return nullptr; 6393 6394 case PREDEF_DECL_TRANSLATION_UNIT_ID: 6395 return Context.getTranslationUnitDecl(); 6396 6397 case PREDEF_DECL_OBJC_ID_ID: 6398 return Context.getObjCIdDecl(); 6399 6400 case PREDEF_DECL_OBJC_SEL_ID: 6401 return Context.getObjCSelDecl(); 6402 6403 case PREDEF_DECL_OBJC_CLASS_ID: 6404 return Context.getObjCClassDecl(); 6405 6406 case PREDEF_DECL_OBJC_PROTOCOL_ID: 6407 return Context.getObjCProtocolDecl(); 6408 6409 case PREDEF_DECL_INT_128_ID: 6410 return Context.getInt128Decl(); 6411 6412 case PREDEF_DECL_UNSIGNED_INT_128_ID: 6413 return Context.getUInt128Decl(); 6414 6415 case PREDEF_DECL_OBJC_INSTANCETYPE_ID: 6416 return Context.getObjCInstanceTypeDecl(); 6417 6418 case PREDEF_DECL_BUILTIN_VA_LIST_ID: 6419 return Context.getBuiltinVaListDecl(); 6420 6421 case PREDEF_DECL_VA_LIST_TAG: 6422 return Context.getVaListTagDecl(); 6423 6424 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID: 6425 return Context.getBuiltinMSVaListDecl(); 6426 6427 case PREDEF_DECL_EXTERN_C_CONTEXT_ID: 6428 return Context.getExternCContextDecl(); 6429 6430 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID: 6431 return Context.getMakeIntegerSeqDecl(); 6432 6433 case PREDEF_DECL_CF_CONSTANT_STRING_ID: 6434 return Context.getCFConstantStringDecl(); 6435 6436 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID: 6437 return Context.getCFConstantStringTagDecl(); 6438 6439 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID: 6440 return Context.getTypePackElementDecl(); 6441 } 6442 llvm_unreachable("PredefinedDeclIDs unknown enum value"); 6443 } 6444 6445 Decl *ASTReader::GetExistingDecl(DeclID ID) { 6446 if (ID < NUM_PREDEF_DECL_IDS) { 6447 Decl *D = getPredefinedDecl(Context, (PredefinedDeclIDs)ID); 6448 if (D) { 6449 // Track that we have merged the declaration with ID \p ID into the 6450 // pre-existing predefined declaration \p D. 6451 auto &Merged = KeyDecls[D->getCanonicalDecl()]; 6452 if (Merged.empty()) 6453 Merged.push_back(ID); 6454 } 6455 return D; 6456 } 6457 6458 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 6459 6460 if (Index >= DeclsLoaded.size()) { 6461 assert(0 && "declaration ID out-of-range for AST file"); 6462 Error("declaration ID out-of-range for AST file"); 6463 return nullptr; 6464 } 6465 6466 return DeclsLoaded[Index]; 6467 } 6468 6469 Decl *ASTReader::GetDecl(DeclID ID) { 6470 if (ID < NUM_PREDEF_DECL_IDS) 6471 return GetExistingDecl(ID); 6472 6473 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 6474 6475 if (Index >= DeclsLoaded.size()) { 6476 assert(0 && "declaration ID out-of-range for AST file"); 6477 Error("declaration ID out-of-range for AST file"); 6478 return nullptr; 6479 } 6480 6481 if (!DeclsLoaded[Index]) { 6482 ReadDeclRecord(ID); 6483 if (DeserializationListener) 6484 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); 6485 } 6486 6487 return DeclsLoaded[Index]; 6488 } 6489 6490 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M, 6491 DeclID GlobalID) { 6492 if (GlobalID < NUM_PREDEF_DECL_IDS) 6493 return GlobalID; 6494 6495 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID); 6496 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 6497 ModuleFile *Owner = I->second; 6498 6499 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos 6500 = M.GlobalToLocalDeclIDs.find(Owner); 6501 if (Pos == M.GlobalToLocalDeclIDs.end()) 6502 return 0; 6503 6504 return GlobalID - Owner->BaseDeclID + Pos->second; 6505 } 6506 6507 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F, 6508 const RecordData &Record, 6509 unsigned &Idx) { 6510 if (Idx >= Record.size()) { 6511 Error("Corrupted AST file"); 6512 return 0; 6513 } 6514 6515 return getGlobalDeclID(F, Record[Idx++]); 6516 } 6517 6518 /// \brief Resolve the offset of a statement into a statement. 6519 /// 6520 /// This operation will read a new statement from the external 6521 /// source each time it is called, and is meant to be used via a 6522 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 6523 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) { 6524 // Switch case IDs are per Decl. 6525 ClearSwitchCaseIDs(); 6526 6527 // Offset here is a global offset across the entire chain. 6528 RecordLocation Loc = getLocalBitOffset(Offset); 6529 Loc.F->DeclsCursor.JumpToBit(Loc.Offset); 6530 return ReadStmtFromStream(*Loc.F); 6531 } 6532 6533 void ASTReader::FindExternalLexicalDecls( 6534 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant, 6535 SmallVectorImpl<Decl *> &Decls) { 6536 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {}; 6537 6538 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) { 6539 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries"); 6540 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) { 6541 auto K = (Decl::Kind)+LexicalDecls[I]; 6542 if (!IsKindWeWant(K)) 6543 continue; 6544 6545 auto ID = (serialization::DeclID)+LexicalDecls[I + 1]; 6546 6547 // Don't add predefined declarations to the lexical context more 6548 // than once. 6549 if (ID < NUM_PREDEF_DECL_IDS) { 6550 if (PredefsVisited[ID]) 6551 continue; 6552 6553 PredefsVisited[ID] = true; 6554 } 6555 6556 if (Decl *D = GetLocalDecl(*M, ID)) { 6557 assert(D->getKind() == K && "wrong kind for lexical decl"); 6558 if (!DC->isDeclInLexicalTraversal(D)) 6559 Decls.push_back(D); 6560 } 6561 } 6562 }; 6563 6564 if (isa<TranslationUnitDecl>(DC)) { 6565 for (auto Lexical : TULexicalDecls) 6566 Visit(Lexical.first, Lexical.second); 6567 } else { 6568 auto I = LexicalDecls.find(DC); 6569 if (I != LexicalDecls.end()) 6570 Visit(I->second.first, I->second.second); 6571 } 6572 6573 ++NumLexicalDeclContextsRead; 6574 } 6575 6576 namespace { 6577 6578 class DeclIDComp { 6579 ASTReader &Reader; 6580 ModuleFile &Mod; 6581 6582 public: 6583 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {} 6584 6585 bool operator()(LocalDeclID L, LocalDeclID R) const { 6586 SourceLocation LHS = getLocation(L); 6587 SourceLocation RHS = getLocation(R); 6588 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6589 } 6590 6591 bool operator()(SourceLocation LHS, LocalDeclID R) const { 6592 SourceLocation RHS = getLocation(R); 6593 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6594 } 6595 6596 bool operator()(LocalDeclID L, SourceLocation RHS) const { 6597 SourceLocation LHS = getLocation(L); 6598 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6599 } 6600 6601 SourceLocation getLocation(LocalDeclID ID) const { 6602 return Reader.getSourceManager().getFileLoc( 6603 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID))); 6604 } 6605 }; 6606 6607 } 6608 6609 void ASTReader::FindFileRegionDecls(FileID File, 6610 unsigned Offset, unsigned Length, 6611 SmallVectorImpl<Decl *> &Decls) { 6612 SourceManager &SM = getSourceManager(); 6613 6614 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File); 6615 if (I == FileDeclIDs.end()) 6616 return; 6617 6618 FileDeclsInfo &DInfo = I->second; 6619 if (DInfo.Decls.empty()) 6620 return; 6621 6622 SourceLocation 6623 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset); 6624 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length); 6625 6626 DeclIDComp DIDComp(*this, *DInfo.Mod); 6627 ArrayRef<serialization::LocalDeclID>::iterator 6628 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(), 6629 BeginLoc, DIDComp); 6630 if (BeginIt != DInfo.Decls.begin()) 6631 --BeginIt; 6632 6633 // If we are pointing at a top-level decl inside an objc container, we need 6634 // to backtrack until we find it otherwise we will fail to report that the 6635 // region overlaps with an objc container. 6636 while (BeginIt != DInfo.Decls.begin() && 6637 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt)) 6638 ->isTopLevelDeclInObjCContainer()) 6639 --BeginIt; 6640 6641 ArrayRef<serialization::LocalDeclID>::iterator 6642 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(), 6643 EndLoc, DIDComp); 6644 if (EndIt != DInfo.Decls.end()) 6645 ++EndIt; 6646 6647 for (ArrayRef<serialization::LocalDeclID>::iterator 6648 DIt = BeginIt; DIt != EndIt; ++DIt) 6649 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt))); 6650 } 6651 6652 bool 6653 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, 6654 DeclarationName Name) { 6655 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() && 6656 "DeclContext has no visible decls in storage"); 6657 if (!Name) 6658 return false; 6659 6660 auto It = Lookups.find(DC); 6661 if (It == Lookups.end()) 6662 return false; 6663 6664 Deserializing LookupResults(this); 6665 6666 // Load the list of declarations. 6667 SmallVector<NamedDecl *, 64> Decls; 6668 for (DeclID ID : It->second.Table.find(Name)) { 6669 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 6670 if (ND->getDeclName() == Name) 6671 Decls.push_back(ND); 6672 } 6673 6674 ++NumVisibleDeclContextsRead; 6675 SetExternalVisibleDeclsForName(DC, Name, Decls); 6676 return !Decls.empty(); 6677 } 6678 6679 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) { 6680 if (!DC->hasExternalVisibleStorage()) 6681 return; 6682 6683 auto It = Lookups.find(DC); 6684 assert(It != Lookups.end() && 6685 "have external visible storage but no lookup tables"); 6686 6687 DeclsMap Decls; 6688 6689 for (DeclID ID : It->second.Table.findAll()) { 6690 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 6691 Decls[ND->getDeclName()].push_back(ND); 6692 } 6693 6694 ++NumVisibleDeclContextsRead; 6695 6696 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) { 6697 SetExternalVisibleDeclsForName(DC, I->first, I->second); 6698 } 6699 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false); 6700 } 6701 6702 const serialization::reader::DeclContextLookupTable * 6703 ASTReader::getLoadedLookupTables(DeclContext *Primary) const { 6704 auto I = Lookups.find(Primary); 6705 return I == Lookups.end() ? nullptr : &I->second; 6706 } 6707 6708 /// \brief Under non-PCH compilation the consumer receives the objc methods 6709 /// before receiving the implementation, and codegen depends on this. 6710 /// We simulate this by deserializing and passing to consumer the methods of the 6711 /// implementation before passing the deserialized implementation decl. 6712 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD, 6713 ASTConsumer *Consumer) { 6714 assert(ImplD && Consumer); 6715 6716 for (auto *I : ImplD->methods()) 6717 Consumer->HandleInterestingDecl(DeclGroupRef(I)); 6718 6719 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD)); 6720 } 6721 6722 void ASTReader::PassInterestingDeclsToConsumer() { 6723 assert(Consumer); 6724 6725 if (PassingDeclsToConsumer) 6726 return; 6727 6728 // Guard variable to avoid recursively redoing the process of passing 6729 // decls to consumer. 6730 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer, 6731 true); 6732 6733 // Ensure that we've loaded all potentially-interesting declarations 6734 // that need to be eagerly loaded. 6735 for (auto ID : EagerlyDeserializedDecls) 6736 GetDecl(ID); 6737 EagerlyDeserializedDecls.clear(); 6738 6739 while (!InterestingDecls.empty()) { 6740 Decl *D = InterestingDecls.front(); 6741 InterestingDecls.pop_front(); 6742 6743 PassInterestingDeclToConsumer(D); 6744 } 6745 } 6746 6747 void ASTReader::PassInterestingDeclToConsumer(Decl *D) { 6748 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) 6749 PassObjCImplDeclToConsumer(ImplD, Consumer); 6750 else 6751 Consumer->HandleInterestingDecl(DeclGroupRef(D)); 6752 } 6753 6754 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) { 6755 this->Consumer = Consumer; 6756 6757 if (Consumer) 6758 PassInterestingDeclsToConsumer(); 6759 6760 if (DeserializationListener) 6761 DeserializationListener->ReaderInitialized(this); 6762 } 6763 6764 void ASTReader::PrintStats() { 6765 std::fprintf(stderr, "*** AST File Statistics:\n"); 6766 6767 unsigned NumTypesLoaded 6768 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), 6769 QualType()); 6770 unsigned NumDeclsLoaded 6771 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), 6772 (Decl *)nullptr); 6773 unsigned NumIdentifiersLoaded 6774 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), 6775 IdentifiersLoaded.end(), 6776 (IdentifierInfo *)nullptr); 6777 unsigned NumMacrosLoaded 6778 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(), 6779 MacrosLoaded.end(), 6780 (MacroInfo *)nullptr); 6781 unsigned NumSelectorsLoaded 6782 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), 6783 SelectorsLoaded.end(), 6784 Selector()); 6785 6786 if (unsigned TotalNumSLocEntries = getTotalNumSLocs()) 6787 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", 6788 NumSLocEntriesRead, TotalNumSLocEntries, 6789 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); 6790 if (!TypesLoaded.empty()) 6791 std::fprintf(stderr, " %u/%u types read (%f%%)\n", 6792 NumTypesLoaded, (unsigned)TypesLoaded.size(), 6793 ((float)NumTypesLoaded/TypesLoaded.size() * 100)); 6794 if (!DeclsLoaded.empty()) 6795 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", 6796 NumDeclsLoaded, (unsigned)DeclsLoaded.size(), 6797 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); 6798 if (!IdentifiersLoaded.empty()) 6799 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", 6800 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), 6801 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); 6802 if (!MacrosLoaded.empty()) 6803 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 6804 NumMacrosLoaded, (unsigned)MacrosLoaded.size(), 6805 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100)); 6806 if (!SelectorsLoaded.empty()) 6807 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", 6808 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), 6809 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); 6810 if (TotalNumStatements) 6811 std::fprintf(stderr, " %u/%u statements read (%f%%)\n", 6812 NumStatementsRead, TotalNumStatements, 6813 ((float)NumStatementsRead/TotalNumStatements * 100)); 6814 if (TotalNumMacros) 6815 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 6816 NumMacrosRead, TotalNumMacros, 6817 ((float)NumMacrosRead/TotalNumMacros * 100)); 6818 if (TotalLexicalDeclContexts) 6819 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", 6820 NumLexicalDeclContextsRead, TotalLexicalDeclContexts, 6821 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts 6822 * 100)); 6823 if (TotalVisibleDeclContexts) 6824 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", 6825 NumVisibleDeclContextsRead, TotalVisibleDeclContexts, 6826 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts 6827 * 100)); 6828 if (TotalNumMethodPoolEntries) { 6829 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", 6830 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, 6831 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries 6832 * 100)); 6833 } 6834 if (NumMethodPoolLookups) { 6835 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n", 6836 NumMethodPoolHits, NumMethodPoolLookups, 6837 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0)); 6838 } 6839 if (NumMethodPoolTableLookups) { 6840 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n", 6841 NumMethodPoolTableHits, NumMethodPoolTableLookups, 6842 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups 6843 * 100.0)); 6844 } 6845 6846 if (NumIdentifierLookupHits) { 6847 std::fprintf(stderr, 6848 " %u / %u identifier table lookups succeeded (%f%%)\n", 6849 NumIdentifierLookupHits, NumIdentifierLookups, 6850 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups); 6851 } 6852 6853 if (GlobalIndex) { 6854 std::fprintf(stderr, "\n"); 6855 GlobalIndex->printStats(); 6856 } 6857 6858 std::fprintf(stderr, "\n"); 6859 dump(); 6860 std::fprintf(stderr, "\n"); 6861 } 6862 6863 template<typename Key, typename ModuleFile, unsigned InitialCapacity> 6864 static void 6865 dumpModuleIDMap(StringRef Name, 6866 const ContinuousRangeMap<Key, ModuleFile *, 6867 InitialCapacity> &Map) { 6868 if (Map.begin() == Map.end()) 6869 return; 6870 6871 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType; 6872 llvm::errs() << Name << ":\n"; 6873 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end(); 6874 I != IEnd; ++I) { 6875 llvm::errs() << " " << I->first << " -> " << I->second->FileName 6876 << "\n"; 6877 } 6878 } 6879 6880 LLVM_DUMP_METHOD void ASTReader::dump() { 6881 llvm::errs() << "*** PCH/ModuleFile Remappings:\n"; 6882 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap); 6883 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap); 6884 dumpModuleIDMap("Global type map", GlobalTypeMap); 6885 dumpModuleIDMap("Global declaration map", GlobalDeclMap); 6886 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap); 6887 dumpModuleIDMap("Global macro map", GlobalMacroMap); 6888 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap); 6889 dumpModuleIDMap("Global selector map", GlobalSelectorMap); 6890 dumpModuleIDMap("Global preprocessed entity map", 6891 GlobalPreprocessedEntityMap); 6892 6893 llvm::errs() << "\n*** PCH/Modules Loaded:"; 6894 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(), 6895 MEnd = ModuleMgr.end(); 6896 M != MEnd; ++M) 6897 (*M)->dump(); 6898 } 6899 6900 /// Return the amount of memory used by memory buffers, breaking down 6901 /// by heap-backed versus mmap'ed memory. 6902 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const { 6903 for (ModuleConstIterator I = ModuleMgr.begin(), 6904 E = ModuleMgr.end(); I != E; ++I) { 6905 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) { 6906 size_t bytes = buf->getBufferSize(); 6907 switch (buf->getBufferKind()) { 6908 case llvm::MemoryBuffer::MemoryBuffer_Malloc: 6909 sizes.malloc_bytes += bytes; 6910 break; 6911 case llvm::MemoryBuffer::MemoryBuffer_MMap: 6912 sizes.mmap_bytes += bytes; 6913 break; 6914 } 6915 } 6916 } 6917 } 6918 6919 void ASTReader::InitializeSema(Sema &S) { 6920 SemaObj = &S; 6921 S.addExternalSource(this); 6922 6923 // Makes sure any declarations that were deserialized "too early" 6924 // still get added to the identifier's declaration chains. 6925 for (uint64_t ID : PreloadedDeclIDs) { 6926 NamedDecl *D = cast<NamedDecl>(GetDecl(ID)); 6927 pushExternalDeclIntoScope(D, D->getDeclName()); 6928 } 6929 PreloadedDeclIDs.clear(); 6930 6931 // FIXME: What happens if these are changed by a module import? 6932 if (!FPPragmaOptions.empty()) { 6933 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS"); 6934 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0]; 6935 } 6936 6937 // FIXME: What happens if these are changed by a module import? 6938 if (!OpenCLExtensions.empty()) { 6939 unsigned I = 0; 6940 #define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++]; 6941 #include "clang/Basic/OpenCLExtensions.def" 6942 6943 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS"); 6944 } 6945 6946 UpdateSema(); 6947 } 6948 6949 void ASTReader::UpdateSema() { 6950 assert(SemaObj && "no Sema to update"); 6951 6952 // Load the offsets of the declarations that Sema references. 6953 // They will be lazily deserialized when needed. 6954 if (!SemaDeclRefs.empty()) { 6955 assert(SemaDeclRefs.size() % 2 == 0); 6956 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 2) { 6957 if (!SemaObj->StdNamespace) 6958 SemaObj->StdNamespace = SemaDeclRefs[I]; 6959 if (!SemaObj->StdBadAlloc) 6960 SemaObj->StdBadAlloc = SemaDeclRefs[I+1]; 6961 } 6962 SemaDeclRefs.clear(); 6963 } 6964 6965 // Update the state of pragmas. Use the same API as if we had encountered the 6966 // pragma in the source. 6967 if(OptimizeOffPragmaLocation.isValid()) 6968 SemaObj->ActOnPragmaOptimize(/* IsOn = */ false, OptimizeOffPragmaLocation); 6969 if (PragmaMSStructState != -1) 6970 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState); 6971 if (PointersToMembersPragmaLocation.isValid()) { 6972 SemaObj->ActOnPragmaMSPointersToMembers( 6973 (LangOptions::PragmaMSPointersToMembersKind) 6974 PragmaMSPointersToMembersState, 6975 PointersToMembersPragmaLocation); 6976 } 6977 } 6978 6979 IdentifierInfo *ASTReader::get(StringRef Name) { 6980 // Note that we are loading an identifier. 6981 Deserializing AnIdentifier(this); 6982 6983 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0, 6984 NumIdentifierLookups, 6985 NumIdentifierLookupHits); 6986 6987 // We don't need to do identifier table lookups in C++ modules (we preload 6988 // all interesting declarations, and don't need to use the scope for name 6989 // lookups). Perform the lookup in PCH files, though, since we don't build 6990 // a complete initial identifier table if we're carrying on from a PCH. 6991 if (Context.getLangOpts().CPlusPlus) { 6992 for (auto F : ModuleMgr.pch_modules()) 6993 if (Visitor(*F)) 6994 break; 6995 } else { 6996 // If there is a global index, look there first to determine which modules 6997 // provably do not have any results for this identifier. 6998 GlobalModuleIndex::HitSet Hits; 6999 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 7000 if (!loadGlobalIndex()) { 7001 if (GlobalIndex->lookupIdentifier(Name, Hits)) { 7002 HitsPtr = &Hits; 7003 } 7004 } 7005 7006 ModuleMgr.visit(Visitor, HitsPtr); 7007 } 7008 7009 IdentifierInfo *II = Visitor.getIdentifierInfo(); 7010 markIdentifierUpToDate(II); 7011 return II; 7012 } 7013 7014 namespace clang { 7015 /// \brief An identifier-lookup iterator that enumerates all of the 7016 /// identifiers stored within a set of AST files. 7017 class ASTIdentifierIterator : public IdentifierIterator { 7018 /// \brief The AST reader whose identifiers are being enumerated. 7019 const ASTReader &Reader; 7020 7021 /// \brief The current index into the chain of AST files stored in 7022 /// the AST reader. 7023 unsigned Index; 7024 7025 /// \brief The current position within the identifier lookup table 7026 /// of the current AST file. 7027 ASTIdentifierLookupTable::key_iterator Current; 7028 7029 /// \brief The end position within the identifier lookup table of 7030 /// the current AST file. 7031 ASTIdentifierLookupTable::key_iterator End; 7032 7033 /// \brief Whether to skip any modules in the ASTReader. 7034 bool SkipModules; 7035 7036 public: 7037 explicit ASTIdentifierIterator(const ASTReader &Reader, 7038 bool SkipModules = false); 7039 7040 StringRef Next() override; 7041 }; 7042 } 7043 7044 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader, 7045 bool SkipModules) 7046 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) { 7047 } 7048 7049 StringRef ASTIdentifierIterator::Next() { 7050 while (Current == End) { 7051 // If we have exhausted all of our AST files, we're done. 7052 if (Index == 0) 7053 return StringRef(); 7054 7055 --Index; 7056 ModuleFile &F = Reader.ModuleMgr[Index]; 7057 if (SkipModules && F.isModule()) 7058 continue; 7059 7060 ASTIdentifierLookupTable *IdTable = 7061 (ASTIdentifierLookupTable *)F.IdentifierLookupTable; 7062 Current = IdTable->key_begin(); 7063 End = IdTable->key_end(); 7064 } 7065 7066 // We have any identifiers remaining in the current AST file; return 7067 // the next one. 7068 StringRef Result = *Current; 7069 ++Current; 7070 return Result; 7071 } 7072 7073 namespace { 7074 /// A utility for appending two IdentifierIterators. 7075 class ChainedIdentifierIterator : public IdentifierIterator { 7076 std::unique_ptr<IdentifierIterator> Current; 7077 std::unique_ptr<IdentifierIterator> Queued; 7078 7079 public: 7080 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First, 7081 std::unique_ptr<IdentifierIterator> Second) 7082 : Current(std::move(First)), Queued(std::move(Second)) {} 7083 7084 StringRef Next() override { 7085 if (!Current) 7086 return StringRef(); 7087 7088 StringRef result = Current->Next(); 7089 if (!result.empty()) 7090 return result; 7091 7092 // Try the queued iterator, which may itself be empty. 7093 Current.reset(); 7094 std::swap(Current, Queued); 7095 return Next(); 7096 } 7097 }; 7098 } // end anonymous namespace. 7099 7100 IdentifierIterator *ASTReader::getIdentifiers() { 7101 if (!loadGlobalIndex()) { 7102 std::unique_ptr<IdentifierIterator> ReaderIter( 7103 new ASTIdentifierIterator(*this, /*SkipModules=*/true)); 7104 std::unique_ptr<IdentifierIterator> ModulesIter( 7105 GlobalIndex->createIdentifierIterator()); 7106 return new ChainedIdentifierIterator(std::move(ReaderIter), 7107 std::move(ModulesIter)); 7108 } 7109 7110 return new ASTIdentifierIterator(*this); 7111 } 7112 7113 namespace clang { namespace serialization { 7114 class ReadMethodPoolVisitor { 7115 ASTReader &Reader; 7116 Selector Sel; 7117 unsigned PriorGeneration; 7118 unsigned InstanceBits; 7119 unsigned FactoryBits; 7120 bool InstanceHasMoreThanOneDecl; 7121 bool FactoryHasMoreThanOneDecl; 7122 SmallVector<ObjCMethodDecl *, 4> InstanceMethods; 7123 SmallVector<ObjCMethodDecl *, 4> FactoryMethods; 7124 7125 public: 7126 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel, 7127 unsigned PriorGeneration) 7128 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration), 7129 InstanceBits(0), FactoryBits(0), InstanceHasMoreThanOneDecl(false), 7130 FactoryHasMoreThanOneDecl(false) {} 7131 7132 bool operator()(ModuleFile &M) { 7133 if (!M.SelectorLookupTable) 7134 return false; 7135 7136 // If we've already searched this module file, skip it now. 7137 if (M.Generation <= PriorGeneration) 7138 return true; 7139 7140 ++Reader.NumMethodPoolTableLookups; 7141 ASTSelectorLookupTable *PoolTable 7142 = (ASTSelectorLookupTable*)M.SelectorLookupTable; 7143 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel); 7144 if (Pos == PoolTable->end()) 7145 return false; 7146 7147 ++Reader.NumMethodPoolTableHits; 7148 ++Reader.NumSelectorsRead; 7149 // FIXME: Not quite happy with the statistics here. We probably should 7150 // disable this tracking when called via LoadSelector. 7151 // Also, should entries without methods count as misses? 7152 ++Reader.NumMethodPoolEntriesRead; 7153 ASTSelectorLookupTrait::data_type Data = *Pos; 7154 if (Reader.DeserializationListener) 7155 Reader.DeserializationListener->SelectorRead(Data.ID, Sel); 7156 7157 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end()); 7158 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end()); 7159 InstanceBits = Data.InstanceBits; 7160 FactoryBits = Data.FactoryBits; 7161 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl; 7162 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl; 7163 return true; 7164 } 7165 7166 /// \brief Retrieve the instance methods found by this visitor. 7167 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const { 7168 return InstanceMethods; 7169 } 7170 7171 /// \brief Retrieve the instance methods found by this visitor. 7172 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const { 7173 return FactoryMethods; 7174 } 7175 7176 unsigned getInstanceBits() const { return InstanceBits; } 7177 unsigned getFactoryBits() const { return FactoryBits; } 7178 bool instanceHasMoreThanOneDecl() const { 7179 return InstanceHasMoreThanOneDecl; 7180 } 7181 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; } 7182 }; 7183 } } // end namespace clang::serialization 7184 7185 /// \brief Add the given set of methods to the method list. 7186 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods, 7187 ObjCMethodList &List) { 7188 for (unsigned I = 0, N = Methods.size(); I != N; ++I) { 7189 S.addMethodToGlobalList(&List, Methods[I]); 7190 } 7191 } 7192 7193 void ASTReader::ReadMethodPool(Selector Sel) { 7194 // Get the selector generation and update it to the current generation. 7195 unsigned &Generation = SelectorGeneration[Sel]; 7196 unsigned PriorGeneration = Generation; 7197 Generation = getGeneration(); 7198 SelectorOutOfDate[Sel] = false; 7199 7200 // Search for methods defined with this selector. 7201 ++NumMethodPoolLookups; 7202 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration); 7203 ModuleMgr.visit(Visitor); 7204 7205 if (Visitor.getInstanceMethods().empty() && 7206 Visitor.getFactoryMethods().empty()) 7207 return; 7208 7209 ++NumMethodPoolHits; 7210 7211 if (!getSema()) 7212 return; 7213 7214 Sema &S = *getSema(); 7215 Sema::GlobalMethodPool::iterator Pos 7216 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first; 7217 7218 Pos->second.first.setBits(Visitor.getInstanceBits()); 7219 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl()); 7220 Pos->second.second.setBits(Visitor.getFactoryBits()); 7221 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl()); 7222 7223 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since 7224 // when building a module we keep every method individually and may need to 7225 // update hasMoreThanOneDecl as we add the methods. 7226 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first); 7227 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second); 7228 } 7229 7230 void ASTReader::updateOutOfDateSelector(Selector Sel) { 7231 if (SelectorOutOfDate[Sel]) 7232 ReadMethodPool(Sel); 7233 } 7234 7235 void ASTReader::ReadKnownNamespaces( 7236 SmallVectorImpl<NamespaceDecl *> &Namespaces) { 7237 Namespaces.clear(); 7238 7239 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) { 7240 if (NamespaceDecl *Namespace 7241 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I]))) 7242 Namespaces.push_back(Namespace); 7243 } 7244 } 7245 7246 void ASTReader::ReadUndefinedButUsed( 7247 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) { 7248 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) { 7249 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++])); 7250 SourceLocation Loc = 7251 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]); 7252 Undefined.insert(std::make_pair(D, Loc)); 7253 } 7254 } 7255 7256 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector< 7257 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> & 7258 Exprs) { 7259 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) { 7260 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++])); 7261 uint64_t Count = DelayedDeleteExprs[Idx++]; 7262 for (uint64_t C = 0; C < Count; ++C) { 7263 SourceLocation DeleteLoc = 7264 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]); 7265 const bool IsArrayForm = DelayedDeleteExprs[Idx++]; 7266 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm)); 7267 } 7268 } 7269 } 7270 7271 void ASTReader::ReadTentativeDefinitions( 7272 SmallVectorImpl<VarDecl *> &TentativeDefs) { 7273 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { 7274 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I])); 7275 if (Var) 7276 TentativeDefs.push_back(Var); 7277 } 7278 TentativeDefinitions.clear(); 7279 } 7280 7281 void ASTReader::ReadUnusedFileScopedDecls( 7282 SmallVectorImpl<const DeclaratorDecl *> &Decls) { 7283 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) { 7284 DeclaratorDecl *D 7285 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I])); 7286 if (D) 7287 Decls.push_back(D); 7288 } 7289 UnusedFileScopedDecls.clear(); 7290 } 7291 7292 void ASTReader::ReadDelegatingConstructors( 7293 SmallVectorImpl<CXXConstructorDecl *> &Decls) { 7294 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) { 7295 CXXConstructorDecl *D 7296 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I])); 7297 if (D) 7298 Decls.push_back(D); 7299 } 7300 DelegatingCtorDecls.clear(); 7301 } 7302 7303 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) { 7304 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) { 7305 TypedefNameDecl *D 7306 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])); 7307 if (D) 7308 Decls.push_back(D); 7309 } 7310 ExtVectorDecls.clear(); 7311 } 7312 7313 void ASTReader::ReadUnusedLocalTypedefNameCandidates( 7314 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) { 7315 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N; 7316 ++I) { 7317 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>( 7318 GetDecl(UnusedLocalTypedefNameCandidates[I])); 7319 if (D) 7320 Decls.insert(D); 7321 } 7322 UnusedLocalTypedefNameCandidates.clear(); 7323 } 7324 7325 void ASTReader::ReadReferencedSelectors( 7326 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) { 7327 if (ReferencedSelectorsData.empty()) 7328 return; 7329 7330 // If there are @selector references added them to its pool. This is for 7331 // implementation of -Wselector. 7332 unsigned int DataSize = ReferencedSelectorsData.size()-1; 7333 unsigned I = 0; 7334 while (I < DataSize) { 7335 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]); 7336 SourceLocation SelLoc 7337 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]); 7338 Sels.push_back(std::make_pair(Sel, SelLoc)); 7339 } 7340 ReferencedSelectorsData.clear(); 7341 } 7342 7343 void ASTReader::ReadWeakUndeclaredIdentifiers( 7344 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) { 7345 if (WeakUndeclaredIdentifiers.empty()) 7346 return; 7347 7348 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) { 7349 IdentifierInfo *WeakId 7350 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 7351 IdentifierInfo *AliasId 7352 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 7353 SourceLocation Loc 7354 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]); 7355 bool Used = WeakUndeclaredIdentifiers[I++]; 7356 WeakInfo WI(AliasId, Loc); 7357 WI.setUsed(Used); 7358 WeakIDs.push_back(std::make_pair(WeakId, WI)); 7359 } 7360 WeakUndeclaredIdentifiers.clear(); 7361 } 7362 7363 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) { 7364 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) { 7365 ExternalVTableUse VT; 7366 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); 7367 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]); 7368 VT.DefinitionRequired = VTableUses[Idx++]; 7369 VTables.push_back(VT); 7370 } 7371 7372 VTableUses.clear(); 7373 } 7374 7375 void ASTReader::ReadPendingInstantiations( 7376 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) { 7377 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) { 7378 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++])); 7379 SourceLocation Loc 7380 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]); 7381 7382 Pending.push_back(std::make_pair(D, Loc)); 7383 } 7384 PendingInstantiations.clear(); 7385 } 7386 7387 void ASTReader::ReadLateParsedTemplates( 7388 llvm::MapVector<const FunctionDecl *, LateParsedTemplate *> &LPTMap) { 7389 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N; 7390 /* In loop */) { 7391 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++])); 7392 7393 LateParsedTemplate *LT = new LateParsedTemplate; 7394 LT->D = GetDecl(LateParsedTemplates[Idx++]); 7395 7396 ModuleFile *F = getOwningModuleFile(LT->D); 7397 assert(F && "No module"); 7398 7399 unsigned TokN = LateParsedTemplates[Idx++]; 7400 LT->Toks.reserve(TokN); 7401 for (unsigned T = 0; T < TokN; ++T) 7402 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx)); 7403 7404 LPTMap.insert(std::make_pair(FD, LT)); 7405 } 7406 7407 LateParsedTemplates.clear(); 7408 } 7409 7410 void ASTReader::LoadSelector(Selector Sel) { 7411 // It would be complicated to avoid reading the methods anyway. So don't. 7412 ReadMethodPool(Sel); 7413 } 7414 7415 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) { 7416 assert(ID && "Non-zero identifier ID required"); 7417 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); 7418 IdentifiersLoaded[ID - 1] = II; 7419 if (DeserializationListener) 7420 DeserializationListener->IdentifierRead(ID, II); 7421 } 7422 7423 /// \brief Set the globally-visible declarations associated with the given 7424 /// identifier. 7425 /// 7426 /// If the AST reader is currently in a state where the given declaration IDs 7427 /// cannot safely be resolved, they are queued until it is safe to resolve 7428 /// them. 7429 /// 7430 /// \param II an IdentifierInfo that refers to one or more globally-visible 7431 /// declarations. 7432 /// 7433 /// \param DeclIDs the set of declaration IDs with the name @p II that are 7434 /// visible at global scope. 7435 /// 7436 /// \param Decls if non-null, this vector will be populated with the set of 7437 /// deserialized declarations. These declarations will not be pushed into 7438 /// scope. 7439 void 7440 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, 7441 const SmallVectorImpl<uint32_t> &DeclIDs, 7442 SmallVectorImpl<Decl *> *Decls) { 7443 if (NumCurrentElementsDeserializing && !Decls) { 7444 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end()); 7445 return; 7446 } 7447 7448 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { 7449 if (!SemaObj) { 7450 // Queue this declaration so that it will be added to the 7451 // translation unit scope and identifier's declaration chain 7452 // once a Sema object is known. 7453 PreloadedDeclIDs.push_back(DeclIDs[I]); 7454 continue; 7455 } 7456 7457 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); 7458 7459 // If we're simply supposed to record the declarations, do so now. 7460 if (Decls) { 7461 Decls->push_back(D); 7462 continue; 7463 } 7464 7465 // Introduce this declaration into the translation-unit scope 7466 // and add it to the declaration chain for this identifier, so 7467 // that (unqualified) name lookup will find it. 7468 pushExternalDeclIntoScope(D, II); 7469 } 7470 } 7471 7472 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) { 7473 if (ID == 0) 7474 return nullptr; 7475 7476 if (IdentifiersLoaded.empty()) { 7477 Error("no identifier table in AST file"); 7478 return nullptr; 7479 } 7480 7481 ID -= 1; 7482 if (!IdentifiersLoaded[ID]) { 7483 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1); 7484 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map"); 7485 ModuleFile *M = I->second; 7486 unsigned Index = ID - M->BaseIdentifierID; 7487 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index]; 7488 7489 // All of the strings in the AST file are preceded by a 16-bit length. 7490 // Extract that 16-bit length to avoid having to execute strlen(). 7491 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as 7492 // unsigned integers. This is important to avoid integer overflow when 7493 // we cast them to 'unsigned'. 7494 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2; 7495 unsigned StrLen = (((unsigned) StrLenPtr[0]) 7496 | (((unsigned) StrLenPtr[1]) << 8)) - 1; 7497 auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen)); 7498 IdentifiersLoaded[ID] = &II; 7499 markIdentifierFromAST(*this, II); 7500 if (DeserializationListener) 7501 DeserializationListener->IdentifierRead(ID + 1, &II); 7502 } 7503 7504 return IdentifiersLoaded[ID]; 7505 } 7506 7507 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) { 7508 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID)); 7509 } 7510 7511 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) { 7512 if (LocalID < NUM_PREDEF_IDENT_IDS) 7513 return LocalID; 7514 7515 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7516 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS); 7517 assert(I != M.IdentifierRemap.end() 7518 && "Invalid index into identifier index remap"); 7519 7520 return LocalID + I->second; 7521 } 7522 7523 MacroInfo *ASTReader::getMacro(MacroID ID) { 7524 if (ID == 0) 7525 return nullptr; 7526 7527 if (MacrosLoaded.empty()) { 7528 Error("no macro table in AST file"); 7529 return nullptr; 7530 } 7531 7532 ID -= NUM_PREDEF_MACRO_IDS; 7533 if (!MacrosLoaded[ID]) { 7534 GlobalMacroMapType::iterator I 7535 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS); 7536 assert(I != GlobalMacroMap.end() && "Corrupted global macro map"); 7537 ModuleFile *M = I->second; 7538 unsigned Index = ID - M->BaseMacroID; 7539 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]); 7540 7541 if (DeserializationListener) 7542 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS, 7543 MacrosLoaded[ID]); 7544 } 7545 7546 return MacrosLoaded[ID]; 7547 } 7548 7549 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) { 7550 if (LocalID < NUM_PREDEF_MACRO_IDS) 7551 return LocalID; 7552 7553 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7554 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS); 7555 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap"); 7556 7557 return LocalID + I->second; 7558 } 7559 7560 serialization::SubmoduleID 7561 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) { 7562 if (LocalID < NUM_PREDEF_SUBMODULE_IDS) 7563 return LocalID; 7564 7565 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7566 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS); 7567 assert(I != M.SubmoduleRemap.end() 7568 && "Invalid index into submodule index remap"); 7569 7570 return LocalID + I->second; 7571 } 7572 7573 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) { 7574 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) { 7575 assert(GlobalID == 0 && "Unhandled global submodule ID"); 7576 return nullptr; 7577 } 7578 7579 if (GlobalID > SubmodulesLoaded.size()) { 7580 Error("submodule ID out of range in AST file"); 7581 return nullptr; 7582 } 7583 7584 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS]; 7585 } 7586 7587 Module *ASTReader::getModule(unsigned ID) { 7588 return getSubmodule(ID); 7589 } 7590 7591 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) { 7592 if (ID & 1) { 7593 // It's a module, look it up by submodule ID. 7594 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1)); 7595 return I == GlobalSubmoduleMap.end() ? nullptr : I->second; 7596 } else { 7597 // It's a prefix (preamble, PCH, ...). Look it up by index. 7598 unsigned IndexFromEnd = ID >> 1; 7599 assert(IndexFromEnd && "got reference to unknown module file"); 7600 return getModuleManager().pch_modules().end()[-IndexFromEnd]; 7601 } 7602 } 7603 7604 unsigned ASTReader::getModuleFileID(ModuleFile *F) { 7605 if (!F) 7606 return 1; 7607 7608 // For a file representing a module, use the submodule ID of the top-level 7609 // module as the file ID. For any other kind of file, the number of such 7610 // files loaded beforehand will be the same on reload. 7611 // FIXME: Is this true even if we have an explicit module file and a PCH? 7612 if (F->isModule()) 7613 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1; 7614 7615 auto PCHModules = getModuleManager().pch_modules(); 7616 auto I = std::find(PCHModules.begin(), PCHModules.end(), F); 7617 assert(I != PCHModules.end() && "emitting reference to unknown file"); 7618 return (I - PCHModules.end()) << 1; 7619 } 7620 7621 llvm::Optional<ExternalASTSource::ASTSourceDescriptor> 7622 ASTReader::getSourceDescriptor(unsigned ID) { 7623 if (const Module *M = getSubmodule(ID)) 7624 return ExternalASTSource::ASTSourceDescriptor(*M); 7625 7626 // If there is only a single PCH, return it instead. 7627 // Chained PCH are not suported. 7628 if (ModuleMgr.size() == 1) { 7629 ModuleFile &MF = ModuleMgr.getPrimaryModule(); 7630 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName); 7631 StringRef FileName = llvm::sys::path::filename(MF.FileName); 7632 return ASTReader::ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName, 7633 MF.Signature); 7634 } 7635 return None; 7636 } 7637 7638 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) { 7639 return DecodeSelector(getGlobalSelectorID(M, LocalID)); 7640 } 7641 7642 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) { 7643 if (ID == 0) 7644 return Selector(); 7645 7646 if (ID > SelectorsLoaded.size()) { 7647 Error("selector ID out of range in AST file"); 7648 return Selector(); 7649 } 7650 7651 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) { 7652 // Load this selector from the selector table. 7653 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID); 7654 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map"); 7655 ModuleFile &M = *I->second; 7656 ASTSelectorLookupTrait Trait(*this, M); 7657 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS; 7658 SelectorsLoaded[ID - 1] = 7659 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0); 7660 if (DeserializationListener) 7661 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 7662 } 7663 7664 return SelectorsLoaded[ID - 1]; 7665 } 7666 7667 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) { 7668 return DecodeSelector(ID); 7669 } 7670 7671 uint32_t ASTReader::GetNumExternalSelectors() { 7672 // ID 0 (the null selector) is considered an external selector. 7673 return getTotalNumSelectors() + 1; 7674 } 7675 7676 serialization::SelectorID 7677 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const { 7678 if (LocalID < NUM_PREDEF_SELECTOR_IDS) 7679 return LocalID; 7680 7681 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7682 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS); 7683 assert(I != M.SelectorRemap.end() 7684 && "Invalid index into selector index remap"); 7685 7686 return LocalID + I->second; 7687 } 7688 7689 DeclarationName 7690 ASTReader::ReadDeclarationName(ModuleFile &F, 7691 const RecordData &Record, unsigned &Idx) { 7692 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++]; 7693 switch (Kind) { 7694 case DeclarationName::Identifier: 7695 return DeclarationName(GetIdentifierInfo(F, Record, Idx)); 7696 7697 case DeclarationName::ObjCZeroArgSelector: 7698 case DeclarationName::ObjCOneArgSelector: 7699 case DeclarationName::ObjCMultiArgSelector: 7700 return DeclarationName(ReadSelector(F, Record, Idx)); 7701 7702 case DeclarationName::CXXConstructorName: 7703 return Context.DeclarationNames.getCXXConstructorName( 7704 Context.getCanonicalType(readType(F, Record, Idx))); 7705 7706 case DeclarationName::CXXDestructorName: 7707 return Context.DeclarationNames.getCXXDestructorName( 7708 Context.getCanonicalType(readType(F, Record, Idx))); 7709 7710 case DeclarationName::CXXConversionFunctionName: 7711 return Context.DeclarationNames.getCXXConversionFunctionName( 7712 Context.getCanonicalType(readType(F, Record, Idx))); 7713 7714 case DeclarationName::CXXOperatorName: 7715 return Context.DeclarationNames.getCXXOperatorName( 7716 (OverloadedOperatorKind)Record[Idx++]); 7717 7718 case DeclarationName::CXXLiteralOperatorName: 7719 return Context.DeclarationNames.getCXXLiteralOperatorName( 7720 GetIdentifierInfo(F, Record, Idx)); 7721 7722 case DeclarationName::CXXUsingDirective: 7723 return DeclarationName::getUsingDirectiveName(); 7724 } 7725 7726 llvm_unreachable("Invalid NameKind!"); 7727 } 7728 7729 void ASTReader::ReadDeclarationNameLoc(ModuleFile &F, 7730 DeclarationNameLoc &DNLoc, 7731 DeclarationName Name, 7732 const RecordData &Record, unsigned &Idx) { 7733 switch (Name.getNameKind()) { 7734 case DeclarationName::CXXConstructorName: 7735 case DeclarationName::CXXDestructorName: 7736 case DeclarationName::CXXConversionFunctionName: 7737 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx); 7738 break; 7739 7740 case DeclarationName::CXXOperatorName: 7741 DNLoc.CXXOperatorName.BeginOpNameLoc 7742 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 7743 DNLoc.CXXOperatorName.EndOpNameLoc 7744 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 7745 break; 7746 7747 case DeclarationName::CXXLiteralOperatorName: 7748 DNLoc.CXXLiteralOperatorName.OpNameLoc 7749 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 7750 break; 7751 7752 case DeclarationName::Identifier: 7753 case DeclarationName::ObjCZeroArgSelector: 7754 case DeclarationName::ObjCOneArgSelector: 7755 case DeclarationName::ObjCMultiArgSelector: 7756 case DeclarationName::CXXUsingDirective: 7757 break; 7758 } 7759 } 7760 7761 void ASTReader::ReadDeclarationNameInfo(ModuleFile &F, 7762 DeclarationNameInfo &NameInfo, 7763 const RecordData &Record, unsigned &Idx) { 7764 NameInfo.setName(ReadDeclarationName(F, Record, Idx)); 7765 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx)); 7766 DeclarationNameLoc DNLoc; 7767 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx); 7768 NameInfo.setInfo(DNLoc); 7769 } 7770 7771 void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info, 7772 const RecordData &Record, unsigned &Idx) { 7773 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx); 7774 unsigned NumTPLists = Record[Idx++]; 7775 Info.NumTemplParamLists = NumTPLists; 7776 if (NumTPLists) { 7777 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists]; 7778 for (unsigned i=0; i != NumTPLists; ++i) 7779 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx); 7780 } 7781 } 7782 7783 TemplateName 7784 ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record, 7785 unsigned &Idx) { 7786 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++]; 7787 switch (Kind) { 7788 case TemplateName::Template: 7789 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx)); 7790 7791 case TemplateName::OverloadedTemplate: { 7792 unsigned size = Record[Idx++]; 7793 UnresolvedSet<8> Decls; 7794 while (size--) 7795 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx)); 7796 7797 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end()); 7798 } 7799 7800 case TemplateName::QualifiedTemplate: { 7801 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx); 7802 bool hasTemplKeyword = Record[Idx++]; 7803 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx); 7804 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template); 7805 } 7806 7807 case TemplateName::DependentTemplate: { 7808 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx); 7809 if (Record[Idx++]) // isIdentifier 7810 return Context.getDependentTemplateName(NNS, 7811 GetIdentifierInfo(F, Record, 7812 Idx)); 7813 return Context.getDependentTemplateName(NNS, 7814 (OverloadedOperatorKind)Record[Idx++]); 7815 } 7816 7817 case TemplateName::SubstTemplateTemplateParm: { 7818 TemplateTemplateParmDecl *param 7819 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx); 7820 if (!param) return TemplateName(); 7821 TemplateName replacement = ReadTemplateName(F, Record, Idx); 7822 return Context.getSubstTemplateTemplateParm(param, replacement); 7823 } 7824 7825 case TemplateName::SubstTemplateTemplateParmPack: { 7826 TemplateTemplateParmDecl *Param 7827 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx); 7828 if (!Param) 7829 return TemplateName(); 7830 7831 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx); 7832 if (ArgPack.getKind() != TemplateArgument::Pack) 7833 return TemplateName(); 7834 7835 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack); 7836 } 7837 } 7838 7839 llvm_unreachable("Unhandled template name kind!"); 7840 } 7841 7842 TemplateArgument ASTReader::ReadTemplateArgument(ModuleFile &F, 7843 const RecordData &Record, 7844 unsigned &Idx, 7845 bool Canonicalize) { 7846 if (Canonicalize) { 7847 // The caller wants a canonical template argument. Sometimes the AST only 7848 // wants template arguments in canonical form (particularly as the template 7849 // argument lists of template specializations) so ensure we preserve that 7850 // canonical form across serialization. 7851 TemplateArgument Arg = ReadTemplateArgument(F, Record, Idx, false); 7852 return Context.getCanonicalTemplateArgument(Arg); 7853 } 7854 7855 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++]; 7856 switch (Kind) { 7857 case TemplateArgument::Null: 7858 return TemplateArgument(); 7859 case TemplateArgument::Type: 7860 return TemplateArgument(readType(F, Record, Idx)); 7861 case TemplateArgument::Declaration: { 7862 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx); 7863 return TemplateArgument(D, readType(F, Record, Idx)); 7864 } 7865 case TemplateArgument::NullPtr: 7866 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true); 7867 case TemplateArgument::Integral: { 7868 llvm::APSInt Value = ReadAPSInt(Record, Idx); 7869 QualType T = readType(F, Record, Idx); 7870 return TemplateArgument(Context, Value, T); 7871 } 7872 case TemplateArgument::Template: 7873 return TemplateArgument(ReadTemplateName(F, Record, Idx)); 7874 case TemplateArgument::TemplateExpansion: { 7875 TemplateName Name = ReadTemplateName(F, Record, Idx); 7876 Optional<unsigned> NumTemplateExpansions; 7877 if (unsigned NumExpansions = Record[Idx++]) 7878 NumTemplateExpansions = NumExpansions - 1; 7879 return TemplateArgument(Name, NumTemplateExpansions); 7880 } 7881 case TemplateArgument::Expression: 7882 return TemplateArgument(ReadExpr(F)); 7883 case TemplateArgument::Pack: { 7884 unsigned NumArgs = Record[Idx++]; 7885 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs]; 7886 for (unsigned I = 0; I != NumArgs; ++I) 7887 Args[I] = ReadTemplateArgument(F, Record, Idx); 7888 return TemplateArgument(llvm::makeArrayRef(Args, NumArgs)); 7889 } 7890 } 7891 7892 llvm_unreachable("Unhandled template argument kind!"); 7893 } 7894 7895 TemplateParameterList * 7896 ASTReader::ReadTemplateParameterList(ModuleFile &F, 7897 const RecordData &Record, unsigned &Idx) { 7898 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx); 7899 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx); 7900 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx); 7901 7902 unsigned NumParams = Record[Idx++]; 7903 SmallVector<NamedDecl *, 16> Params; 7904 Params.reserve(NumParams); 7905 while (NumParams--) 7906 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx)); 7907 7908 // TODO: Concepts 7909 TemplateParameterList* TemplateParams = 7910 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, 7911 Params, RAngleLoc, nullptr); 7912 return TemplateParams; 7913 } 7914 7915 void 7916 ASTReader:: 7917 ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs, 7918 ModuleFile &F, const RecordData &Record, 7919 unsigned &Idx, bool Canonicalize) { 7920 unsigned NumTemplateArgs = Record[Idx++]; 7921 TemplArgs.reserve(NumTemplateArgs); 7922 while (NumTemplateArgs--) 7923 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx, Canonicalize)); 7924 } 7925 7926 /// \brief Read a UnresolvedSet structure. 7927 void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set, 7928 const RecordData &Record, unsigned &Idx) { 7929 unsigned NumDecls = Record[Idx++]; 7930 Set.reserve(Context, NumDecls); 7931 while (NumDecls--) { 7932 DeclID ID = ReadDeclID(F, Record, Idx); 7933 AccessSpecifier AS = (AccessSpecifier)Record[Idx++]; 7934 Set.addLazyDecl(Context, ID, AS); 7935 } 7936 } 7937 7938 CXXBaseSpecifier 7939 ASTReader::ReadCXXBaseSpecifier(ModuleFile &F, 7940 const RecordData &Record, unsigned &Idx) { 7941 bool isVirtual = static_cast<bool>(Record[Idx++]); 7942 bool isBaseOfClass = static_cast<bool>(Record[Idx++]); 7943 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]); 7944 bool inheritConstructors = static_cast<bool>(Record[Idx++]); 7945 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx); 7946 SourceRange Range = ReadSourceRange(F, Record, Idx); 7947 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx); 7948 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo, 7949 EllipsisLoc); 7950 Result.setInheritConstructors(inheritConstructors); 7951 return Result; 7952 } 7953 7954 CXXCtorInitializer ** 7955 ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record, 7956 unsigned &Idx) { 7957 unsigned NumInitializers = Record[Idx++]; 7958 assert(NumInitializers && "wrote ctor initializers but have no inits"); 7959 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers]; 7960 for (unsigned i = 0; i != NumInitializers; ++i) { 7961 TypeSourceInfo *TInfo = nullptr; 7962 bool IsBaseVirtual = false; 7963 FieldDecl *Member = nullptr; 7964 IndirectFieldDecl *IndirectMember = nullptr; 7965 7966 CtorInitializerType Type = (CtorInitializerType)Record[Idx++]; 7967 switch (Type) { 7968 case CTOR_INITIALIZER_BASE: 7969 TInfo = GetTypeSourceInfo(F, Record, Idx); 7970 IsBaseVirtual = Record[Idx++]; 7971 break; 7972 7973 case CTOR_INITIALIZER_DELEGATING: 7974 TInfo = GetTypeSourceInfo(F, Record, Idx); 7975 break; 7976 7977 case CTOR_INITIALIZER_MEMBER: 7978 Member = ReadDeclAs<FieldDecl>(F, Record, Idx); 7979 break; 7980 7981 case CTOR_INITIALIZER_INDIRECT_MEMBER: 7982 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx); 7983 break; 7984 } 7985 7986 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx); 7987 Expr *Init = ReadExpr(F); 7988 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx); 7989 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx); 7990 bool IsWritten = Record[Idx++]; 7991 unsigned SourceOrderOrNumArrayIndices; 7992 SmallVector<VarDecl *, 8> Indices; 7993 if (IsWritten) { 7994 SourceOrderOrNumArrayIndices = Record[Idx++]; 7995 } else { 7996 SourceOrderOrNumArrayIndices = Record[Idx++]; 7997 Indices.reserve(SourceOrderOrNumArrayIndices); 7998 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i) 7999 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx)); 8000 } 8001 8002 CXXCtorInitializer *BOMInit; 8003 if (Type == CTOR_INITIALIZER_BASE) { 8004 BOMInit = new (Context) 8005 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init, 8006 RParenLoc, MemberOrEllipsisLoc); 8007 } else if (Type == CTOR_INITIALIZER_DELEGATING) { 8008 BOMInit = new (Context) 8009 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc); 8010 } else if (IsWritten) { 8011 if (Member) 8012 BOMInit = new (Context) CXXCtorInitializer( 8013 Context, Member, MemberOrEllipsisLoc, LParenLoc, Init, RParenLoc); 8014 else 8015 BOMInit = new (Context) 8016 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc, 8017 LParenLoc, Init, RParenLoc); 8018 } else { 8019 if (IndirectMember) { 8020 assert(Indices.empty() && "Indirect field improperly initialized"); 8021 BOMInit = new (Context) 8022 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc, 8023 LParenLoc, Init, RParenLoc); 8024 } else { 8025 BOMInit = CXXCtorInitializer::Create( 8026 Context, Member, MemberOrEllipsisLoc, LParenLoc, Init, RParenLoc, 8027 Indices.data(), Indices.size()); 8028 } 8029 } 8030 8031 if (IsWritten) 8032 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices); 8033 CtorInitializers[i] = BOMInit; 8034 } 8035 8036 return CtorInitializers; 8037 } 8038 8039 NestedNameSpecifier * 8040 ASTReader::ReadNestedNameSpecifier(ModuleFile &F, 8041 const RecordData &Record, unsigned &Idx) { 8042 unsigned N = Record[Idx++]; 8043 NestedNameSpecifier *NNS = nullptr, *Prev = nullptr; 8044 for (unsigned I = 0; I != N; ++I) { 8045 NestedNameSpecifier::SpecifierKind Kind 8046 = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; 8047 switch (Kind) { 8048 case NestedNameSpecifier::Identifier: { 8049 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx); 8050 NNS = NestedNameSpecifier::Create(Context, Prev, II); 8051 break; 8052 } 8053 8054 case NestedNameSpecifier::Namespace: { 8055 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx); 8056 NNS = NestedNameSpecifier::Create(Context, Prev, NS); 8057 break; 8058 } 8059 8060 case NestedNameSpecifier::NamespaceAlias: { 8061 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx); 8062 NNS = NestedNameSpecifier::Create(Context, Prev, Alias); 8063 break; 8064 } 8065 8066 case NestedNameSpecifier::TypeSpec: 8067 case NestedNameSpecifier::TypeSpecWithTemplate: { 8068 const Type *T = readType(F, Record, Idx).getTypePtrOrNull(); 8069 if (!T) 8070 return nullptr; 8071 8072 bool Template = Record[Idx++]; 8073 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T); 8074 break; 8075 } 8076 8077 case NestedNameSpecifier::Global: { 8078 NNS = NestedNameSpecifier::GlobalSpecifier(Context); 8079 // No associated value, and there can't be a prefix. 8080 break; 8081 } 8082 8083 case NestedNameSpecifier::Super: { 8084 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx); 8085 NNS = NestedNameSpecifier::SuperSpecifier(Context, RD); 8086 break; 8087 } 8088 } 8089 Prev = NNS; 8090 } 8091 return NNS; 8092 } 8093 8094 NestedNameSpecifierLoc 8095 ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record, 8096 unsigned &Idx) { 8097 unsigned N = Record[Idx++]; 8098 NestedNameSpecifierLocBuilder Builder; 8099 for (unsigned I = 0; I != N; ++I) { 8100 NestedNameSpecifier::SpecifierKind Kind 8101 = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; 8102 switch (Kind) { 8103 case NestedNameSpecifier::Identifier: { 8104 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx); 8105 SourceRange Range = ReadSourceRange(F, Record, Idx); 8106 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd()); 8107 break; 8108 } 8109 8110 case NestedNameSpecifier::Namespace: { 8111 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx); 8112 SourceRange Range = ReadSourceRange(F, Record, Idx); 8113 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd()); 8114 break; 8115 } 8116 8117 case NestedNameSpecifier::NamespaceAlias: { 8118 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx); 8119 SourceRange Range = ReadSourceRange(F, Record, Idx); 8120 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd()); 8121 break; 8122 } 8123 8124 case NestedNameSpecifier::TypeSpec: 8125 case NestedNameSpecifier::TypeSpecWithTemplate: { 8126 bool Template = Record[Idx++]; 8127 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx); 8128 if (!T) 8129 return NestedNameSpecifierLoc(); 8130 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx); 8131 8132 // FIXME: 'template' keyword location not saved anywhere, so we fake it. 8133 Builder.Extend(Context, 8134 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(), 8135 T->getTypeLoc(), ColonColonLoc); 8136 break; 8137 } 8138 8139 case NestedNameSpecifier::Global: { 8140 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx); 8141 Builder.MakeGlobal(Context, ColonColonLoc); 8142 break; 8143 } 8144 8145 case NestedNameSpecifier::Super: { 8146 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx); 8147 SourceRange Range = ReadSourceRange(F, Record, Idx); 8148 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd()); 8149 break; 8150 } 8151 } 8152 } 8153 8154 return Builder.getWithLocInContext(Context); 8155 } 8156 8157 SourceRange 8158 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record, 8159 unsigned &Idx) { 8160 SourceLocation beg = ReadSourceLocation(F, Record, Idx); 8161 SourceLocation end = ReadSourceLocation(F, Record, Idx); 8162 return SourceRange(beg, end); 8163 } 8164 8165 /// \brief Read an integral value 8166 llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) { 8167 unsigned BitWidth = Record[Idx++]; 8168 unsigned NumWords = llvm::APInt::getNumWords(BitWidth); 8169 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]); 8170 Idx += NumWords; 8171 return Result; 8172 } 8173 8174 /// \brief Read a signed integral value 8175 llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) { 8176 bool isUnsigned = Record[Idx++]; 8177 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned); 8178 } 8179 8180 /// \brief Read a floating-point value 8181 llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, 8182 const llvm::fltSemantics &Sem, 8183 unsigned &Idx) { 8184 return llvm::APFloat(Sem, ReadAPInt(Record, Idx)); 8185 } 8186 8187 // \brief Read a string 8188 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) { 8189 unsigned Len = Record[Idx++]; 8190 std::string Result(Record.data() + Idx, Record.data() + Idx + Len); 8191 Idx += Len; 8192 return Result; 8193 } 8194 8195 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record, 8196 unsigned &Idx) { 8197 std::string Filename = ReadString(Record, Idx); 8198 ResolveImportedPath(F, Filename); 8199 return Filename; 8200 } 8201 8202 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record, 8203 unsigned &Idx) { 8204 unsigned Major = Record[Idx++]; 8205 unsigned Minor = Record[Idx++]; 8206 unsigned Subminor = Record[Idx++]; 8207 if (Minor == 0) 8208 return VersionTuple(Major); 8209 if (Subminor == 0) 8210 return VersionTuple(Major, Minor - 1); 8211 return VersionTuple(Major, Minor - 1, Subminor - 1); 8212 } 8213 8214 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F, 8215 const RecordData &Record, 8216 unsigned &Idx) { 8217 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx); 8218 return CXXTemporary::Create(Context, Decl); 8219 } 8220 8221 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) { 8222 return Diag(CurrentImportLoc, DiagID); 8223 } 8224 8225 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) { 8226 return Diags.Report(Loc, DiagID); 8227 } 8228 8229 /// \brief Retrieve the identifier table associated with the 8230 /// preprocessor. 8231 IdentifierTable &ASTReader::getIdentifierTable() { 8232 return PP.getIdentifierTable(); 8233 } 8234 8235 /// \brief Record that the given ID maps to the given switch-case 8236 /// statement. 8237 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { 8238 assert((*CurrSwitchCaseStmts)[ID] == nullptr && 8239 "Already have a SwitchCase with this ID"); 8240 (*CurrSwitchCaseStmts)[ID] = SC; 8241 } 8242 8243 /// \brief Retrieve the switch-case statement with the given ID. 8244 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) { 8245 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID"); 8246 return (*CurrSwitchCaseStmts)[ID]; 8247 } 8248 8249 void ASTReader::ClearSwitchCaseIDs() { 8250 CurrSwitchCaseStmts->clear(); 8251 } 8252 8253 void ASTReader::ReadComments() { 8254 std::vector<RawComment *> Comments; 8255 for (SmallVectorImpl<std::pair<BitstreamCursor, 8256 serialization::ModuleFile *> >::iterator 8257 I = CommentsCursors.begin(), 8258 E = CommentsCursors.end(); 8259 I != E; ++I) { 8260 Comments.clear(); 8261 BitstreamCursor &Cursor = I->first; 8262 serialization::ModuleFile &F = *I->second; 8263 SavedStreamPosition SavedPosition(Cursor); 8264 8265 RecordData Record; 8266 while (true) { 8267 llvm::BitstreamEntry Entry = 8268 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd); 8269 8270 switch (Entry.Kind) { 8271 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 8272 case llvm::BitstreamEntry::Error: 8273 Error("malformed block record in AST file"); 8274 return; 8275 case llvm::BitstreamEntry::EndBlock: 8276 goto NextCursor; 8277 case llvm::BitstreamEntry::Record: 8278 // The interesting case. 8279 break; 8280 } 8281 8282 // Read a record. 8283 Record.clear(); 8284 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) { 8285 case COMMENTS_RAW_COMMENT: { 8286 unsigned Idx = 0; 8287 SourceRange SR = ReadSourceRange(F, Record, Idx); 8288 RawComment::CommentKind Kind = 8289 (RawComment::CommentKind) Record[Idx++]; 8290 bool IsTrailingComment = Record[Idx++]; 8291 bool IsAlmostTrailingComment = Record[Idx++]; 8292 Comments.push_back(new (Context) RawComment( 8293 SR, Kind, IsTrailingComment, IsAlmostTrailingComment, 8294 Context.getLangOpts().CommentOpts.ParseAllComments)); 8295 break; 8296 } 8297 } 8298 } 8299 NextCursor: 8300 Context.Comments.addDeserializedComments(Comments); 8301 } 8302 } 8303 8304 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) { 8305 // If we know the owning module, use it. 8306 if (Module *M = D->getImportedOwningModule()) 8307 return M->getFullModuleName(); 8308 8309 // Otherwise, use the name of the top-level module the decl is within. 8310 if (ModuleFile *M = getOwningModuleFile(D)) 8311 return M->ModuleName; 8312 8313 // Not from a module. 8314 return ""; 8315 } 8316 8317 void ASTReader::finishPendingActions() { 8318 while (!PendingIdentifierInfos.empty() || 8319 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() || 8320 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() || 8321 !PendingUpdateRecords.empty()) { 8322 // If any identifiers with corresponding top-level declarations have 8323 // been loaded, load those declarations now. 8324 typedef llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> > 8325 TopLevelDeclsMap; 8326 TopLevelDeclsMap TopLevelDecls; 8327 8328 while (!PendingIdentifierInfos.empty()) { 8329 IdentifierInfo *II = PendingIdentifierInfos.back().first; 8330 SmallVector<uint32_t, 4> DeclIDs = 8331 std::move(PendingIdentifierInfos.back().second); 8332 PendingIdentifierInfos.pop_back(); 8333 8334 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]); 8335 } 8336 8337 // For each decl chain that we wanted to complete while deserializing, mark 8338 // it as "still needs to be completed". 8339 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) { 8340 markIncompleteDeclChain(PendingIncompleteDeclChains[I]); 8341 } 8342 PendingIncompleteDeclChains.clear(); 8343 8344 // Load pending declaration chains. 8345 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) 8346 loadPendingDeclChain(PendingDeclChains[I].first, PendingDeclChains[I].second); 8347 PendingDeclChains.clear(); 8348 8349 // Make the most recent of the top-level declarations visible. 8350 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(), 8351 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) { 8352 IdentifierInfo *II = TLD->first; 8353 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) { 8354 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II); 8355 } 8356 } 8357 8358 // Load any pending macro definitions. 8359 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) { 8360 IdentifierInfo *II = PendingMacroIDs.begin()[I].first; 8361 SmallVector<PendingMacroInfo, 2> GlobalIDs; 8362 GlobalIDs.swap(PendingMacroIDs.begin()[I].second); 8363 // Initialize the macro history from chained-PCHs ahead of module imports. 8364 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 8365 ++IDIdx) { 8366 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 8367 if (Info.M->Kind != MK_ImplicitModule && 8368 Info.M->Kind != MK_ExplicitModule) 8369 resolvePendingMacro(II, Info); 8370 } 8371 // Handle module imports. 8372 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 8373 ++IDIdx) { 8374 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 8375 if (Info.M->Kind == MK_ImplicitModule || 8376 Info.M->Kind == MK_ExplicitModule) 8377 resolvePendingMacro(II, Info); 8378 } 8379 } 8380 PendingMacroIDs.clear(); 8381 8382 // Wire up the DeclContexts for Decls that we delayed setting until 8383 // recursive loading is completed. 8384 while (!PendingDeclContextInfos.empty()) { 8385 PendingDeclContextInfo Info = PendingDeclContextInfos.front(); 8386 PendingDeclContextInfos.pop_front(); 8387 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC)); 8388 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC)); 8389 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext()); 8390 } 8391 8392 // Perform any pending declaration updates. 8393 while (!PendingUpdateRecords.empty()) { 8394 auto Update = PendingUpdateRecords.pop_back_val(); 8395 ReadingKindTracker ReadingKind(Read_Decl, *this); 8396 loadDeclUpdateRecords(Update.first, Update.second); 8397 } 8398 } 8399 8400 // At this point, all update records for loaded decls are in place, so any 8401 // fake class definitions should have become real. 8402 assert(PendingFakeDefinitionData.empty() && 8403 "faked up a class definition but never saw the real one"); 8404 8405 // If we deserialized any C++ or Objective-C class definitions, any 8406 // Objective-C protocol definitions, or any redeclarable templates, make sure 8407 // that all redeclarations point to the definitions. Note that this can only 8408 // happen now, after the redeclaration chains have been fully wired. 8409 for (Decl *D : PendingDefinitions) { 8410 if (TagDecl *TD = dyn_cast<TagDecl>(D)) { 8411 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) { 8412 // Make sure that the TagType points at the definition. 8413 const_cast<TagType*>(TagT)->decl = TD; 8414 } 8415 8416 if (auto RD = dyn_cast<CXXRecordDecl>(D)) { 8417 for (auto *R = getMostRecentExistingDecl(RD); R; 8418 R = R->getPreviousDecl()) { 8419 assert((R == D) == 8420 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() && 8421 "declaration thinks it's the definition but it isn't"); 8422 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData; 8423 } 8424 } 8425 8426 continue; 8427 } 8428 8429 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) { 8430 // Make sure that the ObjCInterfaceType points at the definition. 8431 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl)) 8432 ->Decl = ID; 8433 8434 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl()) 8435 cast<ObjCInterfaceDecl>(R)->Data = ID->Data; 8436 8437 continue; 8438 } 8439 8440 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) { 8441 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl()) 8442 cast<ObjCProtocolDecl>(R)->Data = PD->Data; 8443 8444 continue; 8445 } 8446 8447 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl(); 8448 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl()) 8449 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common; 8450 } 8451 PendingDefinitions.clear(); 8452 8453 // Load the bodies of any functions or methods we've encountered. We do 8454 // this now (delayed) so that we can be sure that the declaration chains 8455 // have been fully wired up (hasBody relies on this). 8456 // FIXME: We shouldn't require complete redeclaration chains here. 8457 for (PendingBodiesMap::iterator PB = PendingBodies.begin(), 8458 PBEnd = PendingBodies.end(); 8459 PB != PBEnd; ++PB) { 8460 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) { 8461 // FIXME: Check for =delete/=default? 8462 // FIXME: Complain about ODR violations here? 8463 if (!getContext().getLangOpts().Modules || !FD->hasBody()) 8464 FD->setLazyBody(PB->second); 8465 continue; 8466 } 8467 8468 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first); 8469 if (!getContext().getLangOpts().Modules || !MD->hasBody()) 8470 MD->setLazyBody(PB->second); 8471 } 8472 PendingBodies.clear(); 8473 8474 // Do some cleanup. 8475 for (auto *ND : PendingMergedDefinitionsToDeduplicate) 8476 getContext().deduplicateMergedDefinitonsFor(ND); 8477 PendingMergedDefinitionsToDeduplicate.clear(); 8478 } 8479 8480 void ASTReader::diagnoseOdrViolations() { 8481 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty()) 8482 return; 8483 8484 // Trigger the import of the full definition of each class that had any 8485 // odr-merging problems, so we can produce better diagnostics for them. 8486 // These updates may in turn find and diagnose some ODR failures, so take 8487 // ownership of the set first. 8488 auto OdrMergeFailures = std::move(PendingOdrMergeFailures); 8489 PendingOdrMergeFailures.clear(); 8490 for (auto &Merge : OdrMergeFailures) { 8491 Merge.first->buildLookup(); 8492 Merge.first->decls_begin(); 8493 Merge.first->bases_begin(); 8494 Merge.first->vbases_begin(); 8495 for (auto *RD : Merge.second) { 8496 RD->decls_begin(); 8497 RD->bases_begin(); 8498 RD->vbases_begin(); 8499 } 8500 } 8501 8502 // For each declaration from a merged context, check that the canonical 8503 // definition of that context also contains a declaration of the same 8504 // entity. 8505 // 8506 // Caution: this loop does things that might invalidate iterators into 8507 // PendingOdrMergeChecks. Don't turn this into a range-based for loop! 8508 while (!PendingOdrMergeChecks.empty()) { 8509 NamedDecl *D = PendingOdrMergeChecks.pop_back_val(); 8510 8511 // FIXME: Skip over implicit declarations for now. This matters for things 8512 // like implicitly-declared special member functions. This isn't entirely 8513 // correct; we can end up with multiple unmerged declarations of the same 8514 // implicit entity. 8515 if (D->isImplicit()) 8516 continue; 8517 8518 DeclContext *CanonDef = D->getDeclContext(); 8519 8520 bool Found = false; 8521 const Decl *DCanon = D->getCanonicalDecl(); 8522 8523 for (auto RI : D->redecls()) { 8524 if (RI->getLexicalDeclContext() == CanonDef) { 8525 Found = true; 8526 break; 8527 } 8528 } 8529 if (Found) 8530 continue; 8531 8532 // Quick check failed, time to do the slow thing. Note, we can't just 8533 // look up the name of D in CanonDef here, because the member that is 8534 // in CanonDef might not be found by name lookup (it might have been 8535 // replaced by a more recent declaration in the lookup table), and we 8536 // can't necessarily find it in the redeclaration chain because it might 8537 // be merely mergeable, not redeclarable. 8538 llvm::SmallVector<const NamedDecl*, 4> Candidates; 8539 for (auto *CanonMember : CanonDef->decls()) { 8540 if (CanonMember->getCanonicalDecl() == DCanon) { 8541 // This can happen if the declaration is merely mergeable and not 8542 // actually redeclarable (we looked for redeclarations earlier). 8543 // 8544 // FIXME: We should be able to detect this more efficiently, without 8545 // pulling in all of the members of CanonDef. 8546 Found = true; 8547 break; 8548 } 8549 if (auto *ND = dyn_cast<NamedDecl>(CanonMember)) 8550 if (ND->getDeclName() == D->getDeclName()) 8551 Candidates.push_back(ND); 8552 } 8553 8554 if (!Found) { 8555 // The AST doesn't like TagDecls becoming invalid after they've been 8556 // completed. We only really need to mark FieldDecls as invalid here. 8557 if (!isa<TagDecl>(D)) 8558 D->setInvalidDecl(); 8559 8560 // Ensure we don't accidentally recursively enter deserialization while 8561 // we're producing our diagnostic. 8562 Deserializing RecursionGuard(this); 8563 8564 std::string CanonDefModule = 8565 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef)); 8566 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl) 8567 << D << getOwningModuleNameForDiagnostic(D) 8568 << CanonDef << CanonDefModule.empty() << CanonDefModule; 8569 8570 if (Candidates.empty()) 8571 Diag(cast<Decl>(CanonDef)->getLocation(), 8572 diag::note_module_odr_violation_no_possible_decls) << D; 8573 else { 8574 for (unsigned I = 0, N = Candidates.size(); I != N; ++I) 8575 Diag(Candidates[I]->getLocation(), 8576 diag::note_module_odr_violation_possible_decl) 8577 << Candidates[I]; 8578 } 8579 8580 DiagnosedOdrMergeFailures.insert(CanonDef); 8581 } 8582 } 8583 8584 if (OdrMergeFailures.empty()) 8585 return; 8586 8587 // Ensure we don't accidentally recursively enter deserialization while 8588 // we're producing our diagnostics. 8589 Deserializing RecursionGuard(this); 8590 8591 // Issue any pending ODR-failure diagnostics. 8592 for (auto &Merge : OdrMergeFailures) { 8593 // If we've already pointed out a specific problem with this class, don't 8594 // bother issuing a general "something's different" diagnostic. 8595 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 8596 continue; 8597 8598 bool Diagnosed = false; 8599 for (auto *RD : Merge.second) { 8600 // Multiple different declarations got merged together; tell the user 8601 // where they came from. 8602 if (Merge.first != RD) { 8603 // FIXME: Walk the definition, figure out what's different, 8604 // and diagnose that. 8605 if (!Diagnosed) { 8606 std::string Module = getOwningModuleNameForDiagnostic(Merge.first); 8607 Diag(Merge.first->getLocation(), 8608 diag::err_module_odr_violation_different_definitions) 8609 << Merge.first << Module.empty() << Module; 8610 Diagnosed = true; 8611 } 8612 8613 Diag(RD->getLocation(), 8614 diag::note_module_odr_violation_different_definitions) 8615 << getOwningModuleNameForDiagnostic(RD); 8616 } 8617 } 8618 8619 if (!Diagnosed) { 8620 // All definitions are updates to the same declaration. This happens if a 8621 // module instantiates the declaration of a class template specialization 8622 // and two or more other modules instantiate its definition. 8623 // 8624 // FIXME: Indicate which modules had instantiations of this definition. 8625 // FIXME: How can this even happen? 8626 Diag(Merge.first->getLocation(), 8627 diag::err_module_odr_violation_different_instantiations) 8628 << Merge.first; 8629 } 8630 } 8631 } 8632 8633 void ASTReader::StartedDeserializing() { 8634 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get()) 8635 ReadTimer->startTimer(); 8636 } 8637 8638 void ASTReader::FinishedDeserializing() { 8639 assert(NumCurrentElementsDeserializing && 8640 "FinishedDeserializing not paired with StartedDeserializing"); 8641 if (NumCurrentElementsDeserializing == 1) { 8642 // We decrease NumCurrentElementsDeserializing only after pending actions 8643 // are finished, to avoid recursively re-calling finishPendingActions(). 8644 finishPendingActions(); 8645 } 8646 --NumCurrentElementsDeserializing; 8647 8648 if (NumCurrentElementsDeserializing == 0) { 8649 // Propagate exception specification updates along redeclaration chains. 8650 while (!PendingExceptionSpecUpdates.empty()) { 8651 auto Updates = std::move(PendingExceptionSpecUpdates); 8652 PendingExceptionSpecUpdates.clear(); 8653 for (auto Update : Updates) { 8654 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 8655 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>(); 8656 auto ESI = FPT->getExtProtoInfo().ExceptionSpec; 8657 if (auto *Listener = Context.getASTMutationListener()) 8658 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second)); 8659 for (auto *Redecl : Update.second->redecls()) 8660 Context.adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI); 8661 } 8662 } 8663 8664 if (ReadTimer) 8665 ReadTimer->stopTimer(); 8666 8667 diagnoseOdrViolations(); 8668 8669 // We are not in recursive loading, so it's safe to pass the "interesting" 8670 // decls to the consumer. 8671 if (Consumer) 8672 PassInterestingDeclsToConsumer(); 8673 } 8674 } 8675 8676 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) { 8677 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) { 8678 // Remove any fake results before adding any real ones. 8679 auto It = PendingFakeLookupResults.find(II); 8680 if (It != PendingFakeLookupResults.end()) { 8681 for (auto *ND : It->second) 8682 SemaObj->IdResolver.RemoveDecl(ND); 8683 // FIXME: this works around module+PCH performance issue. 8684 // Rather than erase the result from the map, which is O(n), just clear 8685 // the vector of NamedDecls. 8686 It->second.clear(); 8687 } 8688 } 8689 8690 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) { 8691 SemaObj->TUScope->AddDecl(D); 8692 } else if (SemaObj->TUScope) { 8693 // Adding the decl to IdResolver may have failed because it was already in 8694 // (even though it was not added in scope). If it is already in, make sure 8695 // it gets in the scope as well. 8696 if (std::find(SemaObj->IdResolver.begin(Name), 8697 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end()) 8698 SemaObj->TUScope->AddDecl(D); 8699 } 8700 } 8701 8702 ASTReader::ASTReader( 8703 Preprocessor &PP, ASTContext &Context, 8704 const PCHContainerReader &PCHContainerRdr, 8705 ArrayRef<IntrusiveRefCntPtr<ModuleFileExtension>> Extensions, 8706 StringRef isysroot, bool DisableValidation, 8707 bool AllowASTWithCompilerErrors, 8708 bool AllowConfigurationMismatch, bool ValidateSystemInputs, 8709 bool UseGlobalIndex, 8710 std::unique_ptr<llvm::Timer> ReadTimer) 8711 : Listener(new PCHValidator(PP, *this)), DeserializationListener(nullptr), 8712 OwnsDeserializationListener(false), SourceMgr(PP.getSourceManager()), 8713 FileMgr(PP.getFileManager()), PCHContainerRdr(PCHContainerRdr), 8714 Diags(PP.getDiagnostics()), SemaObj(nullptr), PP(PP), Context(Context), 8715 Consumer(nullptr), ModuleMgr(PP.getFileManager(), PCHContainerRdr), 8716 DummyIdResolver(PP), 8717 ReadTimer(std::move(ReadTimer)), 8718 PragmaMSStructState(-1), 8719 PragmaMSPointersToMembersState(-1), 8720 isysroot(isysroot), DisableValidation(DisableValidation), 8721 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors), 8722 AllowConfigurationMismatch(AllowConfigurationMismatch), 8723 ValidateSystemInputs(ValidateSystemInputs), 8724 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false), 8725 ProcessingUpdateRecords(false), 8726 CurrSwitchCaseStmts(&SwitchCaseStmts), NumSLocEntriesRead(0), 8727 TotalNumSLocEntries(0), NumStatementsRead(0), TotalNumStatements(0), 8728 NumMacrosRead(0), TotalNumMacros(0), NumIdentifierLookups(0), 8729 NumIdentifierLookupHits(0), NumSelectorsRead(0), 8730 NumMethodPoolEntriesRead(0), NumMethodPoolLookups(0), 8731 NumMethodPoolHits(0), NumMethodPoolTableLookups(0), 8732 NumMethodPoolTableHits(0), TotalNumMethodPoolEntries(0), 8733 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0), 8734 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0), 8735 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0), 8736 PassingDeclsToConsumer(false), ReadingKind(Read_None) { 8737 SourceMgr.setExternalSLocEntrySource(this); 8738 8739 for (const auto &Ext : Extensions) { 8740 auto BlockName = Ext->getExtensionMetadata().BlockName; 8741 auto Known = ModuleFileExtensions.find(BlockName); 8742 if (Known != ModuleFileExtensions.end()) { 8743 Diags.Report(diag::warn_duplicate_module_file_extension) 8744 << BlockName; 8745 continue; 8746 } 8747 8748 ModuleFileExtensions.insert({BlockName, Ext}); 8749 } 8750 } 8751 8752 ASTReader::~ASTReader() { 8753 if (OwnsDeserializationListener) 8754 delete DeserializationListener; 8755 } 8756 8757 IdentifierResolver &ASTReader::getIdResolver() { 8758 return SemaObj ? SemaObj->IdResolver : DummyIdResolver; 8759 } 8760