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