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/MemoryBufferCache.h" 50 #include "clang/Basic/Module.h" 51 #include "clang/Basic/ObjCRuntime.h" 52 #include "clang/Basic/OperatorKinds.h" 53 #include "clang/Basic/PragmaKinds.h" 54 #include "clang/Basic/Sanitizers.h" 55 #include "clang/Basic/SourceLocation.h" 56 #include "clang/Basic/SourceManager.h" 57 #include "clang/Basic/SourceManagerInternals.h" 58 #include "clang/Basic/Specifiers.h" 59 #include "clang/Basic/TargetInfo.h" 60 #include "clang/Basic/TargetOptions.h" 61 #include "clang/Basic/TokenKinds.h" 62 #include "clang/Basic/Version.h" 63 #include "clang/Lex/HeaderSearch.h" 64 #include "clang/Lex/HeaderSearchOptions.h" 65 #include "clang/Lex/MacroInfo.h" 66 #include "clang/Lex/ModuleMap.h" 67 #include "clang/Lex/PreprocessingRecord.h" 68 #include "clang/Lex/Preprocessor.h" 69 #include "clang/Lex/PreprocessorOptions.h" 70 #include "clang/Lex/Token.h" 71 #include "clang/Sema/ObjCMethodList.h" 72 #include "clang/Sema/Scope.h" 73 #include "clang/Sema/Sema.h" 74 #include "clang/Sema/Weak.h" 75 #include "clang/Serialization/ASTBitCodes.h" 76 #include "clang/Serialization/ASTDeserializationListener.h" 77 #include "clang/Serialization/ContinuousRangeMap.h" 78 #include "clang/Serialization/GlobalModuleIndex.h" 79 #include "clang/Serialization/Module.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/SmallPtrSet.h" 96 #include "llvm/ADT/SmallString.h" 97 #include "llvm/ADT/SmallVector.h" 98 #include "llvm/ADT/StringExtras.h" 99 #include "llvm/ADT/StringMap.h" 100 #include "llvm/ADT/StringRef.h" 101 #include "llvm/ADT/Triple.h" 102 #include "llvm/ADT/iterator_range.h" 103 #include "llvm/Bitcode/BitstreamReader.h" 104 #include "llvm/Support/Casting.h" 105 #include "llvm/Support/Compiler.h" 106 #include "llvm/Support/Compression.h" 107 #include "llvm/Support/DJB.h" 108 #include "llvm/Support/Endian.h" 109 #include "llvm/Support/Error.h" 110 #include "llvm/Support/ErrorHandling.h" 111 #include "llvm/Support/FileSystem.h" 112 #include "llvm/Support/MemoryBuffer.h" 113 #include "llvm/Support/Path.h" 114 #include "llvm/Support/SaveAndRestore.h" 115 #include "llvm/Support/Timer.h" 116 #include "llvm/Support/VersionTuple.h" 117 #include "llvm/Support/raw_ostream.h" 118 #include <algorithm> 119 #include <cassert> 120 #include <cstddef> 121 #include <cstdint> 122 #include <cstdio> 123 #include <ctime> 124 #include <iterator> 125 #include <limits> 126 #include <map> 127 #include <memory> 128 #include <string> 129 #include <system_error> 130 #include <tuple> 131 #include <utility> 132 #include <vector> 133 134 using namespace clang; 135 using namespace clang::serialization; 136 using namespace clang::serialization::reader; 137 using llvm::BitstreamCursor; 138 139 //===----------------------------------------------------------------------===// 140 // ChainedASTReaderListener implementation 141 //===----------------------------------------------------------------------===// 142 143 bool 144 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) { 145 return First->ReadFullVersionInformation(FullVersion) || 146 Second->ReadFullVersionInformation(FullVersion); 147 } 148 149 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) { 150 First->ReadModuleName(ModuleName); 151 Second->ReadModuleName(ModuleName); 152 } 153 154 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) { 155 First->ReadModuleMapFile(ModuleMapPath); 156 Second->ReadModuleMapFile(ModuleMapPath); 157 } 158 159 bool 160 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts, 161 bool Complain, 162 bool AllowCompatibleDifferences) { 163 return First->ReadLanguageOptions(LangOpts, Complain, 164 AllowCompatibleDifferences) || 165 Second->ReadLanguageOptions(LangOpts, Complain, 166 AllowCompatibleDifferences); 167 } 168 169 bool ChainedASTReaderListener::ReadTargetOptions( 170 const TargetOptions &TargetOpts, bool Complain, 171 bool AllowCompatibleDifferences) { 172 return First->ReadTargetOptions(TargetOpts, Complain, 173 AllowCompatibleDifferences) || 174 Second->ReadTargetOptions(TargetOpts, Complain, 175 AllowCompatibleDifferences); 176 } 177 178 bool ChainedASTReaderListener::ReadDiagnosticOptions( 179 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 180 return First->ReadDiagnosticOptions(DiagOpts, Complain) || 181 Second->ReadDiagnosticOptions(DiagOpts, Complain); 182 } 183 184 bool 185 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts, 186 bool Complain) { 187 return First->ReadFileSystemOptions(FSOpts, Complain) || 188 Second->ReadFileSystemOptions(FSOpts, Complain); 189 } 190 191 bool ChainedASTReaderListener::ReadHeaderSearchOptions( 192 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath, 193 bool Complain) { 194 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 195 Complain) || 196 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 197 Complain); 198 } 199 200 bool ChainedASTReaderListener::ReadPreprocessorOptions( 201 const PreprocessorOptions &PPOpts, bool Complain, 202 std::string &SuggestedPredefines) { 203 return First->ReadPreprocessorOptions(PPOpts, Complain, 204 SuggestedPredefines) || 205 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines); 206 } 207 208 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M, 209 unsigned Value) { 210 First->ReadCounter(M, Value); 211 Second->ReadCounter(M, Value); 212 } 213 214 bool ChainedASTReaderListener::needsInputFileVisitation() { 215 return First->needsInputFileVisitation() || 216 Second->needsInputFileVisitation(); 217 } 218 219 bool ChainedASTReaderListener::needsSystemInputFileVisitation() { 220 return First->needsSystemInputFileVisitation() || 221 Second->needsSystemInputFileVisitation(); 222 } 223 224 void ChainedASTReaderListener::visitModuleFile(StringRef Filename, 225 ModuleKind Kind) { 226 First->visitModuleFile(Filename, Kind); 227 Second->visitModuleFile(Filename, Kind); 228 } 229 230 bool ChainedASTReaderListener::visitInputFile(StringRef Filename, 231 bool isSystem, 232 bool isOverridden, 233 bool isExplicitModule) { 234 bool Continue = false; 235 if (First->needsInputFileVisitation() && 236 (!isSystem || First->needsSystemInputFileVisitation())) 237 Continue |= First->visitInputFile(Filename, isSystem, isOverridden, 238 isExplicitModule); 239 if (Second->needsInputFileVisitation() && 240 (!isSystem || Second->needsSystemInputFileVisitation())) 241 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden, 242 isExplicitModule); 243 return Continue; 244 } 245 246 void ChainedASTReaderListener::readModuleFileExtension( 247 const ModuleFileExtensionMetadata &Metadata) { 248 First->readModuleFileExtension(Metadata); 249 Second->readModuleFileExtension(Metadata); 250 } 251 252 //===----------------------------------------------------------------------===// 253 // PCH validator implementation 254 //===----------------------------------------------------------------------===// 255 256 ASTReaderListener::~ASTReaderListener() = default; 257 258 /// Compare the given set of language options against an existing set of 259 /// language options. 260 /// 261 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 262 /// \param AllowCompatibleDifferences If true, differences between compatible 263 /// language options will be permitted. 264 /// 265 /// \returns true if the languagae options mis-match, false otherwise. 266 static bool checkLanguageOptions(const LangOptions &LangOpts, 267 const LangOptions &ExistingLangOpts, 268 DiagnosticsEngine *Diags, 269 bool AllowCompatibleDifferences = true) { 270 #define LANGOPT(Name, Bits, Default, Description) \ 271 if (ExistingLangOpts.Name != LangOpts.Name) { \ 272 if (Diags) \ 273 Diags->Report(diag::err_pch_langopt_mismatch) \ 274 << Description << LangOpts.Name << ExistingLangOpts.Name; \ 275 return true; \ 276 } 277 278 #define VALUE_LANGOPT(Name, Bits, Default, Description) \ 279 if (ExistingLangOpts.Name != LangOpts.Name) { \ 280 if (Diags) \ 281 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 282 << Description; \ 283 return true; \ 284 } 285 286 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 287 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \ 288 if (Diags) \ 289 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 290 << Description; \ 291 return true; \ 292 } 293 294 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \ 295 if (!AllowCompatibleDifferences) \ 296 LANGOPT(Name, Bits, Default, Description) 297 298 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \ 299 if (!AllowCompatibleDifferences) \ 300 ENUM_LANGOPT(Name, Bits, Default, Description) 301 302 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \ 303 if (!AllowCompatibleDifferences) \ 304 VALUE_LANGOPT(Name, Bits, Default, Description) 305 306 #define BENIGN_LANGOPT(Name, Bits, Default, Description) 307 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) 308 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description) 309 #include "clang/Basic/LangOptions.def" 310 311 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) { 312 if (Diags) 313 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features"; 314 return true; 315 } 316 317 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) { 318 if (Diags) 319 Diags->Report(diag::err_pch_langopt_value_mismatch) 320 << "target Objective-C runtime"; 321 return true; 322 } 323 324 if (ExistingLangOpts.CommentOpts.BlockCommandNames != 325 LangOpts.CommentOpts.BlockCommandNames) { 326 if (Diags) 327 Diags->Report(diag::err_pch_langopt_value_mismatch) 328 << "block command names"; 329 return true; 330 } 331 332 // Sanitizer feature mismatches are treated as compatible differences. If 333 // compatible differences aren't allowed, we still only want to check for 334 // mismatches of non-modular sanitizers (the only ones which can affect AST 335 // generation). 336 if (!AllowCompatibleDifferences) { 337 SanitizerMask ModularSanitizers = getPPTransparentSanitizers(); 338 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize; 339 SanitizerSet ImportedSanitizers = LangOpts.Sanitize; 340 ExistingSanitizers.clear(ModularSanitizers); 341 ImportedSanitizers.clear(ModularSanitizers); 342 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) { 343 const std::string Flag = "-fsanitize="; 344 if (Diags) { 345 #define SANITIZER(NAME, ID) \ 346 { \ 347 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \ 348 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \ 349 if (InExistingModule != InImportedModule) \ 350 Diags->Report(diag::err_pch_targetopt_feature_mismatch) \ 351 << InExistingModule << (Flag + NAME); \ 352 } 353 #include "clang/Basic/Sanitizers.def" 354 } 355 return true; 356 } 357 } 358 359 return false; 360 } 361 362 /// Compare the given set of target options against an existing set of 363 /// target options. 364 /// 365 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 366 /// 367 /// \returns true if the target options mis-match, false otherwise. 368 static bool checkTargetOptions(const TargetOptions &TargetOpts, 369 const TargetOptions &ExistingTargetOpts, 370 DiagnosticsEngine *Diags, 371 bool AllowCompatibleDifferences = true) { 372 #define CHECK_TARGET_OPT(Field, Name) \ 373 if (TargetOpts.Field != ExistingTargetOpts.Field) { \ 374 if (Diags) \ 375 Diags->Report(diag::err_pch_targetopt_mismatch) \ 376 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \ 377 return true; \ 378 } 379 380 // The triple and ABI must match exactly. 381 CHECK_TARGET_OPT(Triple, "target"); 382 CHECK_TARGET_OPT(ABI, "target ABI"); 383 384 // We can tolerate different CPUs in many cases, notably when one CPU 385 // supports a strict superset of another. When allowing compatible 386 // differences skip this check. 387 if (!AllowCompatibleDifferences) 388 CHECK_TARGET_OPT(CPU, "target CPU"); 389 390 #undef CHECK_TARGET_OPT 391 392 // Compare feature sets. 393 SmallVector<StringRef, 4> ExistingFeatures( 394 ExistingTargetOpts.FeaturesAsWritten.begin(), 395 ExistingTargetOpts.FeaturesAsWritten.end()); 396 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(), 397 TargetOpts.FeaturesAsWritten.end()); 398 llvm::sort(ExistingFeatures); 399 llvm::sort(ReadFeatures); 400 401 // We compute the set difference in both directions explicitly so that we can 402 // diagnose the differences differently. 403 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures; 404 std::set_difference( 405 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(), 406 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures)); 407 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(), 408 ExistingFeatures.begin(), ExistingFeatures.end(), 409 std::back_inserter(UnmatchedReadFeatures)); 410 411 // If we are allowing compatible differences and the read feature set is 412 // a strict subset of the existing feature set, there is nothing to diagnose. 413 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty()) 414 return false; 415 416 if (Diags) { 417 for (StringRef Feature : UnmatchedReadFeatures) 418 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 419 << /* is-existing-feature */ false << Feature; 420 for (StringRef Feature : UnmatchedExistingFeatures) 421 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 422 << /* is-existing-feature */ true << Feature; 423 } 424 425 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty(); 426 } 427 428 bool 429 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts, 430 bool Complain, 431 bool AllowCompatibleDifferences) { 432 const LangOptions &ExistingLangOpts = PP.getLangOpts(); 433 return checkLanguageOptions(LangOpts, ExistingLangOpts, 434 Complain ? &Reader.Diags : nullptr, 435 AllowCompatibleDifferences); 436 } 437 438 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts, 439 bool Complain, 440 bool AllowCompatibleDifferences) { 441 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts(); 442 return checkTargetOptions(TargetOpts, ExistingTargetOpts, 443 Complain ? &Reader.Diags : nullptr, 444 AllowCompatibleDifferences); 445 } 446 447 namespace { 448 449 using MacroDefinitionsMap = 450 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>; 451 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>; 452 453 } // namespace 454 455 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags, 456 DiagnosticsEngine &Diags, 457 bool Complain) { 458 using Level = DiagnosticsEngine::Level; 459 460 // Check current mappings for new -Werror mappings, and the stored mappings 461 // for cases that were explicitly mapped to *not* be errors that are now 462 // errors because of options like -Werror. 463 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags }; 464 465 for (DiagnosticsEngine *MappingSource : MappingSources) { 466 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) { 467 diag::kind DiagID = DiagIDMappingPair.first; 468 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation()); 469 if (CurLevel < DiagnosticsEngine::Error) 470 continue; // not significant 471 Level StoredLevel = 472 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation()); 473 if (StoredLevel < DiagnosticsEngine::Error) { 474 if (Complain) 475 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" + 476 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str(); 477 return true; 478 } 479 } 480 } 481 482 return false; 483 } 484 485 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) { 486 diag::Severity Ext = Diags.getExtensionHandlingBehavior(); 487 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors()) 488 return true; 489 return Ext >= diag::Severity::Error; 490 } 491 492 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags, 493 DiagnosticsEngine &Diags, 494 bool IsSystem, bool Complain) { 495 // Top-level options 496 if (IsSystem) { 497 if (Diags.getSuppressSystemWarnings()) 498 return false; 499 // If -Wsystem-headers was not enabled before, be conservative 500 if (StoredDiags.getSuppressSystemWarnings()) { 501 if (Complain) 502 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers"; 503 return true; 504 } 505 } 506 507 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) { 508 if (Complain) 509 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror"; 510 return true; 511 } 512 513 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() && 514 !StoredDiags.getEnableAllWarnings()) { 515 if (Complain) 516 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror"; 517 return true; 518 } 519 520 if (isExtHandlingFromDiagsError(Diags) && 521 !isExtHandlingFromDiagsError(StoredDiags)) { 522 if (Complain) 523 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors"; 524 return true; 525 } 526 527 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain); 528 } 529 530 /// Return the top import module if it is implicit, nullptr otherwise. 531 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr, 532 Preprocessor &PP) { 533 // If the original import came from a file explicitly generated by the user, 534 // don't check the diagnostic mappings. 535 // FIXME: currently this is approximated by checking whether this is not a 536 // module import of an implicitly-loaded module file. 537 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in 538 // the transitive closure of its imports, since unrelated modules cannot be 539 // imported until after this module finishes validation. 540 ModuleFile *TopImport = &*ModuleMgr.rbegin(); 541 while (!TopImport->ImportedBy.empty()) 542 TopImport = TopImport->ImportedBy[0]; 543 if (TopImport->Kind != MK_ImplicitModule) 544 return nullptr; 545 546 StringRef ModuleName = TopImport->ModuleName; 547 assert(!ModuleName.empty() && "diagnostic options read before module name"); 548 549 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName); 550 assert(M && "missing module"); 551 return M; 552 } 553 554 bool PCHValidator::ReadDiagnosticOptions( 555 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 556 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics(); 557 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs()); 558 IntrusiveRefCntPtr<DiagnosticsEngine> Diags( 559 new DiagnosticsEngine(DiagIDs, DiagOpts.get())); 560 // This should never fail, because we would have processed these options 561 // before writing them to an ASTFile. 562 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false); 563 564 ModuleManager &ModuleMgr = Reader.getModuleManager(); 565 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then"); 566 567 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP); 568 if (!TopM) 569 return false; 570 571 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that 572 // contains the union of their flags. 573 return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem, 574 Complain); 575 } 576 577 /// Collect the macro definitions provided by the given preprocessor 578 /// options. 579 static void 580 collectMacroDefinitions(const PreprocessorOptions &PPOpts, 581 MacroDefinitionsMap &Macros, 582 SmallVectorImpl<StringRef> *MacroNames = nullptr) { 583 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) { 584 StringRef Macro = PPOpts.Macros[I].first; 585 bool IsUndef = PPOpts.Macros[I].second; 586 587 std::pair<StringRef, StringRef> MacroPair = Macro.split('='); 588 StringRef MacroName = MacroPair.first; 589 StringRef MacroBody = MacroPair.second; 590 591 // For an #undef'd macro, we only care about the name. 592 if (IsUndef) { 593 if (MacroNames && !Macros.count(MacroName)) 594 MacroNames->push_back(MacroName); 595 596 Macros[MacroName] = std::make_pair("", true); 597 continue; 598 } 599 600 // For a #define'd macro, figure out the actual definition. 601 if (MacroName.size() == Macro.size()) 602 MacroBody = "1"; 603 else { 604 // Note: GCC drops anything following an end-of-line character. 605 StringRef::size_type End = MacroBody.find_first_of("\n\r"); 606 MacroBody = MacroBody.substr(0, End); 607 } 608 609 if (MacroNames && !Macros.count(MacroName)) 610 MacroNames->push_back(MacroName); 611 Macros[MacroName] = std::make_pair(MacroBody, false); 612 } 613 } 614 615 /// Check the preprocessor options deserialized from the control block 616 /// against the preprocessor options in an existing preprocessor. 617 /// 618 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 619 /// \param Validate If true, validate preprocessor options. If false, allow 620 /// macros defined by \p ExistingPPOpts to override those defined by 621 /// \p PPOpts in SuggestedPredefines. 622 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts, 623 const PreprocessorOptions &ExistingPPOpts, 624 DiagnosticsEngine *Diags, 625 FileManager &FileMgr, 626 std::string &SuggestedPredefines, 627 const LangOptions &LangOpts, 628 bool Validate = true) { 629 // Check macro definitions. 630 MacroDefinitionsMap ASTFileMacros; 631 collectMacroDefinitions(PPOpts, ASTFileMacros); 632 MacroDefinitionsMap ExistingMacros; 633 SmallVector<StringRef, 4> ExistingMacroNames; 634 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames); 635 636 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) { 637 // Dig out the macro definition in the existing preprocessor options. 638 StringRef MacroName = ExistingMacroNames[I]; 639 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName]; 640 641 // Check whether we know anything about this macro name or not. 642 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known = 643 ASTFileMacros.find(MacroName); 644 if (!Validate || Known == ASTFileMacros.end()) { 645 // FIXME: Check whether this identifier was referenced anywhere in the 646 // AST file. If so, we should reject the AST file. Unfortunately, this 647 // information isn't in the control block. What shall we do about it? 648 649 if (Existing.second) { 650 SuggestedPredefines += "#undef "; 651 SuggestedPredefines += MacroName.str(); 652 SuggestedPredefines += '\n'; 653 } else { 654 SuggestedPredefines += "#define "; 655 SuggestedPredefines += MacroName.str(); 656 SuggestedPredefines += ' '; 657 SuggestedPredefines += Existing.first.str(); 658 SuggestedPredefines += '\n'; 659 } 660 continue; 661 } 662 663 // If the macro was defined in one but undef'd in the other, we have a 664 // conflict. 665 if (Existing.second != Known->second.second) { 666 if (Diags) { 667 Diags->Report(diag::err_pch_macro_def_undef) 668 << MacroName << Known->second.second; 669 } 670 return true; 671 } 672 673 // If the macro was #undef'd in both, or if the macro bodies are identical, 674 // it's fine. 675 if (Existing.second || Existing.first == Known->second.first) 676 continue; 677 678 // The macro bodies differ; complain. 679 if (Diags) { 680 Diags->Report(diag::err_pch_macro_def_conflict) 681 << MacroName << Known->second.first << Existing.first; 682 } 683 return true; 684 } 685 686 // Check whether we're using predefines. 687 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) { 688 if (Diags) { 689 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines; 690 } 691 return true; 692 } 693 694 // Detailed record is important since it is used for the module cache hash. 695 if (LangOpts.Modules && 696 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) { 697 if (Diags) { 698 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord; 699 } 700 return true; 701 } 702 703 // Compute the #include and #include_macros lines we need. 704 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) { 705 StringRef File = ExistingPPOpts.Includes[I]; 706 707 if (!ExistingPPOpts.ImplicitPCHInclude.empty() && 708 !ExistingPPOpts.PCHThroughHeader.empty()) { 709 // In case the through header is an include, we must add all the includes 710 // to the predefines so the start point can be determined. 711 SuggestedPredefines += "#include \""; 712 SuggestedPredefines += File; 713 SuggestedPredefines += "\"\n"; 714 continue; 715 } 716 717 if (File == ExistingPPOpts.ImplicitPCHInclude) 718 continue; 719 720 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File) 721 != PPOpts.Includes.end()) 722 continue; 723 724 SuggestedPredefines += "#include \""; 725 SuggestedPredefines += File; 726 SuggestedPredefines += "\"\n"; 727 } 728 729 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) { 730 StringRef File = ExistingPPOpts.MacroIncludes[I]; 731 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(), 732 File) 733 != PPOpts.MacroIncludes.end()) 734 continue; 735 736 SuggestedPredefines += "#__include_macros \""; 737 SuggestedPredefines += File; 738 SuggestedPredefines += "\"\n##\n"; 739 } 740 741 return false; 742 } 743 744 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 745 bool Complain, 746 std::string &SuggestedPredefines) { 747 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts(); 748 749 return checkPreprocessorOptions(PPOpts, ExistingPPOpts, 750 Complain? &Reader.Diags : nullptr, 751 PP.getFileManager(), 752 SuggestedPredefines, 753 PP.getLangOpts()); 754 } 755 756 bool SimpleASTReaderListener::ReadPreprocessorOptions( 757 const PreprocessorOptions &PPOpts, 758 bool Complain, 759 std::string &SuggestedPredefines) { 760 return checkPreprocessorOptions(PPOpts, 761 PP.getPreprocessorOpts(), 762 nullptr, 763 PP.getFileManager(), 764 SuggestedPredefines, 765 PP.getLangOpts(), 766 false); 767 } 768 769 /// Check the header search options deserialized from the control block 770 /// against the header search options in an existing preprocessor. 771 /// 772 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 773 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 774 StringRef SpecificModuleCachePath, 775 StringRef ExistingModuleCachePath, 776 DiagnosticsEngine *Diags, 777 const LangOptions &LangOpts) { 778 if (LangOpts.Modules) { 779 if (SpecificModuleCachePath != ExistingModuleCachePath) { 780 if (Diags) 781 Diags->Report(diag::err_pch_modulecache_mismatch) 782 << SpecificModuleCachePath << ExistingModuleCachePath; 783 return true; 784 } 785 } 786 787 return false; 788 } 789 790 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 791 StringRef SpecificModuleCachePath, 792 bool Complain) { 793 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 794 PP.getHeaderSearchInfo().getModuleCachePath(), 795 Complain ? &Reader.Diags : nullptr, 796 PP.getLangOpts()); 797 } 798 799 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) { 800 PP.setCounterValue(Value); 801 } 802 803 //===----------------------------------------------------------------------===// 804 // AST reader implementation 805 //===----------------------------------------------------------------------===// 806 807 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener, 808 bool TakeOwnership) { 809 DeserializationListener = Listener; 810 OwnsDeserializationListener = TakeOwnership; 811 } 812 813 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) { 814 return serialization::ComputeHash(Sel); 815 } 816 817 std::pair<unsigned, unsigned> 818 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 819 using namespace llvm::support; 820 821 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 822 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 823 return std::make_pair(KeyLen, DataLen); 824 } 825 826 ASTSelectorLookupTrait::internal_key_type 827 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) { 828 using namespace llvm::support; 829 830 SelectorTable &SelTable = Reader.getContext().Selectors; 831 unsigned N = endian::readNext<uint16_t, little, unaligned>(d); 832 IdentifierInfo *FirstII = Reader.getLocalIdentifier( 833 F, endian::readNext<uint32_t, little, unaligned>(d)); 834 if (N == 0) 835 return SelTable.getNullarySelector(FirstII); 836 else if (N == 1) 837 return SelTable.getUnarySelector(FirstII); 838 839 SmallVector<IdentifierInfo *, 16> Args; 840 Args.push_back(FirstII); 841 for (unsigned I = 1; I != N; ++I) 842 Args.push_back(Reader.getLocalIdentifier( 843 F, endian::readNext<uint32_t, little, unaligned>(d))); 844 845 return SelTable.getSelector(N, Args.data()); 846 } 847 848 ASTSelectorLookupTrait::data_type 849 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d, 850 unsigned DataLen) { 851 using namespace llvm::support; 852 853 data_type Result; 854 855 Result.ID = Reader.getGlobalSelectorID( 856 F, endian::readNext<uint32_t, little, unaligned>(d)); 857 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d); 858 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d); 859 Result.InstanceBits = FullInstanceBits & 0x3; 860 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1; 861 Result.FactoryBits = FullFactoryBits & 0x3; 862 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1; 863 unsigned NumInstanceMethods = FullInstanceBits >> 3; 864 unsigned NumFactoryMethods = FullFactoryBits >> 3; 865 866 // Load instance methods 867 for (unsigned I = 0; I != NumInstanceMethods; ++I) { 868 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 869 F, endian::readNext<uint32_t, little, unaligned>(d))) 870 Result.Instance.push_back(Method); 871 } 872 873 // Load factory methods 874 for (unsigned I = 0; I != NumFactoryMethods; ++I) { 875 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 876 F, endian::readNext<uint32_t, little, unaligned>(d))) 877 Result.Factory.push_back(Method); 878 } 879 880 return Result; 881 } 882 883 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) { 884 return llvm::djbHash(a); 885 } 886 887 std::pair<unsigned, unsigned> 888 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) { 889 using namespace llvm::support; 890 891 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 892 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 893 return std::make_pair(KeyLen, DataLen); 894 } 895 896 ASTIdentifierLookupTraitBase::internal_key_type 897 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) { 898 assert(n >= 2 && d[n-1] == '\0'); 899 return StringRef((const char*) d, n-1); 900 } 901 902 /// Whether the given identifier is "interesting". 903 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II, 904 bool IsModule) { 905 return II.hadMacroDefinition() || 906 II.isPoisoned() || 907 (IsModule ? II.hasRevertedBuiltin() : II.getObjCOrBuiltinID()) || 908 II.hasRevertedTokenIDToIdentifier() || 909 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) && 910 II.getFETokenInfo()); 911 } 912 913 static bool readBit(unsigned &Bits) { 914 bool Value = Bits & 0x1; 915 Bits >>= 1; 916 return Value; 917 } 918 919 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) { 920 using namespace llvm::support; 921 922 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 923 return Reader.getGlobalIdentifierID(F, RawID >> 1); 924 } 925 926 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) { 927 if (!II.isFromAST()) { 928 II.setIsFromAST(); 929 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr; 930 if (isInterestingIdentifier(Reader, II, IsModule)) 931 II.setChangedSinceDeserialization(); 932 } 933 } 934 935 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k, 936 const unsigned char* d, 937 unsigned DataLen) { 938 using namespace llvm::support; 939 940 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 941 bool IsInteresting = RawID & 0x01; 942 943 // Wipe out the "is interesting" bit. 944 RawID = RawID >> 1; 945 946 // Build the IdentifierInfo and link the identifier ID with it. 947 IdentifierInfo *II = KnownII; 948 if (!II) { 949 II = &Reader.getIdentifierTable().getOwn(k); 950 KnownII = II; 951 } 952 markIdentifierFromAST(Reader, *II); 953 Reader.markIdentifierUpToDate(II); 954 955 IdentID ID = Reader.getGlobalIdentifierID(F, RawID); 956 if (!IsInteresting) { 957 // For uninteresting identifiers, there's nothing else to do. Just notify 958 // the reader that we've finished loading this identifier. 959 Reader.SetIdentifierInfo(ID, II); 960 return II; 961 } 962 963 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d); 964 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d); 965 bool CPlusPlusOperatorKeyword = readBit(Bits); 966 bool HasRevertedTokenIDToIdentifier = readBit(Bits); 967 bool HasRevertedBuiltin = readBit(Bits); 968 bool Poisoned = readBit(Bits); 969 bool ExtensionToken = readBit(Bits); 970 bool HadMacroDefinition = readBit(Bits); 971 972 assert(Bits == 0 && "Extra bits in the identifier?"); 973 DataLen -= 8; 974 975 // Set or check the various bits in the IdentifierInfo structure. 976 // Token IDs are read-only. 977 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier) 978 II->revertTokenIDToIdentifier(); 979 if (!F.isModule()) 980 II->setObjCOrBuiltinID(ObjCOrBuiltinID); 981 else if (HasRevertedBuiltin && II->getBuiltinID()) { 982 II->revertBuiltin(); 983 assert((II->hasRevertedBuiltin() || 984 II->getObjCOrBuiltinID() == ObjCOrBuiltinID) && 985 "Incorrect ObjC keyword or builtin ID"); 986 } 987 assert(II->isExtensionToken() == ExtensionToken && 988 "Incorrect extension token flag"); 989 (void)ExtensionToken; 990 if (Poisoned) 991 II->setIsPoisoned(true); 992 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword && 993 "Incorrect C++ operator keyword flag"); 994 (void)CPlusPlusOperatorKeyword; 995 996 // If this identifier is a macro, deserialize the macro 997 // definition. 998 if (HadMacroDefinition) { 999 uint32_t MacroDirectivesOffset = 1000 endian::readNext<uint32_t, little, unaligned>(d); 1001 DataLen -= 4; 1002 1003 Reader.addPendingMacro(II, &F, MacroDirectivesOffset); 1004 } 1005 1006 Reader.SetIdentifierInfo(ID, II); 1007 1008 // Read all of the declarations visible at global scope with this 1009 // name. 1010 if (DataLen > 0) { 1011 SmallVector<uint32_t, 4> DeclIDs; 1012 for (; DataLen > 0; DataLen -= 4) 1013 DeclIDs.push_back(Reader.getGlobalDeclID( 1014 F, endian::readNext<uint32_t, little, unaligned>(d))); 1015 Reader.SetGloballyVisibleDecls(II, DeclIDs); 1016 } 1017 1018 return II; 1019 } 1020 1021 DeclarationNameKey::DeclarationNameKey(DeclarationName Name) 1022 : Kind(Name.getNameKind()) { 1023 switch (Kind) { 1024 case DeclarationName::Identifier: 1025 Data = (uint64_t)Name.getAsIdentifierInfo(); 1026 break; 1027 case DeclarationName::ObjCZeroArgSelector: 1028 case DeclarationName::ObjCOneArgSelector: 1029 case DeclarationName::ObjCMultiArgSelector: 1030 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr(); 1031 break; 1032 case DeclarationName::CXXOperatorName: 1033 Data = Name.getCXXOverloadedOperator(); 1034 break; 1035 case DeclarationName::CXXLiteralOperatorName: 1036 Data = (uint64_t)Name.getCXXLiteralIdentifier(); 1037 break; 1038 case DeclarationName::CXXDeductionGuideName: 1039 Data = (uint64_t)Name.getCXXDeductionGuideTemplate() 1040 ->getDeclName().getAsIdentifierInfo(); 1041 break; 1042 case DeclarationName::CXXConstructorName: 1043 case DeclarationName::CXXDestructorName: 1044 case DeclarationName::CXXConversionFunctionName: 1045 case DeclarationName::CXXUsingDirective: 1046 Data = 0; 1047 break; 1048 } 1049 } 1050 1051 unsigned DeclarationNameKey::getHash() const { 1052 llvm::FoldingSetNodeID ID; 1053 ID.AddInteger(Kind); 1054 1055 switch (Kind) { 1056 case DeclarationName::Identifier: 1057 case DeclarationName::CXXLiteralOperatorName: 1058 case DeclarationName::CXXDeductionGuideName: 1059 ID.AddString(((IdentifierInfo*)Data)->getName()); 1060 break; 1061 case DeclarationName::ObjCZeroArgSelector: 1062 case DeclarationName::ObjCOneArgSelector: 1063 case DeclarationName::ObjCMultiArgSelector: 1064 ID.AddInteger(serialization::ComputeHash(Selector(Data))); 1065 break; 1066 case DeclarationName::CXXOperatorName: 1067 ID.AddInteger((OverloadedOperatorKind)Data); 1068 break; 1069 case DeclarationName::CXXConstructorName: 1070 case DeclarationName::CXXDestructorName: 1071 case DeclarationName::CXXConversionFunctionName: 1072 case DeclarationName::CXXUsingDirective: 1073 break; 1074 } 1075 1076 return ID.ComputeHash(); 1077 } 1078 1079 ModuleFile * 1080 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) { 1081 using namespace llvm::support; 1082 1083 uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d); 1084 return Reader.getLocalModuleFile(F, ModuleFileID); 1085 } 1086 1087 std::pair<unsigned, unsigned> 1088 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) { 1089 using namespace llvm::support; 1090 1091 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 1092 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 1093 return std::make_pair(KeyLen, DataLen); 1094 } 1095 1096 ASTDeclContextNameLookupTrait::internal_key_type 1097 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) { 1098 using namespace llvm::support; 1099 1100 auto Kind = (DeclarationName::NameKind)*d++; 1101 uint64_t Data; 1102 switch (Kind) { 1103 case DeclarationName::Identifier: 1104 case DeclarationName::CXXLiteralOperatorName: 1105 case DeclarationName::CXXDeductionGuideName: 1106 Data = (uint64_t)Reader.getLocalIdentifier( 1107 F, endian::readNext<uint32_t, little, unaligned>(d)); 1108 break; 1109 case DeclarationName::ObjCZeroArgSelector: 1110 case DeclarationName::ObjCOneArgSelector: 1111 case DeclarationName::ObjCMultiArgSelector: 1112 Data = 1113 (uint64_t)Reader.getLocalSelector( 1114 F, endian::readNext<uint32_t, little, unaligned>( 1115 d)).getAsOpaquePtr(); 1116 break; 1117 case DeclarationName::CXXOperatorName: 1118 Data = *d++; // OverloadedOperatorKind 1119 break; 1120 case DeclarationName::CXXConstructorName: 1121 case DeclarationName::CXXDestructorName: 1122 case DeclarationName::CXXConversionFunctionName: 1123 case DeclarationName::CXXUsingDirective: 1124 Data = 0; 1125 break; 1126 } 1127 1128 return DeclarationNameKey(Kind, Data); 1129 } 1130 1131 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type, 1132 const unsigned char *d, 1133 unsigned DataLen, 1134 data_type_builder &Val) { 1135 using namespace llvm::support; 1136 1137 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) { 1138 uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d); 1139 Val.insert(Reader.getGlobalDeclID(F, LocalID)); 1140 } 1141 } 1142 1143 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M, 1144 BitstreamCursor &Cursor, 1145 uint64_t Offset, 1146 DeclContext *DC) { 1147 assert(Offset != 0); 1148 1149 SavedStreamPosition SavedPosition(Cursor); 1150 Cursor.JumpToBit(Offset); 1151 1152 RecordData Record; 1153 StringRef Blob; 1154 unsigned Code = Cursor.ReadCode(); 1155 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob); 1156 if (RecCode != DECL_CONTEXT_LEXICAL) { 1157 Error("Expected lexical block"); 1158 return true; 1159 } 1160 1161 assert(!isa<TranslationUnitDecl>(DC) && 1162 "expected a TU_UPDATE_LEXICAL record for TU"); 1163 // If we are handling a C++ class template instantiation, we can see multiple 1164 // lexical updates for the same record. It's important that we select only one 1165 // of them, so that field numbering works properly. Just pick the first one we 1166 // see. 1167 auto &Lex = LexicalDecls[DC]; 1168 if (!Lex.first) { 1169 Lex = std::make_pair( 1170 &M, llvm::makeArrayRef( 1171 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 1172 Blob.data()), 1173 Blob.size() / 4)); 1174 } 1175 DC->setHasExternalLexicalStorage(true); 1176 return false; 1177 } 1178 1179 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M, 1180 BitstreamCursor &Cursor, 1181 uint64_t Offset, 1182 DeclID ID) { 1183 assert(Offset != 0); 1184 1185 SavedStreamPosition SavedPosition(Cursor); 1186 Cursor.JumpToBit(Offset); 1187 1188 RecordData Record; 1189 StringRef Blob; 1190 unsigned Code = Cursor.ReadCode(); 1191 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob); 1192 if (RecCode != DECL_CONTEXT_VISIBLE) { 1193 Error("Expected visible lookup table block"); 1194 return true; 1195 } 1196 1197 // We can't safely determine the primary context yet, so delay attaching the 1198 // lookup table until we're done with recursive deserialization. 1199 auto *Data = (const unsigned char*)Blob.data(); 1200 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data}); 1201 return false; 1202 } 1203 1204 void ASTReader::Error(StringRef Msg) const { 1205 Error(diag::err_fe_pch_malformed, Msg); 1206 if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() && 1207 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) { 1208 Diag(diag::note_module_cache_path) 1209 << PP.getHeaderSearchInfo().getModuleCachePath(); 1210 } 1211 } 1212 1213 void ASTReader::Error(unsigned DiagID, 1214 StringRef Arg1, StringRef Arg2) const { 1215 if (Diags.isDiagnosticInFlight()) 1216 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2); 1217 else 1218 Diag(DiagID) << Arg1 << Arg2; 1219 } 1220 1221 //===----------------------------------------------------------------------===// 1222 // Source Manager Deserialization 1223 //===----------------------------------------------------------------------===// 1224 1225 /// Read the line table in the source manager block. 1226 /// \returns true if there was an error. 1227 bool ASTReader::ParseLineTable(ModuleFile &F, 1228 const RecordData &Record) { 1229 unsigned Idx = 0; 1230 LineTableInfo &LineTable = SourceMgr.getLineTable(); 1231 1232 // Parse the file names 1233 std::map<int, int> FileIDs; 1234 FileIDs[-1] = -1; // For unspecified filenames. 1235 for (unsigned I = 0; Record[Idx]; ++I) { 1236 // Extract the file name 1237 auto Filename = ReadPath(F, Record, Idx); 1238 FileIDs[I] = LineTable.getLineTableFilenameID(Filename); 1239 } 1240 ++Idx; 1241 1242 // Parse the line entries 1243 std::vector<LineEntry> Entries; 1244 while (Idx < Record.size()) { 1245 int FID = Record[Idx++]; 1246 assert(FID >= 0 && "Serialized line entries for non-local file."); 1247 // Remap FileID from 1-based old view. 1248 FID += F.SLocEntryBaseID - 1; 1249 1250 // Extract the line entries 1251 unsigned NumEntries = Record[Idx++]; 1252 assert(NumEntries && "no line entries for file ID"); 1253 Entries.clear(); 1254 Entries.reserve(NumEntries); 1255 for (unsigned I = 0; I != NumEntries; ++I) { 1256 unsigned FileOffset = Record[Idx++]; 1257 unsigned LineNo = Record[Idx++]; 1258 int FilenameID = FileIDs[Record[Idx++]]; 1259 SrcMgr::CharacteristicKind FileKind 1260 = (SrcMgr::CharacteristicKind)Record[Idx++]; 1261 unsigned IncludeOffset = Record[Idx++]; 1262 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, 1263 FileKind, IncludeOffset)); 1264 } 1265 LineTable.AddEntry(FileID::get(FID), Entries); 1266 } 1267 1268 return false; 1269 } 1270 1271 /// Read a source manager block 1272 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) { 1273 using namespace SrcMgr; 1274 1275 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor; 1276 1277 // Set the source-location entry cursor to the current position in 1278 // the stream. This cursor will be used to read the contents of the 1279 // source manager block initially, and then lazily read 1280 // source-location entries as needed. 1281 SLocEntryCursor = F.Stream; 1282 1283 // The stream itself is going to skip over the source manager block. 1284 if (F.Stream.SkipBlock()) { 1285 Error("malformed block record in AST file"); 1286 return true; 1287 } 1288 1289 // Enter the source manager block. 1290 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) { 1291 Error("malformed source manager block record in AST file"); 1292 return true; 1293 } 1294 1295 RecordData Record; 1296 while (true) { 1297 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks(); 1298 1299 switch (E.Kind) { 1300 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1301 case llvm::BitstreamEntry::Error: 1302 Error("malformed block record in AST file"); 1303 return true; 1304 case llvm::BitstreamEntry::EndBlock: 1305 return false; 1306 case llvm::BitstreamEntry::Record: 1307 // The interesting case. 1308 break; 1309 } 1310 1311 // Read a record. 1312 Record.clear(); 1313 StringRef Blob; 1314 switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) { 1315 default: // Default behavior: ignore. 1316 break; 1317 1318 case SM_SLOC_FILE_ENTRY: 1319 case SM_SLOC_BUFFER_ENTRY: 1320 case SM_SLOC_EXPANSION_ENTRY: 1321 // Once we hit one of the source location entries, we're done. 1322 return false; 1323 } 1324 } 1325 } 1326 1327 /// If a header file is not found at the path that we expect it to be 1328 /// and the PCH file was moved from its original location, try to resolve the 1329 /// file by assuming that header+PCH were moved together and the header is in 1330 /// the same place relative to the PCH. 1331 static std::string 1332 resolveFileRelativeToOriginalDir(const std::string &Filename, 1333 const std::string &OriginalDir, 1334 const std::string &CurrDir) { 1335 assert(OriginalDir != CurrDir && 1336 "No point trying to resolve the file if the PCH dir didn't change"); 1337 1338 using namespace llvm::sys; 1339 1340 SmallString<128> filePath(Filename); 1341 fs::make_absolute(filePath); 1342 assert(path::is_absolute(OriginalDir)); 1343 SmallString<128> currPCHPath(CurrDir); 1344 1345 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)), 1346 fileDirE = path::end(path::parent_path(filePath)); 1347 path::const_iterator origDirI = path::begin(OriginalDir), 1348 origDirE = path::end(OriginalDir); 1349 // Skip the common path components from filePath and OriginalDir. 1350 while (fileDirI != fileDirE && origDirI != origDirE && 1351 *fileDirI == *origDirI) { 1352 ++fileDirI; 1353 ++origDirI; 1354 } 1355 for (; origDirI != origDirE; ++origDirI) 1356 path::append(currPCHPath, ".."); 1357 path::append(currPCHPath, fileDirI, fileDirE); 1358 path::append(currPCHPath, path::filename(Filename)); 1359 return currPCHPath.str(); 1360 } 1361 1362 bool ASTReader::ReadSLocEntry(int ID) { 1363 if (ID == 0) 1364 return false; 1365 1366 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1367 Error("source location entry ID out-of-range for AST file"); 1368 return true; 1369 } 1370 1371 // Local helper to read the (possibly-compressed) buffer data following the 1372 // entry record. 1373 auto ReadBuffer = [this]( 1374 BitstreamCursor &SLocEntryCursor, 1375 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> { 1376 RecordData Record; 1377 StringRef Blob; 1378 unsigned Code = SLocEntryCursor.ReadCode(); 1379 unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob); 1380 1381 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) { 1382 if (!llvm::zlib::isAvailable()) { 1383 Error("zlib is not available"); 1384 return nullptr; 1385 } 1386 SmallString<0> Uncompressed; 1387 if (llvm::Error E = 1388 llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) { 1389 Error("could not decompress embedded file contents: " + 1390 llvm::toString(std::move(E))); 1391 return nullptr; 1392 } 1393 return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name); 1394 } else if (RecCode == SM_SLOC_BUFFER_BLOB) { 1395 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true); 1396 } else { 1397 Error("AST record has invalid code"); 1398 return nullptr; 1399 } 1400 }; 1401 1402 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second; 1403 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]); 1404 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor; 1405 unsigned BaseOffset = F->SLocEntryBaseOffset; 1406 1407 ++NumSLocEntriesRead; 1408 llvm::BitstreamEntry Entry = SLocEntryCursor.advance(); 1409 if (Entry.Kind != llvm::BitstreamEntry::Record) { 1410 Error("incorrectly-formatted source location entry in AST file"); 1411 return true; 1412 } 1413 1414 RecordData Record; 1415 StringRef Blob; 1416 switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) { 1417 default: 1418 Error("incorrectly-formatted source location entry in AST file"); 1419 return true; 1420 1421 case SM_SLOC_FILE_ENTRY: { 1422 // We will detect whether a file changed and return 'Failure' for it, but 1423 // we will also try to fail gracefully by setting up the SLocEntry. 1424 unsigned InputID = Record[4]; 1425 InputFile IF = getInputFile(*F, InputID); 1426 const FileEntry *File = IF.getFile(); 1427 bool OverriddenBuffer = IF.isOverridden(); 1428 1429 // Note that we only check if a File was returned. If it was out-of-date 1430 // we have complained but we will continue creating a FileID to recover 1431 // gracefully. 1432 if (!File) 1433 return true; 1434 1435 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1436 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) { 1437 // This is the module's main file. 1438 IncludeLoc = getImportLocation(F); 1439 } 1440 SrcMgr::CharacteristicKind 1441 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1442 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter, 1443 ID, BaseOffset + Record[0]); 1444 SrcMgr::FileInfo &FileInfo = 1445 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()); 1446 FileInfo.NumCreatedFIDs = Record[5]; 1447 if (Record[3]) 1448 FileInfo.setHasLineDirectives(); 1449 1450 const DeclID *FirstDecl = F->FileSortedDecls + Record[6]; 1451 unsigned NumFileDecls = Record[7]; 1452 if (NumFileDecls && ContextObj) { 1453 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?"); 1454 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl, 1455 NumFileDecls)); 1456 } 1457 1458 const SrcMgr::ContentCache *ContentCache 1459 = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter)); 1460 if (OverriddenBuffer && !ContentCache->BufferOverridden && 1461 ContentCache->ContentsEntry == ContentCache->OrigEntry && 1462 !ContentCache->getRawBuffer()) { 1463 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName()); 1464 if (!Buffer) 1465 return true; 1466 SourceMgr.overrideFileContents(File, std::move(Buffer)); 1467 } 1468 1469 break; 1470 } 1471 1472 case SM_SLOC_BUFFER_ENTRY: { 1473 const char *Name = Blob.data(); 1474 unsigned Offset = Record[0]; 1475 SrcMgr::CharacteristicKind 1476 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1477 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1478 if (IncludeLoc.isInvalid() && F->isModule()) { 1479 IncludeLoc = getImportLocation(F); 1480 } 1481 1482 auto Buffer = ReadBuffer(SLocEntryCursor, Name); 1483 if (!Buffer) 1484 return true; 1485 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID, 1486 BaseOffset + Offset, IncludeLoc); 1487 break; 1488 } 1489 1490 case SM_SLOC_EXPANSION_ENTRY: { 1491 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]); 1492 SourceMgr.createExpansionLoc(SpellingLoc, 1493 ReadSourceLocation(*F, Record[2]), 1494 ReadSourceLocation(*F, Record[3]), 1495 Record[5], 1496 Record[4], 1497 ID, 1498 BaseOffset + Record[0]); 1499 break; 1500 } 1501 } 1502 1503 return false; 1504 } 1505 1506 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) { 1507 if (ID == 0) 1508 return std::make_pair(SourceLocation(), ""); 1509 1510 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1511 Error("source location entry ID out-of-range for AST file"); 1512 return std::make_pair(SourceLocation(), ""); 1513 } 1514 1515 // Find which module file this entry lands in. 1516 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second; 1517 if (!M->isModule()) 1518 return std::make_pair(SourceLocation(), ""); 1519 1520 // FIXME: Can we map this down to a particular submodule? That would be 1521 // ideal. 1522 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName)); 1523 } 1524 1525 /// Find the location where the module F is imported. 1526 SourceLocation ASTReader::getImportLocation(ModuleFile *F) { 1527 if (F->ImportLoc.isValid()) 1528 return F->ImportLoc; 1529 1530 // Otherwise we have a PCH. It's considered to be "imported" at the first 1531 // location of its includer. 1532 if (F->ImportedBy.empty() || !F->ImportedBy[0]) { 1533 // Main file is the importer. 1534 assert(SourceMgr.getMainFileID().isValid() && "missing main file"); 1535 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); 1536 } 1537 return F->ImportedBy[0]->FirstLoc; 1538 } 1539 1540 /// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the 1541 /// specified cursor. Read the abbreviations that are at the top of the block 1542 /// and then leave the cursor pointing into the block. 1543 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) { 1544 if (Cursor.EnterSubBlock(BlockID)) 1545 return true; 1546 1547 while (true) { 1548 uint64_t Offset = Cursor.GetCurrentBitNo(); 1549 unsigned Code = Cursor.ReadCode(); 1550 1551 // We expect all abbrevs to be at the start of the block. 1552 if (Code != llvm::bitc::DEFINE_ABBREV) { 1553 Cursor.JumpToBit(Offset); 1554 return false; 1555 } 1556 Cursor.ReadAbbrevRecord(); 1557 } 1558 } 1559 1560 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record, 1561 unsigned &Idx) { 1562 Token Tok; 1563 Tok.startToken(); 1564 Tok.setLocation(ReadSourceLocation(F, Record, Idx)); 1565 Tok.setLength(Record[Idx++]); 1566 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++])) 1567 Tok.setIdentifierInfo(II); 1568 Tok.setKind((tok::TokenKind)Record[Idx++]); 1569 Tok.setFlag((Token::TokenFlags)Record[Idx++]); 1570 return Tok; 1571 } 1572 1573 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) { 1574 BitstreamCursor &Stream = F.MacroCursor; 1575 1576 // Keep track of where we are in the stream, then jump back there 1577 // after reading this macro. 1578 SavedStreamPosition SavedPosition(Stream); 1579 1580 Stream.JumpToBit(Offset); 1581 RecordData Record; 1582 SmallVector<IdentifierInfo*, 16> MacroParams; 1583 MacroInfo *Macro = nullptr; 1584 1585 while (true) { 1586 // Advance to the next record, but if we get to the end of the block, don't 1587 // pop it (removing all the abbreviations from the cursor) since we want to 1588 // be able to reseek within the block and read entries. 1589 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd; 1590 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags); 1591 1592 switch (Entry.Kind) { 1593 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1594 case llvm::BitstreamEntry::Error: 1595 Error("malformed block record in AST file"); 1596 return Macro; 1597 case llvm::BitstreamEntry::EndBlock: 1598 return Macro; 1599 case llvm::BitstreamEntry::Record: 1600 // The interesting case. 1601 break; 1602 } 1603 1604 // Read a record. 1605 Record.clear(); 1606 PreprocessorRecordTypes RecType = 1607 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record); 1608 switch (RecType) { 1609 case PP_MODULE_MACRO: 1610 case PP_MACRO_DIRECTIVE_HISTORY: 1611 return Macro; 1612 1613 case PP_MACRO_OBJECT_LIKE: 1614 case PP_MACRO_FUNCTION_LIKE: { 1615 // If we already have a macro, that means that we've hit the end 1616 // of the definition of the macro we were looking for. We're 1617 // done. 1618 if (Macro) 1619 return Macro; 1620 1621 unsigned NextIndex = 1; // Skip identifier ID. 1622 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex); 1623 MacroInfo *MI = PP.AllocateMacroInfo(Loc); 1624 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex)); 1625 MI->setIsUsed(Record[NextIndex++]); 1626 MI->setUsedForHeaderGuard(Record[NextIndex++]); 1627 1628 if (RecType == PP_MACRO_FUNCTION_LIKE) { 1629 // Decode function-like macro info. 1630 bool isC99VarArgs = Record[NextIndex++]; 1631 bool isGNUVarArgs = Record[NextIndex++]; 1632 bool hasCommaPasting = Record[NextIndex++]; 1633 MacroParams.clear(); 1634 unsigned NumArgs = Record[NextIndex++]; 1635 for (unsigned i = 0; i != NumArgs; ++i) 1636 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++])); 1637 1638 // Install function-like macro info. 1639 MI->setIsFunctionLike(); 1640 if (isC99VarArgs) MI->setIsC99Varargs(); 1641 if (isGNUVarArgs) MI->setIsGNUVarargs(); 1642 if (hasCommaPasting) MI->setHasCommaPasting(); 1643 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator()); 1644 } 1645 1646 // Remember that we saw this macro last so that we add the tokens that 1647 // form its body to it. 1648 Macro = MI; 1649 1650 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() && 1651 Record[NextIndex]) { 1652 // We have a macro definition. Register the association 1653 PreprocessedEntityID 1654 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]); 1655 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 1656 PreprocessingRecord::PPEntityID PPID = 1657 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true); 1658 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>( 1659 PPRec.getPreprocessedEntity(PPID)); 1660 if (PPDef) 1661 PPRec.RegisterMacroDefinition(Macro, PPDef); 1662 } 1663 1664 ++NumMacrosRead; 1665 break; 1666 } 1667 1668 case PP_TOKEN: { 1669 // If we see a TOKEN before a PP_MACRO_*, then the file is 1670 // erroneous, just pretend we didn't see this. 1671 if (!Macro) break; 1672 1673 unsigned Idx = 0; 1674 Token Tok = ReadToken(F, Record, Idx); 1675 Macro->AddTokenToBody(Tok); 1676 break; 1677 } 1678 } 1679 } 1680 } 1681 1682 PreprocessedEntityID 1683 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, 1684 unsigned LocalID) const { 1685 if (!M.ModuleOffsetMap.empty()) 1686 ReadModuleOffsetMap(M); 1687 1688 ContinuousRangeMap<uint32_t, int, 2>::const_iterator 1689 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS); 1690 assert(I != M.PreprocessedEntityRemap.end() 1691 && "Invalid index into preprocessed entity index remap"); 1692 1693 return LocalID + I->second; 1694 } 1695 1696 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) { 1697 return llvm::hash_combine(ikey.Size, ikey.ModTime); 1698 } 1699 1700 HeaderFileInfoTrait::internal_key_type 1701 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) { 1702 internal_key_type ikey = {FE->getSize(), 1703 M.HasTimestamps ? FE->getModificationTime() : 0, 1704 FE->getName(), /*Imported*/ false}; 1705 return ikey; 1706 } 1707 1708 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) { 1709 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime)) 1710 return false; 1711 1712 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename) 1713 return true; 1714 1715 // Determine whether the actual files are equivalent. 1716 FileManager &FileMgr = Reader.getFileManager(); 1717 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* { 1718 if (!Key.Imported) 1719 return FileMgr.getFile(Key.Filename); 1720 1721 std::string Resolved = Key.Filename; 1722 Reader.ResolveImportedPath(M, Resolved); 1723 return FileMgr.getFile(Resolved); 1724 }; 1725 1726 const FileEntry *FEA = GetFile(a); 1727 const FileEntry *FEB = GetFile(b); 1728 return FEA && FEA == FEB; 1729 } 1730 1731 std::pair<unsigned, unsigned> 1732 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) { 1733 using namespace llvm::support; 1734 1735 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d); 1736 unsigned DataLen = (unsigned) *d++; 1737 return std::make_pair(KeyLen, DataLen); 1738 } 1739 1740 HeaderFileInfoTrait::internal_key_type 1741 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) { 1742 using namespace llvm::support; 1743 1744 internal_key_type ikey; 1745 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d)); 1746 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d)); 1747 ikey.Filename = (const char *)d; 1748 ikey.Imported = true; 1749 return ikey; 1750 } 1751 1752 HeaderFileInfoTrait::data_type 1753 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d, 1754 unsigned DataLen) { 1755 using namespace llvm::support; 1756 1757 const unsigned char *End = d + DataLen; 1758 HeaderFileInfo HFI; 1759 unsigned Flags = *d++; 1760 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp. 1761 HFI.isImport |= (Flags >> 5) & 0x01; 1762 HFI.isPragmaOnce |= (Flags >> 4) & 0x01; 1763 HFI.DirInfo = (Flags >> 1) & 0x07; 1764 HFI.IndexHeaderMapHeader = Flags & 0x01; 1765 // FIXME: Find a better way to handle this. Maybe just store a 1766 // "has been included" flag? 1767 HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d), 1768 HFI.NumIncludes); 1769 HFI.ControllingMacroID = Reader.getGlobalIdentifierID( 1770 M, endian::readNext<uint32_t, little, unaligned>(d)); 1771 if (unsigned FrameworkOffset = 1772 endian::readNext<uint32_t, little, unaligned>(d)) { 1773 // The framework offset is 1 greater than the actual offset, 1774 // since 0 is used as an indicator for "no framework name". 1775 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1); 1776 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName); 1777 } 1778 1779 assert((End - d) % 4 == 0 && 1780 "Wrong data length in HeaderFileInfo deserialization"); 1781 while (d != End) { 1782 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d); 1783 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3); 1784 LocalSMID >>= 2; 1785 1786 // This header is part of a module. Associate it with the module to enable 1787 // implicit module import. 1788 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID); 1789 Module *Mod = Reader.getSubmodule(GlobalSMID); 1790 FileManager &FileMgr = Reader.getFileManager(); 1791 ModuleMap &ModMap = 1792 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap(); 1793 1794 std::string Filename = key.Filename; 1795 if (key.Imported) 1796 Reader.ResolveImportedPath(M, Filename); 1797 // FIXME: This is not always the right filename-as-written, but we're not 1798 // going to use this information to rebuild the module, so it doesn't make 1799 // a lot of difference. 1800 Module::Header H = { key.Filename, FileMgr.getFile(Filename) }; 1801 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true); 1802 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader); 1803 } 1804 1805 // This HeaderFileInfo was externally loaded. 1806 HFI.External = true; 1807 HFI.IsValid = true; 1808 return HFI; 1809 } 1810 1811 void ASTReader::addPendingMacro(IdentifierInfo *II, 1812 ModuleFile *M, 1813 uint64_t MacroDirectivesOffset) { 1814 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard"); 1815 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset)); 1816 } 1817 1818 void ASTReader::ReadDefinedMacros() { 1819 // Note that we are loading defined macros. 1820 Deserializing Macros(this); 1821 1822 for (ModuleFile &I : llvm::reverse(ModuleMgr)) { 1823 BitstreamCursor &MacroCursor = I.MacroCursor; 1824 1825 // If there was no preprocessor block, skip this file. 1826 if (MacroCursor.getBitcodeBytes().empty()) 1827 continue; 1828 1829 BitstreamCursor Cursor = MacroCursor; 1830 Cursor.JumpToBit(I.MacroStartOffset); 1831 1832 RecordData Record; 1833 while (true) { 1834 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks(); 1835 1836 switch (E.Kind) { 1837 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1838 case llvm::BitstreamEntry::Error: 1839 Error("malformed block record in AST file"); 1840 return; 1841 case llvm::BitstreamEntry::EndBlock: 1842 goto NextCursor; 1843 1844 case llvm::BitstreamEntry::Record: 1845 Record.clear(); 1846 switch (Cursor.readRecord(E.ID, Record)) { 1847 default: // Default behavior: ignore. 1848 break; 1849 1850 case PP_MACRO_OBJECT_LIKE: 1851 case PP_MACRO_FUNCTION_LIKE: { 1852 IdentifierInfo *II = getLocalIdentifier(I, Record[0]); 1853 if (II->isOutOfDate()) 1854 updateOutOfDateIdentifier(*II); 1855 break; 1856 } 1857 1858 case PP_TOKEN: 1859 // Ignore tokens. 1860 break; 1861 } 1862 break; 1863 } 1864 } 1865 NextCursor: ; 1866 } 1867 } 1868 1869 namespace { 1870 1871 /// Visitor class used to look up identifirs in an AST file. 1872 class IdentifierLookupVisitor { 1873 StringRef Name; 1874 unsigned NameHash; 1875 unsigned PriorGeneration; 1876 unsigned &NumIdentifierLookups; 1877 unsigned &NumIdentifierLookupHits; 1878 IdentifierInfo *Found = nullptr; 1879 1880 public: 1881 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration, 1882 unsigned &NumIdentifierLookups, 1883 unsigned &NumIdentifierLookupHits) 1884 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)), 1885 PriorGeneration(PriorGeneration), 1886 NumIdentifierLookups(NumIdentifierLookups), 1887 NumIdentifierLookupHits(NumIdentifierLookupHits) {} 1888 1889 bool operator()(ModuleFile &M) { 1890 // If we've already searched this module file, skip it now. 1891 if (M.Generation <= PriorGeneration) 1892 return true; 1893 1894 ASTIdentifierLookupTable *IdTable 1895 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable; 1896 if (!IdTable) 1897 return false; 1898 1899 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M, 1900 Found); 1901 ++NumIdentifierLookups; 1902 ASTIdentifierLookupTable::iterator Pos = 1903 IdTable->find_hashed(Name, NameHash, &Trait); 1904 if (Pos == IdTable->end()) 1905 return false; 1906 1907 // Dereferencing the iterator has the effect of building the 1908 // IdentifierInfo node and populating it with the various 1909 // declarations it needs. 1910 ++NumIdentifierLookupHits; 1911 Found = *Pos; 1912 return true; 1913 } 1914 1915 // Retrieve the identifier info found within the module 1916 // files. 1917 IdentifierInfo *getIdentifierInfo() const { return Found; } 1918 }; 1919 1920 } // namespace 1921 1922 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) { 1923 // Note that we are loading an identifier. 1924 Deserializing AnIdentifier(this); 1925 1926 unsigned PriorGeneration = 0; 1927 if (getContext().getLangOpts().Modules) 1928 PriorGeneration = IdentifierGeneration[&II]; 1929 1930 // If there is a global index, look there first to determine which modules 1931 // provably do not have any results for this identifier. 1932 GlobalModuleIndex::HitSet Hits; 1933 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 1934 if (!loadGlobalIndex()) { 1935 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) { 1936 HitsPtr = &Hits; 1937 } 1938 } 1939 1940 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration, 1941 NumIdentifierLookups, 1942 NumIdentifierLookupHits); 1943 ModuleMgr.visit(Visitor, HitsPtr); 1944 markIdentifierUpToDate(&II); 1945 } 1946 1947 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) { 1948 if (!II) 1949 return; 1950 1951 II->setOutOfDate(false); 1952 1953 // Update the generation for this identifier. 1954 if (getContext().getLangOpts().Modules) 1955 IdentifierGeneration[II] = getGeneration(); 1956 } 1957 1958 void ASTReader::resolvePendingMacro(IdentifierInfo *II, 1959 const PendingMacroInfo &PMInfo) { 1960 ModuleFile &M = *PMInfo.M; 1961 1962 BitstreamCursor &Cursor = M.MacroCursor; 1963 SavedStreamPosition SavedPosition(Cursor); 1964 Cursor.JumpToBit(PMInfo.MacroDirectivesOffset); 1965 1966 struct ModuleMacroRecord { 1967 SubmoduleID SubModID; 1968 MacroInfo *MI; 1969 SmallVector<SubmoduleID, 8> Overrides; 1970 }; 1971 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros; 1972 1973 // We expect to see a sequence of PP_MODULE_MACRO records listing exported 1974 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete 1975 // macro histroy. 1976 RecordData Record; 1977 while (true) { 1978 llvm::BitstreamEntry Entry = 1979 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 1980 if (Entry.Kind != llvm::BitstreamEntry::Record) { 1981 Error("malformed block record in AST file"); 1982 return; 1983 } 1984 1985 Record.clear(); 1986 switch ((PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record)) { 1987 case PP_MACRO_DIRECTIVE_HISTORY: 1988 break; 1989 1990 case PP_MODULE_MACRO: { 1991 ModuleMacros.push_back(ModuleMacroRecord()); 1992 auto &Info = ModuleMacros.back(); 1993 Info.SubModID = getGlobalSubmoduleID(M, Record[0]); 1994 Info.MI = getMacro(getGlobalMacroID(M, Record[1])); 1995 for (int I = 2, N = Record.size(); I != N; ++I) 1996 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I])); 1997 continue; 1998 } 1999 2000 default: 2001 Error("malformed block record in AST file"); 2002 return; 2003 } 2004 2005 // We found the macro directive history; that's the last record 2006 // for this macro. 2007 break; 2008 } 2009 2010 // Module macros are listed in reverse dependency order. 2011 { 2012 std::reverse(ModuleMacros.begin(), ModuleMacros.end()); 2013 llvm::SmallVector<ModuleMacro*, 8> Overrides; 2014 for (auto &MMR : ModuleMacros) { 2015 Overrides.clear(); 2016 for (unsigned ModID : MMR.Overrides) { 2017 Module *Mod = getSubmodule(ModID); 2018 auto *Macro = PP.getModuleMacro(Mod, II); 2019 assert(Macro && "missing definition for overridden macro"); 2020 Overrides.push_back(Macro); 2021 } 2022 2023 bool Inserted = false; 2024 Module *Owner = getSubmodule(MMR.SubModID); 2025 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted); 2026 } 2027 } 2028 2029 // Don't read the directive history for a module; we don't have anywhere 2030 // to put it. 2031 if (M.isModule()) 2032 return; 2033 2034 // Deserialize the macro directives history in reverse source-order. 2035 MacroDirective *Latest = nullptr, *Earliest = nullptr; 2036 unsigned Idx = 0, N = Record.size(); 2037 while (Idx < N) { 2038 MacroDirective *MD = nullptr; 2039 SourceLocation Loc = ReadSourceLocation(M, Record, Idx); 2040 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++]; 2041 switch (K) { 2042 case MacroDirective::MD_Define: { 2043 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++])); 2044 MD = PP.AllocateDefMacroDirective(MI, Loc); 2045 break; 2046 } 2047 case MacroDirective::MD_Undefine: 2048 MD = PP.AllocateUndefMacroDirective(Loc); 2049 break; 2050 case MacroDirective::MD_Visibility: 2051 bool isPublic = Record[Idx++]; 2052 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic); 2053 break; 2054 } 2055 2056 if (!Latest) 2057 Latest = MD; 2058 if (Earliest) 2059 Earliest->setPrevious(MD); 2060 Earliest = MD; 2061 } 2062 2063 if (Latest) 2064 PP.setLoadedMacroDirective(II, Earliest, Latest); 2065 } 2066 2067 ASTReader::InputFileInfo 2068 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) { 2069 // Go find this input file. 2070 BitstreamCursor &Cursor = F.InputFilesCursor; 2071 SavedStreamPosition SavedPosition(Cursor); 2072 Cursor.JumpToBit(F.InputFileOffsets[ID-1]); 2073 2074 unsigned Code = Cursor.ReadCode(); 2075 RecordData Record; 2076 StringRef Blob; 2077 2078 unsigned Result = Cursor.readRecord(Code, Record, &Blob); 2079 assert(static_cast<InputFileRecordTypes>(Result) == INPUT_FILE && 2080 "invalid record type for input file"); 2081 (void)Result; 2082 2083 assert(Record[0] == ID && "Bogus stored ID or offset"); 2084 InputFileInfo R; 2085 R.StoredSize = static_cast<off_t>(Record[1]); 2086 R.StoredTime = static_cast<time_t>(Record[2]); 2087 R.Overridden = static_cast<bool>(Record[3]); 2088 R.Transient = static_cast<bool>(Record[4]); 2089 R.TopLevelModuleMap = static_cast<bool>(Record[5]); 2090 R.Filename = Blob; 2091 ResolveImportedPath(F, R.Filename); 2092 return R; 2093 } 2094 2095 static unsigned moduleKindForDiagnostic(ModuleKind Kind); 2096 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) { 2097 // If this ID is bogus, just return an empty input file. 2098 if (ID == 0 || ID > F.InputFilesLoaded.size()) 2099 return InputFile(); 2100 2101 // If we've already loaded this input file, return it. 2102 if (F.InputFilesLoaded[ID-1].getFile()) 2103 return F.InputFilesLoaded[ID-1]; 2104 2105 if (F.InputFilesLoaded[ID-1].isNotFound()) 2106 return InputFile(); 2107 2108 // Go find this input file. 2109 BitstreamCursor &Cursor = F.InputFilesCursor; 2110 SavedStreamPosition SavedPosition(Cursor); 2111 Cursor.JumpToBit(F.InputFileOffsets[ID-1]); 2112 2113 InputFileInfo FI = readInputFileInfo(F, ID); 2114 off_t StoredSize = FI.StoredSize; 2115 time_t StoredTime = FI.StoredTime; 2116 bool Overridden = FI.Overridden; 2117 bool Transient = FI.Transient; 2118 StringRef Filename = FI.Filename; 2119 2120 const FileEntry *File = FileMgr.getFile(Filename, /*OpenFile=*/false); 2121 // If we didn't find the file, resolve it relative to the 2122 // original directory from which this AST file was created. 2123 if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() && 2124 F.OriginalDir != F.BaseDirectory) { 2125 std::string Resolved = resolveFileRelativeToOriginalDir( 2126 Filename, F.OriginalDir, F.BaseDirectory); 2127 if (!Resolved.empty()) 2128 File = FileMgr.getFile(Resolved); 2129 } 2130 2131 // For an overridden file, create a virtual file with the stored 2132 // size/timestamp. 2133 if ((Overridden || Transient) && File == nullptr) 2134 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime); 2135 2136 if (File == nullptr) { 2137 if (Complain) { 2138 std::string ErrorStr = "could not find file '"; 2139 ErrorStr += Filename; 2140 ErrorStr += "' referenced by AST file '"; 2141 ErrorStr += F.FileName; 2142 ErrorStr += "'"; 2143 Error(ErrorStr); 2144 } 2145 // Record that we didn't find the file. 2146 F.InputFilesLoaded[ID-1] = InputFile::getNotFound(); 2147 return InputFile(); 2148 } 2149 2150 // Check if there was a request to override the contents of the file 2151 // that was part of the precompiled header. Overriding such a file 2152 // can lead to problems when lexing using the source locations from the 2153 // PCH. 2154 SourceManager &SM = getSourceManager(); 2155 // FIXME: Reject if the overrides are different. 2156 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) { 2157 if (Complain) 2158 Error(diag::err_fe_pch_file_overridden, Filename); 2159 // After emitting the diagnostic, recover by disabling the override so 2160 // that the original file will be used. 2161 // 2162 // FIXME: This recovery is just as broken as the original state; there may 2163 // be another precompiled module that's using the overridden contents, or 2164 // we might be half way through parsing it. Instead, we should treat the 2165 // overridden contents as belonging to a separate FileEntry. 2166 SM.disableFileContentsOverride(File); 2167 // The FileEntry is a virtual file entry with the size of the contents 2168 // that would override the original contents. Set it to the original's 2169 // size/time. 2170 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File), 2171 StoredSize, StoredTime); 2172 } 2173 2174 bool IsOutOfDate = false; 2175 2176 // For an overridden file, there is nothing to validate. 2177 if (!Overridden && // 2178 (StoredSize != File->getSize() || 2179 (StoredTime && StoredTime != File->getModificationTime() && 2180 !DisableValidation) 2181 )) { 2182 if (Complain) { 2183 // Build a list of the PCH imports that got us here (in reverse). 2184 SmallVector<ModuleFile *, 4> ImportStack(1, &F); 2185 while (!ImportStack.back()->ImportedBy.empty()) 2186 ImportStack.push_back(ImportStack.back()->ImportedBy[0]); 2187 2188 // The top-level PCH is stale. 2189 StringRef TopLevelPCHName(ImportStack.back()->FileName); 2190 unsigned DiagnosticKind = moduleKindForDiagnostic(ImportStack.back()->Kind); 2191 if (DiagnosticKind == 0) 2192 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName); 2193 else if (DiagnosticKind == 1) 2194 Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName); 2195 else 2196 Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName); 2197 2198 // Print the import stack. 2199 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) { 2200 Diag(diag::note_pch_required_by) 2201 << Filename << ImportStack[0]->FileName; 2202 for (unsigned I = 1; I < ImportStack.size(); ++I) 2203 Diag(diag::note_pch_required_by) 2204 << ImportStack[I-1]->FileName << ImportStack[I]->FileName; 2205 } 2206 2207 if (!Diags.isDiagnosticInFlight()) 2208 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName; 2209 } 2210 2211 IsOutOfDate = true; 2212 } 2213 // FIXME: If the file is overridden and we've already opened it, 2214 // issue an error (or split it into a separate FileEntry). 2215 2216 InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate); 2217 2218 // Note that we've loaded this input file. 2219 F.InputFilesLoaded[ID-1] = IF; 2220 return IF; 2221 } 2222 2223 /// If we are loading a relocatable PCH or module file, and the filename 2224 /// is not an absolute path, add the system or module root to the beginning of 2225 /// the file name. 2226 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) { 2227 // Resolve relative to the base directory, if we have one. 2228 if (!M.BaseDirectory.empty()) 2229 return ResolveImportedPath(Filename, M.BaseDirectory); 2230 } 2231 2232 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) { 2233 if (Filename.empty() || llvm::sys::path::is_absolute(Filename)) 2234 return; 2235 2236 SmallString<128> Buffer; 2237 llvm::sys::path::append(Buffer, Prefix, Filename); 2238 Filename.assign(Buffer.begin(), Buffer.end()); 2239 } 2240 2241 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) { 2242 switch (ARR) { 2243 case ASTReader::Failure: return true; 2244 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing); 2245 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate); 2246 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch); 2247 case ASTReader::ConfigurationMismatch: 2248 return !(Caps & ASTReader::ARR_ConfigurationMismatch); 2249 case ASTReader::HadErrors: return true; 2250 case ASTReader::Success: return false; 2251 } 2252 2253 llvm_unreachable("unknown ASTReadResult"); 2254 } 2255 2256 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock( 2257 BitstreamCursor &Stream, unsigned ClientLoadCapabilities, 2258 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener, 2259 std::string &SuggestedPredefines) { 2260 if (Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) 2261 return Failure; 2262 2263 // Read all of the records in the options block. 2264 RecordData Record; 2265 ASTReadResult Result = Success; 2266 while (true) { 2267 llvm::BitstreamEntry Entry = Stream.advance(); 2268 2269 switch (Entry.Kind) { 2270 case llvm::BitstreamEntry::Error: 2271 case llvm::BitstreamEntry::SubBlock: 2272 return Failure; 2273 2274 case llvm::BitstreamEntry::EndBlock: 2275 return Result; 2276 2277 case llvm::BitstreamEntry::Record: 2278 // The interesting case. 2279 break; 2280 } 2281 2282 // Read and process a record. 2283 Record.clear(); 2284 switch ((OptionsRecordTypes)Stream.readRecord(Entry.ID, Record)) { 2285 case LANGUAGE_OPTIONS: { 2286 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2287 if (ParseLanguageOptions(Record, Complain, Listener, 2288 AllowCompatibleConfigurationMismatch)) 2289 Result = ConfigurationMismatch; 2290 break; 2291 } 2292 2293 case TARGET_OPTIONS: { 2294 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2295 if (ParseTargetOptions(Record, Complain, Listener, 2296 AllowCompatibleConfigurationMismatch)) 2297 Result = ConfigurationMismatch; 2298 break; 2299 } 2300 2301 case FILE_SYSTEM_OPTIONS: { 2302 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2303 if (!AllowCompatibleConfigurationMismatch && 2304 ParseFileSystemOptions(Record, Complain, Listener)) 2305 Result = ConfigurationMismatch; 2306 break; 2307 } 2308 2309 case HEADER_SEARCH_OPTIONS: { 2310 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2311 if (!AllowCompatibleConfigurationMismatch && 2312 ParseHeaderSearchOptions(Record, Complain, Listener)) 2313 Result = ConfigurationMismatch; 2314 break; 2315 } 2316 2317 case PREPROCESSOR_OPTIONS: 2318 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2319 if (!AllowCompatibleConfigurationMismatch && 2320 ParsePreprocessorOptions(Record, Complain, Listener, 2321 SuggestedPredefines)) 2322 Result = ConfigurationMismatch; 2323 break; 2324 } 2325 } 2326 } 2327 2328 ASTReader::ASTReadResult 2329 ASTReader::ReadControlBlock(ModuleFile &F, 2330 SmallVectorImpl<ImportedModule> &Loaded, 2331 const ModuleFile *ImportedBy, 2332 unsigned ClientLoadCapabilities) { 2333 BitstreamCursor &Stream = F.Stream; 2334 ASTReadResult Result = Success; 2335 2336 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) { 2337 Error("malformed block record in AST file"); 2338 return Failure; 2339 } 2340 2341 // Lambda to read the unhashed control block the first time it's called. 2342 // 2343 // For PCM files, the unhashed control block cannot be read until after the 2344 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still 2345 // need to look ahead before reading the IMPORTS record. For consistency, 2346 // this block is always read somehow (see BitstreamEntry::EndBlock). 2347 bool HasReadUnhashedControlBlock = false; 2348 auto readUnhashedControlBlockOnce = [&]() { 2349 if (!HasReadUnhashedControlBlock) { 2350 HasReadUnhashedControlBlock = true; 2351 if (ASTReadResult Result = 2352 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities)) 2353 return Result; 2354 } 2355 return Success; 2356 }; 2357 2358 // Read all of the records and blocks in the control block. 2359 RecordData Record; 2360 unsigned NumInputs = 0; 2361 unsigned NumUserInputs = 0; 2362 while (true) { 2363 llvm::BitstreamEntry Entry = Stream.advance(); 2364 2365 switch (Entry.Kind) { 2366 case llvm::BitstreamEntry::Error: 2367 Error("malformed block record in AST file"); 2368 return Failure; 2369 case llvm::BitstreamEntry::EndBlock: { 2370 // Validate the module before returning. This call catches an AST with 2371 // no module name and no imports. 2372 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2373 return Result; 2374 2375 // Validate input files. 2376 const HeaderSearchOptions &HSOpts = 2377 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 2378 2379 // All user input files reside at the index range [0, NumUserInputs), and 2380 // system input files reside at [NumUserInputs, NumInputs). For explicitly 2381 // loaded module files, ignore missing inputs. 2382 if (!DisableValidation && F.Kind != MK_ExplicitModule && 2383 F.Kind != MK_PrebuiltModule) { 2384 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 2385 2386 // If we are reading a module, we will create a verification timestamp, 2387 // so we verify all input files. Otherwise, verify only user input 2388 // files. 2389 2390 unsigned N = NumUserInputs; 2391 if (ValidateSystemInputs || 2392 (HSOpts.ModulesValidateOncePerBuildSession && 2393 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp && 2394 F.Kind == MK_ImplicitModule)) 2395 N = NumInputs; 2396 2397 for (unsigned I = 0; I < N; ++I) { 2398 InputFile IF = getInputFile(F, I+1, Complain); 2399 if (!IF.getFile() || IF.isOutOfDate()) 2400 return OutOfDate; 2401 } 2402 } 2403 2404 if (Listener) 2405 Listener->visitModuleFile(F.FileName, F.Kind); 2406 2407 if (Listener && Listener->needsInputFileVisitation()) { 2408 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs 2409 : NumUserInputs; 2410 for (unsigned I = 0; I < N; ++I) { 2411 bool IsSystem = I >= NumUserInputs; 2412 InputFileInfo FI = readInputFileInfo(F, I+1); 2413 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden, 2414 F.Kind == MK_ExplicitModule || 2415 F.Kind == MK_PrebuiltModule); 2416 } 2417 } 2418 2419 return Result; 2420 } 2421 2422 case llvm::BitstreamEntry::SubBlock: 2423 switch (Entry.ID) { 2424 case INPUT_FILES_BLOCK_ID: 2425 F.InputFilesCursor = Stream; 2426 if (Stream.SkipBlock() || // Skip with the main cursor 2427 // Read the abbreviations 2428 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) { 2429 Error("malformed block record in AST file"); 2430 return Failure; 2431 } 2432 continue; 2433 2434 case OPTIONS_BLOCK_ID: 2435 // If we're reading the first module for this group, check its options 2436 // are compatible with ours. For modules it imports, no further checking 2437 // is required, because we checked them when we built it. 2438 if (Listener && !ImportedBy) { 2439 // Should we allow the configuration of the module file to differ from 2440 // the configuration of the current translation unit in a compatible 2441 // way? 2442 // 2443 // FIXME: Allow this for files explicitly specified with -include-pch. 2444 bool AllowCompatibleConfigurationMismatch = 2445 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 2446 2447 Result = ReadOptionsBlock(Stream, ClientLoadCapabilities, 2448 AllowCompatibleConfigurationMismatch, 2449 *Listener, SuggestedPredefines); 2450 if (Result == Failure) { 2451 Error("malformed block record in AST file"); 2452 return Result; 2453 } 2454 2455 if (DisableValidation || 2456 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 2457 Result = Success; 2458 2459 // If we can't load the module, exit early since we likely 2460 // will rebuild the module anyway. The stream may be in the 2461 // middle of a block. 2462 if (Result != Success) 2463 return Result; 2464 } else if (Stream.SkipBlock()) { 2465 Error("malformed block record in AST file"); 2466 return Failure; 2467 } 2468 continue; 2469 2470 default: 2471 if (Stream.SkipBlock()) { 2472 Error("malformed block record in AST file"); 2473 return Failure; 2474 } 2475 continue; 2476 } 2477 2478 case llvm::BitstreamEntry::Record: 2479 // The interesting case. 2480 break; 2481 } 2482 2483 // Read and process a record. 2484 Record.clear(); 2485 StringRef Blob; 2486 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) { 2487 case METADATA: { 2488 if (Record[0] != VERSION_MAJOR && !DisableValidation) { 2489 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2490 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old 2491 : diag::err_pch_version_too_new); 2492 return VersionMismatch; 2493 } 2494 2495 bool hasErrors = Record[7]; 2496 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) { 2497 Diag(diag::err_pch_with_compiler_errors); 2498 return HadErrors; 2499 } 2500 if (hasErrors) { 2501 Diags.ErrorOccurred = true; 2502 Diags.UncompilableErrorOccurred = true; 2503 Diags.UnrecoverableErrorOccurred = true; 2504 } 2505 2506 F.RelocatablePCH = Record[4]; 2507 // Relative paths in a relocatable PCH are relative to our sysroot. 2508 if (F.RelocatablePCH) 2509 F.BaseDirectory = isysroot.empty() ? "/" : isysroot; 2510 2511 F.HasTimestamps = Record[5]; 2512 2513 F.PCHHasObjectFile = Record[6]; 2514 2515 const std::string &CurBranch = getClangFullRepositoryVersion(); 2516 StringRef ASTBranch = Blob; 2517 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) { 2518 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2519 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch; 2520 return VersionMismatch; 2521 } 2522 break; 2523 } 2524 2525 case IMPORTS: { 2526 // Validate the AST before processing any imports (otherwise, untangling 2527 // them can be error-prone and expensive). A module will have a name and 2528 // will already have been validated, but this catches the PCH case. 2529 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2530 return Result; 2531 2532 // Load each of the imported PCH files. 2533 unsigned Idx = 0, N = Record.size(); 2534 while (Idx < N) { 2535 // Read information about the AST file. 2536 ModuleKind ImportedKind = (ModuleKind)Record[Idx++]; 2537 // The import location will be the local one for now; we will adjust 2538 // all import locations of module imports after the global source 2539 // location info are setup, in ReadAST. 2540 SourceLocation ImportLoc = 2541 ReadUntranslatedSourceLocation(Record[Idx++]); 2542 off_t StoredSize = (off_t)Record[Idx++]; 2543 time_t StoredModTime = (time_t)Record[Idx++]; 2544 ASTFileSignature StoredSignature = { 2545 {{(uint32_t)Record[Idx++], (uint32_t)Record[Idx++], 2546 (uint32_t)Record[Idx++], (uint32_t)Record[Idx++], 2547 (uint32_t)Record[Idx++]}}}; 2548 2549 std::string ImportedName = ReadString(Record, Idx); 2550 std::string ImportedFile; 2551 2552 // For prebuilt and explicit modules first consult the file map for 2553 // an override. Note that here we don't search prebuilt module 2554 // directories, only the explicit name to file mappings. Also, we will 2555 // still verify the size/signature making sure it is essentially the 2556 // same file but perhaps in a different location. 2557 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule) 2558 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName( 2559 ImportedName, /*FileMapOnly*/ true); 2560 2561 if (ImportedFile.empty()) 2562 ImportedFile = ReadPath(F, Record, Idx); 2563 else 2564 SkipPath(Record, Idx); 2565 2566 // If our client can't cope with us being out of date, we can't cope with 2567 // our dependency being missing. 2568 unsigned Capabilities = ClientLoadCapabilities; 2569 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 2570 Capabilities &= ~ARR_Missing; 2571 2572 // Load the AST file. 2573 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, 2574 Loaded, StoredSize, StoredModTime, 2575 StoredSignature, Capabilities); 2576 2577 // If we diagnosed a problem, produce a backtrace. 2578 if (isDiagnosedResult(Result, Capabilities)) 2579 Diag(diag::note_module_file_imported_by) 2580 << F.FileName << !F.ModuleName.empty() << F.ModuleName; 2581 2582 switch (Result) { 2583 case Failure: return Failure; 2584 // If we have to ignore the dependency, we'll have to ignore this too. 2585 case Missing: 2586 case OutOfDate: return OutOfDate; 2587 case VersionMismatch: return VersionMismatch; 2588 case ConfigurationMismatch: return ConfigurationMismatch; 2589 case HadErrors: return HadErrors; 2590 case Success: break; 2591 } 2592 } 2593 break; 2594 } 2595 2596 case ORIGINAL_FILE: 2597 F.OriginalSourceFileID = FileID::get(Record[0]); 2598 F.ActualOriginalSourceFileName = Blob; 2599 F.OriginalSourceFileName = F.ActualOriginalSourceFileName; 2600 ResolveImportedPath(F, F.OriginalSourceFileName); 2601 break; 2602 2603 case ORIGINAL_FILE_ID: 2604 F.OriginalSourceFileID = FileID::get(Record[0]); 2605 break; 2606 2607 case ORIGINAL_PCH_DIR: 2608 F.OriginalDir = Blob; 2609 break; 2610 2611 case MODULE_NAME: 2612 F.ModuleName = Blob; 2613 if (Listener) 2614 Listener->ReadModuleName(F.ModuleName); 2615 2616 // Validate the AST as soon as we have a name so we can exit early on 2617 // failure. 2618 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2619 return Result; 2620 2621 break; 2622 2623 case MODULE_DIRECTORY: { 2624 assert(!F.ModuleName.empty() && 2625 "MODULE_DIRECTORY found before MODULE_NAME"); 2626 // If we've already loaded a module map file covering this module, we may 2627 // have a better path for it (relative to the current build). 2628 Module *M = PP.getHeaderSearchInfo().lookupModule( 2629 F.ModuleName, /*AllowSearch*/ true, 2630 /*AllowExtraModuleMapSearch*/ true); 2631 if (M && M->Directory) { 2632 // If we're implicitly loading a module, the base directory can't 2633 // change between the build and use. 2634 // Don't emit module relocation error if we have -fno-validate-pch 2635 if (!PP.getPreprocessorOpts().DisablePCHValidation && 2636 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) { 2637 const DirectoryEntry *BuildDir = 2638 PP.getFileManager().getDirectory(Blob); 2639 if (!BuildDir || BuildDir != M->Directory) { 2640 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 2641 Diag(diag::err_imported_module_relocated) 2642 << F.ModuleName << Blob << M->Directory->getName(); 2643 return OutOfDate; 2644 } 2645 } 2646 F.BaseDirectory = M->Directory->getName(); 2647 } else { 2648 F.BaseDirectory = Blob; 2649 } 2650 break; 2651 } 2652 2653 case MODULE_MAP_FILE: 2654 if (ASTReadResult Result = 2655 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities)) 2656 return Result; 2657 break; 2658 2659 case INPUT_FILE_OFFSETS: 2660 NumInputs = Record[0]; 2661 NumUserInputs = Record[1]; 2662 F.InputFileOffsets = 2663 (const llvm::support::unaligned_uint64_t *)Blob.data(); 2664 F.InputFilesLoaded.resize(NumInputs); 2665 F.NumUserInputFiles = NumUserInputs; 2666 break; 2667 } 2668 } 2669 } 2670 2671 ASTReader::ASTReadResult 2672 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) { 2673 BitstreamCursor &Stream = F.Stream; 2674 2675 if (Stream.EnterSubBlock(AST_BLOCK_ID)) { 2676 Error("malformed block record in AST file"); 2677 return Failure; 2678 } 2679 2680 // Read all of the records and blocks for the AST file. 2681 RecordData Record; 2682 while (true) { 2683 llvm::BitstreamEntry Entry = Stream.advance(); 2684 2685 switch (Entry.Kind) { 2686 case llvm::BitstreamEntry::Error: 2687 Error("error at end of module block in AST file"); 2688 return Failure; 2689 case llvm::BitstreamEntry::EndBlock: 2690 // Outside of C++, we do not store a lookup map for the translation unit. 2691 // Instead, mark it as needing a lookup map to be built if this module 2692 // contains any declarations lexically within it (which it always does!). 2693 // This usually has no cost, since we very rarely need the lookup map for 2694 // the translation unit outside C++. 2695 if (ASTContext *Ctx = ContextObj) { 2696 DeclContext *DC = Ctx->getTranslationUnitDecl(); 2697 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus) 2698 DC->setMustBuildLookupTable(); 2699 } 2700 2701 return Success; 2702 case llvm::BitstreamEntry::SubBlock: 2703 switch (Entry.ID) { 2704 case DECLTYPES_BLOCK_ID: 2705 // We lazily load the decls block, but we want to set up the 2706 // DeclsCursor cursor to point into it. Clone our current bitcode 2707 // cursor to it, enter the block and read the abbrevs in that block. 2708 // With the main cursor, we just skip over it. 2709 F.DeclsCursor = Stream; 2710 if (Stream.SkipBlock() || // Skip with the main cursor. 2711 // Read the abbrevs. 2712 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) { 2713 Error("malformed block record in AST file"); 2714 return Failure; 2715 } 2716 break; 2717 2718 case PREPROCESSOR_BLOCK_ID: 2719 F.MacroCursor = Stream; 2720 if (!PP.getExternalSource()) 2721 PP.setExternalSource(this); 2722 2723 if (Stream.SkipBlock() || 2724 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) { 2725 Error("malformed block record in AST file"); 2726 return Failure; 2727 } 2728 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo(); 2729 break; 2730 2731 case PREPROCESSOR_DETAIL_BLOCK_ID: 2732 F.PreprocessorDetailCursor = Stream; 2733 if (Stream.SkipBlock() || 2734 ReadBlockAbbrevs(F.PreprocessorDetailCursor, 2735 PREPROCESSOR_DETAIL_BLOCK_ID)) { 2736 Error("malformed preprocessor detail record in AST file"); 2737 return Failure; 2738 } 2739 F.PreprocessorDetailStartOffset 2740 = F.PreprocessorDetailCursor.GetCurrentBitNo(); 2741 2742 if (!PP.getPreprocessingRecord()) 2743 PP.createPreprocessingRecord(); 2744 if (!PP.getPreprocessingRecord()->getExternalSource()) 2745 PP.getPreprocessingRecord()->SetExternalSource(*this); 2746 break; 2747 2748 case SOURCE_MANAGER_BLOCK_ID: 2749 if (ReadSourceManagerBlock(F)) 2750 return Failure; 2751 break; 2752 2753 case SUBMODULE_BLOCK_ID: 2754 if (ASTReadResult Result = 2755 ReadSubmoduleBlock(F, ClientLoadCapabilities)) 2756 return Result; 2757 break; 2758 2759 case COMMENTS_BLOCK_ID: { 2760 BitstreamCursor C = Stream; 2761 if (Stream.SkipBlock() || 2762 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) { 2763 Error("malformed comments block in AST file"); 2764 return Failure; 2765 } 2766 CommentsCursors.push_back(std::make_pair(C, &F)); 2767 break; 2768 } 2769 2770 default: 2771 if (Stream.SkipBlock()) { 2772 Error("malformed block record in AST file"); 2773 return Failure; 2774 } 2775 break; 2776 } 2777 continue; 2778 2779 case llvm::BitstreamEntry::Record: 2780 // The interesting case. 2781 break; 2782 } 2783 2784 // Read and process a record. 2785 Record.clear(); 2786 StringRef Blob; 2787 auto RecordType = 2788 (ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob); 2789 2790 // If we're not loading an AST context, we don't care about most records. 2791 if (!ContextObj) { 2792 switch (RecordType) { 2793 case IDENTIFIER_TABLE: 2794 case IDENTIFIER_OFFSET: 2795 case INTERESTING_IDENTIFIERS: 2796 case STATISTICS: 2797 case PP_CONDITIONAL_STACK: 2798 case PP_COUNTER_VALUE: 2799 case SOURCE_LOCATION_OFFSETS: 2800 case MODULE_OFFSET_MAP: 2801 case SOURCE_MANAGER_LINE_TABLE: 2802 case SOURCE_LOCATION_PRELOADS: 2803 case PPD_ENTITIES_OFFSETS: 2804 case HEADER_SEARCH_TABLE: 2805 case IMPORTED_MODULES: 2806 case MACRO_OFFSET: 2807 break; 2808 default: 2809 continue; 2810 } 2811 } 2812 2813 switch (RecordType) { 2814 default: // Default behavior: ignore. 2815 break; 2816 2817 case TYPE_OFFSET: { 2818 if (F.LocalNumTypes != 0) { 2819 Error("duplicate TYPE_OFFSET record in AST file"); 2820 return Failure; 2821 } 2822 F.TypeOffsets = (const uint32_t *)Blob.data(); 2823 F.LocalNumTypes = Record[0]; 2824 unsigned LocalBaseTypeIndex = Record[1]; 2825 F.BaseTypeIndex = getTotalNumTypes(); 2826 2827 if (F.LocalNumTypes > 0) { 2828 // Introduce the global -> local mapping for types within this module. 2829 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F)); 2830 2831 // Introduce the local -> global mapping for types within this module. 2832 F.TypeRemap.insertOrReplace( 2833 std::make_pair(LocalBaseTypeIndex, 2834 F.BaseTypeIndex - LocalBaseTypeIndex)); 2835 2836 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes); 2837 } 2838 break; 2839 } 2840 2841 case DECL_OFFSET: { 2842 if (F.LocalNumDecls != 0) { 2843 Error("duplicate DECL_OFFSET record in AST file"); 2844 return Failure; 2845 } 2846 F.DeclOffsets = (const DeclOffset *)Blob.data(); 2847 F.LocalNumDecls = Record[0]; 2848 unsigned LocalBaseDeclID = Record[1]; 2849 F.BaseDeclID = getTotalNumDecls(); 2850 2851 if (F.LocalNumDecls > 0) { 2852 // Introduce the global -> local mapping for declarations within this 2853 // module. 2854 GlobalDeclMap.insert( 2855 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F)); 2856 2857 // Introduce the local -> global mapping for declarations within this 2858 // module. 2859 F.DeclRemap.insertOrReplace( 2860 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID)); 2861 2862 // Introduce the global -> local mapping for declarations within this 2863 // module. 2864 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID; 2865 2866 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls); 2867 } 2868 break; 2869 } 2870 2871 case TU_UPDATE_LEXICAL: { 2872 DeclContext *TU = ContextObj->getTranslationUnitDecl(); 2873 LexicalContents Contents( 2874 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 2875 Blob.data()), 2876 static_cast<unsigned int>(Blob.size() / 4)); 2877 TULexicalDecls.push_back(std::make_pair(&F, Contents)); 2878 TU->setHasExternalLexicalStorage(true); 2879 break; 2880 } 2881 2882 case UPDATE_VISIBLE: { 2883 unsigned Idx = 0; 2884 serialization::DeclID ID = ReadDeclID(F, Record, Idx); 2885 auto *Data = (const unsigned char*)Blob.data(); 2886 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data}); 2887 // If we've already loaded the decl, perform the updates when we finish 2888 // loading this block. 2889 if (Decl *D = GetExistingDecl(ID)) 2890 PendingUpdateRecords.push_back( 2891 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 2892 break; 2893 } 2894 2895 case IDENTIFIER_TABLE: 2896 F.IdentifierTableData = Blob.data(); 2897 if (Record[0]) { 2898 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create( 2899 (const unsigned char *)F.IdentifierTableData + Record[0], 2900 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t), 2901 (const unsigned char *)F.IdentifierTableData, 2902 ASTIdentifierLookupTrait(*this, F)); 2903 2904 PP.getIdentifierTable().setExternalIdentifierLookup(this); 2905 } 2906 break; 2907 2908 case IDENTIFIER_OFFSET: { 2909 if (F.LocalNumIdentifiers != 0) { 2910 Error("duplicate IDENTIFIER_OFFSET record in AST file"); 2911 return Failure; 2912 } 2913 F.IdentifierOffsets = (const uint32_t *)Blob.data(); 2914 F.LocalNumIdentifiers = Record[0]; 2915 unsigned LocalBaseIdentifierID = Record[1]; 2916 F.BaseIdentifierID = getTotalNumIdentifiers(); 2917 2918 if (F.LocalNumIdentifiers > 0) { 2919 // Introduce the global -> local mapping for identifiers within this 2920 // module. 2921 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1, 2922 &F)); 2923 2924 // Introduce the local -> global mapping for identifiers within this 2925 // module. 2926 F.IdentifierRemap.insertOrReplace( 2927 std::make_pair(LocalBaseIdentifierID, 2928 F.BaseIdentifierID - LocalBaseIdentifierID)); 2929 2930 IdentifiersLoaded.resize(IdentifiersLoaded.size() 2931 + F.LocalNumIdentifiers); 2932 } 2933 break; 2934 } 2935 2936 case INTERESTING_IDENTIFIERS: 2937 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end()); 2938 break; 2939 2940 case EAGERLY_DESERIALIZED_DECLS: 2941 // FIXME: Skip reading this record if our ASTConsumer doesn't care 2942 // about "interesting" decls (for instance, if we're building a module). 2943 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2944 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 2945 break; 2946 2947 case MODULAR_CODEGEN_DECLS: 2948 // FIXME: Skip reading this record if our ASTConsumer doesn't care about 2949 // them (ie: if we're not codegenerating this module). 2950 if (F.Kind == MK_MainFile) 2951 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2952 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 2953 break; 2954 2955 case SPECIAL_TYPES: 2956 if (SpecialTypes.empty()) { 2957 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2958 SpecialTypes.push_back(getGlobalTypeID(F, Record[I])); 2959 break; 2960 } 2961 2962 if (SpecialTypes.size() != Record.size()) { 2963 Error("invalid special-types record"); 2964 return Failure; 2965 } 2966 2967 for (unsigned I = 0, N = Record.size(); I != N; ++I) { 2968 serialization::TypeID ID = getGlobalTypeID(F, Record[I]); 2969 if (!SpecialTypes[I]) 2970 SpecialTypes[I] = ID; 2971 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate 2972 // merge step? 2973 } 2974 break; 2975 2976 case STATISTICS: 2977 TotalNumStatements += Record[0]; 2978 TotalNumMacros += Record[1]; 2979 TotalLexicalDeclContexts += Record[2]; 2980 TotalVisibleDeclContexts += Record[3]; 2981 break; 2982 2983 case UNUSED_FILESCOPED_DECLS: 2984 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2985 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I])); 2986 break; 2987 2988 case DELEGATING_CTORS: 2989 for (unsigned I = 0, N = Record.size(); I != N; ++I) 2990 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I])); 2991 break; 2992 2993 case WEAK_UNDECLARED_IDENTIFIERS: 2994 if (Record.size() % 4 != 0) { 2995 Error("invalid weak identifiers record"); 2996 return Failure; 2997 } 2998 2999 // FIXME: Ignore weak undeclared identifiers from non-original PCH 3000 // files. This isn't the way to do it :) 3001 WeakUndeclaredIdentifiers.clear(); 3002 3003 // Translate the weak, undeclared identifiers into global IDs. 3004 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) { 3005 WeakUndeclaredIdentifiers.push_back( 3006 getGlobalIdentifierID(F, Record[I++])); 3007 WeakUndeclaredIdentifiers.push_back( 3008 getGlobalIdentifierID(F, Record[I++])); 3009 WeakUndeclaredIdentifiers.push_back( 3010 ReadSourceLocation(F, Record, I).getRawEncoding()); 3011 WeakUndeclaredIdentifiers.push_back(Record[I++]); 3012 } 3013 break; 3014 3015 case SELECTOR_OFFSETS: { 3016 F.SelectorOffsets = (const uint32_t *)Blob.data(); 3017 F.LocalNumSelectors = Record[0]; 3018 unsigned LocalBaseSelectorID = Record[1]; 3019 F.BaseSelectorID = getTotalNumSelectors(); 3020 3021 if (F.LocalNumSelectors > 0) { 3022 // Introduce the global -> local mapping for selectors within this 3023 // module. 3024 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F)); 3025 3026 // Introduce the local -> global mapping for selectors within this 3027 // module. 3028 F.SelectorRemap.insertOrReplace( 3029 std::make_pair(LocalBaseSelectorID, 3030 F.BaseSelectorID - LocalBaseSelectorID)); 3031 3032 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors); 3033 } 3034 break; 3035 } 3036 3037 case METHOD_POOL: 3038 F.SelectorLookupTableData = (const unsigned char *)Blob.data(); 3039 if (Record[0]) 3040 F.SelectorLookupTable 3041 = ASTSelectorLookupTable::Create( 3042 F.SelectorLookupTableData + Record[0], 3043 F.SelectorLookupTableData, 3044 ASTSelectorLookupTrait(*this, F)); 3045 TotalNumMethodPoolEntries += Record[1]; 3046 break; 3047 3048 case REFERENCED_SELECTOR_POOL: 3049 if (!Record.empty()) { 3050 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) { 3051 ReferencedSelectorsData.push_back(getGlobalSelectorID(F, 3052 Record[Idx++])); 3053 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx). 3054 getRawEncoding()); 3055 } 3056 } 3057 break; 3058 3059 case PP_CONDITIONAL_STACK: 3060 if (!Record.empty()) { 3061 unsigned Idx = 0, End = Record.size() - 1; 3062 bool ReachedEOFWhileSkipping = Record[Idx++]; 3063 llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo; 3064 if (ReachedEOFWhileSkipping) { 3065 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx); 3066 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx); 3067 bool FoundNonSkipPortion = Record[Idx++]; 3068 bool FoundElse = Record[Idx++]; 3069 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx); 3070 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion, 3071 FoundElse, ElseLoc); 3072 } 3073 SmallVector<PPConditionalInfo, 4> ConditionalStack; 3074 while (Idx < End) { 3075 auto Loc = ReadSourceLocation(F, Record, Idx); 3076 bool WasSkipping = Record[Idx++]; 3077 bool FoundNonSkip = Record[Idx++]; 3078 bool FoundElse = Record[Idx++]; 3079 ConditionalStack.push_back( 3080 {Loc, WasSkipping, FoundNonSkip, FoundElse}); 3081 } 3082 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo); 3083 } 3084 break; 3085 3086 case PP_COUNTER_VALUE: 3087 if (!Record.empty() && Listener) 3088 Listener->ReadCounter(F, Record[0]); 3089 break; 3090 3091 case FILE_SORTED_DECLS: 3092 F.FileSortedDecls = (const DeclID *)Blob.data(); 3093 F.NumFileSortedDecls = Record[0]; 3094 break; 3095 3096 case SOURCE_LOCATION_OFFSETS: { 3097 F.SLocEntryOffsets = (const uint32_t *)Blob.data(); 3098 F.LocalNumSLocEntries = Record[0]; 3099 unsigned SLocSpaceSize = Record[1]; 3100 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) = 3101 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, 3102 SLocSpaceSize); 3103 if (!F.SLocEntryBaseID) { 3104 Error("ran out of source locations"); 3105 break; 3106 } 3107 // Make our entry in the range map. BaseID is negative and growing, so 3108 // we invert it. Because we invert it, though, we need the other end of 3109 // the range. 3110 unsigned RangeStart = 3111 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1; 3112 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F)); 3113 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset); 3114 3115 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing. 3116 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0); 3117 GlobalSLocOffsetMap.insert( 3118 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset 3119 - SLocSpaceSize,&F)); 3120 3121 // Initialize the remapping table. 3122 // Invalid stays invalid. 3123 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0)); 3124 // This module. Base was 2 when being compiled. 3125 F.SLocRemap.insertOrReplace(std::make_pair(2U, 3126 static_cast<int>(F.SLocEntryBaseOffset - 2))); 3127 3128 TotalNumSLocEntries += F.LocalNumSLocEntries; 3129 break; 3130 } 3131 3132 case MODULE_OFFSET_MAP: 3133 F.ModuleOffsetMap = Blob; 3134 break; 3135 3136 case SOURCE_MANAGER_LINE_TABLE: 3137 if (ParseLineTable(F, Record)) 3138 return Failure; 3139 break; 3140 3141 case SOURCE_LOCATION_PRELOADS: { 3142 // Need to transform from the local view (1-based IDs) to the global view, 3143 // which is based off F.SLocEntryBaseID. 3144 if (!F.PreloadSLocEntries.empty()) { 3145 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file"); 3146 return Failure; 3147 } 3148 3149 F.PreloadSLocEntries.swap(Record); 3150 break; 3151 } 3152 3153 case EXT_VECTOR_DECLS: 3154 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3155 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I])); 3156 break; 3157 3158 case VTABLE_USES: 3159 if (Record.size() % 3 != 0) { 3160 Error("Invalid VTABLE_USES record"); 3161 return Failure; 3162 } 3163 3164 // Later tables overwrite earlier ones. 3165 // FIXME: Modules will have some trouble with this. This is clearly not 3166 // the right way to do this. 3167 VTableUses.clear(); 3168 3169 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) { 3170 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++])); 3171 VTableUses.push_back( 3172 ReadSourceLocation(F, Record, Idx).getRawEncoding()); 3173 VTableUses.push_back(Record[Idx++]); 3174 } 3175 break; 3176 3177 case PENDING_IMPLICIT_INSTANTIATIONS: 3178 if (PendingInstantiations.size() % 2 != 0) { 3179 Error("Invalid existing PendingInstantiations"); 3180 return Failure; 3181 } 3182 3183 if (Record.size() % 2 != 0) { 3184 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block"); 3185 return Failure; 3186 } 3187 3188 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3189 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++])); 3190 PendingInstantiations.push_back( 3191 ReadSourceLocation(F, Record, I).getRawEncoding()); 3192 } 3193 break; 3194 3195 case SEMA_DECL_REFS: 3196 if (Record.size() != 3) { 3197 Error("Invalid SEMA_DECL_REFS block"); 3198 return Failure; 3199 } 3200 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3201 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3202 break; 3203 3204 case PPD_ENTITIES_OFFSETS: { 3205 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data(); 3206 assert(Blob.size() % sizeof(PPEntityOffset) == 0); 3207 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset); 3208 3209 unsigned LocalBasePreprocessedEntityID = Record[0]; 3210 3211 unsigned StartingID; 3212 if (!PP.getPreprocessingRecord()) 3213 PP.createPreprocessingRecord(); 3214 if (!PP.getPreprocessingRecord()->getExternalSource()) 3215 PP.getPreprocessingRecord()->SetExternalSource(*this); 3216 StartingID 3217 = PP.getPreprocessingRecord() 3218 ->allocateLoadedEntities(F.NumPreprocessedEntities); 3219 F.BasePreprocessedEntityID = StartingID; 3220 3221 if (F.NumPreprocessedEntities > 0) { 3222 // Introduce the global -> local mapping for preprocessed entities in 3223 // this module. 3224 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F)); 3225 3226 // Introduce the local -> global mapping for preprocessed entities in 3227 // this module. 3228 F.PreprocessedEntityRemap.insertOrReplace( 3229 std::make_pair(LocalBasePreprocessedEntityID, 3230 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID)); 3231 } 3232 3233 break; 3234 } 3235 3236 case PPD_SKIPPED_RANGES: { 3237 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data(); 3238 assert(Blob.size() % sizeof(PPSkippedRange) == 0); 3239 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange); 3240 3241 if (!PP.getPreprocessingRecord()) 3242 PP.createPreprocessingRecord(); 3243 if (!PP.getPreprocessingRecord()->getExternalSource()) 3244 PP.getPreprocessingRecord()->SetExternalSource(*this); 3245 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord() 3246 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges); 3247 3248 if (F.NumPreprocessedSkippedRanges > 0) 3249 GlobalSkippedRangeMap.insert( 3250 std::make_pair(F.BasePreprocessedSkippedRangeID, &F)); 3251 break; 3252 } 3253 3254 case DECL_UPDATE_OFFSETS: 3255 if (Record.size() % 2 != 0) { 3256 Error("invalid DECL_UPDATE_OFFSETS block in AST file"); 3257 return Failure; 3258 } 3259 for (unsigned I = 0, N = Record.size(); I != N; I += 2) { 3260 GlobalDeclID ID = getGlobalDeclID(F, Record[I]); 3261 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1])); 3262 3263 // If we've already loaded the decl, perform the updates when we finish 3264 // loading this block. 3265 if (Decl *D = GetExistingDecl(ID)) 3266 PendingUpdateRecords.push_back( 3267 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3268 } 3269 break; 3270 3271 case OBJC_CATEGORIES_MAP: 3272 if (F.LocalNumObjCCategoriesInMap != 0) { 3273 Error("duplicate OBJC_CATEGORIES_MAP record in AST file"); 3274 return Failure; 3275 } 3276 3277 F.LocalNumObjCCategoriesInMap = Record[0]; 3278 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data(); 3279 break; 3280 3281 case OBJC_CATEGORIES: 3282 F.ObjCCategories.swap(Record); 3283 break; 3284 3285 case CUDA_SPECIAL_DECL_REFS: 3286 // Later tables overwrite earlier ones. 3287 // FIXME: Modules will have trouble with this. 3288 CUDASpecialDeclRefs.clear(); 3289 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3290 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3291 break; 3292 3293 case HEADER_SEARCH_TABLE: 3294 F.HeaderFileInfoTableData = Blob.data(); 3295 F.LocalNumHeaderFileInfos = Record[1]; 3296 if (Record[0]) { 3297 F.HeaderFileInfoTable 3298 = HeaderFileInfoLookupTable::Create( 3299 (const unsigned char *)F.HeaderFileInfoTableData + Record[0], 3300 (const unsigned char *)F.HeaderFileInfoTableData, 3301 HeaderFileInfoTrait(*this, F, 3302 &PP.getHeaderSearchInfo(), 3303 Blob.data() + Record[2])); 3304 3305 PP.getHeaderSearchInfo().SetExternalSource(this); 3306 if (!PP.getHeaderSearchInfo().getExternalLookup()) 3307 PP.getHeaderSearchInfo().SetExternalLookup(this); 3308 } 3309 break; 3310 3311 case FP_PRAGMA_OPTIONS: 3312 // Later tables overwrite earlier ones. 3313 FPPragmaOptions.swap(Record); 3314 break; 3315 3316 case OPENCL_EXTENSIONS: 3317 for (unsigned I = 0, E = Record.size(); I != E; ) { 3318 auto Name = ReadString(Record, I); 3319 auto &Opt = OpenCLExtensions.OptMap[Name]; 3320 Opt.Supported = Record[I++] != 0; 3321 Opt.Enabled = Record[I++] != 0; 3322 Opt.Avail = Record[I++]; 3323 Opt.Core = Record[I++]; 3324 } 3325 break; 3326 3327 case OPENCL_EXTENSION_TYPES: 3328 for (unsigned I = 0, E = Record.size(); I != E;) { 3329 auto TypeID = static_cast<::TypeID>(Record[I++]); 3330 auto *Type = GetType(TypeID).getTypePtr(); 3331 auto NumExt = static_cast<unsigned>(Record[I++]); 3332 for (unsigned II = 0; II != NumExt; ++II) { 3333 auto Ext = ReadString(Record, I); 3334 OpenCLTypeExtMap[Type].insert(Ext); 3335 } 3336 } 3337 break; 3338 3339 case OPENCL_EXTENSION_DECLS: 3340 for (unsigned I = 0, E = Record.size(); I != E;) { 3341 auto DeclID = static_cast<::DeclID>(Record[I++]); 3342 auto *Decl = GetDecl(DeclID); 3343 auto NumExt = static_cast<unsigned>(Record[I++]); 3344 for (unsigned II = 0; II != NumExt; ++II) { 3345 auto Ext = ReadString(Record, I); 3346 OpenCLDeclExtMap[Decl].insert(Ext); 3347 } 3348 } 3349 break; 3350 3351 case TENTATIVE_DEFINITIONS: 3352 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3353 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I])); 3354 break; 3355 3356 case KNOWN_NAMESPACES: 3357 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3358 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I])); 3359 break; 3360 3361 case UNDEFINED_BUT_USED: 3362 if (UndefinedButUsed.size() % 2 != 0) { 3363 Error("Invalid existing UndefinedButUsed"); 3364 return Failure; 3365 } 3366 3367 if (Record.size() % 2 != 0) { 3368 Error("invalid undefined-but-used record"); 3369 return Failure; 3370 } 3371 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3372 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++])); 3373 UndefinedButUsed.push_back( 3374 ReadSourceLocation(F, Record, I).getRawEncoding()); 3375 } 3376 break; 3377 3378 case DELETE_EXPRS_TO_ANALYZE: 3379 for (unsigned I = 0, N = Record.size(); I != N;) { 3380 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++])); 3381 const uint64_t Count = Record[I++]; 3382 DelayedDeleteExprs.push_back(Count); 3383 for (uint64_t C = 0; C < Count; ++C) { 3384 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding()); 3385 bool IsArrayForm = Record[I++] == 1; 3386 DelayedDeleteExprs.push_back(IsArrayForm); 3387 } 3388 } 3389 break; 3390 3391 case IMPORTED_MODULES: 3392 if (!F.isModule()) { 3393 // If we aren't loading a module (which has its own exports), make 3394 // all of the imported modules visible. 3395 // FIXME: Deal with macros-only imports. 3396 for (unsigned I = 0, N = Record.size(); I != N; /**/) { 3397 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]); 3398 SourceLocation Loc = ReadSourceLocation(F, Record, I); 3399 if (GlobalID) { 3400 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc)); 3401 if (DeserializationListener) 3402 DeserializationListener->ModuleImportRead(GlobalID, Loc); 3403 } 3404 } 3405 } 3406 break; 3407 3408 case MACRO_OFFSET: { 3409 if (F.LocalNumMacros != 0) { 3410 Error("duplicate MACRO_OFFSET record in AST file"); 3411 return Failure; 3412 } 3413 F.MacroOffsets = (const uint32_t *)Blob.data(); 3414 F.LocalNumMacros = Record[0]; 3415 unsigned LocalBaseMacroID = Record[1]; 3416 F.BaseMacroID = getTotalNumMacros(); 3417 3418 if (F.LocalNumMacros > 0) { 3419 // Introduce the global -> local mapping for macros within this module. 3420 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F)); 3421 3422 // Introduce the local -> global mapping for macros within this module. 3423 F.MacroRemap.insertOrReplace( 3424 std::make_pair(LocalBaseMacroID, 3425 F.BaseMacroID - LocalBaseMacroID)); 3426 3427 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros); 3428 } 3429 break; 3430 } 3431 3432 case LATE_PARSED_TEMPLATE: 3433 LateParsedTemplates.append(Record.begin(), Record.end()); 3434 break; 3435 3436 case OPTIMIZE_PRAGMA_OPTIONS: 3437 if (Record.size() != 1) { 3438 Error("invalid pragma optimize record"); 3439 return Failure; 3440 } 3441 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]); 3442 break; 3443 3444 case MSSTRUCT_PRAGMA_OPTIONS: 3445 if (Record.size() != 1) { 3446 Error("invalid pragma ms_struct record"); 3447 return Failure; 3448 } 3449 PragmaMSStructState = Record[0]; 3450 break; 3451 3452 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS: 3453 if (Record.size() != 2) { 3454 Error("invalid pragma ms_struct record"); 3455 return Failure; 3456 } 3457 PragmaMSPointersToMembersState = Record[0]; 3458 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]); 3459 break; 3460 3461 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES: 3462 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3463 UnusedLocalTypedefNameCandidates.push_back( 3464 getGlobalDeclID(F, Record[I])); 3465 break; 3466 3467 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH: 3468 if (Record.size() != 1) { 3469 Error("invalid cuda pragma options record"); 3470 return Failure; 3471 } 3472 ForceCUDAHostDeviceDepth = Record[0]; 3473 break; 3474 3475 case PACK_PRAGMA_OPTIONS: { 3476 if (Record.size() < 3) { 3477 Error("invalid pragma pack record"); 3478 return Failure; 3479 } 3480 PragmaPackCurrentValue = Record[0]; 3481 PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]); 3482 unsigned NumStackEntries = Record[2]; 3483 unsigned Idx = 3; 3484 // Reset the stack when importing a new module. 3485 PragmaPackStack.clear(); 3486 for (unsigned I = 0; I < NumStackEntries; ++I) { 3487 PragmaPackStackEntry Entry; 3488 Entry.Value = Record[Idx++]; 3489 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3490 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3491 PragmaPackStrings.push_back(ReadString(Record, Idx)); 3492 Entry.SlotLabel = PragmaPackStrings.back(); 3493 PragmaPackStack.push_back(Entry); 3494 } 3495 break; 3496 } 3497 } 3498 } 3499 } 3500 3501 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const { 3502 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read"); 3503 3504 // Additional remapping information. 3505 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data(); 3506 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size(); 3507 F.ModuleOffsetMap = StringRef(); 3508 3509 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders. 3510 if (F.SLocRemap.find(0) == F.SLocRemap.end()) { 3511 F.SLocRemap.insert(std::make_pair(0U, 0)); 3512 F.SLocRemap.insert(std::make_pair(2U, 1)); 3513 } 3514 3515 // Continuous range maps we may be updating in our module. 3516 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder; 3517 RemapBuilder SLocRemap(F.SLocRemap); 3518 RemapBuilder IdentifierRemap(F.IdentifierRemap); 3519 RemapBuilder MacroRemap(F.MacroRemap); 3520 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap); 3521 RemapBuilder SubmoduleRemap(F.SubmoduleRemap); 3522 RemapBuilder SelectorRemap(F.SelectorRemap); 3523 RemapBuilder DeclRemap(F.DeclRemap); 3524 RemapBuilder TypeRemap(F.TypeRemap); 3525 3526 while (Data < DataEnd) { 3527 // FIXME: Looking up dependency modules by filename is horrible. Let's 3528 // start fixing this with prebuilt and explicit modules and see how it 3529 // goes... 3530 using namespace llvm::support; 3531 ModuleKind Kind = static_cast<ModuleKind>( 3532 endian::readNext<uint8_t, little, unaligned>(Data)); 3533 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data); 3534 StringRef Name = StringRef((const char*)Data, Len); 3535 Data += Len; 3536 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule 3537 ? ModuleMgr.lookupByModuleName(Name) 3538 : ModuleMgr.lookupByFileName(Name)); 3539 if (!OM) { 3540 std::string Msg = 3541 "SourceLocation remap refers to unknown module, cannot find "; 3542 Msg.append(Name); 3543 Error(Msg); 3544 return; 3545 } 3546 3547 uint32_t SLocOffset = 3548 endian::readNext<uint32_t, little, unaligned>(Data); 3549 uint32_t IdentifierIDOffset = 3550 endian::readNext<uint32_t, little, unaligned>(Data); 3551 uint32_t MacroIDOffset = 3552 endian::readNext<uint32_t, little, unaligned>(Data); 3553 uint32_t PreprocessedEntityIDOffset = 3554 endian::readNext<uint32_t, little, unaligned>(Data); 3555 uint32_t SubmoduleIDOffset = 3556 endian::readNext<uint32_t, little, unaligned>(Data); 3557 uint32_t SelectorIDOffset = 3558 endian::readNext<uint32_t, little, unaligned>(Data); 3559 uint32_t DeclIDOffset = 3560 endian::readNext<uint32_t, little, unaligned>(Data); 3561 uint32_t TypeIndexOffset = 3562 endian::readNext<uint32_t, little, unaligned>(Data); 3563 3564 uint32_t None = std::numeric_limits<uint32_t>::max(); 3565 3566 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset, 3567 RemapBuilder &Remap) { 3568 if (Offset != None) 3569 Remap.insert(std::make_pair(Offset, 3570 static_cast<int>(BaseOffset - Offset))); 3571 }; 3572 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap); 3573 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap); 3574 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap); 3575 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID, 3576 PreprocessedEntityRemap); 3577 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap); 3578 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap); 3579 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap); 3580 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap); 3581 3582 // Global -> local mappings. 3583 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset; 3584 } 3585 } 3586 3587 ASTReader::ASTReadResult 3588 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F, 3589 const ModuleFile *ImportedBy, 3590 unsigned ClientLoadCapabilities) { 3591 unsigned Idx = 0; 3592 F.ModuleMapPath = ReadPath(F, Record, Idx); 3593 3594 // Try to resolve ModuleName in the current header search context and 3595 // verify that it is found in the same module map file as we saved. If the 3596 // top-level AST file is a main file, skip this check because there is no 3597 // usable header search context. 3598 assert(!F.ModuleName.empty() && 3599 "MODULE_NAME should come before MODULE_MAP_FILE"); 3600 if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) { 3601 // An implicitly-loaded module file should have its module listed in some 3602 // module map file that we've already loaded. 3603 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName); 3604 auto &Map = PP.getHeaderSearchInfo().getModuleMap(); 3605 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr; 3606 // Don't emit module relocation error if we have -fno-validate-pch 3607 if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) { 3608 assert(ImportedBy && "top-level import should be verified"); 3609 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) { 3610 if (auto *ASTFE = M ? M->getASTFile() : nullptr) { 3611 // This module was defined by an imported (explicit) module. 3612 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName 3613 << ASTFE->getName(); 3614 } else { 3615 // This module was built with a different module map. 3616 Diag(diag::err_imported_module_not_found) 3617 << F.ModuleName << F.FileName << ImportedBy->FileName 3618 << F.ModuleMapPath; 3619 // In case it was imported by a PCH, there's a chance the user is 3620 // just missing to include the search path to the directory containing 3621 // the modulemap. 3622 if (ImportedBy->Kind == MK_PCH) 3623 Diag(diag::note_imported_by_pch_module_not_found) 3624 << llvm::sys::path::parent_path(F.ModuleMapPath); 3625 } 3626 } 3627 return OutOfDate; 3628 } 3629 3630 assert(M->Name == F.ModuleName && "found module with different name"); 3631 3632 // Check the primary module map file. 3633 const FileEntry *StoredModMap = FileMgr.getFile(F.ModuleMapPath); 3634 if (StoredModMap == nullptr || StoredModMap != ModMap) { 3635 assert(ModMap && "found module is missing module map file"); 3636 assert(ImportedBy && "top-level import should be verified"); 3637 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3638 Diag(diag::err_imported_module_modmap_changed) 3639 << F.ModuleName << ImportedBy->FileName 3640 << ModMap->getName() << F.ModuleMapPath; 3641 return OutOfDate; 3642 } 3643 3644 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps; 3645 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) { 3646 // FIXME: we should use input files rather than storing names. 3647 std::string Filename = ReadPath(F, Record, Idx); 3648 const FileEntry *F = 3649 FileMgr.getFile(Filename, false, false); 3650 if (F == nullptr) { 3651 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3652 Error("could not find file '" + Filename +"' referenced by AST file"); 3653 return OutOfDate; 3654 } 3655 AdditionalStoredMaps.insert(F); 3656 } 3657 3658 // Check any additional module map files (e.g. module.private.modulemap) 3659 // that are not in the pcm. 3660 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) { 3661 for (const FileEntry *ModMap : *AdditionalModuleMaps) { 3662 // Remove files that match 3663 // Note: SmallPtrSet::erase is really remove 3664 if (!AdditionalStoredMaps.erase(ModMap)) { 3665 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3666 Diag(diag::err_module_different_modmap) 3667 << F.ModuleName << /*new*/0 << ModMap->getName(); 3668 return OutOfDate; 3669 } 3670 } 3671 } 3672 3673 // Check any additional module map files that are in the pcm, but not 3674 // found in header search. Cases that match are already removed. 3675 for (const FileEntry *ModMap : AdditionalStoredMaps) { 3676 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3677 Diag(diag::err_module_different_modmap) 3678 << F.ModuleName << /*not new*/1 << ModMap->getName(); 3679 return OutOfDate; 3680 } 3681 } 3682 3683 if (Listener) 3684 Listener->ReadModuleMapFile(F.ModuleMapPath); 3685 return Success; 3686 } 3687 3688 /// Move the given method to the back of the global list of methods. 3689 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) { 3690 // Find the entry for this selector in the method pool. 3691 Sema::GlobalMethodPool::iterator Known 3692 = S.MethodPool.find(Method->getSelector()); 3693 if (Known == S.MethodPool.end()) 3694 return; 3695 3696 // Retrieve the appropriate method list. 3697 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first 3698 : Known->second.second; 3699 bool Found = false; 3700 for (ObjCMethodList *List = &Start; List; List = List->getNext()) { 3701 if (!Found) { 3702 if (List->getMethod() == Method) { 3703 Found = true; 3704 } else { 3705 // Keep searching. 3706 continue; 3707 } 3708 } 3709 3710 if (List->getNext()) 3711 List->setMethod(List->getNext()->getMethod()); 3712 else 3713 List->setMethod(Method); 3714 } 3715 } 3716 3717 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) { 3718 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?"); 3719 for (Decl *D : Names) { 3720 bool wasHidden = D->isHidden(); 3721 D->setVisibleDespiteOwningModule(); 3722 3723 if (wasHidden && SemaObj) { 3724 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) { 3725 moveMethodToBackOfGlobalList(*SemaObj, Method); 3726 } 3727 } 3728 } 3729 } 3730 3731 void ASTReader::makeModuleVisible(Module *Mod, 3732 Module::NameVisibilityKind NameVisibility, 3733 SourceLocation ImportLoc) { 3734 llvm::SmallPtrSet<Module *, 4> Visited; 3735 SmallVector<Module *, 4> Stack; 3736 Stack.push_back(Mod); 3737 while (!Stack.empty()) { 3738 Mod = Stack.pop_back_val(); 3739 3740 if (NameVisibility <= Mod->NameVisibility) { 3741 // This module already has this level of visibility (or greater), so 3742 // there is nothing more to do. 3743 continue; 3744 } 3745 3746 if (!Mod->isAvailable()) { 3747 // Modules that aren't available cannot be made visible. 3748 continue; 3749 } 3750 3751 // Update the module's name visibility. 3752 Mod->NameVisibility = NameVisibility; 3753 3754 // If we've already deserialized any names from this module, 3755 // mark them as visible. 3756 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod); 3757 if (Hidden != HiddenNamesMap.end()) { 3758 auto HiddenNames = std::move(*Hidden); 3759 HiddenNamesMap.erase(Hidden); 3760 makeNamesVisible(HiddenNames.second, HiddenNames.first); 3761 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() && 3762 "making names visible added hidden names"); 3763 } 3764 3765 // Push any exported modules onto the stack to be marked as visible. 3766 SmallVector<Module *, 16> Exports; 3767 Mod->getExportedModules(Exports); 3768 for (SmallVectorImpl<Module *>::iterator 3769 I = Exports.begin(), E = Exports.end(); I != E; ++I) { 3770 Module *Exported = *I; 3771 if (Visited.insert(Exported).second) 3772 Stack.push_back(Exported); 3773 } 3774 } 3775 } 3776 3777 /// We've merged the definition \p MergedDef into the existing definition 3778 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made 3779 /// visible. 3780 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def, 3781 NamedDecl *MergedDef) { 3782 if (Def->isHidden()) { 3783 // If MergedDef is visible or becomes visible, make the definition visible. 3784 if (!MergedDef->isHidden()) 3785 Def->setVisibleDespiteOwningModule(); 3786 else { 3787 getContext().mergeDefinitionIntoModule( 3788 Def, MergedDef->getImportedOwningModule(), 3789 /*NotifyListeners*/ false); 3790 PendingMergedDefinitionsToDeduplicate.insert(Def); 3791 } 3792 } 3793 } 3794 3795 bool ASTReader::loadGlobalIndex() { 3796 if (GlobalIndex) 3797 return false; 3798 3799 if (TriedLoadingGlobalIndex || !UseGlobalIndex || 3800 !PP.getLangOpts().Modules) 3801 return true; 3802 3803 // Try to load the global index. 3804 TriedLoadingGlobalIndex = true; 3805 StringRef ModuleCachePath 3806 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath(); 3807 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result 3808 = GlobalModuleIndex::readIndex(ModuleCachePath); 3809 if (!Result.first) 3810 return true; 3811 3812 GlobalIndex.reset(Result.first); 3813 ModuleMgr.setGlobalIndex(GlobalIndex.get()); 3814 return false; 3815 } 3816 3817 bool ASTReader::isGlobalIndexUnavailable() const { 3818 return PP.getLangOpts().Modules && UseGlobalIndex && 3819 !hasGlobalIndex() && TriedLoadingGlobalIndex; 3820 } 3821 3822 static void updateModuleTimestamp(ModuleFile &MF) { 3823 // Overwrite the timestamp file contents so that file's mtime changes. 3824 std::string TimestampFilename = MF.getTimestampFilename(); 3825 std::error_code EC; 3826 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::F_Text); 3827 if (EC) 3828 return; 3829 OS << "Timestamp file\n"; 3830 OS.close(); 3831 OS.clear_error(); // Avoid triggering a fatal error. 3832 } 3833 3834 /// Given a cursor at the start of an AST file, scan ahead and drop the 3835 /// cursor into the start of the given block ID, returning false on success and 3836 /// true on failure. 3837 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) { 3838 while (true) { 3839 llvm::BitstreamEntry Entry = Cursor.advance(); 3840 switch (Entry.Kind) { 3841 case llvm::BitstreamEntry::Error: 3842 case llvm::BitstreamEntry::EndBlock: 3843 return true; 3844 3845 case llvm::BitstreamEntry::Record: 3846 // Ignore top-level records. 3847 Cursor.skipRecord(Entry.ID); 3848 break; 3849 3850 case llvm::BitstreamEntry::SubBlock: 3851 if (Entry.ID == BlockID) { 3852 if (Cursor.EnterSubBlock(BlockID)) 3853 return true; 3854 // Found it! 3855 return false; 3856 } 3857 3858 if (Cursor.SkipBlock()) 3859 return true; 3860 } 3861 } 3862 } 3863 3864 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, 3865 ModuleKind Type, 3866 SourceLocation ImportLoc, 3867 unsigned ClientLoadCapabilities, 3868 SmallVectorImpl<ImportedSubmodule> *Imported) { 3869 llvm::SaveAndRestore<SourceLocation> 3870 SetCurImportLocRAII(CurrentImportLoc, ImportLoc); 3871 3872 // Defer any pending actions until we get to the end of reading the AST file. 3873 Deserializing AnASTFile(this); 3874 3875 // Bump the generation number. 3876 unsigned PreviousGeneration = 0; 3877 if (ContextObj) 3878 PreviousGeneration = incrementGeneration(*ContextObj); 3879 3880 unsigned NumModules = ModuleMgr.size(); 3881 SmallVector<ImportedModule, 4> Loaded; 3882 switch (ASTReadResult ReadResult = 3883 ReadASTCore(FileName, Type, ImportLoc, 3884 /*ImportedBy=*/nullptr, Loaded, 0, 0, 3885 ASTFileSignature(), ClientLoadCapabilities)) { 3886 case Failure: 3887 case Missing: 3888 case OutOfDate: 3889 case VersionMismatch: 3890 case ConfigurationMismatch: 3891 case HadErrors: { 3892 llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet; 3893 for (const ImportedModule &IM : Loaded) 3894 LoadedSet.insert(IM.Mod); 3895 3896 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, LoadedSet, 3897 PP.getLangOpts().Modules 3898 ? &PP.getHeaderSearchInfo().getModuleMap() 3899 : nullptr); 3900 3901 // If we find that any modules are unusable, the global index is going 3902 // to be out-of-date. Just remove it. 3903 GlobalIndex.reset(); 3904 ModuleMgr.setGlobalIndex(nullptr); 3905 return ReadResult; 3906 } 3907 case Success: 3908 break; 3909 } 3910 3911 // Here comes stuff that we only do once the entire chain is loaded. 3912 3913 // Load the AST blocks of all of the modules that we loaded. 3914 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(), 3915 MEnd = Loaded.end(); 3916 M != MEnd; ++M) { 3917 ModuleFile &F = *M->Mod; 3918 3919 // Read the AST block. 3920 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities)) 3921 return Result; 3922 3923 // Read the extension blocks. 3924 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) { 3925 if (ASTReadResult Result = ReadExtensionBlock(F)) 3926 return Result; 3927 } 3928 3929 // Once read, set the ModuleFile bit base offset and update the size in 3930 // bits of all files we've seen. 3931 F.GlobalBitOffset = TotalModulesSizeInBits; 3932 TotalModulesSizeInBits += F.SizeInBits; 3933 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F)); 3934 3935 // Preload SLocEntries. 3936 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) { 3937 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID; 3938 // Load it through the SourceManager and don't call ReadSLocEntry() 3939 // directly because the entry may have already been loaded in which case 3940 // calling ReadSLocEntry() directly would trigger an assertion in 3941 // SourceManager. 3942 SourceMgr.getLoadedSLocEntryByID(Index); 3943 } 3944 3945 // Map the original source file ID into the ID space of the current 3946 // compilation. 3947 if (F.OriginalSourceFileID.isValid()) { 3948 F.OriginalSourceFileID = FileID::get( 3949 F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1); 3950 } 3951 3952 // Preload all the pending interesting identifiers by marking them out of 3953 // date. 3954 for (auto Offset : F.PreloadIdentifierOffsets) { 3955 const unsigned char *Data = reinterpret_cast<const unsigned char *>( 3956 F.IdentifierTableData + Offset); 3957 3958 ASTIdentifierLookupTrait Trait(*this, F); 3959 auto KeyDataLen = Trait.ReadKeyDataLength(Data); 3960 auto Key = Trait.ReadKey(Data, KeyDataLen.first); 3961 auto &II = PP.getIdentifierTable().getOwn(Key); 3962 II.setOutOfDate(true); 3963 3964 // Mark this identifier as being from an AST file so that we can track 3965 // whether we need to serialize it. 3966 markIdentifierFromAST(*this, II); 3967 3968 // Associate the ID with the identifier so that the writer can reuse it. 3969 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first); 3970 SetIdentifierInfo(ID, &II); 3971 } 3972 } 3973 3974 // Setup the import locations and notify the module manager that we've 3975 // committed to these module files. 3976 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(), 3977 MEnd = Loaded.end(); 3978 M != MEnd; ++M) { 3979 ModuleFile &F = *M->Mod; 3980 3981 ModuleMgr.moduleFileAccepted(&F); 3982 3983 // Set the import location. 3984 F.DirectImportLoc = ImportLoc; 3985 // FIXME: We assume that locations from PCH / preamble do not need 3986 // any translation. 3987 if (!M->ImportedBy) 3988 F.ImportLoc = M->ImportLoc; 3989 else 3990 F.ImportLoc = TranslateSourceLocation(*M->ImportedBy, M->ImportLoc); 3991 } 3992 3993 if (!PP.getLangOpts().CPlusPlus || 3994 (Type != MK_ImplicitModule && Type != MK_ExplicitModule && 3995 Type != MK_PrebuiltModule)) { 3996 // Mark all of the identifiers in the identifier table as being out of date, 3997 // so that various accessors know to check the loaded modules when the 3998 // identifier is used. 3999 // 4000 // For C++ modules, we don't need information on many identifiers (just 4001 // those that provide macros or are poisoned), so we mark all of 4002 // the interesting ones via PreloadIdentifierOffsets. 4003 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(), 4004 IdEnd = PP.getIdentifierTable().end(); 4005 Id != IdEnd; ++Id) 4006 Id->second->setOutOfDate(true); 4007 } 4008 // Mark selectors as out of date. 4009 for (auto Sel : SelectorGeneration) 4010 SelectorOutOfDate[Sel.first] = true; 4011 4012 // Resolve any unresolved module exports. 4013 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) { 4014 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I]; 4015 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID); 4016 Module *ResolvedMod = getSubmodule(GlobalID); 4017 4018 switch (Unresolved.Kind) { 4019 case UnresolvedModuleRef::Conflict: 4020 if (ResolvedMod) { 4021 Module::Conflict Conflict; 4022 Conflict.Other = ResolvedMod; 4023 Conflict.Message = Unresolved.String.str(); 4024 Unresolved.Mod->Conflicts.push_back(Conflict); 4025 } 4026 continue; 4027 4028 case UnresolvedModuleRef::Import: 4029 if (ResolvedMod) 4030 Unresolved.Mod->Imports.insert(ResolvedMod); 4031 continue; 4032 4033 case UnresolvedModuleRef::Export: 4034 if (ResolvedMod || Unresolved.IsWildcard) 4035 Unresolved.Mod->Exports.push_back( 4036 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard)); 4037 continue; 4038 } 4039 } 4040 UnresolvedModuleRefs.clear(); 4041 4042 if (Imported) 4043 Imported->append(ImportedModules.begin(), 4044 ImportedModules.end()); 4045 4046 // FIXME: How do we load the 'use'd modules? They may not be submodules. 4047 // Might be unnecessary as use declarations are only used to build the 4048 // module itself. 4049 4050 if (ContextObj) 4051 InitializeContext(); 4052 4053 if (SemaObj) 4054 UpdateSema(); 4055 4056 if (DeserializationListener) 4057 DeserializationListener->ReaderInitialized(this); 4058 4059 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule(); 4060 if (PrimaryModule.OriginalSourceFileID.isValid()) { 4061 // If this AST file is a precompiled preamble, then set the 4062 // preamble file ID of the source manager to the file source file 4063 // from which the preamble was built. 4064 if (Type == MK_Preamble) { 4065 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID); 4066 } else if (Type == MK_MainFile) { 4067 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID); 4068 } 4069 } 4070 4071 // For any Objective-C class definitions we have already loaded, make sure 4072 // that we load any additional categories. 4073 if (ContextObj) { 4074 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) { 4075 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(), 4076 ObjCClassesLoaded[I], 4077 PreviousGeneration); 4078 } 4079 } 4080 4081 if (PP.getHeaderSearchInfo() 4082 .getHeaderSearchOpts() 4083 .ModulesValidateOncePerBuildSession) { 4084 // Now we are certain that the module and all modules it depends on are 4085 // up to date. Create or update timestamp files for modules that are 4086 // located in the module cache (not for PCH files that could be anywhere 4087 // in the filesystem). 4088 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) { 4089 ImportedModule &M = Loaded[I]; 4090 if (M.Mod->Kind == MK_ImplicitModule) { 4091 updateModuleTimestamp(*M.Mod); 4092 } 4093 } 4094 } 4095 4096 return Success; 4097 } 4098 4099 static ASTFileSignature readASTFileSignature(StringRef PCH); 4100 4101 /// Whether \p Stream starts with the AST/PCH file magic number 'CPCH'. 4102 static bool startsWithASTFileMagic(BitstreamCursor &Stream) { 4103 return Stream.canSkipToPos(4) && 4104 Stream.Read(8) == 'C' && 4105 Stream.Read(8) == 'P' && 4106 Stream.Read(8) == 'C' && 4107 Stream.Read(8) == 'H'; 4108 } 4109 4110 static unsigned moduleKindForDiagnostic(ModuleKind Kind) { 4111 switch (Kind) { 4112 case MK_PCH: 4113 return 0; // PCH 4114 case MK_ImplicitModule: 4115 case MK_ExplicitModule: 4116 case MK_PrebuiltModule: 4117 return 1; // module 4118 case MK_MainFile: 4119 case MK_Preamble: 4120 return 2; // main source file 4121 } 4122 llvm_unreachable("unknown module kind"); 4123 } 4124 4125 ASTReader::ASTReadResult 4126 ASTReader::ReadASTCore(StringRef FileName, 4127 ModuleKind Type, 4128 SourceLocation ImportLoc, 4129 ModuleFile *ImportedBy, 4130 SmallVectorImpl<ImportedModule> &Loaded, 4131 off_t ExpectedSize, time_t ExpectedModTime, 4132 ASTFileSignature ExpectedSignature, 4133 unsigned ClientLoadCapabilities) { 4134 ModuleFile *M; 4135 std::string ErrorStr; 4136 ModuleManager::AddModuleResult AddResult 4137 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy, 4138 getGeneration(), ExpectedSize, ExpectedModTime, 4139 ExpectedSignature, readASTFileSignature, 4140 M, ErrorStr); 4141 4142 switch (AddResult) { 4143 case ModuleManager::AlreadyLoaded: 4144 return Success; 4145 4146 case ModuleManager::NewlyLoaded: 4147 // Load module file below. 4148 break; 4149 4150 case ModuleManager::Missing: 4151 // The module file was missing; if the client can handle that, return 4152 // it. 4153 if (ClientLoadCapabilities & ARR_Missing) 4154 return Missing; 4155 4156 // Otherwise, return an error. 4157 Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type) 4158 << FileName << !ErrorStr.empty() 4159 << ErrorStr; 4160 return Failure; 4161 4162 case ModuleManager::OutOfDate: 4163 // We couldn't load the module file because it is out-of-date. If the 4164 // client can handle out-of-date, return it. 4165 if (ClientLoadCapabilities & ARR_OutOfDate) 4166 return OutOfDate; 4167 4168 // Otherwise, return an error. 4169 Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type) 4170 << FileName << !ErrorStr.empty() 4171 << ErrorStr; 4172 return Failure; 4173 } 4174 4175 assert(M && "Missing module file"); 4176 4177 ModuleFile &F = *M; 4178 BitstreamCursor &Stream = F.Stream; 4179 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer)); 4180 F.SizeInBits = F.Buffer->getBufferSize() * 8; 4181 4182 // Sniff for the signature. 4183 if (!startsWithASTFileMagic(Stream)) { 4184 Diag(diag::err_module_file_invalid) << moduleKindForDiagnostic(Type) 4185 << FileName; 4186 return Failure; 4187 } 4188 4189 // This is used for compatibility with older PCH formats. 4190 bool HaveReadControlBlock = false; 4191 while (true) { 4192 llvm::BitstreamEntry Entry = Stream.advance(); 4193 4194 switch (Entry.Kind) { 4195 case llvm::BitstreamEntry::Error: 4196 case llvm::BitstreamEntry::Record: 4197 case llvm::BitstreamEntry::EndBlock: 4198 Error("invalid record at top-level of AST file"); 4199 return Failure; 4200 4201 case llvm::BitstreamEntry::SubBlock: 4202 break; 4203 } 4204 4205 switch (Entry.ID) { 4206 case CONTROL_BLOCK_ID: 4207 HaveReadControlBlock = true; 4208 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) { 4209 case Success: 4210 // Check that we didn't try to load a non-module AST file as a module. 4211 // 4212 // FIXME: Should we also perform the converse check? Loading a module as 4213 // a PCH file sort of works, but it's a bit wonky. 4214 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule || 4215 Type == MK_PrebuiltModule) && 4216 F.ModuleName.empty()) { 4217 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure; 4218 if (Result != OutOfDate || 4219 (ClientLoadCapabilities & ARR_OutOfDate) == 0) 4220 Diag(diag::err_module_file_not_module) << FileName; 4221 return Result; 4222 } 4223 break; 4224 4225 case Failure: return Failure; 4226 case Missing: return Missing; 4227 case OutOfDate: return OutOfDate; 4228 case VersionMismatch: return VersionMismatch; 4229 case ConfigurationMismatch: return ConfigurationMismatch; 4230 case HadErrors: return HadErrors; 4231 } 4232 break; 4233 4234 case AST_BLOCK_ID: 4235 if (!HaveReadControlBlock) { 4236 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 4237 Diag(diag::err_pch_version_too_old); 4238 return VersionMismatch; 4239 } 4240 4241 // Record that we've loaded this module. 4242 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc)); 4243 return Success; 4244 4245 case UNHASHED_CONTROL_BLOCK_ID: 4246 // This block is handled using look-ahead during ReadControlBlock. We 4247 // shouldn't get here! 4248 Error("malformed block record in AST file"); 4249 return Failure; 4250 4251 default: 4252 if (Stream.SkipBlock()) { 4253 Error("malformed block record in AST file"); 4254 return Failure; 4255 } 4256 break; 4257 } 4258 } 4259 4260 return Success; 4261 } 4262 4263 ASTReader::ASTReadResult 4264 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy, 4265 unsigned ClientLoadCapabilities) { 4266 const HeaderSearchOptions &HSOpts = 4267 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 4268 bool AllowCompatibleConfigurationMismatch = 4269 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 4270 4271 ASTReadResult Result = readUnhashedControlBlockImpl( 4272 &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch, 4273 Listener.get(), 4274 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions); 4275 4276 // If F was directly imported by another module, it's implicitly validated by 4277 // the importing module. 4278 if (DisableValidation || WasImportedBy || 4279 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 4280 return Success; 4281 4282 if (Result == Failure) { 4283 Error("malformed block record in AST file"); 4284 return Failure; 4285 } 4286 4287 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) { 4288 // If this module has already been finalized in the PCMCache, we're stuck 4289 // with it; we can only load a single version of each module. 4290 // 4291 // This can happen when a module is imported in two contexts: in one, as a 4292 // user module; in another, as a system module (due to an import from 4293 // another module marked with the [system] flag). It usually indicates a 4294 // bug in the module map: this module should also be marked with [system]. 4295 // 4296 // If -Wno-system-headers (the default), and the first import is as a 4297 // system module, then validation will fail during the as-user import, 4298 // since -Werror flags won't have been validated. However, it's reasonable 4299 // to treat this consistently as a system module. 4300 // 4301 // If -Wsystem-headers, the PCM on disk was built with 4302 // -Wno-system-headers, and the first import is as a user module, then 4303 // validation will fail during the as-system import since the PCM on disk 4304 // doesn't guarantee that -Werror was respected. However, the -Werror 4305 // flags were checked during the initial as-user import. 4306 if (PCMCache.isBufferFinal(F.FileName)) { 4307 Diag(diag::warn_module_system_bit_conflict) << F.FileName; 4308 return Success; 4309 } 4310 } 4311 4312 return Result; 4313 } 4314 4315 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl( 4316 ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities, 4317 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener, 4318 bool ValidateDiagnosticOptions) { 4319 // Initialize a stream. 4320 BitstreamCursor Stream(StreamData); 4321 4322 // Sniff for the signature. 4323 if (!startsWithASTFileMagic(Stream)) 4324 return Failure; 4325 4326 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 4327 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 4328 return Failure; 4329 4330 // Read all of the records in the options block. 4331 RecordData Record; 4332 ASTReadResult Result = Success; 4333 while (true) { 4334 llvm::BitstreamEntry Entry = Stream.advance(); 4335 4336 switch (Entry.Kind) { 4337 case llvm::BitstreamEntry::Error: 4338 case llvm::BitstreamEntry::SubBlock: 4339 return Failure; 4340 4341 case llvm::BitstreamEntry::EndBlock: 4342 return Result; 4343 4344 case llvm::BitstreamEntry::Record: 4345 // The interesting case. 4346 break; 4347 } 4348 4349 // Read and process a record. 4350 Record.clear(); 4351 switch ( 4352 (UnhashedControlBlockRecordTypes)Stream.readRecord(Entry.ID, Record)) { 4353 case SIGNATURE: 4354 if (F) 4355 std::copy(Record.begin(), Record.end(), F->Signature.data()); 4356 break; 4357 case DIAGNOSTIC_OPTIONS: { 4358 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 4359 if (Listener && ValidateDiagnosticOptions && 4360 !AllowCompatibleConfigurationMismatch && 4361 ParseDiagnosticOptions(Record, Complain, *Listener)) 4362 Result = OutOfDate; // Don't return early. Read the signature. 4363 break; 4364 } 4365 case DIAG_PRAGMA_MAPPINGS: 4366 if (!F) 4367 break; 4368 if (F->PragmaDiagMappings.empty()) 4369 F->PragmaDiagMappings.swap(Record); 4370 else 4371 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(), 4372 Record.begin(), Record.end()); 4373 break; 4374 } 4375 } 4376 } 4377 4378 /// Parse a record and blob containing module file extension metadata. 4379 static bool parseModuleFileExtensionMetadata( 4380 const SmallVectorImpl<uint64_t> &Record, 4381 StringRef Blob, 4382 ModuleFileExtensionMetadata &Metadata) { 4383 if (Record.size() < 4) return true; 4384 4385 Metadata.MajorVersion = Record[0]; 4386 Metadata.MinorVersion = Record[1]; 4387 4388 unsigned BlockNameLen = Record[2]; 4389 unsigned UserInfoLen = Record[3]; 4390 4391 if (BlockNameLen + UserInfoLen > Blob.size()) return true; 4392 4393 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen); 4394 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen, 4395 Blob.data() + BlockNameLen + UserInfoLen); 4396 return false; 4397 } 4398 4399 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) { 4400 BitstreamCursor &Stream = F.Stream; 4401 4402 RecordData Record; 4403 while (true) { 4404 llvm::BitstreamEntry Entry = Stream.advance(); 4405 switch (Entry.Kind) { 4406 case llvm::BitstreamEntry::SubBlock: 4407 if (Stream.SkipBlock()) 4408 return Failure; 4409 4410 continue; 4411 4412 case llvm::BitstreamEntry::EndBlock: 4413 return Success; 4414 4415 case llvm::BitstreamEntry::Error: 4416 return HadErrors; 4417 4418 case llvm::BitstreamEntry::Record: 4419 break; 4420 } 4421 4422 Record.clear(); 4423 StringRef Blob; 4424 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob); 4425 switch (RecCode) { 4426 case EXTENSION_METADATA: { 4427 ModuleFileExtensionMetadata Metadata; 4428 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) 4429 return Failure; 4430 4431 // Find a module file extension with this block name. 4432 auto Known = ModuleFileExtensions.find(Metadata.BlockName); 4433 if (Known == ModuleFileExtensions.end()) break; 4434 4435 // Form a reader. 4436 if (auto Reader = Known->second->createExtensionReader(Metadata, *this, 4437 F, Stream)) { 4438 F.ExtensionReaders.push_back(std::move(Reader)); 4439 } 4440 4441 break; 4442 } 4443 } 4444 } 4445 4446 return Success; 4447 } 4448 4449 void ASTReader::InitializeContext() { 4450 assert(ContextObj && "no context to initialize"); 4451 ASTContext &Context = *ContextObj; 4452 4453 // If there's a listener, notify them that we "read" the translation unit. 4454 if (DeserializationListener) 4455 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, 4456 Context.getTranslationUnitDecl()); 4457 4458 // FIXME: Find a better way to deal with collisions between these 4459 // built-in types. Right now, we just ignore the problem. 4460 4461 // Load the special types. 4462 if (SpecialTypes.size() >= NumSpecialTypeIDs) { 4463 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) { 4464 if (!Context.CFConstantStringTypeDecl) 4465 Context.setCFConstantStringType(GetType(String)); 4466 } 4467 4468 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) { 4469 QualType FileType = GetType(File); 4470 if (FileType.isNull()) { 4471 Error("FILE type is NULL"); 4472 return; 4473 } 4474 4475 if (!Context.FILEDecl) { 4476 if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) 4477 Context.setFILEDecl(Typedef->getDecl()); 4478 else { 4479 const TagType *Tag = FileType->getAs<TagType>(); 4480 if (!Tag) { 4481 Error("Invalid FILE type in AST file"); 4482 return; 4483 } 4484 Context.setFILEDecl(Tag->getDecl()); 4485 } 4486 } 4487 } 4488 4489 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) { 4490 QualType Jmp_bufType = GetType(Jmp_buf); 4491 if (Jmp_bufType.isNull()) { 4492 Error("jmp_buf type is NULL"); 4493 return; 4494 } 4495 4496 if (!Context.jmp_bufDecl) { 4497 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) 4498 Context.setjmp_bufDecl(Typedef->getDecl()); 4499 else { 4500 const TagType *Tag = Jmp_bufType->getAs<TagType>(); 4501 if (!Tag) { 4502 Error("Invalid jmp_buf type in AST file"); 4503 return; 4504 } 4505 Context.setjmp_bufDecl(Tag->getDecl()); 4506 } 4507 } 4508 } 4509 4510 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) { 4511 QualType Sigjmp_bufType = GetType(Sigjmp_buf); 4512 if (Sigjmp_bufType.isNull()) { 4513 Error("sigjmp_buf type is NULL"); 4514 return; 4515 } 4516 4517 if (!Context.sigjmp_bufDecl) { 4518 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) 4519 Context.setsigjmp_bufDecl(Typedef->getDecl()); 4520 else { 4521 const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); 4522 assert(Tag && "Invalid sigjmp_buf type in AST file"); 4523 Context.setsigjmp_bufDecl(Tag->getDecl()); 4524 } 4525 } 4526 } 4527 4528 if (unsigned ObjCIdRedef 4529 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) { 4530 if (Context.ObjCIdRedefinitionType.isNull()) 4531 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef); 4532 } 4533 4534 if (unsigned ObjCClassRedef 4535 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) { 4536 if (Context.ObjCClassRedefinitionType.isNull()) 4537 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef); 4538 } 4539 4540 if (unsigned ObjCSelRedef 4541 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) { 4542 if (Context.ObjCSelRedefinitionType.isNull()) 4543 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef); 4544 } 4545 4546 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) { 4547 QualType Ucontext_tType = GetType(Ucontext_t); 4548 if (Ucontext_tType.isNull()) { 4549 Error("ucontext_t type is NULL"); 4550 return; 4551 } 4552 4553 if (!Context.ucontext_tDecl) { 4554 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>()) 4555 Context.setucontext_tDecl(Typedef->getDecl()); 4556 else { 4557 const TagType *Tag = Ucontext_tType->getAs<TagType>(); 4558 assert(Tag && "Invalid ucontext_t type in AST file"); 4559 Context.setucontext_tDecl(Tag->getDecl()); 4560 } 4561 } 4562 } 4563 } 4564 4565 ReadPragmaDiagnosticMappings(Context.getDiagnostics()); 4566 4567 // If there were any CUDA special declarations, deserialize them. 4568 if (!CUDASpecialDeclRefs.empty()) { 4569 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!"); 4570 Context.setcudaConfigureCallDecl( 4571 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0]))); 4572 } 4573 4574 // Re-export any modules that were imported by a non-module AST file. 4575 // FIXME: This does not make macro-only imports visible again. 4576 for (auto &Import : ImportedModules) { 4577 if (Module *Imported = getSubmodule(Import.ID)) { 4578 makeModuleVisible(Imported, Module::AllVisible, 4579 /*ImportLoc=*/Import.ImportLoc); 4580 if (Import.ImportLoc.isValid()) 4581 PP.makeModuleVisible(Imported, Import.ImportLoc); 4582 // FIXME: should we tell Sema to make the module visible too? 4583 } 4584 } 4585 ImportedModules.clear(); 4586 } 4587 4588 void ASTReader::finalizeForWriting() { 4589 // Nothing to do for now. 4590 } 4591 4592 /// Reads and return the signature record from \p PCH's control block, or 4593 /// else returns 0. 4594 static ASTFileSignature readASTFileSignature(StringRef PCH) { 4595 BitstreamCursor Stream(PCH); 4596 if (!startsWithASTFileMagic(Stream)) 4597 return ASTFileSignature(); 4598 4599 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 4600 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 4601 return ASTFileSignature(); 4602 4603 // Scan for SIGNATURE inside the diagnostic options block. 4604 ASTReader::RecordData Record; 4605 while (true) { 4606 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 4607 if (Entry.Kind != llvm::BitstreamEntry::Record) 4608 return ASTFileSignature(); 4609 4610 Record.clear(); 4611 StringRef Blob; 4612 if (SIGNATURE == Stream.readRecord(Entry.ID, Record, &Blob)) 4613 return {{{(uint32_t)Record[0], (uint32_t)Record[1], (uint32_t)Record[2], 4614 (uint32_t)Record[3], (uint32_t)Record[4]}}}; 4615 } 4616 } 4617 4618 /// Retrieve the name of the original source file name 4619 /// directly from the AST file, without actually loading the AST 4620 /// file. 4621 std::string ASTReader::getOriginalSourceFile( 4622 const std::string &ASTFileName, FileManager &FileMgr, 4623 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) { 4624 // Open the AST file. 4625 auto Buffer = FileMgr.getBufferForFile(ASTFileName); 4626 if (!Buffer) { 4627 Diags.Report(diag::err_fe_unable_to_read_pch_file) 4628 << ASTFileName << Buffer.getError().message(); 4629 return std::string(); 4630 } 4631 4632 // Initialize the stream 4633 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer)); 4634 4635 // Sniff for the signature. 4636 if (!startsWithASTFileMagic(Stream)) { 4637 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName; 4638 return std::string(); 4639 } 4640 4641 // Scan for the CONTROL_BLOCK_ID block. 4642 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) { 4643 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 4644 return std::string(); 4645 } 4646 4647 // Scan for ORIGINAL_FILE inside the control block. 4648 RecordData Record; 4649 while (true) { 4650 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); 4651 if (Entry.Kind == llvm::BitstreamEntry::EndBlock) 4652 return std::string(); 4653 4654 if (Entry.Kind != llvm::BitstreamEntry::Record) { 4655 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 4656 return std::string(); 4657 } 4658 4659 Record.clear(); 4660 StringRef Blob; 4661 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE) 4662 return Blob.str(); 4663 } 4664 } 4665 4666 namespace { 4667 4668 class SimplePCHValidator : public ASTReaderListener { 4669 const LangOptions &ExistingLangOpts; 4670 const TargetOptions &ExistingTargetOpts; 4671 const PreprocessorOptions &ExistingPPOpts; 4672 std::string ExistingModuleCachePath; 4673 FileManager &FileMgr; 4674 4675 public: 4676 SimplePCHValidator(const LangOptions &ExistingLangOpts, 4677 const TargetOptions &ExistingTargetOpts, 4678 const PreprocessorOptions &ExistingPPOpts, 4679 StringRef ExistingModuleCachePath, 4680 FileManager &FileMgr) 4681 : ExistingLangOpts(ExistingLangOpts), 4682 ExistingTargetOpts(ExistingTargetOpts), 4683 ExistingPPOpts(ExistingPPOpts), 4684 ExistingModuleCachePath(ExistingModuleCachePath), 4685 FileMgr(FileMgr) {} 4686 4687 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain, 4688 bool AllowCompatibleDifferences) override { 4689 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr, 4690 AllowCompatibleDifferences); 4691 } 4692 4693 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain, 4694 bool AllowCompatibleDifferences) override { 4695 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr, 4696 AllowCompatibleDifferences); 4697 } 4698 4699 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 4700 StringRef SpecificModuleCachePath, 4701 bool Complain) override { 4702 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 4703 ExistingModuleCachePath, 4704 nullptr, ExistingLangOpts); 4705 } 4706 4707 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 4708 bool Complain, 4709 std::string &SuggestedPredefines) override { 4710 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr, 4711 SuggestedPredefines, ExistingLangOpts); 4712 } 4713 }; 4714 4715 } // namespace 4716 4717 bool ASTReader::readASTFileControlBlock( 4718 StringRef Filename, FileManager &FileMgr, 4719 const PCHContainerReader &PCHContainerRdr, 4720 bool FindModuleFileExtensions, 4721 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) { 4722 // Open the AST file. 4723 // FIXME: This allows use of the VFS; we do not allow use of the 4724 // VFS when actually loading a module. 4725 auto Buffer = FileMgr.getBufferForFile(Filename); 4726 if (!Buffer) { 4727 return true; 4728 } 4729 4730 // Initialize the stream 4731 StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer); 4732 BitstreamCursor Stream(Bytes); 4733 4734 // Sniff for the signature. 4735 if (!startsWithASTFileMagic(Stream)) 4736 return true; 4737 4738 // Scan for the CONTROL_BLOCK_ID block. 4739 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) 4740 return true; 4741 4742 bool NeedsInputFiles = Listener.needsInputFileVisitation(); 4743 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation(); 4744 bool NeedsImports = Listener.needsImportVisitation(); 4745 BitstreamCursor InputFilesCursor; 4746 4747 RecordData Record; 4748 std::string ModuleDir; 4749 bool DoneWithControlBlock = false; 4750 while (!DoneWithControlBlock) { 4751 llvm::BitstreamEntry Entry = Stream.advance(); 4752 4753 switch (Entry.Kind) { 4754 case llvm::BitstreamEntry::SubBlock: { 4755 switch (Entry.ID) { 4756 case OPTIONS_BLOCK_ID: { 4757 std::string IgnoredSuggestedPredefines; 4758 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate, 4759 /*AllowCompatibleConfigurationMismatch*/ false, 4760 Listener, IgnoredSuggestedPredefines) != Success) 4761 return true; 4762 break; 4763 } 4764 4765 case INPUT_FILES_BLOCK_ID: 4766 InputFilesCursor = Stream; 4767 if (Stream.SkipBlock() || 4768 (NeedsInputFiles && 4769 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))) 4770 return true; 4771 break; 4772 4773 default: 4774 if (Stream.SkipBlock()) 4775 return true; 4776 break; 4777 } 4778 4779 continue; 4780 } 4781 4782 case llvm::BitstreamEntry::EndBlock: 4783 DoneWithControlBlock = true; 4784 break; 4785 4786 case llvm::BitstreamEntry::Error: 4787 return true; 4788 4789 case llvm::BitstreamEntry::Record: 4790 break; 4791 } 4792 4793 if (DoneWithControlBlock) break; 4794 4795 Record.clear(); 4796 StringRef Blob; 4797 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob); 4798 switch ((ControlRecordTypes)RecCode) { 4799 case METADATA: 4800 if (Record[0] != VERSION_MAJOR) 4801 return true; 4802 if (Listener.ReadFullVersionInformation(Blob)) 4803 return true; 4804 break; 4805 case MODULE_NAME: 4806 Listener.ReadModuleName(Blob); 4807 break; 4808 case MODULE_DIRECTORY: 4809 ModuleDir = Blob; 4810 break; 4811 case MODULE_MAP_FILE: { 4812 unsigned Idx = 0; 4813 auto Path = ReadString(Record, Idx); 4814 ResolveImportedPath(Path, ModuleDir); 4815 Listener.ReadModuleMapFile(Path); 4816 break; 4817 } 4818 case INPUT_FILE_OFFSETS: { 4819 if (!NeedsInputFiles) 4820 break; 4821 4822 unsigned NumInputFiles = Record[0]; 4823 unsigned NumUserFiles = Record[1]; 4824 const llvm::support::unaligned_uint64_t *InputFileOffs = 4825 (const llvm::support::unaligned_uint64_t *)Blob.data(); 4826 for (unsigned I = 0; I != NumInputFiles; ++I) { 4827 // Go find this input file. 4828 bool isSystemFile = I >= NumUserFiles; 4829 4830 if (isSystemFile && !NeedsSystemInputFiles) 4831 break; // the rest are system input files 4832 4833 BitstreamCursor &Cursor = InputFilesCursor; 4834 SavedStreamPosition SavedPosition(Cursor); 4835 Cursor.JumpToBit(InputFileOffs[I]); 4836 4837 unsigned Code = Cursor.ReadCode(); 4838 RecordData Record; 4839 StringRef Blob; 4840 bool shouldContinue = false; 4841 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) { 4842 case INPUT_FILE: 4843 bool Overridden = static_cast<bool>(Record[3]); 4844 std::string Filename = Blob; 4845 ResolveImportedPath(Filename, ModuleDir); 4846 shouldContinue = Listener.visitInputFile( 4847 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false); 4848 break; 4849 } 4850 if (!shouldContinue) 4851 break; 4852 } 4853 break; 4854 } 4855 4856 case IMPORTS: { 4857 if (!NeedsImports) 4858 break; 4859 4860 unsigned Idx = 0, N = Record.size(); 4861 while (Idx < N) { 4862 // Read information about the AST file. 4863 Idx += 1+1+1+1+5; // Kind, ImportLoc, Size, ModTime, Signature 4864 std::string ModuleName = ReadString(Record, Idx); 4865 std::string Filename = ReadString(Record, Idx); 4866 ResolveImportedPath(Filename, ModuleDir); 4867 Listener.visitImport(ModuleName, Filename); 4868 } 4869 break; 4870 } 4871 4872 default: 4873 // No other validation to perform. 4874 break; 4875 } 4876 } 4877 4878 // Look for module file extension blocks, if requested. 4879 if (FindModuleFileExtensions) { 4880 BitstreamCursor SavedStream = Stream; 4881 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) { 4882 bool DoneWithExtensionBlock = false; 4883 while (!DoneWithExtensionBlock) { 4884 llvm::BitstreamEntry Entry = Stream.advance(); 4885 4886 switch (Entry.Kind) { 4887 case llvm::BitstreamEntry::SubBlock: 4888 if (Stream.SkipBlock()) 4889 return true; 4890 4891 continue; 4892 4893 case llvm::BitstreamEntry::EndBlock: 4894 DoneWithExtensionBlock = true; 4895 continue; 4896 4897 case llvm::BitstreamEntry::Error: 4898 return true; 4899 4900 case llvm::BitstreamEntry::Record: 4901 break; 4902 } 4903 4904 Record.clear(); 4905 StringRef Blob; 4906 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob); 4907 switch (RecCode) { 4908 case EXTENSION_METADATA: { 4909 ModuleFileExtensionMetadata Metadata; 4910 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) 4911 return true; 4912 4913 Listener.readModuleFileExtension(Metadata); 4914 break; 4915 } 4916 } 4917 } 4918 } 4919 Stream = SavedStream; 4920 } 4921 4922 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 4923 if (readUnhashedControlBlockImpl( 4924 nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate, 4925 /*AllowCompatibleConfigurationMismatch*/ false, &Listener, 4926 ValidateDiagnosticOptions) != Success) 4927 return true; 4928 4929 return false; 4930 } 4931 4932 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr, 4933 const PCHContainerReader &PCHContainerRdr, 4934 const LangOptions &LangOpts, 4935 const TargetOptions &TargetOpts, 4936 const PreprocessorOptions &PPOpts, 4937 StringRef ExistingModuleCachePath) { 4938 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, 4939 ExistingModuleCachePath, FileMgr); 4940 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr, 4941 /*FindModuleFileExtensions=*/false, 4942 validator, 4943 /*ValidateDiagnosticOptions=*/true); 4944 } 4945 4946 ASTReader::ASTReadResult 4947 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) { 4948 // Enter the submodule block. 4949 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) { 4950 Error("malformed submodule block record in AST file"); 4951 return Failure; 4952 } 4953 4954 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap(); 4955 bool First = true; 4956 Module *CurrentModule = nullptr; 4957 RecordData Record; 4958 while (true) { 4959 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks(); 4960 4961 switch (Entry.Kind) { 4962 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 4963 case llvm::BitstreamEntry::Error: 4964 Error("malformed block record in AST file"); 4965 return Failure; 4966 case llvm::BitstreamEntry::EndBlock: 4967 return Success; 4968 case llvm::BitstreamEntry::Record: 4969 // The interesting case. 4970 break; 4971 } 4972 4973 // Read a record. 4974 StringRef Blob; 4975 Record.clear(); 4976 auto Kind = F.Stream.readRecord(Entry.ID, Record, &Blob); 4977 4978 if ((Kind == SUBMODULE_METADATA) != First) { 4979 Error("submodule metadata record should be at beginning of block"); 4980 return Failure; 4981 } 4982 First = false; 4983 4984 // Submodule information is only valid if we have a current module. 4985 // FIXME: Should we error on these cases? 4986 if (!CurrentModule && Kind != SUBMODULE_METADATA && 4987 Kind != SUBMODULE_DEFINITION) 4988 continue; 4989 4990 switch (Kind) { 4991 default: // Default behavior: ignore. 4992 break; 4993 4994 case SUBMODULE_DEFINITION: { 4995 if (Record.size() < 12) { 4996 Error("malformed module definition"); 4997 return Failure; 4998 } 4999 5000 StringRef Name = Blob; 5001 unsigned Idx = 0; 5002 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]); 5003 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]); 5004 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++]; 5005 bool IsFramework = Record[Idx++]; 5006 bool IsExplicit = Record[Idx++]; 5007 bool IsSystem = Record[Idx++]; 5008 bool IsExternC = Record[Idx++]; 5009 bool InferSubmodules = Record[Idx++]; 5010 bool InferExplicitSubmodules = Record[Idx++]; 5011 bool InferExportWildcard = Record[Idx++]; 5012 bool ConfigMacrosExhaustive = Record[Idx++]; 5013 bool ModuleMapIsPrivate = Record[Idx++]; 5014 5015 Module *ParentModule = nullptr; 5016 if (Parent) 5017 ParentModule = getSubmodule(Parent); 5018 5019 // Retrieve this (sub)module from the module map, creating it if 5020 // necessary. 5021 CurrentModule = 5022 ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit) 5023 .first; 5024 5025 // FIXME: set the definition loc for CurrentModule, or call 5026 // ModMap.setInferredModuleAllowedBy() 5027 5028 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS; 5029 if (GlobalIndex >= SubmodulesLoaded.size() || 5030 SubmodulesLoaded[GlobalIndex]) { 5031 Error("too many submodules"); 5032 return Failure; 5033 } 5034 5035 if (!ParentModule) { 5036 if (const FileEntry *CurFile = CurrentModule->getASTFile()) { 5037 // Don't emit module relocation error if we have -fno-validate-pch 5038 if (!PP.getPreprocessorOpts().DisablePCHValidation && 5039 CurFile != F.File) { 5040 if (!Diags.isDiagnosticInFlight()) { 5041 Diag(diag::err_module_file_conflict) 5042 << CurrentModule->getTopLevelModuleName() 5043 << CurFile->getName() 5044 << F.File->getName(); 5045 } 5046 return Failure; 5047 } 5048 } 5049 5050 CurrentModule->setASTFile(F.File); 5051 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath; 5052 } 5053 5054 CurrentModule->Kind = Kind; 5055 CurrentModule->Signature = F.Signature; 5056 CurrentModule->IsFromModuleFile = true; 5057 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem; 5058 CurrentModule->IsExternC = IsExternC; 5059 CurrentModule->InferSubmodules = InferSubmodules; 5060 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules; 5061 CurrentModule->InferExportWildcard = InferExportWildcard; 5062 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive; 5063 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate; 5064 if (DeserializationListener) 5065 DeserializationListener->ModuleRead(GlobalID, CurrentModule); 5066 5067 SubmodulesLoaded[GlobalIndex] = CurrentModule; 5068 5069 // Clear out data that will be replaced by what is in the module file. 5070 CurrentModule->LinkLibraries.clear(); 5071 CurrentModule->ConfigMacros.clear(); 5072 CurrentModule->UnresolvedConflicts.clear(); 5073 CurrentModule->Conflicts.clear(); 5074 5075 // The module is available unless it's missing a requirement; relevant 5076 // requirements will be (re-)added by SUBMODULE_REQUIRES records. 5077 // Missing headers that were present when the module was built do not 5078 // make it unavailable -- if we got this far, this must be an explicitly 5079 // imported module file. 5080 CurrentModule->Requirements.clear(); 5081 CurrentModule->MissingHeaders.clear(); 5082 CurrentModule->IsMissingRequirement = 5083 ParentModule && ParentModule->IsMissingRequirement; 5084 CurrentModule->IsAvailable = !CurrentModule->IsMissingRequirement; 5085 break; 5086 } 5087 5088 case SUBMODULE_UMBRELLA_HEADER: { 5089 std::string Filename = Blob; 5090 ResolveImportedPath(F, Filename); 5091 if (auto *Umbrella = PP.getFileManager().getFile(Filename)) { 5092 if (!CurrentModule->getUmbrellaHeader()) 5093 ModMap.setUmbrellaHeader(CurrentModule, Umbrella, Blob); 5094 else if (CurrentModule->getUmbrellaHeader().Entry != Umbrella) { 5095 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 5096 Error("mismatched umbrella headers in submodule"); 5097 return OutOfDate; 5098 } 5099 } 5100 break; 5101 } 5102 5103 case SUBMODULE_HEADER: 5104 case SUBMODULE_EXCLUDED_HEADER: 5105 case SUBMODULE_PRIVATE_HEADER: 5106 // We lazily associate headers with their modules via the HeaderInfo table. 5107 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead 5108 // of complete filenames or remove it entirely. 5109 break; 5110 5111 case SUBMODULE_TEXTUAL_HEADER: 5112 case SUBMODULE_PRIVATE_TEXTUAL_HEADER: 5113 // FIXME: Textual headers are not marked in the HeaderInfo table. Load 5114 // them here. 5115 break; 5116 5117 case SUBMODULE_TOPHEADER: 5118 CurrentModule->addTopHeaderFilename(Blob); 5119 break; 5120 5121 case SUBMODULE_UMBRELLA_DIR: { 5122 std::string Dirname = Blob; 5123 ResolveImportedPath(F, Dirname); 5124 if (auto *Umbrella = PP.getFileManager().getDirectory(Dirname)) { 5125 if (!CurrentModule->getUmbrellaDir()) 5126 ModMap.setUmbrellaDir(CurrentModule, Umbrella, Blob); 5127 else if (CurrentModule->getUmbrellaDir().Entry != Umbrella) { 5128 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 5129 Error("mismatched umbrella directories in submodule"); 5130 return OutOfDate; 5131 } 5132 } 5133 break; 5134 } 5135 5136 case SUBMODULE_METADATA: { 5137 F.BaseSubmoduleID = getTotalNumSubmodules(); 5138 F.LocalNumSubmodules = Record[0]; 5139 unsigned LocalBaseSubmoduleID = Record[1]; 5140 if (F.LocalNumSubmodules > 0) { 5141 // Introduce the global -> local mapping for submodules within this 5142 // module. 5143 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F)); 5144 5145 // Introduce the local -> global mapping for submodules within this 5146 // module. 5147 F.SubmoduleRemap.insertOrReplace( 5148 std::make_pair(LocalBaseSubmoduleID, 5149 F.BaseSubmoduleID - LocalBaseSubmoduleID)); 5150 5151 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules); 5152 } 5153 break; 5154 } 5155 5156 case SUBMODULE_IMPORTS: 5157 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) { 5158 UnresolvedModuleRef Unresolved; 5159 Unresolved.File = &F; 5160 Unresolved.Mod = CurrentModule; 5161 Unresolved.ID = Record[Idx]; 5162 Unresolved.Kind = UnresolvedModuleRef::Import; 5163 Unresolved.IsWildcard = false; 5164 UnresolvedModuleRefs.push_back(Unresolved); 5165 } 5166 break; 5167 5168 case SUBMODULE_EXPORTS: 5169 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) { 5170 UnresolvedModuleRef Unresolved; 5171 Unresolved.File = &F; 5172 Unresolved.Mod = CurrentModule; 5173 Unresolved.ID = Record[Idx]; 5174 Unresolved.Kind = UnresolvedModuleRef::Export; 5175 Unresolved.IsWildcard = Record[Idx + 1]; 5176 UnresolvedModuleRefs.push_back(Unresolved); 5177 } 5178 5179 // Once we've loaded the set of exports, there's no reason to keep 5180 // the parsed, unresolved exports around. 5181 CurrentModule->UnresolvedExports.clear(); 5182 break; 5183 5184 case SUBMODULE_REQUIRES: 5185 CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(), 5186 PP.getTargetInfo()); 5187 break; 5188 5189 case SUBMODULE_LINK_LIBRARY: 5190 ModMap.resolveLinkAsDependencies(CurrentModule); 5191 CurrentModule->LinkLibraries.push_back( 5192 Module::LinkLibrary(Blob, Record[0])); 5193 break; 5194 5195 case SUBMODULE_CONFIG_MACRO: 5196 CurrentModule->ConfigMacros.push_back(Blob.str()); 5197 break; 5198 5199 case SUBMODULE_CONFLICT: { 5200 UnresolvedModuleRef Unresolved; 5201 Unresolved.File = &F; 5202 Unresolved.Mod = CurrentModule; 5203 Unresolved.ID = Record[0]; 5204 Unresolved.Kind = UnresolvedModuleRef::Conflict; 5205 Unresolved.IsWildcard = false; 5206 Unresolved.String = Blob; 5207 UnresolvedModuleRefs.push_back(Unresolved); 5208 break; 5209 } 5210 5211 case SUBMODULE_INITIALIZERS: { 5212 if (!ContextObj) 5213 break; 5214 SmallVector<uint32_t, 16> Inits; 5215 for (auto &ID : Record) 5216 Inits.push_back(getGlobalDeclID(F, ID)); 5217 ContextObj->addLazyModuleInitializers(CurrentModule, Inits); 5218 break; 5219 } 5220 5221 case SUBMODULE_EXPORT_AS: 5222 CurrentModule->ExportAsModule = Blob.str(); 5223 ModMap.addLinkAsDependency(CurrentModule); 5224 break; 5225 } 5226 } 5227 } 5228 5229 /// Parse the record that corresponds to a LangOptions data 5230 /// structure. 5231 /// 5232 /// This routine parses the language options from the AST file and then gives 5233 /// them to the AST listener if one is set. 5234 /// 5235 /// \returns true if the listener deems the file unacceptable, false otherwise. 5236 bool ASTReader::ParseLanguageOptions(const RecordData &Record, 5237 bool Complain, 5238 ASTReaderListener &Listener, 5239 bool AllowCompatibleDifferences) { 5240 LangOptions LangOpts; 5241 unsigned Idx = 0; 5242 #define LANGOPT(Name, Bits, Default, Description) \ 5243 LangOpts.Name = Record[Idx++]; 5244 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 5245 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++])); 5246 #include "clang/Basic/LangOptions.def" 5247 #define SANITIZER(NAME, ID) \ 5248 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]); 5249 #include "clang/Basic/Sanitizers.def" 5250 5251 for (unsigned N = Record[Idx++]; N; --N) 5252 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx)); 5253 5254 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++]; 5255 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx); 5256 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion); 5257 5258 LangOpts.CurrentModule = ReadString(Record, Idx); 5259 5260 // Comment options. 5261 for (unsigned N = Record[Idx++]; N; --N) { 5262 LangOpts.CommentOpts.BlockCommandNames.push_back( 5263 ReadString(Record, Idx)); 5264 } 5265 LangOpts.CommentOpts.ParseAllComments = Record[Idx++]; 5266 5267 // OpenMP offloading options. 5268 for (unsigned N = Record[Idx++]; N; --N) { 5269 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx))); 5270 } 5271 5272 LangOpts.OMPHostIRFile = ReadString(Record, Idx); 5273 5274 return Listener.ReadLanguageOptions(LangOpts, Complain, 5275 AllowCompatibleDifferences); 5276 } 5277 5278 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain, 5279 ASTReaderListener &Listener, 5280 bool AllowCompatibleDifferences) { 5281 unsigned Idx = 0; 5282 TargetOptions TargetOpts; 5283 TargetOpts.Triple = ReadString(Record, Idx); 5284 TargetOpts.CPU = ReadString(Record, Idx); 5285 TargetOpts.ABI = ReadString(Record, Idx); 5286 for (unsigned N = Record[Idx++]; N; --N) { 5287 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx)); 5288 } 5289 for (unsigned N = Record[Idx++]; N; --N) { 5290 TargetOpts.Features.push_back(ReadString(Record, Idx)); 5291 } 5292 5293 return Listener.ReadTargetOptions(TargetOpts, Complain, 5294 AllowCompatibleDifferences); 5295 } 5296 5297 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain, 5298 ASTReaderListener &Listener) { 5299 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions); 5300 unsigned Idx = 0; 5301 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++]; 5302 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \ 5303 DiagOpts->set##Name(static_cast<Type>(Record[Idx++])); 5304 #include "clang/Basic/DiagnosticOptions.def" 5305 5306 for (unsigned N = Record[Idx++]; N; --N) 5307 DiagOpts->Warnings.push_back(ReadString(Record, Idx)); 5308 for (unsigned N = Record[Idx++]; N; --N) 5309 DiagOpts->Remarks.push_back(ReadString(Record, Idx)); 5310 5311 return Listener.ReadDiagnosticOptions(DiagOpts, Complain); 5312 } 5313 5314 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain, 5315 ASTReaderListener &Listener) { 5316 FileSystemOptions FSOpts; 5317 unsigned Idx = 0; 5318 FSOpts.WorkingDir = ReadString(Record, Idx); 5319 return Listener.ReadFileSystemOptions(FSOpts, Complain); 5320 } 5321 5322 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record, 5323 bool Complain, 5324 ASTReaderListener &Listener) { 5325 HeaderSearchOptions HSOpts; 5326 unsigned Idx = 0; 5327 HSOpts.Sysroot = ReadString(Record, Idx); 5328 5329 // Include entries. 5330 for (unsigned N = Record[Idx++]; N; --N) { 5331 std::string Path = ReadString(Record, Idx); 5332 frontend::IncludeDirGroup Group 5333 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]); 5334 bool IsFramework = Record[Idx++]; 5335 bool IgnoreSysRoot = Record[Idx++]; 5336 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework, 5337 IgnoreSysRoot); 5338 } 5339 5340 // System header prefixes. 5341 for (unsigned N = Record[Idx++]; N; --N) { 5342 std::string Prefix = ReadString(Record, Idx); 5343 bool IsSystemHeader = Record[Idx++]; 5344 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader); 5345 } 5346 5347 HSOpts.ResourceDir = ReadString(Record, Idx); 5348 HSOpts.ModuleCachePath = ReadString(Record, Idx); 5349 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx); 5350 HSOpts.DisableModuleHash = Record[Idx++]; 5351 HSOpts.ImplicitModuleMaps = Record[Idx++]; 5352 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++]; 5353 HSOpts.UseBuiltinIncludes = Record[Idx++]; 5354 HSOpts.UseStandardSystemIncludes = Record[Idx++]; 5355 HSOpts.UseStandardCXXIncludes = Record[Idx++]; 5356 HSOpts.UseLibcxx = Record[Idx++]; 5357 std::string SpecificModuleCachePath = ReadString(Record, Idx); 5358 5359 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5360 Complain); 5361 } 5362 5363 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record, 5364 bool Complain, 5365 ASTReaderListener &Listener, 5366 std::string &SuggestedPredefines) { 5367 PreprocessorOptions PPOpts; 5368 unsigned Idx = 0; 5369 5370 // Macro definitions/undefs 5371 for (unsigned N = Record[Idx++]; N; --N) { 5372 std::string Macro = ReadString(Record, Idx); 5373 bool IsUndef = Record[Idx++]; 5374 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef)); 5375 } 5376 5377 // Includes 5378 for (unsigned N = Record[Idx++]; N; --N) { 5379 PPOpts.Includes.push_back(ReadString(Record, Idx)); 5380 } 5381 5382 // Macro Includes 5383 for (unsigned N = Record[Idx++]; N; --N) { 5384 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx)); 5385 } 5386 5387 PPOpts.UsePredefines = Record[Idx++]; 5388 PPOpts.DetailedRecord = Record[Idx++]; 5389 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx); 5390 PPOpts.ObjCXXARCStandardLibrary = 5391 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]); 5392 SuggestedPredefines.clear(); 5393 return Listener.ReadPreprocessorOptions(PPOpts, Complain, 5394 SuggestedPredefines); 5395 } 5396 5397 std::pair<ModuleFile *, unsigned> 5398 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) { 5399 GlobalPreprocessedEntityMapType::iterator 5400 I = GlobalPreprocessedEntityMap.find(GlobalIndex); 5401 assert(I != GlobalPreprocessedEntityMap.end() && 5402 "Corrupted global preprocessed entity map"); 5403 ModuleFile *M = I->second; 5404 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID; 5405 return std::make_pair(M, LocalIndex); 5406 } 5407 5408 llvm::iterator_range<PreprocessingRecord::iterator> 5409 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const { 5410 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord()) 5411 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID, 5412 Mod.NumPreprocessedEntities); 5413 5414 return llvm::make_range(PreprocessingRecord::iterator(), 5415 PreprocessingRecord::iterator()); 5416 } 5417 5418 llvm::iterator_range<ASTReader::ModuleDeclIterator> 5419 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) { 5420 return llvm::make_range( 5421 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls), 5422 ModuleDeclIterator(this, &Mod, 5423 Mod.FileSortedDecls + Mod.NumFileSortedDecls)); 5424 } 5425 5426 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) { 5427 auto I = GlobalSkippedRangeMap.find(GlobalIndex); 5428 assert(I != GlobalSkippedRangeMap.end() && 5429 "Corrupted global skipped range map"); 5430 ModuleFile *M = I->second; 5431 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID; 5432 assert(LocalIndex < M->NumPreprocessedSkippedRanges); 5433 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex]; 5434 SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()), 5435 TranslateSourceLocation(*M, RawRange.getEnd())); 5436 assert(Range.isValid()); 5437 return Range; 5438 } 5439 5440 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) { 5441 PreprocessedEntityID PPID = Index+1; 5442 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 5443 ModuleFile &M = *PPInfo.first; 5444 unsigned LocalIndex = PPInfo.second; 5445 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 5446 5447 if (!PP.getPreprocessingRecord()) { 5448 Error("no preprocessing record"); 5449 return nullptr; 5450 } 5451 5452 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor); 5453 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset); 5454 5455 llvm::BitstreamEntry Entry = 5456 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 5457 if (Entry.Kind != llvm::BitstreamEntry::Record) 5458 return nullptr; 5459 5460 // Read the record. 5461 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()), 5462 TranslateSourceLocation(M, PPOffs.getEnd())); 5463 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 5464 StringRef Blob; 5465 RecordData Record; 5466 PreprocessorDetailRecordTypes RecType = 5467 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord( 5468 Entry.ID, Record, &Blob); 5469 switch (RecType) { 5470 case PPD_MACRO_EXPANSION: { 5471 bool isBuiltin = Record[0]; 5472 IdentifierInfo *Name = nullptr; 5473 MacroDefinitionRecord *Def = nullptr; 5474 if (isBuiltin) 5475 Name = getLocalIdentifier(M, Record[1]); 5476 else { 5477 PreprocessedEntityID GlobalID = 5478 getGlobalPreprocessedEntityID(M, Record[1]); 5479 Def = cast<MacroDefinitionRecord>( 5480 PPRec.getLoadedPreprocessedEntity(GlobalID - 1)); 5481 } 5482 5483 MacroExpansion *ME; 5484 if (isBuiltin) 5485 ME = new (PPRec) MacroExpansion(Name, Range); 5486 else 5487 ME = new (PPRec) MacroExpansion(Def, Range); 5488 5489 return ME; 5490 } 5491 5492 case PPD_MACRO_DEFINITION: { 5493 // Decode the identifier info and then check again; if the macro is 5494 // still defined and associated with the identifier, 5495 IdentifierInfo *II = getLocalIdentifier(M, Record[0]); 5496 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range); 5497 5498 if (DeserializationListener) 5499 DeserializationListener->MacroDefinitionRead(PPID, MD); 5500 5501 return MD; 5502 } 5503 5504 case PPD_INCLUSION_DIRECTIVE: { 5505 const char *FullFileNameStart = Blob.data() + Record[0]; 5506 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]); 5507 const FileEntry *File = nullptr; 5508 if (!FullFileName.empty()) 5509 File = PP.getFileManager().getFile(FullFileName); 5510 5511 // FIXME: Stable encoding 5512 InclusionDirective::InclusionKind Kind 5513 = static_cast<InclusionDirective::InclusionKind>(Record[2]); 5514 InclusionDirective *ID 5515 = new (PPRec) InclusionDirective(PPRec, Kind, 5516 StringRef(Blob.data(), Record[0]), 5517 Record[1], Record[3], 5518 File, 5519 Range); 5520 return ID; 5521 } 5522 } 5523 5524 llvm_unreachable("Invalid PreprocessorDetailRecordTypes"); 5525 } 5526 5527 /// Find the next module that contains entities and return the ID 5528 /// of the first entry. 5529 /// 5530 /// \param SLocMapI points at a chunk of a module that contains no 5531 /// preprocessed entities or the entities it contains are not the ones we are 5532 /// looking for. 5533 PreprocessedEntityID ASTReader::findNextPreprocessedEntity( 5534 GlobalSLocOffsetMapType::const_iterator SLocMapI) const { 5535 ++SLocMapI; 5536 for (GlobalSLocOffsetMapType::const_iterator 5537 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) { 5538 ModuleFile &M = *SLocMapI->second; 5539 if (M.NumPreprocessedEntities) 5540 return M.BasePreprocessedEntityID; 5541 } 5542 5543 return getTotalNumPreprocessedEntities(); 5544 } 5545 5546 namespace { 5547 5548 struct PPEntityComp { 5549 const ASTReader &Reader; 5550 ModuleFile &M; 5551 5552 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {} 5553 5554 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const { 5555 SourceLocation LHS = getLoc(L); 5556 SourceLocation RHS = getLoc(R); 5557 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 5558 } 5559 5560 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const { 5561 SourceLocation LHS = getLoc(L); 5562 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 5563 } 5564 5565 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const { 5566 SourceLocation RHS = getLoc(R); 5567 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 5568 } 5569 5570 SourceLocation getLoc(const PPEntityOffset &PPE) const { 5571 return Reader.TranslateSourceLocation(M, PPE.getBegin()); 5572 } 5573 }; 5574 5575 } // namespace 5576 5577 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc, 5578 bool EndsAfter) const { 5579 if (SourceMgr.isLocalSourceLocation(Loc)) 5580 return getTotalNumPreprocessedEntities(); 5581 5582 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find( 5583 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1); 5584 assert(SLocMapI != GlobalSLocOffsetMap.end() && 5585 "Corrupted global sloc offset map"); 5586 5587 if (SLocMapI->second->NumPreprocessedEntities == 0) 5588 return findNextPreprocessedEntity(SLocMapI); 5589 5590 ModuleFile &M = *SLocMapI->second; 5591 5592 using pp_iterator = const PPEntityOffset *; 5593 5594 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 5595 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 5596 5597 size_t Count = M.NumPreprocessedEntities; 5598 size_t Half; 5599 pp_iterator First = pp_begin; 5600 pp_iterator PPI; 5601 5602 if (EndsAfter) { 5603 PPI = std::upper_bound(pp_begin, pp_end, Loc, 5604 PPEntityComp(*this, M)); 5605 } else { 5606 // Do a binary search manually instead of using std::lower_bound because 5607 // The end locations of entities may be unordered (when a macro expansion 5608 // is inside another macro argument), but for this case it is not important 5609 // whether we get the first macro expansion or its containing macro. 5610 while (Count > 0) { 5611 Half = Count / 2; 5612 PPI = First; 5613 std::advance(PPI, Half); 5614 if (SourceMgr.isBeforeInTranslationUnit( 5615 TranslateSourceLocation(M, PPI->getEnd()), Loc)) { 5616 First = PPI; 5617 ++First; 5618 Count = Count - Half - 1; 5619 } else 5620 Count = Half; 5621 } 5622 } 5623 5624 if (PPI == pp_end) 5625 return findNextPreprocessedEntity(SLocMapI); 5626 5627 return M.BasePreprocessedEntityID + (PPI - pp_begin); 5628 } 5629 5630 /// Returns a pair of [Begin, End) indices of preallocated 5631 /// preprocessed entities that \arg Range encompasses. 5632 std::pair<unsigned, unsigned> 5633 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) { 5634 if (Range.isInvalid()) 5635 return std::make_pair(0,0); 5636 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); 5637 5638 PreprocessedEntityID BeginID = 5639 findPreprocessedEntity(Range.getBegin(), false); 5640 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true); 5641 return std::make_pair(BeginID, EndID); 5642 } 5643 5644 /// Optionally returns true or false if the preallocated preprocessed 5645 /// entity with index \arg Index came from file \arg FID. 5646 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index, 5647 FileID FID) { 5648 if (FID.isInvalid()) 5649 return false; 5650 5651 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 5652 ModuleFile &M = *PPInfo.first; 5653 unsigned LocalIndex = PPInfo.second; 5654 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 5655 5656 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin()); 5657 if (Loc.isInvalid()) 5658 return false; 5659 5660 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID)) 5661 return true; 5662 else 5663 return false; 5664 } 5665 5666 namespace { 5667 5668 /// Visitor used to search for information about a header file. 5669 class HeaderFileInfoVisitor { 5670 const FileEntry *FE; 5671 Optional<HeaderFileInfo> HFI; 5672 5673 public: 5674 explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {} 5675 5676 bool operator()(ModuleFile &M) { 5677 HeaderFileInfoLookupTable *Table 5678 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable); 5679 if (!Table) 5680 return false; 5681 5682 // Look in the on-disk hash table for an entry for this file name. 5683 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE); 5684 if (Pos == Table->end()) 5685 return false; 5686 5687 HFI = *Pos; 5688 return true; 5689 } 5690 5691 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; } 5692 }; 5693 5694 } // namespace 5695 5696 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) { 5697 HeaderFileInfoVisitor Visitor(FE); 5698 ModuleMgr.visit(Visitor); 5699 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) 5700 return *HFI; 5701 5702 return HeaderFileInfo(); 5703 } 5704 5705 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) { 5706 using DiagState = DiagnosticsEngine::DiagState; 5707 SmallVector<DiagState *, 32> DiagStates; 5708 5709 for (ModuleFile &F : ModuleMgr) { 5710 unsigned Idx = 0; 5711 auto &Record = F.PragmaDiagMappings; 5712 if (Record.empty()) 5713 continue; 5714 5715 DiagStates.clear(); 5716 5717 auto ReadDiagState = 5718 [&](const DiagState &BasedOn, SourceLocation Loc, 5719 bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * { 5720 unsigned BackrefID = Record[Idx++]; 5721 if (BackrefID != 0) 5722 return DiagStates[BackrefID - 1]; 5723 5724 // A new DiagState was created here. 5725 Diag.DiagStates.push_back(BasedOn); 5726 DiagState *NewState = &Diag.DiagStates.back(); 5727 DiagStates.push_back(NewState); 5728 unsigned Size = Record[Idx++]; 5729 assert(Idx + Size * 2 <= Record.size() && 5730 "Invalid data, not enough diag/map pairs"); 5731 while (Size--) { 5732 unsigned DiagID = Record[Idx++]; 5733 DiagnosticMapping NewMapping = 5734 DiagnosticMapping::deserialize(Record[Idx++]); 5735 if (!NewMapping.isPragma() && !IncludeNonPragmaStates) 5736 continue; 5737 5738 DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID); 5739 5740 // If this mapping was specified as a warning but the severity was 5741 // upgraded due to diagnostic settings, simulate the current diagnostic 5742 // settings (and use a warning). 5743 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) { 5744 NewMapping.setSeverity(diag::Severity::Warning); 5745 NewMapping.setUpgradedFromWarning(false); 5746 } 5747 5748 Mapping = NewMapping; 5749 } 5750 return NewState; 5751 }; 5752 5753 // Read the first state. 5754 DiagState *FirstState; 5755 if (F.Kind == MK_ImplicitModule) { 5756 // Implicitly-built modules are reused with different diagnostic 5757 // settings. Use the initial diagnostic state from Diag to simulate this 5758 // compilation's diagnostic settings. 5759 FirstState = Diag.DiagStatesByLoc.FirstDiagState; 5760 DiagStates.push_back(FirstState); 5761 5762 // Skip the initial diagnostic state from the serialized module. 5763 assert(Record[1] == 0 && 5764 "Invalid data, unexpected backref in initial state"); 5765 Idx = 3 + Record[2] * 2; 5766 assert(Idx < Record.size() && 5767 "Invalid data, not enough state change pairs in initial state"); 5768 } else if (F.isModule()) { 5769 // For an explicit module, preserve the flags from the module build 5770 // command line (-w, -Weverything, -Werror, ...) along with any explicit 5771 // -Wblah flags. 5772 unsigned Flags = Record[Idx++]; 5773 DiagState Initial; 5774 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1; 5775 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1; 5776 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1; 5777 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1; 5778 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1; 5779 Initial.ExtBehavior = (diag::Severity)Flags; 5780 FirstState = ReadDiagState(Initial, SourceLocation(), true); 5781 5782 assert(F.OriginalSourceFileID.isValid()); 5783 5784 // Set up the root buffer of the module to start with the initial 5785 // diagnostic state of the module itself, to cover files that contain no 5786 // explicit transitions (for which we did not serialize anything). 5787 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID] 5788 .StateTransitions.push_back({FirstState, 0}); 5789 } else { 5790 // For prefix ASTs, start with whatever the user configured on the 5791 // command line. 5792 Idx++; // Skip flags. 5793 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, 5794 SourceLocation(), false); 5795 } 5796 5797 // Read the state transitions. 5798 unsigned NumLocations = Record[Idx++]; 5799 while (NumLocations--) { 5800 assert(Idx < Record.size() && 5801 "Invalid data, missing pragma diagnostic states"); 5802 SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]); 5803 auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc); 5804 assert(IDAndOffset.first.isValid() && "invalid FileID for transition"); 5805 assert(IDAndOffset.second == 0 && "not a start location for a FileID"); 5806 unsigned Transitions = Record[Idx++]; 5807 5808 // Note that we don't need to set up Parent/ParentOffset here, because 5809 // we won't be changing the diagnostic state within imported FileIDs 5810 // (other than perhaps appending to the main source file, which has no 5811 // parent). 5812 auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first]; 5813 F.StateTransitions.reserve(F.StateTransitions.size() + Transitions); 5814 for (unsigned I = 0; I != Transitions; ++I) { 5815 unsigned Offset = Record[Idx++]; 5816 auto *State = 5817 ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false); 5818 F.StateTransitions.push_back({State, Offset}); 5819 } 5820 } 5821 5822 // Read the final state. 5823 assert(Idx < Record.size() && 5824 "Invalid data, missing final pragma diagnostic state"); 5825 SourceLocation CurStateLoc = 5826 ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]); 5827 auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false); 5828 5829 if (!F.isModule()) { 5830 Diag.DiagStatesByLoc.CurDiagState = CurState; 5831 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc; 5832 5833 // Preserve the property that the imaginary root file describes the 5834 // current state. 5835 FileID NullFile; 5836 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions; 5837 if (T.empty()) 5838 T.push_back({CurState, 0}); 5839 else 5840 T[0].State = CurState; 5841 } 5842 5843 // Don't try to read these mappings again. 5844 Record.clear(); 5845 } 5846 } 5847 5848 /// Get the correct cursor and offset for loading a type. 5849 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) { 5850 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index); 5851 assert(I != GlobalTypeMap.end() && "Corrupted global type map"); 5852 ModuleFile *M = I->second; 5853 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]); 5854 } 5855 5856 /// Read and return the type with the given index.. 5857 /// 5858 /// The index is the type ID, shifted and minus the number of predefs. This 5859 /// routine actually reads the record corresponding to the type at the given 5860 /// location. It is a helper routine for GetType, which deals with reading type 5861 /// IDs. 5862 QualType ASTReader::readTypeRecord(unsigned Index) { 5863 assert(ContextObj && "reading type with no AST context"); 5864 ASTContext &Context = *ContextObj; 5865 RecordLocation Loc = TypeCursorForIndex(Index); 5866 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 5867 5868 // Keep track of where we are in the stream, then jump back there 5869 // after reading this type. 5870 SavedStreamPosition SavedPosition(DeclsCursor); 5871 5872 ReadingKindTracker ReadingKind(Read_Type, *this); 5873 5874 // Note that we are loading a type record. 5875 Deserializing AType(this); 5876 5877 unsigned Idx = 0; 5878 DeclsCursor.JumpToBit(Loc.Offset); 5879 RecordData Record; 5880 unsigned Code = DeclsCursor.ReadCode(); 5881 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) { 5882 case TYPE_EXT_QUAL: { 5883 if (Record.size() != 2) { 5884 Error("Incorrect encoding of extended qualifier type"); 5885 return QualType(); 5886 } 5887 QualType Base = readType(*Loc.F, Record, Idx); 5888 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]); 5889 return Context.getQualifiedType(Base, Quals); 5890 } 5891 5892 case TYPE_COMPLEX: { 5893 if (Record.size() != 1) { 5894 Error("Incorrect encoding of complex type"); 5895 return QualType(); 5896 } 5897 QualType ElemType = readType(*Loc.F, Record, Idx); 5898 return Context.getComplexType(ElemType); 5899 } 5900 5901 case TYPE_POINTER: { 5902 if (Record.size() != 1) { 5903 Error("Incorrect encoding of pointer type"); 5904 return QualType(); 5905 } 5906 QualType PointeeType = readType(*Loc.F, Record, Idx); 5907 return Context.getPointerType(PointeeType); 5908 } 5909 5910 case TYPE_DECAYED: { 5911 if (Record.size() != 1) { 5912 Error("Incorrect encoding of decayed type"); 5913 return QualType(); 5914 } 5915 QualType OriginalType = readType(*Loc.F, Record, Idx); 5916 QualType DT = Context.getAdjustedParameterType(OriginalType); 5917 if (!isa<DecayedType>(DT)) 5918 Error("Decayed type does not decay"); 5919 return DT; 5920 } 5921 5922 case TYPE_ADJUSTED: { 5923 if (Record.size() != 2) { 5924 Error("Incorrect encoding of adjusted type"); 5925 return QualType(); 5926 } 5927 QualType OriginalTy = readType(*Loc.F, Record, Idx); 5928 QualType AdjustedTy = readType(*Loc.F, Record, Idx); 5929 return Context.getAdjustedType(OriginalTy, AdjustedTy); 5930 } 5931 5932 case TYPE_BLOCK_POINTER: { 5933 if (Record.size() != 1) { 5934 Error("Incorrect encoding of block pointer type"); 5935 return QualType(); 5936 } 5937 QualType PointeeType = readType(*Loc.F, Record, Idx); 5938 return Context.getBlockPointerType(PointeeType); 5939 } 5940 5941 case TYPE_LVALUE_REFERENCE: { 5942 if (Record.size() != 2) { 5943 Error("Incorrect encoding of lvalue reference type"); 5944 return QualType(); 5945 } 5946 QualType PointeeType = readType(*Loc.F, Record, Idx); 5947 return Context.getLValueReferenceType(PointeeType, Record[1]); 5948 } 5949 5950 case TYPE_RVALUE_REFERENCE: { 5951 if (Record.size() != 1) { 5952 Error("Incorrect encoding of rvalue reference type"); 5953 return QualType(); 5954 } 5955 QualType PointeeType = readType(*Loc.F, Record, Idx); 5956 return Context.getRValueReferenceType(PointeeType); 5957 } 5958 5959 case TYPE_MEMBER_POINTER: { 5960 if (Record.size() != 2) { 5961 Error("Incorrect encoding of member pointer type"); 5962 return QualType(); 5963 } 5964 QualType PointeeType = readType(*Loc.F, Record, Idx); 5965 QualType ClassType = readType(*Loc.F, Record, Idx); 5966 if (PointeeType.isNull() || ClassType.isNull()) 5967 return QualType(); 5968 5969 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr()); 5970 } 5971 5972 case TYPE_CONSTANT_ARRAY: { 5973 QualType ElementType = readType(*Loc.F, Record, Idx); 5974 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 5975 unsigned IndexTypeQuals = Record[2]; 5976 unsigned Idx = 3; 5977 llvm::APInt Size = ReadAPInt(Record, Idx); 5978 return Context.getConstantArrayType(ElementType, Size, 5979 ASM, IndexTypeQuals); 5980 } 5981 5982 case TYPE_INCOMPLETE_ARRAY: { 5983 QualType ElementType = readType(*Loc.F, Record, Idx); 5984 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 5985 unsigned IndexTypeQuals = Record[2]; 5986 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals); 5987 } 5988 5989 case TYPE_VARIABLE_ARRAY: { 5990 QualType ElementType = readType(*Loc.F, Record, Idx); 5991 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 5992 unsigned IndexTypeQuals = Record[2]; 5993 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]); 5994 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]); 5995 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F), 5996 ASM, IndexTypeQuals, 5997 SourceRange(LBLoc, RBLoc)); 5998 } 5999 6000 case TYPE_VECTOR: { 6001 if (Record.size() != 3) { 6002 Error("incorrect encoding of vector type in AST file"); 6003 return QualType(); 6004 } 6005 6006 QualType ElementType = readType(*Loc.F, Record, Idx); 6007 unsigned NumElements = Record[1]; 6008 unsigned VecKind = Record[2]; 6009 return Context.getVectorType(ElementType, NumElements, 6010 (VectorType::VectorKind)VecKind); 6011 } 6012 6013 case TYPE_EXT_VECTOR: { 6014 if (Record.size() != 3) { 6015 Error("incorrect encoding of extended vector type in AST file"); 6016 return QualType(); 6017 } 6018 6019 QualType ElementType = readType(*Loc.F, Record, Idx); 6020 unsigned NumElements = Record[1]; 6021 return Context.getExtVectorType(ElementType, NumElements); 6022 } 6023 6024 case TYPE_FUNCTION_NO_PROTO: { 6025 if (Record.size() != 8) { 6026 Error("incorrect encoding of no-proto function type"); 6027 return QualType(); 6028 } 6029 QualType ResultType = readType(*Loc.F, Record, Idx); 6030 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3], 6031 (CallingConv)Record[4], Record[5], Record[6], 6032 Record[7]); 6033 return Context.getFunctionNoProtoType(ResultType, Info); 6034 } 6035 6036 case TYPE_FUNCTION_PROTO: { 6037 QualType ResultType = readType(*Loc.F, Record, Idx); 6038 6039 FunctionProtoType::ExtProtoInfo EPI; 6040 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1], 6041 /*hasregparm*/ Record[2], 6042 /*regparm*/ Record[3], 6043 static_cast<CallingConv>(Record[4]), 6044 /*produces*/ Record[5], 6045 /*nocallersavedregs*/ Record[6], 6046 /*nocfcheck*/ Record[7]); 6047 6048 unsigned Idx = 8; 6049 6050 EPI.Variadic = Record[Idx++]; 6051 EPI.HasTrailingReturn = Record[Idx++]; 6052 EPI.TypeQuals = Qualifiers::fromOpaqueValue(Record[Idx++]); 6053 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]); 6054 SmallVector<QualType, 8> ExceptionStorage; 6055 readExceptionSpec(*Loc.F, ExceptionStorage, EPI.ExceptionSpec, Record, Idx); 6056 6057 unsigned NumParams = Record[Idx++]; 6058 SmallVector<QualType, 16> ParamTypes; 6059 for (unsigned I = 0; I != NumParams; ++I) 6060 ParamTypes.push_back(readType(*Loc.F, Record, Idx)); 6061 6062 SmallVector<FunctionProtoType::ExtParameterInfo, 4> ExtParameterInfos; 6063 if (Idx != Record.size()) { 6064 for (unsigned I = 0; I != NumParams; ++I) 6065 ExtParameterInfos.push_back( 6066 FunctionProtoType::ExtParameterInfo 6067 ::getFromOpaqueValue(Record[Idx++])); 6068 EPI.ExtParameterInfos = ExtParameterInfos.data(); 6069 } 6070 6071 assert(Idx == Record.size()); 6072 6073 return Context.getFunctionType(ResultType, ParamTypes, EPI); 6074 } 6075 6076 case TYPE_UNRESOLVED_USING: { 6077 unsigned Idx = 0; 6078 return Context.getTypeDeclType( 6079 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx)); 6080 } 6081 6082 case TYPE_TYPEDEF: { 6083 if (Record.size() != 2) { 6084 Error("incorrect encoding of typedef type"); 6085 return QualType(); 6086 } 6087 unsigned Idx = 0; 6088 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx); 6089 QualType Canonical = readType(*Loc.F, Record, Idx); 6090 if (!Canonical.isNull()) 6091 Canonical = Context.getCanonicalType(Canonical); 6092 return Context.getTypedefType(Decl, Canonical); 6093 } 6094 6095 case TYPE_TYPEOF_EXPR: 6096 return Context.getTypeOfExprType(ReadExpr(*Loc.F)); 6097 6098 case TYPE_TYPEOF: { 6099 if (Record.size() != 1) { 6100 Error("incorrect encoding of typeof(type) in AST file"); 6101 return QualType(); 6102 } 6103 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 6104 return Context.getTypeOfType(UnderlyingType); 6105 } 6106 6107 case TYPE_DECLTYPE: { 6108 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 6109 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType); 6110 } 6111 6112 case TYPE_UNARY_TRANSFORM: { 6113 QualType BaseType = readType(*Loc.F, Record, Idx); 6114 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 6115 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2]; 6116 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind); 6117 } 6118 6119 case TYPE_AUTO: { 6120 QualType Deduced = readType(*Loc.F, Record, Idx); 6121 AutoTypeKeyword Keyword = (AutoTypeKeyword)Record[Idx++]; 6122 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false; 6123 return Context.getAutoType(Deduced, Keyword, IsDependent); 6124 } 6125 6126 case TYPE_DEDUCED_TEMPLATE_SPECIALIZATION: { 6127 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx); 6128 QualType Deduced = readType(*Loc.F, Record, Idx); 6129 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false; 6130 return Context.getDeducedTemplateSpecializationType(Name, Deduced, 6131 IsDependent); 6132 } 6133 6134 case TYPE_RECORD: { 6135 if (Record.size() != 2) { 6136 Error("incorrect encoding of record type"); 6137 return QualType(); 6138 } 6139 unsigned Idx = 0; 6140 bool IsDependent = Record[Idx++]; 6141 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx); 6142 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl()); 6143 QualType T = Context.getRecordType(RD); 6144 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 6145 return T; 6146 } 6147 6148 case TYPE_ENUM: { 6149 if (Record.size() != 2) { 6150 Error("incorrect encoding of enum type"); 6151 return QualType(); 6152 } 6153 unsigned Idx = 0; 6154 bool IsDependent = Record[Idx++]; 6155 QualType T 6156 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx)); 6157 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 6158 return T; 6159 } 6160 6161 case TYPE_ATTRIBUTED: { 6162 if (Record.size() != 3) { 6163 Error("incorrect encoding of attributed type"); 6164 return QualType(); 6165 } 6166 QualType modifiedType = readType(*Loc.F, Record, Idx); 6167 QualType equivalentType = readType(*Loc.F, Record, Idx); 6168 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]); 6169 return Context.getAttributedType(kind, modifiedType, equivalentType); 6170 } 6171 6172 case TYPE_PAREN: { 6173 if (Record.size() != 1) { 6174 Error("incorrect encoding of paren type"); 6175 return QualType(); 6176 } 6177 QualType InnerType = readType(*Loc.F, Record, Idx); 6178 return Context.getParenType(InnerType); 6179 } 6180 6181 case TYPE_PACK_EXPANSION: { 6182 if (Record.size() != 2) { 6183 Error("incorrect encoding of pack expansion type"); 6184 return QualType(); 6185 } 6186 QualType Pattern = readType(*Loc.F, Record, Idx); 6187 if (Pattern.isNull()) 6188 return QualType(); 6189 Optional<unsigned> NumExpansions; 6190 if (Record[1]) 6191 NumExpansions = Record[1] - 1; 6192 return Context.getPackExpansionType(Pattern, NumExpansions); 6193 } 6194 6195 case TYPE_ELABORATED: { 6196 unsigned Idx = 0; 6197 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 6198 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 6199 QualType NamedType = readType(*Loc.F, Record, Idx); 6200 TagDecl *OwnedTagDecl = ReadDeclAs<TagDecl>(*Loc.F, Record, Idx); 6201 return Context.getElaboratedType(Keyword, NNS, NamedType, OwnedTagDecl); 6202 } 6203 6204 case TYPE_OBJC_INTERFACE: { 6205 unsigned Idx = 0; 6206 ObjCInterfaceDecl *ItfD 6207 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx); 6208 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl()); 6209 } 6210 6211 case TYPE_OBJC_TYPE_PARAM: { 6212 unsigned Idx = 0; 6213 ObjCTypeParamDecl *Decl 6214 = ReadDeclAs<ObjCTypeParamDecl>(*Loc.F, Record, Idx); 6215 unsigned NumProtos = Record[Idx++]; 6216 SmallVector<ObjCProtocolDecl*, 4> Protos; 6217 for (unsigned I = 0; I != NumProtos; ++I) 6218 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx)); 6219 return Context.getObjCTypeParamType(Decl, Protos); 6220 } 6221 6222 case TYPE_OBJC_OBJECT: { 6223 unsigned Idx = 0; 6224 QualType Base = readType(*Loc.F, Record, Idx); 6225 unsigned NumTypeArgs = Record[Idx++]; 6226 SmallVector<QualType, 4> TypeArgs; 6227 for (unsigned I = 0; I != NumTypeArgs; ++I) 6228 TypeArgs.push_back(readType(*Loc.F, Record, Idx)); 6229 unsigned NumProtos = Record[Idx++]; 6230 SmallVector<ObjCProtocolDecl*, 4> Protos; 6231 for (unsigned I = 0; I != NumProtos; ++I) 6232 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx)); 6233 bool IsKindOf = Record[Idx++]; 6234 return Context.getObjCObjectType(Base, TypeArgs, Protos, IsKindOf); 6235 } 6236 6237 case TYPE_OBJC_OBJECT_POINTER: { 6238 unsigned Idx = 0; 6239 QualType Pointee = readType(*Loc.F, Record, Idx); 6240 return Context.getObjCObjectPointerType(Pointee); 6241 } 6242 6243 case TYPE_SUBST_TEMPLATE_TYPE_PARM: { 6244 unsigned Idx = 0; 6245 QualType Parm = readType(*Loc.F, Record, Idx); 6246 QualType Replacement = readType(*Loc.F, Record, Idx); 6247 return Context.getSubstTemplateTypeParmType( 6248 cast<TemplateTypeParmType>(Parm), 6249 Context.getCanonicalType(Replacement)); 6250 } 6251 6252 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: { 6253 unsigned Idx = 0; 6254 QualType Parm = readType(*Loc.F, Record, Idx); 6255 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx); 6256 return Context.getSubstTemplateTypeParmPackType( 6257 cast<TemplateTypeParmType>(Parm), 6258 ArgPack); 6259 } 6260 6261 case TYPE_INJECTED_CLASS_NAME: { 6262 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx); 6263 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable 6264 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable 6265 // for AST reading, too much interdependencies. 6266 const Type *T = nullptr; 6267 for (auto *DI = D; DI; DI = DI->getPreviousDecl()) { 6268 if (const Type *Existing = DI->getTypeForDecl()) { 6269 T = Existing; 6270 break; 6271 } 6272 } 6273 if (!T) { 6274 T = new (Context, TypeAlignment) InjectedClassNameType(D, TST); 6275 for (auto *DI = D; DI; DI = DI->getPreviousDecl()) 6276 DI->setTypeForDecl(T); 6277 } 6278 return QualType(T, 0); 6279 } 6280 6281 case TYPE_TEMPLATE_TYPE_PARM: { 6282 unsigned Idx = 0; 6283 unsigned Depth = Record[Idx++]; 6284 unsigned Index = Record[Idx++]; 6285 bool Pack = Record[Idx++]; 6286 TemplateTypeParmDecl *D 6287 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx); 6288 return Context.getTemplateTypeParmType(Depth, Index, Pack, D); 6289 } 6290 6291 case TYPE_DEPENDENT_NAME: { 6292 unsigned Idx = 0; 6293 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 6294 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 6295 const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx); 6296 QualType Canon = readType(*Loc.F, Record, Idx); 6297 if (!Canon.isNull()) 6298 Canon = Context.getCanonicalType(Canon); 6299 return Context.getDependentNameType(Keyword, NNS, Name, Canon); 6300 } 6301 6302 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: { 6303 unsigned Idx = 0; 6304 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 6305 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 6306 const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx); 6307 unsigned NumArgs = Record[Idx++]; 6308 SmallVector<TemplateArgument, 8> Args; 6309 Args.reserve(NumArgs); 6310 while (NumArgs--) 6311 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx)); 6312 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name, 6313 Args); 6314 } 6315 6316 case TYPE_DEPENDENT_SIZED_ARRAY: { 6317 unsigned Idx = 0; 6318 6319 // ArrayType 6320 QualType ElementType = readType(*Loc.F, Record, Idx); 6321 ArrayType::ArraySizeModifier ASM 6322 = (ArrayType::ArraySizeModifier)Record[Idx++]; 6323 unsigned IndexTypeQuals = Record[Idx++]; 6324 6325 // DependentSizedArrayType 6326 Expr *NumElts = ReadExpr(*Loc.F); 6327 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx); 6328 6329 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM, 6330 IndexTypeQuals, Brackets); 6331 } 6332 6333 case TYPE_TEMPLATE_SPECIALIZATION: { 6334 unsigned Idx = 0; 6335 bool IsDependent = Record[Idx++]; 6336 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx); 6337 SmallVector<TemplateArgument, 8> Args; 6338 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx); 6339 QualType Underlying = readType(*Loc.F, Record, Idx); 6340 QualType T; 6341 if (Underlying.isNull()) 6342 T = Context.getCanonicalTemplateSpecializationType(Name, Args); 6343 else 6344 T = Context.getTemplateSpecializationType(Name, Args, Underlying); 6345 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 6346 return T; 6347 } 6348 6349 case TYPE_ATOMIC: { 6350 if (Record.size() != 1) { 6351 Error("Incorrect encoding of atomic type"); 6352 return QualType(); 6353 } 6354 QualType ValueType = readType(*Loc.F, Record, Idx); 6355 return Context.getAtomicType(ValueType); 6356 } 6357 6358 case TYPE_PIPE: { 6359 if (Record.size() != 2) { 6360 Error("Incorrect encoding of pipe type"); 6361 return QualType(); 6362 } 6363 6364 // Reading the pipe element type. 6365 QualType ElementType = readType(*Loc.F, Record, Idx); 6366 unsigned ReadOnly = Record[1]; 6367 return Context.getPipeType(ElementType, ReadOnly); 6368 } 6369 6370 case TYPE_DEPENDENT_SIZED_VECTOR: { 6371 unsigned Idx = 0; 6372 QualType ElementType = readType(*Loc.F, Record, Idx); 6373 Expr *SizeExpr = ReadExpr(*Loc.F); 6374 SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx); 6375 unsigned VecKind = Record[Idx]; 6376 6377 return Context.getDependentVectorType(ElementType, SizeExpr, AttrLoc, 6378 (VectorType::VectorKind)VecKind); 6379 } 6380 6381 case TYPE_DEPENDENT_SIZED_EXT_VECTOR: { 6382 unsigned Idx = 0; 6383 6384 // DependentSizedExtVectorType 6385 QualType ElementType = readType(*Loc.F, Record, Idx); 6386 Expr *SizeExpr = ReadExpr(*Loc.F); 6387 SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx); 6388 6389 return Context.getDependentSizedExtVectorType(ElementType, SizeExpr, 6390 AttrLoc); 6391 } 6392 6393 case TYPE_DEPENDENT_ADDRESS_SPACE: { 6394 unsigned Idx = 0; 6395 6396 // DependentAddressSpaceType 6397 QualType PointeeType = readType(*Loc.F, Record, Idx); 6398 Expr *AddrSpaceExpr = ReadExpr(*Loc.F); 6399 SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx); 6400 6401 return Context.getDependentAddressSpaceType(PointeeType, AddrSpaceExpr, 6402 AttrLoc); 6403 } 6404 } 6405 llvm_unreachable("Invalid TypeCode!"); 6406 } 6407 6408 void ASTReader::readExceptionSpec(ModuleFile &ModuleFile, 6409 SmallVectorImpl<QualType> &Exceptions, 6410 FunctionProtoType::ExceptionSpecInfo &ESI, 6411 const RecordData &Record, unsigned &Idx) { 6412 ExceptionSpecificationType EST = 6413 static_cast<ExceptionSpecificationType>(Record[Idx++]); 6414 ESI.Type = EST; 6415 if (EST == EST_Dynamic) { 6416 for (unsigned I = 0, N = Record[Idx++]; I != N; ++I) 6417 Exceptions.push_back(readType(ModuleFile, Record, Idx)); 6418 ESI.Exceptions = Exceptions; 6419 } else if (isComputedNoexcept(EST)) { 6420 ESI.NoexceptExpr = ReadExpr(ModuleFile); 6421 } else if (EST == EST_Uninstantiated) { 6422 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx); 6423 ESI.SourceTemplate = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx); 6424 } else if (EST == EST_Unevaluated) { 6425 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx); 6426 } 6427 } 6428 6429 namespace clang { 6430 6431 class TypeLocReader : public TypeLocVisitor<TypeLocReader> { 6432 ModuleFile *F; 6433 ASTReader *Reader; 6434 const ASTReader::RecordData &Record; 6435 unsigned &Idx; 6436 6437 SourceLocation ReadSourceLocation() { 6438 return Reader->ReadSourceLocation(*F, Record, Idx); 6439 } 6440 6441 TypeSourceInfo *GetTypeSourceInfo() { 6442 return Reader->GetTypeSourceInfo(*F, Record, Idx); 6443 } 6444 6445 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() { 6446 return Reader->ReadNestedNameSpecifierLoc(*F, Record, Idx); 6447 } 6448 6449 Attr *ReadAttr() { 6450 return Reader->ReadAttr(*F, Record, Idx); 6451 } 6452 6453 public: 6454 TypeLocReader(ModuleFile &F, ASTReader &Reader, 6455 const ASTReader::RecordData &Record, unsigned &Idx) 6456 : F(&F), Reader(&Reader), Record(Record), Idx(Idx) {} 6457 6458 // We want compile-time assurance that we've enumerated all of 6459 // these, so unfortunately we have to declare them first, then 6460 // define them out-of-line. 6461 #define ABSTRACT_TYPELOC(CLASS, PARENT) 6462 #define TYPELOC(CLASS, PARENT) \ 6463 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 6464 #include "clang/AST/TypeLocNodes.def" 6465 6466 void VisitFunctionTypeLoc(FunctionTypeLoc); 6467 void VisitArrayTypeLoc(ArrayTypeLoc); 6468 }; 6469 6470 } // namespace clang 6471 6472 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 6473 // nothing to do 6474 } 6475 6476 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 6477 TL.setBuiltinLoc(ReadSourceLocation()); 6478 if (TL.needsExtraLocalData()) { 6479 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++])); 6480 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++])); 6481 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++])); 6482 TL.setModeAttr(Record[Idx++]); 6483 } 6484 } 6485 6486 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { 6487 TL.setNameLoc(ReadSourceLocation()); 6488 } 6489 6490 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { 6491 TL.setStarLoc(ReadSourceLocation()); 6492 } 6493 6494 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) { 6495 // nothing to do 6496 } 6497 6498 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { 6499 // nothing to do 6500 } 6501 6502 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 6503 TL.setCaretLoc(ReadSourceLocation()); 6504 } 6505 6506 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 6507 TL.setAmpLoc(ReadSourceLocation()); 6508 } 6509 6510 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 6511 TL.setAmpAmpLoc(ReadSourceLocation()); 6512 } 6513 6514 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 6515 TL.setStarLoc(ReadSourceLocation()); 6516 TL.setClassTInfo(GetTypeSourceInfo()); 6517 } 6518 6519 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { 6520 TL.setLBracketLoc(ReadSourceLocation()); 6521 TL.setRBracketLoc(ReadSourceLocation()); 6522 if (Record[Idx++]) 6523 TL.setSizeExpr(Reader->ReadExpr(*F)); 6524 else 6525 TL.setSizeExpr(nullptr); 6526 } 6527 6528 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 6529 VisitArrayTypeLoc(TL); 6530 } 6531 6532 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 6533 VisitArrayTypeLoc(TL); 6534 } 6535 6536 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 6537 VisitArrayTypeLoc(TL); 6538 } 6539 6540 void TypeLocReader::VisitDependentSizedArrayTypeLoc( 6541 DependentSizedArrayTypeLoc TL) { 6542 VisitArrayTypeLoc(TL); 6543 } 6544 6545 void TypeLocReader::VisitDependentAddressSpaceTypeLoc( 6546 DependentAddressSpaceTypeLoc TL) { 6547 6548 TL.setAttrNameLoc(ReadSourceLocation()); 6549 SourceRange range; 6550 range.setBegin(ReadSourceLocation()); 6551 range.setEnd(ReadSourceLocation()); 6552 TL.setAttrOperandParensRange(range); 6553 TL.setAttrExprOperand(Reader->ReadExpr(*F)); 6554 } 6555 6556 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( 6557 DependentSizedExtVectorTypeLoc TL) { 6558 TL.setNameLoc(ReadSourceLocation()); 6559 } 6560 6561 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { 6562 TL.setNameLoc(ReadSourceLocation()); 6563 } 6564 6565 void TypeLocReader::VisitDependentVectorTypeLoc( 6566 DependentVectorTypeLoc TL) { 6567 TL.setNameLoc(ReadSourceLocation()); 6568 } 6569 6570 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 6571 TL.setNameLoc(ReadSourceLocation()); 6572 } 6573 6574 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 6575 TL.setLocalRangeBegin(ReadSourceLocation()); 6576 TL.setLParenLoc(ReadSourceLocation()); 6577 TL.setRParenLoc(ReadSourceLocation()); 6578 TL.setExceptionSpecRange(SourceRange(Reader->ReadSourceLocation(*F, Record, Idx), 6579 Reader->ReadSourceLocation(*F, Record, Idx))); 6580 TL.setLocalRangeEnd(ReadSourceLocation()); 6581 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) { 6582 TL.setParam(i, Reader->ReadDeclAs<ParmVarDecl>(*F, Record, Idx)); 6583 } 6584 } 6585 6586 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 6587 VisitFunctionTypeLoc(TL); 6588 } 6589 6590 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 6591 VisitFunctionTypeLoc(TL); 6592 } 6593 6594 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 6595 TL.setNameLoc(ReadSourceLocation()); 6596 } 6597 6598 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 6599 TL.setNameLoc(ReadSourceLocation()); 6600 } 6601 6602 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 6603 TL.setTypeofLoc(ReadSourceLocation()); 6604 TL.setLParenLoc(ReadSourceLocation()); 6605 TL.setRParenLoc(ReadSourceLocation()); 6606 } 6607 6608 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 6609 TL.setTypeofLoc(ReadSourceLocation()); 6610 TL.setLParenLoc(ReadSourceLocation()); 6611 TL.setRParenLoc(ReadSourceLocation()); 6612 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6613 } 6614 6615 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 6616 TL.setNameLoc(ReadSourceLocation()); 6617 } 6618 6619 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 6620 TL.setKWLoc(ReadSourceLocation()); 6621 TL.setLParenLoc(ReadSourceLocation()); 6622 TL.setRParenLoc(ReadSourceLocation()); 6623 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6624 } 6625 6626 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) { 6627 TL.setNameLoc(ReadSourceLocation()); 6628 } 6629 6630 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc( 6631 DeducedTemplateSpecializationTypeLoc TL) { 6632 TL.setTemplateNameLoc(ReadSourceLocation()); 6633 } 6634 6635 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { 6636 TL.setNameLoc(ReadSourceLocation()); 6637 } 6638 6639 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { 6640 TL.setNameLoc(ReadSourceLocation()); 6641 } 6642 6643 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 6644 TL.setAttr(ReadAttr()); 6645 } 6646 6647 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 6648 TL.setNameLoc(ReadSourceLocation()); 6649 } 6650 6651 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( 6652 SubstTemplateTypeParmTypeLoc TL) { 6653 TL.setNameLoc(ReadSourceLocation()); 6654 } 6655 6656 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc( 6657 SubstTemplateTypeParmPackTypeLoc TL) { 6658 TL.setNameLoc(ReadSourceLocation()); 6659 } 6660 6661 void TypeLocReader::VisitTemplateSpecializationTypeLoc( 6662 TemplateSpecializationTypeLoc TL) { 6663 TL.setTemplateKeywordLoc(ReadSourceLocation()); 6664 TL.setTemplateNameLoc(ReadSourceLocation()); 6665 TL.setLAngleLoc(ReadSourceLocation()); 6666 TL.setRAngleLoc(ReadSourceLocation()); 6667 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6668 TL.setArgLocInfo( 6669 i, 6670 Reader->GetTemplateArgumentLocInfo( 6671 *F, TL.getTypePtr()->getArg(i).getKind(), Record, Idx)); 6672 } 6673 6674 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) { 6675 TL.setLParenLoc(ReadSourceLocation()); 6676 TL.setRParenLoc(ReadSourceLocation()); 6677 } 6678 6679 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 6680 TL.setElaboratedKeywordLoc(ReadSourceLocation()); 6681 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6682 } 6683 6684 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 6685 TL.setNameLoc(ReadSourceLocation()); 6686 } 6687 6688 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 6689 TL.setElaboratedKeywordLoc(ReadSourceLocation()); 6690 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6691 TL.setNameLoc(ReadSourceLocation()); 6692 } 6693 6694 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( 6695 DependentTemplateSpecializationTypeLoc TL) { 6696 TL.setElaboratedKeywordLoc(ReadSourceLocation()); 6697 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6698 TL.setTemplateKeywordLoc(ReadSourceLocation()); 6699 TL.setTemplateNameLoc(ReadSourceLocation()); 6700 TL.setLAngleLoc(ReadSourceLocation()); 6701 TL.setRAngleLoc(ReadSourceLocation()); 6702 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 6703 TL.setArgLocInfo( 6704 I, 6705 Reader->GetTemplateArgumentLocInfo( 6706 *F, TL.getTypePtr()->getArg(I).getKind(), Record, Idx)); 6707 } 6708 6709 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 6710 TL.setEllipsisLoc(ReadSourceLocation()); 6711 } 6712 6713 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 6714 TL.setNameLoc(ReadSourceLocation()); 6715 } 6716 6717 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) { 6718 if (TL.getNumProtocols()) { 6719 TL.setProtocolLAngleLoc(ReadSourceLocation()); 6720 TL.setProtocolRAngleLoc(ReadSourceLocation()); 6721 } 6722 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6723 TL.setProtocolLoc(i, ReadSourceLocation()); 6724 } 6725 6726 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 6727 TL.setHasBaseTypeAsWritten(Record[Idx++]); 6728 TL.setTypeArgsLAngleLoc(ReadSourceLocation()); 6729 TL.setTypeArgsRAngleLoc(ReadSourceLocation()); 6730 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i) 6731 TL.setTypeArgTInfo(i, GetTypeSourceInfo()); 6732 TL.setProtocolLAngleLoc(ReadSourceLocation()); 6733 TL.setProtocolRAngleLoc(ReadSourceLocation()); 6734 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6735 TL.setProtocolLoc(i, ReadSourceLocation()); 6736 } 6737 6738 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 6739 TL.setStarLoc(ReadSourceLocation()); 6740 } 6741 6742 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) { 6743 TL.setKWLoc(ReadSourceLocation()); 6744 TL.setLParenLoc(ReadSourceLocation()); 6745 TL.setRParenLoc(ReadSourceLocation()); 6746 } 6747 6748 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) { 6749 TL.setKWLoc(ReadSourceLocation()); 6750 } 6751 6752 void ASTReader::ReadTypeLoc(ModuleFile &F, const ASTReader::RecordData &Record, 6753 unsigned &Idx, TypeLoc TL) { 6754 TypeLocReader TLR(F, *this, Record, Idx); 6755 for (; !TL.isNull(); TL = TL.getNextTypeLoc()) 6756 TLR.Visit(TL); 6757 } 6758 6759 TypeSourceInfo * 6760 ASTReader::GetTypeSourceInfo(ModuleFile &F, const ASTReader::RecordData &Record, 6761 unsigned &Idx) { 6762 QualType InfoTy = readType(F, Record, Idx); 6763 if (InfoTy.isNull()) 6764 return nullptr; 6765 6766 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy); 6767 ReadTypeLoc(F, Record, Idx, TInfo->getTypeLoc()); 6768 return TInfo; 6769 } 6770 6771 QualType ASTReader::GetType(TypeID ID) { 6772 assert(ContextObj && "reading type with no AST context"); 6773 ASTContext &Context = *ContextObj; 6774 6775 unsigned FastQuals = ID & Qualifiers::FastMask; 6776 unsigned Index = ID >> Qualifiers::FastWidth; 6777 6778 if (Index < NUM_PREDEF_TYPE_IDS) { 6779 QualType T; 6780 switch ((PredefinedTypeIDs)Index) { 6781 case PREDEF_TYPE_NULL_ID: 6782 return QualType(); 6783 case PREDEF_TYPE_VOID_ID: 6784 T = Context.VoidTy; 6785 break; 6786 case PREDEF_TYPE_BOOL_ID: 6787 T = Context.BoolTy; 6788 break; 6789 case PREDEF_TYPE_CHAR_U_ID: 6790 case PREDEF_TYPE_CHAR_S_ID: 6791 // FIXME: Check that the signedness of CharTy is correct! 6792 T = Context.CharTy; 6793 break; 6794 case PREDEF_TYPE_UCHAR_ID: 6795 T = Context.UnsignedCharTy; 6796 break; 6797 case PREDEF_TYPE_USHORT_ID: 6798 T = Context.UnsignedShortTy; 6799 break; 6800 case PREDEF_TYPE_UINT_ID: 6801 T = Context.UnsignedIntTy; 6802 break; 6803 case PREDEF_TYPE_ULONG_ID: 6804 T = Context.UnsignedLongTy; 6805 break; 6806 case PREDEF_TYPE_ULONGLONG_ID: 6807 T = Context.UnsignedLongLongTy; 6808 break; 6809 case PREDEF_TYPE_UINT128_ID: 6810 T = Context.UnsignedInt128Ty; 6811 break; 6812 case PREDEF_TYPE_SCHAR_ID: 6813 T = Context.SignedCharTy; 6814 break; 6815 case PREDEF_TYPE_WCHAR_ID: 6816 T = Context.WCharTy; 6817 break; 6818 case PREDEF_TYPE_SHORT_ID: 6819 T = Context.ShortTy; 6820 break; 6821 case PREDEF_TYPE_INT_ID: 6822 T = Context.IntTy; 6823 break; 6824 case PREDEF_TYPE_LONG_ID: 6825 T = Context.LongTy; 6826 break; 6827 case PREDEF_TYPE_LONGLONG_ID: 6828 T = Context.LongLongTy; 6829 break; 6830 case PREDEF_TYPE_INT128_ID: 6831 T = Context.Int128Ty; 6832 break; 6833 case PREDEF_TYPE_HALF_ID: 6834 T = Context.HalfTy; 6835 break; 6836 case PREDEF_TYPE_FLOAT_ID: 6837 T = Context.FloatTy; 6838 break; 6839 case PREDEF_TYPE_DOUBLE_ID: 6840 T = Context.DoubleTy; 6841 break; 6842 case PREDEF_TYPE_LONGDOUBLE_ID: 6843 T = Context.LongDoubleTy; 6844 break; 6845 case PREDEF_TYPE_SHORT_ACCUM_ID: 6846 T = Context.ShortAccumTy; 6847 break; 6848 case PREDEF_TYPE_ACCUM_ID: 6849 T = Context.AccumTy; 6850 break; 6851 case PREDEF_TYPE_LONG_ACCUM_ID: 6852 T = Context.LongAccumTy; 6853 break; 6854 case PREDEF_TYPE_USHORT_ACCUM_ID: 6855 T = Context.UnsignedShortAccumTy; 6856 break; 6857 case PREDEF_TYPE_UACCUM_ID: 6858 T = Context.UnsignedAccumTy; 6859 break; 6860 case PREDEF_TYPE_ULONG_ACCUM_ID: 6861 T = Context.UnsignedLongAccumTy; 6862 break; 6863 case PREDEF_TYPE_SHORT_FRACT_ID: 6864 T = Context.ShortFractTy; 6865 break; 6866 case PREDEF_TYPE_FRACT_ID: 6867 T = Context.FractTy; 6868 break; 6869 case PREDEF_TYPE_LONG_FRACT_ID: 6870 T = Context.LongFractTy; 6871 break; 6872 case PREDEF_TYPE_USHORT_FRACT_ID: 6873 T = Context.UnsignedShortFractTy; 6874 break; 6875 case PREDEF_TYPE_UFRACT_ID: 6876 T = Context.UnsignedFractTy; 6877 break; 6878 case PREDEF_TYPE_ULONG_FRACT_ID: 6879 T = Context.UnsignedLongFractTy; 6880 break; 6881 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID: 6882 T = Context.SatShortAccumTy; 6883 break; 6884 case PREDEF_TYPE_SAT_ACCUM_ID: 6885 T = Context.SatAccumTy; 6886 break; 6887 case PREDEF_TYPE_SAT_LONG_ACCUM_ID: 6888 T = Context.SatLongAccumTy; 6889 break; 6890 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID: 6891 T = Context.SatUnsignedShortAccumTy; 6892 break; 6893 case PREDEF_TYPE_SAT_UACCUM_ID: 6894 T = Context.SatUnsignedAccumTy; 6895 break; 6896 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID: 6897 T = Context.SatUnsignedLongAccumTy; 6898 break; 6899 case PREDEF_TYPE_SAT_SHORT_FRACT_ID: 6900 T = Context.SatShortFractTy; 6901 break; 6902 case PREDEF_TYPE_SAT_FRACT_ID: 6903 T = Context.SatFractTy; 6904 break; 6905 case PREDEF_TYPE_SAT_LONG_FRACT_ID: 6906 T = Context.SatLongFractTy; 6907 break; 6908 case PREDEF_TYPE_SAT_USHORT_FRACT_ID: 6909 T = Context.SatUnsignedShortFractTy; 6910 break; 6911 case PREDEF_TYPE_SAT_UFRACT_ID: 6912 T = Context.SatUnsignedFractTy; 6913 break; 6914 case PREDEF_TYPE_SAT_ULONG_FRACT_ID: 6915 T = Context.SatUnsignedLongFractTy; 6916 break; 6917 case PREDEF_TYPE_FLOAT16_ID: 6918 T = Context.Float16Ty; 6919 break; 6920 case PREDEF_TYPE_FLOAT128_ID: 6921 T = Context.Float128Ty; 6922 break; 6923 case PREDEF_TYPE_OVERLOAD_ID: 6924 T = Context.OverloadTy; 6925 break; 6926 case PREDEF_TYPE_BOUND_MEMBER: 6927 T = Context.BoundMemberTy; 6928 break; 6929 case PREDEF_TYPE_PSEUDO_OBJECT: 6930 T = Context.PseudoObjectTy; 6931 break; 6932 case PREDEF_TYPE_DEPENDENT_ID: 6933 T = Context.DependentTy; 6934 break; 6935 case PREDEF_TYPE_UNKNOWN_ANY: 6936 T = Context.UnknownAnyTy; 6937 break; 6938 case PREDEF_TYPE_NULLPTR_ID: 6939 T = Context.NullPtrTy; 6940 break; 6941 case PREDEF_TYPE_CHAR8_ID: 6942 T = Context.Char8Ty; 6943 break; 6944 case PREDEF_TYPE_CHAR16_ID: 6945 T = Context.Char16Ty; 6946 break; 6947 case PREDEF_TYPE_CHAR32_ID: 6948 T = Context.Char32Ty; 6949 break; 6950 case PREDEF_TYPE_OBJC_ID: 6951 T = Context.ObjCBuiltinIdTy; 6952 break; 6953 case PREDEF_TYPE_OBJC_CLASS: 6954 T = Context.ObjCBuiltinClassTy; 6955 break; 6956 case PREDEF_TYPE_OBJC_SEL: 6957 T = Context.ObjCBuiltinSelTy; 6958 break; 6959 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 6960 case PREDEF_TYPE_##Id##_ID: \ 6961 T = Context.SingletonId; \ 6962 break; 6963 #include "clang/Basic/OpenCLImageTypes.def" 6964 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ 6965 case PREDEF_TYPE_##Id##_ID: \ 6966 T = Context.Id##Ty; \ 6967 break; 6968 #include "clang/Basic/OpenCLExtensionTypes.def" 6969 case PREDEF_TYPE_SAMPLER_ID: 6970 T = Context.OCLSamplerTy; 6971 break; 6972 case PREDEF_TYPE_EVENT_ID: 6973 T = Context.OCLEventTy; 6974 break; 6975 case PREDEF_TYPE_CLK_EVENT_ID: 6976 T = Context.OCLClkEventTy; 6977 break; 6978 case PREDEF_TYPE_QUEUE_ID: 6979 T = Context.OCLQueueTy; 6980 break; 6981 case PREDEF_TYPE_RESERVE_ID_ID: 6982 T = Context.OCLReserveIDTy; 6983 break; 6984 case PREDEF_TYPE_AUTO_DEDUCT: 6985 T = Context.getAutoDeductType(); 6986 break; 6987 case PREDEF_TYPE_AUTO_RREF_DEDUCT: 6988 T = Context.getAutoRRefDeductType(); 6989 break; 6990 case PREDEF_TYPE_ARC_UNBRIDGED_CAST: 6991 T = Context.ARCUnbridgedCastTy; 6992 break; 6993 case PREDEF_TYPE_BUILTIN_FN: 6994 T = Context.BuiltinFnTy; 6995 break; 6996 case PREDEF_TYPE_OMP_ARRAY_SECTION: 6997 T = Context.OMPArraySectionTy; 6998 break; 6999 } 7000 7001 assert(!T.isNull() && "Unknown predefined type"); 7002 return T.withFastQualifiers(FastQuals); 7003 } 7004 7005 Index -= NUM_PREDEF_TYPE_IDS; 7006 assert(Index < TypesLoaded.size() && "Type index out-of-range"); 7007 if (TypesLoaded[Index].isNull()) { 7008 TypesLoaded[Index] = readTypeRecord(Index); 7009 if (TypesLoaded[Index].isNull()) 7010 return QualType(); 7011 7012 TypesLoaded[Index]->setFromAST(); 7013 if (DeserializationListener) 7014 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID), 7015 TypesLoaded[Index]); 7016 } 7017 7018 return TypesLoaded[Index].withFastQualifiers(FastQuals); 7019 } 7020 7021 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) { 7022 return GetType(getGlobalTypeID(F, LocalID)); 7023 } 7024 7025 serialization::TypeID 7026 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const { 7027 unsigned FastQuals = LocalID & Qualifiers::FastMask; 7028 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth; 7029 7030 if (LocalIndex < NUM_PREDEF_TYPE_IDS) 7031 return LocalID; 7032 7033 if (!F.ModuleOffsetMap.empty()) 7034 ReadModuleOffsetMap(F); 7035 7036 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7037 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS); 7038 assert(I != F.TypeRemap.end() && "Invalid index into type index remap"); 7039 7040 unsigned GlobalIndex = LocalIndex + I->second; 7041 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals; 7042 } 7043 7044 TemplateArgumentLocInfo 7045 ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F, 7046 TemplateArgument::ArgKind Kind, 7047 const RecordData &Record, 7048 unsigned &Index) { 7049 switch (Kind) { 7050 case TemplateArgument::Expression: 7051 return ReadExpr(F); 7052 case TemplateArgument::Type: 7053 return GetTypeSourceInfo(F, Record, Index); 7054 case TemplateArgument::Template: { 7055 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, 7056 Index); 7057 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index); 7058 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 7059 SourceLocation()); 7060 } 7061 case TemplateArgument::TemplateExpansion: { 7062 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, 7063 Index); 7064 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index); 7065 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index); 7066 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 7067 EllipsisLoc); 7068 } 7069 case TemplateArgument::Null: 7070 case TemplateArgument::Integral: 7071 case TemplateArgument::Declaration: 7072 case TemplateArgument::NullPtr: 7073 case TemplateArgument::Pack: 7074 // FIXME: Is this right? 7075 return TemplateArgumentLocInfo(); 7076 } 7077 llvm_unreachable("unexpected template argument loc"); 7078 } 7079 7080 TemplateArgumentLoc 7081 ASTReader::ReadTemplateArgumentLoc(ModuleFile &F, 7082 const RecordData &Record, unsigned &Index) { 7083 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index); 7084 7085 if (Arg.getKind() == TemplateArgument::Expression) { 7086 if (Record[Index++]) // bool InfoHasSameExpr. 7087 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); 7088 } 7089 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(), 7090 Record, Index)); 7091 } 7092 7093 const ASTTemplateArgumentListInfo* 7094 ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F, 7095 const RecordData &Record, 7096 unsigned &Index) { 7097 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index); 7098 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index); 7099 unsigned NumArgsAsWritten = Record[Index++]; 7100 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc); 7101 for (unsigned i = 0; i != NumArgsAsWritten; ++i) 7102 TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index)); 7103 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo); 7104 } 7105 7106 Decl *ASTReader::GetExternalDecl(uint32_t ID) { 7107 return GetDecl(ID); 7108 } 7109 7110 void ASTReader::CompleteRedeclChain(const Decl *D) { 7111 if (NumCurrentElementsDeserializing) { 7112 // We arrange to not care about the complete redeclaration chain while we're 7113 // deserializing. Just remember that the AST has marked this one as complete 7114 // but that it's not actually complete yet, so we know we still need to 7115 // complete it later. 7116 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D)); 7117 return; 7118 } 7119 7120 const DeclContext *DC = D->getDeclContext()->getRedeclContext(); 7121 7122 // If this is a named declaration, complete it by looking it up 7123 // within its context. 7124 // 7125 // FIXME: Merging a function definition should merge 7126 // all mergeable entities within it. 7127 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) || 7128 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) { 7129 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) { 7130 if (!getContext().getLangOpts().CPlusPlus && 7131 isa<TranslationUnitDecl>(DC)) { 7132 // Outside of C++, we don't have a lookup table for the TU, so update 7133 // the identifier instead. (For C++ modules, we don't store decls 7134 // in the serialized identifier table, so we do the lookup in the TU.) 7135 auto *II = Name.getAsIdentifierInfo(); 7136 assert(II && "non-identifier name in C?"); 7137 if (II->isOutOfDate()) 7138 updateOutOfDateIdentifier(*II); 7139 } else 7140 DC->lookup(Name); 7141 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) { 7142 // Find all declarations of this kind from the relevant context. 7143 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) { 7144 auto *DC = cast<DeclContext>(DCDecl); 7145 SmallVector<Decl*, 8> Decls; 7146 FindExternalLexicalDecls( 7147 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls); 7148 } 7149 } 7150 } 7151 7152 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) 7153 CTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7154 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) 7155 VTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7156 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 7157 if (auto *Template = FD->getPrimaryTemplate()) 7158 Template->LoadLazySpecializations(); 7159 } 7160 } 7161 7162 CXXCtorInitializer ** 7163 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) { 7164 RecordLocation Loc = getLocalBitOffset(Offset); 7165 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7166 SavedStreamPosition SavedPosition(Cursor); 7167 Cursor.JumpToBit(Loc.Offset); 7168 ReadingKindTracker ReadingKind(Read_Decl, *this); 7169 7170 RecordData Record; 7171 unsigned Code = Cursor.ReadCode(); 7172 unsigned RecCode = Cursor.readRecord(Code, Record); 7173 if (RecCode != DECL_CXX_CTOR_INITIALIZERS) { 7174 Error("malformed AST file: missing C++ ctor initializers"); 7175 return nullptr; 7176 } 7177 7178 unsigned Idx = 0; 7179 return ReadCXXCtorInitializers(*Loc.F, Record, Idx); 7180 } 7181 7182 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) { 7183 assert(ContextObj && "reading base specifiers with no AST context"); 7184 ASTContext &Context = *ContextObj; 7185 7186 RecordLocation Loc = getLocalBitOffset(Offset); 7187 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7188 SavedStreamPosition SavedPosition(Cursor); 7189 Cursor.JumpToBit(Loc.Offset); 7190 ReadingKindTracker ReadingKind(Read_Decl, *this); 7191 RecordData Record; 7192 unsigned Code = Cursor.ReadCode(); 7193 unsigned RecCode = Cursor.readRecord(Code, Record); 7194 if (RecCode != DECL_CXX_BASE_SPECIFIERS) { 7195 Error("malformed AST file: missing C++ base specifiers"); 7196 return nullptr; 7197 } 7198 7199 unsigned Idx = 0; 7200 unsigned NumBases = Record[Idx++]; 7201 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases); 7202 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases]; 7203 for (unsigned I = 0; I != NumBases; ++I) 7204 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx); 7205 return Bases; 7206 } 7207 7208 serialization::DeclID 7209 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const { 7210 if (LocalID < NUM_PREDEF_DECL_IDS) 7211 return LocalID; 7212 7213 if (!F.ModuleOffsetMap.empty()) 7214 ReadModuleOffsetMap(F); 7215 7216 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7217 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS); 7218 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap"); 7219 7220 return LocalID + I->second; 7221 } 7222 7223 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID, 7224 ModuleFile &M) const { 7225 // Predefined decls aren't from any module. 7226 if (ID < NUM_PREDEF_DECL_IDS) 7227 return false; 7228 7229 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID && 7230 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls; 7231 } 7232 7233 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) { 7234 if (!D->isFromASTFile()) 7235 return nullptr; 7236 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID()); 7237 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7238 return I->second; 7239 } 7240 7241 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) { 7242 if (ID < NUM_PREDEF_DECL_IDS) 7243 return SourceLocation(); 7244 7245 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7246 7247 if (Index > DeclsLoaded.size()) { 7248 Error("declaration ID out-of-range for AST file"); 7249 return SourceLocation(); 7250 } 7251 7252 if (Decl *D = DeclsLoaded[Index]) 7253 return D->getLocation(); 7254 7255 SourceLocation Loc; 7256 DeclCursorForID(ID, Loc); 7257 return Loc; 7258 } 7259 7260 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) { 7261 switch (ID) { 7262 case PREDEF_DECL_NULL_ID: 7263 return nullptr; 7264 7265 case PREDEF_DECL_TRANSLATION_UNIT_ID: 7266 return Context.getTranslationUnitDecl(); 7267 7268 case PREDEF_DECL_OBJC_ID_ID: 7269 return Context.getObjCIdDecl(); 7270 7271 case PREDEF_DECL_OBJC_SEL_ID: 7272 return Context.getObjCSelDecl(); 7273 7274 case PREDEF_DECL_OBJC_CLASS_ID: 7275 return Context.getObjCClassDecl(); 7276 7277 case PREDEF_DECL_OBJC_PROTOCOL_ID: 7278 return Context.getObjCProtocolDecl(); 7279 7280 case PREDEF_DECL_INT_128_ID: 7281 return Context.getInt128Decl(); 7282 7283 case PREDEF_DECL_UNSIGNED_INT_128_ID: 7284 return Context.getUInt128Decl(); 7285 7286 case PREDEF_DECL_OBJC_INSTANCETYPE_ID: 7287 return Context.getObjCInstanceTypeDecl(); 7288 7289 case PREDEF_DECL_BUILTIN_VA_LIST_ID: 7290 return Context.getBuiltinVaListDecl(); 7291 7292 case PREDEF_DECL_VA_LIST_TAG: 7293 return Context.getVaListTagDecl(); 7294 7295 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID: 7296 return Context.getBuiltinMSVaListDecl(); 7297 7298 case PREDEF_DECL_EXTERN_C_CONTEXT_ID: 7299 return Context.getExternCContextDecl(); 7300 7301 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID: 7302 return Context.getMakeIntegerSeqDecl(); 7303 7304 case PREDEF_DECL_CF_CONSTANT_STRING_ID: 7305 return Context.getCFConstantStringDecl(); 7306 7307 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID: 7308 return Context.getCFConstantStringTagDecl(); 7309 7310 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID: 7311 return Context.getTypePackElementDecl(); 7312 } 7313 llvm_unreachable("PredefinedDeclIDs unknown enum value"); 7314 } 7315 7316 Decl *ASTReader::GetExistingDecl(DeclID ID) { 7317 assert(ContextObj && "reading decl with no AST context"); 7318 if (ID < NUM_PREDEF_DECL_IDS) { 7319 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID); 7320 if (D) { 7321 // Track that we have merged the declaration with ID \p ID into the 7322 // pre-existing predefined declaration \p D. 7323 auto &Merged = KeyDecls[D->getCanonicalDecl()]; 7324 if (Merged.empty()) 7325 Merged.push_back(ID); 7326 } 7327 return D; 7328 } 7329 7330 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7331 7332 if (Index >= DeclsLoaded.size()) { 7333 assert(0 && "declaration ID out-of-range for AST file"); 7334 Error("declaration ID out-of-range for AST file"); 7335 return nullptr; 7336 } 7337 7338 return DeclsLoaded[Index]; 7339 } 7340 7341 Decl *ASTReader::GetDecl(DeclID ID) { 7342 if (ID < NUM_PREDEF_DECL_IDS) 7343 return GetExistingDecl(ID); 7344 7345 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7346 7347 if (Index >= DeclsLoaded.size()) { 7348 assert(0 && "declaration ID out-of-range for AST file"); 7349 Error("declaration ID out-of-range for AST file"); 7350 return nullptr; 7351 } 7352 7353 if (!DeclsLoaded[Index]) { 7354 ReadDeclRecord(ID); 7355 if (DeserializationListener) 7356 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); 7357 } 7358 7359 return DeclsLoaded[Index]; 7360 } 7361 7362 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M, 7363 DeclID GlobalID) { 7364 if (GlobalID < NUM_PREDEF_DECL_IDS) 7365 return GlobalID; 7366 7367 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID); 7368 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7369 ModuleFile *Owner = I->second; 7370 7371 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos 7372 = M.GlobalToLocalDeclIDs.find(Owner); 7373 if (Pos == M.GlobalToLocalDeclIDs.end()) 7374 return 0; 7375 7376 return GlobalID - Owner->BaseDeclID + Pos->second; 7377 } 7378 7379 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F, 7380 const RecordData &Record, 7381 unsigned &Idx) { 7382 if (Idx >= Record.size()) { 7383 Error("Corrupted AST file"); 7384 return 0; 7385 } 7386 7387 return getGlobalDeclID(F, Record[Idx++]); 7388 } 7389 7390 /// Resolve the offset of a statement into a statement. 7391 /// 7392 /// This operation will read a new statement from the external 7393 /// source each time it is called, and is meant to be used via a 7394 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 7395 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) { 7396 // Switch case IDs are per Decl. 7397 ClearSwitchCaseIDs(); 7398 7399 // Offset here is a global offset across the entire chain. 7400 RecordLocation Loc = getLocalBitOffset(Offset); 7401 Loc.F->DeclsCursor.JumpToBit(Loc.Offset); 7402 assert(NumCurrentElementsDeserializing == 0 && 7403 "should not be called while already deserializing"); 7404 Deserializing D(this); 7405 return ReadStmtFromStream(*Loc.F); 7406 } 7407 7408 void ASTReader::FindExternalLexicalDecls( 7409 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant, 7410 SmallVectorImpl<Decl *> &Decls) { 7411 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {}; 7412 7413 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) { 7414 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries"); 7415 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) { 7416 auto K = (Decl::Kind)+LexicalDecls[I]; 7417 if (!IsKindWeWant(K)) 7418 continue; 7419 7420 auto ID = (serialization::DeclID)+LexicalDecls[I + 1]; 7421 7422 // Don't add predefined declarations to the lexical context more 7423 // than once. 7424 if (ID < NUM_PREDEF_DECL_IDS) { 7425 if (PredefsVisited[ID]) 7426 continue; 7427 7428 PredefsVisited[ID] = true; 7429 } 7430 7431 if (Decl *D = GetLocalDecl(*M, ID)) { 7432 assert(D->getKind() == K && "wrong kind for lexical decl"); 7433 if (!DC->isDeclInLexicalTraversal(D)) 7434 Decls.push_back(D); 7435 } 7436 } 7437 }; 7438 7439 if (isa<TranslationUnitDecl>(DC)) { 7440 for (auto Lexical : TULexicalDecls) 7441 Visit(Lexical.first, Lexical.second); 7442 } else { 7443 auto I = LexicalDecls.find(DC); 7444 if (I != LexicalDecls.end()) 7445 Visit(I->second.first, I->second.second); 7446 } 7447 7448 ++NumLexicalDeclContextsRead; 7449 } 7450 7451 namespace { 7452 7453 class DeclIDComp { 7454 ASTReader &Reader; 7455 ModuleFile &Mod; 7456 7457 public: 7458 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {} 7459 7460 bool operator()(LocalDeclID L, LocalDeclID R) const { 7461 SourceLocation LHS = getLocation(L); 7462 SourceLocation RHS = getLocation(R); 7463 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7464 } 7465 7466 bool operator()(SourceLocation LHS, LocalDeclID R) const { 7467 SourceLocation RHS = getLocation(R); 7468 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7469 } 7470 7471 bool operator()(LocalDeclID L, SourceLocation RHS) const { 7472 SourceLocation LHS = getLocation(L); 7473 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7474 } 7475 7476 SourceLocation getLocation(LocalDeclID ID) const { 7477 return Reader.getSourceManager().getFileLoc( 7478 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID))); 7479 } 7480 }; 7481 7482 } // namespace 7483 7484 void ASTReader::FindFileRegionDecls(FileID File, 7485 unsigned Offset, unsigned Length, 7486 SmallVectorImpl<Decl *> &Decls) { 7487 SourceManager &SM = getSourceManager(); 7488 7489 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File); 7490 if (I == FileDeclIDs.end()) 7491 return; 7492 7493 FileDeclsInfo &DInfo = I->second; 7494 if (DInfo.Decls.empty()) 7495 return; 7496 7497 SourceLocation 7498 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset); 7499 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length); 7500 7501 DeclIDComp DIDComp(*this, *DInfo.Mod); 7502 ArrayRef<serialization::LocalDeclID>::iterator 7503 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(), 7504 BeginLoc, DIDComp); 7505 if (BeginIt != DInfo.Decls.begin()) 7506 --BeginIt; 7507 7508 // If we are pointing at a top-level decl inside an objc container, we need 7509 // to backtrack until we find it otherwise we will fail to report that the 7510 // region overlaps with an objc container. 7511 while (BeginIt != DInfo.Decls.begin() && 7512 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt)) 7513 ->isTopLevelDeclInObjCContainer()) 7514 --BeginIt; 7515 7516 ArrayRef<serialization::LocalDeclID>::iterator 7517 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(), 7518 EndLoc, DIDComp); 7519 if (EndIt != DInfo.Decls.end()) 7520 ++EndIt; 7521 7522 for (ArrayRef<serialization::LocalDeclID>::iterator 7523 DIt = BeginIt; DIt != EndIt; ++DIt) 7524 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt))); 7525 } 7526 7527 bool 7528 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, 7529 DeclarationName Name) { 7530 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() && 7531 "DeclContext has no visible decls in storage"); 7532 if (!Name) 7533 return false; 7534 7535 auto It = Lookups.find(DC); 7536 if (It == Lookups.end()) 7537 return false; 7538 7539 Deserializing LookupResults(this); 7540 7541 // Load the list of declarations. 7542 SmallVector<NamedDecl *, 64> Decls; 7543 for (DeclID ID : It->second.Table.find(Name)) { 7544 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7545 if (ND->getDeclName() == Name) 7546 Decls.push_back(ND); 7547 } 7548 7549 ++NumVisibleDeclContextsRead; 7550 SetExternalVisibleDeclsForName(DC, Name, Decls); 7551 return !Decls.empty(); 7552 } 7553 7554 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) { 7555 if (!DC->hasExternalVisibleStorage()) 7556 return; 7557 7558 auto It = Lookups.find(DC); 7559 assert(It != Lookups.end() && 7560 "have external visible storage but no lookup tables"); 7561 7562 DeclsMap Decls; 7563 7564 for (DeclID ID : It->second.Table.findAll()) { 7565 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7566 Decls[ND->getDeclName()].push_back(ND); 7567 } 7568 7569 ++NumVisibleDeclContextsRead; 7570 7571 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) { 7572 SetExternalVisibleDeclsForName(DC, I->first, I->second); 7573 } 7574 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false); 7575 } 7576 7577 const serialization::reader::DeclContextLookupTable * 7578 ASTReader::getLoadedLookupTables(DeclContext *Primary) const { 7579 auto I = Lookups.find(Primary); 7580 return I == Lookups.end() ? nullptr : &I->second; 7581 } 7582 7583 /// Under non-PCH compilation the consumer receives the objc methods 7584 /// before receiving the implementation, and codegen depends on this. 7585 /// We simulate this by deserializing and passing to consumer the methods of the 7586 /// implementation before passing the deserialized implementation decl. 7587 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD, 7588 ASTConsumer *Consumer) { 7589 assert(ImplD && Consumer); 7590 7591 for (auto *I : ImplD->methods()) 7592 Consumer->HandleInterestingDecl(DeclGroupRef(I)); 7593 7594 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD)); 7595 } 7596 7597 void ASTReader::PassInterestingDeclToConsumer(Decl *D) { 7598 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) 7599 PassObjCImplDeclToConsumer(ImplD, Consumer); 7600 else 7601 Consumer->HandleInterestingDecl(DeclGroupRef(D)); 7602 } 7603 7604 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) { 7605 this->Consumer = Consumer; 7606 7607 if (Consumer) 7608 PassInterestingDeclsToConsumer(); 7609 7610 if (DeserializationListener) 7611 DeserializationListener->ReaderInitialized(this); 7612 } 7613 7614 void ASTReader::PrintStats() { 7615 std::fprintf(stderr, "*** AST File Statistics:\n"); 7616 7617 unsigned NumTypesLoaded 7618 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), 7619 QualType()); 7620 unsigned NumDeclsLoaded 7621 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), 7622 (Decl *)nullptr); 7623 unsigned NumIdentifiersLoaded 7624 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), 7625 IdentifiersLoaded.end(), 7626 (IdentifierInfo *)nullptr); 7627 unsigned NumMacrosLoaded 7628 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(), 7629 MacrosLoaded.end(), 7630 (MacroInfo *)nullptr); 7631 unsigned NumSelectorsLoaded 7632 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), 7633 SelectorsLoaded.end(), 7634 Selector()); 7635 7636 if (unsigned TotalNumSLocEntries = getTotalNumSLocs()) 7637 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", 7638 NumSLocEntriesRead, TotalNumSLocEntries, 7639 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); 7640 if (!TypesLoaded.empty()) 7641 std::fprintf(stderr, " %u/%u types read (%f%%)\n", 7642 NumTypesLoaded, (unsigned)TypesLoaded.size(), 7643 ((float)NumTypesLoaded/TypesLoaded.size() * 100)); 7644 if (!DeclsLoaded.empty()) 7645 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", 7646 NumDeclsLoaded, (unsigned)DeclsLoaded.size(), 7647 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); 7648 if (!IdentifiersLoaded.empty()) 7649 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", 7650 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), 7651 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); 7652 if (!MacrosLoaded.empty()) 7653 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7654 NumMacrosLoaded, (unsigned)MacrosLoaded.size(), 7655 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100)); 7656 if (!SelectorsLoaded.empty()) 7657 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", 7658 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), 7659 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); 7660 if (TotalNumStatements) 7661 std::fprintf(stderr, " %u/%u statements read (%f%%)\n", 7662 NumStatementsRead, TotalNumStatements, 7663 ((float)NumStatementsRead/TotalNumStatements * 100)); 7664 if (TotalNumMacros) 7665 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7666 NumMacrosRead, TotalNumMacros, 7667 ((float)NumMacrosRead/TotalNumMacros * 100)); 7668 if (TotalLexicalDeclContexts) 7669 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", 7670 NumLexicalDeclContextsRead, TotalLexicalDeclContexts, 7671 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts 7672 * 100)); 7673 if (TotalVisibleDeclContexts) 7674 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", 7675 NumVisibleDeclContextsRead, TotalVisibleDeclContexts, 7676 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts 7677 * 100)); 7678 if (TotalNumMethodPoolEntries) 7679 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", 7680 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, 7681 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries 7682 * 100)); 7683 if (NumMethodPoolLookups) 7684 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n", 7685 NumMethodPoolHits, NumMethodPoolLookups, 7686 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0)); 7687 if (NumMethodPoolTableLookups) 7688 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n", 7689 NumMethodPoolTableHits, NumMethodPoolTableLookups, 7690 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups 7691 * 100.0)); 7692 if (NumIdentifierLookupHits) 7693 std::fprintf(stderr, 7694 " %u / %u identifier table lookups succeeded (%f%%)\n", 7695 NumIdentifierLookupHits, NumIdentifierLookups, 7696 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups); 7697 7698 if (GlobalIndex) { 7699 std::fprintf(stderr, "\n"); 7700 GlobalIndex->printStats(); 7701 } 7702 7703 std::fprintf(stderr, "\n"); 7704 dump(); 7705 std::fprintf(stderr, "\n"); 7706 } 7707 7708 template<typename Key, typename ModuleFile, unsigned InitialCapacity> 7709 LLVM_DUMP_METHOD static void 7710 dumpModuleIDMap(StringRef Name, 7711 const ContinuousRangeMap<Key, ModuleFile *, 7712 InitialCapacity> &Map) { 7713 if (Map.begin() == Map.end()) 7714 return; 7715 7716 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>; 7717 7718 llvm::errs() << Name << ":\n"; 7719 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end(); 7720 I != IEnd; ++I) { 7721 llvm::errs() << " " << I->first << " -> " << I->second->FileName 7722 << "\n"; 7723 } 7724 } 7725 7726 LLVM_DUMP_METHOD void ASTReader::dump() { 7727 llvm::errs() << "*** PCH/ModuleFile Remappings:\n"; 7728 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap); 7729 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap); 7730 dumpModuleIDMap("Global type map", GlobalTypeMap); 7731 dumpModuleIDMap("Global declaration map", GlobalDeclMap); 7732 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap); 7733 dumpModuleIDMap("Global macro map", GlobalMacroMap); 7734 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap); 7735 dumpModuleIDMap("Global selector map", GlobalSelectorMap); 7736 dumpModuleIDMap("Global preprocessed entity map", 7737 GlobalPreprocessedEntityMap); 7738 7739 llvm::errs() << "\n*** PCH/Modules Loaded:"; 7740 for (ModuleFile &M : ModuleMgr) 7741 M.dump(); 7742 } 7743 7744 /// Return the amount of memory used by memory buffers, breaking down 7745 /// by heap-backed versus mmap'ed memory. 7746 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const { 7747 for (ModuleFile &I : ModuleMgr) { 7748 if (llvm::MemoryBuffer *buf = I.Buffer) { 7749 size_t bytes = buf->getBufferSize(); 7750 switch (buf->getBufferKind()) { 7751 case llvm::MemoryBuffer::MemoryBuffer_Malloc: 7752 sizes.malloc_bytes += bytes; 7753 break; 7754 case llvm::MemoryBuffer::MemoryBuffer_MMap: 7755 sizes.mmap_bytes += bytes; 7756 break; 7757 } 7758 } 7759 } 7760 } 7761 7762 void ASTReader::InitializeSema(Sema &S) { 7763 SemaObj = &S; 7764 S.addExternalSource(this); 7765 7766 // Makes sure any declarations that were deserialized "too early" 7767 // still get added to the identifier's declaration chains. 7768 for (uint64_t ID : PreloadedDeclIDs) { 7769 NamedDecl *D = cast<NamedDecl>(GetDecl(ID)); 7770 pushExternalDeclIntoScope(D, D->getDeclName()); 7771 } 7772 PreloadedDeclIDs.clear(); 7773 7774 // FIXME: What happens if these are changed by a module import? 7775 if (!FPPragmaOptions.empty()) { 7776 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS"); 7777 SemaObj->FPFeatures = FPOptions(FPPragmaOptions[0]); 7778 } 7779 7780 SemaObj->OpenCLFeatures.copy(OpenCLExtensions); 7781 SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap; 7782 SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap; 7783 7784 UpdateSema(); 7785 } 7786 7787 void ASTReader::UpdateSema() { 7788 assert(SemaObj && "no Sema to update"); 7789 7790 // Load the offsets of the declarations that Sema references. 7791 // They will be lazily deserialized when needed. 7792 if (!SemaDeclRefs.empty()) { 7793 assert(SemaDeclRefs.size() % 3 == 0); 7794 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) { 7795 if (!SemaObj->StdNamespace) 7796 SemaObj->StdNamespace = SemaDeclRefs[I]; 7797 if (!SemaObj->StdBadAlloc) 7798 SemaObj->StdBadAlloc = SemaDeclRefs[I+1]; 7799 if (!SemaObj->StdAlignValT) 7800 SemaObj->StdAlignValT = SemaDeclRefs[I+2]; 7801 } 7802 SemaDeclRefs.clear(); 7803 } 7804 7805 // Update the state of pragmas. Use the same API as if we had encountered the 7806 // pragma in the source. 7807 if(OptimizeOffPragmaLocation.isValid()) 7808 SemaObj->ActOnPragmaOptimize(/* IsOn = */ false, OptimizeOffPragmaLocation); 7809 if (PragmaMSStructState != -1) 7810 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState); 7811 if (PointersToMembersPragmaLocation.isValid()) { 7812 SemaObj->ActOnPragmaMSPointersToMembers( 7813 (LangOptions::PragmaMSPointersToMembersKind) 7814 PragmaMSPointersToMembersState, 7815 PointersToMembersPragmaLocation); 7816 } 7817 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth; 7818 7819 if (PragmaPackCurrentValue) { 7820 // The bottom of the stack might have a default value. It must be adjusted 7821 // to the current value to ensure that the packing state is preserved after 7822 // popping entries that were included/imported from a PCH/module. 7823 bool DropFirst = false; 7824 if (!PragmaPackStack.empty() && 7825 PragmaPackStack.front().Location.isInvalid()) { 7826 assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue && 7827 "Expected a default alignment value"); 7828 SemaObj->PackStack.Stack.emplace_back( 7829 PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue, 7830 SemaObj->PackStack.CurrentPragmaLocation, 7831 PragmaPackStack.front().PushLocation); 7832 DropFirst = true; 7833 } 7834 for (const auto &Entry : 7835 llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0)) 7836 SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value, 7837 Entry.Location, Entry.PushLocation); 7838 if (PragmaPackCurrentLocation.isInvalid()) { 7839 assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue && 7840 "Expected a default alignment value"); 7841 // Keep the current values. 7842 } else { 7843 SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue; 7844 SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation; 7845 } 7846 } 7847 } 7848 7849 IdentifierInfo *ASTReader::get(StringRef Name) { 7850 // Note that we are loading an identifier. 7851 Deserializing AnIdentifier(this); 7852 7853 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0, 7854 NumIdentifierLookups, 7855 NumIdentifierLookupHits); 7856 7857 // We don't need to do identifier table lookups in C++ modules (we preload 7858 // all interesting declarations, and don't need to use the scope for name 7859 // lookups). Perform the lookup in PCH files, though, since we don't build 7860 // a complete initial identifier table if we're carrying on from a PCH. 7861 if (PP.getLangOpts().CPlusPlus) { 7862 for (auto F : ModuleMgr.pch_modules()) 7863 if (Visitor(*F)) 7864 break; 7865 } else { 7866 // If there is a global index, look there first to determine which modules 7867 // provably do not have any results for this identifier. 7868 GlobalModuleIndex::HitSet Hits; 7869 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 7870 if (!loadGlobalIndex()) { 7871 if (GlobalIndex->lookupIdentifier(Name, Hits)) { 7872 HitsPtr = &Hits; 7873 } 7874 } 7875 7876 ModuleMgr.visit(Visitor, HitsPtr); 7877 } 7878 7879 IdentifierInfo *II = Visitor.getIdentifierInfo(); 7880 markIdentifierUpToDate(II); 7881 return II; 7882 } 7883 7884 namespace clang { 7885 7886 /// An identifier-lookup iterator that enumerates all of the 7887 /// identifiers stored within a set of AST files. 7888 class ASTIdentifierIterator : public IdentifierIterator { 7889 /// The AST reader whose identifiers are being enumerated. 7890 const ASTReader &Reader; 7891 7892 /// The current index into the chain of AST files stored in 7893 /// the AST reader. 7894 unsigned Index; 7895 7896 /// The current position within the identifier lookup table 7897 /// of the current AST file. 7898 ASTIdentifierLookupTable::key_iterator Current; 7899 7900 /// The end position within the identifier lookup table of 7901 /// the current AST file. 7902 ASTIdentifierLookupTable::key_iterator End; 7903 7904 /// Whether to skip any modules in the ASTReader. 7905 bool SkipModules; 7906 7907 public: 7908 explicit ASTIdentifierIterator(const ASTReader &Reader, 7909 bool SkipModules = false); 7910 7911 StringRef Next() override; 7912 }; 7913 7914 } // namespace clang 7915 7916 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader, 7917 bool SkipModules) 7918 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) { 7919 } 7920 7921 StringRef ASTIdentifierIterator::Next() { 7922 while (Current == End) { 7923 // If we have exhausted all of our AST files, we're done. 7924 if (Index == 0) 7925 return StringRef(); 7926 7927 --Index; 7928 ModuleFile &F = Reader.ModuleMgr[Index]; 7929 if (SkipModules && F.isModule()) 7930 continue; 7931 7932 ASTIdentifierLookupTable *IdTable = 7933 (ASTIdentifierLookupTable *)F.IdentifierLookupTable; 7934 Current = IdTable->key_begin(); 7935 End = IdTable->key_end(); 7936 } 7937 7938 // We have any identifiers remaining in the current AST file; return 7939 // the next one. 7940 StringRef Result = *Current; 7941 ++Current; 7942 return Result; 7943 } 7944 7945 namespace { 7946 7947 /// A utility for appending two IdentifierIterators. 7948 class ChainedIdentifierIterator : public IdentifierIterator { 7949 std::unique_ptr<IdentifierIterator> Current; 7950 std::unique_ptr<IdentifierIterator> Queued; 7951 7952 public: 7953 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First, 7954 std::unique_ptr<IdentifierIterator> Second) 7955 : Current(std::move(First)), Queued(std::move(Second)) {} 7956 7957 StringRef Next() override { 7958 if (!Current) 7959 return StringRef(); 7960 7961 StringRef result = Current->Next(); 7962 if (!result.empty()) 7963 return result; 7964 7965 // Try the queued iterator, which may itself be empty. 7966 Current.reset(); 7967 std::swap(Current, Queued); 7968 return Next(); 7969 } 7970 }; 7971 7972 } // namespace 7973 7974 IdentifierIterator *ASTReader::getIdentifiers() { 7975 if (!loadGlobalIndex()) { 7976 std::unique_ptr<IdentifierIterator> ReaderIter( 7977 new ASTIdentifierIterator(*this, /*SkipModules=*/true)); 7978 std::unique_ptr<IdentifierIterator> ModulesIter( 7979 GlobalIndex->createIdentifierIterator()); 7980 return new ChainedIdentifierIterator(std::move(ReaderIter), 7981 std::move(ModulesIter)); 7982 } 7983 7984 return new ASTIdentifierIterator(*this); 7985 } 7986 7987 namespace clang { 7988 namespace serialization { 7989 7990 class ReadMethodPoolVisitor { 7991 ASTReader &Reader; 7992 Selector Sel; 7993 unsigned PriorGeneration; 7994 unsigned InstanceBits = 0; 7995 unsigned FactoryBits = 0; 7996 bool InstanceHasMoreThanOneDecl = false; 7997 bool FactoryHasMoreThanOneDecl = false; 7998 SmallVector<ObjCMethodDecl *, 4> InstanceMethods; 7999 SmallVector<ObjCMethodDecl *, 4> FactoryMethods; 8000 8001 public: 8002 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel, 8003 unsigned PriorGeneration) 8004 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {} 8005 8006 bool operator()(ModuleFile &M) { 8007 if (!M.SelectorLookupTable) 8008 return false; 8009 8010 // If we've already searched this module file, skip it now. 8011 if (M.Generation <= PriorGeneration) 8012 return true; 8013 8014 ++Reader.NumMethodPoolTableLookups; 8015 ASTSelectorLookupTable *PoolTable 8016 = (ASTSelectorLookupTable*)M.SelectorLookupTable; 8017 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel); 8018 if (Pos == PoolTable->end()) 8019 return false; 8020 8021 ++Reader.NumMethodPoolTableHits; 8022 ++Reader.NumSelectorsRead; 8023 // FIXME: Not quite happy with the statistics here. We probably should 8024 // disable this tracking when called via LoadSelector. 8025 // Also, should entries without methods count as misses? 8026 ++Reader.NumMethodPoolEntriesRead; 8027 ASTSelectorLookupTrait::data_type Data = *Pos; 8028 if (Reader.DeserializationListener) 8029 Reader.DeserializationListener->SelectorRead(Data.ID, Sel); 8030 8031 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end()); 8032 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end()); 8033 InstanceBits = Data.InstanceBits; 8034 FactoryBits = Data.FactoryBits; 8035 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl; 8036 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl; 8037 return true; 8038 } 8039 8040 /// Retrieve the instance methods found by this visitor. 8041 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const { 8042 return InstanceMethods; 8043 } 8044 8045 /// Retrieve the instance methods found by this visitor. 8046 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const { 8047 return FactoryMethods; 8048 } 8049 8050 unsigned getInstanceBits() const { return InstanceBits; } 8051 unsigned getFactoryBits() const { return FactoryBits; } 8052 8053 bool instanceHasMoreThanOneDecl() const { 8054 return InstanceHasMoreThanOneDecl; 8055 } 8056 8057 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; } 8058 }; 8059 8060 } // namespace serialization 8061 } // namespace clang 8062 8063 /// Add the given set of methods to the method list. 8064 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods, 8065 ObjCMethodList &List) { 8066 for (unsigned I = 0, N = Methods.size(); I != N; ++I) { 8067 S.addMethodToGlobalList(&List, Methods[I]); 8068 } 8069 } 8070 8071 void ASTReader::ReadMethodPool(Selector Sel) { 8072 // Get the selector generation and update it to the current generation. 8073 unsigned &Generation = SelectorGeneration[Sel]; 8074 unsigned PriorGeneration = Generation; 8075 Generation = getGeneration(); 8076 SelectorOutOfDate[Sel] = false; 8077 8078 // Search for methods defined with this selector. 8079 ++NumMethodPoolLookups; 8080 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration); 8081 ModuleMgr.visit(Visitor); 8082 8083 if (Visitor.getInstanceMethods().empty() && 8084 Visitor.getFactoryMethods().empty()) 8085 return; 8086 8087 ++NumMethodPoolHits; 8088 8089 if (!getSema()) 8090 return; 8091 8092 Sema &S = *getSema(); 8093 Sema::GlobalMethodPool::iterator Pos 8094 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first; 8095 8096 Pos->second.first.setBits(Visitor.getInstanceBits()); 8097 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl()); 8098 Pos->second.second.setBits(Visitor.getFactoryBits()); 8099 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl()); 8100 8101 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since 8102 // when building a module we keep every method individually and may need to 8103 // update hasMoreThanOneDecl as we add the methods. 8104 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first); 8105 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second); 8106 } 8107 8108 void ASTReader::updateOutOfDateSelector(Selector Sel) { 8109 if (SelectorOutOfDate[Sel]) 8110 ReadMethodPool(Sel); 8111 } 8112 8113 void ASTReader::ReadKnownNamespaces( 8114 SmallVectorImpl<NamespaceDecl *> &Namespaces) { 8115 Namespaces.clear(); 8116 8117 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) { 8118 if (NamespaceDecl *Namespace 8119 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I]))) 8120 Namespaces.push_back(Namespace); 8121 } 8122 } 8123 8124 void ASTReader::ReadUndefinedButUsed( 8125 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) { 8126 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) { 8127 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++])); 8128 SourceLocation Loc = 8129 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]); 8130 Undefined.insert(std::make_pair(D, Loc)); 8131 } 8132 } 8133 8134 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector< 8135 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> & 8136 Exprs) { 8137 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) { 8138 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++])); 8139 uint64_t Count = DelayedDeleteExprs[Idx++]; 8140 for (uint64_t C = 0; C < Count; ++C) { 8141 SourceLocation DeleteLoc = 8142 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]); 8143 const bool IsArrayForm = DelayedDeleteExprs[Idx++]; 8144 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm)); 8145 } 8146 } 8147 } 8148 8149 void ASTReader::ReadTentativeDefinitions( 8150 SmallVectorImpl<VarDecl *> &TentativeDefs) { 8151 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { 8152 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I])); 8153 if (Var) 8154 TentativeDefs.push_back(Var); 8155 } 8156 TentativeDefinitions.clear(); 8157 } 8158 8159 void ASTReader::ReadUnusedFileScopedDecls( 8160 SmallVectorImpl<const DeclaratorDecl *> &Decls) { 8161 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) { 8162 DeclaratorDecl *D 8163 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I])); 8164 if (D) 8165 Decls.push_back(D); 8166 } 8167 UnusedFileScopedDecls.clear(); 8168 } 8169 8170 void ASTReader::ReadDelegatingConstructors( 8171 SmallVectorImpl<CXXConstructorDecl *> &Decls) { 8172 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) { 8173 CXXConstructorDecl *D 8174 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I])); 8175 if (D) 8176 Decls.push_back(D); 8177 } 8178 DelegatingCtorDecls.clear(); 8179 } 8180 8181 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) { 8182 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) { 8183 TypedefNameDecl *D 8184 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])); 8185 if (D) 8186 Decls.push_back(D); 8187 } 8188 ExtVectorDecls.clear(); 8189 } 8190 8191 void ASTReader::ReadUnusedLocalTypedefNameCandidates( 8192 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) { 8193 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N; 8194 ++I) { 8195 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>( 8196 GetDecl(UnusedLocalTypedefNameCandidates[I])); 8197 if (D) 8198 Decls.insert(D); 8199 } 8200 UnusedLocalTypedefNameCandidates.clear(); 8201 } 8202 8203 void ASTReader::ReadReferencedSelectors( 8204 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) { 8205 if (ReferencedSelectorsData.empty()) 8206 return; 8207 8208 // If there are @selector references added them to its pool. This is for 8209 // implementation of -Wselector. 8210 unsigned int DataSize = ReferencedSelectorsData.size()-1; 8211 unsigned I = 0; 8212 while (I < DataSize) { 8213 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]); 8214 SourceLocation SelLoc 8215 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]); 8216 Sels.push_back(std::make_pair(Sel, SelLoc)); 8217 } 8218 ReferencedSelectorsData.clear(); 8219 } 8220 8221 void ASTReader::ReadWeakUndeclaredIdentifiers( 8222 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) { 8223 if (WeakUndeclaredIdentifiers.empty()) 8224 return; 8225 8226 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) { 8227 IdentifierInfo *WeakId 8228 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8229 IdentifierInfo *AliasId 8230 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8231 SourceLocation Loc 8232 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]); 8233 bool Used = WeakUndeclaredIdentifiers[I++]; 8234 WeakInfo WI(AliasId, Loc); 8235 WI.setUsed(Used); 8236 WeakIDs.push_back(std::make_pair(WeakId, WI)); 8237 } 8238 WeakUndeclaredIdentifiers.clear(); 8239 } 8240 8241 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) { 8242 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) { 8243 ExternalVTableUse VT; 8244 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); 8245 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]); 8246 VT.DefinitionRequired = VTableUses[Idx++]; 8247 VTables.push_back(VT); 8248 } 8249 8250 VTableUses.clear(); 8251 } 8252 8253 void ASTReader::ReadPendingInstantiations( 8254 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) { 8255 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) { 8256 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++])); 8257 SourceLocation Loc 8258 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]); 8259 8260 Pending.push_back(std::make_pair(D, Loc)); 8261 } 8262 PendingInstantiations.clear(); 8263 } 8264 8265 void ASTReader::ReadLateParsedTemplates( 8266 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>> 8267 &LPTMap) { 8268 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N; 8269 /* In loop */) { 8270 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++])); 8271 8272 auto LT = llvm::make_unique<LateParsedTemplate>(); 8273 LT->D = GetDecl(LateParsedTemplates[Idx++]); 8274 8275 ModuleFile *F = getOwningModuleFile(LT->D); 8276 assert(F && "No module"); 8277 8278 unsigned TokN = LateParsedTemplates[Idx++]; 8279 LT->Toks.reserve(TokN); 8280 for (unsigned T = 0; T < TokN; ++T) 8281 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx)); 8282 8283 LPTMap.insert(std::make_pair(FD, std::move(LT))); 8284 } 8285 8286 LateParsedTemplates.clear(); 8287 } 8288 8289 void ASTReader::LoadSelector(Selector Sel) { 8290 // It would be complicated to avoid reading the methods anyway. So don't. 8291 ReadMethodPool(Sel); 8292 } 8293 8294 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) { 8295 assert(ID && "Non-zero identifier ID required"); 8296 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); 8297 IdentifiersLoaded[ID - 1] = II; 8298 if (DeserializationListener) 8299 DeserializationListener->IdentifierRead(ID, II); 8300 } 8301 8302 /// Set the globally-visible declarations associated with the given 8303 /// identifier. 8304 /// 8305 /// If the AST reader is currently in a state where the given declaration IDs 8306 /// cannot safely be resolved, they are queued until it is safe to resolve 8307 /// them. 8308 /// 8309 /// \param II an IdentifierInfo that refers to one or more globally-visible 8310 /// declarations. 8311 /// 8312 /// \param DeclIDs the set of declaration IDs with the name @p II that are 8313 /// visible at global scope. 8314 /// 8315 /// \param Decls if non-null, this vector will be populated with the set of 8316 /// deserialized declarations. These declarations will not be pushed into 8317 /// scope. 8318 void 8319 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, 8320 const SmallVectorImpl<uint32_t> &DeclIDs, 8321 SmallVectorImpl<Decl *> *Decls) { 8322 if (NumCurrentElementsDeserializing && !Decls) { 8323 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end()); 8324 return; 8325 } 8326 8327 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { 8328 if (!SemaObj) { 8329 // Queue this declaration so that it will be added to the 8330 // translation unit scope and identifier's declaration chain 8331 // once a Sema object is known. 8332 PreloadedDeclIDs.push_back(DeclIDs[I]); 8333 continue; 8334 } 8335 8336 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); 8337 8338 // If we're simply supposed to record the declarations, do so now. 8339 if (Decls) { 8340 Decls->push_back(D); 8341 continue; 8342 } 8343 8344 // Introduce this declaration into the translation-unit scope 8345 // and add it to the declaration chain for this identifier, so 8346 // that (unqualified) name lookup will find it. 8347 pushExternalDeclIntoScope(D, II); 8348 } 8349 } 8350 8351 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) { 8352 if (ID == 0) 8353 return nullptr; 8354 8355 if (IdentifiersLoaded.empty()) { 8356 Error("no identifier table in AST file"); 8357 return nullptr; 8358 } 8359 8360 ID -= 1; 8361 if (!IdentifiersLoaded[ID]) { 8362 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1); 8363 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map"); 8364 ModuleFile *M = I->second; 8365 unsigned Index = ID - M->BaseIdentifierID; 8366 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index]; 8367 8368 // All of the strings in the AST file are preceded by a 16-bit length. 8369 // Extract that 16-bit length to avoid having to execute strlen(). 8370 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as 8371 // unsigned integers. This is important to avoid integer overflow when 8372 // we cast them to 'unsigned'. 8373 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2; 8374 unsigned StrLen = (((unsigned) StrLenPtr[0]) 8375 | (((unsigned) StrLenPtr[1]) << 8)) - 1; 8376 auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen)); 8377 IdentifiersLoaded[ID] = &II; 8378 markIdentifierFromAST(*this, II); 8379 if (DeserializationListener) 8380 DeserializationListener->IdentifierRead(ID + 1, &II); 8381 } 8382 8383 return IdentifiersLoaded[ID]; 8384 } 8385 8386 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) { 8387 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID)); 8388 } 8389 8390 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) { 8391 if (LocalID < NUM_PREDEF_IDENT_IDS) 8392 return LocalID; 8393 8394 if (!M.ModuleOffsetMap.empty()) 8395 ReadModuleOffsetMap(M); 8396 8397 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8398 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS); 8399 assert(I != M.IdentifierRemap.end() 8400 && "Invalid index into identifier index remap"); 8401 8402 return LocalID + I->second; 8403 } 8404 8405 MacroInfo *ASTReader::getMacro(MacroID ID) { 8406 if (ID == 0) 8407 return nullptr; 8408 8409 if (MacrosLoaded.empty()) { 8410 Error("no macro table in AST file"); 8411 return nullptr; 8412 } 8413 8414 ID -= NUM_PREDEF_MACRO_IDS; 8415 if (!MacrosLoaded[ID]) { 8416 GlobalMacroMapType::iterator I 8417 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS); 8418 assert(I != GlobalMacroMap.end() && "Corrupted global macro map"); 8419 ModuleFile *M = I->second; 8420 unsigned Index = ID - M->BaseMacroID; 8421 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]); 8422 8423 if (DeserializationListener) 8424 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS, 8425 MacrosLoaded[ID]); 8426 } 8427 8428 return MacrosLoaded[ID]; 8429 } 8430 8431 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) { 8432 if (LocalID < NUM_PREDEF_MACRO_IDS) 8433 return LocalID; 8434 8435 if (!M.ModuleOffsetMap.empty()) 8436 ReadModuleOffsetMap(M); 8437 8438 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8439 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS); 8440 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap"); 8441 8442 return LocalID + I->second; 8443 } 8444 8445 serialization::SubmoduleID 8446 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) { 8447 if (LocalID < NUM_PREDEF_SUBMODULE_IDS) 8448 return LocalID; 8449 8450 if (!M.ModuleOffsetMap.empty()) 8451 ReadModuleOffsetMap(M); 8452 8453 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8454 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS); 8455 assert(I != M.SubmoduleRemap.end() 8456 && "Invalid index into submodule index remap"); 8457 8458 return LocalID + I->second; 8459 } 8460 8461 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) { 8462 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) { 8463 assert(GlobalID == 0 && "Unhandled global submodule ID"); 8464 return nullptr; 8465 } 8466 8467 if (GlobalID > SubmodulesLoaded.size()) { 8468 Error("submodule ID out of range in AST file"); 8469 return nullptr; 8470 } 8471 8472 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS]; 8473 } 8474 8475 Module *ASTReader::getModule(unsigned ID) { 8476 return getSubmodule(ID); 8477 } 8478 8479 bool ASTReader::DeclIsFromPCHWithObjectFile(const Decl *D) { 8480 ModuleFile *MF = getOwningModuleFile(D); 8481 return MF && MF->PCHHasObjectFile; 8482 } 8483 8484 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) { 8485 if (ID & 1) { 8486 // It's a module, look it up by submodule ID. 8487 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1)); 8488 return I == GlobalSubmoduleMap.end() ? nullptr : I->second; 8489 } else { 8490 // It's a prefix (preamble, PCH, ...). Look it up by index. 8491 unsigned IndexFromEnd = ID >> 1; 8492 assert(IndexFromEnd && "got reference to unknown module file"); 8493 return getModuleManager().pch_modules().end()[-IndexFromEnd]; 8494 } 8495 } 8496 8497 unsigned ASTReader::getModuleFileID(ModuleFile *F) { 8498 if (!F) 8499 return 1; 8500 8501 // For a file representing a module, use the submodule ID of the top-level 8502 // module as the file ID. For any other kind of file, the number of such 8503 // files loaded beforehand will be the same on reload. 8504 // FIXME: Is this true even if we have an explicit module file and a PCH? 8505 if (F->isModule()) 8506 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1; 8507 8508 auto PCHModules = getModuleManager().pch_modules(); 8509 auto I = std::find(PCHModules.begin(), PCHModules.end(), F); 8510 assert(I != PCHModules.end() && "emitting reference to unknown file"); 8511 return (I - PCHModules.end()) << 1; 8512 } 8513 8514 llvm::Optional<ExternalASTSource::ASTSourceDescriptor> 8515 ASTReader::getSourceDescriptor(unsigned ID) { 8516 if (const Module *M = getSubmodule(ID)) 8517 return ExternalASTSource::ASTSourceDescriptor(*M); 8518 8519 // If there is only a single PCH, return it instead. 8520 // Chained PCH are not supported. 8521 const auto &PCHChain = ModuleMgr.pch_modules(); 8522 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) { 8523 ModuleFile &MF = ModuleMgr.getPrimaryModule(); 8524 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName); 8525 StringRef FileName = llvm::sys::path::filename(MF.FileName); 8526 return ASTReader::ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName, 8527 MF.Signature); 8528 } 8529 return None; 8530 } 8531 8532 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) { 8533 auto I = DefinitionSource.find(FD); 8534 if (I == DefinitionSource.end()) 8535 return EK_ReplyHazy; 8536 return I->second ? EK_Never : EK_Always; 8537 } 8538 8539 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) { 8540 return DecodeSelector(getGlobalSelectorID(M, LocalID)); 8541 } 8542 8543 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) { 8544 if (ID == 0) 8545 return Selector(); 8546 8547 if (ID > SelectorsLoaded.size()) { 8548 Error("selector ID out of range in AST file"); 8549 return Selector(); 8550 } 8551 8552 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) { 8553 // Load this selector from the selector table. 8554 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID); 8555 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map"); 8556 ModuleFile &M = *I->second; 8557 ASTSelectorLookupTrait Trait(*this, M); 8558 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS; 8559 SelectorsLoaded[ID - 1] = 8560 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0); 8561 if (DeserializationListener) 8562 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 8563 } 8564 8565 return SelectorsLoaded[ID - 1]; 8566 } 8567 8568 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) { 8569 return DecodeSelector(ID); 8570 } 8571 8572 uint32_t ASTReader::GetNumExternalSelectors() { 8573 // ID 0 (the null selector) is considered an external selector. 8574 return getTotalNumSelectors() + 1; 8575 } 8576 8577 serialization::SelectorID 8578 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const { 8579 if (LocalID < NUM_PREDEF_SELECTOR_IDS) 8580 return LocalID; 8581 8582 if (!M.ModuleOffsetMap.empty()) 8583 ReadModuleOffsetMap(M); 8584 8585 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8586 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS); 8587 assert(I != M.SelectorRemap.end() 8588 && "Invalid index into selector index remap"); 8589 8590 return LocalID + I->second; 8591 } 8592 8593 DeclarationName 8594 ASTReader::ReadDeclarationName(ModuleFile &F, 8595 const RecordData &Record, unsigned &Idx) { 8596 ASTContext &Context = getContext(); 8597 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++]; 8598 switch (Kind) { 8599 case DeclarationName::Identifier: 8600 return DeclarationName(GetIdentifierInfo(F, Record, Idx)); 8601 8602 case DeclarationName::ObjCZeroArgSelector: 8603 case DeclarationName::ObjCOneArgSelector: 8604 case DeclarationName::ObjCMultiArgSelector: 8605 return DeclarationName(ReadSelector(F, Record, Idx)); 8606 8607 case DeclarationName::CXXConstructorName: 8608 return Context.DeclarationNames.getCXXConstructorName( 8609 Context.getCanonicalType(readType(F, Record, Idx))); 8610 8611 case DeclarationName::CXXDestructorName: 8612 return Context.DeclarationNames.getCXXDestructorName( 8613 Context.getCanonicalType(readType(F, Record, Idx))); 8614 8615 case DeclarationName::CXXDeductionGuideName: 8616 return Context.DeclarationNames.getCXXDeductionGuideName( 8617 ReadDeclAs<TemplateDecl>(F, Record, Idx)); 8618 8619 case DeclarationName::CXXConversionFunctionName: 8620 return Context.DeclarationNames.getCXXConversionFunctionName( 8621 Context.getCanonicalType(readType(F, Record, Idx))); 8622 8623 case DeclarationName::CXXOperatorName: 8624 return Context.DeclarationNames.getCXXOperatorName( 8625 (OverloadedOperatorKind)Record[Idx++]); 8626 8627 case DeclarationName::CXXLiteralOperatorName: 8628 return Context.DeclarationNames.getCXXLiteralOperatorName( 8629 GetIdentifierInfo(F, Record, Idx)); 8630 8631 case DeclarationName::CXXUsingDirective: 8632 return DeclarationName::getUsingDirectiveName(); 8633 } 8634 8635 llvm_unreachable("Invalid NameKind!"); 8636 } 8637 8638 void ASTReader::ReadDeclarationNameLoc(ModuleFile &F, 8639 DeclarationNameLoc &DNLoc, 8640 DeclarationName Name, 8641 const RecordData &Record, unsigned &Idx) { 8642 switch (Name.getNameKind()) { 8643 case DeclarationName::CXXConstructorName: 8644 case DeclarationName::CXXDestructorName: 8645 case DeclarationName::CXXConversionFunctionName: 8646 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx); 8647 break; 8648 8649 case DeclarationName::CXXOperatorName: 8650 DNLoc.CXXOperatorName.BeginOpNameLoc 8651 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 8652 DNLoc.CXXOperatorName.EndOpNameLoc 8653 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 8654 break; 8655 8656 case DeclarationName::CXXLiteralOperatorName: 8657 DNLoc.CXXLiteralOperatorName.OpNameLoc 8658 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 8659 break; 8660 8661 case DeclarationName::Identifier: 8662 case DeclarationName::ObjCZeroArgSelector: 8663 case DeclarationName::ObjCOneArgSelector: 8664 case DeclarationName::ObjCMultiArgSelector: 8665 case DeclarationName::CXXUsingDirective: 8666 case DeclarationName::CXXDeductionGuideName: 8667 break; 8668 } 8669 } 8670 8671 void ASTReader::ReadDeclarationNameInfo(ModuleFile &F, 8672 DeclarationNameInfo &NameInfo, 8673 const RecordData &Record, unsigned &Idx) { 8674 NameInfo.setName(ReadDeclarationName(F, Record, Idx)); 8675 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx)); 8676 DeclarationNameLoc DNLoc; 8677 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx); 8678 NameInfo.setInfo(DNLoc); 8679 } 8680 8681 void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info, 8682 const RecordData &Record, unsigned &Idx) { 8683 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx); 8684 unsigned NumTPLists = Record[Idx++]; 8685 Info.NumTemplParamLists = NumTPLists; 8686 if (NumTPLists) { 8687 Info.TemplParamLists = 8688 new (getContext()) TemplateParameterList *[NumTPLists]; 8689 for (unsigned i = 0; i != NumTPLists; ++i) 8690 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx); 8691 } 8692 } 8693 8694 TemplateName 8695 ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record, 8696 unsigned &Idx) { 8697 ASTContext &Context = getContext(); 8698 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++]; 8699 switch (Kind) { 8700 case TemplateName::Template: 8701 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx)); 8702 8703 case TemplateName::OverloadedTemplate: { 8704 unsigned size = Record[Idx++]; 8705 UnresolvedSet<8> Decls; 8706 while (size--) 8707 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx)); 8708 8709 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end()); 8710 } 8711 8712 case TemplateName::QualifiedTemplate: { 8713 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx); 8714 bool hasTemplKeyword = Record[Idx++]; 8715 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx); 8716 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template); 8717 } 8718 8719 case TemplateName::DependentTemplate: { 8720 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx); 8721 if (Record[Idx++]) // isIdentifier 8722 return Context.getDependentTemplateName(NNS, 8723 GetIdentifierInfo(F, Record, 8724 Idx)); 8725 return Context.getDependentTemplateName(NNS, 8726 (OverloadedOperatorKind)Record[Idx++]); 8727 } 8728 8729 case TemplateName::SubstTemplateTemplateParm: { 8730 TemplateTemplateParmDecl *param 8731 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx); 8732 if (!param) return TemplateName(); 8733 TemplateName replacement = ReadTemplateName(F, Record, Idx); 8734 return Context.getSubstTemplateTemplateParm(param, replacement); 8735 } 8736 8737 case TemplateName::SubstTemplateTemplateParmPack: { 8738 TemplateTemplateParmDecl *Param 8739 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx); 8740 if (!Param) 8741 return TemplateName(); 8742 8743 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx); 8744 if (ArgPack.getKind() != TemplateArgument::Pack) 8745 return TemplateName(); 8746 8747 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack); 8748 } 8749 } 8750 8751 llvm_unreachable("Unhandled template name kind!"); 8752 } 8753 8754 TemplateArgument ASTReader::ReadTemplateArgument(ModuleFile &F, 8755 const RecordData &Record, 8756 unsigned &Idx, 8757 bool Canonicalize) { 8758 ASTContext &Context = getContext(); 8759 if (Canonicalize) { 8760 // The caller wants a canonical template argument. Sometimes the AST only 8761 // wants template arguments in canonical form (particularly as the template 8762 // argument lists of template specializations) so ensure we preserve that 8763 // canonical form across serialization. 8764 TemplateArgument Arg = ReadTemplateArgument(F, Record, Idx, false); 8765 return Context.getCanonicalTemplateArgument(Arg); 8766 } 8767 8768 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++]; 8769 switch (Kind) { 8770 case TemplateArgument::Null: 8771 return TemplateArgument(); 8772 case TemplateArgument::Type: 8773 return TemplateArgument(readType(F, Record, Idx)); 8774 case TemplateArgument::Declaration: { 8775 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx); 8776 return TemplateArgument(D, readType(F, Record, Idx)); 8777 } 8778 case TemplateArgument::NullPtr: 8779 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true); 8780 case TemplateArgument::Integral: { 8781 llvm::APSInt Value = ReadAPSInt(Record, Idx); 8782 QualType T = readType(F, Record, Idx); 8783 return TemplateArgument(Context, Value, T); 8784 } 8785 case TemplateArgument::Template: 8786 return TemplateArgument(ReadTemplateName(F, Record, Idx)); 8787 case TemplateArgument::TemplateExpansion: { 8788 TemplateName Name = ReadTemplateName(F, Record, Idx); 8789 Optional<unsigned> NumTemplateExpansions; 8790 if (unsigned NumExpansions = Record[Idx++]) 8791 NumTemplateExpansions = NumExpansions - 1; 8792 return TemplateArgument(Name, NumTemplateExpansions); 8793 } 8794 case TemplateArgument::Expression: 8795 return TemplateArgument(ReadExpr(F)); 8796 case TemplateArgument::Pack: { 8797 unsigned NumArgs = Record[Idx++]; 8798 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs]; 8799 for (unsigned I = 0; I != NumArgs; ++I) 8800 Args[I] = ReadTemplateArgument(F, Record, Idx); 8801 return TemplateArgument(llvm::makeArrayRef(Args, NumArgs)); 8802 } 8803 } 8804 8805 llvm_unreachable("Unhandled template argument kind!"); 8806 } 8807 8808 TemplateParameterList * 8809 ASTReader::ReadTemplateParameterList(ModuleFile &F, 8810 const RecordData &Record, unsigned &Idx) { 8811 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx); 8812 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx); 8813 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx); 8814 8815 unsigned NumParams = Record[Idx++]; 8816 SmallVector<NamedDecl *, 16> Params; 8817 Params.reserve(NumParams); 8818 while (NumParams--) 8819 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx)); 8820 8821 // TODO: Concepts 8822 TemplateParameterList *TemplateParams = TemplateParameterList::Create( 8823 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, nullptr); 8824 return TemplateParams; 8825 } 8826 8827 void 8828 ASTReader:: 8829 ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs, 8830 ModuleFile &F, const RecordData &Record, 8831 unsigned &Idx, bool Canonicalize) { 8832 unsigned NumTemplateArgs = Record[Idx++]; 8833 TemplArgs.reserve(NumTemplateArgs); 8834 while (NumTemplateArgs--) 8835 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx, Canonicalize)); 8836 } 8837 8838 /// Read a UnresolvedSet structure. 8839 void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set, 8840 const RecordData &Record, unsigned &Idx) { 8841 unsigned NumDecls = Record[Idx++]; 8842 Set.reserve(getContext(), NumDecls); 8843 while (NumDecls--) { 8844 DeclID ID = ReadDeclID(F, Record, Idx); 8845 AccessSpecifier AS = (AccessSpecifier)Record[Idx++]; 8846 Set.addLazyDecl(getContext(), ID, AS); 8847 } 8848 } 8849 8850 CXXBaseSpecifier 8851 ASTReader::ReadCXXBaseSpecifier(ModuleFile &F, 8852 const RecordData &Record, unsigned &Idx) { 8853 bool isVirtual = static_cast<bool>(Record[Idx++]); 8854 bool isBaseOfClass = static_cast<bool>(Record[Idx++]); 8855 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]); 8856 bool inheritConstructors = static_cast<bool>(Record[Idx++]); 8857 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx); 8858 SourceRange Range = ReadSourceRange(F, Record, Idx); 8859 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx); 8860 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo, 8861 EllipsisLoc); 8862 Result.setInheritConstructors(inheritConstructors); 8863 return Result; 8864 } 8865 8866 CXXCtorInitializer ** 8867 ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record, 8868 unsigned &Idx) { 8869 ASTContext &Context = getContext(); 8870 unsigned NumInitializers = Record[Idx++]; 8871 assert(NumInitializers && "wrote ctor initializers but have no inits"); 8872 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers]; 8873 for (unsigned i = 0; i != NumInitializers; ++i) { 8874 TypeSourceInfo *TInfo = nullptr; 8875 bool IsBaseVirtual = false; 8876 FieldDecl *Member = nullptr; 8877 IndirectFieldDecl *IndirectMember = nullptr; 8878 8879 CtorInitializerType Type = (CtorInitializerType)Record[Idx++]; 8880 switch (Type) { 8881 case CTOR_INITIALIZER_BASE: 8882 TInfo = GetTypeSourceInfo(F, Record, Idx); 8883 IsBaseVirtual = Record[Idx++]; 8884 break; 8885 8886 case CTOR_INITIALIZER_DELEGATING: 8887 TInfo = GetTypeSourceInfo(F, Record, Idx); 8888 break; 8889 8890 case CTOR_INITIALIZER_MEMBER: 8891 Member = ReadDeclAs<FieldDecl>(F, Record, Idx); 8892 break; 8893 8894 case CTOR_INITIALIZER_INDIRECT_MEMBER: 8895 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx); 8896 break; 8897 } 8898 8899 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx); 8900 Expr *Init = ReadExpr(F); 8901 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx); 8902 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx); 8903 8904 CXXCtorInitializer *BOMInit; 8905 if (Type == CTOR_INITIALIZER_BASE) 8906 BOMInit = new (Context) 8907 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init, 8908 RParenLoc, MemberOrEllipsisLoc); 8909 else if (Type == CTOR_INITIALIZER_DELEGATING) 8910 BOMInit = new (Context) 8911 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc); 8912 else if (Member) 8913 BOMInit = new (Context) 8914 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc, 8915 Init, RParenLoc); 8916 else 8917 BOMInit = new (Context) 8918 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc, 8919 LParenLoc, Init, RParenLoc); 8920 8921 if (/*IsWritten*/Record[Idx++]) { 8922 unsigned SourceOrder = Record[Idx++]; 8923 BOMInit->setSourceOrder(SourceOrder); 8924 } 8925 8926 CtorInitializers[i] = BOMInit; 8927 } 8928 8929 return CtorInitializers; 8930 } 8931 8932 NestedNameSpecifier * 8933 ASTReader::ReadNestedNameSpecifier(ModuleFile &F, 8934 const RecordData &Record, unsigned &Idx) { 8935 ASTContext &Context = getContext(); 8936 unsigned N = Record[Idx++]; 8937 NestedNameSpecifier *NNS = nullptr, *Prev = nullptr; 8938 for (unsigned I = 0; I != N; ++I) { 8939 NestedNameSpecifier::SpecifierKind Kind 8940 = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; 8941 switch (Kind) { 8942 case NestedNameSpecifier::Identifier: { 8943 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx); 8944 NNS = NestedNameSpecifier::Create(Context, Prev, II); 8945 break; 8946 } 8947 8948 case NestedNameSpecifier::Namespace: { 8949 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx); 8950 NNS = NestedNameSpecifier::Create(Context, Prev, NS); 8951 break; 8952 } 8953 8954 case NestedNameSpecifier::NamespaceAlias: { 8955 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx); 8956 NNS = NestedNameSpecifier::Create(Context, Prev, Alias); 8957 break; 8958 } 8959 8960 case NestedNameSpecifier::TypeSpec: 8961 case NestedNameSpecifier::TypeSpecWithTemplate: { 8962 const Type *T = readType(F, Record, Idx).getTypePtrOrNull(); 8963 if (!T) 8964 return nullptr; 8965 8966 bool Template = Record[Idx++]; 8967 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T); 8968 break; 8969 } 8970 8971 case NestedNameSpecifier::Global: 8972 NNS = NestedNameSpecifier::GlobalSpecifier(Context); 8973 // No associated value, and there can't be a prefix. 8974 break; 8975 8976 case NestedNameSpecifier::Super: { 8977 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx); 8978 NNS = NestedNameSpecifier::SuperSpecifier(Context, RD); 8979 break; 8980 } 8981 } 8982 Prev = NNS; 8983 } 8984 return NNS; 8985 } 8986 8987 NestedNameSpecifierLoc 8988 ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record, 8989 unsigned &Idx) { 8990 ASTContext &Context = getContext(); 8991 unsigned N = Record[Idx++]; 8992 NestedNameSpecifierLocBuilder Builder; 8993 for (unsigned I = 0; I != N; ++I) { 8994 NestedNameSpecifier::SpecifierKind Kind 8995 = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; 8996 switch (Kind) { 8997 case NestedNameSpecifier::Identifier: { 8998 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx); 8999 SourceRange Range = ReadSourceRange(F, Record, Idx); 9000 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd()); 9001 break; 9002 } 9003 9004 case NestedNameSpecifier::Namespace: { 9005 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx); 9006 SourceRange Range = ReadSourceRange(F, Record, Idx); 9007 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd()); 9008 break; 9009 } 9010 9011 case NestedNameSpecifier::NamespaceAlias: { 9012 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx); 9013 SourceRange Range = ReadSourceRange(F, Record, Idx); 9014 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd()); 9015 break; 9016 } 9017 9018 case NestedNameSpecifier::TypeSpec: 9019 case NestedNameSpecifier::TypeSpecWithTemplate: { 9020 bool Template = Record[Idx++]; 9021 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx); 9022 if (!T) 9023 return NestedNameSpecifierLoc(); 9024 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx); 9025 9026 // FIXME: 'template' keyword location not saved anywhere, so we fake it. 9027 Builder.Extend(Context, 9028 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(), 9029 T->getTypeLoc(), ColonColonLoc); 9030 break; 9031 } 9032 9033 case NestedNameSpecifier::Global: { 9034 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx); 9035 Builder.MakeGlobal(Context, ColonColonLoc); 9036 break; 9037 } 9038 9039 case NestedNameSpecifier::Super: { 9040 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx); 9041 SourceRange Range = ReadSourceRange(F, Record, Idx); 9042 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd()); 9043 break; 9044 } 9045 } 9046 } 9047 9048 return Builder.getWithLocInContext(Context); 9049 } 9050 9051 SourceRange 9052 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record, 9053 unsigned &Idx) { 9054 SourceLocation beg = ReadSourceLocation(F, Record, Idx); 9055 SourceLocation end = ReadSourceLocation(F, Record, Idx); 9056 return SourceRange(beg, end); 9057 } 9058 9059 /// Read an integral value 9060 llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) { 9061 unsigned BitWidth = Record[Idx++]; 9062 unsigned NumWords = llvm::APInt::getNumWords(BitWidth); 9063 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]); 9064 Idx += NumWords; 9065 return Result; 9066 } 9067 9068 /// Read a signed integral value 9069 llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) { 9070 bool isUnsigned = Record[Idx++]; 9071 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned); 9072 } 9073 9074 /// Read a floating-point value 9075 llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, 9076 const llvm::fltSemantics &Sem, 9077 unsigned &Idx) { 9078 return llvm::APFloat(Sem, ReadAPInt(Record, Idx)); 9079 } 9080 9081 // Read a string 9082 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) { 9083 unsigned Len = Record[Idx++]; 9084 std::string Result(Record.data() + Idx, Record.data() + Idx + Len); 9085 Idx += Len; 9086 return Result; 9087 } 9088 9089 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record, 9090 unsigned &Idx) { 9091 std::string Filename = ReadString(Record, Idx); 9092 ResolveImportedPath(F, Filename); 9093 return Filename; 9094 } 9095 9096 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record, 9097 unsigned &Idx) { 9098 unsigned Major = Record[Idx++]; 9099 unsigned Minor = Record[Idx++]; 9100 unsigned Subminor = Record[Idx++]; 9101 if (Minor == 0) 9102 return VersionTuple(Major); 9103 if (Subminor == 0) 9104 return VersionTuple(Major, Minor - 1); 9105 return VersionTuple(Major, Minor - 1, Subminor - 1); 9106 } 9107 9108 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F, 9109 const RecordData &Record, 9110 unsigned &Idx) { 9111 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx); 9112 return CXXTemporary::Create(getContext(), Decl); 9113 } 9114 9115 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const { 9116 return Diag(CurrentImportLoc, DiagID); 9117 } 9118 9119 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const { 9120 return Diags.Report(Loc, DiagID); 9121 } 9122 9123 /// Retrieve the identifier table associated with the 9124 /// preprocessor. 9125 IdentifierTable &ASTReader::getIdentifierTable() { 9126 return PP.getIdentifierTable(); 9127 } 9128 9129 /// Record that the given ID maps to the given switch-case 9130 /// statement. 9131 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { 9132 assert((*CurrSwitchCaseStmts)[ID] == nullptr && 9133 "Already have a SwitchCase with this ID"); 9134 (*CurrSwitchCaseStmts)[ID] = SC; 9135 } 9136 9137 /// Retrieve the switch-case statement with the given ID. 9138 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) { 9139 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID"); 9140 return (*CurrSwitchCaseStmts)[ID]; 9141 } 9142 9143 void ASTReader::ClearSwitchCaseIDs() { 9144 CurrSwitchCaseStmts->clear(); 9145 } 9146 9147 void ASTReader::ReadComments() { 9148 ASTContext &Context = getContext(); 9149 std::vector<RawComment *> Comments; 9150 for (SmallVectorImpl<std::pair<BitstreamCursor, 9151 serialization::ModuleFile *>>::iterator 9152 I = CommentsCursors.begin(), 9153 E = CommentsCursors.end(); 9154 I != E; ++I) { 9155 Comments.clear(); 9156 BitstreamCursor &Cursor = I->first; 9157 serialization::ModuleFile &F = *I->second; 9158 SavedStreamPosition SavedPosition(Cursor); 9159 9160 RecordData Record; 9161 while (true) { 9162 llvm::BitstreamEntry Entry = 9163 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd); 9164 9165 switch (Entry.Kind) { 9166 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 9167 case llvm::BitstreamEntry::Error: 9168 Error("malformed block record in AST file"); 9169 return; 9170 case llvm::BitstreamEntry::EndBlock: 9171 goto NextCursor; 9172 case llvm::BitstreamEntry::Record: 9173 // The interesting case. 9174 break; 9175 } 9176 9177 // Read a record. 9178 Record.clear(); 9179 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) { 9180 case COMMENTS_RAW_COMMENT: { 9181 unsigned Idx = 0; 9182 SourceRange SR = ReadSourceRange(F, Record, Idx); 9183 RawComment::CommentKind Kind = 9184 (RawComment::CommentKind) Record[Idx++]; 9185 bool IsTrailingComment = Record[Idx++]; 9186 bool IsAlmostTrailingComment = Record[Idx++]; 9187 Comments.push_back(new (Context) RawComment( 9188 SR, Kind, IsTrailingComment, IsAlmostTrailingComment)); 9189 break; 9190 } 9191 } 9192 } 9193 NextCursor: 9194 // De-serialized SourceLocations get negative FileIDs for other modules, 9195 // potentially invalidating the original order. Sort it again. 9196 llvm::sort(Comments, BeforeThanCompare<RawComment>(SourceMgr)); 9197 Context.Comments.addDeserializedComments(Comments); 9198 } 9199 } 9200 9201 void ASTReader::visitInputFiles(serialization::ModuleFile &MF, 9202 bool IncludeSystem, bool Complain, 9203 llvm::function_ref<void(const serialization::InputFile &IF, 9204 bool isSystem)> Visitor) { 9205 unsigned NumUserInputs = MF.NumUserInputFiles; 9206 unsigned NumInputs = MF.InputFilesLoaded.size(); 9207 assert(NumUserInputs <= NumInputs); 9208 unsigned N = IncludeSystem ? NumInputs : NumUserInputs; 9209 for (unsigned I = 0; I < N; ++I) { 9210 bool IsSystem = I >= NumUserInputs; 9211 InputFile IF = getInputFile(MF, I+1, Complain); 9212 Visitor(IF, IsSystem); 9213 } 9214 } 9215 9216 void ASTReader::visitTopLevelModuleMaps( 9217 serialization::ModuleFile &MF, 9218 llvm::function_ref<void(const FileEntry *FE)> Visitor) { 9219 unsigned NumInputs = MF.InputFilesLoaded.size(); 9220 for (unsigned I = 0; I < NumInputs; ++I) { 9221 InputFileInfo IFI = readInputFileInfo(MF, I + 1); 9222 if (IFI.TopLevelModuleMap) 9223 // FIXME: This unnecessarily re-reads the InputFileInfo. 9224 if (auto *FE = getInputFile(MF, I + 1).getFile()) 9225 Visitor(FE); 9226 } 9227 } 9228 9229 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) { 9230 // If we know the owning module, use it. 9231 if (Module *M = D->getImportedOwningModule()) 9232 return M->getFullModuleName(); 9233 9234 // Otherwise, use the name of the top-level module the decl is within. 9235 if (ModuleFile *M = getOwningModuleFile(D)) 9236 return M->ModuleName; 9237 9238 // Not from a module. 9239 return {}; 9240 } 9241 9242 void ASTReader::finishPendingActions() { 9243 while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() || 9244 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() || 9245 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() || 9246 !PendingUpdateRecords.empty()) { 9247 // If any identifiers with corresponding top-level declarations have 9248 // been loaded, load those declarations now. 9249 using TopLevelDeclsMap = 9250 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>; 9251 TopLevelDeclsMap TopLevelDecls; 9252 9253 while (!PendingIdentifierInfos.empty()) { 9254 IdentifierInfo *II = PendingIdentifierInfos.back().first; 9255 SmallVector<uint32_t, 4> DeclIDs = 9256 std::move(PendingIdentifierInfos.back().second); 9257 PendingIdentifierInfos.pop_back(); 9258 9259 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]); 9260 } 9261 9262 // Load each function type that we deferred loading because it was a 9263 // deduced type that might refer to a local type declared within itself. 9264 for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) { 9265 auto *FD = PendingFunctionTypes[I].first; 9266 FD->setType(GetType(PendingFunctionTypes[I].second)); 9267 9268 // If we gave a function a deduced return type, remember that we need to 9269 // propagate that along the redeclaration chain. 9270 auto *DT = FD->getReturnType()->getContainedDeducedType(); 9271 if (DT && DT->isDeduced()) 9272 PendingDeducedTypeUpdates.insert( 9273 {FD->getCanonicalDecl(), FD->getReturnType()}); 9274 } 9275 PendingFunctionTypes.clear(); 9276 9277 // For each decl chain that we wanted to complete while deserializing, mark 9278 // it as "still needs to be completed". 9279 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) { 9280 markIncompleteDeclChain(PendingIncompleteDeclChains[I]); 9281 } 9282 PendingIncompleteDeclChains.clear(); 9283 9284 // Load pending declaration chains. 9285 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) 9286 loadPendingDeclChain(PendingDeclChains[I].first, 9287 PendingDeclChains[I].second); 9288 PendingDeclChains.clear(); 9289 9290 // Make the most recent of the top-level declarations visible. 9291 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(), 9292 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) { 9293 IdentifierInfo *II = TLD->first; 9294 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) { 9295 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II); 9296 } 9297 } 9298 9299 // Load any pending macro definitions. 9300 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) { 9301 IdentifierInfo *II = PendingMacroIDs.begin()[I].first; 9302 SmallVector<PendingMacroInfo, 2> GlobalIDs; 9303 GlobalIDs.swap(PendingMacroIDs.begin()[I].second); 9304 // Initialize the macro history from chained-PCHs ahead of module imports. 9305 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9306 ++IDIdx) { 9307 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9308 if (!Info.M->isModule()) 9309 resolvePendingMacro(II, Info); 9310 } 9311 // Handle module imports. 9312 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9313 ++IDIdx) { 9314 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9315 if (Info.M->isModule()) 9316 resolvePendingMacro(II, Info); 9317 } 9318 } 9319 PendingMacroIDs.clear(); 9320 9321 // Wire up the DeclContexts for Decls that we delayed setting until 9322 // recursive loading is completed. 9323 while (!PendingDeclContextInfos.empty()) { 9324 PendingDeclContextInfo Info = PendingDeclContextInfos.front(); 9325 PendingDeclContextInfos.pop_front(); 9326 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC)); 9327 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC)); 9328 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext()); 9329 } 9330 9331 // Perform any pending declaration updates. 9332 while (!PendingUpdateRecords.empty()) { 9333 auto Update = PendingUpdateRecords.pop_back_val(); 9334 ReadingKindTracker ReadingKind(Read_Decl, *this); 9335 loadDeclUpdateRecords(Update); 9336 } 9337 } 9338 9339 // At this point, all update records for loaded decls are in place, so any 9340 // fake class definitions should have become real. 9341 assert(PendingFakeDefinitionData.empty() && 9342 "faked up a class definition but never saw the real one"); 9343 9344 // If we deserialized any C++ or Objective-C class definitions, any 9345 // Objective-C protocol definitions, or any redeclarable templates, make sure 9346 // that all redeclarations point to the definitions. Note that this can only 9347 // happen now, after the redeclaration chains have been fully wired. 9348 for (Decl *D : PendingDefinitions) { 9349 if (TagDecl *TD = dyn_cast<TagDecl>(D)) { 9350 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) { 9351 // Make sure that the TagType points at the definition. 9352 const_cast<TagType*>(TagT)->decl = TD; 9353 } 9354 9355 if (auto RD = dyn_cast<CXXRecordDecl>(D)) { 9356 for (auto *R = getMostRecentExistingDecl(RD); R; 9357 R = R->getPreviousDecl()) { 9358 assert((R == D) == 9359 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() && 9360 "declaration thinks it's the definition but it isn't"); 9361 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData; 9362 } 9363 } 9364 9365 continue; 9366 } 9367 9368 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) { 9369 // Make sure that the ObjCInterfaceType points at the definition. 9370 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl)) 9371 ->Decl = ID; 9372 9373 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl()) 9374 cast<ObjCInterfaceDecl>(R)->Data = ID->Data; 9375 9376 continue; 9377 } 9378 9379 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) { 9380 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl()) 9381 cast<ObjCProtocolDecl>(R)->Data = PD->Data; 9382 9383 continue; 9384 } 9385 9386 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl(); 9387 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl()) 9388 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common; 9389 } 9390 PendingDefinitions.clear(); 9391 9392 // Load the bodies of any functions or methods we've encountered. We do 9393 // this now (delayed) so that we can be sure that the declaration chains 9394 // have been fully wired up (hasBody relies on this). 9395 // FIXME: We shouldn't require complete redeclaration chains here. 9396 for (PendingBodiesMap::iterator PB = PendingBodies.begin(), 9397 PBEnd = PendingBodies.end(); 9398 PB != PBEnd; ++PB) { 9399 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) { 9400 // For a function defined inline within a class template, force the 9401 // canonical definition to be the one inside the canonical definition of 9402 // the template. This ensures that we instantiate from a correct view 9403 // of the template. 9404 // 9405 // Sadly we can't do this more generally: we can't be sure that all 9406 // copies of an arbitrary class definition will have the same members 9407 // defined (eg, some member functions may not be instantiated, and some 9408 // special members may or may not have been implicitly defined). 9409 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent())) 9410 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition()) 9411 continue; 9412 9413 // FIXME: Check for =delete/=default? 9414 // FIXME: Complain about ODR violations here? 9415 const FunctionDecl *Defn = nullptr; 9416 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) { 9417 FD->setLazyBody(PB->second); 9418 } else { 9419 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn); 9420 mergeDefinitionVisibility(NonConstDefn, FD); 9421 9422 if (!FD->isLateTemplateParsed() && 9423 !NonConstDefn->isLateTemplateParsed() && 9424 FD->getODRHash() != NonConstDefn->getODRHash()) { 9425 if (!isa<CXXMethodDecl>(FD)) { 9426 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9427 } else if (FD->getLexicalParent()->isFileContext() && 9428 NonConstDefn->getLexicalParent()->isFileContext()) { 9429 // Only diagnose out-of-line method definitions. If they are 9430 // in class definitions, then an error will be generated when 9431 // processing the class bodies. 9432 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9433 } 9434 } 9435 } 9436 continue; 9437 } 9438 9439 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first); 9440 if (!getContext().getLangOpts().Modules || !MD->hasBody()) 9441 MD->setLazyBody(PB->second); 9442 } 9443 PendingBodies.clear(); 9444 9445 // Do some cleanup. 9446 for (auto *ND : PendingMergedDefinitionsToDeduplicate) 9447 getContext().deduplicateMergedDefinitonsFor(ND); 9448 PendingMergedDefinitionsToDeduplicate.clear(); 9449 } 9450 9451 void ASTReader::diagnoseOdrViolations() { 9452 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() && 9453 PendingFunctionOdrMergeFailures.empty() && 9454 PendingEnumOdrMergeFailures.empty()) 9455 return; 9456 9457 // Trigger the import of the full definition of each class that had any 9458 // odr-merging problems, so we can produce better diagnostics for them. 9459 // These updates may in turn find and diagnose some ODR failures, so take 9460 // ownership of the set first. 9461 auto OdrMergeFailures = std::move(PendingOdrMergeFailures); 9462 PendingOdrMergeFailures.clear(); 9463 for (auto &Merge : OdrMergeFailures) { 9464 Merge.first->buildLookup(); 9465 Merge.first->decls_begin(); 9466 Merge.first->bases_begin(); 9467 Merge.first->vbases_begin(); 9468 for (auto &RecordPair : Merge.second) { 9469 auto *RD = RecordPair.first; 9470 RD->decls_begin(); 9471 RD->bases_begin(); 9472 RD->vbases_begin(); 9473 } 9474 } 9475 9476 // Trigger the import of functions. 9477 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures); 9478 PendingFunctionOdrMergeFailures.clear(); 9479 for (auto &Merge : FunctionOdrMergeFailures) { 9480 Merge.first->buildLookup(); 9481 Merge.first->decls_begin(); 9482 Merge.first->getBody(); 9483 for (auto &FD : Merge.second) { 9484 FD->buildLookup(); 9485 FD->decls_begin(); 9486 FD->getBody(); 9487 } 9488 } 9489 9490 // Trigger the import of enums. 9491 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures); 9492 PendingEnumOdrMergeFailures.clear(); 9493 for (auto &Merge : EnumOdrMergeFailures) { 9494 Merge.first->decls_begin(); 9495 for (auto &Enum : Merge.second) { 9496 Enum->decls_begin(); 9497 } 9498 } 9499 9500 // For each declaration from a merged context, check that the canonical 9501 // definition of that context also contains a declaration of the same 9502 // entity. 9503 // 9504 // Caution: this loop does things that might invalidate iterators into 9505 // PendingOdrMergeChecks. Don't turn this into a range-based for loop! 9506 while (!PendingOdrMergeChecks.empty()) { 9507 NamedDecl *D = PendingOdrMergeChecks.pop_back_val(); 9508 9509 // FIXME: Skip over implicit declarations for now. This matters for things 9510 // like implicitly-declared special member functions. This isn't entirely 9511 // correct; we can end up with multiple unmerged declarations of the same 9512 // implicit entity. 9513 if (D->isImplicit()) 9514 continue; 9515 9516 DeclContext *CanonDef = D->getDeclContext(); 9517 9518 bool Found = false; 9519 const Decl *DCanon = D->getCanonicalDecl(); 9520 9521 for (auto RI : D->redecls()) { 9522 if (RI->getLexicalDeclContext() == CanonDef) { 9523 Found = true; 9524 break; 9525 } 9526 } 9527 if (Found) 9528 continue; 9529 9530 // Quick check failed, time to do the slow thing. Note, we can't just 9531 // look up the name of D in CanonDef here, because the member that is 9532 // in CanonDef might not be found by name lookup (it might have been 9533 // replaced by a more recent declaration in the lookup table), and we 9534 // can't necessarily find it in the redeclaration chain because it might 9535 // be merely mergeable, not redeclarable. 9536 llvm::SmallVector<const NamedDecl*, 4> Candidates; 9537 for (auto *CanonMember : CanonDef->decls()) { 9538 if (CanonMember->getCanonicalDecl() == DCanon) { 9539 // This can happen if the declaration is merely mergeable and not 9540 // actually redeclarable (we looked for redeclarations earlier). 9541 // 9542 // FIXME: We should be able to detect this more efficiently, without 9543 // pulling in all of the members of CanonDef. 9544 Found = true; 9545 break; 9546 } 9547 if (auto *ND = dyn_cast<NamedDecl>(CanonMember)) 9548 if (ND->getDeclName() == D->getDeclName()) 9549 Candidates.push_back(ND); 9550 } 9551 9552 if (!Found) { 9553 // The AST doesn't like TagDecls becoming invalid after they've been 9554 // completed. We only really need to mark FieldDecls as invalid here. 9555 if (!isa<TagDecl>(D)) 9556 D->setInvalidDecl(); 9557 9558 // Ensure we don't accidentally recursively enter deserialization while 9559 // we're producing our diagnostic. 9560 Deserializing RecursionGuard(this); 9561 9562 std::string CanonDefModule = 9563 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef)); 9564 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl) 9565 << D << getOwningModuleNameForDiagnostic(D) 9566 << CanonDef << CanonDefModule.empty() << CanonDefModule; 9567 9568 if (Candidates.empty()) 9569 Diag(cast<Decl>(CanonDef)->getLocation(), 9570 diag::note_module_odr_violation_no_possible_decls) << D; 9571 else { 9572 for (unsigned I = 0, N = Candidates.size(); I != N; ++I) 9573 Diag(Candidates[I]->getLocation(), 9574 diag::note_module_odr_violation_possible_decl) 9575 << Candidates[I]; 9576 } 9577 9578 DiagnosedOdrMergeFailures.insert(CanonDef); 9579 } 9580 } 9581 9582 if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() && 9583 EnumOdrMergeFailures.empty()) 9584 return; 9585 9586 // Ensure we don't accidentally recursively enter deserialization while 9587 // we're producing our diagnostics. 9588 Deserializing RecursionGuard(this); 9589 9590 // Common code for hashing helpers. 9591 ODRHash Hash; 9592 auto ComputeQualTypeODRHash = [&Hash](QualType Ty) { 9593 Hash.clear(); 9594 Hash.AddQualType(Ty); 9595 return Hash.CalculateHash(); 9596 }; 9597 9598 auto ComputeODRHash = [&Hash](const Stmt *S) { 9599 assert(S); 9600 Hash.clear(); 9601 Hash.AddStmt(S); 9602 return Hash.CalculateHash(); 9603 }; 9604 9605 auto ComputeSubDeclODRHash = [&Hash](const Decl *D) { 9606 assert(D); 9607 Hash.clear(); 9608 Hash.AddSubDecl(D); 9609 return Hash.CalculateHash(); 9610 }; 9611 9612 auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) { 9613 Hash.clear(); 9614 Hash.AddTemplateArgument(TA); 9615 return Hash.CalculateHash(); 9616 }; 9617 9618 auto ComputeTemplateParameterListODRHash = 9619 [&Hash](const TemplateParameterList *TPL) { 9620 assert(TPL); 9621 Hash.clear(); 9622 Hash.AddTemplateParameterList(TPL); 9623 return Hash.CalculateHash(); 9624 }; 9625 9626 // Issue any pending ODR-failure diagnostics. 9627 for (auto &Merge : OdrMergeFailures) { 9628 // If we've already pointed out a specific problem with this class, don't 9629 // bother issuing a general "something's different" diagnostic. 9630 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 9631 continue; 9632 9633 bool Diagnosed = false; 9634 CXXRecordDecl *FirstRecord = Merge.first; 9635 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord); 9636 for (auto &RecordPair : Merge.second) { 9637 CXXRecordDecl *SecondRecord = RecordPair.first; 9638 // Multiple different declarations got merged together; tell the user 9639 // where they came from. 9640 if (FirstRecord == SecondRecord) 9641 continue; 9642 9643 std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord); 9644 9645 auto *FirstDD = FirstRecord->DefinitionData; 9646 auto *SecondDD = RecordPair.second; 9647 9648 assert(FirstDD && SecondDD && "Definitions without DefinitionData"); 9649 9650 // Diagnostics from DefinitionData are emitted here. 9651 if (FirstDD != SecondDD) { 9652 enum ODRDefinitionDataDifference { 9653 NumBases, 9654 NumVBases, 9655 BaseType, 9656 BaseVirtual, 9657 BaseAccess, 9658 }; 9659 auto ODRDiagError = [FirstRecord, &FirstModule, 9660 this](SourceLocation Loc, SourceRange Range, 9661 ODRDefinitionDataDifference DiffType) { 9662 return Diag(Loc, diag::err_module_odr_violation_definition_data) 9663 << FirstRecord << FirstModule.empty() << FirstModule << Range 9664 << DiffType; 9665 }; 9666 auto ODRDiagNote = [&SecondModule, 9667 this](SourceLocation Loc, SourceRange Range, 9668 ODRDefinitionDataDifference DiffType) { 9669 return Diag(Loc, diag::note_module_odr_violation_definition_data) 9670 << SecondModule << Range << DiffType; 9671 }; 9672 9673 unsigned FirstNumBases = FirstDD->NumBases; 9674 unsigned FirstNumVBases = FirstDD->NumVBases; 9675 unsigned SecondNumBases = SecondDD->NumBases; 9676 unsigned SecondNumVBases = SecondDD->NumVBases; 9677 9678 auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) { 9679 unsigned NumBases = DD->NumBases; 9680 if (NumBases == 0) return SourceRange(); 9681 auto bases = DD->bases(); 9682 return SourceRange(bases[0].getBeginLoc(), 9683 bases[NumBases - 1].getEndLoc()); 9684 }; 9685 9686 if (FirstNumBases != SecondNumBases) { 9687 ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 9688 NumBases) 9689 << FirstNumBases; 9690 ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 9691 NumBases) 9692 << SecondNumBases; 9693 Diagnosed = true; 9694 break; 9695 } 9696 9697 if (FirstNumVBases != SecondNumVBases) { 9698 ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 9699 NumVBases) 9700 << FirstNumVBases; 9701 ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 9702 NumVBases) 9703 << SecondNumVBases; 9704 Diagnosed = true; 9705 break; 9706 } 9707 9708 auto FirstBases = FirstDD->bases(); 9709 auto SecondBases = SecondDD->bases(); 9710 unsigned i = 0; 9711 for (i = 0; i < FirstNumBases; ++i) { 9712 auto FirstBase = FirstBases[i]; 9713 auto SecondBase = SecondBases[i]; 9714 if (ComputeQualTypeODRHash(FirstBase.getType()) != 9715 ComputeQualTypeODRHash(SecondBase.getType())) { 9716 ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(), 9717 BaseType) 9718 << (i + 1) << FirstBase.getType(); 9719 ODRDiagNote(SecondRecord->getLocation(), 9720 SecondBase.getSourceRange(), BaseType) 9721 << (i + 1) << SecondBase.getType(); 9722 break; 9723 } 9724 9725 if (FirstBase.isVirtual() != SecondBase.isVirtual()) { 9726 ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(), 9727 BaseVirtual) 9728 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType(); 9729 ODRDiagNote(SecondRecord->getLocation(), 9730 SecondBase.getSourceRange(), BaseVirtual) 9731 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType(); 9732 break; 9733 } 9734 9735 if (FirstBase.getAccessSpecifierAsWritten() != 9736 SecondBase.getAccessSpecifierAsWritten()) { 9737 ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(), 9738 BaseAccess) 9739 << (i + 1) << FirstBase.getType() 9740 << (int)FirstBase.getAccessSpecifierAsWritten(); 9741 ODRDiagNote(SecondRecord->getLocation(), 9742 SecondBase.getSourceRange(), BaseAccess) 9743 << (i + 1) << SecondBase.getType() 9744 << (int)SecondBase.getAccessSpecifierAsWritten(); 9745 break; 9746 } 9747 } 9748 9749 if (i != FirstNumBases) { 9750 Diagnosed = true; 9751 break; 9752 } 9753 } 9754 9755 using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>; 9756 9757 const ClassTemplateDecl *FirstTemplate = 9758 FirstRecord->getDescribedClassTemplate(); 9759 const ClassTemplateDecl *SecondTemplate = 9760 SecondRecord->getDescribedClassTemplate(); 9761 9762 assert(!FirstTemplate == !SecondTemplate && 9763 "Both pointers should be null or non-null"); 9764 9765 enum ODRTemplateDifference { 9766 ParamEmptyName, 9767 ParamName, 9768 ParamSingleDefaultArgument, 9769 ParamDifferentDefaultArgument, 9770 }; 9771 9772 if (FirstTemplate && SecondTemplate) { 9773 DeclHashes FirstTemplateHashes; 9774 DeclHashes SecondTemplateHashes; 9775 9776 auto PopulateTemplateParameterHashs = 9777 [&ComputeSubDeclODRHash](DeclHashes &Hashes, 9778 const ClassTemplateDecl *TD) { 9779 for (auto *D : TD->getTemplateParameters()->asArray()) { 9780 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 9781 } 9782 }; 9783 9784 PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate); 9785 PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate); 9786 9787 assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() && 9788 "Number of template parameters should be equal."); 9789 9790 auto FirstIt = FirstTemplateHashes.begin(); 9791 auto FirstEnd = FirstTemplateHashes.end(); 9792 auto SecondIt = SecondTemplateHashes.begin(); 9793 for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) { 9794 if (FirstIt->second == SecondIt->second) 9795 continue; 9796 9797 auto ODRDiagError = [FirstRecord, &FirstModule, 9798 this](SourceLocation Loc, SourceRange Range, 9799 ODRTemplateDifference DiffType) { 9800 return Diag(Loc, diag::err_module_odr_violation_template_parameter) 9801 << FirstRecord << FirstModule.empty() << FirstModule << Range 9802 << DiffType; 9803 }; 9804 auto ODRDiagNote = [&SecondModule, 9805 this](SourceLocation Loc, SourceRange Range, 9806 ODRTemplateDifference DiffType) { 9807 return Diag(Loc, diag::note_module_odr_violation_template_parameter) 9808 << SecondModule << Range << DiffType; 9809 }; 9810 9811 const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first); 9812 const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first); 9813 9814 assert(FirstDecl->getKind() == SecondDecl->getKind() && 9815 "Parameter Decl's should be the same kind."); 9816 9817 DeclarationName FirstName = FirstDecl->getDeclName(); 9818 DeclarationName SecondName = SecondDecl->getDeclName(); 9819 9820 if (FirstName != SecondName) { 9821 const bool FirstNameEmpty = 9822 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo(); 9823 const bool SecondNameEmpty = 9824 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo(); 9825 assert((!FirstNameEmpty || !SecondNameEmpty) && 9826 "Both template parameters cannot be unnamed."); 9827 ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(), 9828 FirstNameEmpty ? ParamEmptyName : ParamName) 9829 << FirstName; 9830 ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(), 9831 SecondNameEmpty ? ParamEmptyName : ParamName) 9832 << SecondName; 9833 break; 9834 } 9835 9836 switch (FirstDecl->getKind()) { 9837 default: 9838 llvm_unreachable("Invalid template parameter type."); 9839 case Decl::TemplateTypeParm: { 9840 const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl); 9841 const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl); 9842 const bool HasFirstDefaultArgument = 9843 FirstParam->hasDefaultArgument() && 9844 !FirstParam->defaultArgumentWasInherited(); 9845 const bool HasSecondDefaultArgument = 9846 SecondParam->hasDefaultArgument() && 9847 !SecondParam->defaultArgumentWasInherited(); 9848 9849 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 9850 ODRDiagError(FirstDecl->getLocation(), 9851 FirstDecl->getSourceRange(), 9852 ParamSingleDefaultArgument) 9853 << HasFirstDefaultArgument; 9854 ODRDiagNote(SecondDecl->getLocation(), 9855 SecondDecl->getSourceRange(), 9856 ParamSingleDefaultArgument) 9857 << HasSecondDefaultArgument; 9858 break; 9859 } 9860 9861 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 9862 "Expecting default arguments."); 9863 9864 ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(), 9865 ParamDifferentDefaultArgument); 9866 ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(), 9867 ParamDifferentDefaultArgument); 9868 9869 break; 9870 } 9871 case Decl::NonTypeTemplateParm: { 9872 const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl); 9873 const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl); 9874 const bool HasFirstDefaultArgument = 9875 FirstParam->hasDefaultArgument() && 9876 !FirstParam->defaultArgumentWasInherited(); 9877 const bool HasSecondDefaultArgument = 9878 SecondParam->hasDefaultArgument() && 9879 !SecondParam->defaultArgumentWasInherited(); 9880 9881 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 9882 ODRDiagError(FirstDecl->getLocation(), 9883 FirstDecl->getSourceRange(), 9884 ParamSingleDefaultArgument) 9885 << HasFirstDefaultArgument; 9886 ODRDiagNote(SecondDecl->getLocation(), 9887 SecondDecl->getSourceRange(), 9888 ParamSingleDefaultArgument) 9889 << HasSecondDefaultArgument; 9890 break; 9891 } 9892 9893 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 9894 "Expecting default arguments."); 9895 9896 ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(), 9897 ParamDifferentDefaultArgument); 9898 ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(), 9899 ParamDifferentDefaultArgument); 9900 9901 break; 9902 } 9903 case Decl::TemplateTemplateParm: { 9904 const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl); 9905 const auto *SecondParam = 9906 cast<TemplateTemplateParmDecl>(SecondDecl); 9907 const bool HasFirstDefaultArgument = 9908 FirstParam->hasDefaultArgument() && 9909 !FirstParam->defaultArgumentWasInherited(); 9910 const bool HasSecondDefaultArgument = 9911 SecondParam->hasDefaultArgument() && 9912 !SecondParam->defaultArgumentWasInherited(); 9913 9914 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 9915 ODRDiagError(FirstDecl->getLocation(), 9916 FirstDecl->getSourceRange(), 9917 ParamSingleDefaultArgument) 9918 << HasFirstDefaultArgument; 9919 ODRDiagNote(SecondDecl->getLocation(), 9920 SecondDecl->getSourceRange(), 9921 ParamSingleDefaultArgument) 9922 << HasSecondDefaultArgument; 9923 break; 9924 } 9925 9926 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 9927 "Expecting default arguments."); 9928 9929 ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(), 9930 ParamDifferentDefaultArgument); 9931 ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(), 9932 ParamDifferentDefaultArgument); 9933 9934 break; 9935 } 9936 } 9937 9938 break; 9939 } 9940 9941 if (FirstIt != FirstEnd) { 9942 Diagnosed = true; 9943 break; 9944 } 9945 } 9946 9947 DeclHashes FirstHashes; 9948 DeclHashes SecondHashes; 9949 9950 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstRecord]( 9951 DeclHashes &Hashes, CXXRecordDecl *Record) { 9952 for (auto *D : Record->decls()) { 9953 // Due to decl merging, the first CXXRecordDecl is the parent of 9954 // Decls in both records. 9955 if (!ODRHash::isWhitelistedDecl(D, FirstRecord)) 9956 continue; 9957 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 9958 } 9959 }; 9960 PopulateHashes(FirstHashes, FirstRecord); 9961 PopulateHashes(SecondHashes, SecondRecord); 9962 9963 // Used with err_module_odr_violation_mismatch_decl and 9964 // note_module_odr_violation_mismatch_decl 9965 // This list should be the same Decl's as in ODRHash::isWhiteListedDecl 9966 enum { 9967 EndOfClass, 9968 PublicSpecifer, 9969 PrivateSpecifer, 9970 ProtectedSpecifer, 9971 StaticAssert, 9972 Field, 9973 CXXMethod, 9974 TypeAlias, 9975 TypeDef, 9976 Var, 9977 Friend, 9978 FunctionTemplate, 9979 Other 9980 } FirstDiffType = Other, 9981 SecondDiffType = Other; 9982 9983 auto DifferenceSelector = [](Decl *D) { 9984 assert(D && "valid Decl required"); 9985 switch (D->getKind()) { 9986 default: 9987 return Other; 9988 case Decl::AccessSpec: 9989 switch (D->getAccess()) { 9990 case AS_public: 9991 return PublicSpecifer; 9992 case AS_private: 9993 return PrivateSpecifer; 9994 case AS_protected: 9995 return ProtectedSpecifer; 9996 case AS_none: 9997 break; 9998 } 9999 llvm_unreachable("Invalid access specifier"); 10000 case Decl::StaticAssert: 10001 return StaticAssert; 10002 case Decl::Field: 10003 return Field; 10004 case Decl::CXXMethod: 10005 case Decl::CXXConstructor: 10006 case Decl::CXXDestructor: 10007 return CXXMethod; 10008 case Decl::TypeAlias: 10009 return TypeAlias; 10010 case Decl::Typedef: 10011 return TypeDef; 10012 case Decl::Var: 10013 return Var; 10014 case Decl::Friend: 10015 return Friend; 10016 case Decl::FunctionTemplate: 10017 return FunctionTemplate; 10018 } 10019 }; 10020 10021 Decl *FirstDecl = nullptr; 10022 Decl *SecondDecl = nullptr; 10023 auto FirstIt = FirstHashes.begin(); 10024 auto SecondIt = SecondHashes.begin(); 10025 10026 // If there is a diagnoseable difference, FirstDiffType and 10027 // SecondDiffType will not be Other and FirstDecl and SecondDecl will be 10028 // filled in if not EndOfClass. 10029 while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) { 10030 if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() && 10031 FirstIt->second == SecondIt->second) { 10032 ++FirstIt; 10033 ++SecondIt; 10034 continue; 10035 } 10036 10037 FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first; 10038 SecondDecl = SecondIt == SecondHashes.end() ? nullptr : SecondIt->first; 10039 10040 FirstDiffType = FirstDecl ? DifferenceSelector(FirstDecl) : EndOfClass; 10041 SecondDiffType = 10042 SecondDecl ? DifferenceSelector(SecondDecl) : EndOfClass; 10043 10044 break; 10045 } 10046 10047 if (FirstDiffType == Other || SecondDiffType == Other) { 10048 // Reaching this point means an unexpected Decl was encountered 10049 // or no difference was detected. This causes a generic error 10050 // message to be emitted. 10051 Diag(FirstRecord->getLocation(), 10052 diag::err_module_odr_violation_different_definitions) 10053 << FirstRecord << FirstModule.empty() << FirstModule; 10054 10055 if (FirstDecl) { 10056 Diag(FirstDecl->getLocation(), diag::note_first_module_difference) 10057 << FirstRecord << FirstDecl->getSourceRange(); 10058 } 10059 10060 Diag(SecondRecord->getLocation(), 10061 diag::note_module_odr_violation_different_definitions) 10062 << SecondModule; 10063 10064 if (SecondDecl) { 10065 Diag(SecondDecl->getLocation(), diag::note_second_module_difference) 10066 << SecondDecl->getSourceRange(); 10067 } 10068 10069 Diagnosed = true; 10070 break; 10071 } 10072 10073 if (FirstDiffType != SecondDiffType) { 10074 SourceLocation FirstLoc; 10075 SourceRange FirstRange; 10076 if (FirstDiffType == EndOfClass) { 10077 FirstLoc = FirstRecord->getBraceRange().getEnd(); 10078 } else { 10079 FirstLoc = FirstIt->first->getLocation(); 10080 FirstRange = FirstIt->first->getSourceRange(); 10081 } 10082 Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl) 10083 << FirstRecord << FirstModule.empty() << FirstModule << FirstRange 10084 << FirstDiffType; 10085 10086 SourceLocation SecondLoc; 10087 SourceRange SecondRange; 10088 if (SecondDiffType == EndOfClass) { 10089 SecondLoc = SecondRecord->getBraceRange().getEnd(); 10090 } else { 10091 SecondLoc = SecondDecl->getLocation(); 10092 SecondRange = SecondDecl->getSourceRange(); 10093 } 10094 Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl) 10095 << SecondModule << SecondRange << SecondDiffType; 10096 Diagnosed = true; 10097 break; 10098 } 10099 10100 assert(FirstDiffType == SecondDiffType); 10101 10102 // Used with err_module_odr_violation_mismatch_decl_diff and 10103 // note_module_odr_violation_mismatch_decl_diff 10104 enum ODRDeclDifference { 10105 StaticAssertCondition, 10106 StaticAssertMessage, 10107 StaticAssertOnlyMessage, 10108 FieldName, 10109 FieldTypeName, 10110 FieldSingleBitField, 10111 FieldDifferentWidthBitField, 10112 FieldSingleMutable, 10113 FieldSingleInitializer, 10114 FieldDifferentInitializers, 10115 MethodName, 10116 MethodDeleted, 10117 MethodDefaulted, 10118 MethodVirtual, 10119 MethodStatic, 10120 MethodVolatile, 10121 MethodConst, 10122 MethodInline, 10123 MethodNumberParameters, 10124 MethodParameterType, 10125 MethodParameterName, 10126 MethodParameterSingleDefaultArgument, 10127 MethodParameterDifferentDefaultArgument, 10128 MethodNoTemplateArguments, 10129 MethodDifferentNumberTemplateArguments, 10130 MethodDifferentTemplateArgument, 10131 MethodSingleBody, 10132 MethodDifferentBody, 10133 TypedefName, 10134 TypedefType, 10135 VarName, 10136 VarType, 10137 VarSingleInitializer, 10138 VarDifferentInitializer, 10139 VarConstexpr, 10140 FriendTypeFunction, 10141 FriendType, 10142 FriendFunction, 10143 FunctionTemplateDifferentNumberParameters, 10144 FunctionTemplateParameterDifferentKind, 10145 FunctionTemplateParameterName, 10146 FunctionTemplateParameterSingleDefaultArgument, 10147 FunctionTemplateParameterDifferentDefaultArgument, 10148 FunctionTemplateParameterDifferentType, 10149 FunctionTemplatePackParameter, 10150 }; 10151 10152 // These lambdas have the common portions of the ODR diagnostics. This 10153 // has the same return as Diag(), so addition parameters can be passed 10154 // in with operator<< 10155 auto ODRDiagError = [FirstRecord, &FirstModule, this]( 10156 SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) { 10157 return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff) 10158 << FirstRecord << FirstModule.empty() << FirstModule << Range 10159 << DiffType; 10160 }; 10161 auto ODRDiagNote = [&SecondModule, this]( 10162 SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) { 10163 return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff) 10164 << SecondModule << Range << DiffType; 10165 }; 10166 10167 switch (FirstDiffType) { 10168 case Other: 10169 case EndOfClass: 10170 case PublicSpecifer: 10171 case PrivateSpecifer: 10172 case ProtectedSpecifer: 10173 llvm_unreachable("Invalid diff type"); 10174 10175 case StaticAssert: { 10176 StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl); 10177 StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl); 10178 10179 Expr *FirstExpr = FirstSA->getAssertExpr(); 10180 Expr *SecondExpr = SecondSA->getAssertExpr(); 10181 unsigned FirstODRHash = ComputeODRHash(FirstExpr); 10182 unsigned SecondODRHash = ComputeODRHash(SecondExpr); 10183 if (FirstODRHash != SecondODRHash) { 10184 ODRDiagError(FirstExpr->getBeginLoc(), FirstExpr->getSourceRange(), 10185 StaticAssertCondition); 10186 ODRDiagNote(SecondExpr->getBeginLoc(), SecondExpr->getSourceRange(), 10187 StaticAssertCondition); 10188 Diagnosed = true; 10189 break; 10190 } 10191 10192 StringLiteral *FirstStr = FirstSA->getMessage(); 10193 StringLiteral *SecondStr = SecondSA->getMessage(); 10194 assert((FirstStr || SecondStr) && "Both messages cannot be empty"); 10195 if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) { 10196 SourceLocation FirstLoc, SecondLoc; 10197 SourceRange FirstRange, SecondRange; 10198 if (FirstStr) { 10199 FirstLoc = FirstStr->getBeginLoc(); 10200 FirstRange = FirstStr->getSourceRange(); 10201 } else { 10202 FirstLoc = FirstSA->getBeginLoc(); 10203 FirstRange = FirstSA->getSourceRange(); 10204 } 10205 if (SecondStr) { 10206 SecondLoc = SecondStr->getBeginLoc(); 10207 SecondRange = SecondStr->getSourceRange(); 10208 } else { 10209 SecondLoc = SecondSA->getBeginLoc(); 10210 SecondRange = SecondSA->getSourceRange(); 10211 } 10212 ODRDiagError(FirstLoc, FirstRange, StaticAssertOnlyMessage) 10213 << (FirstStr == nullptr); 10214 ODRDiagNote(SecondLoc, SecondRange, StaticAssertOnlyMessage) 10215 << (SecondStr == nullptr); 10216 Diagnosed = true; 10217 break; 10218 } 10219 10220 if (FirstStr && SecondStr && 10221 FirstStr->getString() != SecondStr->getString()) { 10222 ODRDiagError(FirstStr->getBeginLoc(), FirstStr->getSourceRange(), 10223 StaticAssertMessage); 10224 ODRDiagNote(SecondStr->getBeginLoc(), SecondStr->getSourceRange(), 10225 StaticAssertMessage); 10226 Diagnosed = true; 10227 break; 10228 } 10229 break; 10230 } 10231 case Field: { 10232 FieldDecl *FirstField = cast<FieldDecl>(FirstDecl); 10233 FieldDecl *SecondField = cast<FieldDecl>(SecondDecl); 10234 IdentifierInfo *FirstII = FirstField->getIdentifier(); 10235 IdentifierInfo *SecondII = SecondField->getIdentifier(); 10236 if (FirstII->getName() != SecondII->getName()) { 10237 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(), 10238 FieldName) 10239 << FirstII; 10240 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(), 10241 FieldName) 10242 << SecondII; 10243 10244 Diagnosed = true; 10245 break; 10246 } 10247 10248 assert(getContext().hasSameType(FirstField->getType(), 10249 SecondField->getType())); 10250 10251 QualType FirstType = FirstField->getType(); 10252 QualType SecondType = SecondField->getType(); 10253 if (ComputeQualTypeODRHash(FirstType) != 10254 ComputeQualTypeODRHash(SecondType)) { 10255 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(), 10256 FieldTypeName) 10257 << FirstII << FirstType; 10258 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(), 10259 FieldTypeName) 10260 << SecondII << SecondType; 10261 10262 Diagnosed = true; 10263 break; 10264 } 10265 10266 const bool IsFirstBitField = FirstField->isBitField(); 10267 const bool IsSecondBitField = SecondField->isBitField(); 10268 if (IsFirstBitField != IsSecondBitField) { 10269 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(), 10270 FieldSingleBitField) 10271 << FirstII << IsFirstBitField; 10272 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(), 10273 FieldSingleBitField) 10274 << SecondII << IsSecondBitField; 10275 Diagnosed = true; 10276 break; 10277 } 10278 10279 if (IsFirstBitField && IsSecondBitField) { 10280 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(), 10281 FieldDifferentWidthBitField) 10282 << FirstII << FirstField->getBitWidth()->getSourceRange(); 10283 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(), 10284 FieldDifferentWidthBitField) 10285 << SecondII << SecondField->getBitWidth()->getSourceRange(); 10286 Diagnosed = true; 10287 break; 10288 } 10289 10290 const bool IsFirstMutable = FirstField->isMutable(); 10291 const bool IsSecondMutable = SecondField->isMutable(); 10292 if (IsFirstMutable != IsSecondMutable) { 10293 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(), 10294 FieldSingleMutable) 10295 << FirstII << IsFirstMutable; 10296 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(), 10297 FieldSingleMutable) 10298 << SecondII << IsSecondMutable; 10299 Diagnosed = true; 10300 break; 10301 } 10302 10303 const Expr *FirstInitializer = FirstField->getInClassInitializer(); 10304 const Expr *SecondInitializer = SecondField->getInClassInitializer(); 10305 if ((!FirstInitializer && SecondInitializer) || 10306 (FirstInitializer && !SecondInitializer)) { 10307 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(), 10308 FieldSingleInitializer) 10309 << FirstII << (FirstInitializer != nullptr); 10310 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(), 10311 FieldSingleInitializer) 10312 << SecondII << (SecondInitializer != nullptr); 10313 Diagnosed = true; 10314 break; 10315 } 10316 10317 if (FirstInitializer && SecondInitializer) { 10318 unsigned FirstInitHash = ComputeODRHash(FirstInitializer); 10319 unsigned SecondInitHash = ComputeODRHash(SecondInitializer); 10320 if (FirstInitHash != SecondInitHash) { 10321 ODRDiagError(FirstField->getLocation(), 10322 FirstField->getSourceRange(), 10323 FieldDifferentInitializers) 10324 << FirstII << FirstInitializer->getSourceRange(); 10325 ODRDiagNote(SecondField->getLocation(), 10326 SecondField->getSourceRange(), 10327 FieldDifferentInitializers) 10328 << SecondII << SecondInitializer->getSourceRange(); 10329 Diagnosed = true; 10330 break; 10331 } 10332 } 10333 10334 break; 10335 } 10336 case CXXMethod: { 10337 enum { 10338 DiagMethod, 10339 DiagConstructor, 10340 DiagDestructor, 10341 } FirstMethodType, 10342 SecondMethodType; 10343 auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) { 10344 if (isa<CXXConstructorDecl>(D)) return DiagConstructor; 10345 if (isa<CXXDestructorDecl>(D)) return DiagDestructor; 10346 return DiagMethod; 10347 }; 10348 const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl); 10349 const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl); 10350 FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod); 10351 SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod); 10352 auto FirstName = FirstMethod->getDeclName(); 10353 auto SecondName = SecondMethod->getDeclName(); 10354 if (FirstMethodType != SecondMethodType || FirstName != SecondName) { 10355 ODRDiagError(FirstMethod->getLocation(), 10356 FirstMethod->getSourceRange(), MethodName) 10357 << FirstMethodType << FirstName; 10358 ODRDiagNote(SecondMethod->getLocation(), 10359 SecondMethod->getSourceRange(), MethodName) 10360 << SecondMethodType << SecondName; 10361 10362 Diagnosed = true; 10363 break; 10364 } 10365 10366 const bool FirstDeleted = FirstMethod->isDeletedAsWritten(); 10367 const bool SecondDeleted = SecondMethod->isDeletedAsWritten(); 10368 if (FirstDeleted != SecondDeleted) { 10369 ODRDiagError(FirstMethod->getLocation(), 10370 FirstMethod->getSourceRange(), MethodDeleted) 10371 << FirstMethodType << FirstName << FirstDeleted; 10372 10373 ODRDiagNote(SecondMethod->getLocation(), 10374 SecondMethod->getSourceRange(), MethodDeleted) 10375 << SecondMethodType << SecondName << SecondDeleted; 10376 Diagnosed = true; 10377 break; 10378 } 10379 10380 const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted(); 10381 const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted(); 10382 if (FirstDefaulted != SecondDefaulted) { 10383 ODRDiagError(FirstMethod->getLocation(), 10384 FirstMethod->getSourceRange(), MethodDefaulted) 10385 << FirstMethodType << FirstName << FirstDefaulted; 10386 10387 ODRDiagNote(SecondMethod->getLocation(), 10388 SecondMethod->getSourceRange(), MethodDefaulted) 10389 << SecondMethodType << SecondName << SecondDefaulted; 10390 Diagnosed = true; 10391 break; 10392 } 10393 10394 const bool FirstVirtual = FirstMethod->isVirtualAsWritten(); 10395 const bool SecondVirtual = SecondMethod->isVirtualAsWritten(); 10396 const bool FirstPure = FirstMethod->isPure(); 10397 const bool SecondPure = SecondMethod->isPure(); 10398 if ((FirstVirtual || SecondVirtual) && 10399 (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) { 10400 ODRDiagError(FirstMethod->getLocation(), 10401 FirstMethod->getSourceRange(), MethodVirtual) 10402 << FirstMethodType << FirstName << FirstPure << FirstVirtual; 10403 ODRDiagNote(SecondMethod->getLocation(), 10404 SecondMethod->getSourceRange(), MethodVirtual) 10405 << SecondMethodType << SecondName << SecondPure << SecondVirtual; 10406 Diagnosed = true; 10407 break; 10408 } 10409 10410 // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging, 10411 // FirstDecl is the canonical Decl of SecondDecl, so the storage 10412 // class needs to be checked instead. 10413 const auto FirstStorage = FirstMethod->getStorageClass(); 10414 const auto SecondStorage = SecondMethod->getStorageClass(); 10415 const bool FirstStatic = FirstStorage == SC_Static; 10416 const bool SecondStatic = SecondStorage == SC_Static; 10417 if (FirstStatic != SecondStatic) { 10418 ODRDiagError(FirstMethod->getLocation(), 10419 FirstMethod->getSourceRange(), MethodStatic) 10420 << FirstMethodType << FirstName << FirstStatic; 10421 ODRDiagNote(SecondMethod->getLocation(), 10422 SecondMethod->getSourceRange(), MethodStatic) 10423 << SecondMethodType << SecondName << SecondStatic; 10424 Diagnosed = true; 10425 break; 10426 } 10427 10428 const bool FirstVolatile = FirstMethod->isVolatile(); 10429 const bool SecondVolatile = SecondMethod->isVolatile(); 10430 if (FirstVolatile != SecondVolatile) { 10431 ODRDiagError(FirstMethod->getLocation(), 10432 FirstMethod->getSourceRange(), MethodVolatile) 10433 << FirstMethodType << FirstName << FirstVolatile; 10434 ODRDiagNote(SecondMethod->getLocation(), 10435 SecondMethod->getSourceRange(), MethodVolatile) 10436 << SecondMethodType << SecondName << SecondVolatile; 10437 Diagnosed = true; 10438 break; 10439 } 10440 10441 const bool FirstConst = FirstMethod->isConst(); 10442 const bool SecondConst = SecondMethod->isConst(); 10443 if (FirstConst != SecondConst) { 10444 ODRDiagError(FirstMethod->getLocation(), 10445 FirstMethod->getSourceRange(), MethodConst) 10446 << FirstMethodType << FirstName << FirstConst; 10447 ODRDiagNote(SecondMethod->getLocation(), 10448 SecondMethod->getSourceRange(), MethodConst) 10449 << SecondMethodType << SecondName << SecondConst; 10450 Diagnosed = true; 10451 break; 10452 } 10453 10454 const bool FirstInline = FirstMethod->isInlineSpecified(); 10455 const bool SecondInline = SecondMethod->isInlineSpecified(); 10456 if (FirstInline != SecondInline) { 10457 ODRDiagError(FirstMethod->getLocation(), 10458 FirstMethod->getSourceRange(), MethodInline) 10459 << FirstMethodType << FirstName << FirstInline; 10460 ODRDiagNote(SecondMethod->getLocation(), 10461 SecondMethod->getSourceRange(), MethodInline) 10462 << SecondMethodType << SecondName << SecondInline; 10463 Diagnosed = true; 10464 break; 10465 } 10466 10467 const unsigned FirstNumParameters = FirstMethod->param_size(); 10468 const unsigned SecondNumParameters = SecondMethod->param_size(); 10469 if (FirstNumParameters != SecondNumParameters) { 10470 ODRDiagError(FirstMethod->getLocation(), 10471 FirstMethod->getSourceRange(), MethodNumberParameters) 10472 << FirstMethodType << FirstName << FirstNumParameters; 10473 ODRDiagNote(SecondMethod->getLocation(), 10474 SecondMethod->getSourceRange(), MethodNumberParameters) 10475 << SecondMethodType << SecondName << SecondNumParameters; 10476 Diagnosed = true; 10477 break; 10478 } 10479 10480 // Need this status boolean to know when break out of the switch. 10481 bool ParameterMismatch = false; 10482 for (unsigned I = 0; I < FirstNumParameters; ++I) { 10483 const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I); 10484 const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I); 10485 10486 QualType FirstParamType = FirstParam->getType(); 10487 QualType SecondParamType = SecondParam->getType(); 10488 if (FirstParamType != SecondParamType && 10489 ComputeQualTypeODRHash(FirstParamType) != 10490 ComputeQualTypeODRHash(SecondParamType)) { 10491 if (const DecayedType *ParamDecayedType = 10492 FirstParamType->getAs<DecayedType>()) { 10493 ODRDiagError(FirstMethod->getLocation(), 10494 FirstMethod->getSourceRange(), MethodParameterType) 10495 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10496 << true << ParamDecayedType->getOriginalType(); 10497 } else { 10498 ODRDiagError(FirstMethod->getLocation(), 10499 FirstMethod->getSourceRange(), MethodParameterType) 10500 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10501 << false; 10502 } 10503 10504 if (const DecayedType *ParamDecayedType = 10505 SecondParamType->getAs<DecayedType>()) { 10506 ODRDiagNote(SecondMethod->getLocation(), 10507 SecondMethod->getSourceRange(), MethodParameterType) 10508 << SecondMethodType << SecondName << (I + 1) 10509 << SecondParamType << true 10510 << ParamDecayedType->getOriginalType(); 10511 } else { 10512 ODRDiagNote(SecondMethod->getLocation(), 10513 SecondMethod->getSourceRange(), MethodParameterType) 10514 << SecondMethodType << SecondName << (I + 1) 10515 << SecondParamType << false; 10516 } 10517 ParameterMismatch = true; 10518 break; 10519 } 10520 10521 DeclarationName FirstParamName = FirstParam->getDeclName(); 10522 DeclarationName SecondParamName = SecondParam->getDeclName(); 10523 if (FirstParamName != SecondParamName) { 10524 ODRDiagError(FirstMethod->getLocation(), 10525 FirstMethod->getSourceRange(), MethodParameterName) 10526 << FirstMethodType << FirstName << (I + 1) << FirstParamName; 10527 ODRDiagNote(SecondMethod->getLocation(), 10528 SecondMethod->getSourceRange(), MethodParameterName) 10529 << SecondMethodType << SecondName << (I + 1) << SecondParamName; 10530 ParameterMismatch = true; 10531 break; 10532 } 10533 10534 const Expr *FirstInit = FirstParam->getInit(); 10535 const Expr *SecondInit = SecondParam->getInit(); 10536 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 10537 ODRDiagError(FirstMethod->getLocation(), 10538 FirstMethod->getSourceRange(), 10539 MethodParameterSingleDefaultArgument) 10540 << FirstMethodType << FirstName << (I + 1) 10541 << (FirstInit == nullptr) 10542 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 10543 ODRDiagNote(SecondMethod->getLocation(), 10544 SecondMethod->getSourceRange(), 10545 MethodParameterSingleDefaultArgument) 10546 << SecondMethodType << SecondName << (I + 1) 10547 << (SecondInit == nullptr) 10548 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 10549 ParameterMismatch = true; 10550 break; 10551 } 10552 10553 if (FirstInit && SecondInit && 10554 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 10555 ODRDiagError(FirstMethod->getLocation(), 10556 FirstMethod->getSourceRange(), 10557 MethodParameterDifferentDefaultArgument) 10558 << FirstMethodType << FirstName << (I + 1) 10559 << FirstInit->getSourceRange(); 10560 ODRDiagNote(SecondMethod->getLocation(), 10561 SecondMethod->getSourceRange(), 10562 MethodParameterDifferentDefaultArgument) 10563 << SecondMethodType << SecondName << (I + 1) 10564 << SecondInit->getSourceRange(); 10565 ParameterMismatch = true; 10566 break; 10567 10568 } 10569 } 10570 10571 if (ParameterMismatch) { 10572 Diagnosed = true; 10573 break; 10574 } 10575 10576 const auto *FirstTemplateArgs = 10577 FirstMethod->getTemplateSpecializationArgs(); 10578 const auto *SecondTemplateArgs = 10579 SecondMethod->getTemplateSpecializationArgs(); 10580 10581 if ((FirstTemplateArgs && !SecondTemplateArgs) || 10582 (!FirstTemplateArgs && SecondTemplateArgs)) { 10583 ODRDiagError(FirstMethod->getLocation(), 10584 FirstMethod->getSourceRange(), MethodNoTemplateArguments) 10585 << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr); 10586 ODRDiagNote(SecondMethod->getLocation(), 10587 SecondMethod->getSourceRange(), MethodNoTemplateArguments) 10588 << SecondMethodType << SecondName 10589 << (SecondTemplateArgs != nullptr); 10590 10591 Diagnosed = true; 10592 break; 10593 } 10594 10595 if (FirstTemplateArgs && SecondTemplateArgs) { 10596 // Remove pack expansions from argument list. 10597 auto ExpandTemplateArgumentList = 10598 [](const TemplateArgumentList *TAL) { 10599 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList; 10600 for (const TemplateArgument &TA : TAL->asArray()) { 10601 if (TA.getKind() != TemplateArgument::Pack) { 10602 ExpandedList.push_back(&TA); 10603 continue; 10604 } 10605 for (const TemplateArgument &PackTA : TA.getPackAsArray()) { 10606 ExpandedList.push_back(&PackTA); 10607 } 10608 } 10609 return ExpandedList; 10610 }; 10611 llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList = 10612 ExpandTemplateArgumentList(FirstTemplateArgs); 10613 llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList = 10614 ExpandTemplateArgumentList(SecondTemplateArgs); 10615 10616 if (FirstExpandedList.size() != SecondExpandedList.size()) { 10617 ODRDiagError(FirstMethod->getLocation(), 10618 FirstMethod->getSourceRange(), 10619 MethodDifferentNumberTemplateArguments) 10620 << FirstMethodType << FirstName 10621 << (unsigned)FirstExpandedList.size(); 10622 ODRDiagNote(SecondMethod->getLocation(), 10623 SecondMethod->getSourceRange(), 10624 MethodDifferentNumberTemplateArguments) 10625 << SecondMethodType << SecondName 10626 << (unsigned)SecondExpandedList.size(); 10627 10628 Diagnosed = true; 10629 break; 10630 } 10631 10632 bool TemplateArgumentMismatch = false; 10633 for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) { 10634 const TemplateArgument &FirstTA = *FirstExpandedList[i], 10635 &SecondTA = *SecondExpandedList[i]; 10636 if (ComputeTemplateArgumentODRHash(FirstTA) == 10637 ComputeTemplateArgumentODRHash(SecondTA)) { 10638 continue; 10639 } 10640 10641 ODRDiagError(FirstMethod->getLocation(), 10642 FirstMethod->getSourceRange(), 10643 MethodDifferentTemplateArgument) 10644 << FirstMethodType << FirstName << FirstTA << i + 1; 10645 ODRDiagNote(SecondMethod->getLocation(), 10646 SecondMethod->getSourceRange(), 10647 MethodDifferentTemplateArgument) 10648 << SecondMethodType << SecondName << SecondTA << i + 1; 10649 10650 TemplateArgumentMismatch = true; 10651 break; 10652 } 10653 10654 if (TemplateArgumentMismatch) { 10655 Diagnosed = true; 10656 break; 10657 } 10658 } 10659 10660 // Compute the hash of the method as if it has no body. 10661 auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) { 10662 Hash.clear(); 10663 Hash.AddFunctionDecl(D, true /*SkipBody*/); 10664 return Hash.CalculateHash(); 10665 }; 10666 10667 // Compare the hash generated to the hash stored. A difference means 10668 // that a body was present in the original source. Due to merging, 10669 // the stardard way of detecting a body will not work. 10670 const bool HasFirstBody = 10671 ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash(); 10672 const bool HasSecondBody = 10673 ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash(); 10674 10675 if (HasFirstBody != HasSecondBody) { 10676 ODRDiagError(FirstMethod->getLocation(), 10677 FirstMethod->getSourceRange(), MethodSingleBody) 10678 << FirstMethodType << FirstName << HasFirstBody; 10679 ODRDiagNote(SecondMethod->getLocation(), 10680 SecondMethod->getSourceRange(), MethodSingleBody) 10681 << SecondMethodType << SecondName << HasSecondBody; 10682 Diagnosed = true; 10683 break; 10684 } 10685 10686 if (HasFirstBody && HasSecondBody) { 10687 ODRDiagError(FirstMethod->getLocation(), 10688 FirstMethod->getSourceRange(), MethodDifferentBody) 10689 << FirstMethodType << FirstName; 10690 ODRDiagNote(SecondMethod->getLocation(), 10691 SecondMethod->getSourceRange(), MethodDifferentBody) 10692 << SecondMethodType << SecondName; 10693 Diagnosed = true; 10694 break; 10695 } 10696 10697 break; 10698 } 10699 case TypeAlias: 10700 case TypeDef: { 10701 TypedefNameDecl *FirstTD = cast<TypedefNameDecl>(FirstDecl); 10702 TypedefNameDecl *SecondTD = cast<TypedefNameDecl>(SecondDecl); 10703 auto FirstName = FirstTD->getDeclName(); 10704 auto SecondName = SecondTD->getDeclName(); 10705 if (FirstName != SecondName) { 10706 ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(), 10707 TypedefName) 10708 << (FirstDiffType == TypeAlias) << FirstName; 10709 ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(), 10710 TypedefName) 10711 << (FirstDiffType == TypeAlias) << SecondName; 10712 Diagnosed = true; 10713 break; 10714 } 10715 10716 QualType FirstType = FirstTD->getUnderlyingType(); 10717 QualType SecondType = SecondTD->getUnderlyingType(); 10718 if (ComputeQualTypeODRHash(FirstType) != 10719 ComputeQualTypeODRHash(SecondType)) { 10720 ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(), 10721 TypedefType) 10722 << (FirstDiffType == TypeAlias) << FirstName << FirstType; 10723 ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(), 10724 TypedefType) 10725 << (FirstDiffType == TypeAlias) << SecondName << SecondType; 10726 Diagnosed = true; 10727 break; 10728 } 10729 break; 10730 } 10731 case Var: { 10732 VarDecl *FirstVD = cast<VarDecl>(FirstDecl); 10733 VarDecl *SecondVD = cast<VarDecl>(SecondDecl); 10734 auto FirstName = FirstVD->getDeclName(); 10735 auto SecondName = SecondVD->getDeclName(); 10736 if (FirstName != SecondName) { 10737 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(), 10738 VarName) 10739 << FirstName; 10740 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(), 10741 VarName) 10742 << SecondName; 10743 Diagnosed = true; 10744 break; 10745 } 10746 10747 QualType FirstType = FirstVD->getType(); 10748 QualType SecondType = SecondVD->getType(); 10749 if (ComputeQualTypeODRHash(FirstType) != 10750 ComputeQualTypeODRHash(SecondType)) { 10751 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(), 10752 VarType) 10753 << FirstName << FirstType; 10754 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(), 10755 VarType) 10756 << SecondName << SecondType; 10757 Diagnosed = true; 10758 break; 10759 } 10760 10761 const Expr *FirstInit = FirstVD->getInit(); 10762 const Expr *SecondInit = SecondVD->getInit(); 10763 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 10764 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(), 10765 VarSingleInitializer) 10766 << FirstName << (FirstInit == nullptr) 10767 << (FirstInit ? FirstInit->getSourceRange(): SourceRange()); 10768 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(), 10769 VarSingleInitializer) 10770 << SecondName << (SecondInit == nullptr) 10771 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 10772 Diagnosed = true; 10773 break; 10774 } 10775 10776 if (FirstInit && SecondInit && 10777 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 10778 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(), 10779 VarDifferentInitializer) 10780 << FirstName << FirstInit->getSourceRange(); 10781 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(), 10782 VarDifferentInitializer) 10783 << SecondName << SecondInit->getSourceRange(); 10784 Diagnosed = true; 10785 break; 10786 } 10787 10788 const bool FirstIsConstexpr = FirstVD->isConstexpr(); 10789 const bool SecondIsConstexpr = SecondVD->isConstexpr(); 10790 if (FirstIsConstexpr != SecondIsConstexpr) { 10791 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(), 10792 VarConstexpr) 10793 << FirstName << FirstIsConstexpr; 10794 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(), 10795 VarConstexpr) 10796 << SecondName << SecondIsConstexpr; 10797 Diagnosed = true; 10798 break; 10799 } 10800 break; 10801 } 10802 case Friend: { 10803 FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl); 10804 FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl); 10805 10806 NamedDecl *FirstND = FirstFriend->getFriendDecl(); 10807 NamedDecl *SecondND = SecondFriend->getFriendDecl(); 10808 10809 TypeSourceInfo *FirstTSI = FirstFriend->getFriendType(); 10810 TypeSourceInfo *SecondTSI = SecondFriend->getFriendType(); 10811 10812 if (FirstND && SecondND) { 10813 ODRDiagError(FirstFriend->getFriendLoc(), 10814 FirstFriend->getSourceRange(), FriendFunction) 10815 << FirstND; 10816 ODRDiagNote(SecondFriend->getFriendLoc(), 10817 SecondFriend->getSourceRange(), FriendFunction) 10818 << SecondND; 10819 10820 Diagnosed = true; 10821 break; 10822 } 10823 10824 if (FirstTSI && SecondTSI) { 10825 QualType FirstFriendType = FirstTSI->getType(); 10826 QualType SecondFriendType = SecondTSI->getType(); 10827 assert(ComputeQualTypeODRHash(FirstFriendType) != 10828 ComputeQualTypeODRHash(SecondFriendType)); 10829 ODRDiagError(FirstFriend->getFriendLoc(), 10830 FirstFriend->getSourceRange(), FriendType) 10831 << FirstFriendType; 10832 ODRDiagNote(SecondFriend->getFriendLoc(), 10833 SecondFriend->getSourceRange(), FriendType) 10834 << SecondFriendType; 10835 Diagnosed = true; 10836 break; 10837 } 10838 10839 ODRDiagError(FirstFriend->getFriendLoc(), FirstFriend->getSourceRange(), 10840 FriendTypeFunction) 10841 << (FirstTSI == nullptr); 10842 ODRDiagNote(SecondFriend->getFriendLoc(), 10843 SecondFriend->getSourceRange(), FriendTypeFunction) 10844 << (SecondTSI == nullptr); 10845 10846 Diagnosed = true; 10847 break; 10848 } 10849 case FunctionTemplate: { 10850 FunctionTemplateDecl *FirstTemplate = 10851 cast<FunctionTemplateDecl>(FirstDecl); 10852 FunctionTemplateDecl *SecondTemplate = 10853 cast<FunctionTemplateDecl>(SecondDecl); 10854 10855 TemplateParameterList *FirstTPL = 10856 FirstTemplate->getTemplateParameters(); 10857 TemplateParameterList *SecondTPL = 10858 SecondTemplate->getTemplateParameters(); 10859 10860 if (FirstTPL->size() != SecondTPL->size()) { 10861 ODRDiagError(FirstTemplate->getLocation(), 10862 FirstTemplate->getSourceRange(), 10863 FunctionTemplateDifferentNumberParameters) 10864 << FirstTemplate << FirstTPL->size(); 10865 ODRDiagNote(SecondTemplate->getLocation(), 10866 SecondTemplate->getSourceRange(), 10867 FunctionTemplateDifferentNumberParameters) 10868 << SecondTemplate << SecondTPL->size(); 10869 10870 Diagnosed = true; 10871 break; 10872 } 10873 10874 bool ParameterMismatch = false; 10875 for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) { 10876 NamedDecl *FirstParam = FirstTPL->getParam(i); 10877 NamedDecl *SecondParam = SecondTPL->getParam(i); 10878 10879 if (FirstParam->getKind() != SecondParam->getKind()) { 10880 enum { 10881 TemplateTypeParameter, 10882 NonTypeTemplateParameter, 10883 TemplateTemplateParameter, 10884 }; 10885 auto GetParamType = [](NamedDecl *D) { 10886 switch (D->getKind()) { 10887 default: 10888 llvm_unreachable("Unexpected template parameter type"); 10889 case Decl::TemplateTypeParm: 10890 return TemplateTypeParameter; 10891 case Decl::NonTypeTemplateParm: 10892 return NonTypeTemplateParameter; 10893 case Decl::TemplateTemplateParm: 10894 return TemplateTemplateParameter; 10895 } 10896 }; 10897 10898 ODRDiagError(FirstTemplate->getLocation(), 10899 FirstTemplate->getSourceRange(), 10900 FunctionTemplateParameterDifferentKind) 10901 << FirstTemplate << (i + 1) << GetParamType(FirstParam); 10902 ODRDiagNote(SecondTemplate->getLocation(), 10903 SecondTemplate->getSourceRange(), 10904 FunctionTemplateParameterDifferentKind) 10905 << SecondTemplate << (i + 1) << GetParamType(SecondParam); 10906 10907 ParameterMismatch = true; 10908 break; 10909 } 10910 10911 if (FirstParam->getName() != SecondParam->getName()) { 10912 ODRDiagError(FirstTemplate->getLocation(), 10913 FirstTemplate->getSourceRange(), 10914 FunctionTemplateParameterName) 10915 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier() 10916 << FirstParam; 10917 ODRDiagNote(SecondTemplate->getLocation(), 10918 SecondTemplate->getSourceRange(), 10919 FunctionTemplateParameterName) 10920 << SecondTemplate << (i + 1) 10921 << (bool)SecondParam->getIdentifier() << SecondParam; 10922 ParameterMismatch = true; 10923 break; 10924 } 10925 10926 if (isa<TemplateTypeParmDecl>(FirstParam) && 10927 isa<TemplateTypeParmDecl>(SecondParam)) { 10928 TemplateTypeParmDecl *FirstTTPD = 10929 cast<TemplateTypeParmDecl>(FirstParam); 10930 TemplateTypeParmDecl *SecondTTPD = 10931 cast<TemplateTypeParmDecl>(SecondParam); 10932 bool HasFirstDefaultArgument = 10933 FirstTTPD->hasDefaultArgument() && 10934 !FirstTTPD->defaultArgumentWasInherited(); 10935 bool HasSecondDefaultArgument = 10936 SecondTTPD->hasDefaultArgument() && 10937 !SecondTTPD->defaultArgumentWasInherited(); 10938 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10939 ODRDiagError(FirstTemplate->getLocation(), 10940 FirstTemplate->getSourceRange(), 10941 FunctionTemplateParameterSingleDefaultArgument) 10942 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 10943 ODRDiagNote(SecondTemplate->getLocation(), 10944 SecondTemplate->getSourceRange(), 10945 FunctionTemplateParameterSingleDefaultArgument) 10946 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 10947 ParameterMismatch = true; 10948 break; 10949 } 10950 10951 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 10952 QualType FirstType = FirstTTPD->getDefaultArgument(); 10953 QualType SecondType = SecondTTPD->getDefaultArgument(); 10954 if (ComputeQualTypeODRHash(FirstType) != 10955 ComputeQualTypeODRHash(SecondType)) { 10956 ODRDiagError(FirstTemplate->getLocation(), 10957 FirstTemplate->getSourceRange(), 10958 FunctionTemplateParameterDifferentDefaultArgument) 10959 << FirstTemplate << (i + 1) << FirstType; 10960 ODRDiagNote(SecondTemplate->getLocation(), 10961 SecondTemplate->getSourceRange(), 10962 FunctionTemplateParameterDifferentDefaultArgument) 10963 << SecondTemplate << (i + 1) << SecondType; 10964 ParameterMismatch = true; 10965 break; 10966 } 10967 } 10968 10969 if (FirstTTPD->isParameterPack() != 10970 SecondTTPD->isParameterPack()) { 10971 ODRDiagError(FirstTemplate->getLocation(), 10972 FirstTemplate->getSourceRange(), 10973 FunctionTemplatePackParameter) 10974 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 10975 ODRDiagNote(SecondTemplate->getLocation(), 10976 SecondTemplate->getSourceRange(), 10977 FunctionTemplatePackParameter) 10978 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 10979 ParameterMismatch = true; 10980 break; 10981 } 10982 } 10983 10984 if (isa<TemplateTemplateParmDecl>(FirstParam) && 10985 isa<TemplateTemplateParmDecl>(SecondParam)) { 10986 TemplateTemplateParmDecl *FirstTTPD = 10987 cast<TemplateTemplateParmDecl>(FirstParam); 10988 TemplateTemplateParmDecl *SecondTTPD = 10989 cast<TemplateTemplateParmDecl>(SecondParam); 10990 10991 TemplateParameterList *FirstTPL = 10992 FirstTTPD->getTemplateParameters(); 10993 TemplateParameterList *SecondTPL = 10994 SecondTTPD->getTemplateParameters(); 10995 10996 if (ComputeTemplateParameterListODRHash(FirstTPL) != 10997 ComputeTemplateParameterListODRHash(SecondTPL)) { 10998 ODRDiagError(FirstTemplate->getLocation(), 10999 FirstTemplate->getSourceRange(), 11000 FunctionTemplateParameterDifferentType) 11001 << FirstTemplate << (i + 1); 11002 ODRDiagNote(SecondTemplate->getLocation(), 11003 SecondTemplate->getSourceRange(), 11004 FunctionTemplateParameterDifferentType) 11005 << SecondTemplate << (i + 1); 11006 ParameterMismatch = true; 11007 break; 11008 } 11009 11010 bool HasFirstDefaultArgument = 11011 FirstTTPD->hasDefaultArgument() && 11012 !FirstTTPD->defaultArgumentWasInherited(); 11013 bool HasSecondDefaultArgument = 11014 SecondTTPD->hasDefaultArgument() && 11015 !SecondTTPD->defaultArgumentWasInherited(); 11016 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11017 ODRDiagError(FirstTemplate->getLocation(), 11018 FirstTemplate->getSourceRange(), 11019 FunctionTemplateParameterSingleDefaultArgument) 11020 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11021 ODRDiagNote(SecondTemplate->getLocation(), 11022 SecondTemplate->getSourceRange(), 11023 FunctionTemplateParameterSingleDefaultArgument) 11024 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11025 ParameterMismatch = true; 11026 break; 11027 } 11028 11029 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11030 TemplateArgument FirstTA = 11031 FirstTTPD->getDefaultArgument().getArgument(); 11032 TemplateArgument SecondTA = 11033 SecondTTPD->getDefaultArgument().getArgument(); 11034 if (ComputeTemplateArgumentODRHash(FirstTA) != 11035 ComputeTemplateArgumentODRHash(SecondTA)) { 11036 ODRDiagError(FirstTemplate->getLocation(), 11037 FirstTemplate->getSourceRange(), 11038 FunctionTemplateParameterDifferentDefaultArgument) 11039 << FirstTemplate << (i + 1) << FirstTA; 11040 ODRDiagNote(SecondTemplate->getLocation(), 11041 SecondTemplate->getSourceRange(), 11042 FunctionTemplateParameterDifferentDefaultArgument) 11043 << SecondTemplate << (i + 1) << SecondTA; 11044 ParameterMismatch = true; 11045 break; 11046 } 11047 } 11048 11049 if (FirstTTPD->isParameterPack() != 11050 SecondTTPD->isParameterPack()) { 11051 ODRDiagError(FirstTemplate->getLocation(), 11052 FirstTemplate->getSourceRange(), 11053 FunctionTemplatePackParameter) 11054 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 11055 ODRDiagNote(SecondTemplate->getLocation(), 11056 SecondTemplate->getSourceRange(), 11057 FunctionTemplatePackParameter) 11058 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 11059 ParameterMismatch = true; 11060 break; 11061 } 11062 } 11063 11064 if (isa<NonTypeTemplateParmDecl>(FirstParam) && 11065 isa<NonTypeTemplateParmDecl>(SecondParam)) { 11066 NonTypeTemplateParmDecl *FirstNTTPD = 11067 cast<NonTypeTemplateParmDecl>(FirstParam); 11068 NonTypeTemplateParmDecl *SecondNTTPD = 11069 cast<NonTypeTemplateParmDecl>(SecondParam); 11070 11071 QualType FirstType = FirstNTTPD->getType(); 11072 QualType SecondType = SecondNTTPD->getType(); 11073 if (ComputeQualTypeODRHash(FirstType) != 11074 ComputeQualTypeODRHash(SecondType)) { 11075 ODRDiagError(FirstTemplate->getLocation(), 11076 FirstTemplate->getSourceRange(), 11077 FunctionTemplateParameterDifferentType) 11078 << FirstTemplate << (i + 1); 11079 ODRDiagNote(SecondTemplate->getLocation(), 11080 SecondTemplate->getSourceRange(), 11081 FunctionTemplateParameterDifferentType) 11082 << SecondTemplate << (i + 1); 11083 ParameterMismatch = true; 11084 break; 11085 } 11086 11087 bool HasFirstDefaultArgument = 11088 FirstNTTPD->hasDefaultArgument() && 11089 !FirstNTTPD->defaultArgumentWasInherited(); 11090 bool HasSecondDefaultArgument = 11091 SecondNTTPD->hasDefaultArgument() && 11092 !SecondNTTPD->defaultArgumentWasInherited(); 11093 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11094 ODRDiagError(FirstTemplate->getLocation(), 11095 FirstTemplate->getSourceRange(), 11096 FunctionTemplateParameterSingleDefaultArgument) 11097 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11098 ODRDiagNote(SecondTemplate->getLocation(), 11099 SecondTemplate->getSourceRange(), 11100 FunctionTemplateParameterSingleDefaultArgument) 11101 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11102 ParameterMismatch = true; 11103 break; 11104 } 11105 11106 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11107 Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument(); 11108 Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument(); 11109 if (ComputeODRHash(FirstDefaultArgument) != 11110 ComputeODRHash(SecondDefaultArgument)) { 11111 ODRDiagError(FirstTemplate->getLocation(), 11112 FirstTemplate->getSourceRange(), 11113 FunctionTemplateParameterDifferentDefaultArgument) 11114 << FirstTemplate << (i + 1) << FirstDefaultArgument; 11115 ODRDiagNote(SecondTemplate->getLocation(), 11116 SecondTemplate->getSourceRange(), 11117 FunctionTemplateParameterDifferentDefaultArgument) 11118 << SecondTemplate << (i + 1) << SecondDefaultArgument; 11119 ParameterMismatch = true; 11120 break; 11121 } 11122 } 11123 11124 if (FirstNTTPD->isParameterPack() != 11125 SecondNTTPD->isParameterPack()) { 11126 ODRDiagError(FirstTemplate->getLocation(), 11127 FirstTemplate->getSourceRange(), 11128 FunctionTemplatePackParameter) 11129 << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack(); 11130 ODRDiagNote(SecondTemplate->getLocation(), 11131 SecondTemplate->getSourceRange(), 11132 FunctionTemplatePackParameter) 11133 << SecondTemplate << (i + 1) 11134 << SecondNTTPD->isParameterPack(); 11135 ParameterMismatch = true; 11136 break; 11137 } 11138 } 11139 } 11140 11141 if (ParameterMismatch) { 11142 Diagnosed = true; 11143 break; 11144 } 11145 11146 break; 11147 } 11148 } 11149 11150 if (Diagnosed) 11151 continue; 11152 11153 Diag(FirstDecl->getLocation(), 11154 diag::err_module_odr_violation_mismatch_decl_unknown) 11155 << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType 11156 << FirstDecl->getSourceRange(); 11157 Diag(SecondDecl->getLocation(), 11158 diag::note_module_odr_violation_mismatch_decl_unknown) 11159 << SecondModule << FirstDiffType << SecondDecl->getSourceRange(); 11160 Diagnosed = true; 11161 } 11162 11163 if (!Diagnosed) { 11164 // All definitions are updates to the same declaration. This happens if a 11165 // module instantiates the declaration of a class template specialization 11166 // and two or more other modules instantiate its definition. 11167 // 11168 // FIXME: Indicate which modules had instantiations of this definition. 11169 // FIXME: How can this even happen? 11170 Diag(Merge.first->getLocation(), 11171 diag::err_module_odr_violation_different_instantiations) 11172 << Merge.first; 11173 } 11174 } 11175 11176 // Issue ODR failures diagnostics for functions. 11177 for (auto &Merge : FunctionOdrMergeFailures) { 11178 enum ODRFunctionDifference { 11179 ReturnType, 11180 ParameterName, 11181 ParameterType, 11182 ParameterSingleDefaultArgument, 11183 ParameterDifferentDefaultArgument, 11184 FunctionBody, 11185 }; 11186 11187 FunctionDecl *FirstFunction = Merge.first; 11188 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction); 11189 11190 bool Diagnosed = false; 11191 for (auto &SecondFunction : Merge.second) { 11192 11193 if (FirstFunction == SecondFunction) 11194 continue; 11195 11196 std::string SecondModule = 11197 getOwningModuleNameForDiagnostic(SecondFunction); 11198 11199 auto ODRDiagError = [FirstFunction, &FirstModule, 11200 this](SourceLocation Loc, SourceRange Range, 11201 ODRFunctionDifference DiffType) { 11202 return Diag(Loc, diag::err_module_odr_violation_function) 11203 << FirstFunction << FirstModule.empty() << FirstModule << Range 11204 << DiffType; 11205 }; 11206 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11207 SourceRange Range, 11208 ODRFunctionDifference DiffType) { 11209 return Diag(Loc, diag::note_module_odr_violation_function) 11210 << SecondModule << Range << DiffType; 11211 }; 11212 11213 if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) != 11214 ComputeQualTypeODRHash(SecondFunction->getReturnType())) { 11215 ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(), 11216 FirstFunction->getReturnTypeSourceRange(), ReturnType) 11217 << FirstFunction->getReturnType(); 11218 ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(), 11219 SecondFunction->getReturnTypeSourceRange(), ReturnType) 11220 << SecondFunction->getReturnType(); 11221 Diagnosed = true; 11222 break; 11223 } 11224 11225 assert(FirstFunction->param_size() == SecondFunction->param_size() && 11226 "Merged functions with different number of parameters"); 11227 11228 auto ParamSize = FirstFunction->param_size(); 11229 bool ParameterMismatch = false; 11230 for (unsigned I = 0; I < ParamSize; ++I) { 11231 auto *FirstParam = FirstFunction->getParamDecl(I); 11232 auto *SecondParam = SecondFunction->getParamDecl(I); 11233 11234 assert(getContext().hasSameType(FirstParam->getType(), 11235 SecondParam->getType()) && 11236 "Merged function has different parameter types."); 11237 11238 if (FirstParam->getDeclName() != SecondParam->getDeclName()) { 11239 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11240 ParameterName) 11241 << I + 1 << FirstParam->getDeclName(); 11242 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11243 ParameterName) 11244 << I + 1 << SecondParam->getDeclName(); 11245 ParameterMismatch = true; 11246 break; 11247 }; 11248 11249 QualType FirstParamType = FirstParam->getType(); 11250 QualType SecondParamType = SecondParam->getType(); 11251 if (FirstParamType != SecondParamType && 11252 ComputeQualTypeODRHash(FirstParamType) != 11253 ComputeQualTypeODRHash(SecondParamType)) { 11254 if (const DecayedType *ParamDecayedType = 11255 FirstParamType->getAs<DecayedType>()) { 11256 ODRDiagError(FirstParam->getLocation(), 11257 FirstParam->getSourceRange(), ParameterType) 11258 << (I + 1) << FirstParamType << true 11259 << ParamDecayedType->getOriginalType(); 11260 } else { 11261 ODRDiagError(FirstParam->getLocation(), 11262 FirstParam->getSourceRange(), ParameterType) 11263 << (I + 1) << FirstParamType << false; 11264 } 11265 11266 if (const DecayedType *ParamDecayedType = 11267 SecondParamType->getAs<DecayedType>()) { 11268 ODRDiagNote(SecondParam->getLocation(), 11269 SecondParam->getSourceRange(), ParameterType) 11270 << (I + 1) << SecondParamType << true 11271 << ParamDecayedType->getOriginalType(); 11272 } else { 11273 ODRDiagNote(SecondParam->getLocation(), 11274 SecondParam->getSourceRange(), ParameterType) 11275 << (I + 1) << SecondParamType << false; 11276 } 11277 ParameterMismatch = true; 11278 break; 11279 } 11280 11281 const Expr *FirstInit = FirstParam->getInit(); 11282 const Expr *SecondInit = SecondParam->getInit(); 11283 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 11284 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11285 ParameterSingleDefaultArgument) 11286 << (I + 1) << (FirstInit == nullptr) 11287 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 11288 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11289 ParameterSingleDefaultArgument) 11290 << (I + 1) << (SecondInit == nullptr) 11291 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 11292 ParameterMismatch = true; 11293 break; 11294 } 11295 11296 if (FirstInit && SecondInit && 11297 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11298 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11299 ParameterDifferentDefaultArgument) 11300 << (I + 1) << FirstInit->getSourceRange(); 11301 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11302 ParameterDifferentDefaultArgument) 11303 << (I + 1) << SecondInit->getSourceRange(); 11304 ParameterMismatch = true; 11305 break; 11306 } 11307 11308 assert(ComputeSubDeclODRHash(FirstParam) == 11309 ComputeSubDeclODRHash(SecondParam) && 11310 "Undiagnosed parameter difference."); 11311 } 11312 11313 if (ParameterMismatch) { 11314 Diagnosed = true; 11315 break; 11316 } 11317 11318 // If no error has been generated before now, assume the problem is in 11319 // the body and generate a message. 11320 ODRDiagError(FirstFunction->getLocation(), 11321 FirstFunction->getSourceRange(), FunctionBody); 11322 ODRDiagNote(SecondFunction->getLocation(), 11323 SecondFunction->getSourceRange(), FunctionBody); 11324 Diagnosed = true; 11325 break; 11326 } 11327 (void)Diagnosed; 11328 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11329 } 11330 11331 // Issue ODR failures diagnostics for enums. 11332 for (auto &Merge : EnumOdrMergeFailures) { 11333 enum ODREnumDifference { 11334 SingleScopedEnum, 11335 EnumTagKeywordMismatch, 11336 SingleSpecifiedType, 11337 DifferentSpecifiedTypes, 11338 DifferentNumberEnumConstants, 11339 EnumConstantName, 11340 EnumConstantSingleInitilizer, 11341 EnumConstantDifferentInitilizer, 11342 }; 11343 11344 // If we've already pointed out a specific problem with this enum, don't 11345 // bother issuing a general "something's different" diagnostic. 11346 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 11347 continue; 11348 11349 EnumDecl *FirstEnum = Merge.first; 11350 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum); 11351 11352 using DeclHashes = 11353 llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>; 11354 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum]( 11355 DeclHashes &Hashes, EnumDecl *Enum) { 11356 for (auto *D : Enum->decls()) { 11357 // Due to decl merging, the first EnumDecl is the parent of 11358 // Decls in both records. 11359 if (!ODRHash::isWhitelistedDecl(D, FirstEnum)) 11360 continue; 11361 assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind"); 11362 Hashes.emplace_back(cast<EnumConstantDecl>(D), 11363 ComputeSubDeclODRHash(D)); 11364 } 11365 }; 11366 DeclHashes FirstHashes; 11367 PopulateHashes(FirstHashes, FirstEnum); 11368 bool Diagnosed = false; 11369 for (auto &SecondEnum : Merge.second) { 11370 11371 if (FirstEnum == SecondEnum) 11372 continue; 11373 11374 std::string SecondModule = 11375 getOwningModuleNameForDiagnostic(SecondEnum); 11376 11377 auto ODRDiagError = [FirstEnum, &FirstModule, 11378 this](SourceLocation Loc, SourceRange Range, 11379 ODREnumDifference DiffType) { 11380 return Diag(Loc, diag::err_module_odr_violation_enum) 11381 << FirstEnum << FirstModule.empty() << FirstModule << Range 11382 << DiffType; 11383 }; 11384 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11385 SourceRange Range, 11386 ODREnumDifference DiffType) { 11387 return Diag(Loc, diag::note_module_odr_violation_enum) 11388 << SecondModule << Range << DiffType; 11389 }; 11390 11391 if (FirstEnum->isScoped() != SecondEnum->isScoped()) { 11392 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11393 SingleScopedEnum) 11394 << FirstEnum->isScoped(); 11395 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11396 SingleScopedEnum) 11397 << SecondEnum->isScoped(); 11398 Diagnosed = true; 11399 continue; 11400 } 11401 11402 if (FirstEnum->isScoped() && SecondEnum->isScoped()) { 11403 if (FirstEnum->isScopedUsingClassTag() != 11404 SecondEnum->isScopedUsingClassTag()) { 11405 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11406 EnumTagKeywordMismatch) 11407 << FirstEnum->isScopedUsingClassTag(); 11408 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11409 EnumTagKeywordMismatch) 11410 << SecondEnum->isScopedUsingClassTag(); 11411 Diagnosed = true; 11412 continue; 11413 } 11414 } 11415 11416 QualType FirstUnderlyingType = 11417 FirstEnum->getIntegerTypeSourceInfo() 11418 ? FirstEnum->getIntegerTypeSourceInfo()->getType() 11419 : QualType(); 11420 QualType SecondUnderlyingType = 11421 SecondEnum->getIntegerTypeSourceInfo() 11422 ? SecondEnum->getIntegerTypeSourceInfo()->getType() 11423 : QualType(); 11424 if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) { 11425 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11426 SingleSpecifiedType) 11427 << !FirstUnderlyingType.isNull(); 11428 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11429 SingleSpecifiedType) 11430 << !SecondUnderlyingType.isNull(); 11431 Diagnosed = true; 11432 continue; 11433 } 11434 11435 if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) { 11436 if (ComputeQualTypeODRHash(FirstUnderlyingType) != 11437 ComputeQualTypeODRHash(SecondUnderlyingType)) { 11438 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11439 DifferentSpecifiedTypes) 11440 << FirstUnderlyingType; 11441 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11442 DifferentSpecifiedTypes) 11443 << SecondUnderlyingType; 11444 Diagnosed = true; 11445 continue; 11446 } 11447 } 11448 11449 DeclHashes SecondHashes; 11450 PopulateHashes(SecondHashes, SecondEnum); 11451 11452 if (FirstHashes.size() != SecondHashes.size()) { 11453 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11454 DifferentNumberEnumConstants) 11455 << (int)FirstHashes.size(); 11456 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11457 DifferentNumberEnumConstants) 11458 << (int)SecondHashes.size(); 11459 Diagnosed = true; 11460 continue; 11461 } 11462 11463 for (unsigned I = 0; I < FirstHashes.size(); ++I) { 11464 if (FirstHashes[I].second == SecondHashes[I].second) 11465 continue; 11466 const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first; 11467 const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first; 11468 11469 if (FirstEnumConstant->getDeclName() != 11470 SecondEnumConstant->getDeclName()) { 11471 11472 ODRDiagError(FirstEnumConstant->getLocation(), 11473 FirstEnumConstant->getSourceRange(), EnumConstantName) 11474 << I + 1 << FirstEnumConstant; 11475 ODRDiagNote(SecondEnumConstant->getLocation(), 11476 SecondEnumConstant->getSourceRange(), EnumConstantName) 11477 << I + 1 << SecondEnumConstant; 11478 Diagnosed = true; 11479 break; 11480 } 11481 11482 const Expr *FirstInit = FirstEnumConstant->getInitExpr(); 11483 const Expr *SecondInit = SecondEnumConstant->getInitExpr(); 11484 if (!FirstInit && !SecondInit) 11485 continue; 11486 11487 if (!FirstInit || !SecondInit) { 11488 ODRDiagError(FirstEnumConstant->getLocation(), 11489 FirstEnumConstant->getSourceRange(), 11490 EnumConstantSingleInitilizer) 11491 << I + 1 << FirstEnumConstant << (FirstInit != nullptr); 11492 ODRDiagNote(SecondEnumConstant->getLocation(), 11493 SecondEnumConstant->getSourceRange(), 11494 EnumConstantSingleInitilizer) 11495 << I + 1 << SecondEnumConstant << (SecondInit != nullptr); 11496 Diagnosed = true; 11497 break; 11498 } 11499 11500 if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11501 ODRDiagError(FirstEnumConstant->getLocation(), 11502 FirstEnumConstant->getSourceRange(), 11503 EnumConstantDifferentInitilizer) 11504 << I + 1 << FirstEnumConstant; 11505 ODRDiagNote(SecondEnumConstant->getLocation(), 11506 SecondEnumConstant->getSourceRange(), 11507 EnumConstantDifferentInitilizer) 11508 << I + 1 << SecondEnumConstant; 11509 Diagnosed = true; 11510 break; 11511 } 11512 } 11513 } 11514 11515 (void)Diagnosed; 11516 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11517 } 11518 } 11519 11520 void ASTReader::StartedDeserializing() { 11521 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get()) 11522 ReadTimer->startTimer(); 11523 } 11524 11525 void ASTReader::FinishedDeserializing() { 11526 assert(NumCurrentElementsDeserializing && 11527 "FinishedDeserializing not paired with StartedDeserializing"); 11528 if (NumCurrentElementsDeserializing == 1) { 11529 // We decrease NumCurrentElementsDeserializing only after pending actions 11530 // are finished, to avoid recursively re-calling finishPendingActions(). 11531 finishPendingActions(); 11532 } 11533 --NumCurrentElementsDeserializing; 11534 11535 if (NumCurrentElementsDeserializing == 0) { 11536 // Propagate exception specification and deduced type updates along 11537 // redeclaration chains. 11538 // 11539 // We do this now rather than in finishPendingActions because we want to 11540 // be able to walk the complete redeclaration chains of the updated decls. 11541 while (!PendingExceptionSpecUpdates.empty() || 11542 !PendingDeducedTypeUpdates.empty()) { 11543 auto ESUpdates = std::move(PendingExceptionSpecUpdates); 11544 PendingExceptionSpecUpdates.clear(); 11545 for (auto Update : ESUpdates) { 11546 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11547 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>(); 11548 auto ESI = FPT->getExtProtoInfo().ExceptionSpec; 11549 if (auto *Listener = getContext().getASTMutationListener()) 11550 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second)); 11551 for (auto *Redecl : Update.second->redecls()) 11552 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI); 11553 } 11554 11555 auto DTUpdates = std::move(PendingDeducedTypeUpdates); 11556 PendingDeducedTypeUpdates.clear(); 11557 for (auto Update : DTUpdates) { 11558 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11559 // FIXME: If the return type is already deduced, check that it matches. 11560 getContext().adjustDeducedFunctionResultType(Update.first, 11561 Update.second); 11562 } 11563 } 11564 11565 if (ReadTimer) 11566 ReadTimer->stopTimer(); 11567 11568 diagnoseOdrViolations(); 11569 11570 // We are not in recursive loading, so it's safe to pass the "interesting" 11571 // decls to the consumer. 11572 if (Consumer) 11573 PassInterestingDeclsToConsumer(); 11574 } 11575 } 11576 11577 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) { 11578 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) { 11579 // Remove any fake results before adding any real ones. 11580 auto It = PendingFakeLookupResults.find(II); 11581 if (It != PendingFakeLookupResults.end()) { 11582 for (auto *ND : It->second) 11583 SemaObj->IdResolver.RemoveDecl(ND); 11584 // FIXME: this works around module+PCH performance issue. 11585 // Rather than erase the result from the map, which is O(n), just clear 11586 // the vector of NamedDecls. 11587 It->second.clear(); 11588 } 11589 } 11590 11591 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) { 11592 SemaObj->TUScope->AddDecl(D); 11593 } else if (SemaObj->TUScope) { 11594 // Adding the decl to IdResolver may have failed because it was already in 11595 // (even though it was not added in scope). If it is already in, make sure 11596 // it gets in the scope as well. 11597 if (std::find(SemaObj->IdResolver.begin(Name), 11598 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end()) 11599 SemaObj->TUScope->AddDecl(D); 11600 } 11601 } 11602 11603 ASTReader::ASTReader(Preprocessor &PP, ASTContext *Context, 11604 const PCHContainerReader &PCHContainerRdr, 11605 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, 11606 StringRef isysroot, bool DisableValidation, 11607 bool AllowASTWithCompilerErrors, 11608 bool AllowConfigurationMismatch, bool ValidateSystemInputs, 11609 bool UseGlobalIndex, 11610 std::unique_ptr<llvm::Timer> ReadTimer) 11611 : Listener(DisableValidation 11612 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP)) 11613 : cast<ASTReaderListener>(new PCHValidator(PP, *this))), 11614 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), 11615 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP), 11616 ContextObj(Context), 11617 ModuleMgr(PP.getFileManager(), PP.getPCMCache(), PCHContainerRdr, 11618 PP.getHeaderSearchInfo()), 11619 PCMCache(PP.getPCMCache()), DummyIdResolver(PP), 11620 ReadTimer(std::move(ReadTimer)), isysroot(isysroot), 11621 DisableValidation(DisableValidation), 11622 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors), 11623 AllowConfigurationMismatch(AllowConfigurationMismatch), 11624 ValidateSystemInputs(ValidateSystemInputs), 11625 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) { 11626 SourceMgr.setExternalSLocEntrySource(this); 11627 11628 for (const auto &Ext : Extensions) { 11629 auto BlockName = Ext->getExtensionMetadata().BlockName; 11630 auto Known = ModuleFileExtensions.find(BlockName); 11631 if (Known != ModuleFileExtensions.end()) { 11632 Diags.Report(diag::warn_duplicate_module_file_extension) 11633 << BlockName; 11634 continue; 11635 } 11636 11637 ModuleFileExtensions.insert({BlockName, Ext}); 11638 } 11639 } 11640 11641 ASTReader::~ASTReader() { 11642 if (OwnsDeserializationListener) 11643 delete DeserializationListener; 11644 } 11645 11646 IdentifierResolver &ASTReader::getIdResolver() { 11647 return SemaObj ? SemaObj->IdResolver : DummyIdResolver; 11648 } 11649 11650 unsigned ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor, 11651 unsigned AbbrevID) { 11652 Idx = 0; 11653 Record.clear(); 11654 return Cursor.readRecord(AbbrevID, Record); 11655 } 11656 //===----------------------------------------------------------------------===// 11657 //// OMPClauseReader implementation 11658 ////===----------------------------------------------------------------------===// 11659 11660 OMPClause *OMPClauseReader::readClause() { 11661 OMPClause *C; 11662 switch (Record.readInt()) { 11663 case OMPC_if: 11664 C = new (Context) OMPIfClause(); 11665 break; 11666 case OMPC_final: 11667 C = new (Context) OMPFinalClause(); 11668 break; 11669 case OMPC_num_threads: 11670 C = new (Context) OMPNumThreadsClause(); 11671 break; 11672 case OMPC_safelen: 11673 C = new (Context) OMPSafelenClause(); 11674 break; 11675 case OMPC_simdlen: 11676 C = new (Context) OMPSimdlenClause(); 11677 break; 11678 case OMPC_collapse: 11679 C = new (Context) OMPCollapseClause(); 11680 break; 11681 case OMPC_default: 11682 C = new (Context) OMPDefaultClause(); 11683 break; 11684 case OMPC_proc_bind: 11685 C = new (Context) OMPProcBindClause(); 11686 break; 11687 case OMPC_schedule: 11688 C = new (Context) OMPScheduleClause(); 11689 break; 11690 case OMPC_ordered: 11691 C = OMPOrderedClause::CreateEmpty(Context, Record.readInt()); 11692 break; 11693 case OMPC_nowait: 11694 C = new (Context) OMPNowaitClause(); 11695 break; 11696 case OMPC_untied: 11697 C = new (Context) OMPUntiedClause(); 11698 break; 11699 case OMPC_mergeable: 11700 C = new (Context) OMPMergeableClause(); 11701 break; 11702 case OMPC_read: 11703 C = new (Context) OMPReadClause(); 11704 break; 11705 case OMPC_write: 11706 C = new (Context) OMPWriteClause(); 11707 break; 11708 case OMPC_update: 11709 C = new (Context) OMPUpdateClause(); 11710 break; 11711 case OMPC_capture: 11712 C = new (Context) OMPCaptureClause(); 11713 break; 11714 case OMPC_seq_cst: 11715 C = new (Context) OMPSeqCstClause(); 11716 break; 11717 case OMPC_threads: 11718 C = new (Context) OMPThreadsClause(); 11719 break; 11720 case OMPC_simd: 11721 C = new (Context) OMPSIMDClause(); 11722 break; 11723 case OMPC_nogroup: 11724 C = new (Context) OMPNogroupClause(); 11725 break; 11726 case OMPC_unified_address: 11727 C = new (Context) OMPUnifiedAddressClause(); 11728 break; 11729 case OMPC_unified_shared_memory: 11730 C = new (Context) OMPUnifiedSharedMemoryClause(); 11731 break; 11732 case OMPC_reverse_offload: 11733 C = new (Context) OMPReverseOffloadClause(); 11734 break; 11735 case OMPC_dynamic_allocators: 11736 C = new (Context) OMPDynamicAllocatorsClause(); 11737 break; 11738 case OMPC_atomic_default_mem_order: 11739 C = new (Context) OMPAtomicDefaultMemOrderClause(); 11740 break; 11741 case OMPC_private: 11742 C = OMPPrivateClause::CreateEmpty(Context, Record.readInt()); 11743 break; 11744 case OMPC_firstprivate: 11745 C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt()); 11746 break; 11747 case OMPC_lastprivate: 11748 C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt()); 11749 break; 11750 case OMPC_shared: 11751 C = OMPSharedClause::CreateEmpty(Context, Record.readInt()); 11752 break; 11753 case OMPC_reduction: 11754 C = OMPReductionClause::CreateEmpty(Context, Record.readInt()); 11755 break; 11756 case OMPC_task_reduction: 11757 C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt()); 11758 break; 11759 case OMPC_in_reduction: 11760 C = OMPInReductionClause::CreateEmpty(Context, Record.readInt()); 11761 break; 11762 case OMPC_linear: 11763 C = OMPLinearClause::CreateEmpty(Context, Record.readInt()); 11764 break; 11765 case OMPC_aligned: 11766 C = OMPAlignedClause::CreateEmpty(Context, Record.readInt()); 11767 break; 11768 case OMPC_copyin: 11769 C = OMPCopyinClause::CreateEmpty(Context, Record.readInt()); 11770 break; 11771 case OMPC_copyprivate: 11772 C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt()); 11773 break; 11774 case OMPC_flush: 11775 C = OMPFlushClause::CreateEmpty(Context, Record.readInt()); 11776 break; 11777 case OMPC_depend: { 11778 unsigned NumVars = Record.readInt(); 11779 unsigned NumLoops = Record.readInt(); 11780 C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops); 11781 break; 11782 } 11783 case OMPC_device: 11784 C = new (Context) OMPDeviceClause(); 11785 break; 11786 case OMPC_map: { 11787 unsigned NumVars = Record.readInt(); 11788 unsigned NumDeclarations = Record.readInt(); 11789 unsigned NumLists = Record.readInt(); 11790 unsigned NumComponents = Record.readInt(); 11791 C = OMPMapClause::CreateEmpty(Context, NumVars, NumDeclarations, NumLists, 11792 NumComponents); 11793 break; 11794 } 11795 case OMPC_num_teams: 11796 C = new (Context) OMPNumTeamsClause(); 11797 break; 11798 case OMPC_thread_limit: 11799 C = new (Context) OMPThreadLimitClause(); 11800 break; 11801 case OMPC_priority: 11802 C = new (Context) OMPPriorityClause(); 11803 break; 11804 case OMPC_grainsize: 11805 C = new (Context) OMPGrainsizeClause(); 11806 break; 11807 case OMPC_num_tasks: 11808 C = new (Context) OMPNumTasksClause(); 11809 break; 11810 case OMPC_hint: 11811 C = new (Context) OMPHintClause(); 11812 break; 11813 case OMPC_dist_schedule: 11814 C = new (Context) OMPDistScheduleClause(); 11815 break; 11816 case OMPC_defaultmap: 11817 C = new (Context) OMPDefaultmapClause(); 11818 break; 11819 case OMPC_to: { 11820 unsigned NumVars = Record.readInt(); 11821 unsigned NumDeclarations = Record.readInt(); 11822 unsigned NumLists = Record.readInt(); 11823 unsigned NumComponents = Record.readInt(); 11824 C = OMPToClause::CreateEmpty(Context, NumVars, NumDeclarations, NumLists, 11825 NumComponents); 11826 break; 11827 } 11828 case OMPC_from: { 11829 unsigned NumVars = Record.readInt(); 11830 unsigned NumDeclarations = Record.readInt(); 11831 unsigned NumLists = Record.readInt(); 11832 unsigned NumComponents = Record.readInt(); 11833 C = OMPFromClause::CreateEmpty(Context, NumVars, NumDeclarations, NumLists, 11834 NumComponents); 11835 break; 11836 } 11837 case OMPC_use_device_ptr: { 11838 unsigned NumVars = Record.readInt(); 11839 unsigned NumDeclarations = Record.readInt(); 11840 unsigned NumLists = Record.readInt(); 11841 unsigned NumComponents = Record.readInt(); 11842 C = OMPUseDevicePtrClause::CreateEmpty(Context, NumVars, NumDeclarations, 11843 NumLists, NumComponents); 11844 break; 11845 } 11846 case OMPC_is_device_ptr: { 11847 unsigned NumVars = Record.readInt(); 11848 unsigned NumDeclarations = Record.readInt(); 11849 unsigned NumLists = Record.readInt(); 11850 unsigned NumComponents = Record.readInt(); 11851 C = OMPIsDevicePtrClause::CreateEmpty(Context, NumVars, NumDeclarations, 11852 NumLists, NumComponents); 11853 break; 11854 } 11855 } 11856 Visit(C); 11857 C->setLocStart(Record.readSourceLocation()); 11858 C->setLocEnd(Record.readSourceLocation()); 11859 11860 return C; 11861 } 11862 11863 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) { 11864 C->setPreInitStmt(Record.readSubStmt(), 11865 static_cast<OpenMPDirectiveKind>(Record.readInt())); 11866 } 11867 11868 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) { 11869 VisitOMPClauseWithPreInit(C); 11870 C->setPostUpdateExpr(Record.readSubExpr()); 11871 } 11872 11873 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) { 11874 VisitOMPClauseWithPreInit(C); 11875 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt())); 11876 C->setNameModifierLoc(Record.readSourceLocation()); 11877 C->setColonLoc(Record.readSourceLocation()); 11878 C->setCondition(Record.readSubExpr()); 11879 C->setLParenLoc(Record.readSourceLocation()); 11880 } 11881 11882 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) { 11883 C->setCondition(Record.readSubExpr()); 11884 C->setLParenLoc(Record.readSourceLocation()); 11885 } 11886 11887 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) { 11888 VisitOMPClauseWithPreInit(C); 11889 C->setNumThreads(Record.readSubExpr()); 11890 C->setLParenLoc(Record.readSourceLocation()); 11891 } 11892 11893 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) { 11894 C->setSafelen(Record.readSubExpr()); 11895 C->setLParenLoc(Record.readSourceLocation()); 11896 } 11897 11898 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) { 11899 C->setSimdlen(Record.readSubExpr()); 11900 C->setLParenLoc(Record.readSourceLocation()); 11901 } 11902 11903 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) { 11904 C->setNumForLoops(Record.readSubExpr()); 11905 C->setLParenLoc(Record.readSourceLocation()); 11906 } 11907 11908 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) { 11909 C->setDefaultKind( 11910 static_cast<OpenMPDefaultClauseKind>(Record.readInt())); 11911 C->setLParenLoc(Record.readSourceLocation()); 11912 C->setDefaultKindKwLoc(Record.readSourceLocation()); 11913 } 11914 11915 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) { 11916 C->setProcBindKind( 11917 static_cast<OpenMPProcBindClauseKind>(Record.readInt())); 11918 C->setLParenLoc(Record.readSourceLocation()); 11919 C->setProcBindKindKwLoc(Record.readSourceLocation()); 11920 } 11921 11922 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) { 11923 VisitOMPClauseWithPreInit(C); 11924 C->setScheduleKind( 11925 static_cast<OpenMPScheduleClauseKind>(Record.readInt())); 11926 C->setFirstScheduleModifier( 11927 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 11928 C->setSecondScheduleModifier( 11929 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 11930 C->setChunkSize(Record.readSubExpr()); 11931 C->setLParenLoc(Record.readSourceLocation()); 11932 C->setFirstScheduleModifierLoc(Record.readSourceLocation()); 11933 C->setSecondScheduleModifierLoc(Record.readSourceLocation()); 11934 C->setScheduleKindLoc(Record.readSourceLocation()); 11935 C->setCommaLoc(Record.readSourceLocation()); 11936 } 11937 11938 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) { 11939 C->setNumForLoops(Record.readSubExpr()); 11940 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 11941 C->setLoopNumIterations(I, Record.readSubExpr()); 11942 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 11943 C->setLoopCounter(I, Record.readSubExpr()); 11944 C->setLParenLoc(Record.readSourceLocation()); 11945 } 11946 11947 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {} 11948 11949 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {} 11950 11951 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {} 11952 11953 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {} 11954 11955 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {} 11956 11957 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *) {} 11958 11959 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {} 11960 11961 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {} 11962 11963 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {} 11964 11965 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {} 11966 11967 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {} 11968 11969 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {} 11970 11971 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause( 11972 OMPUnifiedSharedMemoryClause *) {} 11973 11974 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {} 11975 11976 void 11977 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) { 11978 } 11979 11980 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause( 11981 OMPAtomicDefaultMemOrderClause *C) { 11982 C->setAtomicDefaultMemOrderKind( 11983 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt())); 11984 C->setLParenLoc(Record.readSourceLocation()); 11985 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation()); 11986 } 11987 11988 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) { 11989 C->setLParenLoc(Record.readSourceLocation()); 11990 unsigned NumVars = C->varlist_size(); 11991 SmallVector<Expr *, 16> Vars; 11992 Vars.reserve(NumVars); 11993 for (unsigned i = 0; i != NumVars; ++i) 11994 Vars.push_back(Record.readSubExpr()); 11995 C->setVarRefs(Vars); 11996 Vars.clear(); 11997 for (unsigned i = 0; i != NumVars; ++i) 11998 Vars.push_back(Record.readSubExpr()); 11999 C->setPrivateCopies(Vars); 12000 } 12001 12002 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) { 12003 VisitOMPClauseWithPreInit(C); 12004 C->setLParenLoc(Record.readSourceLocation()); 12005 unsigned NumVars = C->varlist_size(); 12006 SmallVector<Expr *, 16> Vars; 12007 Vars.reserve(NumVars); 12008 for (unsigned i = 0; i != NumVars; ++i) 12009 Vars.push_back(Record.readSubExpr()); 12010 C->setVarRefs(Vars); 12011 Vars.clear(); 12012 for (unsigned i = 0; i != NumVars; ++i) 12013 Vars.push_back(Record.readSubExpr()); 12014 C->setPrivateCopies(Vars); 12015 Vars.clear(); 12016 for (unsigned i = 0; i != NumVars; ++i) 12017 Vars.push_back(Record.readSubExpr()); 12018 C->setInits(Vars); 12019 } 12020 12021 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) { 12022 VisitOMPClauseWithPostUpdate(C); 12023 C->setLParenLoc(Record.readSourceLocation()); 12024 unsigned NumVars = C->varlist_size(); 12025 SmallVector<Expr *, 16> Vars; 12026 Vars.reserve(NumVars); 12027 for (unsigned i = 0; i != NumVars; ++i) 12028 Vars.push_back(Record.readSubExpr()); 12029 C->setVarRefs(Vars); 12030 Vars.clear(); 12031 for (unsigned i = 0; i != NumVars; ++i) 12032 Vars.push_back(Record.readSubExpr()); 12033 C->setPrivateCopies(Vars); 12034 Vars.clear(); 12035 for (unsigned i = 0; i != NumVars; ++i) 12036 Vars.push_back(Record.readSubExpr()); 12037 C->setSourceExprs(Vars); 12038 Vars.clear(); 12039 for (unsigned i = 0; i != NumVars; ++i) 12040 Vars.push_back(Record.readSubExpr()); 12041 C->setDestinationExprs(Vars); 12042 Vars.clear(); 12043 for (unsigned i = 0; i != NumVars; ++i) 12044 Vars.push_back(Record.readSubExpr()); 12045 C->setAssignmentOps(Vars); 12046 } 12047 12048 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) { 12049 C->setLParenLoc(Record.readSourceLocation()); 12050 unsigned NumVars = C->varlist_size(); 12051 SmallVector<Expr *, 16> Vars; 12052 Vars.reserve(NumVars); 12053 for (unsigned i = 0; i != NumVars; ++i) 12054 Vars.push_back(Record.readSubExpr()); 12055 C->setVarRefs(Vars); 12056 } 12057 12058 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) { 12059 VisitOMPClauseWithPostUpdate(C); 12060 C->setLParenLoc(Record.readSourceLocation()); 12061 C->setColonLoc(Record.readSourceLocation()); 12062 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12063 DeclarationNameInfo DNI; 12064 Record.readDeclarationNameInfo(DNI); 12065 C->setQualifierLoc(NNSL); 12066 C->setNameInfo(DNI); 12067 12068 unsigned NumVars = C->varlist_size(); 12069 SmallVector<Expr *, 16> Vars; 12070 Vars.reserve(NumVars); 12071 for (unsigned i = 0; i != NumVars; ++i) 12072 Vars.push_back(Record.readSubExpr()); 12073 C->setVarRefs(Vars); 12074 Vars.clear(); 12075 for (unsigned i = 0; i != NumVars; ++i) 12076 Vars.push_back(Record.readSubExpr()); 12077 C->setPrivates(Vars); 12078 Vars.clear(); 12079 for (unsigned i = 0; i != NumVars; ++i) 12080 Vars.push_back(Record.readSubExpr()); 12081 C->setLHSExprs(Vars); 12082 Vars.clear(); 12083 for (unsigned i = 0; i != NumVars; ++i) 12084 Vars.push_back(Record.readSubExpr()); 12085 C->setRHSExprs(Vars); 12086 Vars.clear(); 12087 for (unsigned i = 0; i != NumVars; ++i) 12088 Vars.push_back(Record.readSubExpr()); 12089 C->setReductionOps(Vars); 12090 } 12091 12092 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) { 12093 VisitOMPClauseWithPostUpdate(C); 12094 C->setLParenLoc(Record.readSourceLocation()); 12095 C->setColonLoc(Record.readSourceLocation()); 12096 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12097 DeclarationNameInfo DNI; 12098 Record.readDeclarationNameInfo(DNI); 12099 C->setQualifierLoc(NNSL); 12100 C->setNameInfo(DNI); 12101 12102 unsigned NumVars = C->varlist_size(); 12103 SmallVector<Expr *, 16> Vars; 12104 Vars.reserve(NumVars); 12105 for (unsigned I = 0; I != NumVars; ++I) 12106 Vars.push_back(Record.readSubExpr()); 12107 C->setVarRefs(Vars); 12108 Vars.clear(); 12109 for (unsigned I = 0; I != NumVars; ++I) 12110 Vars.push_back(Record.readSubExpr()); 12111 C->setPrivates(Vars); 12112 Vars.clear(); 12113 for (unsigned I = 0; I != NumVars; ++I) 12114 Vars.push_back(Record.readSubExpr()); 12115 C->setLHSExprs(Vars); 12116 Vars.clear(); 12117 for (unsigned I = 0; I != NumVars; ++I) 12118 Vars.push_back(Record.readSubExpr()); 12119 C->setRHSExprs(Vars); 12120 Vars.clear(); 12121 for (unsigned I = 0; I != NumVars; ++I) 12122 Vars.push_back(Record.readSubExpr()); 12123 C->setReductionOps(Vars); 12124 } 12125 12126 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) { 12127 VisitOMPClauseWithPostUpdate(C); 12128 C->setLParenLoc(Record.readSourceLocation()); 12129 C->setColonLoc(Record.readSourceLocation()); 12130 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12131 DeclarationNameInfo DNI; 12132 Record.readDeclarationNameInfo(DNI); 12133 C->setQualifierLoc(NNSL); 12134 C->setNameInfo(DNI); 12135 12136 unsigned NumVars = C->varlist_size(); 12137 SmallVector<Expr *, 16> Vars; 12138 Vars.reserve(NumVars); 12139 for (unsigned I = 0; I != NumVars; ++I) 12140 Vars.push_back(Record.readSubExpr()); 12141 C->setVarRefs(Vars); 12142 Vars.clear(); 12143 for (unsigned I = 0; I != NumVars; ++I) 12144 Vars.push_back(Record.readSubExpr()); 12145 C->setPrivates(Vars); 12146 Vars.clear(); 12147 for (unsigned I = 0; I != NumVars; ++I) 12148 Vars.push_back(Record.readSubExpr()); 12149 C->setLHSExprs(Vars); 12150 Vars.clear(); 12151 for (unsigned I = 0; I != NumVars; ++I) 12152 Vars.push_back(Record.readSubExpr()); 12153 C->setRHSExprs(Vars); 12154 Vars.clear(); 12155 for (unsigned I = 0; I != NumVars; ++I) 12156 Vars.push_back(Record.readSubExpr()); 12157 C->setReductionOps(Vars); 12158 Vars.clear(); 12159 for (unsigned I = 0; I != NumVars; ++I) 12160 Vars.push_back(Record.readSubExpr()); 12161 C->setTaskgroupDescriptors(Vars); 12162 } 12163 12164 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) { 12165 VisitOMPClauseWithPostUpdate(C); 12166 C->setLParenLoc(Record.readSourceLocation()); 12167 C->setColonLoc(Record.readSourceLocation()); 12168 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt())); 12169 C->setModifierLoc(Record.readSourceLocation()); 12170 unsigned NumVars = C->varlist_size(); 12171 SmallVector<Expr *, 16> Vars; 12172 Vars.reserve(NumVars); 12173 for (unsigned i = 0; i != NumVars; ++i) 12174 Vars.push_back(Record.readSubExpr()); 12175 C->setVarRefs(Vars); 12176 Vars.clear(); 12177 for (unsigned i = 0; i != NumVars; ++i) 12178 Vars.push_back(Record.readSubExpr()); 12179 C->setPrivates(Vars); 12180 Vars.clear(); 12181 for (unsigned i = 0; i != NumVars; ++i) 12182 Vars.push_back(Record.readSubExpr()); 12183 C->setInits(Vars); 12184 Vars.clear(); 12185 for (unsigned i = 0; i != NumVars; ++i) 12186 Vars.push_back(Record.readSubExpr()); 12187 C->setUpdates(Vars); 12188 Vars.clear(); 12189 for (unsigned i = 0; i != NumVars; ++i) 12190 Vars.push_back(Record.readSubExpr()); 12191 C->setFinals(Vars); 12192 C->setStep(Record.readSubExpr()); 12193 C->setCalcStep(Record.readSubExpr()); 12194 } 12195 12196 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) { 12197 C->setLParenLoc(Record.readSourceLocation()); 12198 C->setColonLoc(Record.readSourceLocation()); 12199 unsigned NumVars = C->varlist_size(); 12200 SmallVector<Expr *, 16> Vars; 12201 Vars.reserve(NumVars); 12202 for (unsigned i = 0; i != NumVars; ++i) 12203 Vars.push_back(Record.readSubExpr()); 12204 C->setVarRefs(Vars); 12205 C->setAlignment(Record.readSubExpr()); 12206 } 12207 12208 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) { 12209 C->setLParenLoc(Record.readSourceLocation()); 12210 unsigned NumVars = C->varlist_size(); 12211 SmallVector<Expr *, 16> Exprs; 12212 Exprs.reserve(NumVars); 12213 for (unsigned i = 0; i != NumVars; ++i) 12214 Exprs.push_back(Record.readSubExpr()); 12215 C->setVarRefs(Exprs); 12216 Exprs.clear(); 12217 for (unsigned i = 0; i != NumVars; ++i) 12218 Exprs.push_back(Record.readSubExpr()); 12219 C->setSourceExprs(Exprs); 12220 Exprs.clear(); 12221 for (unsigned i = 0; i != NumVars; ++i) 12222 Exprs.push_back(Record.readSubExpr()); 12223 C->setDestinationExprs(Exprs); 12224 Exprs.clear(); 12225 for (unsigned i = 0; i != NumVars; ++i) 12226 Exprs.push_back(Record.readSubExpr()); 12227 C->setAssignmentOps(Exprs); 12228 } 12229 12230 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) { 12231 C->setLParenLoc(Record.readSourceLocation()); 12232 unsigned NumVars = C->varlist_size(); 12233 SmallVector<Expr *, 16> Exprs; 12234 Exprs.reserve(NumVars); 12235 for (unsigned i = 0; i != NumVars; ++i) 12236 Exprs.push_back(Record.readSubExpr()); 12237 C->setVarRefs(Exprs); 12238 Exprs.clear(); 12239 for (unsigned i = 0; i != NumVars; ++i) 12240 Exprs.push_back(Record.readSubExpr()); 12241 C->setSourceExprs(Exprs); 12242 Exprs.clear(); 12243 for (unsigned i = 0; i != NumVars; ++i) 12244 Exprs.push_back(Record.readSubExpr()); 12245 C->setDestinationExprs(Exprs); 12246 Exprs.clear(); 12247 for (unsigned i = 0; i != NumVars; ++i) 12248 Exprs.push_back(Record.readSubExpr()); 12249 C->setAssignmentOps(Exprs); 12250 } 12251 12252 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) { 12253 C->setLParenLoc(Record.readSourceLocation()); 12254 unsigned NumVars = C->varlist_size(); 12255 SmallVector<Expr *, 16> Vars; 12256 Vars.reserve(NumVars); 12257 for (unsigned i = 0; i != NumVars; ++i) 12258 Vars.push_back(Record.readSubExpr()); 12259 C->setVarRefs(Vars); 12260 } 12261 12262 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) { 12263 C->setLParenLoc(Record.readSourceLocation()); 12264 C->setDependencyKind( 12265 static_cast<OpenMPDependClauseKind>(Record.readInt())); 12266 C->setDependencyLoc(Record.readSourceLocation()); 12267 C->setColonLoc(Record.readSourceLocation()); 12268 unsigned NumVars = C->varlist_size(); 12269 SmallVector<Expr *, 16> Vars; 12270 Vars.reserve(NumVars); 12271 for (unsigned I = 0; I != NumVars; ++I) 12272 Vars.push_back(Record.readSubExpr()); 12273 C->setVarRefs(Vars); 12274 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I) 12275 C->setLoopData(I, Record.readSubExpr()); 12276 } 12277 12278 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) { 12279 VisitOMPClauseWithPreInit(C); 12280 C->setDevice(Record.readSubExpr()); 12281 C->setLParenLoc(Record.readSourceLocation()); 12282 } 12283 12284 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) { 12285 C->setLParenLoc(Record.readSourceLocation()); 12286 for (unsigned I = 0; I < OMPMapClause::NumberOfModifiers; ++I) { 12287 C->setMapTypeModifier( 12288 I, static_cast<OpenMPMapModifierKind>(Record.readInt())); 12289 C->setMapTypeModifierLoc(I, Record.readSourceLocation()); 12290 } 12291 C->setMapType( 12292 static_cast<OpenMPMapClauseKind>(Record.readInt())); 12293 C->setMapLoc(Record.readSourceLocation()); 12294 C->setColonLoc(Record.readSourceLocation()); 12295 auto NumVars = C->varlist_size(); 12296 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12297 auto TotalLists = C->getTotalComponentListNum(); 12298 auto TotalComponents = C->getTotalComponentsNum(); 12299 12300 SmallVector<Expr *, 16> Vars; 12301 Vars.reserve(NumVars); 12302 for (unsigned i = 0; i != NumVars; ++i) 12303 Vars.push_back(Record.readExpr()); 12304 C->setVarRefs(Vars); 12305 12306 SmallVector<ValueDecl *, 16> Decls; 12307 Decls.reserve(UniqueDecls); 12308 for (unsigned i = 0; i < UniqueDecls; ++i) 12309 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12310 C->setUniqueDecls(Decls); 12311 12312 SmallVector<unsigned, 16> ListsPerDecl; 12313 ListsPerDecl.reserve(UniqueDecls); 12314 for (unsigned i = 0; i < UniqueDecls; ++i) 12315 ListsPerDecl.push_back(Record.readInt()); 12316 C->setDeclNumLists(ListsPerDecl); 12317 12318 SmallVector<unsigned, 32> ListSizes; 12319 ListSizes.reserve(TotalLists); 12320 for (unsigned i = 0; i < TotalLists; ++i) 12321 ListSizes.push_back(Record.readInt()); 12322 C->setComponentListSizes(ListSizes); 12323 12324 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12325 Components.reserve(TotalComponents); 12326 for (unsigned i = 0; i < TotalComponents; ++i) { 12327 Expr *AssociatedExpr = Record.readExpr(); 12328 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12329 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12330 AssociatedExpr, AssociatedDecl)); 12331 } 12332 C->setComponents(Components, ListSizes); 12333 } 12334 12335 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) { 12336 VisitOMPClauseWithPreInit(C); 12337 C->setNumTeams(Record.readSubExpr()); 12338 C->setLParenLoc(Record.readSourceLocation()); 12339 } 12340 12341 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) { 12342 VisitOMPClauseWithPreInit(C); 12343 C->setThreadLimit(Record.readSubExpr()); 12344 C->setLParenLoc(Record.readSourceLocation()); 12345 } 12346 12347 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) { 12348 C->setPriority(Record.readSubExpr()); 12349 C->setLParenLoc(Record.readSourceLocation()); 12350 } 12351 12352 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) { 12353 C->setGrainsize(Record.readSubExpr()); 12354 C->setLParenLoc(Record.readSourceLocation()); 12355 } 12356 12357 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) { 12358 C->setNumTasks(Record.readSubExpr()); 12359 C->setLParenLoc(Record.readSourceLocation()); 12360 } 12361 12362 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) { 12363 C->setHint(Record.readSubExpr()); 12364 C->setLParenLoc(Record.readSourceLocation()); 12365 } 12366 12367 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) { 12368 VisitOMPClauseWithPreInit(C); 12369 C->setDistScheduleKind( 12370 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt())); 12371 C->setChunkSize(Record.readSubExpr()); 12372 C->setLParenLoc(Record.readSourceLocation()); 12373 C->setDistScheduleKindLoc(Record.readSourceLocation()); 12374 C->setCommaLoc(Record.readSourceLocation()); 12375 } 12376 12377 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) { 12378 C->setDefaultmapKind( 12379 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt())); 12380 C->setDefaultmapModifier( 12381 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt())); 12382 C->setLParenLoc(Record.readSourceLocation()); 12383 C->setDefaultmapModifierLoc(Record.readSourceLocation()); 12384 C->setDefaultmapKindLoc(Record.readSourceLocation()); 12385 } 12386 12387 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) { 12388 C->setLParenLoc(Record.readSourceLocation()); 12389 auto NumVars = C->varlist_size(); 12390 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12391 auto TotalLists = C->getTotalComponentListNum(); 12392 auto TotalComponents = C->getTotalComponentsNum(); 12393 12394 SmallVector<Expr *, 16> Vars; 12395 Vars.reserve(NumVars); 12396 for (unsigned i = 0; i != NumVars; ++i) 12397 Vars.push_back(Record.readSubExpr()); 12398 C->setVarRefs(Vars); 12399 12400 SmallVector<ValueDecl *, 16> Decls; 12401 Decls.reserve(UniqueDecls); 12402 for (unsigned i = 0; i < UniqueDecls; ++i) 12403 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12404 C->setUniqueDecls(Decls); 12405 12406 SmallVector<unsigned, 16> ListsPerDecl; 12407 ListsPerDecl.reserve(UniqueDecls); 12408 for (unsigned i = 0; i < UniqueDecls; ++i) 12409 ListsPerDecl.push_back(Record.readInt()); 12410 C->setDeclNumLists(ListsPerDecl); 12411 12412 SmallVector<unsigned, 32> ListSizes; 12413 ListSizes.reserve(TotalLists); 12414 for (unsigned i = 0; i < TotalLists; ++i) 12415 ListSizes.push_back(Record.readInt()); 12416 C->setComponentListSizes(ListSizes); 12417 12418 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12419 Components.reserve(TotalComponents); 12420 for (unsigned i = 0; i < TotalComponents; ++i) { 12421 Expr *AssociatedExpr = Record.readSubExpr(); 12422 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12423 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12424 AssociatedExpr, AssociatedDecl)); 12425 } 12426 C->setComponents(Components, ListSizes); 12427 } 12428 12429 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) { 12430 C->setLParenLoc(Record.readSourceLocation()); 12431 auto NumVars = C->varlist_size(); 12432 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12433 auto TotalLists = C->getTotalComponentListNum(); 12434 auto TotalComponents = C->getTotalComponentsNum(); 12435 12436 SmallVector<Expr *, 16> Vars; 12437 Vars.reserve(NumVars); 12438 for (unsigned i = 0; i != NumVars; ++i) 12439 Vars.push_back(Record.readSubExpr()); 12440 C->setVarRefs(Vars); 12441 12442 SmallVector<ValueDecl *, 16> Decls; 12443 Decls.reserve(UniqueDecls); 12444 for (unsigned i = 0; i < UniqueDecls; ++i) 12445 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12446 C->setUniqueDecls(Decls); 12447 12448 SmallVector<unsigned, 16> ListsPerDecl; 12449 ListsPerDecl.reserve(UniqueDecls); 12450 for (unsigned i = 0; i < UniqueDecls; ++i) 12451 ListsPerDecl.push_back(Record.readInt()); 12452 C->setDeclNumLists(ListsPerDecl); 12453 12454 SmallVector<unsigned, 32> ListSizes; 12455 ListSizes.reserve(TotalLists); 12456 for (unsigned i = 0; i < TotalLists; ++i) 12457 ListSizes.push_back(Record.readInt()); 12458 C->setComponentListSizes(ListSizes); 12459 12460 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12461 Components.reserve(TotalComponents); 12462 for (unsigned i = 0; i < TotalComponents; ++i) { 12463 Expr *AssociatedExpr = Record.readSubExpr(); 12464 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12465 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12466 AssociatedExpr, AssociatedDecl)); 12467 } 12468 C->setComponents(Components, ListSizes); 12469 } 12470 12471 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) { 12472 C->setLParenLoc(Record.readSourceLocation()); 12473 auto NumVars = C->varlist_size(); 12474 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12475 auto TotalLists = C->getTotalComponentListNum(); 12476 auto TotalComponents = C->getTotalComponentsNum(); 12477 12478 SmallVector<Expr *, 16> Vars; 12479 Vars.reserve(NumVars); 12480 for (unsigned i = 0; i != NumVars; ++i) 12481 Vars.push_back(Record.readSubExpr()); 12482 C->setVarRefs(Vars); 12483 Vars.clear(); 12484 for (unsigned i = 0; i != NumVars; ++i) 12485 Vars.push_back(Record.readSubExpr()); 12486 C->setPrivateCopies(Vars); 12487 Vars.clear(); 12488 for (unsigned i = 0; i != NumVars; ++i) 12489 Vars.push_back(Record.readSubExpr()); 12490 C->setInits(Vars); 12491 12492 SmallVector<ValueDecl *, 16> Decls; 12493 Decls.reserve(UniqueDecls); 12494 for (unsigned i = 0; i < UniqueDecls; ++i) 12495 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12496 C->setUniqueDecls(Decls); 12497 12498 SmallVector<unsigned, 16> ListsPerDecl; 12499 ListsPerDecl.reserve(UniqueDecls); 12500 for (unsigned i = 0; i < UniqueDecls; ++i) 12501 ListsPerDecl.push_back(Record.readInt()); 12502 C->setDeclNumLists(ListsPerDecl); 12503 12504 SmallVector<unsigned, 32> ListSizes; 12505 ListSizes.reserve(TotalLists); 12506 for (unsigned i = 0; i < TotalLists; ++i) 12507 ListSizes.push_back(Record.readInt()); 12508 C->setComponentListSizes(ListSizes); 12509 12510 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12511 Components.reserve(TotalComponents); 12512 for (unsigned i = 0; i < TotalComponents; ++i) { 12513 Expr *AssociatedExpr = Record.readSubExpr(); 12514 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12515 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12516 AssociatedExpr, AssociatedDecl)); 12517 } 12518 C->setComponents(Components, ListSizes); 12519 } 12520 12521 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) { 12522 C->setLParenLoc(Record.readSourceLocation()); 12523 auto NumVars = C->varlist_size(); 12524 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12525 auto TotalLists = C->getTotalComponentListNum(); 12526 auto TotalComponents = C->getTotalComponentsNum(); 12527 12528 SmallVector<Expr *, 16> Vars; 12529 Vars.reserve(NumVars); 12530 for (unsigned i = 0; i != NumVars; ++i) 12531 Vars.push_back(Record.readSubExpr()); 12532 C->setVarRefs(Vars); 12533 Vars.clear(); 12534 12535 SmallVector<ValueDecl *, 16> Decls; 12536 Decls.reserve(UniqueDecls); 12537 for (unsigned i = 0; i < UniqueDecls; ++i) 12538 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12539 C->setUniqueDecls(Decls); 12540 12541 SmallVector<unsigned, 16> ListsPerDecl; 12542 ListsPerDecl.reserve(UniqueDecls); 12543 for (unsigned i = 0; i < UniqueDecls; ++i) 12544 ListsPerDecl.push_back(Record.readInt()); 12545 C->setDeclNumLists(ListsPerDecl); 12546 12547 SmallVector<unsigned, 32> ListSizes; 12548 ListSizes.reserve(TotalLists); 12549 for (unsigned i = 0; i < TotalLists; ++i) 12550 ListSizes.push_back(Record.readInt()); 12551 C->setComponentListSizes(ListSizes); 12552 12553 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12554 Components.reserve(TotalComponents); 12555 for (unsigned i = 0; i < TotalComponents; ++i) { 12556 Expr *AssociatedExpr = Record.readSubExpr(); 12557 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12558 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12559 AssociatedExpr, AssociatedDecl)); 12560 } 12561 C->setComponents(Components, ListSizes); 12562 } 12563