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