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 void ASTReader::ReadTypeLoc(ModuleFile &F, const ASTReader::RecordData &Record, 6737 unsigned &Idx, TypeLoc TL) { 6738 TypeLocReader TLR(F, *this, Record, Idx); 6739 for (; !TL.isNull(); TL = TL.getNextTypeLoc()) 6740 TLR.Visit(TL); 6741 } 6742 6743 TypeSourceInfo * 6744 ASTReader::GetTypeSourceInfo(ModuleFile &F, const ASTReader::RecordData &Record, 6745 unsigned &Idx) { 6746 QualType InfoTy = readType(F, Record, Idx); 6747 if (InfoTy.isNull()) 6748 return nullptr; 6749 6750 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy); 6751 ReadTypeLoc(F, Record, Idx, TInfo->getTypeLoc()); 6752 return TInfo; 6753 } 6754 6755 QualType ASTReader::GetType(TypeID ID) { 6756 assert(ContextObj && "reading type with no AST context"); 6757 ASTContext &Context = *ContextObj; 6758 6759 unsigned FastQuals = ID & Qualifiers::FastMask; 6760 unsigned Index = ID >> Qualifiers::FastWidth; 6761 6762 if (Index < NUM_PREDEF_TYPE_IDS) { 6763 QualType T; 6764 switch ((PredefinedTypeIDs)Index) { 6765 case PREDEF_TYPE_NULL_ID: 6766 return QualType(); 6767 case PREDEF_TYPE_VOID_ID: 6768 T = Context.VoidTy; 6769 break; 6770 case PREDEF_TYPE_BOOL_ID: 6771 T = Context.BoolTy; 6772 break; 6773 case PREDEF_TYPE_CHAR_U_ID: 6774 case PREDEF_TYPE_CHAR_S_ID: 6775 // FIXME: Check that the signedness of CharTy is correct! 6776 T = Context.CharTy; 6777 break; 6778 case PREDEF_TYPE_UCHAR_ID: 6779 T = Context.UnsignedCharTy; 6780 break; 6781 case PREDEF_TYPE_USHORT_ID: 6782 T = Context.UnsignedShortTy; 6783 break; 6784 case PREDEF_TYPE_UINT_ID: 6785 T = Context.UnsignedIntTy; 6786 break; 6787 case PREDEF_TYPE_ULONG_ID: 6788 T = Context.UnsignedLongTy; 6789 break; 6790 case PREDEF_TYPE_ULONGLONG_ID: 6791 T = Context.UnsignedLongLongTy; 6792 break; 6793 case PREDEF_TYPE_UINT128_ID: 6794 T = Context.UnsignedInt128Ty; 6795 break; 6796 case PREDEF_TYPE_SCHAR_ID: 6797 T = Context.SignedCharTy; 6798 break; 6799 case PREDEF_TYPE_WCHAR_ID: 6800 T = Context.WCharTy; 6801 break; 6802 case PREDEF_TYPE_SHORT_ID: 6803 T = Context.ShortTy; 6804 break; 6805 case PREDEF_TYPE_INT_ID: 6806 T = Context.IntTy; 6807 break; 6808 case PREDEF_TYPE_LONG_ID: 6809 T = Context.LongTy; 6810 break; 6811 case PREDEF_TYPE_LONGLONG_ID: 6812 T = Context.LongLongTy; 6813 break; 6814 case PREDEF_TYPE_INT128_ID: 6815 T = Context.Int128Ty; 6816 break; 6817 case PREDEF_TYPE_HALF_ID: 6818 T = Context.HalfTy; 6819 break; 6820 case PREDEF_TYPE_FLOAT_ID: 6821 T = Context.FloatTy; 6822 break; 6823 case PREDEF_TYPE_DOUBLE_ID: 6824 T = Context.DoubleTy; 6825 break; 6826 case PREDEF_TYPE_LONGDOUBLE_ID: 6827 T = Context.LongDoubleTy; 6828 break; 6829 case PREDEF_TYPE_SHORT_ACCUM_ID: 6830 T = Context.ShortAccumTy; 6831 break; 6832 case PREDEF_TYPE_ACCUM_ID: 6833 T = Context.AccumTy; 6834 break; 6835 case PREDEF_TYPE_LONG_ACCUM_ID: 6836 T = Context.LongAccumTy; 6837 break; 6838 case PREDEF_TYPE_USHORT_ACCUM_ID: 6839 T = Context.UnsignedShortAccumTy; 6840 break; 6841 case PREDEF_TYPE_UACCUM_ID: 6842 T = Context.UnsignedAccumTy; 6843 break; 6844 case PREDEF_TYPE_ULONG_ACCUM_ID: 6845 T = Context.UnsignedLongAccumTy; 6846 break; 6847 case PREDEF_TYPE_SHORT_FRACT_ID: 6848 T = Context.ShortFractTy; 6849 break; 6850 case PREDEF_TYPE_FRACT_ID: 6851 T = Context.FractTy; 6852 break; 6853 case PREDEF_TYPE_LONG_FRACT_ID: 6854 T = Context.LongFractTy; 6855 break; 6856 case PREDEF_TYPE_USHORT_FRACT_ID: 6857 T = Context.UnsignedShortFractTy; 6858 break; 6859 case PREDEF_TYPE_UFRACT_ID: 6860 T = Context.UnsignedFractTy; 6861 break; 6862 case PREDEF_TYPE_ULONG_FRACT_ID: 6863 T = Context.UnsignedLongFractTy; 6864 break; 6865 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID: 6866 T = Context.SatShortAccumTy; 6867 break; 6868 case PREDEF_TYPE_SAT_ACCUM_ID: 6869 T = Context.SatAccumTy; 6870 break; 6871 case PREDEF_TYPE_SAT_LONG_ACCUM_ID: 6872 T = Context.SatLongAccumTy; 6873 break; 6874 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID: 6875 T = Context.SatUnsignedShortAccumTy; 6876 break; 6877 case PREDEF_TYPE_SAT_UACCUM_ID: 6878 T = Context.SatUnsignedAccumTy; 6879 break; 6880 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID: 6881 T = Context.SatUnsignedLongAccumTy; 6882 break; 6883 case PREDEF_TYPE_SAT_SHORT_FRACT_ID: 6884 T = Context.SatShortFractTy; 6885 break; 6886 case PREDEF_TYPE_SAT_FRACT_ID: 6887 T = Context.SatFractTy; 6888 break; 6889 case PREDEF_TYPE_SAT_LONG_FRACT_ID: 6890 T = Context.SatLongFractTy; 6891 break; 6892 case PREDEF_TYPE_SAT_USHORT_FRACT_ID: 6893 T = Context.SatUnsignedShortFractTy; 6894 break; 6895 case PREDEF_TYPE_SAT_UFRACT_ID: 6896 T = Context.SatUnsignedFractTy; 6897 break; 6898 case PREDEF_TYPE_SAT_ULONG_FRACT_ID: 6899 T = Context.SatUnsignedLongFractTy; 6900 break; 6901 case PREDEF_TYPE_FLOAT16_ID: 6902 T = Context.Float16Ty; 6903 break; 6904 case PREDEF_TYPE_FLOAT128_ID: 6905 T = Context.Float128Ty; 6906 break; 6907 case PREDEF_TYPE_OVERLOAD_ID: 6908 T = Context.OverloadTy; 6909 break; 6910 case PREDEF_TYPE_BOUND_MEMBER: 6911 T = Context.BoundMemberTy; 6912 break; 6913 case PREDEF_TYPE_PSEUDO_OBJECT: 6914 T = Context.PseudoObjectTy; 6915 break; 6916 case PREDEF_TYPE_DEPENDENT_ID: 6917 T = Context.DependentTy; 6918 break; 6919 case PREDEF_TYPE_UNKNOWN_ANY: 6920 T = Context.UnknownAnyTy; 6921 break; 6922 case PREDEF_TYPE_NULLPTR_ID: 6923 T = Context.NullPtrTy; 6924 break; 6925 case PREDEF_TYPE_CHAR8_ID: 6926 T = Context.Char8Ty; 6927 break; 6928 case PREDEF_TYPE_CHAR16_ID: 6929 T = Context.Char16Ty; 6930 break; 6931 case PREDEF_TYPE_CHAR32_ID: 6932 T = Context.Char32Ty; 6933 break; 6934 case PREDEF_TYPE_OBJC_ID: 6935 T = Context.ObjCBuiltinIdTy; 6936 break; 6937 case PREDEF_TYPE_OBJC_CLASS: 6938 T = Context.ObjCBuiltinClassTy; 6939 break; 6940 case PREDEF_TYPE_OBJC_SEL: 6941 T = Context.ObjCBuiltinSelTy; 6942 break; 6943 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 6944 case PREDEF_TYPE_##Id##_ID: \ 6945 T = Context.SingletonId; \ 6946 break; 6947 #include "clang/Basic/OpenCLImageTypes.def" 6948 case PREDEF_TYPE_SAMPLER_ID: 6949 T = Context.OCLSamplerTy; 6950 break; 6951 case PREDEF_TYPE_EVENT_ID: 6952 T = Context.OCLEventTy; 6953 break; 6954 case PREDEF_TYPE_CLK_EVENT_ID: 6955 T = Context.OCLClkEventTy; 6956 break; 6957 case PREDEF_TYPE_QUEUE_ID: 6958 T = Context.OCLQueueTy; 6959 break; 6960 case PREDEF_TYPE_RESERVE_ID_ID: 6961 T = Context.OCLReserveIDTy; 6962 break; 6963 case PREDEF_TYPE_AUTO_DEDUCT: 6964 T = Context.getAutoDeductType(); 6965 break; 6966 case PREDEF_TYPE_AUTO_RREF_DEDUCT: 6967 T = Context.getAutoRRefDeductType(); 6968 break; 6969 case PREDEF_TYPE_ARC_UNBRIDGED_CAST: 6970 T = Context.ARCUnbridgedCastTy; 6971 break; 6972 case PREDEF_TYPE_BUILTIN_FN: 6973 T = Context.BuiltinFnTy; 6974 break; 6975 case PREDEF_TYPE_OMP_ARRAY_SECTION: 6976 T = Context.OMPArraySectionTy; 6977 break; 6978 } 6979 6980 assert(!T.isNull() && "Unknown predefined type"); 6981 return T.withFastQualifiers(FastQuals); 6982 } 6983 6984 Index -= NUM_PREDEF_TYPE_IDS; 6985 assert(Index < TypesLoaded.size() && "Type index out-of-range"); 6986 if (TypesLoaded[Index].isNull()) { 6987 TypesLoaded[Index] = readTypeRecord(Index); 6988 if (TypesLoaded[Index].isNull()) 6989 return QualType(); 6990 6991 TypesLoaded[Index]->setFromAST(); 6992 if (DeserializationListener) 6993 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID), 6994 TypesLoaded[Index]); 6995 } 6996 6997 return TypesLoaded[Index].withFastQualifiers(FastQuals); 6998 } 6999 7000 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) { 7001 return GetType(getGlobalTypeID(F, LocalID)); 7002 } 7003 7004 serialization::TypeID 7005 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const { 7006 unsigned FastQuals = LocalID & Qualifiers::FastMask; 7007 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth; 7008 7009 if (LocalIndex < NUM_PREDEF_TYPE_IDS) 7010 return LocalID; 7011 7012 if (!F.ModuleOffsetMap.empty()) 7013 ReadModuleOffsetMap(F); 7014 7015 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7016 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS); 7017 assert(I != F.TypeRemap.end() && "Invalid index into type index remap"); 7018 7019 unsigned GlobalIndex = LocalIndex + I->second; 7020 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals; 7021 } 7022 7023 TemplateArgumentLocInfo 7024 ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F, 7025 TemplateArgument::ArgKind Kind, 7026 const RecordData &Record, 7027 unsigned &Index) { 7028 switch (Kind) { 7029 case TemplateArgument::Expression: 7030 return ReadExpr(F); 7031 case TemplateArgument::Type: 7032 return GetTypeSourceInfo(F, Record, Index); 7033 case TemplateArgument::Template: { 7034 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, 7035 Index); 7036 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index); 7037 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 7038 SourceLocation()); 7039 } 7040 case TemplateArgument::TemplateExpansion: { 7041 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, 7042 Index); 7043 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index); 7044 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index); 7045 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 7046 EllipsisLoc); 7047 } 7048 case TemplateArgument::Null: 7049 case TemplateArgument::Integral: 7050 case TemplateArgument::Declaration: 7051 case TemplateArgument::NullPtr: 7052 case TemplateArgument::Pack: 7053 // FIXME: Is this right? 7054 return TemplateArgumentLocInfo(); 7055 } 7056 llvm_unreachable("unexpected template argument loc"); 7057 } 7058 7059 TemplateArgumentLoc 7060 ASTReader::ReadTemplateArgumentLoc(ModuleFile &F, 7061 const RecordData &Record, unsigned &Index) { 7062 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index); 7063 7064 if (Arg.getKind() == TemplateArgument::Expression) { 7065 if (Record[Index++]) // bool InfoHasSameExpr. 7066 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); 7067 } 7068 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(), 7069 Record, Index)); 7070 } 7071 7072 const ASTTemplateArgumentListInfo* 7073 ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F, 7074 const RecordData &Record, 7075 unsigned &Index) { 7076 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index); 7077 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index); 7078 unsigned NumArgsAsWritten = Record[Index++]; 7079 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc); 7080 for (unsigned i = 0; i != NumArgsAsWritten; ++i) 7081 TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index)); 7082 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo); 7083 } 7084 7085 Decl *ASTReader::GetExternalDecl(uint32_t ID) { 7086 return GetDecl(ID); 7087 } 7088 7089 void ASTReader::CompleteRedeclChain(const Decl *D) { 7090 if (NumCurrentElementsDeserializing) { 7091 // We arrange to not care about the complete redeclaration chain while we're 7092 // deserializing. Just remember that the AST has marked this one as complete 7093 // but that it's not actually complete yet, so we know we still need to 7094 // complete it later. 7095 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D)); 7096 return; 7097 } 7098 7099 const DeclContext *DC = D->getDeclContext()->getRedeclContext(); 7100 7101 // If this is a named declaration, complete it by looking it up 7102 // within its context. 7103 // 7104 // FIXME: Merging a function definition should merge 7105 // all mergeable entities within it. 7106 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) || 7107 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) { 7108 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) { 7109 if (!getContext().getLangOpts().CPlusPlus && 7110 isa<TranslationUnitDecl>(DC)) { 7111 // Outside of C++, we don't have a lookup table for the TU, so update 7112 // the identifier instead. (For C++ modules, we don't store decls 7113 // in the serialized identifier table, so we do the lookup in the TU.) 7114 auto *II = Name.getAsIdentifierInfo(); 7115 assert(II && "non-identifier name in C?"); 7116 if (II->isOutOfDate()) 7117 updateOutOfDateIdentifier(*II); 7118 } else 7119 DC->lookup(Name); 7120 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) { 7121 // Find all declarations of this kind from the relevant context. 7122 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) { 7123 auto *DC = cast<DeclContext>(DCDecl); 7124 SmallVector<Decl*, 8> Decls; 7125 FindExternalLexicalDecls( 7126 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls); 7127 } 7128 } 7129 } 7130 7131 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) 7132 CTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7133 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) 7134 VTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7135 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 7136 if (auto *Template = FD->getPrimaryTemplate()) 7137 Template->LoadLazySpecializations(); 7138 } 7139 } 7140 7141 CXXCtorInitializer ** 7142 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) { 7143 RecordLocation Loc = getLocalBitOffset(Offset); 7144 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7145 SavedStreamPosition SavedPosition(Cursor); 7146 Cursor.JumpToBit(Loc.Offset); 7147 ReadingKindTracker ReadingKind(Read_Decl, *this); 7148 7149 RecordData Record; 7150 unsigned Code = Cursor.ReadCode(); 7151 unsigned RecCode = Cursor.readRecord(Code, Record); 7152 if (RecCode != DECL_CXX_CTOR_INITIALIZERS) { 7153 Error("malformed AST file: missing C++ ctor initializers"); 7154 return nullptr; 7155 } 7156 7157 unsigned Idx = 0; 7158 return ReadCXXCtorInitializers(*Loc.F, Record, Idx); 7159 } 7160 7161 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) { 7162 assert(ContextObj && "reading base specifiers with no AST context"); 7163 ASTContext &Context = *ContextObj; 7164 7165 RecordLocation Loc = getLocalBitOffset(Offset); 7166 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7167 SavedStreamPosition SavedPosition(Cursor); 7168 Cursor.JumpToBit(Loc.Offset); 7169 ReadingKindTracker ReadingKind(Read_Decl, *this); 7170 RecordData Record; 7171 unsigned Code = Cursor.ReadCode(); 7172 unsigned RecCode = Cursor.readRecord(Code, Record); 7173 if (RecCode != DECL_CXX_BASE_SPECIFIERS) { 7174 Error("malformed AST file: missing C++ base specifiers"); 7175 return nullptr; 7176 } 7177 7178 unsigned Idx = 0; 7179 unsigned NumBases = Record[Idx++]; 7180 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases); 7181 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases]; 7182 for (unsigned I = 0; I != NumBases; ++I) 7183 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx); 7184 return Bases; 7185 } 7186 7187 serialization::DeclID 7188 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const { 7189 if (LocalID < NUM_PREDEF_DECL_IDS) 7190 return LocalID; 7191 7192 if (!F.ModuleOffsetMap.empty()) 7193 ReadModuleOffsetMap(F); 7194 7195 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7196 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS); 7197 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap"); 7198 7199 return LocalID + I->second; 7200 } 7201 7202 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID, 7203 ModuleFile &M) const { 7204 // Predefined decls aren't from any module. 7205 if (ID < NUM_PREDEF_DECL_IDS) 7206 return false; 7207 7208 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID && 7209 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls; 7210 } 7211 7212 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) { 7213 if (!D->isFromASTFile()) 7214 return nullptr; 7215 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID()); 7216 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7217 return I->second; 7218 } 7219 7220 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) { 7221 if (ID < NUM_PREDEF_DECL_IDS) 7222 return SourceLocation(); 7223 7224 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7225 7226 if (Index > DeclsLoaded.size()) { 7227 Error("declaration ID out-of-range for AST file"); 7228 return SourceLocation(); 7229 } 7230 7231 if (Decl *D = DeclsLoaded[Index]) 7232 return D->getLocation(); 7233 7234 SourceLocation Loc; 7235 DeclCursorForID(ID, Loc); 7236 return Loc; 7237 } 7238 7239 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) { 7240 switch (ID) { 7241 case PREDEF_DECL_NULL_ID: 7242 return nullptr; 7243 7244 case PREDEF_DECL_TRANSLATION_UNIT_ID: 7245 return Context.getTranslationUnitDecl(); 7246 7247 case PREDEF_DECL_OBJC_ID_ID: 7248 return Context.getObjCIdDecl(); 7249 7250 case PREDEF_DECL_OBJC_SEL_ID: 7251 return Context.getObjCSelDecl(); 7252 7253 case PREDEF_DECL_OBJC_CLASS_ID: 7254 return Context.getObjCClassDecl(); 7255 7256 case PREDEF_DECL_OBJC_PROTOCOL_ID: 7257 return Context.getObjCProtocolDecl(); 7258 7259 case PREDEF_DECL_INT_128_ID: 7260 return Context.getInt128Decl(); 7261 7262 case PREDEF_DECL_UNSIGNED_INT_128_ID: 7263 return Context.getUInt128Decl(); 7264 7265 case PREDEF_DECL_OBJC_INSTANCETYPE_ID: 7266 return Context.getObjCInstanceTypeDecl(); 7267 7268 case PREDEF_DECL_BUILTIN_VA_LIST_ID: 7269 return Context.getBuiltinVaListDecl(); 7270 7271 case PREDEF_DECL_VA_LIST_TAG: 7272 return Context.getVaListTagDecl(); 7273 7274 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID: 7275 return Context.getBuiltinMSVaListDecl(); 7276 7277 case PREDEF_DECL_EXTERN_C_CONTEXT_ID: 7278 return Context.getExternCContextDecl(); 7279 7280 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID: 7281 return Context.getMakeIntegerSeqDecl(); 7282 7283 case PREDEF_DECL_CF_CONSTANT_STRING_ID: 7284 return Context.getCFConstantStringDecl(); 7285 7286 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID: 7287 return Context.getCFConstantStringTagDecl(); 7288 7289 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID: 7290 return Context.getTypePackElementDecl(); 7291 } 7292 llvm_unreachable("PredefinedDeclIDs unknown enum value"); 7293 } 7294 7295 Decl *ASTReader::GetExistingDecl(DeclID ID) { 7296 assert(ContextObj && "reading decl with no AST context"); 7297 if (ID < NUM_PREDEF_DECL_IDS) { 7298 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID); 7299 if (D) { 7300 // Track that we have merged the declaration with ID \p ID into the 7301 // pre-existing predefined declaration \p D. 7302 auto &Merged = KeyDecls[D->getCanonicalDecl()]; 7303 if (Merged.empty()) 7304 Merged.push_back(ID); 7305 } 7306 return D; 7307 } 7308 7309 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7310 7311 if (Index >= DeclsLoaded.size()) { 7312 assert(0 && "declaration ID out-of-range for AST file"); 7313 Error("declaration ID out-of-range for AST file"); 7314 return nullptr; 7315 } 7316 7317 return DeclsLoaded[Index]; 7318 } 7319 7320 Decl *ASTReader::GetDecl(DeclID ID) { 7321 if (ID < NUM_PREDEF_DECL_IDS) 7322 return GetExistingDecl(ID); 7323 7324 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7325 7326 if (Index >= DeclsLoaded.size()) { 7327 assert(0 && "declaration ID out-of-range for AST file"); 7328 Error("declaration ID out-of-range for AST file"); 7329 return nullptr; 7330 } 7331 7332 if (!DeclsLoaded[Index]) { 7333 ReadDeclRecord(ID); 7334 if (DeserializationListener) 7335 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); 7336 } 7337 7338 return DeclsLoaded[Index]; 7339 } 7340 7341 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M, 7342 DeclID GlobalID) { 7343 if (GlobalID < NUM_PREDEF_DECL_IDS) 7344 return GlobalID; 7345 7346 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID); 7347 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7348 ModuleFile *Owner = I->second; 7349 7350 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos 7351 = M.GlobalToLocalDeclIDs.find(Owner); 7352 if (Pos == M.GlobalToLocalDeclIDs.end()) 7353 return 0; 7354 7355 return GlobalID - Owner->BaseDeclID + Pos->second; 7356 } 7357 7358 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F, 7359 const RecordData &Record, 7360 unsigned &Idx) { 7361 if (Idx >= Record.size()) { 7362 Error("Corrupted AST file"); 7363 return 0; 7364 } 7365 7366 return getGlobalDeclID(F, Record[Idx++]); 7367 } 7368 7369 /// Resolve the offset of a statement into a statement. 7370 /// 7371 /// This operation will read a new statement from the external 7372 /// source each time it is called, and is meant to be used via a 7373 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 7374 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) { 7375 // Switch case IDs are per Decl. 7376 ClearSwitchCaseIDs(); 7377 7378 // Offset here is a global offset across the entire chain. 7379 RecordLocation Loc = getLocalBitOffset(Offset); 7380 Loc.F->DeclsCursor.JumpToBit(Loc.Offset); 7381 assert(NumCurrentElementsDeserializing == 0 && 7382 "should not be called while already deserializing"); 7383 Deserializing D(this); 7384 return ReadStmtFromStream(*Loc.F); 7385 } 7386 7387 void ASTReader::FindExternalLexicalDecls( 7388 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant, 7389 SmallVectorImpl<Decl *> &Decls) { 7390 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {}; 7391 7392 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) { 7393 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries"); 7394 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) { 7395 auto K = (Decl::Kind)+LexicalDecls[I]; 7396 if (!IsKindWeWant(K)) 7397 continue; 7398 7399 auto ID = (serialization::DeclID)+LexicalDecls[I + 1]; 7400 7401 // Don't add predefined declarations to the lexical context more 7402 // than once. 7403 if (ID < NUM_PREDEF_DECL_IDS) { 7404 if (PredefsVisited[ID]) 7405 continue; 7406 7407 PredefsVisited[ID] = true; 7408 } 7409 7410 if (Decl *D = GetLocalDecl(*M, ID)) { 7411 assert(D->getKind() == K && "wrong kind for lexical decl"); 7412 if (!DC->isDeclInLexicalTraversal(D)) 7413 Decls.push_back(D); 7414 } 7415 } 7416 }; 7417 7418 if (isa<TranslationUnitDecl>(DC)) { 7419 for (auto Lexical : TULexicalDecls) 7420 Visit(Lexical.first, Lexical.second); 7421 } else { 7422 auto I = LexicalDecls.find(DC); 7423 if (I != LexicalDecls.end()) 7424 Visit(I->second.first, I->second.second); 7425 } 7426 7427 ++NumLexicalDeclContextsRead; 7428 } 7429 7430 namespace { 7431 7432 class DeclIDComp { 7433 ASTReader &Reader; 7434 ModuleFile &Mod; 7435 7436 public: 7437 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {} 7438 7439 bool operator()(LocalDeclID L, LocalDeclID R) const { 7440 SourceLocation LHS = getLocation(L); 7441 SourceLocation RHS = getLocation(R); 7442 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7443 } 7444 7445 bool operator()(SourceLocation LHS, LocalDeclID R) const { 7446 SourceLocation RHS = getLocation(R); 7447 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7448 } 7449 7450 bool operator()(LocalDeclID L, SourceLocation RHS) const { 7451 SourceLocation LHS = getLocation(L); 7452 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7453 } 7454 7455 SourceLocation getLocation(LocalDeclID ID) const { 7456 return Reader.getSourceManager().getFileLoc( 7457 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID))); 7458 } 7459 }; 7460 7461 } // namespace 7462 7463 void ASTReader::FindFileRegionDecls(FileID File, 7464 unsigned Offset, unsigned Length, 7465 SmallVectorImpl<Decl *> &Decls) { 7466 SourceManager &SM = getSourceManager(); 7467 7468 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File); 7469 if (I == FileDeclIDs.end()) 7470 return; 7471 7472 FileDeclsInfo &DInfo = I->second; 7473 if (DInfo.Decls.empty()) 7474 return; 7475 7476 SourceLocation 7477 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset); 7478 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length); 7479 7480 DeclIDComp DIDComp(*this, *DInfo.Mod); 7481 ArrayRef<serialization::LocalDeclID>::iterator 7482 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(), 7483 BeginLoc, DIDComp); 7484 if (BeginIt != DInfo.Decls.begin()) 7485 --BeginIt; 7486 7487 // If we are pointing at a top-level decl inside an objc container, we need 7488 // to backtrack until we find it otherwise we will fail to report that the 7489 // region overlaps with an objc container. 7490 while (BeginIt != DInfo.Decls.begin() && 7491 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt)) 7492 ->isTopLevelDeclInObjCContainer()) 7493 --BeginIt; 7494 7495 ArrayRef<serialization::LocalDeclID>::iterator 7496 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(), 7497 EndLoc, DIDComp); 7498 if (EndIt != DInfo.Decls.end()) 7499 ++EndIt; 7500 7501 for (ArrayRef<serialization::LocalDeclID>::iterator 7502 DIt = BeginIt; DIt != EndIt; ++DIt) 7503 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt))); 7504 } 7505 7506 bool 7507 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, 7508 DeclarationName Name) { 7509 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() && 7510 "DeclContext has no visible decls in storage"); 7511 if (!Name) 7512 return false; 7513 7514 auto It = Lookups.find(DC); 7515 if (It == Lookups.end()) 7516 return false; 7517 7518 Deserializing LookupResults(this); 7519 7520 // Load the list of declarations. 7521 SmallVector<NamedDecl *, 64> Decls; 7522 for (DeclID ID : It->second.Table.find(Name)) { 7523 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7524 if (ND->getDeclName() == Name) 7525 Decls.push_back(ND); 7526 } 7527 7528 ++NumVisibleDeclContextsRead; 7529 SetExternalVisibleDeclsForName(DC, Name, Decls); 7530 return !Decls.empty(); 7531 } 7532 7533 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) { 7534 if (!DC->hasExternalVisibleStorage()) 7535 return; 7536 7537 auto It = Lookups.find(DC); 7538 assert(It != Lookups.end() && 7539 "have external visible storage but no lookup tables"); 7540 7541 DeclsMap Decls; 7542 7543 for (DeclID ID : It->second.Table.findAll()) { 7544 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7545 Decls[ND->getDeclName()].push_back(ND); 7546 } 7547 7548 ++NumVisibleDeclContextsRead; 7549 7550 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) { 7551 SetExternalVisibleDeclsForName(DC, I->first, I->second); 7552 } 7553 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false); 7554 } 7555 7556 const serialization::reader::DeclContextLookupTable * 7557 ASTReader::getLoadedLookupTables(DeclContext *Primary) const { 7558 auto I = Lookups.find(Primary); 7559 return I == Lookups.end() ? nullptr : &I->second; 7560 } 7561 7562 /// Under non-PCH compilation the consumer receives the objc methods 7563 /// before receiving the implementation, and codegen depends on this. 7564 /// We simulate this by deserializing and passing to consumer the methods of the 7565 /// implementation before passing the deserialized implementation decl. 7566 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD, 7567 ASTConsumer *Consumer) { 7568 assert(ImplD && Consumer); 7569 7570 for (auto *I : ImplD->methods()) 7571 Consumer->HandleInterestingDecl(DeclGroupRef(I)); 7572 7573 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD)); 7574 } 7575 7576 void ASTReader::PassInterestingDeclToConsumer(Decl *D) { 7577 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) 7578 PassObjCImplDeclToConsumer(ImplD, Consumer); 7579 else 7580 Consumer->HandleInterestingDecl(DeclGroupRef(D)); 7581 } 7582 7583 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) { 7584 this->Consumer = Consumer; 7585 7586 if (Consumer) 7587 PassInterestingDeclsToConsumer(); 7588 7589 if (DeserializationListener) 7590 DeserializationListener->ReaderInitialized(this); 7591 } 7592 7593 void ASTReader::PrintStats() { 7594 std::fprintf(stderr, "*** AST File Statistics:\n"); 7595 7596 unsigned NumTypesLoaded 7597 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), 7598 QualType()); 7599 unsigned NumDeclsLoaded 7600 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), 7601 (Decl *)nullptr); 7602 unsigned NumIdentifiersLoaded 7603 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), 7604 IdentifiersLoaded.end(), 7605 (IdentifierInfo *)nullptr); 7606 unsigned NumMacrosLoaded 7607 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(), 7608 MacrosLoaded.end(), 7609 (MacroInfo *)nullptr); 7610 unsigned NumSelectorsLoaded 7611 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), 7612 SelectorsLoaded.end(), 7613 Selector()); 7614 7615 if (unsigned TotalNumSLocEntries = getTotalNumSLocs()) 7616 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", 7617 NumSLocEntriesRead, TotalNumSLocEntries, 7618 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); 7619 if (!TypesLoaded.empty()) 7620 std::fprintf(stderr, " %u/%u types read (%f%%)\n", 7621 NumTypesLoaded, (unsigned)TypesLoaded.size(), 7622 ((float)NumTypesLoaded/TypesLoaded.size() * 100)); 7623 if (!DeclsLoaded.empty()) 7624 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", 7625 NumDeclsLoaded, (unsigned)DeclsLoaded.size(), 7626 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); 7627 if (!IdentifiersLoaded.empty()) 7628 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", 7629 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), 7630 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); 7631 if (!MacrosLoaded.empty()) 7632 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7633 NumMacrosLoaded, (unsigned)MacrosLoaded.size(), 7634 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100)); 7635 if (!SelectorsLoaded.empty()) 7636 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", 7637 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), 7638 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); 7639 if (TotalNumStatements) 7640 std::fprintf(stderr, " %u/%u statements read (%f%%)\n", 7641 NumStatementsRead, TotalNumStatements, 7642 ((float)NumStatementsRead/TotalNumStatements * 100)); 7643 if (TotalNumMacros) 7644 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7645 NumMacrosRead, TotalNumMacros, 7646 ((float)NumMacrosRead/TotalNumMacros * 100)); 7647 if (TotalLexicalDeclContexts) 7648 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", 7649 NumLexicalDeclContextsRead, TotalLexicalDeclContexts, 7650 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts 7651 * 100)); 7652 if (TotalVisibleDeclContexts) 7653 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", 7654 NumVisibleDeclContextsRead, TotalVisibleDeclContexts, 7655 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts 7656 * 100)); 7657 if (TotalNumMethodPoolEntries) 7658 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", 7659 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, 7660 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries 7661 * 100)); 7662 if (NumMethodPoolLookups) 7663 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n", 7664 NumMethodPoolHits, NumMethodPoolLookups, 7665 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0)); 7666 if (NumMethodPoolTableLookups) 7667 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n", 7668 NumMethodPoolTableHits, NumMethodPoolTableLookups, 7669 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups 7670 * 100.0)); 7671 if (NumIdentifierLookupHits) 7672 std::fprintf(stderr, 7673 " %u / %u identifier table lookups succeeded (%f%%)\n", 7674 NumIdentifierLookupHits, NumIdentifierLookups, 7675 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups); 7676 7677 if (GlobalIndex) { 7678 std::fprintf(stderr, "\n"); 7679 GlobalIndex->printStats(); 7680 } 7681 7682 std::fprintf(stderr, "\n"); 7683 dump(); 7684 std::fprintf(stderr, "\n"); 7685 } 7686 7687 template<typename Key, typename ModuleFile, unsigned InitialCapacity> 7688 LLVM_DUMP_METHOD static void 7689 dumpModuleIDMap(StringRef Name, 7690 const ContinuousRangeMap<Key, ModuleFile *, 7691 InitialCapacity> &Map) { 7692 if (Map.begin() == Map.end()) 7693 return; 7694 7695 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>; 7696 7697 llvm::errs() << Name << ":\n"; 7698 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end(); 7699 I != IEnd; ++I) { 7700 llvm::errs() << " " << I->first << " -> " << I->second->FileName 7701 << "\n"; 7702 } 7703 } 7704 7705 LLVM_DUMP_METHOD void ASTReader::dump() { 7706 llvm::errs() << "*** PCH/ModuleFile Remappings:\n"; 7707 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap); 7708 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap); 7709 dumpModuleIDMap("Global type map", GlobalTypeMap); 7710 dumpModuleIDMap("Global declaration map", GlobalDeclMap); 7711 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap); 7712 dumpModuleIDMap("Global macro map", GlobalMacroMap); 7713 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap); 7714 dumpModuleIDMap("Global selector map", GlobalSelectorMap); 7715 dumpModuleIDMap("Global preprocessed entity map", 7716 GlobalPreprocessedEntityMap); 7717 7718 llvm::errs() << "\n*** PCH/Modules Loaded:"; 7719 for (ModuleFile &M : ModuleMgr) 7720 M.dump(); 7721 } 7722 7723 /// Return the amount of memory used by memory buffers, breaking down 7724 /// by heap-backed versus mmap'ed memory. 7725 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const { 7726 for (ModuleFile &I : ModuleMgr) { 7727 if (llvm::MemoryBuffer *buf = I.Buffer) { 7728 size_t bytes = buf->getBufferSize(); 7729 switch (buf->getBufferKind()) { 7730 case llvm::MemoryBuffer::MemoryBuffer_Malloc: 7731 sizes.malloc_bytes += bytes; 7732 break; 7733 case llvm::MemoryBuffer::MemoryBuffer_MMap: 7734 sizes.mmap_bytes += bytes; 7735 break; 7736 } 7737 } 7738 } 7739 } 7740 7741 void ASTReader::InitializeSema(Sema &S) { 7742 SemaObj = &S; 7743 S.addExternalSource(this); 7744 7745 // Makes sure any declarations that were deserialized "too early" 7746 // still get added to the identifier's declaration chains. 7747 for (uint64_t ID : PreloadedDeclIDs) { 7748 NamedDecl *D = cast<NamedDecl>(GetDecl(ID)); 7749 pushExternalDeclIntoScope(D, D->getDeclName()); 7750 } 7751 PreloadedDeclIDs.clear(); 7752 7753 // FIXME: What happens if these are changed by a module import? 7754 if (!FPPragmaOptions.empty()) { 7755 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS"); 7756 SemaObj->FPFeatures = FPOptions(FPPragmaOptions[0]); 7757 } 7758 7759 SemaObj->OpenCLFeatures.copy(OpenCLExtensions); 7760 SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap; 7761 SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap; 7762 7763 UpdateSema(); 7764 } 7765 7766 void ASTReader::UpdateSema() { 7767 assert(SemaObj && "no Sema to update"); 7768 7769 // Load the offsets of the declarations that Sema references. 7770 // They will be lazily deserialized when needed. 7771 if (!SemaDeclRefs.empty()) { 7772 assert(SemaDeclRefs.size() % 3 == 0); 7773 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) { 7774 if (!SemaObj->StdNamespace) 7775 SemaObj->StdNamespace = SemaDeclRefs[I]; 7776 if (!SemaObj->StdBadAlloc) 7777 SemaObj->StdBadAlloc = SemaDeclRefs[I+1]; 7778 if (!SemaObj->StdAlignValT) 7779 SemaObj->StdAlignValT = SemaDeclRefs[I+2]; 7780 } 7781 SemaDeclRefs.clear(); 7782 } 7783 7784 // Update the state of pragmas. Use the same API as if we had encountered the 7785 // pragma in the source. 7786 if(OptimizeOffPragmaLocation.isValid()) 7787 SemaObj->ActOnPragmaOptimize(/* IsOn = */ false, OptimizeOffPragmaLocation); 7788 if (PragmaMSStructState != -1) 7789 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState); 7790 if (PointersToMembersPragmaLocation.isValid()) { 7791 SemaObj->ActOnPragmaMSPointersToMembers( 7792 (LangOptions::PragmaMSPointersToMembersKind) 7793 PragmaMSPointersToMembersState, 7794 PointersToMembersPragmaLocation); 7795 } 7796 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth; 7797 7798 if (PragmaPackCurrentValue) { 7799 // The bottom of the stack might have a default value. It must be adjusted 7800 // to the current value to ensure that the packing state is preserved after 7801 // popping entries that were included/imported from a PCH/module. 7802 bool DropFirst = false; 7803 if (!PragmaPackStack.empty() && 7804 PragmaPackStack.front().Location.isInvalid()) { 7805 assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue && 7806 "Expected a default alignment value"); 7807 SemaObj->PackStack.Stack.emplace_back( 7808 PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue, 7809 SemaObj->PackStack.CurrentPragmaLocation, 7810 PragmaPackStack.front().PushLocation); 7811 DropFirst = true; 7812 } 7813 for (const auto &Entry : 7814 llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0)) 7815 SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value, 7816 Entry.Location, Entry.PushLocation); 7817 if (PragmaPackCurrentLocation.isInvalid()) { 7818 assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue && 7819 "Expected a default alignment value"); 7820 // Keep the current values. 7821 } else { 7822 SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue; 7823 SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation; 7824 } 7825 } 7826 } 7827 7828 IdentifierInfo *ASTReader::get(StringRef Name) { 7829 // Note that we are loading an identifier. 7830 Deserializing AnIdentifier(this); 7831 7832 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0, 7833 NumIdentifierLookups, 7834 NumIdentifierLookupHits); 7835 7836 // We don't need to do identifier table lookups in C++ modules (we preload 7837 // all interesting declarations, and don't need to use the scope for name 7838 // lookups). Perform the lookup in PCH files, though, since we don't build 7839 // a complete initial identifier table if we're carrying on from a PCH. 7840 if (PP.getLangOpts().CPlusPlus) { 7841 for (auto F : ModuleMgr.pch_modules()) 7842 if (Visitor(*F)) 7843 break; 7844 } else { 7845 // If there is a global index, look there first to determine which modules 7846 // provably do not have any results for this identifier. 7847 GlobalModuleIndex::HitSet Hits; 7848 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 7849 if (!loadGlobalIndex()) { 7850 if (GlobalIndex->lookupIdentifier(Name, Hits)) { 7851 HitsPtr = &Hits; 7852 } 7853 } 7854 7855 ModuleMgr.visit(Visitor, HitsPtr); 7856 } 7857 7858 IdentifierInfo *II = Visitor.getIdentifierInfo(); 7859 markIdentifierUpToDate(II); 7860 return II; 7861 } 7862 7863 namespace clang { 7864 7865 /// An identifier-lookup iterator that enumerates all of the 7866 /// identifiers stored within a set of AST files. 7867 class ASTIdentifierIterator : public IdentifierIterator { 7868 /// The AST reader whose identifiers are being enumerated. 7869 const ASTReader &Reader; 7870 7871 /// The current index into the chain of AST files stored in 7872 /// the AST reader. 7873 unsigned Index; 7874 7875 /// The current position within the identifier lookup table 7876 /// of the current AST file. 7877 ASTIdentifierLookupTable::key_iterator Current; 7878 7879 /// The end position within the identifier lookup table of 7880 /// the current AST file. 7881 ASTIdentifierLookupTable::key_iterator End; 7882 7883 /// Whether to skip any modules in the ASTReader. 7884 bool SkipModules; 7885 7886 public: 7887 explicit ASTIdentifierIterator(const ASTReader &Reader, 7888 bool SkipModules = false); 7889 7890 StringRef Next() override; 7891 }; 7892 7893 } // namespace clang 7894 7895 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader, 7896 bool SkipModules) 7897 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) { 7898 } 7899 7900 StringRef ASTIdentifierIterator::Next() { 7901 while (Current == End) { 7902 // If we have exhausted all of our AST files, we're done. 7903 if (Index == 0) 7904 return StringRef(); 7905 7906 --Index; 7907 ModuleFile &F = Reader.ModuleMgr[Index]; 7908 if (SkipModules && F.isModule()) 7909 continue; 7910 7911 ASTIdentifierLookupTable *IdTable = 7912 (ASTIdentifierLookupTable *)F.IdentifierLookupTable; 7913 Current = IdTable->key_begin(); 7914 End = IdTable->key_end(); 7915 } 7916 7917 // We have any identifiers remaining in the current AST file; return 7918 // the next one. 7919 StringRef Result = *Current; 7920 ++Current; 7921 return Result; 7922 } 7923 7924 namespace { 7925 7926 /// A utility for appending two IdentifierIterators. 7927 class ChainedIdentifierIterator : public IdentifierIterator { 7928 std::unique_ptr<IdentifierIterator> Current; 7929 std::unique_ptr<IdentifierIterator> Queued; 7930 7931 public: 7932 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First, 7933 std::unique_ptr<IdentifierIterator> Second) 7934 : Current(std::move(First)), Queued(std::move(Second)) {} 7935 7936 StringRef Next() override { 7937 if (!Current) 7938 return StringRef(); 7939 7940 StringRef result = Current->Next(); 7941 if (!result.empty()) 7942 return result; 7943 7944 // Try the queued iterator, which may itself be empty. 7945 Current.reset(); 7946 std::swap(Current, Queued); 7947 return Next(); 7948 } 7949 }; 7950 7951 } // namespace 7952 7953 IdentifierIterator *ASTReader::getIdentifiers() { 7954 if (!loadGlobalIndex()) { 7955 std::unique_ptr<IdentifierIterator> ReaderIter( 7956 new ASTIdentifierIterator(*this, /*SkipModules=*/true)); 7957 std::unique_ptr<IdentifierIterator> ModulesIter( 7958 GlobalIndex->createIdentifierIterator()); 7959 return new ChainedIdentifierIterator(std::move(ReaderIter), 7960 std::move(ModulesIter)); 7961 } 7962 7963 return new ASTIdentifierIterator(*this); 7964 } 7965 7966 namespace clang { 7967 namespace serialization { 7968 7969 class ReadMethodPoolVisitor { 7970 ASTReader &Reader; 7971 Selector Sel; 7972 unsigned PriorGeneration; 7973 unsigned InstanceBits = 0; 7974 unsigned FactoryBits = 0; 7975 bool InstanceHasMoreThanOneDecl = false; 7976 bool FactoryHasMoreThanOneDecl = false; 7977 SmallVector<ObjCMethodDecl *, 4> InstanceMethods; 7978 SmallVector<ObjCMethodDecl *, 4> FactoryMethods; 7979 7980 public: 7981 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel, 7982 unsigned PriorGeneration) 7983 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {} 7984 7985 bool operator()(ModuleFile &M) { 7986 if (!M.SelectorLookupTable) 7987 return false; 7988 7989 // If we've already searched this module file, skip it now. 7990 if (M.Generation <= PriorGeneration) 7991 return true; 7992 7993 ++Reader.NumMethodPoolTableLookups; 7994 ASTSelectorLookupTable *PoolTable 7995 = (ASTSelectorLookupTable*)M.SelectorLookupTable; 7996 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel); 7997 if (Pos == PoolTable->end()) 7998 return false; 7999 8000 ++Reader.NumMethodPoolTableHits; 8001 ++Reader.NumSelectorsRead; 8002 // FIXME: Not quite happy with the statistics here. We probably should 8003 // disable this tracking when called via LoadSelector. 8004 // Also, should entries without methods count as misses? 8005 ++Reader.NumMethodPoolEntriesRead; 8006 ASTSelectorLookupTrait::data_type Data = *Pos; 8007 if (Reader.DeserializationListener) 8008 Reader.DeserializationListener->SelectorRead(Data.ID, Sel); 8009 8010 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end()); 8011 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end()); 8012 InstanceBits = Data.InstanceBits; 8013 FactoryBits = Data.FactoryBits; 8014 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl; 8015 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl; 8016 return true; 8017 } 8018 8019 /// Retrieve the instance methods found by this visitor. 8020 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const { 8021 return InstanceMethods; 8022 } 8023 8024 /// Retrieve the instance methods found by this visitor. 8025 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const { 8026 return FactoryMethods; 8027 } 8028 8029 unsigned getInstanceBits() const { return InstanceBits; } 8030 unsigned getFactoryBits() const { return FactoryBits; } 8031 8032 bool instanceHasMoreThanOneDecl() const { 8033 return InstanceHasMoreThanOneDecl; 8034 } 8035 8036 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; } 8037 }; 8038 8039 } // namespace serialization 8040 } // namespace clang 8041 8042 /// Add the given set of methods to the method list. 8043 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods, 8044 ObjCMethodList &List) { 8045 for (unsigned I = 0, N = Methods.size(); I != N; ++I) { 8046 S.addMethodToGlobalList(&List, Methods[I]); 8047 } 8048 } 8049 8050 void ASTReader::ReadMethodPool(Selector Sel) { 8051 // Get the selector generation and update it to the current generation. 8052 unsigned &Generation = SelectorGeneration[Sel]; 8053 unsigned PriorGeneration = Generation; 8054 Generation = getGeneration(); 8055 SelectorOutOfDate[Sel] = false; 8056 8057 // Search for methods defined with this selector. 8058 ++NumMethodPoolLookups; 8059 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration); 8060 ModuleMgr.visit(Visitor); 8061 8062 if (Visitor.getInstanceMethods().empty() && 8063 Visitor.getFactoryMethods().empty()) 8064 return; 8065 8066 ++NumMethodPoolHits; 8067 8068 if (!getSema()) 8069 return; 8070 8071 Sema &S = *getSema(); 8072 Sema::GlobalMethodPool::iterator Pos 8073 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first; 8074 8075 Pos->second.first.setBits(Visitor.getInstanceBits()); 8076 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl()); 8077 Pos->second.second.setBits(Visitor.getFactoryBits()); 8078 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl()); 8079 8080 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since 8081 // when building a module we keep every method individually and may need to 8082 // update hasMoreThanOneDecl as we add the methods. 8083 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first); 8084 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second); 8085 } 8086 8087 void ASTReader::updateOutOfDateSelector(Selector Sel) { 8088 if (SelectorOutOfDate[Sel]) 8089 ReadMethodPool(Sel); 8090 } 8091 8092 void ASTReader::ReadKnownNamespaces( 8093 SmallVectorImpl<NamespaceDecl *> &Namespaces) { 8094 Namespaces.clear(); 8095 8096 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) { 8097 if (NamespaceDecl *Namespace 8098 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I]))) 8099 Namespaces.push_back(Namespace); 8100 } 8101 } 8102 8103 void ASTReader::ReadUndefinedButUsed( 8104 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) { 8105 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) { 8106 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++])); 8107 SourceLocation Loc = 8108 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]); 8109 Undefined.insert(std::make_pair(D, Loc)); 8110 } 8111 } 8112 8113 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector< 8114 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> & 8115 Exprs) { 8116 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) { 8117 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++])); 8118 uint64_t Count = DelayedDeleteExprs[Idx++]; 8119 for (uint64_t C = 0; C < Count; ++C) { 8120 SourceLocation DeleteLoc = 8121 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]); 8122 const bool IsArrayForm = DelayedDeleteExprs[Idx++]; 8123 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm)); 8124 } 8125 } 8126 } 8127 8128 void ASTReader::ReadTentativeDefinitions( 8129 SmallVectorImpl<VarDecl *> &TentativeDefs) { 8130 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { 8131 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I])); 8132 if (Var) 8133 TentativeDefs.push_back(Var); 8134 } 8135 TentativeDefinitions.clear(); 8136 } 8137 8138 void ASTReader::ReadUnusedFileScopedDecls( 8139 SmallVectorImpl<const DeclaratorDecl *> &Decls) { 8140 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) { 8141 DeclaratorDecl *D 8142 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I])); 8143 if (D) 8144 Decls.push_back(D); 8145 } 8146 UnusedFileScopedDecls.clear(); 8147 } 8148 8149 void ASTReader::ReadDelegatingConstructors( 8150 SmallVectorImpl<CXXConstructorDecl *> &Decls) { 8151 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) { 8152 CXXConstructorDecl *D 8153 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I])); 8154 if (D) 8155 Decls.push_back(D); 8156 } 8157 DelegatingCtorDecls.clear(); 8158 } 8159 8160 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) { 8161 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) { 8162 TypedefNameDecl *D 8163 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])); 8164 if (D) 8165 Decls.push_back(D); 8166 } 8167 ExtVectorDecls.clear(); 8168 } 8169 8170 void ASTReader::ReadUnusedLocalTypedefNameCandidates( 8171 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) { 8172 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N; 8173 ++I) { 8174 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>( 8175 GetDecl(UnusedLocalTypedefNameCandidates[I])); 8176 if (D) 8177 Decls.insert(D); 8178 } 8179 UnusedLocalTypedefNameCandidates.clear(); 8180 } 8181 8182 void ASTReader::ReadReferencedSelectors( 8183 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) { 8184 if (ReferencedSelectorsData.empty()) 8185 return; 8186 8187 // If there are @selector references added them to its pool. This is for 8188 // implementation of -Wselector. 8189 unsigned int DataSize = ReferencedSelectorsData.size()-1; 8190 unsigned I = 0; 8191 while (I < DataSize) { 8192 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]); 8193 SourceLocation SelLoc 8194 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]); 8195 Sels.push_back(std::make_pair(Sel, SelLoc)); 8196 } 8197 ReferencedSelectorsData.clear(); 8198 } 8199 8200 void ASTReader::ReadWeakUndeclaredIdentifiers( 8201 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) { 8202 if (WeakUndeclaredIdentifiers.empty()) 8203 return; 8204 8205 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) { 8206 IdentifierInfo *WeakId 8207 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8208 IdentifierInfo *AliasId 8209 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8210 SourceLocation Loc 8211 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]); 8212 bool Used = WeakUndeclaredIdentifiers[I++]; 8213 WeakInfo WI(AliasId, Loc); 8214 WI.setUsed(Used); 8215 WeakIDs.push_back(std::make_pair(WeakId, WI)); 8216 } 8217 WeakUndeclaredIdentifiers.clear(); 8218 } 8219 8220 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) { 8221 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) { 8222 ExternalVTableUse VT; 8223 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); 8224 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]); 8225 VT.DefinitionRequired = VTableUses[Idx++]; 8226 VTables.push_back(VT); 8227 } 8228 8229 VTableUses.clear(); 8230 } 8231 8232 void ASTReader::ReadPendingInstantiations( 8233 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) { 8234 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) { 8235 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++])); 8236 SourceLocation Loc 8237 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]); 8238 8239 Pending.push_back(std::make_pair(D, Loc)); 8240 } 8241 PendingInstantiations.clear(); 8242 } 8243 8244 void ASTReader::ReadLateParsedTemplates( 8245 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>> 8246 &LPTMap) { 8247 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N; 8248 /* In loop */) { 8249 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++])); 8250 8251 auto LT = llvm::make_unique<LateParsedTemplate>(); 8252 LT->D = GetDecl(LateParsedTemplates[Idx++]); 8253 8254 ModuleFile *F = getOwningModuleFile(LT->D); 8255 assert(F && "No module"); 8256 8257 unsigned TokN = LateParsedTemplates[Idx++]; 8258 LT->Toks.reserve(TokN); 8259 for (unsigned T = 0; T < TokN; ++T) 8260 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx)); 8261 8262 LPTMap.insert(std::make_pair(FD, std::move(LT))); 8263 } 8264 8265 LateParsedTemplates.clear(); 8266 } 8267 8268 void ASTReader::LoadSelector(Selector Sel) { 8269 // It would be complicated to avoid reading the methods anyway. So don't. 8270 ReadMethodPool(Sel); 8271 } 8272 8273 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) { 8274 assert(ID && "Non-zero identifier ID required"); 8275 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); 8276 IdentifiersLoaded[ID - 1] = II; 8277 if (DeserializationListener) 8278 DeserializationListener->IdentifierRead(ID, II); 8279 } 8280 8281 /// Set the globally-visible declarations associated with the given 8282 /// identifier. 8283 /// 8284 /// If the AST reader is currently in a state where the given declaration IDs 8285 /// cannot safely be resolved, they are queued until it is safe to resolve 8286 /// them. 8287 /// 8288 /// \param II an IdentifierInfo that refers to one or more globally-visible 8289 /// declarations. 8290 /// 8291 /// \param DeclIDs the set of declaration IDs with the name @p II that are 8292 /// visible at global scope. 8293 /// 8294 /// \param Decls if non-null, this vector will be populated with the set of 8295 /// deserialized declarations. These declarations will not be pushed into 8296 /// scope. 8297 void 8298 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, 8299 const SmallVectorImpl<uint32_t> &DeclIDs, 8300 SmallVectorImpl<Decl *> *Decls) { 8301 if (NumCurrentElementsDeserializing && !Decls) { 8302 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end()); 8303 return; 8304 } 8305 8306 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { 8307 if (!SemaObj) { 8308 // Queue this declaration so that it will be added to the 8309 // translation unit scope and identifier's declaration chain 8310 // once a Sema object is known. 8311 PreloadedDeclIDs.push_back(DeclIDs[I]); 8312 continue; 8313 } 8314 8315 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); 8316 8317 // If we're simply supposed to record the declarations, do so now. 8318 if (Decls) { 8319 Decls->push_back(D); 8320 continue; 8321 } 8322 8323 // Introduce this declaration into the translation-unit scope 8324 // and add it to the declaration chain for this identifier, so 8325 // that (unqualified) name lookup will find it. 8326 pushExternalDeclIntoScope(D, II); 8327 } 8328 } 8329 8330 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) { 8331 if (ID == 0) 8332 return nullptr; 8333 8334 if (IdentifiersLoaded.empty()) { 8335 Error("no identifier table in AST file"); 8336 return nullptr; 8337 } 8338 8339 ID -= 1; 8340 if (!IdentifiersLoaded[ID]) { 8341 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1); 8342 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map"); 8343 ModuleFile *M = I->second; 8344 unsigned Index = ID - M->BaseIdentifierID; 8345 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index]; 8346 8347 // All of the strings in the AST file are preceded by a 16-bit length. 8348 // Extract that 16-bit length to avoid having to execute strlen(). 8349 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as 8350 // unsigned integers. This is important to avoid integer overflow when 8351 // we cast them to 'unsigned'. 8352 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2; 8353 unsigned StrLen = (((unsigned) StrLenPtr[0]) 8354 | (((unsigned) StrLenPtr[1]) << 8)) - 1; 8355 auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen)); 8356 IdentifiersLoaded[ID] = &II; 8357 markIdentifierFromAST(*this, II); 8358 if (DeserializationListener) 8359 DeserializationListener->IdentifierRead(ID + 1, &II); 8360 } 8361 8362 return IdentifiersLoaded[ID]; 8363 } 8364 8365 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) { 8366 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID)); 8367 } 8368 8369 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) { 8370 if (LocalID < NUM_PREDEF_IDENT_IDS) 8371 return LocalID; 8372 8373 if (!M.ModuleOffsetMap.empty()) 8374 ReadModuleOffsetMap(M); 8375 8376 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8377 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS); 8378 assert(I != M.IdentifierRemap.end() 8379 && "Invalid index into identifier index remap"); 8380 8381 return LocalID + I->second; 8382 } 8383 8384 MacroInfo *ASTReader::getMacro(MacroID ID) { 8385 if (ID == 0) 8386 return nullptr; 8387 8388 if (MacrosLoaded.empty()) { 8389 Error("no macro table in AST file"); 8390 return nullptr; 8391 } 8392 8393 ID -= NUM_PREDEF_MACRO_IDS; 8394 if (!MacrosLoaded[ID]) { 8395 GlobalMacroMapType::iterator I 8396 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS); 8397 assert(I != GlobalMacroMap.end() && "Corrupted global macro map"); 8398 ModuleFile *M = I->second; 8399 unsigned Index = ID - M->BaseMacroID; 8400 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]); 8401 8402 if (DeserializationListener) 8403 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS, 8404 MacrosLoaded[ID]); 8405 } 8406 8407 return MacrosLoaded[ID]; 8408 } 8409 8410 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) { 8411 if (LocalID < NUM_PREDEF_MACRO_IDS) 8412 return LocalID; 8413 8414 if (!M.ModuleOffsetMap.empty()) 8415 ReadModuleOffsetMap(M); 8416 8417 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8418 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS); 8419 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap"); 8420 8421 return LocalID + I->second; 8422 } 8423 8424 serialization::SubmoduleID 8425 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) { 8426 if (LocalID < NUM_PREDEF_SUBMODULE_IDS) 8427 return LocalID; 8428 8429 if (!M.ModuleOffsetMap.empty()) 8430 ReadModuleOffsetMap(M); 8431 8432 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8433 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS); 8434 assert(I != M.SubmoduleRemap.end() 8435 && "Invalid index into submodule index remap"); 8436 8437 return LocalID + I->second; 8438 } 8439 8440 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) { 8441 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) { 8442 assert(GlobalID == 0 && "Unhandled global submodule ID"); 8443 return nullptr; 8444 } 8445 8446 if (GlobalID > SubmodulesLoaded.size()) { 8447 Error("submodule ID out of range in AST file"); 8448 return nullptr; 8449 } 8450 8451 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS]; 8452 } 8453 8454 Module *ASTReader::getModule(unsigned ID) { 8455 return getSubmodule(ID); 8456 } 8457 8458 bool ASTReader::DeclIsFromPCHWithObjectFile(const Decl *D) { 8459 ModuleFile *MF = getOwningModuleFile(D); 8460 return MF && MF->PCHHasObjectFile; 8461 } 8462 8463 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) { 8464 if (ID & 1) { 8465 // It's a module, look it up by submodule ID. 8466 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1)); 8467 return I == GlobalSubmoduleMap.end() ? nullptr : I->second; 8468 } else { 8469 // It's a prefix (preamble, PCH, ...). Look it up by index. 8470 unsigned IndexFromEnd = ID >> 1; 8471 assert(IndexFromEnd && "got reference to unknown module file"); 8472 return getModuleManager().pch_modules().end()[-IndexFromEnd]; 8473 } 8474 } 8475 8476 unsigned ASTReader::getModuleFileID(ModuleFile *F) { 8477 if (!F) 8478 return 1; 8479 8480 // For a file representing a module, use the submodule ID of the top-level 8481 // module as the file ID. For any other kind of file, the number of such 8482 // files loaded beforehand will be the same on reload. 8483 // FIXME: Is this true even if we have an explicit module file and a PCH? 8484 if (F->isModule()) 8485 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1; 8486 8487 auto PCHModules = getModuleManager().pch_modules(); 8488 auto I = std::find(PCHModules.begin(), PCHModules.end(), F); 8489 assert(I != PCHModules.end() && "emitting reference to unknown file"); 8490 return (I - PCHModules.end()) << 1; 8491 } 8492 8493 llvm::Optional<ExternalASTSource::ASTSourceDescriptor> 8494 ASTReader::getSourceDescriptor(unsigned ID) { 8495 if (const Module *M = getSubmodule(ID)) 8496 return ExternalASTSource::ASTSourceDescriptor(*M); 8497 8498 // If there is only a single PCH, return it instead. 8499 // Chained PCH are not supported. 8500 const auto &PCHChain = ModuleMgr.pch_modules(); 8501 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) { 8502 ModuleFile &MF = ModuleMgr.getPrimaryModule(); 8503 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName); 8504 StringRef FileName = llvm::sys::path::filename(MF.FileName); 8505 return ASTReader::ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName, 8506 MF.Signature); 8507 } 8508 return None; 8509 } 8510 8511 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) { 8512 auto I = DefinitionSource.find(FD); 8513 if (I == DefinitionSource.end()) 8514 return EK_ReplyHazy; 8515 return I->second ? EK_Never : EK_Always; 8516 } 8517 8518 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) { 8519 return DecodeSelector(getGlobalSelectorID(M, LocalID)); 8520 } 8521 8522 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) { 8523 if (ID == 0) 8524 return Selector(); 8525 8526 if (ID > SelectorsLoaded.size()) { 8527 Error("selector ID out of range in AST file"); 8528 return Selector(); 8529 } 8530 8531 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) { 8532 // Load this selector from the selector table. 8533 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID); 8534 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map"); 8535 ModuleFile &M = *I->second; 8536 ASTSelectorLookupTrait Trait(*this, M); 8537 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS; 8538 SelectorsLoaded[ID - 1] = 8539 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0); 8540 if (DeserializationListener) 8541 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 8542 } 8543 8544 return SelectorsLoaded[ID - 1]; 8545 } 8546 8547 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) { 8548 return DecodeSelector(ID); 8549 } 8550 8551 uint32_t ASTReader::GetNumExternalSelectors() { 8552 // ID 0 (the null selector) is considered an external selector. 8553 return getTotalNumSelectors() + 1; 8554 } 8555 8556 serialization::SelectorID 8557 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const { 8558 if (LocalID < NUM_PREDEF_SELECTOR_IDS) 8559 return LocalID; 8560 8561 if (!M.ModuleOffsetMap.empty()) 8562 ReadModuleOffsetMap(M); 8563 8564 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8565 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS); 8566 assert(I != M.SelectorRemap.end() 8567 && "Invalid index into selector index remap"); 8568 8569 return LocalID + I->second; 8570 } 8571 8572 DeclarationName 8573 ASTReader::ReadDeclarationName(ModuleFile &F, 8574 const RecordData &Record, unsigned &Idx) { 8575 ASTContext &Context = getContext(); 8576 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++]; 8577 switch (Kind) { 8578 case DeclarationName::Identifier: 8579 return DeclarationName(GetIdentifierInfo(F, Record, Idx)); 8580 8581 case DeclarationName::ObjCZeroArgSelector: 8582 case DeclarationName::ObjCOneArgSelector: 8583 case DeclarationName::ObjCMultiArgSelector: 8584 return DeclarationName(ReadSelector(F, Record, Idx)); 8585 8586 case DeclarationName::CXXConstructorName: 8587 return Context.DeclarationNames.getCXXConstructorName( 8588 Context.getCanonicalType(readType(F, Record, Idx))); 8589 8590 case DeclarationName::CXXDestructorName: 8591 return Context.DeclarationNames.getCXXDestructorName( 8592 Context.getCanonicalType(readType(F, Record, Idx))); 8593 8594 case DeclarationName::CXXDeductionGuideName: 8595 return Context.DeclarationNames.getCXXDeductionGuideName( 8596 ReadDeclAs<TemplateDecl>(F, Record, Idx)); 8597 8598 case DeclarationName::CXXConversionFunctionName: 8599 return Context.DeclarationNames.getCXXConversionFunctionName( 8600 Context.getCanonicalType(readType(F, Record, Idx))); 8601 8602 case DeclarationName::CXXOperatorName: 8603 return Context.DeclarationNames.getCXXOperatorName( 8604 (OverloadedOperatorKind)Record[Idx++]); 8605 8606 case DeclarationName::CXXLiteralOperatorName: 8607 return Context.DeclarationNames.getCXXLiteralOperatorName( 8608 GetIdentifierInfo(F, Record, Idx)); 8609 8610 case DeclarationName::CXXUsingDirective: 8611 return DeclarationName::getUsingDirectiveName(); 8612 } 8613 8614 llvm_unreachable("Invalid NameKind!"); 8615 } 8616 8617 void ASTReader::ReadDeclarationNameLoc(ModuleFile &F, 8618 DeclarationNameLoc &DNLoc, 8619 DeclarationName Name, 8620 const RecordData &Record, unsigned &Idx) { 8621 switch (Name.getNameKind()) { 8622 case DeclarationName::CXXConstructorName: 8623 case DeclarationName::CXXDestructorName: 8624 case DeclarationName::CXXConversionFunctionName: 8625 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx); 8626 break; 8627 8628 case DeclarationName::CXXOperatorName: 8629 DNLoc.CXXOperatorName.BeginOpNameLoc 8630 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 8631 DNLoc.CXXOperatorName.EndOpNameLoc 8632 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 8633 break; 8634 8635 case DeclarationName::CXXLiteralOperatorName: 8636 DNLoc.CXXLiteralOperatorName.OpNameLoc 8637 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 8638 break; 8639 8640 case DeclarationName::Identifier: 8641 case DeclarationName::ObjCZeroArgSelector: 8642 case DeclarationName::ObjCOneArgSelector: 8643 case DeclarationName::ObjCMultiArgSelector: 8644 case DeclarationName::CXXUsingDirective: 8645 case DeclarationName::CXXDeductionGuideName: 8646 break; 8647 } 8648 } 8649 8650 void ASTReader::ReadDeclarationNameInfo(ModuleFile &F, 8651 DeclarationNameInfo &NameInfo, 8652 const RecordData &Record, unsigned &Idx) { 8653 NameInfo.setName(ReadDeclarationName(F, Record, Idx)); 8654 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx)); 8655 DeclarationNameLoc DNLoc; 8656 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx); 8657 NameInfo.setInfo(DNLoc); 8658 } 8659 8660 void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info, 8661 const RecordData &Record, unsigned &Idx) { 8662 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx); 8663 unsigned NumTPLists = Record[Idx++]; 8664 Info.NumTemplParamLists = NumTPLists; 8665 if (NumTPLists) { 8666 Info.TemplParamLists = 8667 new (getContext()) TemplateParameterList *[NumTPLists]; 8668 for (unsigned i = 0; i != NumTPLists; ++i) 8669 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx); 8670 } 8671 } 8672 8673 TemplateName 8674 ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record, 8675 unsigned &Idx) { 8676 ASTContext &Context = getContext(); 8677 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++]; 8678 switch (Kind) { 8679 case TemplateName::Template: 8680 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx)); 8681 8682 case TemplateName::OverloadedTemplate: { 8683 unsigned size = Record[Idx++]; 8684 UnresolvedSet<8> Decls; 8685 while (size--) 8686 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx)); 8687 8688 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end()); 8689 } 8690 8691 case TemplateName::QualifiedTemplate: { 8692 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx); 8693 bool hasTemplKeyword = Record[Idx++]; 8694 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx); 8695 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template); 8696 } 8697 8698 case TemplateName::DependentTemplate: { 8699 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx); 8700 if (Record[Idx++]) // isIdentifier 8701 return Context.getDependentTemplateName(NNS, 8702 GetIdentifierInfo(F, Record, 8703 Idx)); 8704 return Context.getDependentTemplateName(NNS, 8705 (OverloadedOperatorKind)Record[Idx++]); 8706 } 8707 8708 case TemplateName::SubstTemplateTemplateParm: { 8709 TemplateTemplateParmDecl *param 8710 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx); 8711 if (!param) return TemplateName(); 8712 TemplateName replacement = ReadTemplateName(F, Record, Idx); 8713 return Context.getSubstTemplateTemplateParm(param, replacement); 8714 } 8715 8716 case TemplateName::SubstTemplateTemplateParmPack: { 8717 TemplateTemplateParmDecl *Param 8718 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx); 8719 if (!Param) 8720 return TemplateName(); 8721 8722 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx); 8723 if (ArgPack.getKind() != TemplateArgument::Pack) 8724 return TemplateName(); 8725 8726 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack); 8727 } 8728 } 8729 8730 llvm_unreachable("Unhandled template name kind!"); 8731 } 8732 8733 TemplateArgument ASTReader::ReadTemplateArgument(ModuleFile &F, 8734 const RecordData &Record, 8735 unsigned &Idx, 8736 bool Canonicalize) { 8737 ASTContext &Context = getContext(); 8738 if (Canonicalize) { 8739 // The caller wants a canonical template argument. Sometimes the AST only 8740 // wants template arguments in canonical form (particularly as the template 8741 // argument lists of template specializations) so ensure we preserve that 8742 // canonical form across serialization. 8743 TemplateArgument Arg = ReadTemplateArgument(F, Record, Idx, false); 8744 return Context.getCanonicalTemplateArgument(Arg); 8745 } 8746 8747 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++]; 8748 switch (Kind) { 8749 case TemplateArgument::Null: 8750 return TemplateArgument(); 8751 case TemplateArgument::Type: 8752 return TemplateArgument(readType(F, Record, Idx)); 8753 case TemplateArgument::Declaration: { 8754 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx); 8755 return TemplateArgument(D, readType(F, Record, Idx)); 8756 } 8757 case TemplateArgument::NullPtr: 8758 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true); 8759 case TemplateArgument::Integral: { 8760 llvm::APSInt Value = ReadAPSInt(Record, Idx); 8761 QualType T = readType(F, Record, Idx); 8762 return TemplateArgument(Context, Value, T); 8763 } 8764 case TemplateArgument::Template: 8765 return TemplateArgument(ReadTemplateName(F, Record, Idx)); 8766 case TemplateArgument::TemplateExpansion: { 8767 TemplateName Name = ReadTemplateName(F, Record, Idx); 8768 Optional<unsigned> NumTemplateExpansions; 8769 if (unsigned NumExpansions = Record[Idx++]) 8770 NumTemplateExpansions = NumExpansions - 1; 8771 return TemplateArgument(Name, NumTemplateExpansions); 8772 } 8773 case TemplateArgument::Expression: 8774 return TemplateArgument(ReadExpr(F)); 8775 case TemplateArgument::Pack: { 8776 unsigned NumArgs = Record[Idx++]; 8777 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs]; 8778 for (unsigned I = 0; I != NumArgs; ++I) 8779 Args[I] = ReadTemplateArgument(F, Record, Idx); 8780 return TemplateArgument(llvm::makeArrayRef(Args, NumArgs)); 8781 } 8782 } 8783 8784 llvm_unreachable("Unhandled template argument kind!"); 8785 } 8786 8787 TemplateParameterList * 8788 ASTReader::ReadTemplateParameterList(ModuleFile &F, 8789 const RecordData &Record, unsigned &Idx) { 8790 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx); 8791 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx); 8792 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx); 8793 8794 unsigned NumParams = Record[Idx++]; 8795 SmallVector<NamedDecl *, 16> Params; 8796 Params.reserve(NumParams); 8797 while (NumParams--) 8798 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx)); 8799 8800 // TODO: Concepts 8801 TemplateParameterList *TemplateParams = TemplateParameterList::Create( 8802 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, nullptr); 8803 return TemplateParams; 8804 } 8805 8806 void 8807 ASTReader:: 8808 ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs, 8809 ModuleFile &F, const RecordData &Record, 8810 unsigned &Idx, bool Canonicalize) { 8811 unsigned NumTemplateArgs = Record[Idx++]; 8812 TemplArgs.reserve(NumTemplateArgs); 8813 while (NumTemplateArgs--) 8814 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx, Canonicalize)); 8815 } 8816 8817 /// Read a UnresolvedSet structure. 8818 void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set, 8819 const RecordData &Record, unsigned &Idx) { 8820 unsigned NumDecls = Record[Idx++]; 8821 Set.reserve(getContext(), NumDecls); 8822 while (NumDecls--) { 8823 DeclID ID = ReadDeclID(F, Record, Idx); 8824 AccessSpecifier AS = (AccessSpecifier)Record[Idx++]; 8825 Set.addLazyDecl(getContext(), ID, AS); 8826 } 8827 } 8828 8829 CXXBaseSpecifier 8830 ASTReader::ReadCXXBaseSpecifier(ModuleFile &F, 8831 const RecordData &Record, unsigned &Idx) { 8832 bool isVirtual = static_cast<bool>(Record[Idx++]); 8833 bool isBaseOfClass = static_cast<bool>(Record[Idx++]); 8834 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]); 8835 bool inheritConstructors = static_cast<bool>(Record[Idx++]); 8836 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx); 8837 SourceRange Range = ReadSourceRange(F, Record, Idx); 8838 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx); 8839 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo, 8840 EllipsisLoc); 8841 Result.setInheritConstructors(inheritConstructors); 8842 return Result; 8843 } 8844 8845 CXXCtorInitializer ** 8846 ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record, 8847 unsigned &Idx) { 8848 ASTContext &Context = getContext(); 8849 unsigned NumInitializers = Record[Idx++]; 8850 assert(NumInitializers && "wrote ctor initializers but have no inits"); 8851 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers]; 8852 for (unsigned i = 0; i != NumInitializers; ++i) { 8853 TypeSourceInfo *TInfo = nullptr; 8854 bool IsBaseVirtual = false; 8855 FieldDecl *Member = nullptr; 8856 IndirectFieldDecl *IndirectMember = nullptr; 8857 8858 CtorInitializerType Type = (CtorInitializerType)Record[Idx++]; 8859 switch (Type) { 8860 case CTOR_INITIALIZER_BASE: 8861 TInfo = GetTypeSourceInfo(F, Record, Idx); 8862 IsBaseVirtual = Record[Idx++]; 8863 break; 8864 8865 case CTOR_INITIALIZER_DELEGATING: 8866 TInfo = GetTypeSourceInfo(F, Record, Idx); 8867 break; 8868 8869 case CTOR_INITIALIZER_MEMBER: 8870 Member = ReadDeclAs<FieldDecl>(F, Record, Idx); 8871 break; 8872 8873 case CTOR_INITIALIZER_INDIRECT_MEMBER: 8874 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx); 8875 break; 8876 } 8877 8878 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx); 8879 Expr *Init = ReadExpr(F); 8880 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx); 8881 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx); 8882 8883 CXXCtorInitializer *BOMInit; 8884 if (Type == CTOR_INITIALIZER_BASE) 8885 BOMInit = new (Context) 8886 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init, 8887 RParenLoc, MemberOrEllipsisLoc); 8888 else if (Type == CTOR_INITIALIZER_DELEGATING) 8889 BOMInit = new (Context) 8890 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc); 8891 else if (Member) 8892 BOMInit = new (Context) 8893 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc, 8894 Init, RParenLoc); 8895 else 8896 BOMInit = new (Context) 8897 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc, 8898 LParenLoc, Init, RParenLoc); 8899 8900 if (/*IsWritten*/Record[Idx++]) { 8901 unsigned SourceOrder = Record[Idx++]; 8902 BOMInit->setSourceOrder(SourceOrder); 8903 } 8904 8905 CtorInitializers[i] = BOMInit; 8906 } 8907 8908 return CtorInitializers; 8909 } 8910 8911 NestedNameSpecifier * 8912 ASTReader::ReadNestedNameSpecifier(ModuleFile &F, 8913 const RecordData &Record, unsigned &Idx) { 8914 ASTContext &Context = getContext(); 8915 unsigned N = Record[Idx++]; 8916 NestedNameSpecifier *NNS = nullptr, *Prev = nullptr; 8917 for (unsigned I = 0; I != N; ++I) { 8918 NestedNameSpecifier::SpecifierKind Kind 8919 = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; 8920 switch (Kind) { 8921 case NestedNameSpecifier::Identifier: { 8922 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx); 8923 NNS = NestedNameSpecifier::Create(Context, Prev, II); 8924 break; 8925 } 8926 8927 case NestedNameSpecifier::Namespace: { 8928 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx); 8929 NNS = NestedNameSpecifier::Create(Context, Prev, NS); 8930 break; 8931 } 8932 8933 case NestedNameSpecifier::NamespaceAlias: { 8934 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx); 8935 NNS = NestedNameSpecifier::Create(Context, Prev, Alias); 8936 break; 8937 } 8938 8939 case NestedNameSpecifier::TypeSpec: 8940 case NestedNameSpecifier::TypeSpecWithTemplate: { 8941 const Type *T = readType(F, Record, Idx).getTypePtrOrNull(); 8942 if (!T) 8943 return nullptr; 8944 8945 bool Template = Record[Idx++]; 8946 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T); 8947 break; 8948 } 8949 8950 case NestedNameSpecifier::Global: 8951 NNS = NestedNameSpecifier::GlobalSpecifier(Context); 8952 // No associated value, and there can't be a prefix. 8953 break; 8954 8955 case NestedNameSpecifier::Super: { 8956 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx); 8957 NNS = NestedNameSpecifier::SuperSpecifier(Context, RD); 8958 break; 8959 } 8960 } 8961 Prev = NNS; 8962 } 8963 return NNS; 8964 } 8965 8966 NestedNameSpecifierLoc 8967 ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record, 8968 unsigned &Idx) { 8969 ASTContext &Context = getContext(); 8970 unsigned N = Record[Idx++]; 8971 NestedNameSpecifierLocBuilder Builder; 8972 for (unsigned I = 0; I != N; ++I) { 8973 NestedNameSpecifier::SpecifierKind Kind 8974 = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; 8975 switch (Kind) { 8976 case NestedNameSpecifier::Identifier: { 8977 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx); 8978 SourceRange Range = ReadSourceRange(F, Record, Idx); 8979 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd()); 8980 break; 8981 } 8982 8983 case NestedNameSpecifier::Namespace: { 8984 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx); 8985 SourceRange Range = ReadSourceRange(F, Record, Idx); 8986 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd()); 8987 break; 8988 } 8989 8990 case NestedNameSpecifier::NamespaceAlias: { 8991 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx); 8992 SourceRange Range = ReadSourceRange(F, Record, Idx); 8993 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd()); 8994 break; 8995 } 8996 8997 case NestedNameSpecifier::TypeSpec: 8998 case NestedNameSpecifier::TypeSpecWithTemplate: { 8999 bool Template = Record[Idx++]; 9000 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx); 9001 if (!T) 9002 return NestedNameSpecifierLoc(); 9003 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx); 9004 9005 // FIXME: 'template' keyword location not saved anywhere, so we fake it. 9006 Builder.Extend(Context, 9007 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(), 9008 T->getTypeLoc(), ColonColonLoc); 9009 break; 9010 } 9011 9012 case NestedNameSpecifier::Global: { 9013 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx); 9014 Builder.MakeGlobal(Context, ColonColonLoc); 9015 break; 9016 } 9017 9018 case NestedNameSpecifier::Super: { 9019 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx); 9020 SourceRange Range = ReadSourceRange(F, Record, Idx); 9021 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd()); 9022 break; 9023 } 9024 } 9025 } 9026 9027 return Builder.getWithLocInContext(Context); 9028 } 9029 9030 SourceRange 9031 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record, 9032 unsigned &Idx) { 9033 SourceLocation beg = ReadSourceLocation(F, Record, Idx); 9034 SourceLocation end = ReadSourceLocation(F, Record, Idx); 9035 return SourceRange(beg, end); 9036 } 9037 9038 /// Read an integral value 9039 llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) { 9040 unsigned BitWidth = Record[Idx++]; 9041 unsigned NumWords = llvm::APInt::getNumWords(BitWidth); 9042 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]); 9043 Idx += NumWords; 9044 return Result; 9045 } 9046 9047 /// Read a signed integral value 9048 llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) { 9049 bool isUnsigned = Record[Idx++]; 9050 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned); 9051 } 9052 9053 /// Read a floating-point value 9054 llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, 9055 const llvm::fltSemantics &Sem, 9056 unsigned &Idx) { 9057 return llvm::APFloat(Sem, ReadAPInt(Record, Idx)); 9058 } 9059 9060 // Read a string 9061 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) { 9062 unsigned Len = Record[Idx++]; 9063 std::string Result(Record.data() + Idx, Record.data() + Idx + Len); 9064 Idx += Len; 9065 return Result; 9066 } 9067 9068 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record, 9069 unsigned &Idx) { 9070 std::string Filename = ReadString(Record, Idx); 9071 ResolveImportedPath(F, Filename); 9072 return Filename; 9073 } 9074 9075 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record, 9076 unsigned &Idx) { 9077 unsigned Major = Record[Idx++]; 9078 unsigned Minor = Record[Idx++]; 9079 unsigned Subminor = Record[Idx++]; 9080 if (Minor == 0) 9081 return VersionTuple(Major); 9082 if (Subminor == 0) 9083 return VersionTuple(Major, Minor - 1); 9084 return VersionTuple(Major, Minor - 1, Subminor - 1); 9085 } 9086 9087 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F, 9088 const RecordData &Record, 9089 unsigned &Idx) { 9090 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx); 9091 return CXXTemporary::Create(getContext(), Decl); 9092 } 9093 9094 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const { 9095 return Diag(CurrentImportLoc, DiagID); 9096 } 9097 9098 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const { 9099 return Diags.Report(Loc, DiagID); 9100 } 9101 9102 /// Retrieve the identifier table associated with the 9103 /// preprocessor. 9104 IdentifierTable &ASTReader::getIdentifierTable() { 9105 return PP.getIdentifierTable(); 9106 } 9107 9108 /// Record that the given ID maps to the given switch-case 9109 /// statement. 9110 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { 9111 assert((*CurrSwitchCaseStmts)[ID] == nullptr && 9112 "Already have a SwitchCase with this ID"); 9113 (*CurrSwitchCaseStmts)[ID] = SC; 9114 } 9115 9116 /// Retrieve the switch-case statement with the given ID. 9117 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) { 9118 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID"); 9119 return (*CurrSwitchCaseStmts)[ID]; 9120 } 9121 9122 void ASTReader::ClearSwitchCaseIDs() { 9123 CurrSwitchCaseStmts->clear(); 9124 } 9125 9126 void ASTReader::ReadComments() { 9127 ASTContext &Context = getContext(); 9128 std::vector<RawComment *> Comments; 9129 for (SmallVectorImpl<std::pair<BitstreamCursor, 9130 serialization::ModuleFile *>>::iterator 9131 I = CommentsCursors.begin(), 9132 E = CommentsCursors.end(); 9133 I != E; ++I) { 9134 Comments.clear(); 9135 BitstreamCursor &Cursor = I->first; 9136 serialization::ModuleFile &F = *I->second; 9137 SavedStreamPosition SavedPosition(Cursor); 9138 9139 RecordData Record; 9140 while (true) { 9141 llvm::BitstreamEntry Entry = 9142 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd); 9143 9144 switch (Entry.Kind) { 9145 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 9146 case llvm::BitstreamEntry::Error: 9147 Error("malformed block record in AST file"); 9148 return; 9149 case llvm::BitstreamEntry::EndBlock: 9150 goto NextCursor; 9151 case llvm::BitstreamEntry::Record: 9152 // The interesting case. 9153 break; 9154 } 9155 9156 // Read a record. 9157 Record.clear(); 9158 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) { 9159 case COMMENTS_RAW_COMMENT: { 9160 unsigned Idx = 0; 9161 SourceRange SR = ReadSourceRange(F, Record, Idx); 9162 RawComment::CommentKind Kind = 9163 (RawComment::CommentKind) Record[Idx++]; 9164 bool IsTrailingComment = Record[Idx++]; 9165 bool IsAlmostTrailingComment = Record[Idx++]; 9166 Comments.push_back(new (Context) RawComment( 9167 SR, Kind, IsTrailingComment, IsAlmostTrailingComment)); 9168 break; 9169 } 9170 } 9171 } 9172 NextCursor: 9173 // De-serialized SourceLocations get negative FileIDs for other modules, 9174 // potentially invalidating the original order. Sort it again. 9175 llvm::sort(Comments.begin(), Comments.end(), 9176 BeforeThanCompare<RawComment>(SourceMgr)); 9177 Context.Comments.addDeserializedComments(Comments); 9178 } 9179 } 9180 9181 void ASTReader::visitInputFiles(serialization::ModuleFile &MF, 9182 bool IncludeSystem, bool Complain, 9183 llvm::function_ref<void(const serialization::InputFile &IF, 9184 bool isSystem)> Visitor) { 9185 unsigned NumUserInputs = MF.NumUserInputFiles; 9186 unsigned NumInputs = MF.InputFilesLoaded.size(); 9187 assert(NumUserInputs <= NumInputs); 9188 unsigned N = IncludeSystem ? NumInputs : NumUserInputs; 9189 for (unsigned I = 0; I < N; ++I) { 9190 bool IsSystem = I >= NumUserInputs; 9191 InputFile IF = getInputFile(MF, I+1, Complain); 9192 Visitor(IF, IsSystem); 9193 } 9194 } 9195 9196 void ASTReader::visitTopLevelModuleMaps( 9197 serialization::ModuleFile &MF, 9198 llvm::function_ref<void(const FileEntry *FE)> Visitor) { 9199 unsigned NumInputs = MF.InputFilesLoaded.size(); 9200 for (unsigned I = 0; I < NumInputs; ++I) { 9201 InputFileInfo IFI = readInputFileInfo(MF, I + 1); 9202 if (IFI.TopLevelModuleMap) 9203 // FIXME: This unnecessarily re-reads the InputFileInfo. 9204 if (auto *FE = getInputFile(MF, I + 1).getFile()) 9205 Visitor(FE); 9206 } 9207 } 9208 9209 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) { 9210 // If we know the owning module, use it. 9211 if (Module *M = D->getImportedOwningModule()) 9212 return M->getFullModuleName(); 9213 9214 // Otherwise, use the name of the top-level module the decl is within. 9215 if (ModuleFile *M = getOwningModuleFile(D)) 9216 return M->ModuleName; 9217 9218 // Not from a module. 9219 return {}; 9220 } 9221 9222 void ASTReader::finishPendingActions() { 9223 while (!PendingIdentifierInfos.empty() || 9224 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() || 9225 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() || 9226 !PendingUpdateRecords.empty()) { 9227 // If any identifiers with corresponding top-level declarations have 9228 // been loaded, load those declarations now. 9229 using TopLevelDeclsMap = 9230 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>; 9231 TopLevelDeclsMap TopLevelDecls; 9232 9233 while (!PendingIdentifierInfos.empty()) { 9234 IdentifierInfo *II = PendingIdentifierInfos.back().first; 9235 SmallVector<uint32_t, 4> DeclIDs = 9236 std::move(PendingIdentifierInfos.back().second); 9237 PendingIdentifierInfos.pop_back(); 9238 9239 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]); 9240 } 9241 9242 // For each decl chain that we wanted to complete while deserializing, mark 9243 // it as "still needs to be completed". 9244 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) { 9245 markIncompleteDeclChain(PendingIncompleteDeclChains[I]); 9246 } 9247 PendingIncompleteDeclChains.clear(); 9248 9249 // Load pending declaration chains. 9250 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) 9251 loadPendingDeclChain(PendingDeclChains[I].first, PendingDeclChains[I].second); 9252 PendingDeclChains.clear(); 9253 9254 // Make the most recent of the top-level declarations visible. 9255 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(), 9256 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) { 9257 IdentifierInfo *II = TLD->first; 9258 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) { 9259 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II); 9260 } 9261 } 9262 9263 // Load any pending macro definitions. 9264 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) { 9265 IdentifierInfo *II = PendingMacroIDs.begin()[I].first; 9266 SmallVector<PendingMacroInfo, 2> GlobalIDs; 9267 GlobalIDs.swap(PendingMacroIDs.begin()[I].second); 9268 // Initialize the macro history from chained-PCHs ahead of module imports. 9269 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9270 ++IDIdx) { 9271 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9272 if (!Info.M->isModule()) 9273 resolvePendingMacro(II, Info); 9274 } 9275 // Handle module imports. 9276 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9277 ++IDIdx) { 9278 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9279 if (Info.M->isModule()) 9280 resolvePendingMacro(II, Info); 9281 } 9282 } 9283 PendingMacroIDs.clear(); 9284 9285 // Wire up the DeclContexts for Decls that we delayed setting until 9286 // recursive loading is completed. 9287 while (!PendingDeclContextInfos.empty()) { 9288 PendingDeclContextInfo Info = PendingDeclContextInfos.front(); 9289 PendingDeclContextInfos.pop_front(); 9290 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC)); 9291 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC)); 9292 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext()); 9293 } 9294 9295 // Perform any pending declaration updates. 9296 while (!PendingUpdateRecords.empty()) { 9297 auto Update = PendingUpdateRecords.pop_back_val(); 9298 ReadingKindTracker ReadingKind(Read_Decl, *this); 9299 loadDeclUpdateRecords(Update); 9300 } 9301 } 9302 9303 // At this point, all update records for loaded decls are in place, so any 9304 // fake class definitions should have become real. 9305 assert(PendingFakeDefinitionData.empty() && 9306 "faked up a class definition but never saw the real one"); 9307 9308 // If we deserialized any C++ or Objective-C class definitions, any 9309 // Objective-C protocol definitions, or any redeclarable templates, make sure 9310 // that all redeclarations point to the definitions. Note that this can only 9311 // happen now, after the redeclaration chains have been fully wired. 9312 for (Decl *D : PendingDefinitions) { 9313 if (TagDecl *TD = dyn_cast<TagDecl>(D)) { 9314 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) { 9315 // Make sure that the TagType points at the definition. 9316 const_cast<TagType*>(TagT)->decl = TD; 9317 } 9318 9319 if (auto RD = dyn_cast<CXXRecordDecl>(D)) { 9320 for (auto *R = getMostRecentExistingDecl(RD); R; 9321 R = R->getPreviousDecl()) { 9322 assert((R == D) == 9323 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() && 9324 "declaration thinks it's the definition but it isn't"); 9325 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData; 9326 } 9327 } 9328 9329 continue; 9330 } 9331 9332 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) { 9333 // Make sure that the ObjCInterfaceType points at the definition. 9334 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl)) 9335 ->Decl = ID; 9336 9337 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl()) 9338 cast<ObjCInterfaceDecl>(R)->Data = ID->Data; 9339 9340 continue; 9341 } 9342 9343 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) { 9344 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl()) 9345 cast<ObjCProtocolDecl>(R)->Data = PD->Data; 9346 9347 continue; 9348 } 9349 9350 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl(); 9351 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl()) 9352 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common; 9353 } 9354 PendingDefinitions.clear(); 9355 9356 // Load the bodies of any functions or methods we've encountered. We do 9357 // this now (delayed) so that we can be sure that the declaration chains 9358 // have been fully wired up (hasBody relies on this). 9359 // FIXME: We shouldn't require complete redeclaration chains here. 9360 for (PendingBodiesMap::iterator PB = PendingBodies.begin(), 9361 PBEnd = PendingBodies.end(); 9362 PB != PBEnd; ++PB) { 9363 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) { 9364 // For a function defined inline within a class template, force the 9365 // canonical definition to be the one inside the canonical definition of 9366 // the template. This ensures that we instantiate from a correct view 9367 // of the template. 9368 // 9369 // Sadly we can't do this more generally: we can't be sure that all 9370 // copies of an arbitrary class definition will have the same members 9371 // defined (eg, some member functions may not be instantiated, and some 9372 // special members may or may not have been implicitly defined). 9373 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent())) 9374 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition()) 9375 continue; 9376 9377 // FIXME: Check for =delete/=default? 9378 // FIXME: Complain about ODR violations here? 9379 const FunctionDecl *Defn = nullptr; 9380 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) { 9381 FD->setLazyBody(PB->second); 9382 } else { 9383 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn); 9384 mergeDefinitionVisibility(NonConstDefn, FD); 9385 9386 if (!FD->isLateTemplateParsed() && 9387 !NonConstDefn->isLateTemplateParsed() && 9388 FD->getODRHash() != NonConstDefn->getODRHash()) { 9389 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9390 } 9391 } 9392 continue; 9393 } 9394 9395 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first); 9396 if (!getContext().getLangOpts().Modules || !MD->hasBody()) 9397 MD->setLazyBody(PB->second); 9398 } 9399 PendingBodies.clear(); 9400 9401 // Do some cleanup. 9402 for (auto *ND : PendingMergedDefinitionsToDeduplicate) 9403 getContext().deduplicateMergedDefinitonsFor(ND); 9404 PendingMergedDefinitionsToDeduplicate.clear(); 9405 } 9406 9407 void ASTReader::diagnoseOdrViolations() { 9408 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() && 9409 PendingFunctionOdrMergeFailures.empty()) 9410 return; 9411 9412 // Trigger the import of the full definition of each class that had any 9413 // odr-merging problems, so we can produce better diagnostics for them. 9414 // These updates may in turn find and diagnose some ODR failures, so take 9415 // ownership of the set first. 9416 auto OdrMergeFailures = std::move(PendingOdrMergeFailures); 9417 PendingOdrMergeFailures.clear(); 9418 for (auto &Merge : OdrMergeFailures) { 9419 Merge.first->buildLookup(); 9420 Merge.first->decls_begin(); 9421 Merge.first->bases_begin(); 9422 Merge.first->vbases_begin(); 9423 for (auto &RecordPair : Merge.second) { 9424 auto *RD = RecordPair.first; 9425 RD->decls_begin(); 9426 RD->bases_begin(); 9427 RD->vbases_begin(); 9428 } 9429 } 9430 9431 // Trigger the import of functions. 9432 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures); 9433 PendingFunctionOdrMergeFailures.clear(); 9434 for (auto &Merge : FunctionOdrMergeFailures) { 9435 Merge.first->buildLookup(); 9436 Merge.first->decls_begin(); 9437 Merge.first->getBody(); 9438 for (auto &FD : Merge.second) { 9439 FD->buildLookup(); 9440 FD->decls_begin(); 9441 FD->getBody(); 9442 } 9443 } 9444 9445 // For each declaration from a merged context, check that the canonical 9446 // definition of that context also contains a declaration of the same 9447 // entity. 9448 // 9449 // Caution: this loop does things that might invalidate iterators into 9450 // PendingOdrMergeChecks. Don't turn this into a range-based for loop! 9451 while (!PendingOdrMergeChecks.empty()) { 9452 NamedDecl *D = PendingOdrMergeChecks.pop_back_val(); 9453 9454 // FIXME: Skip over implicit declarations for now. This matters for things 9455 // like implicitly-declared special member functions. This isn't entirely 9456 // correct; we can end up with multiple unmerged declarations of the same 9457 // implicit entity. 9458 if (D->isImplicit()) 9459 continue; 9460 9461 DeclContext *CanonDef = D->getDeclContext(); 9462 9463 bool Found = false; 9464 const Decl *DCanon = D->getCanonicalDecl(); 9465 9466 for (auto RI : D->redecls()) { 9467 if (RI->getLexicalDeclContext() == CanonDef) { 9468 Found = true; 9469 break; 9470 } 9471 } 9472 if (Found) 9473 continue; 9474 9475 // Quick check failed, time to do the slow thing. Note, we can't just 9476 // look up the name of D in CanonDef here, because the member that is 9477 // in CanonDef might not be found by name lookup (it might have been 9478 // replaced by a more recent declaration in the lookup table), and we 9479 // can't necessarily find it in the redeclaration chain because it might 9480 // be merely mergeable, not redeclarable. 9481 llvm::SmallVector<const NamedDecl*, 4> Candidates; 9482 for (auto *CanonMember : CanonDef->decls()) { 9483 if (CanonMember->getCanonicalDecl() == DCanon) { 9484 // This can happen if the declaration is merely mergeable and not 9485 // actually redeclarable (we looked for redeclarations earlier). 9486 // 9487 // FIXME: We should be able to detect this more efficiently, without 9488 // pulling in all of the members of CanonDef. 9489 Found = true; 9490 break; 9491 } 9492 if (auto *ND = dyn_cast<NamedDecl>(CanonMember)) 9493 if (ND->getDeclName() == D->getDeclName()) 9494 Candidates.push_back(ND); 9495 } 9496 9497 if (!Found) { 9498 // The AST doesn't like TagDecls becoming invalid after they've been 9499 // completed. We only really need to mark FieldDecls as invalid here. 9500 if (!isa<TagDecl>(D)) 9501 D->setInvalidDecl(); 9502 9503 // Ensure we don't accidentally recursively enter deserialization while 9504 // we're producing our diagnostic. 9505 Deserializing RecursionGuard(this); 9506 9507 std::string CanonDefModule = 9508 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef)); 9509 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl) 9510 << D << getOwningModuleNameForDiagnostic(D) 9511 << CanonDef << CanonDefModule.empty() << CanonDefModule; 9512 9513 if (Candidates.empty()) 9514 Diag(cast<Decl>(CanonDef)->getLocation(), 9515 diag::note_module_odr_violation_no_possible_decls) << D; 9516 else { 9517 for (unsigned I = 0, N = Candidates.size(); I != N; ++I) 9518 Diag(Candidates[I]->getLocation(), 9519 diag::note_module_odr_violation_possible_decl) 9520 << Candidates[I]; 9521 } 9522 9523 DiagnosedOdrMergeFailures.insert(CanonDef); 9524 } 9525 } 9526 9527 if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty()) 9528 return; 9529 9530 // Ensure we don't accidentally recursively enter deserialization while 9531 // we're producing our diagnostics. 9532 Deserializing RecursionGuard(this); 9533 9534 // Common code for hashing helpers. 9535 ODRHash Hash; 9536 auto ComputeQualTypeODRHash = [&Hash](QualType Ty) { 9537 Hash.clear(); 9538 Hash.AddQualType(Ty); 9539 return Hash.CalculateHash(); 9540 }; 9541 9542 auto ComputeODRHash = [&Hash](const Stmt *S) { 9543 assert(S); 9544 Hash.clear(); 9545 Hash.AddStmt(S); 9546 return Hash.CalculateHash(); 9547 }; 9548 9549 auto ComputeSubDeclODRHash = [&Hash](const Decl *D) { 9550 assert(D); 9551 Hash.clear(); 9552 Hash.AddSubDecl(D); 9553 return Hash.CalculateHash(); 9554 }; 9555 9556 auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) { 9557 Hash.clear(); 9558 Hash.AddTemplateArgument(TA); 9559 return Hash.CalculateHash(); 9560 }; 9561 9562 auto ComputeTemplateParameterListODRHash = 9563 [&Hash](const TemplateParameterList *TPL) { 9564 assert(TPL); 9565 Hash.clear(); 9566 Hash.AddTemplateParameterList(TPL); 9567 return Hash.CalculateHash(); 9568 }; 9569 9570 // Issue any pending ODR-failure diagnostics. 9571 for (auto &Merge : OdrMergeFailures) { 9572 // If we've already pointed out a specific problem with this class, don't 9573 // bother issuing a general "something's different" diagnostic. 9574 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 9575 continue; 9576 9577 bool Diagnosed = false; 9578 CXXRecordDecl *FirstRecord = Merge.first; 9579 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord); 9580 for (auto &RecordPair : Merge.second) { 9581 CXXRecordDecl *SecondRecord = RecordPair.first; 9582 // Multiple different declarations got merged together; tell the user 9583 // where they came from. 9584 if (FirstRecord == SecondRecord) 9585 continue; 9586 9587 std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord); 9588 9589 auto *FirstDD = FirstRecord->DefinitionData; 9590 auto *SecondDD = RecordPair.second; 9591 9592 assert(FirstDD && SecondDD && "Definitions without DefinitionData"); 9593 9594 // Diagnostics from DefinitionData are emitted here. 9595 if (FirstDD != SecondDD) { 9596 enum ODRDefinitionDataDifference { 9597 NumBases, 9598 NumVBases, 9599 BaseType, 9600 BaseVirtual, 9601 BaseAccess, 9602 }; 9603 auto ODRDiagError = [FirstRecord, &FirstModule, 9604 this](SourceLocation Loc, SourceRange Range, 9605 ODRDefinitionDataDifference DiffType) { 9606 return Diag(Loc, diag::err_module_odr_violation_definition_data) 9607 << FirstRecord << FirstModule.empty() << FirstModule << Range 9608 << DiffType; 9609 }; 9610 auto ODRDiagNote = [&SecondModule, 9611 this](SourceLocation Loc, SourceRange Range, 9612 ODRDefinitionDataDifference DiffType) { 9613 return Diag(Loc, diag::note_module_odr_violation_definition_data) 9614 << SecondModule << Range << DiffType; 9615 }; 9616 9617 unsigned FirstNumBases = FirstDD->NumBases; 9618 unsigned FirstNumVBases = FirstDD->NumVBases; 9619 unsigned SecondNumBases = SecondDD->NumBases; 9620 unsigned SecondNumVBases = SecondDD->NumVBases; 9621 9622 auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) { 9623 unsigned NumBases = DD->NumBases; 9624 if (NumBases == 0) return SourceRange(); 9625 auto bases = DD->bases(); 9626 return SourceRange(bases[0].getLocStart(), 9627 bases[NumBases - 1].getLocEnd()); 9628 }; 9629 9630 if (FirstNumBases != SecondNumBases) { 9631 ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 9632 NumBases) 9633 << FirstNumBases; 9634 ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 9635 NumBases) 9636 << SecondNumBases; 9637 Diagnosed = true; 9638 break; 9639 } 9640 9641 if (FirstNumVBases != SecondNumVBases) { 9642 ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 9643 NumVBases) 9644 << FirstNumVBases; 9645 ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 9646 NumVBases) 9647 << SecondNumVBases; 9648 Diagnosed = true; 9649 break; 9650 } 9651 9652 auto FirstBases = FirstDD->bases(); 9653 auto SecondBases = SecondDD->bases(); 9654 unsigned i = 0; 9655 for (i = 0; i < FirstNumBases; ++i) { 9656 auto FirstBase = FirstBases[i]; 9657 auto SecondBase = SecondBases[i]; 9658 if (ComputeQualTypeODRHash(FirstBase.getType()) != 9659 ComputeQualTypeODRHash(SecondBase.getType())) { 9660 ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(), 9661 BaseType) 9662 << (i + 1) << FirstBase.getType(); 9663 ODRDiagNote(SecondRecord->getLocation(), 9664 SecondBase.getSourceRange(), BaseType) 9665 << (i + 1) << SecondBase.getType(); 9666 break; 9667 } 9668 9669 if (FirstBase.isVirtual() != SecondBase.isVirtual()) { 9670 ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(), 9671 BaseVirtual) 9672 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType(); 9673 ODRDiagNote(SecondRecord->getLocation(), 9674 SecondBase.getSourceRange(), BaseVirtual) 9675 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType(); 9676 break; 9677 } 9678 9679 if (FirstBase.getAccessSpecifierAsWritten() != 9680 SecondBase.getAccessSpecifierAsWritten()) { 9681 ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(), 9682 BaseAccess) 9683 << (i + 1) << FirstBase.getType() 9684 << (int)FirstBase.getAccessSpecifierAsWritten(); 9685 ODRDiagNote(SecondRecord->getLocation(), 9686 SecondBase.getSourceRange(), BaseAccess) 9687 << (i + 1) << SecondBase.getType() 9688 << (int)SecondBase.getAccessSpecifierAsWritten(); 9689 break; 9690 } 9691 } 9692 9693 if (i != FirstNumBases) { 9694 Diagnosed = true; 9695 break; 9696 } 9697 } 9698 9699 using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>; 9700 9701 const ClassTemplateDecl *FirstTemplate = 9702 FirstRecord->getDescribedClassTemplate(); 9703 const ClassTemplateDecl *SecondTemplate = 9704 SecondRecord->getDescribedClassTemplate(); 9705 9706 assert(!FirstTemplate == !SecondTemplate && 9707 "Both pointers should be null or non-null"); 9708 9709 enum ODRTemplateDifference { 9710 ParamEmptyName, 9711 ParamName, 9712 ParamSingleDefaultArgument, 9713 ParamDifferentDefaultArgument, 9714 }; 9715 9716 if (FirstTemplate && SecondTemplate) { 9717 DeclHashes FirstTemplateHashes; 9718 DeclHashes SecondTemplateHashes; 9719 9720 auto PopulateTemplateParameterHashs = 9721 [&ComputeSubDeclODRHash](DeclHashes &Hashes, 9722 const ClassTemplateDecl *TD) { 9723 for (auto *D : TD->getTemplateParameters()->asArray()) { 9724 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 9725 } 9726 }; 9727 9728 PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate); 9729 PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate); 9730 9731 assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() && 9732 "Number of template parameters should be equal."); 9733 9734 auto FirstIt = FirstTemplateHashes.begin(); 9735 auto FirstEnd = FirstTemplateHashes.end(); 9736 auto SecondIt = SecondTemplateHashes.begin(); 9737 for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) { 9738 if (FirstIt->second == SecondIt->second) 9739 continue; 9740 9741 auto ODRDiagError = [FirstRecord, &FirstModule, 9742 this](SourceLocation Loc, SourceRange Range, 9743 ODRTemplateDifference DiffType) { 9744 return Diag(Loc, diag::err_module_odr_violation_template_parameter) 9745 << FirstRecord << FirstModule.empty() << FirstModule << Range 9746 << DiffType; 9747 }; 9748 auto ODRDiagNote = [&SecondModule, 9749 this](SourceLocation Loc, SourceRange Range, 9750 ODRTemplateDifference DiffType) { 9751 return Diag(Loc, diag::note_module_odr_violation_template_parameter) 9752 << SecondModule << Range << DiffType; 9753 }; 9754 9755 const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first); 9756 const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first); 9757 9758 assert(FirstDecl->getKind() == SecondDecl->getKind() && 9759 "Parameter Decl's should be the same kind."); 9760 9761 DeclarationName FirstName = FirstDecl->getDeclName(); 9762 DeclarationName SecondName = SecondDecl->getDeclName(); 9763 9764 if (FirstName != SecondName) { 9765 const bool FirstNameEmpty = 9766 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo(); 9767 const bool SecondNameEmpty = 9768 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo(); 9769 assert((!FirstNameEmpty || !SecondNameEmpty) && 9770 "Both template parameters cannot be unnamed."); 9771 ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(), 9772 FirstNameEmpty ? ParamEmptyName : ParamName) 9773 << FirstName; 9774 ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(), 9775 SecondNameEmpty ? ParamEmptyName : ParamName) 9776 << SecondName; 9777 break; 9778 } 9779 9780 switch (FirstDecl->getKind()) { 9781 default: 9782 llvm_unreachable("Invalid template parameter type."); 9783 case Decl::TemplateTypeParm: { 9784 const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl); 9785 const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl); 9786 const bool HasFirstDefaultArgument = 9787 FirstParam->hasDefaultArgument() && 9788 !FirstParam->defaultArgumentWasInherited(); 9789 const bool HasSecondDefaultArgument = 9790 SecondParam->hasDefaultArgument() && 9791 !SecondParam->defaultArgumentWasInherited(); 9792 9793 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 9794 ODRDiagError(FirstDecl->getLocation(), 9795 FirstDecl->getSourceRange(), 9796 ParamSingleDefaultArgument) 9797 << HasFirstDefaultArgument; 9798 ODRDiagNote(SecondDecl->getLocation(), 9799 SecondDecl->getSourceRange(), 9800 ParamSingleDefaultArgument) 9801 << HasSecondDefaultArgument; 9802 break; 9803 } 9804 9805 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 9806 "Expecting default arguments."); 9807 9808 ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(), 9809 ParamDifferentDefaultArgument); 9810 ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(), 9811 ParamDifferentDefaultArgument); 9812 9813 break; 9814 } 9815 case Decl::NonTypeTemplateParm: { 9816 const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl); 9817 const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl); 9818 const bool HasFirstDefaultArgument = 9819 FirstParam->hasDefaultArgument() && 9820 !FirstParam->defaultArgumentWasInherited(); 9821 const bool HasSecondDefaultArgument = 9822 SecondParam->hasDefaultArgument() && 9823 !SecondParam->defaultArgumentWasInherited(); 9824 9825 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 9826 ODRDiagError(FirstDecl->getLocation(), 9827 FirstDecl->getSourceRange(), 9828 ParamSingleDefaultArgument) 9829 << HasFirstDefaultArgument; 9830 ODRDiagNote(SecondDecl->getLocation(), 9831 SecondDecl->getSourceRange(), 9832 ParamSingleDefaultArgument) 9833 << HasSecondDefaultArgument; 9834 break; 9835 } 9836 9837 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 9838 "Expecting default arguments."); 9839 9840 ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(), 9841 ParamDifferentDefaultArgument); 9842 ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(), 9843 ParamDifferentDefaultArgument); 9844 9845 break; 9846 } 9847 case Decl::TemplateTemplateParm: { 9848 const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl); 9849 const auto *SecondParam = 9850 cast<TemplateTemplateParmDecl>(SecondDecl); 9851 const bool HasFirstDefaultArgument = 9852 FirstParam->hasDefaultArgument() && 9853 !FirstParam->defaultArgumentWasInherited(); 9854 const bool HasSecondDefaultArgument = 9855 SecondParam->hasDefaultArgument() && 9856 !SecondParam->defaultArgumentWasInherited(); 9857 9858 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 9859 ODRDiagError(FirstDecl->getLocation(), 9860 FirstDecl->getSourceRange(), 9861 ParamSingleDefaultArgument) 9862 << HasFirstDefaultArgument; 9863 ODRDiagNote(SecondDecl->getLocation(), 9864 SecondDecl->getSourceRange(), 9865 ParamSingleDefaultArgument) 9866 << HasSecondDefaultArgument; 9867 break; 9868 } 9869 9870 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 9871 "Expecting default arguments."); 9872 9873 ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(), 9874 ParamDifferentDefaultArgument); 9875 ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(), 9876 ParamDifferentDefaultArgument); 9877 9878 break; 9879 } 9880 } 9881 9882 break; 9883 } 9884 9885 if (FirstIt != FirstEnd) { 9886 Diagnosed = true; 9887 break; 9888 } 9889 } 9890 9891 DeclHashes FirstHashes; 9892 DeclHashes SecondHashes; 9893 9894 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstRecord]( 9895 DeclHashes &Hashes, CXXRecordDecl *Record) { 9896 for (auto *D : Record->decls()) { 9897 // Due to decl merging, the first CXXRecordDecl is the parent of 9898 // Decls in both records. 9899 if (!ODRHash::isWhitelistedDecl(D, FirstRecord)) 9900 continue; 9901 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 9902 } 9903 }; 9904 PopulateHashes(FirstHashes, FirstRecord); 9905 PopulateHashes(SecondHashes, SecondRecord); 9906 9907 // Used with err_module_odr_violation_mismatch_decl and 9908 // note_module_odr_violation_mismatch_decl 9909 // This list should be the same Decl's as in ODRHash::isWhiteListedDecl 9910 enum { 9911 EndOfClass, 9912 PublicSpecifer, 9913 PrivateSpecifer, 9914 ProtectedSpecifer, 9915 StaticAssert, 9916 Field, 9917 CXXMethod, 9918 TypeAlias, 9919 TypeDef, 9920 Var, 9921 Friend, 9922 FunctionTemplate, 9923 Other 9924 } FirstDiffType = Other, 9925 SecondDiffType = Other; 9926 9927 auto DifferenceSelector = [](Decl *D) { 9928 assert(D && "valid Decl required"); 9929 switch (D->getKind()) { 9930 default: 9931 return Other; 9932 case Decl::AccessSpec: 9933 switch (D->getAccess()) { 9934 case AS_public: 9935 return PublicSpecifer; 9936 case AS_private: 9937 return PrivateSpecifer; 9938 case AS_protected: 9939 return ProtectedSpecifer; 9940 case AS_none: 9941 break; 9942 } 9943 llvm_unreachable("Invalid access specifier"); 9944 case Decl::StaticAssert: 9945 return StaticAssert; 9946 case Decl::Field: 9947 return Field; 9948 case Decl::CXXMethod: 9949 case Decl::CXXConstructor: 9950 case Decl::CXXDestructor: 9951 return CXXMethod; 9952 case Decl::TypeAlias: 9953 return TypeAlias; 9954 case Decl::Typedef: 9955 return TypeDef; 9956 case Decl::Var: 9957 return Var; 9958 case Decl::Friend: 9959 return Friend; 9960 case Decl::FunctionTemplate: 9961 return FunctionTemplate; 9962 } 9963 }; 9964 9965 Decl *FirstDecl = nullptr; 9966 Decl *SecondDecl = nullptr; 9967 auto FirstIt = FirstHashes.begin(); 9968 auto SecondIt = SecondHashes.begin(); 9969 9970 // If there is a diagnoseable difference, FirstDiffType and 9971 // SecondDiffType will not be Other and FirstDecl and SecondDecl will be 9972 // filled in if not EndOfClass. 9973 while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) { 9974 if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() && 9975 FirstIt->second == SecondIt->second) { 9976 ++FirstIt; 9977 ++SecondIt; 9978 continue; 9979 } 9980 9981 FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first; 9982 SecondDecl = SecondIt == SecondHashes.end() ? nullptr : SecondIt->first; 9983 9984 FirstDiffType = FirstDecl ? DifferenceSelector(FirstDecl) : EndOfClass; 9985 SecondDiffType = 9986 SecondDecl ? DifferenceSelector(SecondDecl) : EndOfClass; 9987 9988 break; 9989 } 9990 9991 if (FirstDiffType == Other || SecondDiffType == Other) { 9992 // Reaching this point means an unexpected Decl was encountered 9993 // or no difference was detected. This causes a generic error 9994 // message to be emitted. 9995 Diag(FirstRecord->getLocation(), 9996 diag::err_module_odr_violation_different_definitions) 9997 << FirstRecord << FirstModule.empty() << FirstModule; 9998 9999 if (FirstDecl) { 10000 Diag(FirstDecl->getLocation(), diag::note_first_module_difference) 10001 << FirstRecord << FirstDecl->getSourceRange(); 10002 } 10003 10004 Diag(SecondRecord->getLocation(), 10005 diag::note_module_odr_violation_different_definitions) 10006 << SecondModule; 10007 10008 if (SecondDecl) { 10009 Diag(SecondDecl->getLocation(), diag::note_second_module_difference) 10010 << SecondDecl->getSourceRange(); 10011 } 10012 10013 Diagnosed = true; 10014 break; 10015 } 10016 10017 if (FirstDiffType != SecondDiffType) { 10018 SourceLocation FirstLoc; 10019 SourceRange FirstRange; 10020 if (FirstDiffType == EndOfClass) { 10021 FirstLoc = FirstRecord->getBraceRange().getEnd(); 10022 } else { 10023 FirstLoc = FirstIt->first->getLocation(); 10024 FirstRange = FirstIt->first->getSourceRange(); 10025 } 10026 Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl) 10027 << FirstRecord << FirstModule.empty() << FirstModule << FirstRange 10028 << FirstDiffType; 10029 10030 SourceLocation SecondLoc; 10031 SourceRange SecondRange; 10032 if (SecondDiffType == EndOfClass) { 10033 SecondLoc = SecondRecord->getBraceRange().getEnd(); 10034 } else { 10035 SecondLoc = SecondDecl->getLocation(); 10036 SecondRange = SecondDecl->getSourceRange(); 10037 } 10038 Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl) 10039 << SecondModule << SecondRange << SecondDiffType; 10040 Diagnosed = true; 10041 break; 10042 } 10043 10044 assert(FirstDiffType == SecondDiffType); 10045 10046 // Used with err_module_odr_violation_mismatch_decl_diff and 10047 // note_module_odr_violation_mismatch_decl_diff 10048 enum ODRDeclDifference { 10049 StaticAssertCondition, 10050 StaticAssertMessage, 10051 StaticAssertOnlyMessage, 10052 FieldName, 10053 FieldTypeName, 10054 FieldSingleBitField, 10055 FieldDifferentWidthBitField, 10056 FieldSingleMutable, 10057 FieldSingleInitializer, 10058 FieldDifferentInitializers, 10059 MethodName, 10060 MethodDeleted, 10061 MethodVirtual, 10062 MethodStatic, 10063 MethodVolatile, 10064 MethodConst, 10065 MethodInline, 10066 MethodNumberParameters, 10067 MethodParameterType, 10068 MethodParameterName, 10069 MethodParameterSingleDefaultArgument, 10070 MethodParameterDifferentDefaultArgument, 10071 MethodNoTemplateArguments, 10072 MethodDifferentNumberTemplateArguments, 10073 MethodDifferentTemplateArgument, 10074 TypedefName, 10075 TypedefType, 10076 VarName, 10077 VarType, 10078 VarSingleInitializer, 10079 VarDifferentInitializer, 10080 VarConstexpr, 10081 FriendTypeFunction, 10082 FriendType, 10083 FriendFunction, 10084 FunctionTemplateDifferentNumberParameters, 10085 FunctionTemplateParameterDifferentKind, 10086 FunctionTemplateParameterName, 10087 FunctionTemplateParameterSingleDefaultArgument, 10088 FunctionTemplateParameterDifferentDefaultArgument, 10089 FunctionTemplateParameterDifferentType, 10090 FunctionTemplatePackParameter, 10091 }; 10092 10093 // These lambdas have the common portions of the ODR diagnostics. This 10094 // has the same return as Diag(), so addition parameters can be passed 10095 // in with operator<< 10096 auto ODRDiagError = [FirstRecord, &FirstModule, this]( 10097 SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) { 10098 return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff) 10099 << FirstRecord << FirstModule.empty() << FirstModule << Range 10100 << DiffType; 10101 }; 10102 auto ODRDiagNote = [&SecondModule, this]( 10103 SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) { 10104 return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff) 10105 << SecondModule << Range << DiffType; 10106 }; 10107 10108 switch (FirstDiffType) { 10109 case Other: 10110 case EndOfClass: 10111 case PublicSpecifer: 10112 case PrivateSpecifer: 10113 case ProtectedSpecifer: 10114 llvm_unreachable("Invalid diff type"); 10115 10116 case StaticAssert: { 10117 StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl); 10118 StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl); 10119 10120 Expr *FirstExpr = FirstSA->getAssertExpr(); 10121 Expr *SecondExpr = SecondSA->getAssertExpr(); 10122 unsigned FirstODRHash = ComputeODRHash(FirstExpr); 10123 unsigned SecondODRHash = ComputeODRHash(SecondExpr); 10124 if (FirstODRHash != SecondODRHash) { 10125 ODRDiagError(FirstExpr->getLocStart(), FirstExpr->getSourceRange(), 10126 StaticAssertCondition); 10127 ODRDiagNote(SecondExpr->getLocStart(), 10128 SecondExpr->getSourceRange(), StaticAssertCondition); 10129 Diagnosed = true; 10130 break; 10131 } 10132 10133 StringLiteral *FirstStr = FirstSA->getMessage(); 10134 StringLiteral *SecondStr = SecondSA->getMessage(); 10135 assert((FirstStr || SecondStr) && "Both messages cannot be empty"); 10136 if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) { 10137 SourceLocation FirstLoc, SecondLoc; 10138 SourceRange FirstRange, SecondRange; 10139 if (FirstStr) { 10140 FirstLoc = FirstStr->getLocStart(); 10141 FirstRange = FirstStr->getSourceRange(); 10142 } else { 10143 FirstLoc = FirstSA->getLocStart(); 10144 FirstRange = FirstSA->getSourceRange(); 10145 } 10146 if (SecondStr) { 10147 SecondLoc = SecondStr->getLocStart(); 10148 SecondRange = SecondStr->getSourceRange(); 10149 } else { 10150 SecondLoc = SecondSA->getLocStart(); 10151 SecondRange = SecondSA->getSourceRange(); 10152 } 10153 ODRDiagError(FirstLoc, FirstRange, StaticAssertOnlyMessage) 10154 << (FirstStr == nullptr); 10155 ODRDiagNote(SecondLoc, SecondRange, StaticAssertOnlyMessage) 10156 << (SecondStr == nullptr); 10157 Diagnosed = true; 10158 break; 10159 } 10160 10161 if (FirstStr && SecondStr && 10162 FirstStr->getString() != SecondStr->getString()) { 10163 ODRDiagError(FirstStr->getLocStart(), FirstStr->getSourceRange(), 10164 StaticAssertMessage); 10165 ODRDiagNote(SecondStr->getLocStart(), SecondStr->getSourceRange(), 10166 StaticAssertMessage); 10167 Diagnosed = true; 10168 break; 10169 } 10170 break; 10171 } 10172 case Field: { 10173 FieldDecl *FirstField = cast<FieldDecl>(FirstDecl); 10174 FieldDecl *SecondField = cast<FieldDecl>(SecondDecl); 10175 IdentifierInfo *FirstII = FirstField->getIdentifier(); 10176 IdentifierInfo *SecondII = SecondField->getIdentifier(); 10177 if (FirstII->getName() != SecondII->getName()) { 10178 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(), 10179 FieldName) 10180 << FirstII; 10181 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(), 10182 FieldName) 10183 << SecondII; 10184 10185 Diagnosed = true; 10186 break; 10187 } 10188 10189 assert(getContext().hasSameType(FirstField->getType(), 10190 SecondField->getType())); 10191 10192 QualType FirstType = FirstField->getType(); 10193 QualType SecondType = SecondField->getType(); 10194 if (ComputeQualTypeODRHash(FirstType) != 10195 ComputeQualTypeODRHash(SecondType)) { 10196 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(), 10197 FieldTypeName) 10198 << FirstII << FirstType; 10199 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(), 10200 FieldTypeName) 10201 << SecondII << SecondType; 10202 10203 Diagnosed = true; 10204 break; 10205 } 10206 10207 const bool IsFirstBitField = FirstField->isBitField(); 10208 const bool IsSecondBitField = SecondField->isBitField(); 10209 if (IsFirstBitField != IsSecondBitField) { 10210 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(), 10211 FieldSingleBitField) 10212 << FirstII << IsFirstBitField; 10213 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(), 10214 FieldSingleBitField) 10215 << SecondII << IsSecondBitField; 10216 Diagnosed = true; 10217 break; 10218 } 10219 10220 if (IsFirstBitField && IsSecondBitField) { 10221 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(), 10222 FieldDifferentWidthBitField) 10223 << FirstII << FirstField->getBitWidth()->getSourceRange(); 10224 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(), 10225 FieldDifferentWidthBitField) 10226 << SecondII << SecondField->getBitWidth()->getSourceRange(); 10227 Diagnosed = true; 10228 break; 10229 } 10230 10231 const bool IsFirstMutable = FirstField->isMutable(); 10232 const bool IsSecondMutable = SecondField->isMutable(); 10233 if (IsFirstMutable != IsSecondMutable) { 10234 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(), 10235 FieldSingleMutable) 10236 << FirstII << IsFirstMutable; 10237 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(), 10238 FieldSingleMutable) 10239 << SecondII << IsSecondMutable; 10240 Diagnosed = true; 10241 break; 10242 } 10243 10244 const Expr *FirstInitializer = FirstField->getInClassInitializer(); 10245 const Expr *SecondInitializer = SecondField->getInClassInitializer(); 10246 if ((!FirstInitializer && SecondInitializer) || 10247 (FirstInitializer && !SecondInitializer)) { 10248 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(), 10249 FieldSingleInitializer) 10250 << FirstII << (FirstInitializer != nullptr); 10251 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(), 10252 FieldSingleInitializer) 10253 << SecondII << (SecondInitializer != nullptr); 10254 Diagnosed = true; 10255 break; 10256 } 10257 10258 if (FirstInitializer && SecondInitializer) { 10259 unsigned FirstInitHash = ComputeODRHash(FirstInitializer); 10260 unsigned SecondInitHash = ComputeODRHash(SecondInitializer); 10261 if (FirstInitHash != SecondInitHash) { 10262 ODRDiagError(FirstField->getLocation(), 10263 FirstField->getSourceRange(), 10264 FieldDifferentInitializers) 10265 << FirstII << FirstInitializer->getSourceRange(); 10266 ODRDiagNote(SecondField->getLocation(), 10267 SecondField->getSourceRange(), 10268 FieldDifferentInitializers) 10269 << SecondII << SecondInitializer->getSourceRange(); 10270 Diagnosed = true; 10271 break; 10272 } 10273 } 10274 10275 break; 10276 } 10277 case CXXMethod: { 10278 enum { 10279 DiagMethod, 10280 DiagConstructor, 10281 DiagDestructor, 10282 } FirstMethodType, 10283 SecondMethodType; 10284 auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) { 10285 if (isa<CXXConstructorDecl>(D)) return DiagConstructor; 10286 if (isa<CXXDestructorDecl>(D)) return DiagDestructor; 10287 return DiagMethod; 10288 }; 10289 const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl); 10290 const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl); 10291 FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod); 10292 SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod); 10293 auto FirstName = FirstMethod->getDeclName(); 10294 auto SecondName = SecondMethod->getDeclName(); 10295 if (FirstMethodType != SecondMethodType || FirstName != SecondName) { 10296 ODRDiagError(FirstMethod->getLocation(), 10297 FirstMethod->getSourceRange(), MethodName) 10298 << FirstMethodType << FirstName; 10299 ODRDiagNote(SecondMethod->getLocation(), 10300 SecondMethod->getSourceRange(), MethodName) 10301 << SecondMethodType << SecondName; 10302 10303 Diagnosed = true; 10304 break; 10305 } 10306 10307 const bool FirstDeleted = FirstMethod->isDeleted(); 10308 const bool SecondDeleted = SecondMethod->isDeleted(); 10309 if (FirstDeleted != SecondDeleted) { 10310 ODRDiagError(FirstMethod->getLocation(), 10311 FirstMethod->getSourceRange(), MethodDeleted) 10312 << FirstMethodType << FirstName << FirstDeleted; 10313 10314 ODRDiagNote(SecondMethod->getLocation(), 10315 SecondMethod->getSourceRange(), MethodDeleted) 10316 << SecondMethodType << SecondName << SecondDeleted; 10317 Diagnosed = true; 10318 break; 10319 } 10320 10321 const bool FirstVirtual = FirstMethod->isVirtualAsWritten(); 10322 const bool SecondVirtual = SecondMethod->isVirtualAsWritten(); 10323 const bool FirstPure = FirstMethod->isPure(); 10324 const bool SecondPure = SecondMethod->isPure(); 10325 if ((FirstVirtual || SecondVirtual) && 10326 (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) { 10327 ODRDiagError(FirstMethod->getLocation(), 10328 FirstMethod->getSourceRange(), MethodVirtual) 10329 << FirstMethodType << FirstName << FirstPure << FirstVirtual; 10330 ODRDiagNote(SecondMethod->getLocation(), 10331 SecondMethod->getSourceRange(), MethodVirtual) 10332 << SecondMethodType << SecondName << SecondPure << SecondVirtual; 10333 Diagnosed = true; 10334 break; 10335 } 10336 10337 // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging, 10338 // FirstDecl is the canonical Decl of SecondDecl, so the storage 10339 // class needs to be checked instead. 10340 const auto FirstStorage = FirstMethod->getStorageClass(); 10341 const auto SecondStorage = SecondMethod->getStorageClass(); 10342 const bool FirstStatic = FirstStorage == SC_Static; 10343 const bool SecondStatic = SecondStorage == SC_Static; 10344 if (FirstStatic != SecondStatic) { 10345 ODRDiagError(FirstMethod->getLocation(), 10346 FirstMethod->getSourceRange(), MethodStatic) 10347 << FirstMethodType << FirstName << FirstStatic; 10348 ODRDiagNote(SecondMethod->getLocation(), 10349 SecondMethod->getSourceRange(), MethodStatic) 10350 << SecondMethodType << SecondName << SecondStatic; 10351 Diagnosed = true; 10352 break; 10353 } 10354 10355 const bool FirstVolatile = FirstMethod->isVolatile(); 10356 const bool SecondVolatile = SecondMethod->isVolatile(); 10357 if (FirstVolatile != SecondVolatile) { 10358 ODRDiagError(FirstMethod->getLocation(), 10359 FirstMethod->getSourceRange(), MethodVolatile) 10360 << FirstMethodType << FirstName << FirstVolatile; 10361 ODRDiagNote(SecondMethod->getLocation(), 10362 SecondMethod->getSourceRange(), MethodVolatile) 10363 << SecondMethodType << SecondName << SecondVolatile; 10364 Diagnosed = true; 10365 break; 10366 } 10367 10368 const bool FirstConst = FirstMethod->isConst(); 10369 const bool SecondConst = SecondMethod->isConst(); 10370 if (FirstConst != SecondConst) { 10371 ODRDiagError(FirstMethod->getLocation(), 10372 FirstMethod->getSourceRange(), MethodConst) 10373 << FirstMethodType << FirstName << FirstConst; 10374 ODRDiagNote(SecondMethod->getLocation(), 10375 SecondMethod->getSourceRange(), MethodConst) 10376 << SecondMethodType << SecondName << SecondConst; 10377 Diagnosed = true; 10378 break; 10379 } 10380 10381 const bool FirstInline = FirstMethod->isInlineSpecified(); 10382 const bool SecondInline = SecondMethod->isInlineSpecified(); 10383 if (FirstInline != SecondInline) { 10384 ODRDiagError(FirstMethod->getLocation(), 10385 FirstMethod->getSourceRange(), MethodInline) 10386 << FirstMethodType << FirstName << FirstInline; 10387 ODRDiagNote(SecondMethod->getLocation(), 10388 SecondMethod->getSourceRange(), MethodInline) 10389 << SecondMethodType << SecondName << SecondInline; 10390 Diagnosed = true; 10391 break; 10392 } 10393 10394 const unsigned FirstNumParameters = FirstMethod->param_size(); 10395 const unsigned SecondNumParameters = SecondMethod->param_size(); 10396 if (FirstNumParameters != SecondNumParameters) { 10397 ODRDiagError(FirstMethod->getLocation(), 10398 FirstMethod->getSourceRange(), MethodNumberParameters) 10399 << FirstMethodType << FirstName << FirstNumParameters; 10400 ODRDiagNote(SecondMethod->getLocation(), 10401 SecondMethod->getSourceRange(), MethodNumberParameters) 10402 << SecondMethodType << SecondName << SecondNumParameters; 10403 Diagnosed = true; 10404 break; 10405 } 10406 10407 // Need this status boolean to know when break out of the switch. 10408 bool ParameterMismatch = false; 10409 for (unsigned I = 0; I < FirstNumParameters; ++I) { 10410 const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I); 10411 const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I); 10412 10413 QualType FirstParamType = FirstParam->getType(); 10414 QualType SecondParamType = SecondParam->getType(); 10415 if (FirstParamType != SecondParamType && 10416 ComputeQualTypeODRHash(FirstParamType) != 10417 ComputeQualTypeODRHash(SecondParamType)) { 10418 if (const DecayedType *ParamDecayedType = 10419 FirstParamType->getAs<DecayedType>()) { 10420 ODRDiagError(FirstMethod->getLocation(), 10421 FirstMethod->getSourceRange(), MethodParameterType) 10422 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10423 << true << ParamDecayedType->getOriginalType(); 10424 } else { 10425 ODRDiagError(FirstMethod->getLocation(), 10426 FirstMethod->getSourceRange(), MethodParameterType) 10427 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10428 << false; 10429 } 10430 10431 if (const DecayedType *ParamDecayedType = 10432 SecondParamType->getAs<DecayedType>()) { 10433 ODRDiagNote(SecondMethod->getLocation(), 10434 SecondMethod->getSourceRange(), MethodParameterType) 10435 << SecondMethodType << SecondName << (I + 1) 10436 << SecondParamType << true 10437 << ParamDecayedType->getOriginalType(); 10438 } else { 10439 ODRDiagNote(SecondMethod->getLocation(), 10440 SecondMethod->getSourceRange(), MethodParameterType) 10441 << SecondMethodType << SecondName << (I + 1) 10442 << SecondParamType << false; 10443 } 10444 ParameterMismatch = true; 10445 break; 10446 } 10447 10448 DeclarationName FirstParamName = FirstParam->getDeclName(); 10449 DeclarationName SecondParamName = SecondParam->getDeclName(); 10450 if (FirstParamName != SecondParamName) { 10451 ODRDiagError(FirstMethod->getLocation(), 10452 FirstMethod->getSourceRange(), MethodParameterName) 10453 << FirstMethodType << FirstName << (I + 1) << FirstParamName; 10454 ODRDiagNote(SecondMethod->getLocation(), 10455 SecondMethod->getSourceRange(), MethodParameterName) 10456 << SecondMethodType << SecondName << (I + 1) << SecondParamName; 10457 ParameterMismatch = true; 10458 break; 10459 } 10460 10461 const Expr *FirstInit = FirstParam->getInit(); 10462 const Expr *SecondInit = SecondParam->getInit(); 10463 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 10464 ODRDiagError(FirstMethod->getLocation(), 10465 FirstMethod->getSourceRange(), 10466 MethodParameterSingleDefaultArgument) 10467 << FirstMethodType << FirstName << (I + 1) 10468 << (FirstInit == nullptr) 10469 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 10470 ODRDiagNote(SecondMethod->getLocation(), 10471 SecondMethod->getSourceRange(), 10472 MethodParameterSingleDefaultArgument) 10473 << SecondMethodType << SecondName << (I + 1) 10474 << (SecondInit == nullptr) 10475 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 10476 ParameterMismatch = true; 10477 break; 10478 } 10479 10480 if (FirstInit && SecondInit && 10481 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 10482 ODRDiagError(FirstMethod->getLocation(), 10483 FirstMethod->getSourceRange(), 10484 MethodParameterDifferentDefaultArgument) 10485 << FirstMethodType << FirstName << (I + 1) 10486 << FirstInit->getSourceRange(); 10487 ODRDiagNote(SecondMethod->getLocation(), 10488 SecondMethod->getSourceRange(), 10489 MethodParameterDifferentDefaultArgument) 10490 << SecondMethodType << SecondName << (I + 1) 10491 << SecondInit->getSourceRange(); 10492 ParameterMismatch = true; 10493 break; 10494 10495 } 10496 } 10497 10498 if (ParameterMismatch) { 10499 Diagnosed = true; 10500 break; 10501 } 10502 10503 const auto *FirstTemplateArgs = 10504 FirstMethod->getTemplateSpecializationArgs(); 10505 const auto *SecondTemplateArgs = 10506 SecondMethod->getTemplateSpecializationArgs(); 10507 10508 if ((FirstTemplateArgs && !SecondTemplateArgs) || 10509 (!FirstTemplateArgs && SecondTemplateArgs)) { 10510 ODRDiagError(FirstMethod->getLocation(), 10511 FirstMethod->getSourceRange(), MethodNoTemplateArguments) 10512 << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr); 10513 ODRDiagNote(SecondMethod->getLocation(), 10514 SecondMethod->getSourceRange(), MethodNoTemplateArguments) 10515 << SecondMethodType << SecondName 10516 << (SecondTemplateArgs != nullptr); 10517 10518 Diagnosed = true; 10519 break; 10520 } 10521 10522 if (FirstTemplateArgs && SecondTemplateArgs) { 10523 // Remove pack expansions from argument list. 10524 auto ExpandTemplateArgumentList = 10525 [](const TemplateArgumentList *TAL) { 10526 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList; 10527 for (const TemplateArgument &TA : TAL->asArray()) { 10528 if (TA.getKind() != TemplateArgument::Pack) { 10529 ExpandedList.push_back(&TA); 10530 continue; 10531 } 10532 for (const TemplateArgument &PackTA : TA.getPackAsArray()) { 10533 ExpandedList.push_back(&PackTA); 10534 } 10535 } 10536 return ExpandedList; 10537 }; 10538 llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList = 10539 ExpandTemplateArgumentList(FirstTemplateArgs); 10540 llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList = 10541 ExpandTemplateArgumentList(SecondTemplateArgs); 10542 10543 if (FirstExpandedList.size() != SecondExpandedList.size()) { 10544 ODRDiagError(FirstMethod->getLocation(), 10545 FirstMethod->getSourceRange(), 10546 MethodDifferentNumberTemplateArguments) 10547 << FirstMethodType << FirstName 10548 << (unsigned)FirstExpandedList.size(); 10549 ODRDiagNote(SecondMethod->getLocation(), 10550 SecondMethod->getSourceRange(), 10551 MethodDifferentNumberTemplateArguments) 10552 << SecondMethodType << SecondName 10553 << (unsigned)SecondExpandedList.size(); 10554 10555 Diagnosed = true; 10556 break; 10557 } 10558 10559 bool TemplateArgumentMismatch = false; 10560 for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) { 10561 const TemplateArgument &FirstTA = *FirstExpandedList[i], 10562 &SecondTA = *SecondExpandedList[i]; 10563 if (ComputeTemplateArgumentODRHash(FirstTA) == 10564 ComputeTemplateArgumentODRHash(SecondTA)) { 10565 continue; 10566 } 10567 10568 ODRDiagError(FirstMethod->getLocation(), 10569 FirstMethod->getSourceRange(), 10570 MethodDifferentTemplateArgument) 10571 << FirstMethodType << FirstName << FirstTA << i + 1; 10572 ODRDiagNote(SecondMethod->getLocation(), 10573 SecondMethod->getSourceRange(), 10574 MethodDifferentTemplateArgument) 10575 << SecondMethodType << SecondName << SecondTA << i + 1; 10576 10577 TemplateArgumentMismatch = true; 10578 break; 10579 } 10580 10581 if (TemplateArgumentMismatch) { 10582 Diagnosed = true; 10583 break; 10584 } 10585 } 10586 break; 10587 } 10588 case TypeAlias: 10589 case TypeDef: { 10590 TypedefNameDecl *FirstTD = cast<TypedefNameDecl>(FirstDecl); 10591 TypedefNameDecl *SecondTD = cast<TypedefNameDecl>(SecondDecl); 10592 auto FirstName = FirstTD->getDeclName(); 10593 auto SecondName = SecondTD->getDeclName(); 10594 if (FirstName != SecondName) { 10595 ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(), 10596 TypedefName) 10597 << (FirstDiffType == TypeAlias) << FirstName; 10598 ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(), 10599 TypedefName) 10600 << (FirstDiffType == TypeAlias) << SecondName; 10601 Diagnosed = true; 10602 break; 10603 } 10604 10605 QualType FirstType = FirstTD->getUnderlyingType(); 10606 QualType SecondType = SecondTD->getUnderlyingType(); 10607 if (ComputeQualTypeODRHash(FirstType) != 10608 ComputeQualTypeODRHash(SecondType)) { 10609 ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(), 10610 TypedefType) 10611 << (FirstDiffType == TypeAlias) << FirstName << FirstType; 10612 ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(), 10613 TypedefType) 10614 << (FirstDiffType == TypeAlias) << SecondName << SecondType; 10615 Diagnosed = true; 10616 break; 10617 } 10618 break; 10619 } 10620 case Var: { 10621 VarDecl *FirstVD = cast<VarDecl>(FirstDecl); 10622 VarDecl *SecondVD = cast<VarDecl>(SecondDecl); 10623 auto FirstName = FirstVD->getDeclName(); 10624 auto SecondName = SecondVD->getDeclName(); 10625 if (FirstName != SecondName) { 10626 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(), 10627 VarName) 10628 << FirstName; 10629 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(), 10630 VarName) 10631 << SecondName; 10632 Diagnosed = true; 10633 break; 10634 } 10635 10636 QualType FirstType = FirstVD->getType(); 10637 QualType SecondType = SecondVD->getType(); 10638 if (ComputeQualTypeODRHash(FirstType) != 10639 ComputeQualTypeODRHash(SecondType)) { 10640 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(), 10641 VarType) 10642 << FirstName << FirstType; 10643 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(), 10644 VarType) 10645 << SecondName << SecondType; 10646 Diagnosed = true; 10647 break; 10648 } 10649 10650 const Expr *FirstInit = FirstVD->getInit(); 10651 const Expr *SecondInit = SecondVD->getInit(); 10652 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 10653 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(), 10654 VarSingleInitializer) 10655 << FirstName << (FirstInit == nullptr) 10656 << (FirstInit ? FirstInit->getSourceRange(): SourceRange()); 10657 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(), 10658 VarSingleInitializer) 10659 << SecondName << (SecondInit == nullptr) 10660 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 10661 Diagnosed = true; 10662 break; 10663 } 10664 10665 if (FirstInit && SecondInit && 10666 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 10667 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(), 10668 VarDifferentInitializer) 10669 << FirstName << FirstInit->getSourceRange(); 10670 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(), 10671 VarDifferentInitializer) 10672 << SecondName << SecondInit->getSourceRange(); 10673 Diagnosed = true; 10674 break; 10675 } 10676 10677 const bool FirstIsConstexpr = FirstVD->isConstexpr(); 10678 const bool SecondIsConstexpr = SecondVD->isConstexpr(); 10679 if (FirstIsConstexpr != SecondIsConstexpr) { 10680 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(), 10681 VarConstexpr) 10682 << FirstName << FirstIsConstexpr; 10683 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(), 10684 VarConstexpr) 10685 << SecondName << SecondIsConstexpr; 10686 Diagnosed = true; 10687 break; 10688 } 10689 break; 10690 } 10691 case Friend: { 10692 FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl); 10693 FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl); 10694 10695 NamedDecl *FirstND = FirstFriend->getFriendDecl(); 10696 NamedDecl *SecondND = SecondFriend->getFriendDecl(); 10697 10698 TypeSourceInfo *FirstTSI = FirstFriend->getFriendType(); 10699 TypeSourceInfo *SecondTSI = SecondFriend->getFriendType(); 10700 10701 if (FirstND && SecondND) { 10702 ODRDiagError(FirstFriend->getFriendLoc(), 10703 FirstFriend->getSourceRange(), FriendFunction) 10704 << FirstND; 10705 ODRDiagNote(SecondFriend->getFriendLoc(), 10706 SecondFriend->getSourceRange(), FriendFunction) 10707 << SecondND; 10708 10709 Diagnosed = true; 10710 break; 10711 } 10712 10713 if (FirstTSI && SecondTSI) { 10714 QualType FirstFriendType = FirstTSI->getType(); 10715 QualType SecondFriendType = SecondTSI->getType(); 10716 assert(ComputeQualTypeODRHash(FirstFriendType) != 10717 ComputeQualTypeODRHash(SecondFriendType)); 10718 ODRDiagError(FirstFriend->getFriendLoc(), 10719 FirstFriend->getSourceRange(), FriendType) 10720 << FirstFriendType; 10721 ODRDiagNote(SecondFriend->getFriendLoc(), 10722 SecondFriend->getSourceRange(), FriendType) 10723 << SecondFriendType; 10724 Diagnosed = true; 10725 break; 10726 } 10727 10728 ODRDiagError(FirstFriend->getFriendLoc(), FirstFriend->getSourceRange(), 10729 FriendTypeFunction) 10730 << (FirstTSI == nullptr); 10731 ODRDiagNote(SecondFriend->getFriendLoc(), 10732 SecondFriend->getSourceRange(), FriendTypeFunction) 10733 << (SecondTSI == nullptr); 10734 10735 Diagnosed = true; 10736 break; 10737 } 10738 case FunctionTemplate: { 10739 FunctionTemplateDecl *FirstTemplate = 10740 cast<FunctionTemplateDecl>(FirstDecl); 10741 FunctionTemplateDecl *SecondTemplate = 10742 cast<FunctionTemplateDecl>(SecondDecl); 10743 10744 TemplateParameterList *FirstTPL = 10745 FirstTemplate->getTemplateParameters(); 10746 TemplateParameterList *SecondTPL = 10747 SecondTemplate->getTemplateParameters(); 10748 10749 if (FirstTPL->size() != SecondTPL->size()) { 10750 ODRDiagError(FirstTemplate->getLocation(), 10751 FirstTemplate->getSourceRange(), 10752 FunctionTemplateDifferentNumberParameters) 10753 << FirstTemplate << FirstTPL->size(); 10754 ODRDiagNote(SecondTemplate->getLocation(), 10755 SecondTemplate->getSourceRange(), 10756 FunctionTemplateDifferentNumberParameters) 10757 << SecondTemplate << SecondTPL->size(); 10758 10759 Diagnosed = true; 10760 break; 10761 } 10762 10763 bool ParameterMismatch = false; 10764 for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) { 10765 NamedDecl *FirstParam = FirstTPL->getParam(i); 10766 NamedDecl *SecondParam = SecondTPL->getParam(i); 10767 10768 if (FirstParam->getKind() != SecondParam->getKind()) { 10769 enum { 10770 TemplateTypeParameter, 10771 NonTypeTemplateParameter, 10772 TemplateTemplateParameter, 10773 }; 10774 auto GetParamType = [](NamedDecl *D) { 10775 switch (D->getKind()) { 10776 default: 10777 llvm_unreachable("Unexpected template parameter type"); 10778 case Decl::TemplateTypeParm: 10779 return TemplateTypeParameter; 10780 case Decl::NonTypeTemplateParm: 10781 return NonTypeTemplateParameter; 10782 case Decl::TemplateTemplateParm: 10783 return TemplateTemplateParameter; 10784 } 10785 }; 10786 10787 ODRDiagError(FirstTemplate->getLocation(), 10788 FirstTemplate->getSourceRange(), 10789 FunctionTemplateParameterDifferentKind) 10790 << FirstTemplate << (i + 1) << GetParamType(FirstParam); 10791 ODRDiagNote(SecondTemplate->getLocation(), 10792 SecondTemplate->getSourceRange(), 10793 FunctionTemplateParameterDifferentKind) 10794 << SecondTemplate << (i + 1) << GetParamType(SecondParam); 10795 10796 ParameterMismatch = true; 10797 break; 10798 } 10799 10800 if (FirstParam->getName() != SecondParam->getName()) { 10801 ODRDiagError(FirstTemplate->getLocation(), 10802 FirstTemplate->getSourceRange(), 10803 FunctionTemplateParameterName) 10804 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier() 10805 << FirstParam; 10806 ODRDiagNote(SecondTemplate->getLocation(), 10807 SecondTemplate->getSourceRange(), 10808 FunctionTemplateParameterName) 10809 << SecondTemplate << (i + 1) 10810 << (bool)SecondParam->getIdentifier() << SecondParam; 10811 ParameterMismatch = true; 10812 break; 10813 } 10814 10815 if (isa<TemplateTypeParmDecl>(FirstParam) && 10816 isa<TemplateTypeParmDecl>(SecondParam)) { 10817 TemplateTypeParmDecl *FirstTTPD = 10818 cast<TemplateTypeParmDecl>(FirstParam); 10819 TemplateTypeParmDecl *SecondTTPD = 10820 cast<TemplateTypeParmDecl>(SecondParam); 10821 bool HasFirstDefaultArgument = 10822 FirstTTPD->hasDefaultArgument() && 10823 !FirstTTPD->defaultArgumentWasInherited(); 10824 bool HasSecondDefaultArgument = 10825 SecondTTPD->hasDefaultArgument() && 10826 !SecondTTPD->defaultArgumentWasInherited(); 10827 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10828 ODRDiagError(FirstTemplate->getLocation(), 10829 FirstTemplate->getSourceRange(), 10830 FunctionTemplateParameterSingleDefaultArgument) 10831 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 10832 ODRDiagNote(SecondTemplate->getLocation(), 10833 SecondTemplate->getSourceRange(), 10834 FunctionTemplateParameterSingleDefaultArgument) 10835 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 10836 ParameterMismatch = true; 10837 break; 10838 } 10839 10840 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 10841 QualType FirstType = FirstTTPD->getDefaultArgument(); 10842 QualType SecondType = SecondTTPD->getDefaultArgument(); 10843 if (ComputeQualTypeODRHash(FirstType) != 10844 ComputeQualTypeODRHash(SecondType)) { 10845 ODRDiagError(FirstTemplate->getLocation(), 10846 FirstTemplate->getSourceRange(), 10847 FunctionTemplateParameterDifferentDefaultArgument) 10848 << FirstTemplate << (i + 1) << FirstType; 10849 ODRDiagNote(SecondTemplate->getLocation(), 10850 SecondTemplate->getSourceRange(), 10851 FunctionTemplateParameterDifferentDefaultArgument) 10852 << SecondTemplate << (i + 1) << SecondType; 10853 ParameterMismatch = true; 10854 break; 10855 } 10856 } 10857 10858 if (FirstTTPD->isParameterPack() != 10859 SecondTTPD->isParameterPack()) { 10860 ODRDiagError(FirstTemplate->getLocation(), 10861 FirstTemplate->getSourceRange(), 10862 FunctionTemplatePackParameter) 10863 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 10864 ODRDiagNote(SecondTemplate->getLocation(), 10865 SecondTemplate->getSourceRange(), 10866 FunctionTemplatePackParameter) 10867 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 10868 ParameterMismatch = true; 10869 break; 10870 } 10871 } 10872 10873 if (isa<TemplateTemplateParmDecl>(FirstParam) && 10874 isa<TemplateTemplateParmDecl>(SecondParam)) { 10875 TemplateTemplateParmDecl *FirstTTPD = 10876 cast<TemplateTemplateParmDecl>(FirstParam); 10877 TemplateTemplateParmDecl *SecondTTPD = 10878 cast<TemplateTemplateParmDecl>(SecondParam); 10879 10880 TemplateParameterList *FirstTPL = 10881 FirstTTPD->getTemplateParameters(); 10882 TemplateParameterList *SecondTPL = 10883 SecondTTPD->getTemplateParameters(); 10884 10885 if (ComputeTemplateParameterListODRHash(FirstTPL) != 10886 ComputeTemplateParameterListODRHash(SecondTPL)) { 10887 ODRDiagError(FirstTemplate->getLocation(), 10888 FirstTemplate->getSourceRange(), 10889 FunctionTemplateParameterDifferentType) 10890 << FirstTemplate << (i + 1); 10891 ODRDiagNote(SecondTemplate->getLocation(), 10892 SecondTemplate->getSourceRange(), 10893 FunctionTemplateParameterDifferentType) 10894 << SecondTemplate << (i + 1); 10895 ParameterMismatch = true; 10896 break; 10897 } 10898 10899 bool HasFirstDefaultArgument = 10900 FirstTTPD->hasDefaultArgument() && 10901 !FirstTTPD->defaultArgumentWasInherited(); 10902 bool HasSecondDefaultArgument = 10903 SecondTTPD->hasDefaultArgument() && 10904 !SecondTTPD->defaultArgumentWasInherited(); 10905 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10906 ODRDiagError(FirstTemplate->getLocation(), 10907 FirstTemplate->getSourceRange(), 10908 FunctionTemplateParameterSingleDefaultArgument) 10909 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 10910 ODRDiagNote(SecondTemplate->getLocation(), 10911 SecondTemplate->getSourceRange(), 10912 FunctionTemplateParameterSingleDefaultArgument) 10913 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 10914 ParameterMismatch = true; 10915 break; 10916 } 10917 10918 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 10919 TemplateArgument FirstTA = 10920 FirstTTPD->getDefaultArgument().getArgument(); 10921 TemplateArgument SecondTA = 10922 SecondTTPD->getDefaultArgument().getArgument(); 10923 if (ComputeTemplateArgumentODRHash(FirstTA) != 10924 ComputeTemplateArgumentODRHash(SecondTA)) { 10925 ODRDiagError(FirstTemplate->getLocation(), 10926 FirstTemplate->getSourceRange(), 10927 FunctionTemplateParameterDifferentDefaultArgument) 10928 << FirstTemplate << (i + 1) << FirstTA; 10929 ODRDiagNote(SecondTemplate->getLocation(), 10930 SecondTemplate->getSourceRange(), 10931 FunctionTemplateParameterDifferentDefaultArgument) 10932 << SecondTemplate << (i + 1) << SecondTA; 10933 ParameterMismatch = true; 10934 break; 10935 } 10936 } 10937 10938 if (FirstTTPD->isParameterPack() != 10939 SecondTTPD->isParameterPack()) { 10940 ODRDiagError(FirstTemplate->getLocation(), 10941 FirstTemplate->getSourceRange(), 10942 FunctionTemplatePackParameter) 10943 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 10944 ODRDiagNote(SecondTemplate->getLocation(), 10945 SecondTemplate->getSourceRange(), 10946 FunctionTemplatePackParameter) 10947 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 10948 ParameterMismatch = true; 10949 break; 10950 } 10951 } 10952 10953 if (isa<NonTypeTemplateParmDecl>(FirstParam) && 10954 isa<NonTypeTemplateParmDecl>(SecondParam)) { 10955 NonTypeTemplateParmDecl *FirstNTTPD = 10956 cast<NonTypeTemplateParmDecl>(FirstParam); 10957 NonTypeTemplateParmDecl *SecondNTTPD = 10958 cast<NonTypeTemplateParmDecl>(SecondParam); 10959 10960 QualType FirstType = FirstNTTPD->getType(); 10961 QualType SecondType = SecondNTTPD->getType(); 10962 if (ComputeQualTypeODRHash(FirstType) != 10963 ComputeQualTypeODRHash(SecondType)) { 10964 ODRDiagError(FirstTemplate->getLocation(), 10965 FirstTemplate->getSourceRange(), 10966 FunctionTemplateParameterDifferentType) 10967 << FirstTemplate << (i + 1); 10968 ODRDiagNote(SecondTemplate->getLocation(), 10969 SecondTemplate->getSourceRange(), 10970 FunctionTemplateParameterDifferentType) 10971 << SecondTemplate << (i + 1); 10972 ParameterMismatch = true; 10973 break; 10974 } 10975 10976 bool HasFirstDefaultArgument = 10977 FirstNTTPD->hasDefaultArgument() && 10978 !FirstNTTPD->defaultArgumentWasInherited(); 10979 bool HasSecondDefaultArgument = 10980 SecondNTTPD->hasDefaultArgument() && 10981 !SecondNTTPD->defaultArgumentWasInherited(); 10982 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10983 ODRDiagError(FirstTemplate->getLocation(), 10984 FirstTemplate->getSourceRange(), 10985 FunctionTemplateParameterSingleDefaultArgument) 10986 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 10987 ODRDiagNote(SecondTemplate->getLocation(), 10988 SecondTemplate->getSourceRange(), 10989 FunctionTemplateParameterSingleDefaultArgument) 10990 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 10991 ParameterMismatch = true; 10992 break; 10993 } 10994 10995 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 10996 Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument(); 10997 Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument(); 10998 if (ComputeODRHash(FirstDefaultArgument) != 10999 ComputeODRHash(SecondDefaultArgument)) { 11000 ODRDiagError(FirstTemplate->getLocation(), 11001 FirstTemplate->getSourceRange(), 11002 FunctionTemplateParameterDifferentDefaultArgument) 11003 << FirstTemplate << (i + 1) << FirstDefaultArgument; 11004 ODRDiagNote(SecondTemplate->getLocation(), 11005 SecondTemplate->getSourceRange(), 11006 FunctionTemplateParameterDifferentDefaultArgument) 11007 << SecondTemplate << (i + 1) << SecondDefaultArgument; 11008 ParameterMismatch = true; 11009 break; 11010 } 11011 } 11012 11013 if (FirstNTTPD->isParameterPack() != 11014 SecondNTTPD->isParameterPack()) { 11015 ODRDiagError(FirstTemplate->getLocation(), 11016 FirstTemplate->getSourceRange(), 11017 FunctionTemplatePackParameter) 11018 << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack(); 11019 ODRDiagNote(SecondTemplate->getLocation(), 11020 SecondTemplate->getSourceRange(), 11021 FunctionTemplatePackParameter) 11022 << SecondTemplate << (i + 1) 11023 << SecondNTTPD->isParameterPack(); 11024 ParameterMismatch = true; 11025 break; 11026 } 11027 } 11028 } 11029 11030 if (ParameterMismatch) { 11031 Diagnosed = true; 11032 break; 11033 } 11034 11035 break; 11036 } 11037 } 11038 11039 if (Diagnosed) 11040 continue; 11041 11042 Diag(FirstDecl->getLocation(), 11043 diag::err_module_odr_violation_mismatch_decl_unknown) 11044 << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType 11045 << FirstDecl->getSourceRange(); 11046 Diag(SecondDecl->getLocation(), 11047 diag::note_module_odr_violation_mismatch_decl_unknown) 11048 << SecondModule << FirstDiffType << SecondDecl->getSourceRange(); 11049 Diagnosed = true; 11050 } 11051 11052 if (!Diagnosed) { 11053 // All definitions are updates to the same declaration. This happens if a 11054 // module instantiates the declaration of a class template specialization 11055 // and two or more other modules instantiate its definition. 11056 // 11057 // FIXME: Indicate which modules had instantiations of this definition. 11058 // FIXME: How can this even happen? 11059 Diag(Merge.first->getLocation(), 11060 diag::err_module_odr_violation_different_instantiations) 11061 << Merge.first; 11062 } 11063 } 11064 11065 // Issue ODR failures diagnostics for functions. 11066 for (auto &Merge : FunctionOdrMergeFailures) { 11067 enum ODRFunctionDifference { 11068 ReturnType, 11069 ParameterName, 11070 ParameterType, 11071 ParameterSingleDefaultArgument, 11072 ParameterDifferentDefaultArgument, 11073 FunctionBody, 11074 }; 11075 11076 FunctionDecl *FirstFunction = Merge.first; 11077 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction); 11078 11079 bool Diagnosed = false; 11080 for (auto &SecondFunction : Merge.second) { 11081 11082 if (FirstFunction == SecondFunction) 11083 continue; 11084 11085 std::string SecondModule = 11086 getOwningModuleNameForDiagnostic(SecondFunction); 11087 11088 auto ODRDiagError = [FirstFunction, &FirstModule, 11089 this](SourceLocation Loc, SourceRange Range, 11090 ODRFunctionDifference DiffType) { 11091 return Diag(Loc, diag::err_module_odr_violation_function) 11092 << FirstFunction << FirstModule.empty() << FirstModule << Range 11093 << DiffType; 11094 }; 11095 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11096 SourceRange Range, 11097 ODRFunctionDifference DiffType) { 11098 return Diag(Loc, diag::note_module_odr_violation_function) 11099 << SecondModule << Range << DiffType; 11100 }; 11101 11102 if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) != 11103 ComputeQualTypeODRHash(SecondFunction->getReturnType())) { 11104 ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(), 11105 FirstFunction->getReturnTypeSourceRange(), ReturnType) 11106 << FirstFunction->getReturnType(); 11107 ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(), 11108 SecondFunction->getReturnTypeSourceRange(), ReturnType) 11109 << SecondFunction->getReturnType(); 11110 Diagnosed = true; 11111 break; 11112 } 11113 11114 assert(FirstFunction->param_size() == SecondFunction->param_size() && 11115 "Merged functions with different number of parameters"); 11116 11117 auto ParamSize = FirstFunction->param_size(); 11118 bool ParameterMismatch = false; 11119 for (unsigned I = 0; I < ParamSize; ++I) { 11120 auto *FirstParam = FirstFunction->getParamDecl(I); 11121 auto *SecondParam = SecondFunction->getParamDecl(I); 11122 11123 assert(getContext().hasSameType(FirstParam->getType(), 11124 SecondParam->getType()) && 11125 "Merged function has different parameter types."); 11126 11127 if (FirstParam->getDeclName() != SecondParam->getDeclName()) { 11128 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11129 ParameterName) 11130 << I + 1 << FirstParam->getDeclName(); 11131 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11132 ParameterName) 11133 << I + 1 << SecondParam->getDeclName(); 11134 ParameterMismatch = true; 11135 break; 11136 }; 11137 11138 QualType FirstParamType = FirstParam->getType(); 11139 QualType SecondParamType = SecondParam->getType(); 11140 if (FirstParamType != SecondParamType && 11141 ComputeQualTypeODRHash(FirstParamType) != 11142 ComputeQualTypeODRHash(SecondParamType)) { 11143 if (const DecayedType *ParamDecayedType = 11144 FirstParamType->getAs<DecayedType>()) { 11145 ODRDiagError(FirstParam->getLocation(), 11146 FirstParam->getSourceRange(), ParameterType) 11147 << (I + 1) << FirstParamType << true 11148 << ParamDecayedType->getOriginalType(); 11149 } else { 11150 ODRDiagError(FirstParam->getLocation(), 11151 FirstParam->getSourceRange(), ParameterType) 11152 << (I + 1) << FirstParamType << false; 11153 } 11154 11155 if (const DecayedType *ParamDecayedType = 11156 SecondParamType->getAs<DecayedType>()) { 11157 ODRDiagNote(SecondParam->getLocation(), 11158 SecondParam->getSourceRange(), ParameterType) 11159 << (I + 1) << SecondParamType << true 11160 << ParamDecayedType->getOriginalType(); 11161 } else { 11162 ODRDiagNote(SecondParam->getLocation(), 11163 SecondParam->getSourceRange(), ParameterType) 11164 << (I + 1) << SecondParamType << false; 11165 } 11166 ParameterMismatch = true; 11167 break; 11168 } 11169 11170 const Expr *FirstInit = FirstParam->getInit(); 11171 const Expr *SecondInit = SecondParam->getInit(); 11172 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 11173 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11174 ParameterSingleDefaultArgument) 11175 << (I + 1) << (FirstInit == nullptr) 11176 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 11177 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11178 ParameterSingleDefaultArgument) 11179 << (I + 1) << (SecondInit == nullptr) 11180 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 11181 ParameterMismatch = true; 11182 break; 11183 } 11184 11185 if (FirstInit && SecondInit && 11186 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11187 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11188 ParameterDifferentDefaultArgument) 11189 << (I + 1) << FirstInit->getSourceRange(); 11190 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11191 ParameterDifferentDefaultArgument) 11192 << (I + 1) << SecondInit->getSourceRange(); 11193 ParameterMismatch = true; 11194 break; 11195 } 11196 11197 assert(ComputeSubDeclODRHash(FirstParam) == 11198 ComputeSubDeclODRHash(SecondParam) && 11199 "Undiagnosed parameter difference."); 11200 } 11201 11202 if (ParameterMismatch) { 11203 Diagnosed = true; 11204 break; 11205 } 11206 11207 // If no error has been generated before now, assume the problem is in 11208 // the body and generate a message. 11209 ODRDiagError(FirstFunction->getLocation(), 11210 FirstFunction->getSourceRange(), FunctionBody); 11211 ODRDiagNote(SecondFunction->getLocation(), 11212 SecondFunction->getSourceRange(), FunctionBody); 11213 Diagnosed = true; 11214 break; 11215 } 11216 (void)Diagnosed; 11217 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11218 } 11219 } 11220 11221 void ASTReader::StartedDeserializing() { 11222 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get()) 11223 ReadTimer->startTimer(); 11224 } 11225 11226 void ASTReader::FinishedDeserializing() { 11227 assert(NumCurrentElementsDeserializing && 11228 "FinishedDeserializing not paired with StartedDeserializing"); 11229 if (NumCurrentElementsDeserializing == 1) { 11230 // We decrease NumCurrentElementsDeserializing only after pending actions 11231 // are finished, to avoid recursively re-calling finishPendingActions(). 11232 finishPendingActions(); 11233 } 11234 --NumCurrentElementsDeserializing; 11235 11236 if (NumCurrentElementsDeserializing == 0) { 11237 // Propagate exception specification updates along redeclaration chains. 11238 while (!PendingExceptionSpecUpdates.empty()) { 11239 auto Updates = std::move(PendingExceptionSpecUpdates); 11240 PendingExceptionSpecUpdates.clear(); 11241 for (auto Update : Updates) { 11242 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11243 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>(); 11244 auto ESI = FPT->getExtProtoInfo().ExceptionSpec; 11245 if (auto *Listener = getContext().getASTMutationListener()) 11246 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second)); 11247 for (auto *Redecl : Update.second->redecls()) 11248 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI); 11249 } 11250 } 11251 11252 if (ReadTimer) 11253 ReadTimer->stopTimer(); 11254 11255 diagnoseOdrViolations(); 11256 11257 // We are not in recursive loading, so it's safe to pass the "interesting" 11258 // decls to the consumer. 11259 if (Consumer) 11260 PassInterestingDeclsToConsumer(); 11261 } 11262 } 11263 11264 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) { 11265 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) { 11266 // Remove any fake results before adding any real ones. 11267 auto It = PendingFakeLookupResults.find(II); 11268 if (It != PendingFakeLookupResults.end()) { 11269 for (auto *ND : It->second) 11270 SemaObj->IdResolver.RemoveDecl(ND); 11271 // FIXME: this works around module+PCH performance issue. 11272 // Rather than erase the result from the map, which is O(n), just clear 11273 // the vector of NamedDecls. 11274 It->second.clear(); 11275 } 11276 } 11277 11278 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) { 11279 SemaObj->TUScope->AddDecl(D); 11280 } else if (SemaObj->TUScope) { 11281 // Adding the decl to IdResolver may have failed because it was already in 11282 // (even though it was not added in scope). If it is already in, make sure 11283 // it gets in the scope as well. 11284 if (std::find(SemaObj->IdResolver.begin(Name), 11285 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end()) 11286 SemaObj->TUScope->AddDecl(D); 11287 } 11288 } 11289 11290 ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context, 11291 const PCHContainerReader &PCHContainerRdr, 11292 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, 11293 StringRef isysroot, bool DisableValidation, 11294 bool AllowASTWithCompilerErrors, 11295 bool AllowConfigurationMismatch, bool ValidateSystemInputs, 11296 bool UseGlobalIndex, 11297 std::unique_ptr<llvm::Timer> ReadTimer) 11298 : Listener(DisableValidation 11299 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP)) 11300 : cast<ASTReaderListener>(new PCHValidator(PP, *this))), 11301 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), 11302 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP), 11303 ContextObj(Context), 11304 ModuleMgr(PP.getFileManager(), PP.getPCMCache(), PCHContainerRdr, 11305 PP.getHeaderSearchInfo()), 11306 PCMCache(PP.getPCMCache()), DummyIdResolver(PP), 11307 ReadTimer(std::move(ReadTimer)), isysroot(isysroot), 11308 DisableValidation(DisableValidation), 11309 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors), 11310 AllowConfigurationMismatch(AllowConfigurationMismatch), 11311 ValidateSystemInputs(ValidateSystemInputs), 11312 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) { 11313 SourceMgr.setExternalSLocEntrySource(this); 11314 11315 for (const auto &Ext : Extensions) { 11316 auto BlockName = Ext->getExtensionMetadata().BlockName; 11317 auto Known = ModuleFileExtensions.find(BlockName); 11318 if (Known != ModuleFileExtensions.end()) { 11319 Diags.Report(diag::warn_duplicate_module_file_extension) 11320 << BlockName; 11321 continue; 11322 } 11323 11324 ModuleFileExtensions.insert({BlockName, Ext}); 11325 } 11326 } 11327 11328 ASTReader::~ASTReader() { 11329 if (OwnsDeserializationListener) 11330 delete DeserializationListener; 11331 } 11332 11333 IdentifierResolver &ASTReader::getIdResolver() { 11334 return SemaObj ? SemaObj->IdResolver : DummyIdResolver; 11335 } 11336 11337 unsigned ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor, 11338 unsigned AbbrevID) { 11339 Idx = 0; 11340 Record.clear(); 11341 return Cursor.readRecord(AbbrevID, Record); 11342 } 11343