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