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