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