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