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