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/Basic/OpenMPKinds.h" 14 #include "clang/Serialization/ASTRecordReader.h" 15 #include "ASTCommon.h" 16 #include "ASTReaderInternals.h" 17 #include "clang/AST/AbstractTypeReader.h" 18 #include "clang/AST/ASTConsumer.h" 19 #include "clang/AST/ASTContext.h" 20 #include "clang/AST/ASTMutationListener.h" 21 #include "clang/AST/ASTUnresolvedSet.h" 22 #include "clang/AST/Decl.h" 23 #include "clang/AST/DeclBase.h" 24 #include "clang/AST/DeclCXX.h" 25 #include "clang/AST/DeclFriend.h" 26 #include "clang/AST/DeclGroup.h" 27 #include "clang/AST/DeclObjC.h" 28 #include "clang/AST/DeclTemplate.h" 29 #include "clang/AST/DeclarationName.h" 30 #include "clang/AST/Expr.h" 31 #include "clang/AST/ExprCXX.h" 32 #include "clang/AST/ExternalASTSource.h" 33 #include "clang/AST/NestedNameSpecifier.h" 34 #include "clang/AST/OpenMPClause.h" 35 #include "clang/AST/ODRHash.h" 36 #include "clang/AST/RawCommentList.h" 37 #include "clang/AST/TemplateBase.h" 38 #include "clang/AST/TemplateName.h" 39 #include "clang/AST/Type.h" 40 #include "clang/AST/TypeLoc.h" 41 #include "clang/AST/TypeLocVisitor.h" 42 #include "clang/AST/UnresolvedSet.h" 43 #include "clang/Basic/CommentOptions.h" 44 #include "clang/Basic/Diagnostic.h" 45 #include "clang/Basic/DiagnosticOptions.h" 46 #include "clang/Basic/ExceptionSpecificationType.h" 47 #include "clang/Basic/FileManager.h" 48 #include "clang/Basic/FileSystemOptions.h" 49 #include "clang/Basic/IdentifierTable.h" 50 #include "clang/Basic/LLVM.h" 51 #include "clang/Basic/LangOptions.h" 52 #include "clang/Basic/Module.h" 53 #include "clang/Basic/ObjCRuntime.h" 54 #include "clang/Basic/OperatorKinds.h" 55 #include "clang/Basic/PragmaKinds.h" 56 #include "clang/Basic/Sanitizers.h" 57 #include "clang/Basic/SourceLocation.h" 58 #include "clang/Basic/SourceManager.h" 59 #include "clang/Basic/SourceManagerInternals.h" 60 #include "clang/Basic/Specifiers.h" 61 #include "clang/Basic/TargetInfo.h" 62 #include "clang/Basic/TargetOptions.h" 63 #include "clang/Basic/TokenKinds.h" 64 #include "clang/Basic/Version.h" 65 #include "clang/Lex/HeaderSearch.h" 66 #include "clang/Lex/HeaderSearchOptions.h" 67 #include "clang/Lex/MacroInfo.h" 68 #include "clang/Lex/ModuleMap.h" 69 #include "clang/Lex/PreprocessingRecord.h" 70 #include "clang/Lex/Preprocessor.h" 71 #include "clang/Lex/PreprocessorOptions.h" 72 #include "clang/Lex/Token.h" 73 #include "clang/Sema/ObjCMethodList.h" 74 #include "clang/Sema/Scope.h" 75 #include "clang/Sema/Sema.h" 76 #include "clang/Sema/Weak.h" 77 #include "clang/Serialization/ASTBitCodes.h" 78 #include "clang/Serialization/ASTDeserializationListener.h" 79 #include "clang/Serialization/ContinuousRangeMap.h" 80 #include "clang/Serialization/GlobalModuleIndex.h" 81 #include "clang/Serialization/InMemoryModuleCache.h" 82 #include "clang/Serialization/ModuleFile.h" 83 #include "clang/Serialization/ModuleFileExtension.h" 84 #include "clang/Serialization/ModuleManager.h" 85 #include "clang/Serialization/PCHContainerOperations.h" 86 #include "clang/Serialization/SerializationDiagnostic.h" 87 #include "llvm/ADT/APFloat.h" 88 #include "llvm/ADT/APInt.h" 89 #include "llvm/ADT/APSInt.h" 90 #include "llvm/ADT/ArrayRef.h" 91 #include "llvm/ADT/DenseMap.h" 92 #include "llvm/ADT/FloatingPointMode.h" 93 #include "llvm/ADT/FoldingSet.h" 94 #include "llvm/ADT/Hashing.h" 95 #include "llvm/ADT/IntrusiveRefCntPtr.h" 96 #include "llvm/ADT/None.h" 97 #include "llvm/ADT/Optional.h" 98 #include "llvm/ADT/STLExtras.h" 99 #include "llvm/ADT/ScopeExit.h" 100 #include "llvm/ADT/SmallPtrSet.h" 101 #include "llvm/ADT/SmallString.h" 102 #include "llvm/ADT/SmallVector.h" 103 #include "llvm/ADT/StringExtras.h" 104 #include "llvm/ADT/StringMap.h" 105 #include "llvm/ADT/StringRef.h" 106 #include "llvm/ADT/Triple.h" 107 #include "llvm/ADT/iterator_range.h" 108 #include "llvm/Bitstream/BitstreamReader.h" 109 #include "llvm/Support/Casting.h" 110 #include "llvm/Support/Compiler.h" 111 #include "llvm/Support/Compression.h" 112 #include "llvm/Support/DJB.h" 113 #include "llvm/Support/Endian.h" 114 #include "llvm/Support/Error.h" 115 #include "llvm/Support/ErrorHandling.h" 116 #include "llvm/Support/FileSystem.h" 117 #include "llvm/Support/MemoryBuffer.h" 118 #include "llvm/Support/Path.h" 119 #include "llvm/Support/SaveAndRestore.h" 120 #include "llvm/Support/Timer.h" 121 #include "llvm/Support/VersionTuple.h" 122 #include "llvm/Support/raw_ostream.h" 123 #include <algorithm> 124 #include <cassert> 125 #include <cstddef> 126 #include <cstdint> 127 #include <cstdio> 128 #include <ctime> 129 #include <iterator> 130 #include <limits> 131 #include <map> 132 #include <memory> 133 #include <string> 134 #include <system_error> 135 #include <tuple> 136 #include <utility> 137 #include <vector> 138 139 using namespace clang; 140 using namespace clang::serialization; 141 using namespace clang::serialization::reader; 142 using llvm::BitstreamCursor; 143 using llvm::RoundingMode; 144 145 //===----------------------------------------------------------------------===// 146 // ChainedASTReaderListener implementation 147 //===----------------------------------------------------------------------===// 148 149 bool 150 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) { 151 return First->ReadFullVersionInformation(FullVersion) || 152 Second->ReadFullVersionInformation(FullVersion); 153 } 154 155 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) { 156 First->ReadModuleName(ModuleName); 157 Second->ReadModuleName(ModuleName); 158 } 159 160 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) { 161 First->ReadModuleMapFile(ModuleMapPath); 162 Second->ReadModuleMapFile(ModuleMapPath); 163 } 164 165 bool 166 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts, 167 bool Complain, 168 bool AllowCompatibleDifferences) { 169 return First->ReadLanguageOptions(LangOpts, Complain, 170 AllowCompatibleDifferences) || 171 Second->ReadLanguageOptions(LangOpts, Complain, 172 AllowCompatibleDifferences); 173 } 174 175 bool ChainedASTReaderListener::ReadTargetOptions( 176 const TargetOptions &TargetOpts, bool Complain, 177 bool AllowCompatibleDifferences) { 178 return First->ReadTargetOptions(TargetOpts, Complain, 179 AllowCompatibleDifferences) || 180 Second->ReadTargetOptions(TargetOpts, Complain, 181 AllowCompatibleDifferences); 182 } 183 184 bool ChainedASTReaderListener::ReadDiagnosticOptions( 185 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 186 return First->ReadDiagnosticOptions(DiagOpts, Complain) || 187 Second->ReadDiagnosticOptions(DiagOpts, Complain); 188 } 189 190 bool 191 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts, 192 bool Complain) { 193 return First->ReadFileSystemOptions(FSOpts, Complain) || 194 Second->ReadFileSystemOptions(FSOpts, Complain); 195 } 196 197 bool ChainedASTReaderListener::ReadHeaderSearchOptions( 198 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath, 199 bool Complain) { 200 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 201 Complain) || 202 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 203 Complain); 204 } 205 206 bool ChainedASTReaderListener::ReadPreprocessorOptions( 207 const PreprocessorOptions &PPOpts, bool Complain, 208 std::string &SuggestedPredefines) { 209 return First->ReadPreprocessorOptions(PPOpts, Complain, 210 SuggestedPredefines) || 211 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines); 212 } 213 214 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M, 215 unsigned Value) { 216 First->ReadCounter(M, Value); 217 Second->ReadCounter(M, Value); 218 } 219 220 bool ChainedASTReaderListener::needsInputFileVisitation() { 221 return First->needsInputFileVisitation() || 222 Second->needsInputFileVisitation(); 223 } 224 225 bool ChainedASTReaderListener::needsSystemInputFileVisitation() { 226 return First->needsSystemInputFileVisitation() || 227 Second->needsSystemInputFileVisitation(); 228 } 229 230 void ChainedASTReaderListener::visitModuleFile(StringRef Filename, 231 ModuleKind Kind) { 232 First->visitModuleFile(Filename, Kind); 233 Second->visitModuleFile(Filename, Kind); 234 } 235 236 bool ChainedASTReaderListener::visitInputFile(StringRef Filename, 237 bool isSystem, 238 bool isOverridden, 239 bool isExplicitModule) { 240 bool Continue = false; 241 if (First->needsInputFileVisitation() && 242 (!isSystem || First->needsSystemInputFileVisitation())) 243 Continue |= First->visitInputFile(Filename, isSystem, isOverridden, 244 isExplicitModule); 245 if (Second->needsInputFileVisitation() && 246 (!isSystem || Second->needsSystemInputFileVisitation())) 247 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden, 248 isExplicitModule); 249 return Continue; 250 } 251 252 void ChainedASTReaderListener::readModuleFileExtension( 253 const ModuleFileExtensionMetadata &Metadata) { 254 First->readModuleFileExtension(Metadata); 255 Second->readModuleFileExtension(Metadata); 256 } 257 258 //===----------------------------------------------------------------------===// 259 // PCH validator implementation 260 //===----------------------------------------------------------------------===// 261 262 ASTReaderListener::~ASTReaderListener() = default; 263 264 /// Compare the given set of language options against an existing set of 265 /// language options. 266 /// 267 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 268 /// \param AllowCompatibleDifferences If true, differences between compatible 269 /// language options will be permitted. 270 /// 271 /// \returns true if the languagae options mis-match, false otherwise. 272 static bool checkLanguageOptions(const LangOptions &LangOpts, 273 const LangOptions &ExistingLangOpts, 274 DiagnosticsEngine *Diags, 275 bool AllowCompatibleDifferences = true) { 276 #define LANGOPT(Name, Bits, Default, Description) \ 277 if (ExistingLangOpts.Name != LangOpts.Name) { \ 278 if (Diags) \ 279 Diags->Report(diag::err_pch_langopt_mismatch) \ 280 << Description << LangOpts.Name << ExistingLangOpts.Name; \ 281 return true; \ 282 } 283 284 #define VALUE_LANGOPT(Name, Bits, Default, Description) \ 285 if (ExistingLangOpts.Name != LangOpts.Name) { \ 286 if (Diags) \ 287 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 288 << Description; \ 289 return true; \ 290 } 291 292 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 293 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \ 294 if (Diags) \ 295 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 296 << Description; \ 297 return true; \ 298 } 299 300 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \ 301 if (!AllowCompatibleDifferences) \ 302 LANGOPT(Name, Bits, Default, Description) 303 304 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \ 305 if (!AllowCompatibleDifferences) \ 306 ENUM_LANGOPT(Name, Bits, Default, Description) 307 308 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \ 309 if (!AllowCompatibleDifferences) \ 310 VALUE_LANGOPT(Name, Bits, Default, Description) 311 312 #define BENIGN_LANGOPT(Name, Bits, Default, Description) 313 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) 314 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description) 315 #include "clang/Basic/LangOptions.def" 316 317 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) { 318 if (Diags) 319 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features"; 320 return true; 321 } 322 323 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) { 324 if (Diags) 325 Diags->Report(diag::err_pch_langopt_value_mismatch) 326 << "target Objective-C runtime"; 327 return true; 328 } 329 330 if (ExistingLangOpts.CommentOpts.BlockCommandNames != 331 LangOpts.CommentOpts.BlockCommandNames) { 332 if (Diags) 333 Diags->Report(diag::err_pch_langopt_value_mismatch) 334 << "block command names"; 335 return true; 336 } 337 338 // Sanitizer feature mismatches are treated as compatible differences. If 339 // compatible differences aren't allowed, we still only want to check for 340 // mismatches of non-modular sanitizers (the only ones which can affect AST 341 // generation). 342 if (!AllowCompatibleDifferences) { 343 SanitizerMask ModularSanitizers = getPPTransparentSanitizers(); 344 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize; 345 SanitizerSet ImportedSanitizers = LangOpts.Sanitize; 346 ExistingSanitizers.clear(ModularSanitizers); 347 ImportedSanitizers.clear(ModularSanitizers); 348 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) { 349 const std::string Flag = "-fsanitize="; 350 if (Diags) { 351 #define SANITIZER(NAME, ID) \ 352 { \ 353 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \ 354 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \ 355 if (InExistingModule != InImportedModule) \ 356 Diags->Report(diag::err_pch_targetopt_feature_mismatch) \ 357 << InExistingModule << (Flag + NAME); \ 358 } 359 #include "clang/Basic/Sanitizers.def" 360 } 361 return true; 362 } 363 } 364 365 return false; 366 } 367 368 /// Compare the given set of target options against an existing set of 369 /// target options. 370 /// 371 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 372 /// 373 /// \returns true if the target options mis-match, false otherwise. 374 static bool checkTargetOptions(const TargetOptions &TargetOpts, 375 const TargetOptions &ExistingTargetOpts, 376 DiagnosticsEngine *Diags, 377 bool AllowCompatibleDifferences = true) { 378 #define CHECK_TARGET_OPT(Field, Name) \ 379 if (TargetOpts.Field != ExistingTargetOpts.Field) { \ 380 if (Diags) \ 381 Diags->Report(diag::err_pch_targetopt_mismatch) \ 382 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \ 383 return true; \ 384 } 385 386 // The triple and ABI must match exactly. 387 CHECK_TARGET_OPT(Triple, "target"); 388 CHECK_TARGET_OPT(ABI, "target ABI"); 389 390 // We can tolerate different CPUs in many cases, notably when one CPU 391 // supports a strict superset of another. When allowing compatible 392 // differences skip this check. 393 if (!AllowCompatibleDifferences) { 394 CHECK_TARGET_OPT(CPU, "target CPU"); 395 CHECK_TARGET_OPT(TuneCPU, "tune CPU"); 396 } 397 398 #undef CHECK_TARGET_OPT 399 400 // Compare feature sets. 401 SmallVector<StringRef, 4> ExistingFeatures( 402 ExistingTargetOpts.FeaturesAsWritten.begin(), 403 ExistingTargetOpts.FeaturesAsWritten.end()); 404 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(), 405 TargetOpts.FeaturesAsWritten.end()); 406 llvm::sort(ExistingFeatures); 407 llvm::sort(ReadFeatures); 408 409 // We compute the set difference in both directions explicitly so that we can 410 // diagnose the differences differently. 411 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures; 412 std::set_difference( 413 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(), 414 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures)); 415 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(), 416 ExistingFeatures.begin(), ExistingFeatures.end(), 417 std::back_inserter(UnmatchedReadFeatures)); 418 419 // If we are allowing compatible differences and the read feature set is 420 // a strict subset of the existing feature set, there is nothing to diagnose. 421 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty()) 422 return false; 423 424 if (Diags) { 425 for (StringRef Feature : UnmatchedReadFeatures) 426 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 427 << /* is-existing-feature */ false << Feature; 428 for (StringRef Feature : UnmatchedExistingFeatures) 429 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 430 << /* is-existing-feature */ true << Feature; 431 } 432 433 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty(); 434 } 435 436 bool 437 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts, 438 bool Complain, 439 bool AllowCompatibleDifferences) { 440 const LangOptions &ExistingLangOpts = PP.getLangOpts(); 441 return checkLanguageOptions(LangOpts, ExistingLangOpts, 442 Complain ? &Reader.Diags : nullptr, 443 AllowCompatibleDifferences); 444 } 445 446 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts, 447 bool Complain, 448 bool AllowCompatibleDifferences) { 449 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts(); 450 return checkTargetOptions(TargetOpts, ExistingTargetOpts, 451 Complain ? &Reader.Diags : nullptr, 452 AllowCompatibleDifferences); 453 } 454 455 namespace { 456 457 using MacroDefinitionsMap = 458 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>; 459 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>; 460 461 } // namespace 462 463 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags, 464 DiagnosticsEngine &Diags, 465 bool Complain) { 466 using Level = DiagnosticsEngine::Level; 467 468 // Check current mappings for new -Werror mappings, and the stored mappings 469 // for cases that were explicitly mapped to *not* be errors that are now 470 // errors because of options like -Werror. 471 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags }; 472 473 for (DiagnosticsEngine *MappingSource : MappingSources) { 474 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) { 475 diag::kind DiagID = DiagIDMappingPair.first; 476 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation()); 477 if (CurLevel < DiagnosticsEngine::Error) 478 continue; // not significant 479 Level StoredLevel = 480 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation()); 481 if (StoredLevel < DiagnosticsEngine::Error) { 482 if (Complain) 483 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" + 484 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str(); 485 return true; 486 } 487 } 488 } 489 490 return false; 491 } 492 493 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) { 494 diag::Severity Ext = Diags.getExtensionHandlingBehavior(); 495 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors()) 496 return true; 497 return Ext >= diag::Severity::Error; 498 } 499 500 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags, 501 DiagnosticsEngine &Diags, 502 bool IsSystem, bool Complain) { 503 // Top-level options 504 if (IsSystem) { 505 if (Diags.getSuppressSystemWarnings()) 506 return false; 507 // If -Wsystem-headers was not enabled before, be conservative 508 if (StoredDiags.getSuppressSystemWarnings()) { 509 if (Complain) 510 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers"; 511 return true; 512 } 513 } 514 515 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) { 516 if (Complain) 517 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror"; 518 return true; 519 } 520 521 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() && 522 !StoredDiags.getEnableAllWarnings()) { 523 if (Complain) 524 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror"; 525 return true; 526 } 527 528 if (isExtHandlingFromDiagsError(Diags) && 529 !isExtHandlingFromDiagsError(StoredDiags)) { 530 if (Complain) 531 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors"; 532 return true; 533 } 534 535 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain); 536 } 537 538 /// Return the top import module if it is implicit, nullptr otherwise. 539 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr, 540 Preprocessor &PP) { 541 // If the original import came from a file explicitly generated by the user, 542 // don't check the diagnostic mappings. 543 // FIXME: currently this is approximated by checking whether this is not a 544 // module import of an implicitly-loaded module file. 545 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in 546 // the transitive closure of its imports, since unrelated modules cannot be 547 // imported until after this module finishes validation. 548 ModuleFile *TopImport = &*ModuleMgr.rbegin(); 549 while (!TopImport->ImportedBy.empty()) 550 TopImport = TopImport->ImportedBy[0]; 551 if (TopImport->Kind != MK_ImplicitModule) 552 return nullptr; 553 554 StringRef ModuleName = TopImport->ModuleName; 555 assert(!ModuleName.empty() && "diagnostic options read before module name"); 556 557 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName); 558 assert(M && "missing module"); 559 return M; 560 } 561 562 bool PCHValidator::ReadDiagnosticOptions( 563 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 564 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics(); 565 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs()); 566 IntrusiveRefCntPtr<DiagnosticsEngine> Diags( 567 new DiagnosticsEngine(DiagIDs, DiagOpts.get())); 568 // This should never fail, because we would have processed these options 569 // before writing them to an ASTFile. 570 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false); 571 572 ModuleManager &ModuleMgr = Reader.getModuleManager(); 573 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then"); 574 575 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP); 576 if (!TopM) 577 return false; 578 579 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that 580 // contains the union of their flags. 581 return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem, 582 Complain); 583 } 584 585 /// Collect the macro definitions provided by the given preprocessor 586 /// options. 587 static void 588 collectMacroDefinitions(const PreprocessorOptions &PPOpts, 589 MacroDefinitionsMap &Macros, 590 SmallVectorImpl<StringRef> *MacroNames = nullptr) { 591 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) { 592 StringRef Macro = PPOpts.Macros[I].first; 593 bool IsUndef = PPOpts.Macros[I].second; 594 595 std::pair<StringRef, StringRef> MacroPair = Macro.split('='); 596 StringRef MacroName = MacroPair.first; 597 StringRef MacroBody = MacroPair.second; 598 599 // For an #undef'd macro, we only care about the name. 600 if (IsUndef) { 601 if (MacroNames && !Macros.count(MacroName)) 602 MacroNames->push_back(MacroName); 603 604 Macros[MacroName] = std::make_pair("", true); 605 continue; 606 } 607 608 // For a #define'd macro, figure out the actual definition. 609 if (MacroName.size() == Macro.size()) 610 MacroBody = "1"; 611 else { 612 // Note: GCC drops anything following an end-of-line character. 613 StringRef::size_type End = MacroBody.find_first_of("\n\r"); 614 MacroBody = MacroBody.substr(0, End); 615 } 616 617 if (MacroNames && !Macros.count(MacroName)) 618 MacroNames->push_back(MacroName); 619 Macros[MacroName] = std::make_pair(MacroBody, false); 620 } 621 } 622 623 /// Check the preprocessor options deserialized from the control block 624 /// against the preprocessor options in an existing preprocessor. 625 /// 626 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 627 /// \param Validate If true, validate preprocessor options. If false, allow 628 /// macros defined by \p ExistingPPOpts to override those defined by 629 /// \p PPOpts in SuggestedPredefines. 630 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts, 631 const PreprocessorOptions &ExistingPPOpts, 632 DiagnosticsEngine *Diags, 633 FileManager &FileMgr, 634 std::string &SuggestedPredefines, 635 const LangOptions &LangOpts, 636 bool Validate = true) { 637 // Check macro definitions. 638 MacroDefinitionsMap ASTFileMacros; 639 collectMacroDefinitions(PPOpts, ASTFileMacros); 640 MacroDefinitionsMap ExistingMacros; 641 SmallVector<StringRef, 4> ExistingMacroNames; 642 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames); 643 644 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) { 645 // Dig out the macro definition in the existing preprocessor options. 646 StringRef MacroName = ExistingMacroNames[I]; 647 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName]; 648 649 // Check whether we know anything about this macro name or not. 650 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known = 651 ASTFileMacros.find(MacroName); 652 if (!Validate || Known == ASTFileMacros.end()) { 653 // FIXME: Check whether this identifier was referenced anywhere in the 654 // AST file. If so, we should reject the AST file. Unfortunately, this 655 // information isn't in the control block. What shall we do about it? 656 657 if (Existing.second) { 658 SuggestedPredefines += "#undef "; 659 SuggestedPredefines += MacroName.str(); 660 SuggestedPredefines += '\n'; 661 } else { 662 SuggestedPredefines += "#define "; 663 SuggestedPredefines += MacroName.str(); 664 SuggestedPredefines += ' '; 665 SuggestedPredefines += Existing.first.str(); 666 SuggestedPredefines += '\n'; 667 } 668 continue; 669 } 670 671 // If the macro was defined in one but undef'd in the other, we have a 672 // conflict. 673 if (Existing.second != Known->second.second) { 674 if (Diags) { 675 Diags->Report(diag::err_pch_macro_def_undef) 676 << MacroName << Known->second.second; 677 } 678 return true; 679 } 680 681 // If the macro was #undef'd in both, or if the macro bodies are identical, 682 // it's fine. 683 if (Existing.second || Existing.first == Known->second.first) 684 continue; 685 686 // The macro bodies differ; complain. 687 if (Diags) { 688 Diags->Report(diag::err_pch_macro_def_conflict) 689 << MacroName << Known->second.first << Existing.first; 690 } 691 return true; 692 } 693 694 // Check whether we're using predefines. 695 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) { 696 if (Diags) { 697 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines; 698 } 699 return true; 700 } 701 702 // Detailed record is important since it is used for the module cache hash. 703 if (LangOpts.Modules && 704 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) { 705 if (Diags) { 706 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord; 707 } 708 return true; 709 } 710 711 // Compute the #include and #include_macros lines we need. 712 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) { 713 StringRef File = ExistingPPOpts.Includes[I]; 714 715 if (!ExistingPPOpts.ImplicitPCHInclude.empty() && 716 !ExistingPPOpts.PCHThroughHeader.empty()) { 717 // In case the through header is an include, we must add all the includes 718 // to the predefines so the start point can be determined. 719 SuggestedPredefines += "#include \""; 720 SuggestedPredefines += File; 721 SuggestedPredefines += "\"\n"; 722 continue; 723 } 724 725 if (File == ExistingPPOpts.ImplicitPCHInclude) 726 continue; 727 728 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File) 729 != PPOpts.Includes.end()) 730 continue; 731 732 SuggestedPredefines += "#include \""; 733 SuggestedPredefines += File; 734 SuggestedPredefines += "\"\n"; 735 } 736 737 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) { 738 StringRef File = ExistingPPOpts.MacroIncludes[I]; 739 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(), 740 File) 741 != PPOpts.MacroIncludes.end()) 742 continue; 743 744 SuggestedPredefines += "#__include_macros \""; 745 SuggestedPredefines += File; 746 SuggestedPredefines += "\"\n##\n"; 747 } 748 749 return false; 750 } 751 752 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 753 bool Complain, 754 std::string &SuggestedPredefines) { 755 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts(); 756 757 return checkPreprocessorOptions(PPOpts, ExistingPPOpts, 758 Complain? &Reader.Diags : nullptr, 759 PP.getFileManager(), 760 SuggestedPredefines, 761 PP.getLangOpts()); 762 } 763 764 bool SimpleASTReaderListener::ReadPreprocessorOptions( 765 const PreprocessorOptions &PPOpts, 766 bool Complain, 767 std::string &SuggestedPredefines) { 768 return checkPreprocessorOptions(PPOpts, 769 PP.getPreprocessorOpts(), 770 nullptr, 771 PP.getFileManager(), 772 SuggestedPredefines, 773 PP.getLangOpts(), 774 false); 775 } 776 777 /// Check the header search options deserialized from the control block 778 /// against the header search options in an existing preprocessor. 779 /// 780 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 781 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 782 StringRef SpecificModuleCachePath, 783 StringRef ExistingModuleCachePath, 784 DiagnosticsEngine *Diags, 785 const LangOptions &LangOpts) { 786 if (LangOpts.Modules) { 787 if (SpecificModuleCachePath != ExistingModuleCachePath) { 788 if (Diags) 789 Diags->Report(diag::err_pch_modulecache_mismatch) 790 << SpecificModuleCachePath << ExistingModuleCachePath; 791 return true; 792 } 793 } 794 795 return false; 796 } 797 798 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 799 StringRef SpecificModuleCachePath, 800 bool Complain) { 801 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 802 PP.getHeaderSearchInfo().getModuleCachePath(), 803 Complain ? &Reader.Diags : nullptr, 804 PP.getLangOpts()); 805 } 806 807 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) { 808 PP.setCounterValue(Value); 809 } 810 811 //===----------------------------------------------------------------------===// 812 // AST reader implementation 813 //===----------------------------------------------------------------------===// 814 815 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener, 816 bool TakeOwnership) { 817 DeserializationListener = Listener; 818 OwnsDeserializationListener = TakeOwnership; 819 } 820 821 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) { 822 return serialization::ComputeHash(Sel); 823 } 824 825 std::pair<unsigned, unsigned> 826 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 827 using namespace llvm::support; 828 829 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 830 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 831 return std::make_pair(KeyLen, DataLen); 832 } 833 834 ASTSelectorLookupTrait::internal_key_type 835 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) { 836 using namespace llvm::support; 837 838 SelectorTable &SelTable = Reader.getContext().Selectors; 839 unsigned N = endian::readNext<uint16_t, little, unaligned>(d); 840 IdentifierInfo *FirstII = Reader.getLocalIdentifier( 841 F, endian::readNext<uint32_t, little, unaligned>(d)); 842 if (N == 0) 843 return SelTable.getNullarySelector(FirstII); 844 else if (N == 1) 845 return SelTable.getUnarySelector(FirstII); 846 847 SmallVector<IdentifierInfo *, 16> Args; 848 Args.push_back(FirstII); 849 for (unsigned I = 1; I != N; ++I) 850 Args.push_back(Reader.getLocalIdentifier( 851 F, endian::readNext<uint32_t, little, unaligned>(d))); 852 853 return SelTable.getSelector(N, Args.data()); 854 } 855 856 ASTSelectorLookupTrait::data_type 857 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d, 858 unsigned DataLen) { 859 using namespace llvm::support; 860 861 data_type Result; 862 863 Result.ID = Reader.getGlobalSelectorID( 864 F, endian::readNext<uint32_t, little, unaligned>(d)); 865 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d); 866 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d); 867 Result.InstanceBits = FullInstanceBits & 0x3; 868 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1; 869 Result.FactoryBits = FullFactoryBits & 0x3; 870 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1; 871 unsigned NumInstanceMethods = FullInstanceBits >> 3; 872 unsigned NumFactoryMethods = FullFactoryBits >> 3; 873 874 // Load instance methods 875 for (unsigned I = 0; I != NumInstanceMethods; ++I) { 876 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 877 F, endian::readNext<uint32_t, little, unaligned>(d))) 878 Result.Instance.push_back(Method); 879 } 880 881 // Load factory methods 882 for (unsigned I = 0; I != NumFactoryMethods; ++I) { 883 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 884 F, endian::readNext<uint32_t, little, unaligned>(d))) 885 Result.Factory.push_back(Method); 886 } 887 888 return Result; 889 } 890 891 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) { 892 return llvm::djbHash(a); 893 } 894 895 std::pair<unsigned, unsigned> 896 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) { 897 using namespace llvm::support; 898 899 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 900 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 901 return std::make_pair(KeyLen, DataLen); 902 } 903 904 ASTIdentifierLookupTraitBase::internal_key_type 905 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) { 906 assert(n >= 2 && d[n-1] == '\0'); 907 return StringRef((const char*) d, n-1); 908 } 909 910 /// Whether the given identifier is "interesting". 911 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II, 912 bool IsModule) { 913 return II.hadMacroDefinition() || II.isPoisoned() || 914 (!IsModule && II.getObjCOrBuiltinID()) || 915 II.hasRevertedTokenIDToIdentifier() || 916 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) && 917 II.getFETokenInfo()); 918 } 919 920 static bool readBit(unsigned &Bits) { 921 bool Value = Bits & 0x1; 922 Bits >>= 1; 923 return Value; 924 } 925 926 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) { 927 using namespace llvm::support; 928 929 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 930 return Reader.getGlobalIdentifierID(F, RawID >> 1); 931 } 932 933 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) { 934 if (!II.isFromAST()) { 935 II.setIsFromAST(); 936 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr; 937 if (isInterestingIdentifier(Reader, II, IsModule)) 938 II.setChangedSinceDeserialization(); 939 } 940 } 941 942 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k, 943 const unsigned char* d, 944 unsigned DataLen) { 945 using namespace llvm::support; 946 947 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 948 bool IsInteresting = RawID & 0x01; 949 950 // Wipe out the "is interesting" bit. 951 RawID = RawID >> 1; 952 953 // Build the IdentifierInfo and link the identifier ID with it. 954 IdentifierInfo *II = KnownII; 955 if (!II) { 956 II = &Reader.getIdentifierTable().getOwn(k); 957 KnownII = II; 958 } 959 markIdentifierFromAST(Reader, *II); 960 Reader.markIdentifierUpToDate(II); 961 962 IdentID ID = Reader.getGlobalIdentifierID(F, RawID); 963 if (!IsInteresting) { 964 // For uninteresting identifiers, there's nothing else to do. Just notify 965 // the reader that we've finished loading this identifier. 966 Reader.SetIdentifierInfo(ID, II); 967 return II; 968 } 969 970 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d); 971 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d); 972 bool CPlusPlusOperatorKeyword = readBit(Bits); 973 bool HasRevertedTokenIDToIdentifier = readBit(Bits); 974 bool Poisoned = readBit(Bits); 975 bool ExtensionToken = readBit(Bits); 976 bool HadMacroDefinition = readBit(Bits); 977 978 assert(Bits == 0 && "Extra bits in the identifier?"); 979 DataLen -= 8; 980 981 // Set or check the various bits in the IdentifierInfo structure. 982 // Token IDs are read-only. 983 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier) 984 II->revertTokenIDToIdentifier(); 985 if (!F.isModule()) 986 II->setObjCOrBuiltinID(ObjCOrBuiltinID); 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 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1151 Error(std::move(Err)); 1152 return true; 1153 } 1154 1155 RecordData Record; 1156 StringRef Blob; 1157 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1158 if (!MaybeCode) { 1159 Error(MaybeCode.takeError()); 1160 return true; 1161 } 1162 unsigned Code = MaybeCode.get(); 1163 1164 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1165 if (!MaybeRecCode) { 1166 Error(MaybeRecCode.takeError()); 1167 return true; 1168 } 1169 unsigned RecCode = MaybeRecCode.get(); 1170 if (RecCode != DECL_CONTEXT_LEXICAL) { 1171 Error("Expected lexical block"); 1172 return true; 1173 } 1174 1175 assert(!isa<TranslationUnitDecl>(DC) && 1176 "expected a TU_UPDATE_LEXICAL record for TU"); 1177 // If we are handling a C++ class template instantiation, we can see multiple 1178 // lexical updates for the same record. It's important that we select only one 1179 // of them, so that field numbering works properly. Just pick the first one we 1180 // see. 1181 auto &Lex = LexicalDecls[DC]; 1182 if (!Lex.first) { 1183 Lex = std::make_pair( 1184 &M, llvm::makeArrayRef( 1185 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 1186 Blob.data()), 1187 Blob.size() / 4)); 1188 } 1189 DC->setHasExternalLexicalStorage(true); 1190 return false; 1191 } 1192 1193 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M, 1194 BitstreamCursor &Cursor, 1195 uint64_t Offset, 1196 DeclID ID) { 1197 assert(Offset != 0); 1198 1199 SavedStreamPosition SavedPosition(Cursor); 1200 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1201 Error(std::move(Err)); 1202 return true; 1203 } 1204 1205 RecordData Record; 1206 StringRef Blob; 1207 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1208 if (!MaybeCode) { 1209 Error(MaybeCode.takeError()); 1210 return true; 1211 } 1212 unsigned Code = MaybeCode.get(); 1213 1214 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1215 if (!MaybeRecCode) { 1216 Error(MaybeRecCode.takeError()); 1217 return true; 1218 } 1219 unsigned RecCode = MaybeRecCode.get(); 1220 if (RecCode != DECL_CONTEXT_VISIBLE) { 1221 Error("Expected visible lookup table block"); 1222 return true; 1223 } 1224 1225 // We can't safely determine the primary context yet, so delay attaching the 1226 // lookup table until we're done with recursive deserialization. 1227 auto *Data = (const unsigned char*)Blob.data(); 1228 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data}); 1229 return false; 1230 } 1231 1232 void ASTReader::Error(StringRef Msg) const { 1233 Error(diag::err_fe_pch_malformed, Msg); 1234 if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() && 1235 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) { 1236 Diag(diag::note_module_cache_path) 1237 << PP.getHeaderSearchInfo().getModuleCachePath(); 1238 } 1239 } 1240 1241 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2, 1242 StringRef Arg3) const { 1243 if (Diags.isDiagnosticInFlight()) 1244 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3); 1245 else 1246 Diag(DiagID) << Arg1 << Arg2 << Arg3; 1247 } 1248 1249 void ASTReader::Error(llvm::Error &&Err) const { 1250 Error(toString(std::move(Err))); 1251 } 1252 1253 //===----------------------------------------------------------------------===// 1254 // Source Manager Deserialization 1255 //===----------------------------------------------------------------------===// 1256 1257 /// Read the line table in the source manager block. 1258 /// \returns true if there was an error. 1259 bool ASTReader::ParseLineTable(ModuleFile &F, 1260 const RecordData &Record) { 1261 unsigned Idx = 0; 1262 LineTableInfo &LineTable = SourceMgr.getLineTable(); 1263 1264 // Parse the file names 1265 std::map<int, int> FileIDs; 1266 FileIDs[-1] = -1; // For unspecified filenames. 1267 for (unsigned I = 0; Record[Idx]; ++I) { 1268 // Extract the file name 1269 auto Filename = ReadPath(F, Record, Idx); 1270 FileIDs[I] = LineTable.getLineTableFilenameID(Filename); 1271 } 1272 ++Idx; 1273 1274 // Parse the line entries 1275 std::vector<LineEntry> Entries; 1276 while (Idx < Record.size()) { 1277 int FID = Record[Idx++]; 1278 assert(FID >= 0 && "Serialized line entries for non-local file."); 1279 // Remap FileID from 1-based old view. 1280 FID += F.SLocEntryBaseID - 1; 1281 1282 // Extract the line entries 1283 unsigned NumEntries = Record[Idx++]; 1284 assert(NumEntries && "no line entries for file ID"); 1285 Entries.clear(); 1286 Entries.reserve(NumEntries); 1287 for (unsigned I = 0; I != NumEntries; ++I) { 1288 unsigned FileOffset = Record[Idx++]; 1289 unsigned LineNo = Record[Idx++]; 1290 int FilenameID = FileIDs[Record[Idx++]]; 1291 SrcMgr::CharacteristicKind FileKind 1292 = (SrcMgr::CharacteristicKind)Record[Idx++]; 1293 unsigned IncludeOffset = Record[Idx++]; 1294 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, 1295 FileKind, IncludeOffset)); 1296 } 1297 LineTable.AddEntry(FileID::get(FID), Entries); 1298 } 1299 1300 return false; 1301 } 1302 1303 /// Read a source manager block 1304 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) { 1305 using namespace SrcMgr; 1306 1307 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor; 1308 1309 // Set the source-location entry cursor to the current position in 1310 // the stream. This cursor will be used to read the contents of the 1311 // source manager block initially, and then lazily read 1312 // source-location entries as needed. 1313 SLocEntryCursor = F.Stream; 1314 1315 // The stream itself is going to skip over the source manager block. 1316 if (llvm::Error Err = F.Stream.SkipBlock()) { 1317 Error(std::move(Err)); 1318 return true; 1319 } 1320 1321 // Enter the source manager block. 1322 if (llvm::Error Err = 1323 SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) { 1324 Error(std::move(Err)); 1325 return true; 1326 } 1327 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo(); 1328 1329 RecordData Record; 1330 while (true) { 1331 Expected<llvm::BitstreamEntry> MaybeE = 1332 SLocEntryCursor.advanceSkippingSubblocks(); 1333 if (!MaybeE) { 1334 Error(MaybeE.takeError()); 1335 return true; 1336 } 1337 llvm::BitstreamEntry E = MaybeE.get(); 1338 1339 switch (E.Kind) { 1340 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1341 case llvm::BitstreamEntry::Error: 1342 Error("malformed block record in AST file"); 1343 return true; 1344 case llvm::BitstreamEntry::EndBlock: 1345 return false; 1346 case llvm::BitstreamEntry::Record: 1347 // The interesting case. 1348 break; 1349 } 1350 1351 // Read a record. 1352 Record.clear(); 1353 StringRef Blob; 1354 Expected<unsigned> MaybeRecord = 1355 SLocEntryCursor.readRecord(E.ID, Record, &Blob); 1356 if (!MaybeRecord) { 1357 Error(MaybeRecord.takeError()); 1358 return true; 1359 } 1360 switch (MaybeRecord.get()) { 1361 default: // Default behavior: ignore. 1362 break; 1363 1364 case SM_SLOC_FILE_ENTRY: 1365 case SM_SLOC_BUFFER_ENTRY: 1366 case SM_SLOC_EXPANSION_ENTRY: 1367 // Once we hit one of the source location entries, we're done. 1368 return false; 1369 } 1370 } 1371 } 1372 1373 /// If a header file is not found at the path that we expect it to be 1374 /// and the PCH file was moved from its original location, try to resolve the 1375 /// file by assuming that header+PCH were moved together and the header is in 1376 /// the same place relative to the PCH. 1377 static std::string 1378 resolveFileRelativeToOriginalDir(const std::string &Filename, 1379 const std::string &OriginalDir, 1380 const std::string &CurrDir) { 1381 assert(OriginalDir != CurrDir && 1382 "No point trying to resolve the file if the PCH dir didn't change"); 1383 1384 using namespace llvm::sys; 1385 1386 SmallString<128> filePath(Filename); 1387 fs::make_absolute(filePath); 1388 assert(path::is_absolute(OriginalDir)); 1389 SmallString<128> currPCHPath(CurrDir); 1390 1391 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)), 1392 fileDirE = path::end(path::parent_path(filePath)); 1393 path::const_iterator origDirI = path::begin(OriginalDir), 1394 origDirE = path::end(OriginalDir); 1395 // Skip the common path components from filePath and OriginalDir. 1396 while (fileDirI != fileDirE && origDirI != origDirE && 1397 *fileDirI == *origDirI) { 1398 ++fileDirI; 1399 ++origDirI; 1400 } 1401 for (; origDirI != origDirE; ++origDirI) 1402 path::append(currPCHPath, ".."); 1403 path::append(currPCHPath, fileDirI, fileDirE); 1404 path::append(currPCHPath, path::filename(Filename)); 1405 return std::string(currPCHPath.str()); 1406 } 1407 1408 bool ASTReader::ReadSLocEntry(int ID) { 1409 if (ID == 0) 1410 return false; 1411 1412 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1413 Error("source location entry ID out-of-range for AST file"); 1414 return true; 1415 } 1416 1417 // Local helper to read the (possibly-compressed) buffer data following the 1418 // entry record. 1419 auto ReadBuffer = [this]( 1420 BitstreamCursor &SLocEntryCursor, 1421 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> { 1422 RecordData Record; 1423 StringRef Blob; 1424 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode(); 1425 if (!MaybeCode) { 1426 Error(MaybeCode.takeError()); 1427 return nullptr; 1428 } 1429 unsigned Code = MaybeCode.get(); 1430 1431 Expected<unsigned> MaybeRecCode = 1432 SLocEntryCursor.readRecord(Code, Record, &Blob); 1433 if (!MaybeRecCode) { 1434 Error(MaybeRecCode.takeError()); 1435 return nullptr; 1436 } 1437 unsigned RecCode = MaybeRecCode.get(); 1438 1439 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) { 1440 if (!llvm::zlib::isAvailable()) { 1441 Error("zlib is not available"); 1442 return nullptr; 1443 } 1444 SmallString<0> Uncompressed; 1445 if (llvm::Error E = 1446 llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) { 1447 Error("could not decompress embedded file contents: " + 1448 llvm::toString(std::move(E))); 1449 return nullptr; 1450 } 1451 return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name); 1452 } else if (RecCode == SM_SLOC_BUFFER_BLOB) { 1453 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true); 1454 } else { 1455 Error("AST record has invalid code"); 1456 return nullptr; 1457 } 1458 }; 1459 1460 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second; 1461 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit( 1462 F->SLocEntryOffsetsBase + 1463 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) { 1464 Error(std::move(Err)); 1465 return true; 1466 } 1467 1468 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor; 1469 unsigned BaseOffset = F->SLocEntryBaseOffset; 1470 1471 ++NumSLocEntriesRead; 1472 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance(); 1473 if (!MaybeEntry) { 1474 Error(MaybeEntry.takeError()); 1475 return true; 1476 } 1477 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1478 1479 if (Entry.Kind != llvm::BitstreamEntry::Record) { 1480 Error("incorrectly-formatted source location entry in AST file"); 1481 return true; 1482 } 1483 1484 RecordData Record; 1485 StringRef Blob; 1486 Expected<unsigned> MaybeSLOC = 1487 SLocEntryCursor.readRecord(Entry.ID, Record, &Blob); 1488 if (!MaybeSLOC) { 1489 Error(MaybeSLOC.takeError()); 1490 return true; 1491 } 1492 switch (MaybeSLOC.get()) { 1493 default: 1494 Error("incorrectly-formatted source location entry in AST file"); 1495 return true; 1496 1497 case SM_SLOC_FILE_ENTRY: { 1498 // We will detect whether a file changed and return 'Failure' for it, but 1499 // we will also try to fail gracefully by setting up the SLocEntry. 1500 unsigned InputID = Record[4]; 1501 InputFile IF = getInputFile(*F, InputID); 1502 const FileEntry *File = IF.getFile(); 1503 bool OverriddenBuffer = IF.isOverridden(); 1504 1505 // Note that we only check if a File was returned. If it was out-of-date 1506 // we have complained but we will continue creating a FileID to recover 1507 // gracefully. 1508 if (!File) 1509 return true; 1510 1511 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1512 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) { 1513 // This is the module's main file. 1514 IncludeLoc = getImportLocation(F); 1515 } 1516 SrcMgr::CharacteristicKind 1517 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1518 // FIXME: The FileID should be created from the FileEntryRef. 1519 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter, 1520 ID, BaseOffset + Record[0]); 1521 SrcMgr::FileInfo &FileInfo = 1522 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()); 1523 FileInfo.NumCreatedFIDs = Record[5]; 1524 if (Record[3]) 1525 FileInfo.setHasLineDirectives(); 1526 1527 unsigned NumFileDecls = Record[7]; 1528 if (NumFileDecls && ContextObj) { 1529 const DeclID *FirstDecl = F->FileSortedDecls + Record[6]; 1530 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?"); 1531 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl, 1532 NumFileDecls)); 1533 } 1534 1535 const SrcMgr::ContentCache &ContentCache = 1536 SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter)); 1537 if (OverriddenBuffer && !ContentCache.BufferOverridden && 1538 ContentCache.ContentsEntry == ContentCache.OrigEntry && 1539 !ContentCache.getBufferIfLoaded()) { 1540 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName()); 1541 if (!Buffer) 1542 return true; 1543 SourceMgr.overrideFileContents(File, std::move(Buffer)); 1544 } 1545 1546 break; 1547 } 1548 1549 case SM_SLOC_BUFFER_ENTRY: { 1550 const char *Name = Blob.data(); 1551 unsigned Offset = Record[0]; 1552 SrcMgr::CharacteristicKind 1553 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1554 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1555 if (IncludeLoc.isInvalid() && F->isModule()) { 1556 IncludeLoc = getImportLocation(F); 1557 } 1558 1559 auto Buffer = ReadBuffer(SLocEntryCursor, Name); 1560 if (!Buffer) 1561 return true; 1562 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID, 1563 BaseOffset + Offset, IncludeLoc); 1564 break; 1565 } 1566 1567 case SM_SLOC_EXPANSION_ENTRY: { 1568 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]); 1569 SourceMgr.createExpansionLoc(SpellingLoc, 1570 ReadSourceLocation(*F, Record[2]), 1571 ReadSourceLocation(*F, Record[3]), 1572 Record[5], 1573 Record[4], 1574 ID, 1575 BaseOffset + Record[0]); 1576 break; 1577 } 1578 } 1579 1580 return false; 1581 } 1582 1583 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) { 1584 if (ID == 0) 1585 return std::make_pair(SourceLocation(), ""); 1586 1587 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1588 Error("source location entry ID out-of-range for AST file"); 1589 return std::make_pair(SourceLocation(), ""); 1590 } 1591 1592 // Find which module file this entry lands in. 1593 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second; 1594 if (!M->isModule()) 1595 return std::make_pair(SourceLocation(), ""); 1596 1597 // FIXME: Can we map this down to a particular submodule? That would be 1598 // ideal. 1599 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName)); 1600 } 1601 1602 /// Find the location where the module F is imported. 1603 SourceLocation ASTReader::getImportLocation(ModuleFile *F) { 1604 if (F->ImportLoc.isValid()) 1605 return F->ImportLoc; 1606 1607 // Otherwise we have a PCH. It's considered to be "imported" at the first 1608 // location of its includer. 1609 if (F->ImportedBy.empty() || !F->ImportedBy[0]) { 1610 // Main file is the importer. 1611 assert(SourceMgr.getMainFileID().isValid() && "missing main file"); 1612 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); 1613 } 1614 return F->ImportedBy[0]->FirstLoc; 1615 } 1616 1617 /// Enter a subblock of the specified BlockID with the specified cursor. Read 1618 /// the abbreviations that are at the top of the block and then leave the cursor 1619 /// pointing into the block. 1620 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID, 1621 uint64_t *StartOfBlockOffset) { 1622 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) { 1623 // FIXME this drops errors on the floor. 1624 consumeError(std::move(Err)); 1625 return true; 1626 } 1627 1628 if (StartOfBlockOffset) 1629 *StartOfBlockOffset = Cursor.GetCurrentBitNo(); 1630 1631 while (true) { 1632 uint64_t Offset = Cursor.GetCurrentBitNo(); 1633 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1634 if (!MaybeCode) { 1635 // FIXME this drops errors on the floor. 1636 consumeError(MaybeCode.takeError()); 1637 return true; 1638 } 1639 unsigned Code = MaybeCode.get(); 1640 1641 // We expect all abbrevs to be at the start of the block. 1642 if (Code != llvm::bitc::DEFINE_ABBREV) { 1643 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1644 // FIXME this drops errors on the floor. 1645 consumeError(std::move(Err)); 1646 return true; 1647 } 1648 return false; 1649 } 1650 if (llvm::Error Err = Cursor.ReadAbbrevRecord()) { 1651 // FIXME this drops errors on the floor. 1652 consumeError(std::move(Err)); 1653 return true; 1654 } 1655 } 1656 } 1657 1658 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record, 1659 unsigned &Idx) { 1660 Token Tok; 1661 Tok.startToken(); 1662 Tok.setLocation(ReadSourceLocation(F, Record, Idx)); 1663 Tok.setLength(Record[Idx++]); 1664 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++])) 1665 Tok.setIdentifierInfo(II); 1666 Tok.setKind((tok::TokenKind)Record[Idx++]); 1667 Tok.setFlag((Token::TokenFlags)Record[Idx++]); 1668 return Tok; 1669 } 1670 1671 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) { 1672 BitstreamCursor &Stream = F.MacroCursor; 1673 1674 // Keep track of where we are in the stream, then jump back there 1675 // after reading this macro. 1676 SavedStreamPosition SavedPosition(Stream); 1677 1678 if (llvm::Error Err = Stream.JumpToBit(Offset)) { 1679 // FIXME this drops errors on the floor. 1680 consumeError(std::move(Err)); 1681 return nullptr; 1682 } 1683 RecordData Record; 1684 SmallVector<IdentifierInfo*, 16> MacroParams; 1685 MacroInfo *Macro = nullptr; 1686 1687 while (true) { 1688 // Advance to the next record, but if we get to the end of the block, don't 1689 // pop it (removing all the abbreviations from the cursor) since we want to 1690 // be able to reseek within the block and read entries. 1691 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd; 1692 Expected<llvm::BitstreamEntry> MaybeEntry = 1693 Stream.advanceSkippingSubblocks(Flags); 1694 if (!MaybeEntry) { 1695 Error(MaybeEntry.takeError()); 1696 return Macro; 1697 } 1698 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1699 1700 switch (Entry.Kind) { 1701 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1702 case llvm::BitstreamEntry::Error: 1703 Error("malformed block record in AST file"); 1704 return Macro; 1705 case llvm::BitstreamEntry::EndBlock: 1706 return Macro; 1707 case llvm::BitstreamEntry::Record: 1708 // The interesting case. 1709 break; 1710 } 1711 1712 // Read a record. 1713 Record.clear(); 1714 PreprocessorRecordTypes RecType; 1715 if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record)) 1716 RecType = (PreprocessorRecordTypes)MaybeRecType.get(); 1717 else { 1718 Error(MaybeRecType.takeError()); 1719 return Macro; 1720 } 1721 switch (RecType) { 1722 case PP_MODULE_MACRO: 1723 case PP_MACRO_DIRECTIVE_HISTORY: 1724 return Macro; 1725 1726 case PP_MACRO_OBJECT_LIKE: 1727 case PP_MACRO_FUNCTION_LIKE: { 1728 // If we already have a macro, that means that we've hit the end 1729 // of the definition of the macro we were looking for. We're 1730 // done. 1731 if (Macro) 1732 return Macro; 1733 1734 unsigned NextIndex = 1; // Skip identifier ID. 1735 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex); 1736 MacroInfo *MI = PP.AllocateMacroInfo(Loc); 1737 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex)); 1738 MI->setIsUsed(Record[NextIndex++]); 1739 MI->setUsedForHeaderGuard(Record[NextIndex++]); 1740 1741 if (RecType == PP_MACRO_FUNCTION_LIKE) { 1742 // Decode function-like macro info. 1743 bool isC99VarArgs = Record[NextIndex++]; 1744 bool isGNUVarArgs = Record[NextIndex++]; 1745 bool hasCommaPasting = Record[NextIndex++]; 1746 MacroParams.clear(); 1747 unsigned NumArgs = Record[NextIndex++]; 1748 for (unsigned i = 0; i != NumArgs; ++i) 1749 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++])); 1750 1751 // Install function-like macro info. 1752 MI->setIsFunctionLike(); 1753 if (isC99VarArgs) MI->setIsC99Varargs(); 1754 if (isGNUVarArgs) MI->setIsGNUVarargs(); 1755 if (hasCommaPasting) MI->setHasCommaPasting(); 1756 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator()); 1757 } 1758 1759 // Remember that we saw this macro last so that we add the tokens that 1760 // form its body to it. 1761 Macro = MI; 1762 1763 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() && 1764 Record[NextIndex]) { 1765 // We have a macro definition. Register the association 1766 PreprocessedEntityID 1767 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]); 1768 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 1769 PreprocessingRecord::PPEntityID PPID = 1770 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true); 1771 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>( 1772 PPRec.getPreprocessedEntity(PPID)); 1773 if (PPDef) 1774 PPRec.RegisterMacroDefinition(Macro, PPDef); 1775 } 1776 1777 ++NumMacrosRead; 1778 break; 1779 } 1780 1781 case PP_TOKEN: { 1782 // If we see a TOKEN before a PP_MACRO_*, then the file is 1783 // erroneous, just pretend we didn't see this. 1784 if (!Macro) break; 1785 1786 unsigned Idx = 0; 1787 Token Tok = ReadToken(F, Record, Idx); 1788 Macro->AddTokenToBody(Tok); 1789 break; 1790 } 1791 } 1792 } 1793 } 1794 1795 PreprocessedEntityID 1796 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, 1797 unsigned LocalID) const { 1798 if (!M.ModuleOffsetMap.empty()) 1799 ReadModuleOffsetMap(M); 1800 1801 ContinuousRangeMap<uint32_t, int, 2>::const_iterator 1802 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS); 1803 assert(I != M.PreprocessedEntityRemap.end() 1804 && "Invalid index into preprocessed entity index remap"); 1805 1806 return LocalID + I->second; 1807 } 1808 1809 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) { 1810 return llvm::hash_combine(ikey.Size, ikey.ModTime); 1811 } 1812 1813 HeaderFileInfoTrait::internal_key_type 1814 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) { 1815 internal_key_type ikey = {FE->getSize(), 1816 M.HasTimestamps ? FE->getModificationTime() : 0, 1817 FE->getName(), /*Imported*/ false}; 1818 return ikey; 1819 } 1820 1821 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) { 1822 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime)) 1823 return false; 1824 1825 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename) 1826 return true; 1827 1828 // Determine whether the actual files are equivalent. 1829 FileManager &FileMgr = Reader.getFileManager(); 1830 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* { 1831 if (!Key.Imported) { 1832 if (auto File = FileMgr.getFile(Key.Filename)) 1833 return *File; 1834 return nullptr; 1835 } 1836 1837 std::string Resolved = std::string(Key.Filename); 1838 Reader.ResolveImportedPath(M, Resolved); 1839 if (auto File = FileMgr.getFile(Resolved)) 1840 return *File; 1841 return nullptr; 1842 }; 1843 1844 const FileEntry *FEA = GetFile(a); 1845 const FileEntry *FEB = GetFile(b); 1846 return FEA && FEA == FEB; 1847 } 1848 1849 std::pair<unsigned, unsigned> 1850 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) { 1851 using namespace llvm::support; 1852 1853 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d); 1854 unsigned DataLen = (unsigned) *d++; 1855 return std::make_pair(KeyLen, DataLen); 1856 } 1857 1858 HeaderFileInfoTrait::internal_key_type 1859 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) { 1860 using namespace llvm::support; 1861 1862 internal_key_type ikey; 1863 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d)); 1864 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d)); 1865 ikey.Filename = (const char *)d; 1866 ikey.Imported = true; 1867 return ikey; 1868 } 1869 1870 HeaderFileInfoTrait::data_type 1871 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d, 1872 unsigned DataLen) { 1873 using namespace llvm::support; 1874 1875 const unsigned char *End = d + DataLen; 1876 HeaderFileInfo HFI; 1877 unsigned Flags = *d++; 1878 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp. 1879 HFI.isImport |= (Flags >> 5) & 0x01; 1880 HFI.isPragmaOnce |= (Flags >> 4) & 0x01; 1881 HFI.DirInfo = (Flags >> 1) & 0x07; 1882 HFI.IndexHeaderMapHeader = Flags & 0x01; 1883 // FIXME: Find a better way to handle this. Maybe just store a 1884 // "has been included" flag? 1885 HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d), 1886 HFI.NumIncludes); 1887 HFI.ControllingMacroID = Reader.getGlobalIdentifierID( 1888 M, endian::readNext<uint32_t, little, unaligned>(d)); 1889 if (unsigned FrameworkOffset = 1890 endian::readNext<uint32_t, little, unaligned>(d)) { 1891 // The framework offset is 1 greater than the actual offset, 1892 // since 0 is used as an indicator for "no framework name". 1893 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1); 1894 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName); 1895 } 1896 1897 assert((End - d) % 4 == 0 && 1898 "Wrong data length in HeaderFileInfo deserialization"); 1899 while (d != End) { 1900 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d); 1901 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3); 1902 LocalSMID >>= 2; 1903 1904 // This header is part of a module. Associate it with the module to enable 1905 // implicit module import. 1906 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID); 1907 Module *Mod = Reader.getSubmodule(GlobalSMID); 1908 FileManager &FileMgr = Reader.getFileManager(); 1909 ModuleMap &ModMap = 1910 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap(); 1911 1912 std::string Filename = std::string(key.Filename); 1913 if (key.Imported) 1914 Reader.ResolveImportedPath(M, Filename); 1915 // FIXME: This is not always the right filename-as-written, but we're not 1916 // going to use this information to rebuild the module, so it doesn't make 1917 // a lot of difference. 1918 Module::Header H = {std::string(key.Filename), *FileMgr.getFile(Filename)}; 1919 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true); 1920 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader); 1921 } 1922 1923 // This HeaderFileInfo was externally loaded. 1924 HFI.External = true; 1925 HFI.IsValid = true; 1926 return HFI; 1927 } 1928 1929 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M, 1930 uint32_t MacroDirectivesOffset) { 1931 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard"); 1932 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset)); 1933 } 1934 1935 void ASTReader::ReadDefinedMacros() { 1936 // Note that we are loading defined macros. 1937 Deserializing Macros(this); 1938 1939 for (ModuleFile &I : llvm::reverse(ModuleMgr)) { 1940 BitstreamCursor &MacroCursor = I.MacroCursor; 1941 1942 // If there was no preprocessor block, skip this file. 1943 if (MacroCursor.getBitcodeBytes().empty()) 1944 continue; 1945 1946 BitstreamCursor Cursor = MacroCursor; 1947 if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) { 1948 Error(std::move(Err)); 1949 return; 1950 } 1951 1952 RecordData Record; 1953 while (true) { 1954 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks(); 1955 if (!MaybeE) { 1956 Error(MaybeE.takeError()); 1957 return; 1958 } 1959 llvm::BitstreamEntry E = MaybeE.get(); 1960 1961 switch (E.Kind) { 1962 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1963 case llvm::BitstreamEntry::Error: 1964 Error("malformed block record in AST file"); 1965 return; 1966 case llvm::BitstreamEntry::EndBlock: 1967 goto NextCursor; 1968 1969 case llvm::BitstreamEntry::Record: { 1970 Record.clear(); 1971 Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record); 1972 if (!MaybeRecord) { 1973 Error(MaybeRecord.takeError()); 1974 return; 1975 } 1976 switch (MaybeRecord.get()) { 1977 default: // Default behavior: ignore. 1978 break; 1979 1980 case PP_MACRO_OBJECT_LIKE: 1981 case PP_MACRO_FUNCTION_LIKE: { 1982 IdentifierInfo *II = getLocalIdentifier(I, Record[0]); 1983 if (II->isOutOfDate()) 1984 updateOutOfDateIdentifier(*II); 1985 break; 1986 } 1987 1988 case PP_TOKEN: 1989 // Ignore tokens. 1990 break; 1991 } 1992 break; 1993 } 1994 } 1995 } 1996 NextCursor: ; 1997 } 1998 } 1999 2000 namespace { 2001 2002 /// Visitor class used to look up identifirs in an AST file. 2003 class IdentifierLookupVisitor { 2004 StringRef Name; 2005 unsigned NameHash; 2006 unsigned PriorGeneration; 2007 unsigned &NumIdentifierLookups; 2008 unsigned &NumIdentifierLookupHits; 2009 IdentifierInfo *Found = nullptr; 2010 2011 public: 2012 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration, 2013 unsigned &NumIdentifierLookups, 2014 unsigned &NumIdentifierLookupHits) 2015 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)), 2016 PriorGeneration(PriorGeneration), 2017 NumIdentifierLookups(NumIdentifierLookups), 2018 NumIdentifierLookupHits(NumIdentifierLookupHits) {} 2019 2020 bool operator()(ModuleFile &M) { 2021 // If we've already searched this module file, skip it now. 2022 if (M.Generation <= PriorGeneration) 2023 return true; 2024 2025 ASTIdentifierLookupTable *IdTable 2026 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable; 2027 if (!IdTable) 2028 return false; 2029 2030 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M, 2031 Found); 2032 ++NumIdentifierLookups; 2033 ASTIdentifierLookupTable::iterator Pos = 2034 IdTable->find_hashed(Name, NameHash, &Trait); 2035 if (Pos == IdTable->end()) 2036 return false; 2037 2038 // Dereferencing the iterator has the effect of building the 2039 // IdentifierInfo node and populating it with the various 2040 // declarations it needs. 2041 ++NumIdentifierLookupHits; 2042 Found = *Pos; 2043 return true; 2044 } 2045 2046 // Retrieve the identifier info found within the module 2047 // files. 2048 IdentifierInfo *getIdentifierInfo() const { return Found; } 2049 }; 2050 2051 } // namespace 2052 2053 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) { 2054 // Note that we are loading an identifier. 2055 Deserializing AnIdentifier(this); 2056 2057 unsigned PriorGeneration = 0; 2058 if (getContext().getLangOpts().Modules) 2059 PriorGeneration = IdentifierGeneration[&II]; 2060 2061 // If there is a global index, look there first to determine which modules 2062 // provably do not have any results for this identifier. 2063 GlobalModuleIndex::HitSet Hits; 2064 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 2065 if (!loadGlobalIndex()) { 2066 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) { 2067 HitsPtr = &Hits; 2068 } 2069 } 2070 2071 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration, 2072 NumIdentifierLookups, 2073 NumIdentifierLookupHits); 2074 ModuleMgr.visit(Visitor, HitsPtr); 2075 markIdentifierUpToDate(&II); 2076 } 2077 2078 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) { 2079 if (!II) 2080 return; 2081 2082 II->setOutOfDate(false); 2083 2084 // Update the generation for this identifier. 2085 if (getContext().getLangOpts().Modules) 2086 IdentifierGeneration[II] = getGeneration(); 2087 } 2088 2089 void ASTReader::resolvePendingMacro(IdentifierInfo *II, 2090 const PendingMacroInfo &PMInfo) { 2091 ModuleFile &M = *PMInfo.M; 2092 2093 BitstreamCursor &Cursor = M.MacroCursor; 2094 SavedStreamPosition SavedPosition(Cursor); 2095 if (llvm::Error Err = 2096 Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) { 2097 Error(std::move(Err)); 2098 return; 2099 } 2100 2101 struct ModuleMacroRecord { 2102 SubmoduleID SubModID; 2103 MacroInfo *MI; 2104 SmallVector<SubmoduleID, 8> Overrides; 2105 }; 2106 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros; 2107 2108 // We expect to see a sequence of PP_MODULE_MACRO records listing exported 2109 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete 2110 // macro histroy. 2111 RecordData Record; 2112 while (true) { 2113 Expected<llvm::BitstreamEntry> MaybeEntry = 2114 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 2115 if (!MaybeEntry) { 2116 Error(MaybeEntry.takeError()); 2117 return; 2118 } 2119 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2120 2121 if (Entry.Kind != llvm::BitstreamEntry::Record) { 2122 Error("malformed block record in AST file"); 2123 return; 2124 } 2125 2126 Record.clear(); 2127 Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record); 2128 if (!MaybePP) { 2129 Error(MaybePP.takeError()); 2130 return; 2131 } 2132 switch ((PreprocessorRecordTypes)MaybePP.get()) { 2133 case PP_MACRO_DIRECTIVE_HISTORY: 2134 break; 2135 2136 case PP_MODULE_MACRO: { 2137 ModuleMacros.push_back(ModuleMacroRecord()); 2138 auto &Info = ModuleMacros.back(); 2139 Info.SubModID = getGlobalSubmoduleID(M, Record[0]); 2140 Info.MI = getMacro(getGlobalMacroID(M, Record[1])); 2141 for (int I = 2, N = Record.size(); I != N; ++I) 2142 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I])); 2143 continue; 2144 } 2145 2146 default: 2147 Error("malformed block record in AST file"); 2148 return; 2149 } 2150 2151 // We found the macro directive history; that's the last record 2152 // for this macro. 2153 break; 2154 } 2155 2156 // Module macros are listed in reverse dependency order. 2157 { 2158 std::reverse(ModuleMacros.begin(), ModuleMacros.end()); 2159 llvm::SmallVector<ModuleMacro*, 8> Overrides; 2160 for (auto &MMR : ModuleMacros) { 2161 Overrides.clear(); 2162 for (unsigned ModID : MMR.Overrides) { 2163 Module *Mod = getSubmodule(ModID); 2164 auto *Macro = PP.getModuleMacro(Mod, II); 2165 assert(Macro && "missing definition for overridden macro"); 2166 Overrides.push_back(Macro); 2167 } 2168 2169 bool Inserted = false; 2170 Module *Owner = getSubmodule(MMR.SubModID); 2171 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted); 2172 } 2173 } 2174 2175 // Don't read the directive history for a module; we don't have anywhere 2176 // to put it. 2177 if (M.isModule()) 2178 return; 2179 2180 // Deserialize the macro directives history in reverse source-order. 2181 MacroDirective *Latest = nullptr, *Earliest = nullptr; 2182 unsigned Idx = 0, N = Record.size(); 2183 while (Idx < N) { 2184 MacroDirective *MD = nullptr; 2185 SourceLocation Loc = ReadSourceLocation(M, Record, Idx); 2186 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++]; 2187 switch (K) { 2188 case MacroDirective::MD_Define: { 2189 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++])); 2190 MD = PP.AllocateDefMacroDirective(MI, Loc); 2191 break; 2192 } 2193 case MacroDirective::MD_Undefine: 2194 MD = PP.AllocateUndefMacroDirective(Loc); 2195 break; 2196 case MacroDirective::MD_Visibility: 2197 bool isPublic = Record[Idx++]; 2198 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic); 2199 break; 2200 } 2201 2202 if (!Latest) 2203 Latest = MD; 2204 if (Earliest) 2205 Earliest->setPrevious(MD); 2206 Earliest = MD; 2207 } 2208 2209 if (Latest) 2210 PP.setLoadedMacroDirective(II, Earliest, Latest); 2211 } 2212 2213 ASTReader::InputFileInfo 2214 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) { 2215 // Go find this input file. 2216 BitstreamCursor &Cursor = F.InputFilesCursor; 2217 SavedStreamPosition SavedPosition(Cursor); 2218 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2219 // FIXME this drops errors on the floor. 2220 consumeError(std::move(Err)); 2221 } 2222 2223 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 2224 if (!MaybeCode) { 2225 // FIXME this drops errors on the floor. 2226 consumeError(MaybeCode.takeError()); 2227 } 2228 unsigned Code = MaybeCode.get(); 2229 RecordData Record; 2230 StringRef Blob; 2231 2232 if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob)) 2233 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE && 2234 "invalid record type for input file"); 2235 else { 2236 // FIXME this drops errors on the floor. 2237 consumeError(Maybe.takeError()); 2238 } 2239 2240 assert(Record[0] == ID && "Bogus stored ID or offset"); 2241 InputFileInfo R; 2242 R.StoredSize = static_cast<off_t>(Record[1]); 2243 R.StoredTime = static_cast<time_t>(Record[2]); 2244 R.Overridden = static_cast<bool>(Record[3]); 2245 R.Transient = static_cast<bool>(Record[4]); 2246 R.TopLevelModuleMap = static_cast<bool>(Record[5]); 2247 R.Filename = std::string(Blob); 2248 ResolveImportedPath(F, R.Filename); 2249 2250 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 2251 if (!MaybeEntry) // FIXME this drops errors on the floor. 2252 consumeError(MaybeEntry.takeError()); 2253 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2254 assert(Entry.Kind == llvm::BitstreamEntry::Record && 2255 "expected record type for input file hash"); 2256 2257 Record.clear(); 2258 if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record)) 2259 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH && 2260 "invalid record type for input file hash"); 2261 else { 2262 // FIXME this drops errors on the floor. 2263 consumeError(Maybe.takeError()); 2264 } 2265 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) | 2266 static_cast<uint64_t>(Record[0]); 2267 return R; 2268 } 2269 2270 static unsigned moduleKindForDiagnostic(ModuleKind Kind); 2271 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) { 2272 // If this ID is bogus, just return an empty input file. 2273 if (ID == 0 || ID > F.InputFilesLoaded.size()) 2274 return InputFile(); 2275 2276 // If we've already loaded this input file, return it. 2277 if (F.InputFilesLoaded[ID-1].getFile()) 2278 return F.InputFilesLoaded[ID-1]; 2279 2280 if (F.InputFilesLoaded[ID-1].isNotFound()) 2281 return InputFile(); 2282 2283 // Go find this input file. 2284 BitstreamCursor &Cursor = F.InputFilesCursor; 2285 SavedStreamPosition SavedPosition(Cursor); 2286 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2287 // FIXME this drops errors on the floor. 2288 consumeError(std::move(Err)); 2289 } 2290 2291 InputFileInfo FI = readInputFileInfo(F, ID); 2292 off_t StoredSize = FI.StoredSize; 2293 time_t StoredTime = FI.StoredTime; 2294 bool Overridden = FI.Overridden; 2295 bool Transient = FI.Transient; 2296 StringRef Filename = FI.Filename; 2297 uint64_t StoredContentHash = FI.ContentHash; 2298 2299 OptionalFileEntryRefDegradesToFileEntryPtr File = 2300 expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false)); 2301 2302 // If we didn't find the file, resolve it relative to the 2303 // original directory from which this AST file was created. 2304 if (!File && !F.OriginalDir.empty() && !F.BaseDirectory.empty() && 2305 F.OriginalDir != F.BaseDirectory) { 2306 std::string Resolved = resolveFileRelativeToOriginalDir( 2307 std::string(Filename), F.OriginalDir, F.BaseDirectory); 2308 if (!Resolved.empty()) 2309 File = expectedToOptional(FileMgr.getFileRef(Resolved)); 2310 } 2311 2312 // For an overridden file, create a virtual file with the stored 2313 // size/timestamp. 2314 if ((Overridden || Transient) && !File) 2315 File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime); 2316 2317 if (!File) { 2318 if (Complain) { 2319 std::string ErrorStr = "could not find file '"; 2320 ErrorStr += Filename; 2321 ErrorStr += "' referenced by AST file '"; 2322 ErrorStr += F.FileName; 2323 ErrorStr += "'"; 2324 Error(ErrorStr); 2325 } 2326 // Record that we didn't find the file. 2327 F.InputFilesLoaded[ID-1] = InputFile::getNotFound(); 2328 return InputFile(); 2329 } 2330 2331 // Check if there was a request to override the contents of the file 2332 // that was part of the precompiled header. Overriding such a file 2333 // can lead to problems when lexing using the source locations from the 2334 // PCH. 2335 SourceManager &SM = getSourceManager(); 2336 // FIXME: Reject if the overrides are different. 2337 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) { 2338 if (Complain) 2339 Error(diag::err_fe_pch_file_overridden, Filename); 2340 2341 // After emitting the diagnostic, bypass the overriding file to recover 2342 // (this creates a separate FileEntry). 2343 File = SM.bypassFileContentsOverride(*File); 2344 if (!File) { 2345 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound(); 2346 return InputFile(); 2347 } 2348 } 2349 2350 enum ModificationType { 2351 Size, 2352 ModTime, 2353 Content, 2354 None, 2355 }; 2356 auto HasInputFileChanged = [&]() { 2357 if (StoredSize != File->getSize()) 2358 return ModificationType::Size; 2359 if (!DisableValidation && StoredTime && 2360 StoredTime != File->getModificationTime()) { 2361 // In case the modification time changes but not the content, 2362 // accept the cached file as legit. 2363 if (ValidateASTInputFilesContent && 2364 StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) { 2365 auto MemBuffOrError = FileMgr.getBufferForFile(File); 2366 if (!MemBuffOrError) { 2367 if (!Complain) 2368 return ModificationType::ModTime; 2369 std::string ErrorStr = "could not get buffer for file '"; 2370 ErrorStr += File->getName(); 2371 ErrorStr += "'"; 2372 Error(ErrorStr); 2373 return ModificationType::ModTime; 2374 } 2375 2376 auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer()); 2377 if (StoredContentHash == static_cast<uint64_t>(ContentHash)) 2378 return ModificationType::None; 2379 return ModificationType::Content; 2380 } 2381 return ModificationType::ModTime; 2382 } 2383 return ModificationType::None; 2384 }; 2385 2386 bool IsOutOfDate = false; 2387 auto FileChange = HasInputFileChanged(); 2388 // For an overridden file, there is nothing to validate. 2389 if (!Overridden && FileChange != ModificationType::None) { 2390 if (Complain && !Diags.isDiagnosticInFlight()) { 2391 // Build a list of the PCH imports that got us here (in reverse). 2392 SmallVector<ModuleFile *, 4> ImportStack(1, &F); 2393 while (!ImportStack.back()->ImportedBy.empty()) 2394 ImportStack.push_back(ImportStack.back()->ImportedBy[0]); 2395 2396 // The top-level PCH is stale. 2397 StringRef TopLevelPCHName(ImportStack.back()->FileName); 2398 Diag(diag::err_fe_ast_file_modified) 2399 << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind) 2400 << TopLevelPCHName << FileChange; 2401 2402 // Print the import stack. 2403 if (ImportStack.size() > 1) { 2404 Diag(diag::note_pch_required_by) 2405 << Filename << ImportStack[0]->FileName; 2406 for (unsigned I = 1; I < ImportStack.size(); ++I) 2407 Diag(diag::note_pch_required_by) 2408 << ImportStack[I-1]->FileName << ImportStack[I]->FileName; 2409 } 2410 2411 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName; 2412 } 2413 2414 IsOutOfDate = true; 2415 } 2416 // FIXME: If the file is overridden and we've already opened it, 2417 // issue an error (or split it into a separate FileEntry). 2418 2419 InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate); 2420 2421 // Note that we've loaded this input file. 2422 F.InputFilesLoaded[ID-1] = IF; 2423 return IF; 2424 } 2425 2426 /// If we are loading a relocatable PCH or module file, and the filename 2427 /// is not an absolute path, add the system or module root to the beginning of 2428 /// the file name. 2429 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) { 2430 // Resolve relative to the base directory, if we have one. 2431 if (!M.BaseDirectory.empty()) 2432 return ResolveImportedPath(Filename, M.BaseDirectory); 2433 } 2434 2435 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) { 2436 if (Filename.empty() || llvm::sys::path::is_absolute(Filename)) 2437 return; 2438 2439 SmallString<128> Buffer; 2440 llvm::sys::path::append(Buffer, Prefix, Filename); 2441 Filename.assign(Buffer.begin(), Buffer.end()); 2442 } 2443 2444 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) { 2445 switch (ARR) { 2446 case ASTReader::Failure: return true; 2447 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing); 2448 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate); 2449 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch); 2450 case ASTReader::ConfigurationMismatch: 2451 return !(Caps & ASTReader::ARR_ConfigurationMismatch); 2452 case ASTReader::HadErrors: return true; 2453 case ASTReader::Success: return false; 2454 } 2455 2456 llvm_unreachable("unknown ASTReadResult"); 2457 } 2458 2459 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock( 2460 BitstreamCursor &Stream, unsigned ClientLoadCapabilities, 2461 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener, 2462 std::string &SuggestedPredefines) { 2463 if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) { 2464 // FIXME this drops errors on the floor. 2465 consumeError(std::move(Err)); 2466 return Failure; 2467 } 2468 2469 // Read all of the records in the options block. 2470 RecordData Record; 2471 ASTReadResult Result = Success; 2472 while (true) { 2473 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2474 if (!MaybeEntry) { 2475 // FIXME this drops errors on the floor. 2476 consumeError(MaybeEntry.takeError()); 2477 return Failure; 2478 } 2479 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2480 2481 switch (Entry.Kind) { 2482 case llvm::BitstreamEntry::Error: 2483 case llvm::BitstreamEntry::SubBlock: 2484 return Failure; 2485 2486 case llvm::BitstreamEntry::EndBlock: 2487 return Result; 2488 2489 case llvm::BitstreamEntry::Record: 2490 // The interesting case. 2491 break; 2492 } 2493 2494 // Read and process a record. 2495 Record.clear(); 2496 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record); 2497 if (!MaybeRecordType) { 2498 // FIXME this drops errors on the floor. 2499 consumeError(MaybeRecordType.takeError()); 2500 return Failure; 2501 } 2502 switch ((OptionsRecordTypes)MaybeRecordType.get()) { 2503 case LANGUAGE_OPTIONS: { 2504 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2505 if (ParseLanguageOptions(Record, Complain, Listener, 2506 AllowCompatibleConfigurationMismatch)) 2507 Result = ConfigurationMismatch; 2508 break; 2509 } 2510 2511 case TARGET_OPTIONS: { 2512 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2513 if (ParseTargetOptions(Record, Complain, Listener, 2514 AllowCompatibleConfigurationMismatch)) 2515 Result = ConfigurationMismatch; 2516 break; 2517 } 2518 2519 case FILE_SYSTEM_OPTIONS: { 2520 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2521 if (!AllowCompatibleConfigurationMismatch && 2522 ParseFileSystemOptions(Record, Complain, Listener)) 2523 Result = ConfigurationMismatch; 2524 break; 2525 } 2526 2527 case HEADER_SEARCH_OPTIONS: { 2528 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2529 if (!AllowCompatibleConfigurationMismatch && 2530 ParseHeaderSearchOptions(Record, Complain, Listener)) 2531 Result = ConfigurationMismatch; 2532 break; 2533 } 2534 2535 case PREPROCESSOR_OPTIONS: 2536 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2537 if (!AllowCompatibleConfigurationMismatch && 2538 ParsePreprocessorOptions(Record, Complain, Listener, 2539 SuggestedPredefines)) 2540 Result = ConfigurationMismatch; 2541 break; 2542 } 2543 } 2544 } 2545 2546 ASTReader::ASTReadResult 2547 ASTReader::ReadControlBlock(ModuleFile &F, 2548 SmallVectorImpl<ImportedModule> &Loaded, 2549 const ModuleFile *ImportedBy, 2550 unsigned ClientLoadCapabilities) { 2551 BitstreamCursor &Stream = F.Stream; 2552 2553 if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) { 2554 Error(std::move(Err)); 2555 return Failure; 2556 } 2557 2558 // Lambda to read the unhashed control block the first time it's called. 2559 // 2560 // For PCM files, the unhashed control block cannot be read until after the 2561 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still 2562 // need to look ahead before reading the IMPORTS record. For consistency, 2563 // this block is always read somehow (see BitstreamEntry::EndBlock). 2564 bool HasReadUnhashedControlBlock = false; 2565 auto readUnhashedControlBlockOnce = [&]() { 2566 if (!HasReadUnhashedControlBlock) { 2567 HasReadUnhashedControlBlock = true; 2568 if (ASTReadResult Result = 2569 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities)) 2570 return Result; 2571 } 2572 return Success; 2573 }; 2574 2575 // Read all of the records and blocks in the control block. 2576 RecordData Record; 2577 unsigned NumInputs = 0; 2578 unsigned NumUserInputs = 0; 2579 StringRef BaseDirectoryAsWritten; 2580 while (true) { 2581 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2582 if (!MaybeEntry) { 2583 Error(MaybeEntry.takeError()); 2584 return Failure; 2585 } 2586 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2587 2588 switch (Entry.Kind) { 2589 case llvm::BitstreamEntry::Error: 2590 Error("malformed block record in AST file"); 2591 return Failure; 2592 case llvm::BitstreamEntry::EndBlock: { 2593 // Validate the module before returning. This call catches an AST with 2594 // no module name and no imports. 2595 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2596 return Result; 2597 2598 // Validate input files. 2599 const HeaderSearchOptions &HSOpts = 2600 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 2601 2602 // All user input files reside at the index range [0, NumUserInputs), and 2603 // system input files reside at [NumUserInputs, NumInputs). For explicitly 2604 // loaded module files, ignore missing inputs. 2605 if (!DisableValidation && F.Kind != MK_ExplicitModule && 2606 F.Kind != MK_PrebuiltModule) { 2607 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 2608 2609 // If we are reading a module, we will create a verification timestamp, 2610 // so we verify all input files. Otherwise, verify only user input 2611 // files. 2612 2613 unsigned N = NumUserInputs; 2614 if (ValidateSystemInputs || 2615 (HSOpts.ModulesValidateOncePerBuildSession && 2616 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp && 2617 F.Kind == MK_ImplicitModule)) 2618 N = NumInputs; 2619 2620 for (unsigned I = 0; I < N; ++I) { 2621 InputFile IF = getInputFile(F, I+1, Complain); 2622 if (!IF.getFile() || IF.isOutOfDate()) 2623 return OutOfDate; 2624 } 2625 } 2626 2627 if (Listener) 2628 Listener->visitModuleFile(F.FileName, F.Kind); 2629 2630 if (Listener && Listener->needsInputFileVisitation()) { 2631 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs 2632 : NumUserInputs; 2633 for (unsigned I = 0; I < N; ++I) { 2634 bool IsSystem = I >= NumUserInputs; 2635 InputFileInfo FI = readInputFileInfo(F, I+1); 2636 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden, 2637 F.Kind == MK_ExplicitModule || 2638 F.Kind == MK_PrebuiltModule); 2639 } 2640 } 2641 2642 return Success; 2643 } 2644 2645 case llvm::BitstreamEntry::SubBlock: 2646 switch (Entry.ID) { 2647 case INPUT_FILES_BLOCK_ID: 2648 F.InputFilesCursor = Stream; 2649 if (llvm::Error Err = Stream.SkipBlock()) { 2650 Error(std::move(Err)); 2651 return Failure; 2652 } 2653 if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) { 2654 Error("malformed block record in AST file"); 2655 return Failure; 2656 } 2657 continue; 2658 2659 case OPTIONS_BLOCK_ID: 2660 // If we're reading the first module for this group, check its options 2661 // are compatible with ours. For modules it imports, no further checking 2662 // is required, because we checked them when we built it. 2663 if (Listener && !ImportedBy) { 2664 // Should we allow the configuration of the module file to differ from 2665 // the configuration of the current translation unit in a compatible 2666 // way? 2667 // 2668 // FIXME: Allow this for files explicitly specified with -include-pch. 2669 bool AllowCompatibleConfigurationMismatch = 2670 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 2671 2672 ASTReadResult Result = 2673 ReadOptionsBlock(Stream, ClientLoadCapabilities, 2674 AllowCompatibleConfigurationMismatch, *Listener, 2675 SuggestedPredefines); 2676 if (Result == Failure) { 2677 Error("malformed block record in AST file"); 2678 return Result; 2679 } 2680 2681 if (DisableValidation || 2682 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 2683 Result = Success; 2684 2685 // If we can't load the module, exit early since we likely 2686 // will rebuild the module anyway. The stream may be in the 2687 // middle of a block. 2688 if (Result != Success) 2689 return Result; 2690 } else if (llvm::Error Err = Stream.SkipBlock()) { 2691 Error(std::move(Err)); 2692 return Failure; 2693 } 2694 continue; 2695 2696 default: 2697 if (llvm::Error Err = Stream.SkipBlock()) { 2698 Error(std::move(Err)); 2699 return Failure; 2700 } 2701 continue; 2702 } 2703 2704 case llvm::BitstreamEntry::Record: 2705 // The interesting case. 2706 break; 2707 } 2708 2709 // Read and process a record. 2710 Record.clear(); 2711 StringRef Blob; 2712 Expected<unsigned> MaybeRecordType = 2713 Stream.readRecord(Entry.ID, Record, &Blob); 2714 if (!MaybeRecordType) { 2715 Error(MaybeRecordType.takeError()); 2716 return Failure; 2717 } 2718 switch ((ControlRecordTypes)MaybeRecordType.get()) { 2719 case METADATA: { 2720 if (Record[0] != VERSION_MAJOR && !DisableValidation) { 2721 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2722 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old 2723 : diag::err_pch_version_too_new); 2724 return VersionMismatch; 2725 } 2726 2727 bool hasErrors = Record[6]; 2728 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) { 2729 Diag(diag::err_pch_with_compiler_errors); 2730 return HadErrors; 2731 } 2732 if (hasErrors) { 2733 Diags.ErrorOccurred = true; 2734 Diags.UncompilableErrorOccurred = true; 2735 Diags.UnrecoverableErrorOccurred = true; 2736 } 2737 2738 F.RelocatablePCH = Record[4]; 2739 // Relative paths in a relocatable PCH are relative to our sysroot. 2740 if (F.RelocatablePCH) 2741 F.BaseDirectory = isysroot.empty() ? "/" : isysroot; 2742 2743 F.HasTimestamps = Record[5]; 2744 2745 const std::string &CurBranch = getClangFullRepositoryVersion(); 2746 StringRef ASTBranch = Blob; 2747 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) { 2748 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2749 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch; 2750 return VersionMismatch; 2751 } 2752 break; 2753 } 2754 2755 case IMPORTS: { 2756 // Validate the AST before processing any imports (otherwise, untangling 2757 // them can be error-prone and expensive). A module will have a name and 2758 // will already have been validated, but this catches the PCH case. 2759 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2760 return Result; 2761 2762 // Load each of the imported PCH files. 2763 unsigned Idx = 0, N = Record.size(); 2764 while (Idx < N) { 2765 // Read information about the AST file. 2766 ModuleKind ImportedKind = (ModuleKind)Record[Idx++]; 2767 // The import location will be the local one for now; we will adjust 2768 // all import locations of module imports after the global source 2769 // location info are setup, in ReadAST. 2770 SourceLocation ImportLoc = 2771 ReadUntranslatedSourceLocation(Record[Idx++]); 2772 off_t StoredSize = (off_t)Record[Idx++]; 2773 time_t StoredModTime = (time_t)Record[Idx++]; 2774 auto FirstSignatureByte = Record.begin() + Idx; 2775 ASTFileSignature StoredSignature = ASTFileSignature::create( 2776 FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size); 2777 Idx += ASTFileSignature::size; 2778 2779 std::string ImportedName = ReadString(Record, Idx); 2780 std::string ImportedFile; 2781 2782 // For prebuilt and explicit modules first consult the file map for 2783 // an override. Note that here we don't search prebuilt module 2784 // directories, only the explicit name to file mappings. Also, we will 2785 // still verify the size/signature making sure it is essentially the 2786 // same file but perhaps in a different location. 2787 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule) 2788 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName( 2789 ImportedName, /*FileMapOnly*/ true); 2790 2791 if (ImportedFile.empty()) 2792 // Use BaseDirectoryAsWritten to ensure we use the same path in the 2793 // ModuleCache as when writing. 2794 ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx); 2795 else 2796 SkipPath(Record, Idx); 2797 2798 // If our client can't cope with us being out of date, we can't cope with 2799 // our dependency being missing. 2800 unsigned Capabilities = ClientLoadCapabilities; 2801 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 2802 Capabilities &= ~ARR_Missing; 2803 2804 // Load the AST file. 2805 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, 2806 Loaded, StoredSize, StoredModTime, 2807 StoredSignature, Capabilities); 2808 2809 // If we diagnosed a problem, produce a backtrace. 2810 if (isDiagnosedResult(Result, Capabilities)) 2811 Diag(diag::note_module_file_imported_by) 2812 << F.FileName << !F.ModuleName.empty() << F.ModuleName; 2813 2814 switch (Result) { 2815 case Failure: return Failure; 2816 // If we have to ignore the dependency, we'll have to ignore this too. 2817 case Missing: 2818 case OutOfDate: return OutOfDate; 2819 case VersionMismatch: return VersionMismatch; 2820 case ConfigurationMismatch: return ConfigurationMismatch; 2821 case HadErrors: return HadErrors; 2822 case Success: break; 2823 } 2824 } 2825 break; 2826 } 2827 2828 case ORIGINAL_FILE: 2829 F.OriginalSourceFileID = FileID::get(Record[0]); 2830 F.ActualOriginalSourceFileName = std::string(Blob); 2831 F.OriginalSourceFileName = F.ActualOriginalSourceFileName; 2832 ResolveImportedPath(F, F.OriginalSourceFileName); 2833 break; 2834 2835 case ORIGINAL_FILE_ID: 2836 F.OriginalSourceFileID = FileID::get(Record[0]); 2837 break; 2838 2839 case ORIGINAL_PCH_DIR: 2840 F.OriginalDir = std::string(Blob); 2841 break; 2842 2843 case MODULE_NAME: 2844 F.ModuleName = std::string(Blob); 2845 Diag(diag::remark_module_import) 2846 << F.ModuleName << F.FileName << (ImportedBy ? true : false) 2847 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 2848 if (Listener) 2849 Listener->ReadModuleName(F.ModuleName); 2850 2851 // Validate the AST as soon as we have a name so we can exit early on 2852 // failure. 2853 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2854 return Result; 2855 2856 break; 2857 2858 case MODULE_DIRECTORY: { 2859 // Save the BaseDirectory as written in the PCM for computing the module 2860 // filename for the ModuleCache. 2861 BaseDirectoryAsWritten = Blob; 2862 assert(!F.ModuleName.empty() && 2863 "MODULE_DIRECTORY found before MODULE_NAME"); 2864 // If we've already loaded a module map file covering this module, we may 2865 // have a better path for it (relative to the current build). 2866 Module *M = PP.getHeaderSearchInfo().lookupModule( 2867 F.ModuleName, /*AllowSearch*/ true, 2868 /*AllowExtraModuleMapSearch*/ true); 2869 if (M && M->Directory) { 2870 // If we're implicitly loading a module, the base directory can't 2871 // change between the build and use. 2872 // Don't emit module relocation error if we have -fno-validate-pch 2873 if (!PP.getPreprocessorOpts().DisablePCHValidation && 2874 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) { 2875 auto BuildDir = PP.getFileManager().getDirectory(Blob); 2876 if (!BuildDir || *BuildDir != M->Directory) { 2877 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 2878 Diag(diag::err_imported_module_relocated) 2879 << F.ModuleName << Blob << M->Directory->getName(); 2880 return OutOfDate; 2881 } 2882 } 2883 F.BaseDirectory = std::string(M->Directory->getName()); 2884 } else { 2885 F.BaseDirectory = std::string(Blob); 2886 } 2887 break; 2888 } 2889 2890 case MODULE_MAP_FILE: 2891 if (ASTReadResult Result = 2892 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities)) 2893 return Result; 2894 break; 2895 2896 case INPUT_FILE_OFFSETS: 2897 NumInputs = Record[0]; 2898 NumUserInputs = Record[1]; 2899 F.InputFileOffsets = 2900 (const llvm::support::unaligned_uint64_t *)Blob.data(); 2901 F.InputFilesLoaded.resize(NumInputs); 2902 F.NumUserInputFiles = NumUserInputs; 2903 break; 2904 } 2905 } 2906 } 2907 2908 ASTReader::ASTReadResult 2909 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) { 2910 BitstreamCursor &Stream = F.Stream; 2911 2912 if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) { 2913 Error(std::move(Err)); 2914 return Failure; 2915 } 2916 F.ASTBlockStartOffset = Stream.GetCurrentBitNo(); 2917 2918 // Read all of the records and blocks for the AST file. 2919 RecordData Record; 2920 while (true) { 2921 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2922 if (!MaybeEntry) { 2923 Error(MaybeEntry.takeError()); 2924 return Failure; 2925 } 2926 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2927 2928 switch (Entry.Kind) { 2929 case llvm::BitstreamEntry::Error: 2930 Error("error at end of module block in AST file"); 2931 return Failure; 2932 case llvm::BitstreamEntry::EndBlock: 2933 // Outside of C++, we do not store a lookup map for the translation unit. 2934 // Instead, mark it as needing a lookup map to be built if this module 2935 // contains any declarations lexically within it (which it always does!). 2936 // This usually has no cost, since we very rarely need the lookup map for 2937 // the translation unit outside C++. 2938 if (ASTContext *Ctx = ContextObj) { 2939 DeclContext *DC = Ctx->getTranslationUnitDecl(); 2940 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus) 2941 DC->setMustBuildLookupTable(); 2942 } 2943 2944 return Success; 2945 case llvm::BitstreamEntry::SubBlock: 2946 switch (Entry.ID) { 2947 case DECLTYPES_BLOCK_ID: 2948 // We lazily load the decls block, but we want to set up the 2949 // DeclsCursor cursor to point into it. Clone our current bitcode 2950 // cursor to it, enter the block and read the abbrevs in that block. 2951 // With the main cursor, we just skip over it. 2952 F.DeclsCursor = Stream; 2953 if (llvm::Error Err = Stream.SkipBlock()) { 2954 Error(std::move(Err)); 2955 return Failure; 2956 } 2957 if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID, 2958 &F.DeclsBlockStartOffset)) { 2959 Error("malformed block record in AST file"); 2960 return Failure; 2961 } 2962 break; 2963 2964 case PREPROCESSOR_BLOCK_ID: 2965 F.MacroCursor = Stream; 2966 if (!PP.getExternalSource()) 2967 PP.setExternalSource(this); 2968 2969 if (llvm::Error Err = Stream.SkipBlock()) { 2970 Error(std::move(Err)); 2971 return Failure; 2972 } 2973 if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) { 2974 Error("malformed block record in AST file"); 2975 return Failure; 2976 } 2977 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo(); 2978 break; 2979 2980 case PREPROCESSOR_DETAIL_BLOCK_ID: 2981 F.PreprocessorDetailCursor = Stream; 2982 2983 if (llvm::Error Err = Stream.SkipBlock()) { 2984 Error(std::move(Err)); 2985 return Failure; 2986 } 2987 if (ReadBlockAbbrevs(F.PreprocessorDetailCursor, 2988 PREPROCESSOR_DETAIL_BLOCK_ID)) { 2989 Error("malformed preprocessor detail record in AST file"); 2990 return Failure; 2991 } 2992 F.PreprocessorDetailStartOffset 2993 = F.PreprocessorDetailCursor.GetCurrentBitNo(); 2994 2995 if (!PP.getPreprocessingRecord()) 2996 PP.createPreprocessingRecord(); 2997 if (!PP.getPreprocessingRecord()->getExternalSource()) 2998 PP.getPreprocessingRecord()->SetExternalSource(*this); 2999 break; 3000 3001 case SOURCE_MANAGER_BLOCK_ID: 3002 if (ReadSourceManagerBlock(F)) 3003 return Failure; 3004 break; 3005 3006 case SUBMODULE_BLOCK_ID: 3007 if (ASTReadResult Result = 3008 ReadSubmoduleBlock(F, ClientLoadCapabilities)) 3009 return Result; 3010 break; 3011 3012 case COMMENTS_BLOCK_ID: { 3013 BitstreamCursor C = Stream; 3014 3015 if (llvm::Error Err = Stream.SkipBlock()) { 3016 Error(std::move(Err)); 3017 return Failure; 3018 } 3019 if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) { 3020 Error("malformed comments block in AST file"); 3021 return Failure; 3022 } 3023 CommentsCursors.push_back(std::make_pair(C, &F)); 3024 break; 3025 } 3026 3027 default: 3028 if (llvm::Error Err = Stream.SkipBlock()) { 3029 Error(std::move(Err)); 3030 return Failure; 3031 } 3032 break; 3033 } 3034 continue; 3035 3036 case llvm::BitstreamEntry::Record: 3037 // The interesting case. 3038 break; 3039 } 3040 3041 // Read and process a record. 3042 Record.clear(); 3043 StringRef Blob; 3044 Expected<unsigned> MaybeRecordType = 3045 Stream.readRecord(Entry.ID, Record, &Blob); 3046 if (!MaybeRecordType) { 3047 Error(MaybeRecordType.takeError()); 3048 return Failure; 3049 } 3050 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get(); 3051 3052 // If we're not loading an AST context, we don't care about most records. 3053 if (!ContextObj) { 3054 switch (RecordType) { 3055 case IDENTIFIER_TABLE: 3056 case IDENTIFIER_OFFSET: 3057 case INTERESTING_IDENTIFIERS: 3058 case STATISTICS: 3059 case PP_CONDITIONAL_STACK: 3060 case PP_COUNTER_VALUE: 3061 case SOURCE_LOCATION_OFFSETS: 3062 case MODULE_OFFSET_MAP: 3063 case SOURCE_MANAGER_LINE_TABLE: 3064 case SOURCE_LOCATION_PRELOADS: 3065 case PPD_ENTITIES_OFFSETS: 3066 case HEADER_SEARCH_TABLE: 3067 case IMPORTED_MODULES: 3068 case MACRO_OFFSET: 3069 break; 3070 default: 3071 continue; 3072 } 3073 } 3074 3075 switch (RecordType) { 3076 default: // Default behavior: ignore. 3077 break; 3078 3079 case TYPE_OFFSET: { 3080 if (F.LocalNumTypes != 0) { 3081 Error("duplicate TYPE_OFFSET record in AST file"); 3082 return Failure; 3083 } 3084 F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data()); 3085 F.LocalNumTypes = Record[0]; 3086 unsigned LocalBaseTypeIndex = Record[1]; 3087 F.BaseTypeIndex = getTotalNumTypes(); 3088 3089 if (F.LocalNumTypes > 0) { 3090 // Introduce the global -> local mapping for types within this module. 3091 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F)); 3092 3093 // Introduce the local -> global mapping for types within this module. 3094 F.TypeRemap.insertOrReplace( 3095 std::make_pair(LocalBaseTypeIndex, 3096 F.BaseTypeIndex - LocalBaseTypeIndex)); 3097 3098 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes); 3099 } 3100 break; 3101 } 3102 3103 case DECL_OFFSET: { 3104 if (F.LocalNumDecls != 0) { 3105 Error("duplicate DECL_OFFSET record in AST file"); 3106 return Failure; 3107 } 3108 F.DeclOffsets = (const DeclOffset *)Blob.data(); 3109 F.LocalNumDecls = Record[0]; 3110 unsigned LocalBaseDeclID = Record[1]; 3111 F.BaseDeclID = getTotalNumDecls(); 3112 3113 if (F.LocalNumDecls > 0) { 3114 // Introduce the global -> local mapping for declarations within this 3115 // module. 3116 GlobalDeclMap.insert( 3117 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F)); 3118 3119 // Introduce the local -> global mapping for declarations within this 3120 // module. 3121 F.DeclRemap.insertOrReplace( 3122 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID)); 3123 3124 // Introduce the global -> local mapping for declarations within this 3125 // module. 3126 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID; 3127 3128 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls); 3129 } 3130 break; 3131 } 3132 3133 case TU_UPDATE_LEXICAL: { 3134 DeclContext *TU = ContextObj->getTranslationUnitDecl(); 3135 LexicalContents Contents( 3136 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 3137 Blob.data()), 3138 static_cast<unsigned int>(Blob.size() / 4)); 3139 TULexicalDecls.push_back(std::make_pair(&F, Contents)); 3140 TU->setHasExternalLexicalStorage(true); 3141 break; 3142 } 3143 3144 case UPDATE_VISIBLE: { 3145 unsigned Idx = 0; 3146 serialization::DeclID ID = ReadDeclID(F, Record, Idx); 3147 auto *Data = (const unsigned char*)Blob.data(); 3148 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data}); 3149 // If we've already loaded the decl, perform the updates when we finish 3150 // loading this block. 3151 if (Decl *D = GetExistingDecl(ID)) 3152 PendingUpdateRecords.push_back( 3153 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3154 break; 3155 } 3156 3157 case IDENTIFIER_TABLE: 3158 F.IdentifierTableData = Blob.data(); 3159 if (Record[0]) { 3160 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create( 3161 (const unsigned char *)F.IdentifierTableData + Record[0], 3162 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t), 3163 (const unsigned char *)F.IdentifierTableData, 3164 ASTIdentifierLookupTrait(*this, F)); 3165 3166 PP.getIdentifierTable().setExternalIdentifierLookup(this); 3167 } 3168 break; 3169 3170 case IDENTIFIER_OFFSET: { 3171 if (F.LocalNumIdentifiers != 0) { 3172 Error("duplicate IDENTIFIER_OFFSET record in AST file"); 3173 return Failure; 3174 } 3175 F.IdentifierOffsets = (const uint32_t *)Blob.data(); 3176 F.LocalNumIdentifiers = Record[0]; 3177 unsigned LocalBaseIdentifierID = Record[1]; 3178 F.BaseIdentifierID = getTotalNumIdentifiers(); 3179 3180 if (F.LocalNumIdentifiers > 0) { 3181 // Introduce the global -> local mapping for identifiers within this 3182 // module. 3183 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1, 3184 &F)); 3185 3186 // Introduce the local -> global mapping for identifiers within this 3187 // module. 3188 F.IdentifierRemap.insertOrReplace( 3189 std::make_pair(LocalBaseIdentifierID, 3190 F.BaseIdentifierID - LocalBaseIdentifierID)); 3191 3192 IdentifiersLoaded.resize(IdentifiersLoaded.size() 3193 + F.LocalNumIdentifiers); 3194 } 3195 break; 3196 } 3197 3198 case INTERESTING_IDENTIFIERS: 3199 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end()); 3200 break; 3201 3202 case EAGERLY_DESERIALIZED_DECLS: 3203 // FIXME: Skip reading this record if our ASTConsumer doesn't care 3204 // about "interesting" decls (for instance, if we're building a module). 3205 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3206 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3207 break; 3208 3209 case MODULAR_CODEGEN_DECLS: 3210 // FIXME: Skip reading this record if our ASTConsumer doesn't care about 3211 // them (ie: if we're not codegenerating this module). 3212 if (F.Kind == MK_MainFile || 3213 getContext().getLangOpts().BuildingPCHWithObjectFile) 3214 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3215 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3216 break; 3217 3218 case SPECIAL_TYPES: 3219 if (SpecialTypes.empty()) { 3220 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3221 SpecialTypes.push_back(getGlobalTypeID(F, Record[I])); 3222 break; 3223 } 3224 3225 if (SpecialTypes.size() != Record.size()) { 3226 Error("invalid special-types record"); 3227 return Failure; 3228 } 3229 3230 for (unsigned I = 0, N = Record.size(); I != N; ++I) { 3231 serialization::TypeID ID = getGlobalTypeID(F, Record[I]); 3232 if (!SpecialTypes[I]) 3233 SpecialTypes[I] = ID; 3234 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate 3235 // merge step? 3236 } 3237 break; 3238 3239 case STATISTICS: 3240 TotalNumStatements += Record[0]; 3241 TotalNumMacros += Record[1]; 3242 TotalLexicalDeclContexts += Record[2]; 3243 TotalVisibleDeclContexts += Record[3]; 3244 break; 3245 3246 case UNUSED_FILESCOPED_DECLS: 3247 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3248 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I])); 3249 break; 3250 3251 case DELEGATING_CTORS: 3252 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3253 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I])); 3254 break; 3255 3256 case WEAK_UNDECLARED_IDENTIFIERS: 3257 if (Record.size() % 4 != 0) { 3258 Error("invalid weak identifiers record"); 3259 return Failure; 3260 } 3261 3262 // FIXME: Ignore weak undeclared identifiers from non-original PCH 3263 // files. This isn't the way to do it :) 3264 WeakUndeclaredIdentifiers.clear(); 3265 3266 // Translate the weak, undeclared identifiers into global IDs. 3267 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) { 3268 WeakUndeclaredIdentifiers.push_back( 3269 getGlobalIdentifierID(F, Record[I++])); 3270 WeakUndeclaredIdentifiers.push_back( 3271 getGlobalIdentifierID(F, Record[I++])); 3272 WeakUndeclaredIdentifiers.push_back( 3273 ReadSourceLocation(F, Record, I).getRawEncoding()); 3274 WeakUndeclaredIdentifiers.push_back(Record[I++]); 3275 } 3276 break; 3277 3278 case SELECTOR_OFFSETS: { 3279 F.SelectorOffsets = (const uint32_t *)Blob.data(); 3280 F.LocalNumSelectors = Record[0]; 3281 unsigned LocalBaseSelectorID = Record[1]; 3282 F.BaseSelectorID = getTotalNumSelectors(); 3283 3284 if (F.LocalNumSelectors > 0) { 3285 // Introduce the global -> local mapping for selectors within this 3286 // module. 3287 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F)); 3288 3289 // Introduce the local -> global mapping for selectors within this 3290 // module. 3291 F.SelectorRemap.insertOrReplace( 3292 std::make_pair(LocalBaseSelectorID, 3293 F.BaseSelectorID - LocalBaseSelectorID)); 3294 3295 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors); 3296 } 3297 break; 3298 } 3299 3300 case METHOD_POOL: 3301 F.SelectorLookupTableData = (const unsigned char *)Blob.data(); 3302 if (Record[0]) 3303 F.SelectorLookupTable 3304 = ASTSelectorLookupTable::Create( 3305 F.SelectorLookupTableData + Record[0], 3306 F.SelectorLookupTableData, 3307 ASTSelectorLookupTrait(*this, F)); 3308 TotalNumMethodPoolEntries += Record[1]; 3309 break; 3310 3311 case REFERENCED_SELECTOR_POOL: 3312 if (!Record.empty()) { 3313 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) { 3314 ReferencedSelectorsData.push_back(getGlobalSelectorID(F, 3315 Record[Idx++])); 3316 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx). 3317 getRawEncoding()); 3318 } 3319 } 3320 break; 3321 3322 case PP_CONDITIONAL_STACK: 3323 if (!Record.empty()) { 3324 unsigned Idx = 0, End = Record.size() - 1; 3325 bool ReachedEOFWhileSkipping = Record[Idx++]; 3326 llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo; 3327 if (ReachedEOFWhileSkipping) { 3328 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx); 3329 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx); 3330 bool FoundNonSkipPortion = Record[Idx++]; 3331 bool FoundElse = Record[Idx++]; 3332 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx); 3333 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion, 3334 FoundElse, ElseLoc); 3335 } 3336 SmallVector<PPConditionalInfo, 4> ConditionalStack; 3337 while (Idx < End) { 3338 auto Loc = ReadSourceLocation(F, Record, Idx); 3339 bool WasSkipping = Record[Idx++]; 3340 bool FoundNonSkip = Record[Idx++]; 3341 bool FoundElse = Record[Idx++]; 3342 ConditionalStack.push_back( 3343 {Loc, WasSkipping, FoundNonSkip, FoundElse}); 3344 } 3345 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo); 3346 } 3347 break; 3348 3349 case PP_COUNTER_VALUE: 3350 if (!Record.empty() && Listener) 3351 Listener->ReadCounter(F, Record[0]); 3352 break; 3353 3354 case FILE_SORTED_DECLS: 3355 F.FileSortedDecls = (const DeclID *)Blob.data(); 3356 F.NumFileSortedDecls = Record[0]; 3357 break; 3358 3359 case SOURCE_LOCATION_OFFSETS: { 3360 F.SLocEntryOffsets = (const uint32_t *)Blob.data(); 3361 F.LocalNumSLocEntries = Record[0]; 3362 unsigned SLocSpaceSize = Record[1]; 3363 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset; 3364 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) = 3365 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, 3366 SLocSpaceSize); 3367 if (!F.SLocEntryBaseID) { 3368 Error("ran out of source locations"); 3369 break; 3370 } 3371 // Make our entry in the range map. BaseID is negative and growing, so 3372 // we invert it. Because we invert it, though, we need the other end of 3373 // the range. 3374 unsigned RangeStart = 3375 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1; 3376 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F)); 3377 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset); 3378 3379 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing. 3380 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0); 3381 GlobalSLocOffsetMap.insert( 3382 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset 3383 - SLocSpaceSize,&F)); 3384 3385 // Initialize the remapping table. 3386 // Invalid stays invalid. 3387 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0)); 3388 // This module. Base was 2 when being compiled. 3389 F.SLocRemap.insertOrReplace(std::make_pair(2U, 3390 static_cast<int>(F.SLocEntryBaseOffset - 2))); 3391 3392 TotalNumSLocEntries += F.LocalNumSLocEntries; 3393 break; 3394 } 3395 3396 case MODULE_OFFSET_MAP: 3397 F.ModuleOffsetMap = Blob; 3398 break; 3399 3400 case SOURCE_MANAGER_LINE_TABLE: 3401 if (ParseLineTable(F, Record)) { 3402 Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file"); 3403 return Failure; 3404 } 3405 break; 3406 3407 case SOURCE_LOCATION_PRELOADS: { 3408 // Need to transform from the local view (1-based IDs) to the global view, 3409 // which is based off F.SLocEntryBaseID. 3410 if (!F.PreloadSLocEntries.empty()) { 3411 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file"); 3412 return Failure; 3413 } 3414 3415 F.PreloadSLocEntries.swap(Record); 3416 break; 3417 } 3418 3419 case EXT_VECTOR_DECLS: 3420 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3421 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I])); 3422 break; 3423 3424 case VTABLE_USES: 3425 if (Record.size() % 3 != 0) { 3426 Error("Invalid VTABLE_USES record"); 3427 return Failure; 3428 } 3429 3430 // Later tables overwrite earlier ones. 3431 // FIXME: Modules will have some trouble with this. This is clearly not 3432 // the right way to do this. 3433 VTableUses.clear(); 3434 3435 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) { 3436 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++])); 3437 VTableUses.push_back( 3438 ReadSourceLocation(F, Record, Idx).getRawEncoding()); 3439 VTableUses.push_back(Record[Idx++]); 3440 } 3441 break; 3442 3443 case PENDING_IMPLICIT_INSTANTIATIONS: 3444 if (PendingInstantiations.size() % 2 != 0) { 3445 Error("Invalid existing PendingInstantiations"); 3446 return Failure; 3447 } 3448 3449 if (Record.size() % 2 != 0) { 3450 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block"); 3451 return Failure; 3452 } 3453 3454 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3455 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++])); 3456 PendingInstantiations.push_back( 3457 ReadSourceLocation(F, Record, I).getRawEncoding()); 3458 } 3459 break; 3460 3461 case SEMA_DECL_REFS: 3462 if (Record.size() != 3) { 3463 Error("Invalid SEMA_DECL_REFS block"); 3464 return Failure; 3465 } 3466 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3467 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3468 break; 3469 3470 case PPD_ENTITIES_OFFSETS: { 3471 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data(); 3472 assert(Blob.size() % sizeof(PPEntityOffset) == 0); 3473 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset); 3474 3475 unsigned LocalBasePreprocessedEntityID = Record[0]; 3476 3477 unsigned StartingID; 3478 if (!PP.getPreprocessingRecord()) 3479 PP.createPreprocessingRecord(); 3480 if (!PP.getPreprocessingRecord()->getExternalSource()) 3481 PP.getPreprocessingRecord()->SetExternalSource(*this); 3482 StartingID 3483 = PP.getPreprocessingRecord() 3484 ->allocateLoadedEntities(F.NumPreprocessedEntities); 3485 F.BasePreprocessedEntityID = StartingID; 3486 3487 if (F.NumPreprocessedEntities > 0) { 3488 // Introduce the global -> local mapping for preprocessed entities in 3489 // this module. 3490 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F)); 3491 3492 // Introduce the local -> global mapping for preprocessed entities in 3493 // this module. 3494 F.PreprocessedEntityRemap.insertOrReplace( 3495 std::make_pair(LocalBasePreprocessedEntityID, 3496 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID)); 3497 } 3498 3499 break; 3500 } 3501 3502 case PPD_SKIPPED_RANGES: { 3503 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data(); 3504 assert(Blob.size() % sizeof(PPSkippedRange) == 0); 3505 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange); 3506 3507 if (!PP.getPreprocessingRecord()) 3508 PP.createPreprocessingRecord(); 3509 if (!PP.getPreprocessingRecord()->getExternalSource()) 3510 PP.getPreprocessingRecord()->SetExternalSource(*this); 3511 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord() 3512 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges); 3513 3514 if (F.NumPreprocessedSkippedRanges > 0) 3515 GlobalSkippedRangeMap.insert( 3516 std::make_pair(F.BasePreprocessedSkippedRangeID, &F)); 3517 break; 3518 } 3519 3520 case DECL_UPDATE_OFFSETS: 3521 if (Record.size() % 2 != 0) { 3522 Error("invalid DECL_UPDATE_OFFSETS block in AST file"); 3523 return Failure; 3524 } 3525 for (unsigned I = 0, N = Record.size(); I != N; I += 2) { 3526 GlobalDeclID ID = getGlobalDeclID(F, Record[I]); 3527 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1])); 3528 3529 // If we've already loaded the decl, perform the updates when we finish 3530 // loading this block. 3531 if (Decl *D = GetExistingDecl(ID)) 3532 PendingUpdateRecords.push_back( 3533 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3534 } 3535 break; 3536 3537 case OBJC_CATEGORIES_MAP: 3538 if (F.LocalNumObjCCategoriesInMap != 0) { 3539 Error("duplicate OBJC_CATEGORIES_MAP record in AST file"); 3540 return Failure; 3541 } 3542 3543 F.LocalNumObjCCategoriesInMap = Record[0]; 3544 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data(); 3545 break; 3546 3547 case OBJC_CATEGORIES: 3548 F.ObjCCategories.swap(Record); 3549 break; 3550 3551 case CUDA_SPECIAL_DECL_REFS: 3552 // Later tables overwrite earlier ones. 3553 // FIXME: Modules will have trouble with this. 3554 CUDASpecialDeclRefs.clear(); 3555 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3556 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3557 break; 3558 3559 case HEADER_SEARCH_TABLE: 3560 F.HeaderFileInfoTableData = Blob.data(); 3561 F.LocalNumHeaderFileInfos = Record[1]; 3562 if (Record[0]) { 3563 F.HeaderFileInfoTable 3564 = HeaderFileInfoLookupTable::Create( 3565 (const unsigned char *)F.HeaderFileInfoTableData + Record[0], 3566 (const unsigned char *)F.HeaderFileInfoTableData, 3567 HeaderFileInfoTrait(*this, F, 3568 &PP.getHeaderSearchInfo(), 3569 Blob.data() + Record[2])); 3570 3571 PP.getHeaderSearchInfo().SetExternalSource(this); 3572 if (!PP.getHeaderSearchInfo().getExternalLookup()) 3573 PP.getHeaderSearchInfo().SetExternalLookup(this); 3574 } 3575 break; 3576 3577 case FP_PRAGMA_OPTIONS: 3578 // Later tables overwrite earlier ones. 3579 FPPragmaOptions.swap(Record); 3580 break; 3581 3582 case OPENCL_EXTENSIONS: 3583 for (unsigned I = 0, E = Record.size(); I != E; ) { 3584 auto Name = ReadString(Record, I); 3585 auto &Opt = OpenCLExtensions.OptMap[Name]; 3586 Opt.Supported = Record[I++] != 0; 3587 Opt.Enabled = Record[I++] != 0; 3588 Opt.Avail = Record[I++]; 3589 Opt.Core = Record[I++]; 3590 } 3591 break; 3592 3593 case OPENCL_EXTENSION_TYPES: 3594 for (unsigned I = 0, E = Record.size(); I != E;) { 3595 auto TypeID = static_cast<::TypeID>(Record[I++]); 3596 auto *Type = GetType(TypeID).getTypePtr(); 3597 auto NumExt = static_cast<unsigned>(Record[I++]); 3598 for (unsigned II = 0; II != NumExt; ++II) { 3599 auto Ext = ReadString(Record, I); 3600 OpenCLTypeExtMap[Type].insert(Ext); 3601 } 3602 } 3603 break; 3604 3605 case OPENCL_EXTENSION_DECLS: 3606 for (unsigned I = 0, E = Record.size(); I != E;) { 3607 auto DeclID = static_cast<::DeclID>(Record[I++]); 3608 auto *Decl = GetDecl(DeclID); 3609 auto NumExt = static_cast<unsigned>(Record[I++]); 3610 for (unsigned II = 0; II != NumExt; ++II) { 3611 auto Ext = ReadString(Record, I); 3612 OpenCLDeclExtMap[Decl].insert(Ext); 3613 } 3614 } 3615 break; 3616 3617 case TENTATIVE_DEFINITIONS: 3618 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3619 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I])); 3620 break; 3621 3622 case KNOWN_NAMESPACES: 3623 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3624 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I])); 3625 break; 3626 3627 case UNDEFINED_BUT_USED: 3628 if (UndefinedButUsed.size() % 2 != 0) { 3629 Error("Invalid existing UndefinedButUsed"); 3630 return Failure; 3631 } 3632 3633 if (Record.size() % 2 != 0) { 3634 Error("invalid undefined-but-used record"); 3635 return Failure; 3636 } 3637 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3638 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++])); 3639 UndefinedButUsed.push_back( 3640 ReadSourceLocation(F, Record, I).getRawEncoding()); 3641 } 3642 break; 3643 3644 case DELETE_EXPRS_TO_ANALYZE: 3645 for (unsigned I = 0, N = Record.size(); I != N;) { 3646 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++])); 3647 const uint64_t Count = Record[I++]; 3648 DelayedDeleteExprs.push_back(Count); 3649 for (uint64_t C = 0; C < Count; ++C) { 3650 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding()); 3651 bool IsArrayForm = Record[I++] == 1; 3652 DelayedDeleteExprs.push_back(IsArrayForm); 3653 } 3654 } 3655 break; 3656 3657 case IMPORTED_MODULES: 3658 if (!F.isModule()) { 3659 // If we aren't loading a module (which has its own exports), make 3660 // all of the imported modules visible. 3661 // FIXME: Deal with macros-only imports. 3662 for (unsigned I = 0, N = Record.size(); I != N; /**/) { 3663 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]); 3664 SourceLocation Loc = ReadSourceLocation(F, Record, I); 3665 if (GlobalID) { 3666 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc)); 3667 if (DeserializationListener) 3668 DeserializationListener->ModuleImportRead(GlobalID, Loc); 3669 } 3670 } 3671 } 3672 break; 3673 3674 case MACRO_OFFSET: { 3675 if (F.LocalNumMacros != 0) { 3676 Error("duplicate MACRO_OFFSET record in AST file"); 3677 return Failure; 3678 } 3679 F.MacroOffsets = (const uint32_t *)Blob.data(); 3680 F.LocalNumMacros = Record[0]; 3681 unsigned LocalBaseMacroID = Record[1]; 3682 F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset; 3683 F.BaseMacroID = getTotalNumMacros(); 3684 3685 if (F.LocalNumMacros > 0) { 3686 // Introduce the global -> local mapping for macros within this module. 3687 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F)); 3688 3689 // Introduce the local -> global mapping for macros within this module. 3690 F.MacroRemap.insertOrReplace( 3691 std::make_pair(LocalBaseMacroID, 3692 F.BaseMacroID - LocalBaseMacroID)); 3693 3694 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros); 3695 } 3696 break; 3697 } 3698 3699 case LATE_PARSED_TEMPLATE: 3700 LateParsedTemplates.emplace_back( 3701 std::piecewise_construct, std::forward_as_tuple(&F), 3702 std::forward_as_tuple(Record.begin(), Record.end())); 3703 break; 3704 3705 case OPTIMIZE_PRAGMA_OPTIONS: 3706 if (Record.size() != 1) { 3707 Error("invalid pragma optimize record"); 3708 return Failure; 3709 } 3710 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]); 3711 break; 3712 3713 case MSSTRUCT_PRAGMA_OPTIONS: 3714 if (Record.size() != 1) { 3715 Error("invalid pragma ms_struct record"); 3716 return Failure; 3717 } 3718 PragmaMSStructState = Record[0]; 3719 break; 3720 3721 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS: 3722 if (Record.size() != 2) { 3723 Error("invalid pragma ms_struct record"); 3724 return Failure; 3725 } 3726 PragmaMSPointersToMembersState = Record[0]; 3727 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]); 3728 break; 3729 3730 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES: 3731 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3732 UnusedLocalTypedefNameCandidates.push_back( 3733 getGlobalDeclID(F, Record[I])); 3734 break; 3735 3736 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH: 3737 if (Record.size() != 1) { 3738 Error("invalid cuda pragma options record"); 3739 return Failure; 3740 } 3741 ForceCUDAHostDeviceDepth = Record[0]; 3742 break; 3743 3744 case PACK_PRAGMA_OPTIONS: { 3745 if (Record.size() < 3) { 3746 Error("invalid pragma pack record"); 3747 return Failure; 3748 } 3749 PragmaPackCurrentValue = Record[0]; 3750 PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]); 3751 unsigned NumStackEntries = Record[2]; 3752 unsigned Idx = 3; 3753 // Reset the stack when importing a new module. 3754 PragmaPackStack.clear(); 3755 for (unsigned I = 0; I < NumStackEntries; ++I) { 3756 PragmaPackStackEntry Entry; 3757 Entry.Value = Record[Idx++]; 3758 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3759 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3760 PragmaPackStrings.push_back(ReadString(Record, Idx)); 3761 Entry.SlotLabel = PragmaPackStrings.back(); 3762 PragmaPackStack.push_back(Entry); 3763 } 3764 break; 3765 } 3766 3767 case FLOAT_CONTROL_PRAGMA_OPTIONS: { 3768 if (Record.size() < 3) { 3769 Error("invalid pragma pack record"); 3770 return Failure; 3771 } 3772 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]); 3773 FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]); 3774 unsigned NumStackEntries = Record[2]; 3775 unsigned Idx = 3; 3776 // Reset the stack when importing a new module. 3777 FpPragmaStack.clear(); 3778 for (unsigned I = 0; I < NumStackEntries; ++I) { 3779 FpPragmaStackEntry Entry; 3780 Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]); 3781 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3782 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3783 FpPragmaStrings.push_back(ReadString(Record, Idx)); 3784 Entry.SlotLabel = FpPragmaStrings.back(); 3785 FpPragmaStack.push_back(Entry); 3786 } 3787 break; 3788 } 3789 3790 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS: 3791 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3792 DeclsToCheckForDeferredDiags.push_back(getGlobalDeclID(F, Record[I])); 3793 break; 3794 } 3795 } 3796 } 3797 3798 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const { 3799 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read"); 3800 3801 // Additional remapping information. 3802 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data(); 3803 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size(); 3804 F.ModuleOffsetMap = StringRef(); 3805 3806 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders. 3807 if (F.SLocRemap.find(0) == F.SLocRemap.end()) { 3808 F.SLocRemap.insert(std::make_pair(0U, 0)); 3809 F.SLocRemap.insert(std::make_pair(2U, 1)); 3810 } 3811 3812 // Continuous range maps we may be updating in our module. 3813 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder; 3814 RemapBuilder SLocRemap(F.SLocRemap); 3815 RemapBuilder IdentifierRemap(F.IdentifierRemap); 3816 RemapBuilder MacroRemap(F.MacroRemap); 3817 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap); 3818 RemapBuilder SubmoduleRemap(F.SubmoduleRemap); 3819 RemapBuilder SelectorRemap(F.SelectorRemap); 3820 RemapBuilder DeclRemap(F.DeclRemap); 3821 RemapBuilder TypeRemap(F.TypeRemap); 3822 3823 while (Data < DataEnd) { 3824 // FIXME: Looking up dependency modules by filename is horrible. Let's 3825 // start fixing this with prebuilt, explicit and implicit modules and see 3826 // how it goes... 3827 using namespace llvm::support; 3828 ModuleKind Kind = static_cast<ModuleKind>( 3829 endian::readNext<uint8_t, little, unaligned>(Data)); 3830 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data); 3831 StringRef Name = StringRef((const char*)Data, Len); 3832 Data += Len; 3833 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule || 3834 Kind == MK_ImplicitModule 3835 ? ModuleMgr.lookupByModuleName(Name) 3836 : ModuleMgr.lookupByFileName(Name)); 3837 if (!OM) { 3838 std::string Msg = 3839 "SourceLocation remap refers to unknown module, cannot find "; 3840 Msg.append(std::string(Name)); 3841 Error(Msg); 3842 return; 3843 } 3844 3845 uint32_t SLocOffset = 3846 endian::readNext<uint32_t, little, unaligned>(Data); 3847 uint32_t IdentifierIDOffset = 3848 endian::readNext<uint32_t, little, unaligned>(Data); 3849 uint32_t MacroIDOffset = 3850 endian::readNext<uint32_t, little, unaligned>(Data); 3851 uint32_t PreprocessedEntityIDOffset = 3852 endian::readNext<uint32_t, little, unaligned>(Data); 3853 uint32_t SubmoduleIDOffset = 3854 endian::readNext<uint32_t, little, unaligned>(Data); 3855 uint32_t SelectorIDOffset = 3856 endian::readNext<uint32_t, little, unaligned>(Data); 3857 uint32_t DeclIDOffset = 3858 endian::readNext<uint32_t, little, unaligned>(Data); 3859 uint32_t TypeIndexOffset = 3860 endian::readNext<uint32_t, little, unaligned>(Data); 3861 3862 uint32_t None = std::numeric_limits<uint32_t>::max(); 3863 3864 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset, 3865 RemapBuilder &Remap) { 3866 if (Offset != None) 3867 Remap.insert(std::make_pair(Offset, 3868 static_cast<int>(BaseOffset - Offset))); 3869 }; 3870 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap); 3871 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap); 3872 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap); 3873 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID, 3874 PreprocessedEntityRemap); 3875 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap); 3876 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap); 3877 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap); 3878 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap); 3879 3880 // Global -> local mappings. 3881 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset; 3882 } 3883 } 3884 3885 ASTReader::ASTReadResult 3886 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F, 3887 const ModuleFile *ImportedBy, 3888 unsigned ClientLoadCapabilities) { 3889 unsigned Idx = 0; 3890 F.ModuleMapPath = ReadPath(F, Record, Idx); 3891 3892 // Try to resolve ModuleName in the current header search context and 3893 // verify that it is found in the same module map file as we saved. If the 3894 // top-level AST file is a main file, skip this check because there is no 3895 // usable header search context. 3896 assert(!F.ModuleName.empty() && 3897 "MODULE_NAME should come before MODULE_MAP_FILE"); 3898 if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) { 3899 // An implicitly-loaded module file should have its module listed in some 3900 // module map file that we've already loaded. 3901 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName); 3902 auto &Map = PP.getHeaderSearchInfo().getModuleMap(); 3903 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr; 3904 // Don't emit module relocation error if we have -fno-validate-pch 3905 if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) { 3906 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) { 3907 if (auto ASTFE = M ? M->getASTFile() : None) { 3908 // This module was defined by an imported (explicit) module. 3909 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName 3910 << ASTFE->getName(); 3911 } else { 3912 // This module was built with a different module map. 3913 Diag(diag::err_imported_module_not_found) 3914 << F.ModuleName << F.FileName 3915 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath 3916 << !ImportedBy; 3917 // In case it was imported by a PCH, there's a chance the user is 3918 // just missing to include the search path to the directory containing 3919 // the modulemap. 3920 if (ImportedBy && ImportedBy->Kind == MK_PCH) 3921 Diag(diag::note_imported_by_pch_module_not_found) 3922 << llvm::sys::path::parent_path(F.ModuleMapPath); 3923 } 3924 } 3925 return OutOfDate; 3926 } 3927 3928 assert(M && M->Name == F.ModuleName && "found module with different name"); 3929 3930 // Check the primary module map file. 3931 auto StoredModMap = FileMgr.getFile(F.ModuleMapPath); 3932 if (!StoredModMap || *StoredModMap != ModMap) { 3933 assert(ModMap && "found module is missing module map file"); 3934 assert((ImportedBy || F.Kind == MK_ImplicitModule) && 3935 "top-level import should be verified"); 3936 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy; 3937 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3938 Diag(diag::err_imported_module_modmap_changed) 3939 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName) 3940 << ModMap->getName() << F.ModuleMapPath << NotImported; 3941 return OutOfDate; 3942 } 3943 3944 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps; 3945 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) { 3946 // FIXME: we should use input files rather than storing names. 3947 std::string Filename = ReadPath(F, Record, Idx); 3948 auto F = FileMgr.getFile(Filename, false, false); 3949 if (!F) { 3950 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3951 Error("could not find file '" + Filename +"' referenced by AST file"); 3952 return OutOfDate; 3953 } 3954 AdditionalStoredMaps.insert(*F); 3955 } 3956 3957 // Check any additional module map files (e.g. module.private.modulemap) 3958 // that are not in the pcm. 3959 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) { 3960 for (const FileEntry *ModMap : *AdditionalModuleMaps) { 3961 // Remove files that match 3962 // Note: SmallPtrSet::erase is really remove 3963 if (!AdditionalStoredMaps.erase(ModMap)) { 3964 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3965 Diag(diag::err_module_different_modmap) 3966 << F.ModuleName << /*new*/0 << ModMap->getName(); 3967 return OutOfDate; 3968 } 3969 } 3970 } 3971 3972 // Check any additional module map files that are in the pcm, but not 3973 // found in header search. Cases that match are already removed. 3974 for (const FileEntry *ModMap : AdditionalStoredMaps) { 3975 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3976 Diag(diag::err_module_different_modmap) 3977 << F.ModuleName << /*not new*/1 << ModMap->getName(); 3978 return OutOfDate; 3979 } 3980 } 3981 3982 if (Listener) 3983 Listener->ReadModuleMapFile(F.ModuleMapPath); 3984 return Success; 3985 } 3986 3987 /// Move the given method to the back of the global list of methods. 3988 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) { 3989 // Find the entry for this selector in the method pool. 3990 Sema::GlobalMethodPool::iterator Known 3991 = S.MethodPool.find(Method->getSelector()); 3992 if (Known == S.MethodPool.end()) 3993 return; 3994 3995 // Retrieve the appropriate method list. 3996 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first 3997 : Known->second.second; 3998 bool Found = false; 3999 for (ObjCMethodList *List = &Start; List; List = List->getNext()) { 4000 if (!Found) { 4001 if (List->getMethod() == Method) { 4002 Found = true; 4003 } else { 4004 // Keep searching. 4005 continue; 4006 } 4007 } 4008 4009 if (List->getNext()) 4010 List->setMethod(List->getNext()->getMethod()); 4011 else 4012 List->setMethod(Method); 4013 } 4014 } 4015 4016 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) { 4017 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?"); 4018 for (Decl *D : Names) { 4019 bool wasHidden = !D->isUnconditionallyVisible(); 4020 D->setVisibleDespiteOwningModule(); 4021 4022 if (wasHidden && SemaObj) { 4023 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) { 4024 moveMethodToBackOfGlobalList(*SemaObj, Method); 4025 } 4026 } 4027 } 4028 } 4029 4030 void ASTReader::makeModuleVisible(Module *Mod, 4031 Module::NameVisibilityKind NameVisibility, 4032 SourceLocation ImportLoc) { 4033 llvm::SmallPtrSet<Module *, 4> Visited; 4034 SmallVector<Module *, 4> Stack; 4035 Stack.push_back(Mod); 4036 while (!Stack.empty()) { 4037 Mod = Stack.pop_back_val(); 4038 4039 if (NameVisibility <= Mod->NameVisibility) { 4040 // This module already has this level of visibility (or greater), so 4041 // there is nothing more to do. 4042 continue; 4043 } 4044 4045 if (Mod->isUnimportable()) { 4046 // Modules that aren't importable cannot be made visible. 4047 continue; 4048 } 4049 4050 // Update the module's name visibility. 4051 Mod->NameVisibility = NameVisibility; 4052 4053 // If we've already deserialized any names from this module, 4054 // mark them as visible. 4055 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod); 4056 if (Hidden != HiddenNamesMap.end()) { 4057 auto HiddenNames = std::move(*Hidden); 4058 HiddenNamesMap.erase(Hidden); 4059 makeNamesVisible(HiddenNames.second, HiddenNames.first); 4060 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() && 4061 "making names visible added hidden names"); 4062 } 4063 4064 // Push any exported modules onto the stack to be marked as visible. 4065 SmallVector<Module *, 16> Exports; 4066 Mod->getExportedModules(Exports); 4067 for (SmallVectorImpl<Module *>::iterator 4068 I = Exports.begin(), E = Exports.end(); I != E; ++I) { 4069 Module *Exported = *I; 4070 if (Visited.insert(Exported).second) 4071 Stack.push_back(Exported); 4072 } 4073 } 4074 } 4075 4076 /// We've merged the definition \p MergedDef into the existing definition 4077 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made 4078 /// visible. 4079 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def, 4080 NamedDecl *MergedDef) { 4081 if (!Def->isUnconditionallyVisible()) { 4082 // If MergedDef is visible or becomes visible, make the definition visible. 4083 if (MergedDef->isUnconditionallyVisible()) 4084 Def->setVisibleDespiteOwningModule(); 4085 else { 4086 getContext().mergeDefinitionIntoModule( 4087 Def, MergedDef->getImportedOwningModule(), 4088 /*NotifyListeners*/ false); 4089 PendingMergedDefinitionsToDeduplicate.insert(Def); 4090 } 4091 } 4092 } 4093 4094 bool ASTReader::loadGlobalIndex() { 4095 if (GlobalIndex) 4096 return false; 4097 4098 if (TriedLoadingGlobalIndex || !UseGlobalIndex || 4099 !PP.getLangOpts().Modules) 4100 return true; 4101 4102 // Try to load the global index. 4103 TriedLoadingGlobalIndex = true; 4104 StringRef ModuleCachePath 4105 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath(); 4106 std::pair<GlobalModuleIndex *, llvm::Error> Result = 4107 GlobalModuleIndex::readIndex(ModuleCachePath); 4108 if (llvm::Error Err = std::move(Result.second)) { 4109 assert(!Result.first); 4110 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 4111 return true; 4112 } 4113 4114 GlobalIndex.reset(Result.first); 4115 ModuleMgr.setGlobalIndex(GlobalIndex.get()); 4116 return false; 4117 } 4118 4119 bool ASTReader::isGlobalIndexUnavailable() const { 4120 return PP.getLangOpts().Modules && UseGlobalIndex && 4121 !hasGlobalIndex() && TriedLoadingGlobalIndex; 4122 } 4123 4124 static void updateModuleTimestamp(ModuleFile &MF) { 4125 // Overwrite the timestamp file contents so that file's mtime changes. 4126 std::string TimestampFilename = MF.getTimestampFilename(); 4127 std::error_code EC; 4128 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text); 4129 if (EC) 4130 return; 4131 OS << "Timestamp file\n"; 4132 OS.close(); 4133 OS.clear_error(); // Avoid triggering a fatal error. 4134 } 4135 4136 /// Given a cursor at the start of an AST file, scan ahead and drop the 4137 /// cursor into the start of the given block ID, returning false on success and 4138 /// true on failure. 4139 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) { 4140 while (true) { 4141 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 4142 if (!MaybeEntry) { 4143 // FIXME this drops errors on the floor. 4144 consumeError(MaybeEntry.takeError()); 4145 return true; 4146 } 4147 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4148 4149 switch (Entry.Kind) { 4150 case llvm::BitstreamEntry::Error: 4151 case llvm::BitstreamEntry::EndBlock: 4152 return true; 4153 4154 case llvm::BitstreamEntry::Record: 4155 // Ignore top-level records. 4156 if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID)) 4157 break; 4158 else { 4159 // FIXME this drops errors on the floor. 4160 consumeError(Skipped.takeError()); 4161 return true; 4162 } 4163 4164 case llvm::BitstreamEntry::SubBlock: 4165 if (Entry.ID == BlockID) { 4166 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) { 4167 // FIXME this drops the error on the floor. 4168 consumeError(std::move(Err)); 4169 return true; 4170 } 4171 // Found it! 4172 return false; 4173 } 4174 4175 if (llvm::Error Err = Cursor.SkipBlock()) { 4176 // FIXME this drops the error on the floor. 4177 consumeError(std::move(Err)); 4178 return true; 4179 } 4180 } 4181 } 4182 } 4183 4184 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, 4185 ModuleKind Type, 4186 SourceLocation ImportLoc, 4187 unsigned ClientLoadCapabilities, 4188 SmallVectorImpl<ImportedSubmodule> *Imported) { 4189 llvm::SaveAndRestore<SourceLocation> 4190 SetCurImportLocRAII(CurrentImportLoc, ImportLoc); 4191 4192 // Defer any pending actions until we get to the end of reading the AST file. 4193 Deserializing AnASTFile(this); 4194 4195 // Bump the generation number. 4196 unsigned PreviousGeneration = 0; 4197 if (ContextObj) 4198 PreviousGeneration = incrementGeneration(*ContextObj); 4199 4200 unsigned NumModules = ModuleMgr.size(); 4201 auto removeModulesAndReturn = [&](ASTReadResult ReadResult) { 4202 assert(ReadResult && "expected to return error"); 4203 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, 4204 PP.getLangOpts().Modules 4205 ? &PP.getHeaderSearchInfo().getModuleMap() 4206 : nullptr); 4207 4208 // If we find that any modules are unusable, the global index is going 4209 // to be out-of-date. Just remove it. 4210 GlobalIndex.reset(); 4211 ModuleMgr.setGlobalIndex(nullptr); 4212 return ReadResult; 4213 }; 4214 4215 SmallVector<ImportedModule, 4> Loaded; 4216 switch (ASTReadResult ReadResult = 4217 ReadASTCore(FileName, Type, ImportLoc, 4218 /*ImportedBy=*/nullptr, Loaded, 0, 0, 4219 ASTFileSignature(), ClientLoadCapabilities)) { 4220 case Failure: 4221 case Missing: 4222 case OutOfDate: 4223 case VersionMismatch: 4224 case ConfigurationMismatch: 4225 case HadErrors: 4226 return removeModulesAndReturn(ReadResult); 4227 case Success: 4228 break; 4229 } 4230 4231 // Here comes stuff that we only do once the entire chain is loaded. 4232 4233 // Load the AST blocks of all of the modules that we loaded. We can still 4234 // hit errors parsing the ASTs at this point. 4235 for (ImportedModule &M : Loaded) { 4236 ModuleFile &F = *M.Mod; 4237 4238 // Read the AST block. 4239 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities)) 4240 return removeModulesAndReturn(Result); 4241 4242 // The AST block should always have a definition for the main module. 4243 if (F.isModule() && !F.DidReadTopLevelSubmodule) { 4244 Error(diag::err_module_file_missing_top_level_submodule, F.FileName); 4245 return removeModulesAndReturn(Failure); 4246 } 4247 4248 // Read the extension blocks. 4249 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) { 4250 if (ASTReadResult Result = ReadExtensionBlock(F)) 4251 return removeModulesAndReturn(Result); 4252 } 4253 4254 // Once read, set the ModuleFile bit base offset and update the size in 4255 // bits of all files we've seen. 4256 F.GlobalBitOffset = TotalModulesSizeInBits; 4257 TotalModulesSizeInBits += F.SizeInBits; 4258 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F)); 4259 } 4260 4261 // Preload source locations and interesting indentifiers. 4262 for (ImportedModule &M : Loaded) { 4263 ModuleFile &F = *M.Mod; 4264 4265 // Preload SLocEntries. 4266 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) { 4267 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID; 4268 // Load it through the SourceManager and don't call ReadSLocEntry() 4269 // directly because the entry may have already been loaded in which case 4270 // calling ReadSLocEntry() directly would trigger an assertion in 4271 // SourceManager. 4272 SourceMgr.getLoadedSLocEntryByID(Index); 4273 } 4274 4275 // Map the original source file ID into the ID space of the current 4276 // compilation. 4277 if (F.OriginalSourceFileID.isValid()) { 4278 F.OriginalSourceFileID = FileID::get( 4279 F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1); 4280 } 4281 4282 // Preload all the pending interesting identifiers by marking them out of 4283 // date. 4284 for (auto Offset : F.PreloadIdentifierOffsets) { 4285 const unsigned char *Data = reinterpret_cast<const unsigned char *>( 4286 F.IdentifierTableData + Offset); 4287 4288 ASTIdentifierLookupTrait Trait(*this, F); 4289 auto KeyDataLen = Trait.ReadKeyDataLength(Data); 4290 auto Key = Trait.ReadKey(Data, KeyDataLen.first); 4291 auto &II = PP.getIdentifierTable().getOwn(Key); 4292 II.setOutOfDate(true); 4293 4294 // Mark this identifier as being from an AST file so that we can track 4295 // whether we need to serialize it. 4296 markIdentifierFromAST(*this, II); 4297 4298 // Associate the ID with the identifier so that the writer can reuse it. 4299 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first); 4300 SetIdentifierInfo(ID, &II); 4301 } 4302 } 4303 4304 // Setup the import locations and notify the module manager that we've 4305 // committed to these module files. 4306 for (ImportedModule &M : Loaded) { 4307 ModuleFile &F = *M.Mod; 4308 4309 ModuleMgr.moduleFileAccepted(&F); 4310 4311 // Set the import location. 4312 F.DirectImportLoc = ImportLoc; 4313 // FIXME: We assume that locations from PCH / preamble do not need 4314 // any translation. 4315 if (!M.ImportedBy) 4316 F.ImportLoc = M.ImportLoc; 4317 else 4318 F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc); 4319 } 4320 4321 if (!PP.getLangOpts().CPlusPlus || 4322 (Type != MK_ImplicitModule && Type != MK_ExplicitModule && 4323 Type != MK_PrebuiltModule)) { 4324 // Mark all of the identifiers in the identifier table as being out of date, 4325 // so that various accessors know to check the loaded modules when the 4326 // identifier is used. 4327 // 4328 // For C++ modules, we don't need information on many identifiers (just 4329 // those that provide macros or are poisoned), so we mark all of 4330 // the interesting ones via PreloadIdentifierOffsets. 4331 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(), 4332 IdEnd = PP.getIdentifierTable().end(); 4333 Id != IdEnd; ++Id) 4334 Id->second->setOutOfDate(true); 4335 } 4336 // Mark selectors as out of date. 4337 for (auto Sel : SelectorGeneration) 4338 SelectorOutOfDate[Sel.first] = true; 4339 4340 // Resolve any unresolved module exports. 4341 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) { 4342 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I]; 4343 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID); 4344 Module *ResolvedMod = getSubmodule(GlobalID); 4345 4346 switch (Unresolved.Kind) { 4347 case UnresolvedModuleRef::Conflict: 4348 if (ResolvedMod) { 4349 Module::Conflict Conflict; 4350 Conflict.Other = ResolvedMod; 4351 Conflict.Message = Unresolved.String.str(); 4352 Unresolved.Mod->Conflicts.push_back(Conflict); 4353 } 4354 continue; 4355 4356 case UnresolvedModuleRef::Import: 4357 if (ResolvedMod) 4358 Unresolved.Mod->Imports.insert(ResolvedMod); 4359 continue; 4360 4361 case UnresolvedModuleRef::Export: 4362 if (ResolvedMod || Unresolved.IsWildcard) 4363 Unresolved.Mod->Exports.push_back( 4364 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard)); 4365 continue; 4366 } 4367 } 4368 UnresolvedModuleRefs.clear(); 4369 4370 if (Imported) 4371 Imported->append(ImportedModules.begin(), 4372 ImportedModules.end()); 4373 4374 // FIXME: How do we load the 'use'd modules? They may not be submodules. 4375 // Might be unnecessary as use declarations are only used to build the 4376 // module itself. 4377 4378 if (ContextObj) 4379 InitializeContext(); 4380 4381 if (SemaObj) 4382 UpdateSema(); 4383 4384 if (DeserializationListener) 4385 DeserializationListener->ReaderInitialized(this); 4386 4387 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule(); 4388 if (PrimaryModule.OriginalSourceFileID.isValid()) { 4389 // If this AST file is a precompiled preamble, then set the 4390 // preamble file ID of the source manager to the file source file 4391 // from which the preamble was built. 4392 if (Type == MK_Preamble) { 4393 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID); 4394 } else if (Type == MK_MainFile) { 4395 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID); 4396 } 4397 } 4398 4399 // For any Objective-C class definitions we have already loaded, make sure 4400 // that we load any additional categories. 4401 if (ContextObj) { 4402 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) { 4403 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(), 4404 ObjCClassesLoaded[I], 4405 PreviousGeneration); 4406 } 4407 } 4408 4409 if (PP.getHeaderSearchInfo() 4410 .getHeaderSearchOpts() 4411 .ModulesValidateOncePerBuildSession) { 4412 // Now we are certain that the module and all modules it depends on are 4413 // up to date. Create or update timestamp files for modules that are 4414 // located in the module cache (not for PCH files that could be anywhere 4415 // in the filesystem). 4416 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) { 4417 ImportedModule &M = Loaded[I]; 4418 if (M.Mod->Kind == MK_ImplicitModule) { 4419 updateModuleTimestamp(*M.Mod); 4420 } 4421 } 4422 } 4423 4424 return Success; 4425 } 4426 4427 static ASTFileSignature readASTFileSignature(StringRef PCH); 4428 4429 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'. 4430 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) { 4431 // FIXME checking magic headers is done in other places such as 4432 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't 4433 // always done the same. Unify it all with a helper. 4434 if (!Stream.canSkipToPos(4)) 4435 return llvm::createStringError(std::errc::illegal_byte_sequence, 4436 "file too small to contain AST file magic"); 4437 for (unsigned C : {'C', 'P', 'C', 'H'}) 4438 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) { 4439 if (Res.get() != C) 4440 return llvm::createStringError( 4441 std::errc::illegal_byte_sequence, 4442 "file doesn't start with AST file magic"); 4443 } else 4444 return Res.takeError(); 4445 return llvm::Error::success(); 4446 } 4447 4448 static unsigned moduleKindForDiagnostic(ModuleKind Kind) { 4449 switch (Kind) { 4450 case MK_PCH: 4451 return 0; // PCH 4452 case MK_ImplicitModule: 4453 case MK_ExplicitModule: 4454 case MK_PrebuiltModule: 4455 return 1; // module 4456 case MK_MainFile: 4457 case MK_Preamble: 4458 return 2; // main source file 4459 } 4460 llvm_unreachable("unknown module kind"); 4461 } 4462 4463 ASTReader::ASTReadResult 4464 ASTReader::ReadASTCore(StringRef FileName, 4465 ModuleKind Type, 4466 SourceLocation ImportLoc, 4467 ModuleFile *ImportedBy, 4468 SmallVectorImpl<ImportedModule> &Loaded, 4469 off_t ExpectedSize, time_t ExpectedModTime, 4470 ASTFileSignature ExpectedSignature, 4471 unsigned ClientLoadCapabilities) { 4472 ModuleFile *M; 4473 std::string ErrorStr; 4474 ModuleManager::AddModuleResult AddResult 4475 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy, 4476 getGeneration(), ExpectedSize, ExpectedModTime, 4477 ExpectedSignature, readASTFileSignature, 4478 M, ErrorStr); 4479 4480 switch (AddResult) { 4481 case ModuleManager::AlreadyLoaded: 4482 Diag(diag::remark_module_import) 4483 << M->ModuleName << M->FileName << (ImportedBy ? true : false) 4484 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 4485 return Success; 4486 4487 case ModuleManager::NewlyLoaded: 4488 // Load module file below. 4489 break; 4490 4491 case ModuleManager::Missing: 4492 // The module file was missing; if the client can handle that, return 4493 // it. 4494 if (ClientLoadCapabilities & ARR_Missing) 4495 return Missing; 4496 4497 // Otherwise, return an error. 4498 Diag(diag::err_ast_file_not_found) 4499 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty() 4500 << ErrorStr; 4501 return Failure; 4502 4503 case ModuleManager::OutOfDate: 4504 // We couldn't load the module file because it is out-of-date. If the 4505 // client can handle out-of-date, return it. 4506 if (ClientLoadCapabilities & ARR_OutOfDate) 4507 return OutOfDate; 4508 4509 // Otherwise, return an error. 4510 Diag(diag::err_ast_file_out_of_date) 4511 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty() 4512 << ErrorStr; 4513 return Failure; 4514 } 4515 4516 assert(M && "Missing module file"); 4517 4518 bool ShouldFinalizePCM = false; 4519 auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() { 4520 auto &MC = getModuleManager().getModuleCache(); 4521 if (ShouldFinalizePCM) 4522 MC.finalizePCM(FileName); 4523 else 4524 MC.tryToDropPCM(FileName); 4525 }); 4526 ModuleFile &F = *M; 4527 BitstreamCursor &Stream = F.Stream; 4528 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer)); 4529 F.SizeInBits = F.Buffer->getBufferSize() * 8; 4530 4531 // Sniff for the signature. 4532 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4533 Diag(diag::err_ast_file_invalid) 4534 << moduleKindForDiagnostic(Type) << FileName << std::move(Err); 4535 return Failure; 4536 } 4537 4538 // This is used for compatibility with older PCH formats. 4539 bool HaveReadControlBlock = false; 4540 while (true) { 4541 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4542 if (!MaybeEntry) { 4543 Error(MaybeEntry.takeError()); 4544 return Failure; 4545 } 4546 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4547 4548 switch (Entry.Kind) { 4549 case llvm::BitstreamEntry::Error: 4550 case llvm::BitstreamEntry::Record: 4551 case llvm::BitstreamEntry::EndBlock: 4552 Error("invalid record at top-level of AST file"); 4553 return Failure; 4554 4555 case llvm::BitstreamEntry::SubBlock: 4556 break; 4557 } 4558 4559 switch (Entry.ID) { 4560 case CONTROL_BLOCK_ID: 4561 HaveReadControlBlock = true; 4562 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) { 4563 case Success: 4564 // Check that we didn't try to load a non-module AST file as a module. 4565 // 4566 // FIXME: Should we also perform the converse check? Loading a module as 4567 // a PCH file sort of works, but it's a bit wonky. 4568 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule || 4569 Type == MK_PrebuiltModule) && 4570 F.ModuleName.empty()) { 4571 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure; 4572 if (Result != OutOfDate || 4573 (ClientLoadCapabilities & ARR_OutOfDate) == 0) 4574 Diag(diag::err_module_file_not_module) << FileName; 4575 return Result; 4576 } 4577 break; 4578 4579 case Failure: return Failure; 4580 case Missing: return Missing; 4581 case OutOfDate: return OutOfDate; 4582 case VersionMismatch: return VersionMismatch; 4583 case ConfigurationMismatch: return ConfigurationMismatch; 4584 case HadErrors: return HadErrors; 4585 } 4586 break; 4587 4588 case AST_BLOCK_ID: 4589 if (!HaveReadControlBlock) { 4590 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 4591 Diag(diag::err_pch_version_too_old); 4592 return VersionMismatch; 4593 } 4594 4595 // Record that we've loaded this module. 4596 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc)); 4597 ShouldFinalizePCM = true; 4598 return Success; 4599 4600 case UNHASHED_CONTROL_BLOCK_ID: 4601 // This block is handled using look-ahead during ReadControlBlock. We 4602 // shouldn't get here! 4603 Error("malformed block record in AST file"); 4604 return Failure; 4605 4606 default: 4607 if (llvm::Error Err = Stream.SkipBlock()) { 4608 Error(std::move(Err)); 4609 return Failure; 4610 } 4611 break; 4612 } 4613 } 4614 4615 llvm_unreachable("unexpected break; expected return"); 4616 } 4617 4618 ASTReader::ASTReadResult 4619 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy, 4620 unsigned ClientLoadCapabilities) { 4621 const HeaderSearchOptions &HSOpts = 4622 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 4623 bool AllowCompatibleConfigurationMismatch = 4624 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 4625 4626 ASTReadResult Result = readUnhashedControlBlockImpl( 4627 &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch, 4628 Listener.get(), 4629 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions); 4630 4631 // If F was directly imported by another module, it's implicitly validated by 4632 // the importing module. 4633 if (DisableValidation || WasImportedBy || 4634 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 4635 return Success; 4636 4637 if (Result == Failure) { 4638 Error("malformed block record in AST file"); 4639 return Failure; 4640 } 4641 4642 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) { 4643 // If this module has already been finalized in the ModuleCache, we're stuck 4644 // with it; we can only load a single version of each module. 4645 // 4646 // This can happen when a module is imported in two contexts: in one, as a 4647 // user module; in another, as a system module (due to an import from 4648 // another module marked with the [system] flag). It usually indicates a 4649 // bug in the module map: this module should also be marked with [system]. 4650 // 4651 // If -Wno-system-headers (the default), and the first import is as a 4652 // system module, then validation will fail during the as-user import, 4653 // since -Werror flags won't have been validated. However, it's reasonable 4654 // to treat this consistently as a system module. 4655 // 4656 // If -Wsystem-headers, the PCM on disk was built with 4657 // -Wno-system-headers, and the first import is as a user module, then 4658 // validation will fail during the as-system import since the PCM on disk 4659 // doesn't guarantee that -Werror was respected. However, the -Werror 4660 // flags were checked during the initial as-user import. 4661 if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) { 4662 Diag(diag::warn_module_system_bit_conflict) << F.FileName; 4663 return Success; 4664 } 4665 } 4666 4667 return Result; 4668 } 4669 4670 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl( 4671 ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities, 4672 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener, 4673 bool ValidateDiagnosticOptions) { 4674 // Initialize a stream. 4675 BitstreamCursor Stream(StreamData); 4676 4677 // Sniff for the signature. 4678 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4679 // FIXME this drops the error on the floor. 4680 consumeError(std::move(Err)); 4681 return Failure; 4682 } 4683 4684 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 4685 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 4686 return Failure; 4687 4688 // Read all of the records in the options block. 4689 RecordData Record; 4690 ASTReadResult Result = Success; 4691 while (true) { 4692 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4693 if (!MaybeEntry) { 4694 // FIXME this drops the error on the floor. 4695 consumeError(MaybeEntry.takeError()); 4696 return Failure; 4697 } 4698 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4699 4700 switch (Entry.Kind) { 4701 case llvm::BitstreamEntry::Error: 4702 case llvm::BitstreamEntry::SubBlock: 4703 return Failure; 4704 4705 case llvm::BitstreamEntry::EndBlock: 4706 return Result; 4707 4708 case llvm::BitstreamEntry::Record: 4709 // The interesting case. 4710 break; 4711 } 4712 4713 // Read and process a record. 4714 Record.clear(); 4715 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record); 4716 if (!MaybeRecordType) { 4717 // FIXME this drops the error. 4718 return Failure; 4719 } 4720 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) { 4721 case SIGNATURE: 4722 if (F) 4723 F->Signature = ASTFileSignature::create(Record.begin(), Record.end()); 4724 break; 4725 case AST_BLOCK_HASH: 4726 if (F) 4727 F->ASTBlockHash = 4728 ASTFileSignature::create(Record.begin(), Record.end()); 4729 break; 4730 case DIAGNOSTIC_OPTIONS: { 4731 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 4732 if (Listener && ValidateDiagnosticOptions && 4733 !AllowCompatibleConfigurationMismatch && 4734 ParseDiagnosticOptions(Record, Complain, *Listener)) 4735 Result = OutOfDate; // Don't return early. Read the signature. 4736 break; 4737 } 4738 case DIAG_PRAGMA_MAPPINGS: 4739 if (!F) 4740 break; 4741 if (F->PragmaDiagMappings.empty()) 4742 F->PragmaDiagMappings.swap(Record); 4743 else 4744 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(), 4745 Record.begin(), Record.end()); 4746 break; 4747 } 4748 } 4749 } 4750 4751 /// Parse a record and blob containing module file extension metadata. 4752 static bool parseModuleFileExtensionMetadata( 4753 const SmallVectorImpl<uint64_t> &Record, 4754 StringRef Blob, 4755 ModuleFileExtensionMetadata &Metadata) { 4756 if (Record.size() < 4) return true; 4757 4758 Metadata.MajorVersion = Record[0]; 4759 Metadata.MinorVersion = Record[1]; 4760 4761 unsigned BlockNameLen = Record[2]; 4762 unsigned UserInfoLen = Record[3]; 4763 4764 if (BlockNameLen + UserInfoLen > Blob.size()) return true; 4765 4766 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen); 4767 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen, 4768 Blob.data() + BlockNameLen + UserInfoLen); 4769 return false; 4770 } 4771 4772 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) { 4773 BitstreamCursor &Stream = F.Stream; 4774 4775 RecordData Record; 4776 while (true) { 4777 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4778 if (!MaybeEntry) { 4779 Error(MaybeEntry.takeError()); 4780 return Failure; 4781 } 4782 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4783 4784 switch (Entry.Kind) { 4785 case llvm::BitstreamEntry::SubBlock: 4786 if (llvm::Error Err = Stream.SkipBlock()) { 4787 Error(std::move(Err)); 4788 return Failure; 4789 } 4790 continue; 4791 4792 case llvm::BitstreamEntry::EndBlock: 4793 return Success; 4794 4795 case llvm::BitstreamEntry::Error: 4796 return HadErrors; 4797 4798 case llvm::BitstreamEntry::Record: 4799 break; 4800 } 4801 4802 Record.clear(); 4803 StringRef Blob; 4804 Expected<unsigned> MaybeRecCode = 4805 Stream.readRecord(Entry.ID, Record, &Blob); 4806 if (!MaybeRecCode) { 4807 Error(MaybeRecCode.takeError()); 4808 return Failure; 4809 } 4810 switch (MaybeRecCode.get()) { 4811 case EXTENSION_METADATA: { 4812 ModuleFileExtensionMetadata Metadata; 4813 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) { 4814 Error("malformed EXTENSION_METADATA in AST file"); 4815 return Failure; 4816 } 4817 4818 // Find a module file extension with this block name. 4819 auto Known = ModuleFileExtensions.find(Metadata.BlockName); 4820 if (Known == ModuleFileExtensions.end()) break; 4821 4822 // Form a reader. 4823 if (auto Reader = Known->second->createExtensionReader(Metadata, *this, 4824 F, Stream)) { 4825 F.ExtensionReaders.push_back(std::move(Reader)); 4826 } 4827 4828 break; 4829 } 4830 } 4831 } 4832 4833 return Success; 4834 } 4835 4836 void ASTReader::InitializeContext() { 4837 assert(ContextObj && "no context to initialize"); 4838 ASTContext &Context = *ContextObj; 4839 4840 // If there's a listener, notify them that we "read" the translation unit. 4841 if (DeserializationListener) 4842 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, 4843 Context.getTranslationUnitDecl()); 4844 4845 // FIXME: Find a better way to deal with collisions between these 4846 // built-in types. Right now, we just ignore the problem. 4847 4848 // Load the special types. 4849 if (SpecialTypes.size() >= NumSpecialTypeIDs) { 4850 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) { 4851 if (!Context.CFConstantStringTypeDecl) 4852 Context.setCFConstantStringType(GetType(String)); 4853 } 4854 4855 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) { 4856 QualType FileType = GetType(File); 4857 if (FileType.isNull()) { 4858 Error("FILE type is NULL"); 4859 return; 4860 } 4861 4862 if (!Context.FILEDecl) { 4863 if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) 4864 Context.setFILEDecl(Typedef->getDecl()); 4865 else { 4866 const TagType *Tag = FileType->getAs<TagType>(); 4867 if (!Tag) { 4868 Error("Invalid FILE type in AST file"); 4869 return; 4870 } 4871 Context.setFILEDecl(Tag->getDecl()); 4872 } 4873 } 4874 } 4875 4876 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) { 4877 QualType Jmp_bufType = GetType(Jmp_buf); 4878 if (Jmp_bufType.isNull()) { 4879 Error("jmp_buf type is NULL"); 4880 return; 4881 } 4882 4883 if (!Context.jmp_bufDecl) { 4884 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) 4885 Context.setjmp_bufDecl(Typedef->getDecl()); 4886 else { 4887 const TagType *Tag = Jmp_bufType->getAs<TagType>(); 4888 if (!Tag) { 4889 Error("Invalid jmp_buf type in AST file"); 4890 return; 4891 } 4892 Context.setjmp_bufDecl(Tag->getDecl()); 4893 } 4894 } 4895 } 4896 4897 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) { 4898 QualType Sigjmp_bufType = GetType(Sigjmp_buf); 4899 if (Sigjmp_bufType.isNull()) { 4900 Error("sigjmp_buf type is NULL"); 4901 return; 4902 } 4903 4904 if (!Context.sigjmp_bufDecl) { 4905 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) 4906 Context.setsigjmp_bufDecl(Typedef->getDecl()); 4907 else { 4908 const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); 4909 assert(Tag && "Invalid sigjmp_buf type in AST file"); 4910 Context.setsigjmp_bufDecl(Tag->getDecl()); 4911 } 4912 } 4913 } 4914 4915 if (unsigned ObjCIdRedef 4916 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) { 4917 if (Context.ObjCIdRedefinitionType.isNull()) 4918 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef); 4919 } 4920 4921 if (unsigned ObjCClassRedef 4922 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) { 4923 if (Context.ObjCClassRedefinitionType.isNull()) 4924 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef); 4925 } 4926 4927 if (unsigned ObjCSelRedef 4928 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) { 4929 if (Context.ObjCSelRedefinitionType.isNull()) 4930 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef); 4931 } 4932 4933 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) { 4934 QualType Ucontext_tType = GetType(Ucontext_t); 4935 if (Ucontext_tType.isNull()) { 4936 Error("ucontext_t type is NULL"); 4937 return; 4938 } 4939 4940 if (!Context.ucontext_tDecl) { 4941 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>()) 4942 Context.setucontext_tDecl(Typedef->getDecl()); 4943 else { 4944 const TagType *Tag = Ucontext_tType->getAs<TagType>(); 4945 assert(Tag && "Invalid ucontext_t type in AST file"); 4946 Context.setucontext_tDecl(Tag->getDecl()); 4947 } 4948 } 4949 } 4950 } 4951 4952 ReadPragmaDiagnosticMappings(Context.getDiagnostics()); 4953 4954 // If there were any CUDA special declarations, deserialize them. 4955 if (!CUDASpecialDeclRefs.empty()) { 4956 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!"); 4957 Context.setcudaConfigureCallDecl( 4958 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0]))); 4959 } 4960 4961 // Re-export any modules that were imported by a non-module AST file. 4962 // FIXME: This does not make macro-only imports visible again. 4963 for (auto &Import : ImportedModules) { 4964 if (Module *Imported = getSubmodule(Import.ID)) { 4965 makeModuleVisible(Imported, Module::AllVisible, 4966 /*ImportLoc=*/Import.ImportLoc); 4967 if (Import.ImportLoc.isValid()) 4968 PP.makeModuleVisible(Imported, Import.ImportLoc); 4969 // This updates visibility for Preprocessor only. For Sema, which can be 4970 // nullptr here, we do the same later, in UpdateSema(). 4971 } 4972 } 4973 } 4974 4975 void ASTReader::finalizeForWriting() { 4976 // Nothing to do for now. 4977 } 4978 4979 /// Reads and return the signature record from \p PCH's control block, or 4980 /// else returns 0. 4981 static ASTFileSignature readASTFileSignature(StringRef PCH) { 4982 BitstreamCursor Stream(PCH); 4983 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4984 // FIXME this drops the error on the floor. 4985 consumeError(std::move(Err)); 4986 return ASTFileSignature(); 4987 } 4988 4989 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 4990 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 4991 return ASTFileSignature(); 4992 4993 // Scan for SIGNATURE inside the diagnostic options block. 4994 ASTReader::RecordData Record; 4995 while (true) { 4996 Expected<llvm::BitstreamEntry> MaybeEntry = 4997 Stream.advanceSkippingSubblocks(); 4998 if (!MaybeEntry) { 4999 // FIXME this drops the error on the floor. 5000 consumeError(MaybeEntry.takeError()); 5001 return ASTFileSignature(); 5002 } 5003 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5004 5005 if (Entry.Kind != llvm::BitstreamEntry::Record) 5006 return ASTFileSignature(); 5007 5008 Record.clear(); 5009 StringRef Blob; 5010 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 5011 if (!MaybeRecord) { 5012 // FIXME this drops the error on the floor. 5013 consumeError(MaybeRecord.takeError()); 5014 return ASTFileSignature(); 5015 } 5016 if (SIGNATURE == MaybeRecord.get()) 5017 return ASTFileSignature::create(Record.begin(), 5018 Record.begin() + ASTFileSignature::size); 5019 } 5020 } 5021 5022 /// Retrieve the name of the original source file name 5023 /// directly from the AST file, without actually loading the AST 5024 /// file. 5025 std::string ASTReader::getOriginalSourceFile( 5026 const std::string &ASTFileName, FileManager &FileMgr, 5027 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) { 5028 // Open the AST file. 5029 auto Buffer = FileMgr.getBufferForFile(ASTFileName); 5030 if (!Buffer) { 5031 Diags.Report(diag::err_fe_unable_to_read_pch_file) 5032 << ASTFileName << Buffer.getError().message(); 5033 return std::string(); 5034 } 5035 5036 // Initialize the stream 5037 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer)); 5038 5039 // Sniff for the signature. 5040 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5041 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err); 5042 return std::string(); 5043 } 5044 5045 // Scan for the CONTROL_BLOCK_ID block. 5046 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) { 5047 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 5048 return std::string(); 5049 } 5050 5051 // Scan for ORIGINAL_FILE inside the control block. 5052 RecordData Record; 5053 while (true) { 5054 Expected<llvm::BitstreamEntry> MaybeEntry = 5055 Stream.advanceSkippingSubblocks(); 5056 if (!MaybeEntry) { 5057 // FIXME this drops errors on the floor. 5058 consumeError(MaybeEntry.takeError()); 5059 return std::string(); 5060 } 5061 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5062 5063 if (Entry.Kind == llvm::BitstreamEntry::EndBlock) 5064 return std::string(); 5065 5066 if (Entry.Kind != llvm::BitstreamEntry::Record) { 5067 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 5068 return std::string(); 5069 } 5070 5071 Record.clear(); 5072 StringRef Blob; 5073 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 5074 if (!MaybeRecord) { 5075 // FIXME this drops the errors on the floor. 5076 consumeError(MaybeRecord.takeError()); 5077 return std::string(); 5078 } 5079 if (ORIGINAL_FILE == MaybeRecord.get()) 5080 return Blob.str(); 5081 } 5082 } 5083 5084 namespace { 5085 5086 class SimplePCHValidator : public ASTReaderListener { 5087 const LangOptions &ExistingLangOpts; 5088 const TargetOptions &ExistingTargetOpts; 5089 const PreprocessorOptions &ExistingPPOpts; 5090 std::string ExistingModuleCachePath; 5091 FileManager &FileMgr; 5092 5093 public: 5094 SimplePCHValidator(const LangOptions &ExistingLangOpts, 5095 const TargetOptions &ExistingTargetOpts, 5096 const PreprocessorOptions &ExistingPPOpts, 5097 StringRef ExistingModuleCachePath, FileManager &FileMgr) 5098 : ExistingLangOpts(ExistingLangOpts), 5099 ExistingTargetOpts(ExistingTargetOpts), 5100 ExistingPPOpts(ExistingPPOpts), 5101 ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {} 5102 5103 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain, 5104 bool AllowCompatibleDifferences) override { 5105 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr, 5106 AllowCompatibleDifferences); 5107 } 5108 5109 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain, 5110 bool AllowCompatibleDifferences) override { 5111 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr, 5112 AllowCompatibleDifferences); 5113 } 5114 5115 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 5116 StringRef SpecificModuleCachePath, 5117 bool Complain) override { 5118 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5119 ExistingModuleCachePath, 5120 nullptr, ExistingLangOpts); 5121 } 5122 5123 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 5124 bool Complain, 5125 std::string &SuggestedPredefines) override { 5126 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr, 5127 SuggestedPredefines, ExistingLangOpts); 5128 } 5129 }; 5130 5131 } // namespace 5132 5133 bool ASTReader::readASTFileControlBlock( 5134 StringRef Filename, FileManager &FileMgr, 5135 const PCHContainerReader &PCHContainerRdr, 5136 bool FindModuleFileExtensions, 5137 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) { 5138 // Open the AST file. 5139 // FIXME: This allows use of the VFS; we do not allow use of the 5140 // VFS when actually loading a module. 5141 auto Buffer = FileMgr.getBufferForFile(Filename); 5142 if (!Buffer) { 5143 return true; 5144 } 5145 5146 // Initialize the stream 5147 StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer); 5148 BitstreamCursor Stream(Bytes); 5149 5150 // Sniff for the signature. 5151 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5152 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 5153 return true; 5154 } 5155 5156 // Scan for the CONTROL_BLOCK_ID block. 5157 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) 5158 return true; 5159 5160 bool NeedsInputFiles = Listener.needsInputFileVisitation(); 5161 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation(); 5162 bool NeedsImports = Listener.needsImportVisitation(); 5163 BitstreamCursor InputFilesCursor; 5164 5165 RecordData Record; 5166 std::string ModuleDir; 5167 bool DoneWithControlBlock = false; 5168 while (!DoneWithControlBlock) { 5169 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5170 if (!MaybeEntry) { 5171 // FIXME this drops the error on the floor. 5172 consumeError(MaybeEntry.takeError()); 5173 return true; 5174 } 5175 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5176 5177 switch (Entry.Kind) { 5178 case llvm::BitstreamEntry::SubBlock: { 5179 switch (Entry.ID) { 5180 case OPTIONS_BLOCK_ID: { 5181 std::string IgnoredSuggestedPredefines; 5182 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate, 5183 /*AllowCompatibleConfigurationMismatch*/ false, 5184 Listener, IgnoredSuggestedPredefines) != Success) 5185 return true; 5186 break; 5187 } 5188 5189 case INPUT_FILES_BLOCK_ID: 5190 InputFilesCursor = Stream; 5191 if (llvm::Error Err = Stream.SkipBlock()) { 5192 // FIXME this drops the error on the floor. 5193 consumeError(std::move(Err)); 5194 return true; 5195 } 5196 if (NeedsInputFiles && 5197 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID)) 5198 return true; 5199 break; 5200 5201 default: 5202 if (llvm::Error Err = Stream.SkipBlock()) { 5203 // FIXME this drops the error on the floor. 5204 consumeError(std::move(Err)); 5205 return true; 5206 } 5207 break; 5208 } 5209 5210 continue; 5211 } 5212 5213 case llvm::BitstreamEntry::EndBlock: 5214 DoneWithControlBlock = true; 5215 break; 5216 5217 case llvm::BitstreamEntry::Error: 5218 return true; 5219 5220 case llvm::BitstreamEntry::Record: 5221 break; 5222 } 5223 5224 if (DoneWithControlBlock) break; 5225 5226 Record.clear(); 5227 StringRef Blob; 5228 Expected<unsigned> MaybeRecCode = 5229 Stream.readRecord(Entry.ID, Record, &Blob); 5230 if (!MaybeRecCode) { 5231 // FIXME this drops the error. 5232 return Failure; 5233 } 5234 switch ((ControlRecordTypes)MaybeRecCode.get()) { 5235 case METADATA: 5236 if (Record[0] != VERSION_MAJOR) 5237 return true; 5238 if (Listener.ReadFullVersionInformation(Blob)) 5239 return true; 5240 break; 5241 case MODULE_NAME: 5242 Listener.ReadModuleName(Blob); 5243 break; 5244 case MODULE_DIRECTORY: 5245 ModuleDir = std::string(Blob); 5246 break; 5247 case MODULE_MAP_FILE: { 5248 unsigned Idx = 0; 5249 auto Path = ReadString(Record, Idx); 5250 ResolveImportedPath(Path, ModuleDir); 5251 Listener.ReadModuleMapFile(Path); 5252 break; 5253 } 5254 case INPUT_FILE_OFFSETS: { 5255 if (!NeedsInputFiles) 5256 break; 5257 5258 unsigned NumInputFiles = Record[0]; 5259 unsigned NumUserFiles = Record[1]; 5260 const llvm::support::unaligned_uint64_t *InputFileOffs = 5261 (const llvm::support::unaligned_uint64_t *)Blob.data(); 5262 for (unsigned I = 0; I != NumInputFiles; ++I) { 5263 // Go find this input file. 5264 bool isSystemFile = I >= NumUserFiles; 5265 5266 if (isSystemFile && !NeedsSystemInputFiles) 5267 break; // the rest are system input files 5268 5269 BitstreamCursor &Cursor = InputFilesCursor; 5270 SavedStreamPosition SavedPosition(Cursor); 5271 if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) { 5272 // FIXME this drops errors on the floor. 5273 consumeError(std::move(Err)); 5274 } 5275 5276 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 5277 if (!MaybeCode) { 5278 // FIXME this drops errors on the floor. 5279 consumeError(MaybeCode.takeError()); 5280 } 5281 unsigned Code = MaybeCode.get(); 5282 5283 RecordData Record; 5284 StringRef Blob; 5285 bool shouldContinue = false; 5286 Expected<unsigned> MaybeRecordType = 5287 Cursor.readRecord(Code, Record, &Blob); 5288 if (!MaybeRecordType) { 5289 // FIXME this drops errors on the floor. 5290 consumeError(MaybeRecordType.takeError()); 5291 } 5292 switch ((InputFileRecordTypes)MaybeRecordType.get()) { 5293 case INPUT_FILE_HASH: 5294 break; 5295 case INPUT_FILE: 5296 bool Overridden = static_cast<bool>(Record[3]); 5297 std::string Filename = std::string(Blob); 5298 ResolveImportedPath(Filename, ModuleDir); 5299 shouldContinue = Listener.visitInputFile( 5300 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false); 5301 break; 5302 } 5303 if (!shouldContinue) 5304 break; 5305 } 5306 break; 5307 } 5308 5309 case IMPORTS: { 5310 if (!NeedsImports) 5311 break; 5312 5313 unsigned Idx = 0, N = Record.size(); 5314 while (Idx < N) { 5315 // Read information about the AST file. 5316 Idx += 5317 1 + 1 + 1 + 1 + 5318 ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature 5319 std::string ModuleName = ReadString(Record, Idx); 5320 std::string Filename = ReadString(Record, Idx); 5321 ResolveImportedPath(Filename, ModuleDir); 5322 Listener.visitImport(ModuleName, Filename); 5323 } 5324 break; 5325 } 5326 5327 default: 5328 // No other validation to perform. 5329 break; 5330 } 5331 } 5332 5333 // Look for module file extension blocks, if requested. 5334 if (FindModuleFileExtensions) { 5335 BitstreamCursor SavedStream = Stream; 5336 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) { 5337 bool DoneWithExtensionBlock = false; 5338 while (!DoneWithExtensionBlock) { 5339 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5340 if (!MaybeEntry) { 5341 // FIXME this drops the error. 5342 return true; 5343 } 5344 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5345 5346 switch (Entry.Kind) { 5347 case llvm::BitstreamEntry::SubBlock: 5348 if (llvm::Error Err = Stream.SkipBlock()) { 5349 // FIXME this drops the error on the floor. 5350 consumeError(std::move(Err)); 5351 return true; 5352 } 5353 continue; 5354 5355 case llvm::BitstreamEntry::EndBlock: 5356 DoneWithExtensionBlock = true; 5357 continue; 5358 5359 case llvm::BitstreamEntry::Error: 5360 return true; 5361 5362 case llvm::BitstreamEntry::Record: 5363 break; 5364 } 5365 5366 Record.clear(); 5367 StringRef Blob; 5368 Expected<unsigned> MaybeRecCode = 5369 Stream.readRecord(Entry.ID, Record, &Blob); 5370 if (!MaybeRecCode) { 5371 // FIXME this drops the error. 5372 return true; 5373 } 5374 switch (MaybeRecCode.get()) { 5375 case EXTENSION_METADATA: { 5376 ModuleFileExtensionMetadata Metadata; 5377 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) 5378 return true; 5379 5380 Listener.readModuleFileExtension(Metadata); 5381 break; 5382 } 5383 } 5384 } 5385 } 5386 Stream = SavedStream; 5387 } 5388 5389 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 5390 if (readUnhashedControlBlockImpl( 5391 nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate, 5392 /*AllowCompatibleConfigurationMismatch*/ false, &Listener, 5393 ValidateDiagnosticOptions) != Success) 5394 return true; 5395 5396 return false; 5397 } 5398 5399 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr, 5400 const PCHContainerReader &PCHContainerRdr, 5401 const LangOptions &LangOpts, 5402 const TargetOptions &TargetOpts, 5403 const PreprocessorOptions &PPOpts, 5404 StringRef ExistingModuleCachePath) { 5405 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, 5406 ExistingModuleCachePath, FileMgr); 5407 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr, 5408 /*FindModuleFileExtensions=*/false, 5409 validator, 5410 /*ValidateDiagnosticOptions=*/true); 5411 } 5412 5413 ASTReader::ASTReadResult 5414 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) { 5415 // Enter the submodule block. 5416 if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) { 5417 Error(std::move(Err)); 5418 return Failure; 5419 } 5420 5421 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap(); 5422 bool First = true; 5423 Module *CurrentModule = nullptr; 5424 RecordData Record; 5425 while (true) { 5426 Expected<llvm::BitstreamEntry> MaybeEntry = 5427 F.Stream.advanceSkippingSubblocks(); 5428 if (!MaybeEntry) { 5429 Error(MaybeEntry.takeError()); 5430 return Failure; 5431 } 5432 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5433 5434 switch (Entry.Kind) { 5435 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 5436 case llvm::BitstreamEntry::Error: 5437 Error("malformed block record in AST file"); 5438 return Failure; 5439 case llvm::BitstreamEntry::EndBlock: 5440 return Success; 5441 case llvm::BitstreamEntry::Record: 5442 // The interesting case. 5443 break; 5444 } 5445 5446 // Read a record. 5447 StringRef Blob; 5448 Record.clear(); 5449 Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob); 5450 if (!MaybeKind) { 5451 Error(MaybeKind.takeError()); 5452 return Failure; 5453 } 5454 unsigned Kind = MaybeKind.get(); 5455 5456 if ((Kind == SUBMODULE_METADATA) != First) { 5457 Error("submodule metadata record should be at beginning of block"); 5458 return Failure; 5459 } 5460 First = false; 5461 5462 // Submodule information is only valid if we have a current module. 5463 // FIXME: Should we error on these cases? 5464 if (!CurrentModule && Kind != SUBMODULE_METADATA && 5465 Kind != SUBMODULE_DEFINITION) 5466 continue; 5467 5468 switch (Kind) { 5469 default: // Default behavior: ignore. 5470 break; 5471 5472 case SUBMODULE_DEFINITION: { 5473 if (Record.size() < 12) { 5474 Error("malformed module definition"); 5475 return Failure; 5476 } 5477 5478 StringRef Name = Blob; 5479 unsigned Idx = 0; 5480 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]); 5481 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]); 5482 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++]; 5483 bool IsFramework = Record[Idx++]; 5484 bool IsExplicit = Record[Idx++]; 5485 bool IsSystem = Record[Idx++]; 5486 bool IsExternC = Record[Idx++]; 5487 bool InferSubmodules = Record[Idx++]; 5488 bool InferExplicitSubmodules = Record[Idx++]; 5489 bool InferExportWildcard = Record[Idx++]; 5490 bool ConfigMacrosExhaustive = Record[Idx++]; 5491 bool ModuleMapIsPrivate = Record[Idx++]; 5492 5493 Module *ParentModule = nullptr; 5494 if (Parent) 5495 ParentModule = getSubmodule(Parent); 5496 5497 // Retrieve this (sub)module from the module map, creating it if 5498 // necessary. 5499 CurrentModule = 5500 ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit) 5501 .first; 5502 5503 // FIXME: set the definition loc for CurrentModule, or call 5504 // ModMap.setInferredModuleAllowedBy() 5505 5506 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS; 5507 if (GlobalIndex >= SubmodulesLoaded.size() || 5508 SubmodulesLoaded[GlobalIndex]) { 5509 Error("too many submodules"); 5510 return Failure; 5511 } 5512 5513 if (!ParentModule) { 5514 if (const FileEntry *CurFile = CurrentModule->getASTFile()) { 5515 // Don't emit module relocation error if we have -fno-validate-pch 5516 if (!PP.getPreprocessorOpts().DisablePCHValidation && 5517 CurFile != F.File) { 5518 Error(diag::err_module_file_conflict, 5519 CurrentModule->getTopLevelModuleName(), CurFile->getName(), 5520 F.File->getName()); 5521 return Failure; 5522 } 5523 } 5524 5525 F.DidReadTopLevelSubmodule = true; 5526 CurrentModule->setASTFile(F.File); 5527 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath; 5528 } 5529 5530 CurrentModule->Kind = Kind; 5531 CurrentModule->Signature = F.Signature; 5532 CurrentModule->IsFromModuleFile = true; 5533 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem; 5534 CurrentModule->IsExternC = IsExternC; 5535 CurrentModule->InferSubmodules = InferSubmodules; 5536 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules; 5537 CurrentModule->InferExportWildcard = InferExportWildcard; 5538 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive; 5539 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate; 5540 if (DeserializationListener) 5541 DeserializationListener->ModuleRead(GlobalID, CurrentModule); 5542 5543 SubmodulesLoaded[GlobalIndex] = CurrentModule; 5544 5545 // Clear out data that will be replaced by what is in the module file. 5546 CurrentModule->LinkLibraries.clear(); 5547 CurrentModule->ConfigMacros.clear(); 5548 CurrentModule->UnresolvedConflicts.clear(); 5549 CurrentModule->Conflicts.clear(); 5550 5551 // The module is available unless it's missing a requirement; relevant 5552 // requirements will be (re-)added by SUBMODULE_REQUIRES records. 5553 // Missing headers that were present when the module was built do not 5554 // make it unavailable -- if we got this far, this must be an explicitly 5555 // imported module file. 5556 CurrentModule->Requirements.clear(); 5557 CurrentModule->MissingHeaders.clear(); 5558 CurrentModule->IsUnimportable = 5559 ParentModule && ParentModule->IsUnimportable; 5560 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable; 5561 break; 5562 } 5563 5564 case SUBMODULE_UMBRELLA_HEADER: { 5565 std::string Filename = std::string(Blob); 5566 ResolveImportedPath(F, Filename); 5567 if (auto Umbrella = PP.getFileManager().getFile(Filename)) { 5568 if (!CurrentModule->getUmbrellaHeader()) 5569 ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob); 5570 else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) { 5571 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 5572 Error("mismatched umbrella headers in submodule"); 5573 return OutOfDate; 5574 } 5575 } 5576 break; 5577 } 5578 5579 case SUBMODULE_HEADER: 5580 case SUBMODULE_EXCLUDED_HEADER: 5581 case SUBMODULE_PRIVATE_HEADER: 5582 // We lazily associate headers with their modules via the HeaderInfo table. 5583 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead 5584 // of complete filenames or remove it entirely. 5585 break; 5586 5587 case SUBMODULE_TEXTUAL_HEADER: 5588 case SUBMODULE_PRIVATE_TEXTUAL_HEADER: 5589 // FIXME: Textual headers are not marked in the HeaderInfo table. Load 5590 // them here. 5591 break; 5592 5593 case SUBMODULE_TOPHEADER: 5594 CurrentModule->addTopHeaderFilename(Blob); 5595 break; 5596 5597 case SUBMODULE_UMBRELLA_DIR: { 5598 std::string Dirname = std::string(Blob); 5599 ResolveImportedPath(F, Dirname); 5600 if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) { 5601 if (!CurrentModule->getUmbrellaDir()) 5602 ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob); 5603 else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) { 5604 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 5605 Error("mismatched umbrella directories in submodule"); 5606 return OutOfDate; 5607 } 5608 } 5609 break; 5610 } 5611 5612 case SUBMODULE_METADATA: { 5613 F.BaseSubmoduleID = getTotalNumSubmodules(); 5614 F.LocalNumSubmodules = Record[0]; 5615 unsigned LocalBaseSubmoduleID = Record[1]; 5616 if (F.LocalNumSubmodules > 0) { 5617 // Introduce the global -> local mapping for submodules within this 5618 // module. 5619 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F)); 5620 5621 // Introduce the local -> global mapping for submodules within this 5622 // module. 5623 F.SubmoduleRemap.insertOrReplace( 5624 std::make_pair(LocalBaseSubmoduleID, 5625 F.BaseSubmoduleID - LocalBaseSubmoduleID)); 5626 5627 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules); 5628 } 5629 break; 5630 } 5631 5632 case SUBMODULE_IMPORTS: 5633 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) { 5634 UnresolvedModuleRef Unresolved; 5635 Unresolved.File = &F; 5636 Unresolved.Mod = CurrentModule; 5637 Unresolved.ID = Record[Idx]; 5638 Unresolved.Kind = UnresolvedModuleRef::Import; 5639 Unresolved.IsWildcard = false; 5640 UnresolvedModuleRefs.push_back(Unresolved); 5641 } 5642 break; 5643 5644 case SUBMODULE_EXPORTS: 5645 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) { 5646 UnresolvedModuleRef Unresolved; 5647 Unresolved.File = &F; 5648 Unresolved.Mod = CurrentModule; 5649 Unresolved.ID = Record[Idx]; 5650 Unresolved.Kind = UnresolvedModuleRef::Export; 5651 Unresolved.IsWildcard = Record[Idx + 1]; 5652 UnresolvedModuleRefs.push_back(Unresolved); 5653 } 5654 5655 // Once we've loaded the set of exports, there's no reason to keep 5656 // the parsed, unresolved exports around. 5657 CurrentModule->UnresolvedExports.clear(); 5658 break; 5659 5660 case SUBMODULE_REQUIRES: 5661 CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(), 5662 PP.getTargetInfo()); 5663 break; 5664 5665 case SUBMODULE_LINK_LIBRARY: 5666 ModMap.resolveLinkAsDependencies(CurrentModule); 5667 CurrentModule->LinkLibraries.push_back( 5668 Module::LinkLibrary(std::string(Blob), Record[0])); 5669 break; 5670 5671 case SUBMODULE_CONFIG_MACRO: 5672 CurrentModule->ConfigMacros.push_back(Blob.str()); 5673 break; 5674 5675 case SUBMODULE_CONFLICT: { 5676 UnresolvedModuleRef Unresolved; 5677 Unresolved.File = &F; 5678 Unresolved.Mod = CurrentModule; 5679 Unresolved.ID = Record[0]; 5680 Unresolved.Kind = UnresolvedModuleRef::Conflict; 5681 Unresolved.IsWildcard = false; 5682 Unresolved.String = Blob; 5683 UnresolvedModuleRefs.push_back(Unresolved); 5684 break; 5685 } 5686 5687 case SUBMODULE_INITIALIZERS: { 5688 if (!ContextObj) 5689 break; 5690 SmallVector<uint32_t, 16> Inits; 5691 for (auto &ID : Record) 5692 Inits.push_back(getGlobalDeclID(F, ID)); 5693 ContextObj->addLazyModuleInitializers(CurrentModule, Inits); 5694 break; 5695 } 5696 5697 case SUBMODULE_EXPORT_AS: 5698 CurrentModule->ExportAsModule = Blob.str(); 5699 ModMap.addLinkAsDependency(CurrentModule); 5700 break; 5701 } 5702 } 5703 } 5704 5705 /// Parse the record that corresponds to a LangOptions data 5706 /// structure. 5707 /// 5708 /// This routine parses the language options from the AST file and then gives 5709 /// them to the AST listener if one is set. 5710 /// 5711 /// \returns true if the listener deems the file unacceptable, false otherwise. 5712 bool ASTReader::ParseLanguageOptions(const RecordData &Record, 5713 bool Complain, 5714 ASTReaderListener &Listener, 5715 bool AllowCompatibleDifferences) { 5716 LangOptions LangOpts; 5717 unsigned Idx = 0; 5718 #define LANGOPT(Name, Bits, Default, Description) \ 5719 LangOpts.Name = Record[Idx++]; 5720 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 5721 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++])); 5722 #include "clang/Basic/LangOptions.def" 5723 #define SANITIZER(NAME, ID) \ 5724 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]); 5725 #include "clang/Basic/Sanitizers.def" 5726 5727 for (unsigned N = Record[Idx++]; N; --N) 5728 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx)); 5729 5730 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++]; 5731 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx); 5732 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion); 5733 5734 LangOpts.CurrentModule = ReadString(Record, Idx); 5735 5736 // Comment options. 5737 for (unsigned N = Record[Idx++]; N; --N) { 5738 LangOpts.CommentOpts.BlockCommandNames.push_back( 5739 ReadString(Record, Idx)); 5740 } 5741 LangOpts.CommentOpts.ParseAllComments = Record[Idx++]; 5742 5743 // OpenMP offloading options. 5744 for (unsigned N = Record[Idx++]; N; --N) { 5745 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx))); 5746 } 5747 5748 LangOpts.OMPHostIRFile = ReadString(Record, Idx); 5749 5750 return Listener.ReadLanguageOptions(LangOpts, Complain, 5751 AllowCompatibleDifferences); 5752 } 5753 5754 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain, 5755 ASTReaderListener &Listener, 5756 bool AllowCompatibleDifferences) { 5757 unsigned Idx = 0; 5758 TargetOptions TargetOpts; 5759 TargetOpts.Triple = ReadString(Record, Idx); 5760 TargetOpts.CPU = ReadString(Record, Idx); 5761 TargetOpts.TuneCPU = ReadString(Record, Idx); 5762 TargetOpts.ABI = ReadString(Record, Idx); 5763 for (unsigned N = Record[Idx++]; N; --N) { 5764 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx)); 5765 } 5766 for (unsigned N = Record[Idx++]; N; --N) { 5767 TargetOpts.Features.push_back(ReadString(Record, Idx)); 5768 } 5769 5770 return Listener.ReadTargetOptions(TargetOpts, Complain, 5771 AllowCompatibleDifferences); 5772 } 5773 5774 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain, 5775 ASTReaderListener &Listener) { 5776 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions); 5777 unsigned Idx = 0; 5778 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++]; 5779 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \ 5780 DiagOpts->set##Name(static_cast<Type>(Record[Idx++])); 5781 #include "clang/Basic/DiagnosticOptions.def" 5782 5783 for (unsigned N = Record[Idx++]; N; --N) 5784 DiagOpts->Warnings.push_back(ReadString(Record, Idx)); 5785 for (unsigned N = Record[Idx++]; N; --N) 5786 DiagOpts->Remarks.push_back(ReadString(Record, Idx)); 5787 5788 return Listener.ReadDiagnosticOptions(DiagOpts, Complain); 5789 } 5790 5791 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain, 5792 ASTReaderListener &Listener) { 5793 FileSystemOptions FSOpts; 5794 unsigned Idx = 0; 5795 FSOpts.WorkingDir = ReadString(Record, Idx); 5796 return Listener.ReadFileSystemOptions(FSOpts, Complain); 5797 } 5798 5799 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record, 5800 bool Complain, 5801 ASTReaderListener &Listener) { 5802 HeaderSearchOptions HSOpts; 5803 unsigned Idx = 0; 5804 HSOpts.Sysroot = ReadString(Record, Idx); 5805 5806 // Include entries. 5807 for (unsigned N = Record[Idx++]; N; --N) { 5808 std::string Path = ReadString(Record, Idx); 5809 frontend::IncludeDirGroup Group 5810 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]); 5811 bool IsFramework = Record[Idx++]; 5812 bool IgnoreSysRoot = Record[Idx++]; 5813 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework, 5814 IgnoreSysRoot); 5815 } 5816 5817 // System header prefixes. 5818 for (unsigned N = Record[Idx++]; N; --N) { 5819 std::string Prefix = ReadString(Record, Idx); 5820 bool IsSystemHeader = Record[Idx++]; 5821 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader); 5822 } 5823 5824 HSOpts.ResourceDir = ReadString(Record, Idx); 5825 HSOpts.ModuleCachePath = ReadString(Record, Idx); 5826 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx); 5827 HSOpts.DisableModuleHash = Record[Idx++]; 5828 HSOpts.ImplicitModuleMaps = Record[Idx++]; 5829 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++]; 5830 HSOpts.EnablePrebuiltImplicitModules = Record[Idx++]; 5831 HSOpts.UseBuiltinIncludes = Record[Idx++]; 5832 HSOpts.UseStandardSystemIncludes = Record[Idx++]; 5833 HSOpts.UseStandardCXXIncludes = Record[Idx++]; 5834 HSOpts.UseLibcxx = Record[Idx++]; 5835 std::string SpecificModuleCachePath = ReadString(Record, Idx); 5836 5837 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5838 Complain); 5839 } 5840 5841 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record, 5842 bool Complain, 5843 ASTReaderListener &Listener, 5844 std::string &SuggestedPredefines) { 5845 PreprocessorOptions PPOpts; 5846 unsigned Idx = 0; 5847 5848 // Macro definitions/undefs 5849 for (unsigned N = Record[Idx++]; N; --N) { 5850 std::string Macro = ReadString(Record, Idx); 5851 bool IsUndef = Record[Idx++]; 5852 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef)); 5853 } 5854 5855 // Includes 5856 for (unsigned N = Record[Idx++]; N; --N) { 5857 PPOpts.Includes.push_back(ReadString(Record, Idx)); 5858 } 5859 5860 // Macro Includes 5861 for (unsigned N = Record[Idx++]; N; --N) { 5862 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx)); 5863 } 5864 5865 PPOpts.UsePredefines = Record[Idx++]; 5866 PPOpts.DetailedRecord = Record[Idx++]; 5867 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx); 5868 PPOpts.ObjCXXARCStandardLibrary = 5869 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]); 5870 SuggestedPredefines.clear(); 5871 return Listener.ReadPreprocessorOptions(PPOpts, Complain, 5872 SuggestedPredefines); 5873 } 5874 5875 std::pair<ModuleFile *, unsigned> 5876 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) { 5877 GlobalPreprocessedEntityMapType::iterator 5878 I = GlobalPreprocessedEntityMap.find(GlobalIndex); 5879 assert(I != GlobalPreprocessedEntityMap.end() && 5880 "Corrupted global preprocessed entity map"); 5881 ModuleFile *M = I->second; 5882 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID; 5883 return std::make_pair(M, LocalIndex); 5884 } 5885 5886 llvm::iterator_range<PreprocessingRecord::iterator> 5887 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const { 5888 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord()) 5889 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID, 5890 Mod.NumPreprocessedEntities); 5891 5892 return llvm::make_range(PreprocessingRecord::iterator(), 5893 PreprocessingRecord::iterator()); 5894 } 5895 5896 llvm::iterator_range<ASTReader::ModuleDeclIterator> 5897 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) { 5898 return llvm::make_range( 5899 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls), 5900 ModuleDeclIterator(this, &Mod, 5901 Mod.FileSortedDecls + Mod.NumFileSortedDecls)); 5902 } 5903 5904 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) { 5905 auto I = GlobalSkippedRangeMap.find(GlobalIndex); 5906 assert(I != GlobalSkippedRangeMap.end() && 5907 "Corrupted global skipped range map"); 5908 ModuleFile *M = I->second; 5909 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID; 5910 assert(LocalIndex < M->NumPreprocessedSkippedRanges); 5911 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex]; 5912 SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()), 5913 TranslateSourceLocation(*M, RawRange.getEnd())); 5914 assert(Range.isValid()); 5915 return Range; 5916 } 5917 5918 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) { 5919 PreprocessedEntityID PPID = Index+1; 5920 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 5921 ModuleFile &M = *PPInfo.first; 5922 unsigned LocalIndex = PPInfo.second; 5923 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 5924 5925 if (!PP.getPreprocessingRecord()) { 5926 Error("no preprocessing record"); 5927 return nullptr; 5928 } 5929 5930 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor); 5931 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit( 5932 M.MacroOffsetsBase + PPOffs.BitOffset)) { 5933 Error(std::move(Err)); 5934 return nullptr; 5935 } 5936 5937 Expected<llvm::BitstreamEntry> MaybeEntry = 5938 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 5939 if (!MaybeEntry) { 5940 Error(MaybeEntry.takeError()); 5941 return nullptr; 5942 } 5943 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5944 5945 if (Entry.Kind != llvm::BitstreamEntry::Record) 5946 return nullptr; 5947 5948 // Read the record. 5949 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()), 5950 TranslateSourceLocation(M, PPOffs.getEnd())); 5951 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 5952 StringRef Blob; 5953 RecordData Record; 5954 Expected<unsigned> MaybeRecType = 5955 M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob); 5956 if (!MaybeRecType) { 5957 Error(MaybeRecType.takeError()); 5958 return nullptr; 5959 } 5960 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) { 5961 case PPD_MACRO_EXPANSION: { 5962 bool isBuiltin = Record[0]; 5963 IdentifierInfo *Name = nullptr; 5964 MacroDefinitionRecord *Def = nullptr; 5965 if (isBuiltin) 5966 Name = getLocalIdentifier(M, Record[1]); 5967 else { 5968 PreprocessedEntityID GlobalID = 5969 getGlobalPreprocessedEntityID(M, Record[1]); 5970 Def = cast<MacroDefinitionRecord>( 5971 PPRec.getLoadedPreprocessedEntity(GlobalID - 1)); 5972 } 5973 5974 MacroExpansion *ME; 5975 if (isBuiltin) 5976 ME = new (PPRec) MacroExpansion(Name, Range); 5977 else 5978 ME = new (PPRec) MacroExpansion(Def, Range); 5979 5980 return ME; 5981 } 5982 5983 case PPD_MACRO_DEFINITION: { 5984 // Decode the identifier info and then check again; if the macro is 5985 // still defined and associated with the identifier, 5986 IdentifierInfo *II = getLocalIdentifier(M, Record[0]); 5987 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range); 5988 5989 if (DeserializationListener) 5990 DeserializationListener->MacroDefinitionRead(PPID, MD); 5991 5992 return MD; 5993 } 5994 5995 case PPD_INCLUSION_DIRECTIVE: { 5996 const char *FullFileNameStart = Blob.data() + Record[0]; 5997 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]); 5998 const FileEntry *File = nullptr; 5999 if (!FullFileName.empty()) 6000 if (auto FE = PP.getFileManager().getFile(FullFileName)) 6001 File = *FE; 6002 6003 // FIXME: Stable encoding 6004 InclusionDirective::InclusionKind Kind 6005 = static_cast<InclusionDirective::InclusionKind>(Record[2]); 6006 InclusionDirective *ID 6007 = new (PPRec) InclusionDirective(PPRec, Kind, 6008 StringRef(Blob.data(), Record[0]), 6009 Record[1], Record[3], 6010 File, 6011 Range); 6012 return ID; 6013 } 6014 } 6015 6016 llvm_unreachable("Invalid PreprocessorDetailRecordTypes"); 6017 } 6018 6019 /// Find the next module that contains entities and return the ID 6020 /// of the first entry. 6021 /// 6022 /// \param SLocMapI points at a chunk of a module that contains no 6023 /// preprocessed entities or the entities it contains are not the ones we are 6024 /// looking for. 6025 PreprocessedEntityID ASTReader::findNextPreprocessedEntity( 6026 GlobalSLocOffsetMapType::const_iterator SLocMapI) const { 6027 ++SLocMapI; 6028 for (GlobalSLocOffsetMapType::const_iterator 6029 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) { 6030 ModuleFile &M = *SLocMapI->second; 6031 if (M.NumPreprocessedEntities) 6032 return M.BasePreprocessedEntityID; 6033 } 6034 6035 return getTotalNumPreprocessedEntities(); 6036 } 6037 6038 namespace { 6039 6040 struct PPEntityComp { 6041 const ASTReader &Reader; 6042 ModuleFile &M; 6043 6044 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {} 6045 6046 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const { 6047 SourceLocation LHS = getLoc(L); 6048 SourceLocation RHS = getLoc(R); 6049 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6050 } 6051 6052 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const { 6053 SourceLocation LHS = getLoc(L); 6054 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6055 } 6056 6057 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const { 6058 SourceLocation RHS = getLoc(R); 6059 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6060 } 6061 6062 SourceLocation getLoc(const PPEntityOffset &PPE) const { 6063 return Reader.TranslateSourceLocation(M, PPE.getBegin()); 6064 } 6065 }; 6066 6067 } // namespace 6068 6069 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc, 6070 bool EndsAfter) const { 6071 if (SourceMgr.isLocalSourceLocation(Loc)) 6072 return getTotalNumPreprocessedEntities(); 6073 6074 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find( 6075 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1); 6076 assert(SLocMapI != GlobalSLocOffsetMap.end() && 6077 "Corrupted global sloc offset map"); 6078 6079 if (SLocMapI->second->NumPreprocessedEntities == 0) 6080 return findNextPreprocessedEntity(SLocMapI); 6081 6082 ModuleFile &M = *SLocMapI->second; 6083 6084 using pp_iterator = const PPEntityOffset *; 6085 6086 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 6087 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 6088 6089 size_t Count = M.NumPreprocessedEntities; 6090 size_t Half; 6091 pp_iterator First = pp_begin; 6092 pp_iterator PPI; 6093 6094 if (EndsAfter) { 6095 PPI = std::upper_bound(pp_begin, pp_end, Loc, 6096 PPEntityComp(*this, M)); 6097 } else { 6098 // Do a binary search manually instead of using std::lower_bound because 6099 // The end locations of entities may be unordered (when a macro expansion 6100 // is inside another macro argument), but for this case it is not important 6101 // whether we get the first macro expansion or its containing macro. 6102 while (Count > 0) { 6103 Half = Count / 2; 6104 PPI = First; 6105 std::advance(PPI, Half); 6106 if (SourceMgr.isBeforeInTranslationUnit( 6107 TranslateSourceLocation(M, PPI->getEnd()), Loc)) { 6108 First = PPI; 6109 ++First; 6110 Count = Count - Half - 1; 6111 } else 6112 Count = Half; 6113 } 6114 } 6115 6116 if (PPI == pp_end) 6117 return findNextPreprocessedEntity(SLocMapI); 6118 6119 return M.BasePreprocessedEntityID + (PPI - pp_begin); 6120 } 6121 6122 /// Returns a pair of [Begin, End) indices of preallocated 6123 /// preprocessed entities that \arg Range encompasses. 6124 std::pair<unsigned, unsigned> 6125 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) { 6126 if (Range.isInvalid()) 6127 return std::make_pair(0,0); 6128 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); 6129 6130 PreprocessedEntityID BeginID = 6131 findPreprocessedEntity(Range.getBegin(), false); 6132 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true); 6133 return std::make_pair(BeginID, EndID); 6134 } 6135 6136 /// Optionally returns true or false if the preallocated preprocessed 6137 /// entity with index \arg Index came from file \arg FID. 6138 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index, 6139 FileID FID) { 6140 if (FID.isInvalid()) 6141 return false; 6142 6143 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 6144 ModuleFile &M = *PPInfo.first; 6145 unsigned LocalIndex = PPInfo.second; 6146 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 6147 6148 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin()); 6149 if (Loc.isInvalid()) 6150 return false; 6151 6152 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID)) 6153 return true; 6154 else 6155 return false; 6156 } 6157 6158 namespace { 6159 6160 /// Visitor used to search for information about a header file. 6161 class HeaderFileInfoVisitor { 6162 const FileEntry *FE; 6163 Optional<HeaderFileInfo> HFI; 6164 6165 public: 6166 explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {} 6167 6168 bool operator()(ModuleFile &M) { 6169 HeaderFileInfoLookupTable *Table 6170 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable); 6171 if (!Table) 6172 return false; 6173 6174 // Look in the on-disk hash table for an entry for this file name. 6175 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE); 6176 if (Pos == Table->end()) 6177 return false; 6178 6179 HFI = *Pos; 6180 return true; 6181 } 6182 6183 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; } 6184 }; 6185 6186 } // namespace 6187 6188 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) { 6189 HeaderFileInfoVisitor Visitor(FE); 6190 ModuleMgr.visit(Visitor); 6191 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) 6192 return *HFI; 6193 6194 return HeaderFileInfo(); 6195 } 6196 6197 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) { 6198 using DiagState = DiagnosticsEngine::DiagState; 6199 SmallVector<DiagState *, 32> DiagStates; 6200 6201 for (ModuleFile &F : ModuleMgr) { 6202 unsigned Idx = 0; 6203 auto &Record = F.PragmaDiagMappings; 6204 if (Record.empty()) 6205 continue; 6206 6207 DiagStates.clear(); 6208 6209 auto ReadDiagState = 6210 [&](const DiagState &BasedOn, SourceLocation Loc, 6211 bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * { 6212 unsigned BackrefID = Record[Idx++]; 6213 if (BackrefID != 0) 6214 return DiagStates[BackrefID - 1]; 6215 6216 // A new DiagState was created here. 6217 Diag.DiagStates.push_back(BasedOn); 6218 DiagState *NewState = &Diag.DiagStates.back(); 6219 DiagStates.push_back(NewState); 6220 unsigned Size = Record[Idx++]; 6221 assert(Idx + Size * 2 <= Record.size() && 6222 "Invalid data, not enough diag/map pairs"); 6223 while (Size--) { 6224 unsigned DiagID = Record[Idx++]; 6225 DiagnosticMapping NewMapping = 6226 DiagnosticMapping::deserialize(Record[Idx++]); 6227 if (!NewMapping.isPragma() && !IncludeNonPragmaStates) 6228 continue; 6229 6230 DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID); 6231 6232 // If this mapping was specified as a warning but the severity was 6233 // upgraded due to diagnostic settings, simulate the current diagnostic 6234 // settings (and use a warning). 6235 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) { 6236 NewMapping.setSeverity(diag::Severity::Warning); 6237 NewMapping.setUpgradedFromWarning(false); 6238 } 6239 6240 Mapping = NewMapping; 6241 } 6242 return NewState; 6243 }; 6244 6245 // Read the first state. 6246 DiagState *FirstState; 6247 if (F.Kind == MK_ImplicitModule) { 6248 // Implicitly-built modules are reused with different diagnostic 6249 // settings. Use the initial diagnostic state from Diag to simulate this 6250 // compilation's diagnostic settings. 6251 FirstState = Diag.DiagStatesByLoc.FirstDiagState; 6252 DiagStates.push_back(FirstState); 6253 6254 // Skip the initial diagnostic state from the serialized module. 6255 assert(Record[1] == 0 && 6256 "Invalid data, unexpected backref in initial state"); 6257 Idx = 3 + Record[2] * 2; 6258 assert(Idx < Record.size() && 6259 "Invalid data, not enough state change pairs in initial state"); 6260 } else if (F.isModule()) { 6261 // For an explicit module, preserve the flags from the module build 6262 // command line (-w, -Weverything, -Werror, ...) along with any explicit 6263 // -Wblah flags. 6264 unsigned Flags = Record[Idx++]; 6265 DiagState Initial; 6266 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1; 6267 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1; 6268 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1; 6269 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1; 6270 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1; 6271 Initial.ExtBehavior = (diag::Severity)Flags; 6272 FirstState = ReadDiagState(Initial, SourceLocation(), true); 6273 6274 assert(F.OriginalSourceFileID.isValid()); 6275 6276 // Set up the root buffer of the module to start with the initial 6277 // diagnostic state of the module itself, to cover files that contain no 6278 // explicit transitions (for which we did not serialize anything). 6279 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID] 6280 .StateTransitions.push_back({FirstState, 0}); 6281 } else { 6282 // For prefix ASTs, start with whatever the user configured on the 6283 // command line. 6284 Idx++; // Skip flags. 6285 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, 6286 SourceLocation(), false); 6287 } 6288 6289 // Read the state transitions. 6290 unsigned NumLocations = Record[Idx++]; 6291 while (NumLocations--) { 6292 assert(Idx < Record.size() && 6293 "Invalid data, missing pragma diagnostic states"); 6294 SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]); 6295 auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc); 6296 assert(IDAndOffset.first.isValid() && "invalid FileID for transition"); 6297 assert(IDAndOffset.second == 0 && "not a start location for a FileID"); 6298 unsigned Transitions = Record[Idx++]; 6299 6300 // Note that we don't need to set up Parent/ParentOffset here, because 6301 // we won't be changing the diagnostic state within imported FileIDs 6302 // (other than perhaps appending to the main source file, which has no 6303 // parent). 6304 auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first]; 6305 F.StateTransitions.reserve(F.StateTransitions.size() + Transitions); 6306 for (unsigned I = 0; I != Transitions; ++I) { 6307 unsigned Offset = Record[Idx++]; 6308 auto *State = 6309 ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false); 6310 F.StateTransitions.push_back({State, Offset}); 6311 } 6312 } 6313 6314 // Read the final state. 6315 assert(Idx < Record.size() && 6316 "Invalid data, missing final pragma diagnostic state"); 6317 SourceLocation CurStateLoc = 6318 ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]); 6319 auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false); 6320 6321 if (!F.isModule()) { 6322 Diag.DiagStatesByLoc.CurDiagState = CurState; 6323 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc; 6324 6325 // Preserve the property that the imaginary root file describes the 6326 // current state. 6327 FileID NullFile; 6328 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions; 6329 if (T.empty()) 6330 T.push_back({CurState, 0}); 6331 else 6332 T[0].State = CurState; 6333 } 6334 6335 // Don't try to read these mappings again. 6336 Record.clear(); 6337 } 6338 } 6339 6340 /// Get the correct cursor and offset for loading a type. 6341 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) { 6342 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index); 6343 assert(I != GlobalTypeMap.end() && "Corrupted global type map"); 6344 ModuleFile *M = I->second; 6345 return RecordLocation( 6346 M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() + 6347 M->DeclsBlockStartOffset); 6348 } 6349 6350 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) { 6351 switch (code) { 6352 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \ 6353 case TYPE_##CODE_ID: return Type::CLASS_ID; 6354 #include "clang/Serialization/TypeBitCodes.def" 6355 default: return llvm::None; 6356 } 6357 } 6358 6359 /// Read and return the type with the given index.. 6360 /// 6361 /// The index is the type ID, shifted and minus the number of predefs. This 6362 /// routine actually reads the record corresponding to the type at the given 6363 /// location. It is a helper routine for GetType, which deals with reading type 6364 /// IDs. 6365 QualType ASTReader::readTypeRecord(unsigned Index) { 6366 assert(ContextObj && "reading type with no AST context"); 6367 ASTContext &Context = *ContextObj; 6368 RecordLocation Loc = TypeCursorForIndex(Index); 6369 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 6370 6371 // Keep track of where we are in the stream, then jump back there 6372 // after reading this type. 6373 SavedStreamPosition SavedPosition(DeclsCursor); 6374 6375 ReadingKindTracker ReadingKind(Read_Type, *this); 6376 6377 // Note that we are loading a type record. 6378 Deserializing AType(this); 6379 6380 if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) { 6381 Error(std::move(Err)); 6382 return QualType(); 6383 } 6384 Expected<unsigned> RawCode = DeclsCursor.ReadCode(); 6385 if (!RawCode) { 6386 Error(RawCode.takeError()); 6387 return QualType(); 6388 } 6389 6390 ASTRecordReader Record(*this, *Loc.F); 6391 Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get()); 6392 if (!Code) { 6393 Error(Code.takeError()); 6394 return QualType(); 6395 } 6396 if (Code.get() == TYPE_EXT_QUAL) { 6397 QualType baseType = Record.readQualType(); 6398 Qualifiers quals = Record.readQualifiers(); 6399 return Context.getQualifiedType(baseType, quals); 6400 } 6401 6402 auto maybeClass = getTypeClassForCode((TypeCode) Code.get()); 6403 if (!maybeClass) { 6404 Error("Unexpected code for type"); 6405 return QualType(); 6406 } 6407 6408 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record); 6409 return TypeReader.read(*maybeClass); 6410 } 6411 6412 namespace clang { 6413 6414 class TypeLocReader : public TypeLocVisitor<TypeLocReader> { 6415 ASTRecordReader &Reader; 6416 6417 SourceLocation readSourceLocation() { 6418 return Reader.readSourceLocation(); 6419 } 6420 6421 TypeSourceInfo *GetTypeSourceInfo() { 6422 return Reader.readTypeSourceInfo(); 6423 } 6424 6425 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() { 6426 return Reader.readNestedNameSpecifierLoc(); 6427 } 6428 6429 Attr *ReadAttr() { 6430 return Reader.readAttr(); 6431 } 6432 6433 public: 6434 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {} 6435 6436 // We want compile-time assurance that we've enumerated all of 6437 // these, so unfortunately we have to declare them first, then 6438 // define them out-of-line. 6439 #define ABSTRACT_TYPELOC(CLASS, PARENT) 6440 #define TYPELOC(CLASS, PARENT) \ 6441 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 6442 #include "clang/AST/TypeLocNodes.def" 6443 6444 void VisitFunctionTypeLoc(FunctionTypeLoc); 6445 void VisitArrayTypeLoc(ArrayTypeLoc); 6446 }; 6447 6448 } // namespace clang 6449 6450 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 6451 // nothing to do 6452 } 6453 6454 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 6455 TL.setBuiltinLoc(readSourceLocation()); 6456 if (TL.needsExtraLocalData()) { 6457 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt())); 6458 TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt())); 6459 TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt())); 6460 TL.setModeAttr(Reader.readInt()); 6461 } 6462 } 6463 6464 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { 6465 TL.setNameLoc(readSourceLocation()); 6466 } 6467 6468 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { 6469 TL.setStarLoc(readSourceLocation()); 6470 } 6471 6472 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) { 6473 // nothing to do 6474 } 6475 6476 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { 6477 // nothing to do 6478 } 6479 6480 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) { 6481 TL.setExpansionLoc(readSourceLocation()); 6482 } 6483 6484 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 6485 TL.setCaretLoc(readSourceLocation()); 6486 } 6487 6488 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 6489 TL.setAmpLoc(readSourceLocation()); 6490 } 6491 6492 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 6493 TL.setAmpAmpLoc(readSourceLocation()); 6494 } 6495 6496 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 6497 TL.setStarLoc(readSourceLocation()); 6498 TL.setClassTInfo(GetTypeSourceInfo()); 6499 } 6500 6501 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { 6502 TL.setLBracketLoc(readSourceLocation()); 6503 TL.setRBracketLoc(readSourceLocation()); 6504 if (Reader.readBool()) 6505 TL.setSizeExpr(Reader.readExpr()); 6506 else 6507 TL.setSizeExpr(nullptr); 6508 } 6509 6510 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 6511 VisitArrayTypeLoc(TL); 6512 } 6513 6514 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 6515 VisitArrayTypeLoc(TL); 6516 } 6517 6518 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 6519 VisitArrayTypeLoc(TL); 6520 } 6521 6522 void TypeLocReader::VisitDependentSizedArrayTypeLoc( 6523 DependentSizedArrayTypeLoc TL) { 6524 VisitArrayTypeLoc(TL); 6525 } 6526 6527 void TypeLocReader::VisitDependentAddressSpaceTypeLoc( 6528 DependentAddressSpaceTypeLoc TL) { 6529 6530 TL.setAttrNameLoc(readSourceLocation()); 6531 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6532 TL.setAttrExprOperand(Reader.readExpr()); 6533 } 6534 6535 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( 6536 DependentSizedExtVectorTypeLoc TL) { 6537 TL.setNameLoc(readSourceLocation()); 6538 } 6539 6540 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { 6541 TL.setNameLoc(readSourceLocation()); 6542 } 6543 6544 void TypeLocReader::VisitDependentVectorTypeLoc( 6545 DependentVectorTypeLoc TL) { 6546 TL.setNameLoc(readSourceLocation()); 6547 } 6548 6549 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 6550 TL.setNameLoc(readSourceLocation()); 6551 } 6552 6553 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) { 6554 TL.setAttrNameLoc(readSourceLocation()); 6555 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6556 TL.setAttrRowOperand(Reader.readExpr()); 6557 TL.setAttrColumnOperand(Reader.readExpr()); 6558 } 6559 6560 void TypeLocReader::VisitDependentSizedMatrixTypeLoc( 6561 DependentSizedMatrixTypeLoc TL) { 6562 TL.setAttrNameLoc(readSourceLocation()); 6563 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6564 TL.setAttrRowOperand(Reader.readExpr()); 6565 TL.setAttrColumnOperand(Reader.readExpr()); 6566 } 6567 6568 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 6569 TL.setLocalRangeBegin(readSourceLocation()); 6570 TL.setLParenLoc(readSourceLocation()); 6571 TL.setRParenLoc(readSourceLocation()); 6572 TL.setExceptionSpecRange(Reader.readSourceRange()); 6573 TL.setLocalRangeEnd(readSourceLocation()); 6574 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) { 6575 TL.setParam(i, Reader.readDeclAs<ParmVarDecl>()); 6576 } 6577 } 6578 6579 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 6580 VisitFunctionTypeLoc(TL); 6581 } 6582 6583 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 6584 VisitFunctionTypeLoc(TL); 6585 } 6586 6587 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 6588 TL.setNameLoc(readSourceLocation()); 6589 } 6590 6591 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 6592 TL.setNameLoc(readSourceLocation()); 6593 } 6594 6595 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 6596 TL.setTypeofLoc(readSourceLocation()); 6597 TL.setLParenLoc(readSourceLocation()); 6598 TL.setRParenLoc(readSourceLocation()); 6599 } 6600 6601 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 6602 TL.setTypeofLoc(readSourceLocation()); 6603 TL.setLParenLoc(readSourceLocation()); 6604 TL.setRParenLoc(readSourceLocation()); 6605 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6606 } 6607 6608 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 6609 TL.setNameLoc(readSourceLocation()); 6610 } 6611 6612 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 6613 TL.setKWLoc(readSourceLocation()); 6614 TL.setLParenLoc(readSourceLocation()); 6615 TL.setRParenLoc(readSourceLocation()); 6616 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6617 } 6618 6619 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) { 6620 TL.setNameLoc(readSourceLocation()); 6621 if (Reader.readBool()) { 6622 TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc()); 6623 TL.setTemplateKWLoc(readSourceLocation()); 6624 TL.setConceptNameLoc(readSourceLocation()); 6625 TL.setFoundDecl(Reader.readDeclAs<NamedDecl>()); 6626 TL.setLAngleLoc(readSourceLocation()); 6627 TL.setRAngleLoc(readSourceLocation()); 6628 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6629 TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo( 6630 TL.getTypePtr()->getArg(i).getKind())); 6631 } 6632 } 6633 6634 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc( 6635 DeducedTemplateSpecializationTypeLoc TL) { 6636 TL.setTemplateNameLoc(readSourceLocation()); 6637 } 6638 6639 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { 6640 TL.setNameLoc(readSourceLocation()); 6641 } 6642 6643 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { 6644 TL.setNameLoc(readSourceLocation()); 6645 } 6646 6647 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 6648 TL.setAttr(ReadAttr()); 6649 } 6650 6651 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 6652 TL.setNameLoc(readSourceLocation()); 6653 } 6654 6655 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( 6656 SubstTemplateTypeParmTypeLoc TL) { 6657 TL.setNameLoc(readSourceLocation()); 6658 } 6659 6660 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc( 6661 SubstTemplateTypeParmPackTypeLoc TL) { 6662 TL.setNameLoc(readSourceLocation()); 6663 } 6664 6665 void TypeLocReader::VisitTemplateSpecializationTypeLoc( 6666 TemplateSpecializationTypeLoc TL) { 6667 TL.setTemplateKeywordLoc(readSourceLocation()); 6668 TL.setTemplateNameLoc(readSourceLocation()); 6669 TL.setLAngleLoc(readSourceLocation()); 6670 TL.setRAngleLoc(readSourceLocation()); 6671 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6672 TL.setArgLocInfo( 6673 i, 6674 Reader.readTemplateArgumentLocInfo( 6675 TL.getTypePtr()->getArg(i).getKind())); 6676 } 6677 6678 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) { 6679 TL.setLParenLoc(readSourceLocation()); 6680 TL.setRParenLoc(readSourceLocation()); 6681 } 6682 6683 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 6684 TL.setElaboratedKeywordLoc(readSourceLocation()); 6685 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6686 } 6687 6688 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 6689 TL.setNameLoc(readSourceLocation()); 6690 } 6691 6692 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 6693 TL.setElaboratedKeywordLoc(readSourceLocation()); 6694 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6695 TL.setNameLoc(readSourceLocation()); 6696 } 6697 6698 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( 6699 DependentTemplateSpecializationTypeLoc TL) { 6700 TL.setElaboratedKeywordLoc(readSourceLocation()); 6701 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6702 TL.setTemplateKeywordLoc(readSourceLocation()); 6703 TL.setTemplateNameLoc(readSourceLocation()); 6704 TL.setLAngleLoc(readSourceLocation()); 6705 TL.setRAngleLoc(readSourceLocation()); 6706 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 6707 TL.setArgLocInfo( 6708 I, 6709 Reader.readTemplateArgumentLocInfo( 6710 TL.getTypePtr()->getArg(I).getKind())); 6711 } 6712 6713 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 6714 TL.setEllipsisLoc(readSourceLocation()); 6715 } 6716 6717 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 6718 TL.setNameLoc(readSourceLocation()); 6719 } 6720 6721 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) { 6722 if (TL.getNumProtocols()) { 6723 TL.setProtocolLAngleLoc(readSourceLocation()); 6724 TL.setProtocolRAngleLoc(readSourceLocation()); 6725 } 6726 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6727 TL.setProtocolLoc(i, readSourceLocation()); 6728 } 6729 6730 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 6731 TL.setHasBaseTypeAsWritten(Reader.readBool()); 6732 TL.setTypeArgsLAngleLoc(readSourceLocation()); 6733 TL.setTypeArgsRAngleLoc(readSourceLocation()); 6734 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i) 6735 TL.setTypeArgTInfo(i, GetTypeSourceInfo()); 6736 TL.setProtocolLAngleLoc(readSourceLocation()); 6737 TL.setProtocolRAngleLoc(readSourceLocation()); 6738 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6739 TL.setProtocolLoc(i, readSourceLocation()); 6740 } 6741 6742 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 6743 TL.setStarLoc(readSourceLocation()); 6744 } 6745 6746 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) { 6747 TL.setKWLoc(readSourceLocation()); 6748 TL.setLParenLoc(readSourceLocation()); 6749 TL.setRParenLoc(readSourceLocation()); 6750 } 6751 6752 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) { 6753 TL.setKWLoc(readSourceLocation()); 6754 } 6755 6756 void TypeLocReader::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) { 6757 TL.setNameLoc(readSourceLocation()); 6758 } 6759 void TypeLocReader::VisitDependentExtIntTypeLoc( 6760 clang::DependentExtIntTypeLoc TL) { 6761 TL.setNameLoc(readSourceLocation()); 6762 } 6763 6764 6765 void ASTRecordReader::readTypeLoc(TypeLoc TL) { 6766 TypeLocReader TLR(*this); 6767 for (; !TL.isNull(); TL = TL.getNextTypeLoc()) 6768 TLR.Visit(TL); 6769 } 6770 6771 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() { 6772 QualType InfoTy = readType(); 6773 if (InfoTy.isNull()) 6774 return nullptr; 6775 6776 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy); 6777 readTypeLoc(TInfo->getTypeLoc()); 6778 return TInfo; 6779 } 6780 6781 QualType ASTReader::GetType(TypeID ID) { 6782 assert(ContextObj && "reading type with no AST context"); 6783 ASTContext &Context = *ContextObj; 6784 6785 unsigned FastQuals = ID & Qualifiers::FastMask; 6786 unsigned Index = ID >> Qualifiers::FastWidth; 6787 6788 if (Index < NUM_PREDEF_TYPE_IDS) { 6789 QualType T; 6790 switch ((PredefinedTypeIDs)Index) { 6791 case PREDEF_TYPE_NULL_ID: 6792 return QualType(); 6793 case PREDEF_TYPE_VOID_ID: 6794 T = Context.VoidTy; 6795 break; 6796 case PREDEF_TYPE_BOOL_ID: 6797 T = Context.BoolTy; 6798 break; 6799 case PREDEF_TYPE_CHAR_U_ID: 6800 case PREDEF_TYPE_CHAR_S_ID: 6801 // FIXME: Check that the signedness of CharTy is correct! 6802 T = Context.CharTy; 6803 break; 6804 case PREDEF_TYPE_UCHAR_ID: 6805 T = Context.UnsignedCharTy; 6806 break; 6807 case PREDEF_TYPE_USHORT_ID: 6808 T = Context.UnsignedShortTy; 6809 break; 6810 case PREDEF_TYPE_UINT_ID: 6811 T = Context.UnsignedIntTy; 6812 break; 6813 case PREDEF_TYPE_ULONG_ID: 6814 T = Context.UnsignedLongTy; 6815 break; 6816 case PREDEF_TYPE_ULONGLONG_ID: 6817 T = Context.UnsignedLongLongTy; 6818 break; 6819 case PREDEF_TYPE_UINT128_ID: 6820 T = Context.UnsignedInt128Ty; 6821 break; 6822 case PREDEF_TYPE_SCHAR_ID: 6823 T = Context.SignedCharTy; 6824 break; 6825 case PREDEF_TYPE_WCHAR_ID: 6826 T = Context.WCharTy; 6827 break; 6828 case PREDEF_TYPE_SHORT_ID: 6829 T = Context.ShortTy; 6830 break; 6831 case PREDEF_TYPE_INT_ID: 6832 T = Context.IntTy; 6833 break; 6834 case PREDEF_TYPE_LONG_ID: 6835 T = Context.LongTy; 6836 break; 6837 case PREDEF_TYPE_LONGLONG_ID: 6838 T = Context.LongLongTy; 6839 break; 6840 case PREDEF_TYPE_INT128_ID: 6841 T = Context.Int128Ty; 6842 break; 6843 case PREDEF_TYPE_BFLOAT16_ID: 6844 T = Context.BFloat16Ty; 6845 break; 6846 case PREDEF_TYPE_HALF_ID: 6847 T = Context.HalfTy; 6848 break; 6849 case PREDEF_TYPE_FLOAT_ID: 6850 T = Context.FloatTy; 6851 break; 6852 case PREDEF_TYPE_DOUBLE_ID: 6853 T = Context.DoubleTy; 6854 break; 6855 case PREDEF_TYPE_LONGDOUBLE_ID: 6856 T = Context.LongDoubleTy; 6857 break; 6858 case PREDEF_TYPE_SHORT_ACCUM_ID: 6859 T = Context.ShortAccumTy; 6860 break; 6861 case PREDEF_TYPE_ACCUM_ID: 6862 T = Context.AccumTy; 6863 break; 6864 case PREDEF_TYPE_LONG_ACCUM_ID: 6865 T = Context.LongAccumTy; 6866 break; 6867 case PREDEF_TYPE_USHORT_ACCUM_ID: 6868 T = Context.UnsignedShortAccumTy; 6869 break; 6870 case PREDEF_TYPE_UACCUM_ID: 6871 T = Context.UnsignedAccumTy; 6872 break; 6873 case PREDEF_TYPE_ULONG_ACCUM_ID: 6874 T = Context.UnsignedLongAccumTy; 6875 break; 6876 case PREDEF_TYPE_SHORT_FRACT_ID: 6877 T = Context.ShortFractTy; 6878 break; 6879 case PREDEF_TYPE_FRACT_ID: 6880 T = Context.FractTy; 6881 break; 6882 case PREDEF_TYPE_LONG_FRACT_ID: 6883 T = Context.LongFractTy; 6884 break; 6885 case PREDEF_TYPE_USHORT_FRACT_ID: 6886 T = Context.UnsignedShortFractTy; 6887 break; 6888 case PREDEF_TYPE_UFRACT_ID: 6889 T = Context.UnsignedFractTy; 6890 break; 6891 case PREDEF_TYPE_ULONG_FRACT_ID: 6892 T = Context.UnsignedLongFractTy; 6893 break; 6894 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID: 6895 T = Context.SatShortAccumTy; 6896 break; 6897 case PREDEF_TYPE_SAT_ACCUM_ID: 6898 T = Context.SatAccumTy; 6899 break; 6900 case PREDEF_TYPE_SAT_LONG_ACCUM_ID: 6901 T = Context.SatLongAccumTy; 6902 break; 6903 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID: 6904 T = Context.SatUnsignedShortAccumTy; 6905 break; 6906 case PREDEF_TYPE_SAT_UACCUM_ID: 6907 T = Context.SatUnsignedAccumTy; 6908 break; 6909 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID: 6910 T = Context.SatUnsignedLongAccumTy; 6911 break; 6912 case PREDEF_TYPE_SAT_SHORT_FRACT_ID: 6913 T = Context.SatShortFractTy; 6914 break; 6915 case PREDEF_TYPE_SAT_FRACT_ID: 6916 T = Context.SatFractTy; 6917 break; 6918 case PREDEF_TYPE_SAT_LONG_FRACT_ID: 6919 T = Context.SatLongFractTy; 6920 break; 6921 case PREDEF_TYPE_SAT_USHORT_FRACT_ID: 6922 T = Context.SatUnsignedShortFractTy; 6923 break; 6924 case PREDEF_TYPE_SAT_UFRACT_ID: 6925 T = Context.SatUnsignedFractTy; 6926 break; 6927 case PREDEF_TYPE_SAT_ULONG_FRACT_ID: 6928 T = Context.SatUnsignedLongFractTy; 6929 break; 6930 case PREDEF_TYPE_FLOAT16_ID: 6931 T = Context.Float16Ty; 6932 break; 6933 case PREDEF_TYPE_FLOAT128_ID: 6934 T = Context.Float128Ty; 6935 break; 6936 case PREDEF_TYPE_OVERLOAD_ID: 6937 T = Context.OverloadTy; 6938 break; 6939 case PREDEF_TYPE_BOUND_MEMBER: 6940 T = Context.BoundMemberTy; 6941 break; 6942 case PREDEF_TYPE_PSEUDO_OBJECT: 6943 T = Context.PseudoObjectTy; 6944 break; 6945 case PREDEF_TYPE_DEPENDENT_ID: 6946 T = Context.DependentTy; 6947 break; 6948 case PREDEF_TYPE_UNKNOWN_ANY: 6949 T = Context.UnknownAnyTy; 6950 break; 6951 case PREDEF_TYPE_NULLPTR_ID: 6952 T = Context.NullPtrTy; 6953 break; 6954 case PREDEF_TYPE_CHAR8_ID: 6955 T = Context.Char8Ty; 6956 break; 6957 case PREDEF_TYPE_CHAR16_ID: 6958 T = Context.Char16Ty; 6959 break; 6960 case PREDEF_TYPE_CHAR32_ID: 6961 T = Context.Char32Ty; 6962 break; 6963 case PREDEF_TYPE_OBJC_ID: 6964 T = Context.ObjCBuiltinIdTy; 6965 break; 6966 case PREDEF_TYPE_OBJC_CLASS: 6967 T = Context.ObjCBuiltinClassTy; 6968 break; 6969 case PREDEF_TYPE_OBJC_SEL: 6970 T = Context.ObjCBuiltinSelTy; 6971 break; 6972 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 6973 case PREDEF_TYPE_##Id##_ID: \ 6974 T = Context.SingletonId; \ 6975 break; 6976 #include "clang/Basic/OpenCLImageTypes.def" 6977 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ 6978 case PREDEF_TYPE_##Id##_ID: \ 6979 T = Context.Id##Ty; \ 6980 break; 6981 #include "clang/Basic/OpenCLExtensionTypes.def" 6982 case PREDEF_TYPE_SAMPLER_ID: 6983 T = Context.OCLSamplerTy; 6984 break; 6985 case PREDEF_TYPE_EVENT_ID: 6986 T = Context.OCLEventTy; 6987 break; 6988 case PREDEF_TYPE_CLK_EVENT_ID: 6989 T = Context.OCLClkEventTy; 6990 break; 6991 case PREDEF_TYPE_QUEUE_ID: 6992 T = Context.OCLQueueTy; 6993 break; 6994 case PREDEF_TYPE_RESERVE_ID_ID: 6995 T = Context.OCLReserveIDTy; 6996 break; 6997 case PREDEF_TYPE_AUTO_DEDUCT: 6998 T = Context.getAutoDeductType(); 6999 break; 7000 case PREDEF_TYPE_AUTO_RREF_DEDUCT: 7001 T = Context.getAutoRRefDeductType(); 7002 break; 7003 case PREDEF_TYPE_ARC_UNBRIDGED_CAST: 7004 T = Context.ARCUnbridgedCastTy; 7005 break; 7006 case PREDEF_TYPE_BUILTIN_FN: 7007 T = Context.BuiltinFnTy; 7008 break; 7009 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX: 7010 T = Context.IncompleteMatrixIdxTy; 7011 break; 7012 case PREDEF_TYPE_OMP_ARRAY_SECTION: 7013 T = Context.OMPArraySectionTy; 7014 break; 7015 case PREDEF_TYPE_OMP_ARRAY_SHAPING: 7016 T = Context.OMPArraySectionTy; 7017 break; 7018 case PREDEF_TYPE_OMP_ITERATOR: 7019 T = Context.OMPIteratorTy; 7020 break; 7021 #define SVE_TYPE(Name, Id, SingletonId) \ 7022 case PREDEF_TYPE_##Id##_ID: \ 7023 T = Context.SingletonId; \ 7024 break; 7025 #include "clang/Basic/AArch64SVEACLETypes.def" 7026 #define PPC_MMA_VECTOR_TYPE(Name, Id, Size) \ 7027 case PREDEF_TYPE_##Id##_ID: \ 7028 T = Context.Id##Ty; \ 7029 break; 7030 #include "clang/Basic/PPCTypes.def" 7031 } 7032 7033 assert(!T.isNull() && "Unknown predefined type"); 7034 return T.withFastQualifiers(FastQuals); 7035 } 7036 7037 Index -= NUM_PREDEF_TYPE_IDS; 7038 assert(Index < TypesLoaded.size() && "Type index out-of-range"); 7039 if (TypesLoaded[Index].isNull()) { 7040 TypesLoaded[Index] = readTypeRecord(Index); 7041 if (TypesLoaded[Index].isNull()) 7042 return QualType(); 7043 7044 TypesLoaded[Index]->setFromAST(); 7045 if (DeserializationListener) 7046 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID), 7047 TypesLoaded[Index]); 7048 } 7049 7050 return TypesLoaded[Index].withFastQualifiers(FastQuals); 7051 } 7052 7053 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) { 7054 return GetType(getGlobalTypeID(F, LocalID)); 7055 } 7056 7057 serialization::TypeID 7058 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const { 7059 unsigned FastQuals = LocalID & Qualifiers::FastMask; 7060 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth; 7061 7062 if (LocalIndex < NUM_PREDEF_TYPE_IDS) 7063 return LocalID; 7064 7065 if (!F.ModuleOffsetMap.empty()) 7066 ReadModuleOffsetMap(F); 7067 7068 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7069 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS); 7070 assert(I != F.TypeRemap.end() && "Invalid index into type index remap"); 7071 7072 unsigned GlobalIndex = LocalIndex + I->second; 7073 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals; 7074 } 7075 7076 TemplateArgumentLocInfo 7077 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) { 7078 switch (Kind) { 7079 case TemplateArgument::Expression: 7080 return readExpr(); 7081 case TemplateArgument::Type: 7082 return readTypeSourceInfo(); 7083 case TemplateArgument::Template: { 7084 NestedNameSpecifierLoc QualifierLoc = 7085 readNestedNameSpecifierLoc(); 7086 SourceLocation TemplateNameLoc = readSourceLocation(); 7087 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc, 7088 TemplateNameLoc, SourceLocation()); 7089 } 7090 case TemplateArgument::TemplateExpansion: { 7091 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc(); 7092 SourceLocation TemplateNameLoc = readSourceLocation(); 7093 SourceLocation EllipsisLoc = readSourceLocation(); 7094 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc, 7095 TemplateNameLoc, EllipsisLoc); 7096 } 7097 case TemplateArgument::Null: 7098 case TemplateArgument::Integral: 7099 case TemplateArgument::Declaration: 7100 case TemplateArgument::NullPtr: 7101 case TemplateArgument::Pack: 7102 // FIXME: Is this right? 7103 return TemplateArgumentLocInfo(); 7104 } 7105 llvm_unreachable("unexpected template argument loc"); 7106 } 7107 7108 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() { 7109 TemplateArgument Arg = readTemplateArgument(); 7110 7111 if (Arg.getKind() == TemplateArgument::Expression) { 7112 if (readBool()) // bool InfoHasSameExpr. 7113 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); 7114 } 7115 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind())); 7116 } 7117 7118 const ASTTemplateArgumentListInfo * 7119 ASTRecordReader::readASTTemplateArgumentListInfo() { 7120 SourceLocation LAngleLoc = readSourceLocation(); 7121 SourceLocation RAngleLoc = readSourceLocation(); 7122 unsigned NumArgsAsWritten = readInt(); 7123 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc); 7124 for (unsigned i = 0; i != NumArgsAsWritten; ++i) 7125 TemplArgsInfo.addArgument(readTemplateArgumentLoc()); 7126 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo); 7127 } 7128 7129 Decl *ASTReader::GetExternalDecl(uint32_t ID) { 7130 return GetDecl(ID); 7131 } 7132 7133 void ASTReader::CompleteRedeclChain(const Decl *D) { 7134 if (NumCurrentElementsDeserializing) { 7135 // We arrange to not care about the complete redeclaration chain while we're 7136 // deserializing. Just remember that the AST has marked this one as complete 7137 // but that it's not actually complete yet, so we know we still need to 7138 // complete it later. 7139 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D)); 7140 return; 7141 } 7142 7143 const DeclContext *DC = D->getDeclContext()->getRedeclContext(); 7144 7145 // If this is a named declaration, complete it by looking it up 7146 // within its context. 7147 // 7148 // FIXME: Merging a function definition should merge 7149 // all mergeable entities within it. 7150 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) || 7151 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) { 7152 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) { 7153 if (!getContext().getLangOpts().CPlusPlus && 7154 isa<TranslationUnitDecl>(DC)) { 7155 // Outside of C++, we don't have a lookup table for the TU, so update 7156 // the identifier instead. (For C++ modules, we don't store decls 7157 // in the serialized identifier table, so we do the lookup in the TU.) 7158 auto *II = Name.getAsIdentifierInfo(); 7159 assert(II && "non-identifier name in C?"); 7160 if (II->isOutOfDate()) 7161 updateOutOfDateIdentifier(*II); 7162 } else 7163 DC->lookup(Name); 7164 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) { 7165 // Find all declarations of this kind from the relevant context. 7166 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) { 7167 auto *DC = cast<DeclContext>(DCDecl); 7168 SmallVector<Decl*, 8> Decls; 7169 FindExternalLexicalDecls( 7170 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls); 7171 } 7172 } 7173 } 7174 7175 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) 7176 CTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7177 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) 7178 VTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7179 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 7180 if (auto *Template = FD->getPrimaryTemplate()) 7181 Template->LoadLazySpecializations(); 7182 } 7183 } 7184 7185 CXXCtorInitializer ** 7186 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) { 7187 RecordLocation Loc = getLocalBitOffset(Offset); 7188 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7189 SavedStreamPosition SavedPosition(Cursor); 7190 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7191 Error(std::move(Err)); 7192 return nullptr; 7193 } 7194 ReadingKindTracker ReadingKind(Read_Decl, *this); 7195 7196 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7197 if (!MaybeCode) { 7198 Error(MaybeCode.takeError()); 7199 return nullptr; 7200 } 7201 unsigned Code = MaybeCode.get(); 7202 7203 ASTRecordReader Record(*this, *Loc.F); 7204 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7205 if (!MaybeRecCode) { 7206 Error(MaybeRecCode.takeError()); 7207 return nullptr; 7208 } 7209 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) { 7210 Error("malformed AST file: missing C++ ctor initializers"); 7211 return nullptr; 7212 } 7213 7214 return Record.readCXXCtorInitializers(); 7215 } 7216 7217 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) { 7218 assert(ContextObj && "reading base specifiers with no AST context"); 7219 ASTContext &Context = *ContextObj; 7220 7221 RecordLocation Loc = getLocalBitOffset(Offset); 7222 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7223 SavedStreamPosition SavedPosition(Cursor); 7224 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7225 Error(std::move(Err)); 7226 return nullptr; 7227 } 7228 ReadingKindTracker ReadingKind(Read_Decl, *this); 7229 7230 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7231 if (!MaybeCode) { 7232 Error(MaybeCode.takeError()); 7233 return nullptr; 7234 } 7235 unsigned Code = MaybeCode.get(); 7236 7237 ASTRecordReader Record(*this, *Loc.F); 7238 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7239 if (!MaybeRecCode) { 7240 Error(MaybeCode.takeError()); 7241 return nullptr; 7242 } 7243 unsigned RecCode = MaybeRecCode.get(); 7244 7245 if (RecCode != DECL_CXX_BASE_SPECIFIERS) { 7246 Error("malformed AST file: missing C++ base specifiers"); 7247 return nullptr; 7248 } 7249 7250 unsigned NumBases = Record.readInt(); 7251 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases); 7252 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases]; 7253 for (unsigned I = 0; I != NumBases; ++I) 7254 Bases[I] = Record.readCXXBaseSpecifier(); 7255 return Bases; 7256 } 7257 7258 serialization::DeclID 7259 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const { 7260 if (LocalID < NUM_PREDEF_DECL_IDS) 7261 return LocalID; 7262 7263 if (!F.ModuleOffsetMap.empty()) 7264 ReadModuleOffsetMap(F); 7265 7266 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7267 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS); 7268 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap"); 7269 7270 return LocalID + I->second; 7271 } 7272 7273 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID, 7274 ModuleFile &M) const { 7275 // Predefined decls aren't from any module. 7276 if (ID < NUM_PREDEF_DECL_IDS) 7277 return false; 7278 7279 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID && 7280 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls; 7281 } 7282 7283 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) { 7284 if (!D->isFromASTFile()) 7285 return nullptr; 7286 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID()); 7287 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7288 return I->second; 7289 } 7290 7291 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) { 7292 if (ID < NUM_PREDEF_DECL_IDS) 7293 return SourceLocation(); 7294 7295 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7296 7297 if (Index > DeclsLoaded.size()) { 7298 Error("declaration ID out-of-range for AST file"); 7299 return SourceLocation(); 7300 } 7301 7302 if (Decl *D = DeclsLoaded[Index]) 7303 return D->getLocation(); 7304 7305 SourceLocation Loc; 7306 DeclCursorForID(ID, Loc); 7307 return Loc; 7308 } 7309 7310 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) { 7311 switch (ID) { 7312 case PREDEF_DECL_NULL_ID: 7313 return nullptr; 7314 7315 case PREDEF_DECL_TRANSLATION_UNIT_ID: 7316 return Context.getTranslationUnitDecl(); 7317 7318 case PREDEF_DECL_OBJC_ID_ID: 7319 return Context.getObjCIdDecl(); 7320 7321 case PREDEF_DECL_OBJC_SEL_ID: 7322 return Context.getObjCSelDecl(); 7323 7324 case PREDEF_DECL_OBJC_CLASS_ID: 7325 return Context.getObjCClassDecl(); 7326 7327 case PREDEF_DECL_OBJC_PROTOCOL_ID: 7328 return Context.getObjCProtocolDecl(); 7329 7330 case PREDEF_DECL_INT_128_ID: 7331 return Context.getInt128Decl(); 7332 7333 case PREDEF_DECL_UNSIGNED_INT_128_ID: 7334 return Context.getUInt128Decl(); 7335 7336 case PREDEF_DECL_OBJC_INSTANCETYPE_ID: 7337 return Context.getObjCInstanceTypeDecl(); 7338 7339 case PREDEF_DECL_BUILTIN_VA_LIST_ID: 7340 return Context.getBuiltinVaListDecl(); 7341 7342 case PREDEF_DECL_VA_LIST_TAG: 7343 return Context.getVaListTagDecl(); 7344 7345 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID: 7346 return Context.getBuiltinMSVaListDecl(); 7347 7348 case PREDEF_DECL_BUILTIN_MS_GUID_ID: 7349 return Context.getMSGuidTagDecl(); 7350 7351 case PREDEF_DECL_EXTERN_C_CONTEXT_ID: 7352 return Context.getExternCContextDecl(); 7353 7354 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID: 7355 return Context.getMakeIntegerSeqDecl(); 7356 7357 case PREDEF_DECL_CF_CONSTANT_STRING_ID: 7358 return Context.getCFConstantStringDecl(); 7359 7360 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID: 7361 return Context.getCFConstantStringTagDecl(); 7362 7363 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID: 7364 return Context.getTypePackElementDecl(); 7365 } 7366 llvm_unreachable("PredefinedDeclIDs unknown enum value"); 7367 } 7368 7369 Decl *ASTReader::GetExistingDecl(DeclID ID) { 7370 assert(ContextObj && "reading decl with no AST context"); 7371 if (ID < NUM_PREDEF_DECL_IDS) { 7372 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID); 7373 if (D) { 7374 // Track that we have merged the declaration with ID \p ID into the 7375 // pre-existing predefined declaration \p D. 7376 auto &Merged = KeyDecls[D->getCanonicalDecl()]; 7377 if (Merged.empty()) 7378 Merged.push_back(ID); 7379 } 7380 return D; 7381 } 7382 7383 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7384 7385 if (Index >= DeclsLoaded.size()) { 7386 assert(0 && "declaration ID out-of-range for AST file"); 7387 Error("declaration ID out-of-range for AST file"); 7388 return nullptr; 7389 } 7390 7391 return DeclsLoaded[Index]; 7392 } 7393 7394 Decl *ASTReader::GetDecl(DeclID ID) { 7395 if (ID < NUM_PREDEF_DECL_IDS) 7396 return GetExistingDecl(ID); 7397 7398 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7399 7400 if (Index >= DeclsLoaded.size()) { 7401 assert(0 && "declaration ID out-of-range for AST file"); 7402 Error("declaration ID out-of-range for AST file"); 7403 return nullptr; 7404 } 7405 7406 if (!DeclsLoaded[Index]) { 7407 ReadDeclRecord(ID); 7408 if (DeserializationListener) 7409 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); 7410 } 7411 7412 return DeclsLoaded[Index]; 7413 } 7414 7415 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M, 7416 DeclID GlobalID) { 7417 if (GlobalID < NUM_PREDEF_DECL_IDS) 7418 return GlobalID; 7419 7420 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID); 7421 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7422 ModuleFile *Owner = I->second; 7423 7424 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos 7425 = M.GlobalToLocalDeclIDs.find(Owner); 7426 if (Pos == M.GlobalToLocalDeclIDs.end()) 7427 return 0; 7428 7429 return GlobalID - Owner->BaseDeclID + Pos->second; 7430 } 7431 7432 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F, 7433 const RecordData &Record, 7434 unsigned &Idx) { 7435 if (Idx >= Record.size()) { 7436 Error("Corrupted AST file"); 7437 return 0; 7438 } 7439 7440 return getGlobalDeclID(F, Record[Idx++]); 7441 } 7442 7443 /// Resolve the offset of a statement into a statement. 7444 /// 7445 /// This operation will read a new statement from the external 7446 /// source each time it is called, and is meant to be used via a 7447 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 7448 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) { 7449 // Switch case IDs are per Decl. 7450 ClearSwitchCaseIDs(); 7451 7452 // Offset here is a global offset across the entire chain. 7453 RecordLocation Loc = getLocalBitOffset(Offset); 7454 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) { 7455 Error(std::move(Err)); 7456 return nullptr; 7457 } 7458 assert(NumCurrentElementsDeserializing == 0 && 7459 "should not be called while already deserializing"); 7460 Deserializing D(this); 7461 return ReadStmtFromStream(*Loc.F); 7462 } 7463 7464 void ASTReader::FindExternalLexicalDecls( 7465 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant, 7466 SmallVectorImpl<Decl *> &Decls) { 7467 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {}; 7468 7469 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) { 7470 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries"); 7471 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) { 7472 auto K = (Decl::Kind)+LexicalDecls[I]; 7473 if (!IsKindWeWant(K)) 7474 continue; 7475 7476 auto ID = (serialization::DeclID)+LexicalDecls[I + 1]; 7477 7478 // Don't add predefined declarations to the lexical context more 7479 // than once. 7480 if (ID < NUM_PREDEF_DECL_IDS) { 7481 if (PredefsVisited[ID]) 7482 continue; 7483 7484 PredefsVisited[ID] = true; 7485 } 7486 7487 if (Decl *D = GetLocalDecl(*M, ID)) { 7488 assert(D->getKind() == K && "wrong kind for lexical decl"); 7489 if (!DC->isDeclInLexicalTraversal(D)) 7490 Decls.push_back(D); 7491 } 7492 } 7493 }; 7494 7495 if (isa<TranslationUnitDecl>(DC)) { 7496 for (auto Lexical : TULexicalDecls) 7497 Visit(Lexical.first, Lexical.second); 7498 } else { 7499 auto I = LexicalDecls.find(DC); 7500 if (I != LexicalDecls.end()) 7501 Visit(I->second.first, I->second.second); 7502 } 7503 7504 ++NumLexicalDeclContextsRead; 7505 } 7506 7507 namespace { 7508 7509 class DeclIDComp { 7510 ASTReader &Reader; 7511 ModuleFile &Mod; 7512 7513 public: 7514 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {} 7515 7516 bool operator()(LocalDeclID L, LocalDeclID R) const { 7517 SourceLocation LHS = getLocation(L); 7518 SourceLocation RHS = getLocation(R); 7519 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7520 } 7521 7522 bool operator()(SourceLocation LHS, LocalDeclID R) const { 7523 SourceLocation RHS = getLocation(R); 7524 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7525 } 7526 7527 bool operator()(LocalDeclID L, SourceLocation RHS) const { 7528 SourceLocation LHS = getLocation(L); 7529 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7530 } 7531 7532 SourceLocation getLocation(LocalDeclID ID) const { 7533 return Reader.getSourceManager().getFileLoc( 7534 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID))); 7535 } 7536 }; 7537 7538 } // namespace 7539 7540 void ASTReader::FindFileRegionDecls(FileID File, 7541 unsigned Offset, unsigned Length, 7542 SmallVectorImpl<Decl *> &Decls) { 7543 SourceManager &SM = getSourceManager(); 7544 7545 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File); 7546 if (I == FileDeclIDs.end()) 7547 return; 7548 7549 FileDeclsInfo &DInfo = I->second; 7550 if (DInfo.Decls.empty()) 7551 return; 7552 7553 SourceLocation 7554 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset); 7555 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length); 7556 7557 DeclIDComp DIDComp(*this, *DInfo.Mod); 7558 ArrayRef<serialization::LocalDeclID>::iterator BeginIt = 7559 llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp); 7560 if (BeginIt != DInfo.Decls.begin()) 7561 --BeginIt; 7562 7563 // If we are pointing at a top-level decl inside an objc container, we need 7564 // to backtrack until we find it otherwise we will fail to report that the 7565 // region overlaps with an objc container. 7566 while (BeginIt != DInfo.Decls.begin() && 7567 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt)) 7568 ->isTopLevelDeclInObjCContainer()) 7569 --BeginIt; 7570 7571 ArrayRef<serialization::LocalDeclID>::iterator EndIt = 7572 llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp); 7573 if (EndIt != DInfo.Decls.end()) 7574 ++EndIt; 7575 7576 for (ArrayRef<serialization::LocalDeclID>::iterator 7577 DIt = BeginIt; DIt != EndIt; ++DIt) 7578 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt))); 7579 } 7580 7581 bool 7582 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, 7583 DeclarationName Name) { 7584 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() && 7585 "DeclContext has no visible decls in storage"); 7586 if (!Name) 7587 return false; 7588 7589 auto It = Lookups.find(DC); 7590 if (It == Lookups.end()) 7591 return false; 7592 7593 Deserializing LookupResults(this); 7594 7595 // Load the list of declarations. 7596 SmallVector<NamedDecl *, 64> Decls; 7597 for (DeclID ID : It->second.Table.find(Name)) { 7598 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7599 if (ND->getDeclName() == Name) 7600 Decls.push_back(ND); 7601 } 7602 7603 ++NumVisibleDeclContextsRead; 7604 SetExternalVisibleDeclsForName(DC, Name, Decls); 7605 return !Decls.empty(); 7606 } 7607 7608 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) { 7609 if (!DC->hasExternalVisibleStorage()) 7610 return; 7611 7612 auto It = Lookups.find(DC); 7613 assert(It != Lookups.end() && 7614 "have external visible storage but no lookup tables"); 7615 7616 DeclsMap Decls; 7617 7618 for (DeclID ID : It->second.Table.findAll()) { 7619 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7620 Decls[ND->getDeclName()].push_back(ND); 7621 } 7622 7623 ++NumVisibleDeclContextsRead; 7624 7625 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) { 7626 SetExternalVisibleDeclsForName(DC, I->first, I->second); 7627 } 7628 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false); 7629 } 7630 7631 const serialization::reader::DeclContextLookupTable * 7632 ASTReader::getLoadedLookupTables(DeclContext *Primary) const { 7633 auto I = Lookups.find(Primary); 7634 return I == Lookups.end() ? nullptr : &I->second; 7635 } 7636 7637 /// Under non-PCH compilation the consumer receives the objc methods 7638 /// before receiving the implementation, and codegen depends on this. 7639 /// We simulate this by deserializing and passing to consumer the methods of the 7640 /// implementation before passing the deserialized implementation decl. 7641 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD, 7642 ASTConsumer *Consumer) { 7643 assert(ImplD && Consumer); 7644 7645 for (auto *I : ImplD->methods()) 7646 Consumer->HandleInterestingDecl(DeclGroupRef(I)); 7647 7648 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD)); 7649 } 7650 7651 void ASTReader::PassInterestingDeclToConsumer(Decl *D) { 7652 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) 7653 PassObjCImplDeclToConsumer(ImplD, Consumer); 7654 else 7655 Consumer->HandleInterestingDecl(DeclGroupRef(D)); 7656 } 7657 7658 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) { 7659 this->Consumer = Consumer; 7660 7661 if (Consumer) 7662 PassInterestingDeclsToConsumer(); 7663 7664 if (DeserializationListener) 7665 DeserializationListener->ReaderInitialized(this); 7666 } 7667 7668 void ASTReader::PrintStats() { 7669 std::fprintf(stderr, "*** AST File Statistics:\n"); 7670 7671 unsigned NumTypesLoaded 7672 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), 7673 QualType()); 7674 unsigned NumDeclsLoaded 7675 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), 7676 (Decl *)nullptr); 7677 unsigned NumIdentifiersLoaded 7678 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), 7679 IdentifiersLoaded.end(), 7680 (IdentifierInfo *)nullptr); 7681 unsigned NumMacrosLoaded 7682 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(), 7683 MacrosLoaded.end(), 7684 (MacroInfo *)nullptr); 7685 unsigned NumSelectorsLoaded 7686 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), 7687 SelectorsLoaded.end(), 7688 Selector()); 7689 7690 if (unsigned TotalNumSLocEntries = getTotalNumSLocs()) 7691 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", 7692 NumSLocEntriesRead, TotalNumSLocEntries, 7693 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); 7694 if (!TypesLoaded.empty()) 7695 std::fprintf(stderr, " %u/%u types read (%f%%)\n", 7696 NumTypesLoaded, (unsigned)TypesLoaded.size(), 7697 ((float)NumTypesLoaded/TypesLoaded.size() * 100)); 7698 if (!DeclsLoaded.empty()) 7699 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", 7700 NumDeclsLoaded, (unsigned)DeclsLoaded.size(), 7701 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); 7702 if (!IdentifiersLoaded.empty()) 7703 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", 7704 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), 7705 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); 7706 if (!MacrosLoaded.empty()) 7707 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7708 NumMacrosLoaded, (unsigned)MacrosLoaded.size(), 7709 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100)); 7710 if (!SelectorsLoaded.empty()) 7711 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", 7712 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), 7713 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); 7714 if (TotalNumStatements) 7715 std::fprintf(stderr, " %u/%u statements read (%f%%)\n", 7716 NumStatementsRead, TotalNumStatements, 7717 ((float)NumStatementsRead/TotalNumStatements * 100)); 7718 if (TotalNumMacros) 7719 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7720 NumMacrosRead, TotalNumMacros, 7721 ((float)NumMacrosRead/TotalNumMacros * 100)); 7722 if (TotalLexicalDeclContexts) 7723 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", 7724 NumLexicalDeclContextsRead, TotalLexicalDeclContexts, 7725 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts 7726 * 100)); 7727 if (TotalVisibleDeclContexts) 7728 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", 7729 NumVisibleDeclContextsRead, TotalVisibleDeclContexts, 7730 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts 7731 * 100)); 7732 if (TotalNumMethodPoolEntries) 7733 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", 7734 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, 7735 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries 7736 * 100)); 7737 if (NumMethodPoolLookups) 7738 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n", 7739 NumMethodPoolHits, NumMethodPoolLookups, 7740 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0)); 7741 if (NumMethodPoolTableLookups) 7742 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n", 7743 NumMethodPoolTableHits, NumMethodPoolTableLookups, 7744 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups 7745 * 100.0)); 7746 if (NumIdentifierLookupHits) 7747 std::fprintf(stderr, 7748 " %u / %u identifier table lookups succeeded (%f%%)\n", 7749 NumIdentifierLookupHits, NumIdentifierLookups, 7750 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups); 7751 7752 if (GlobalIndex) { 7753 std::fprintf(stderr, "\n"); 7754 GlobalIndex->printStats(); 7755 } 7756 7757 std::fprintf(stderr, "\n"); 7758 dump(); 7759 std::fprintf(stderr, "\n"); 7760 } 7761 7762 template<typename Key, typename ModuleFile, unsigned InitialCapacity> 7763 LLVM_DUMP_METHOD static void 7764 dumpModuleIDMap(StringRef Name, 7765 const ContinuousRangeMap<Key, ModuleFile *, 7766 InitialCapacity> &Map) { 7767 if (Map.begin() == Map.end()) 7768 return; 7769 7770 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>; 7771 7772 llvm::errs() << Name << ":\n"; 7773 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end(); 7774 I != IEnd; ++I) { 7775 llvm::errs() << " " << I->first << " -> " << I->second->FileName 7776 << "\n"; 7777 } 7778 } 7779 7780 LLVM_DUMP_METHOD void ASTReader::dump() { 7781 llvm::errs() << "*** PCH/ModuleFile Remappings:\n"; 7782 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap); 7783 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap); 7784 dumpModuleIDMap("Global type map", GlobalTypeMap); 7785 dumpModuleIDMap("Global declaration map", GlobalDeclMap); 7786 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap); 7787 dumpModuleIDMap("Global macro map", GlobalMacroMap); 7788 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap); 7789 dumpModuleIDMap("Global selector map", GlobalSelectorMap); 7790 dumpModuleIDMap("Global preprocessed entity map", 7791 GlobalPreprocessedEntityMap); 7792 7793 llvm::errs() << "\n*** PCH/Modules Loaded:"; 7794 for (ModuleFile &M : ModuleMgr) 7795 M.dump(); 7796 } 7797 7798 /// Return the amount of memory used by memory buffers, breaking down 7799 /// by heap-backed versus mmap'ed memory. 7800 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const { 7801 for (ModuleFile &I : ModuleMgr) { 7802 if (llvm::MemoryBuffer *buf = I.Buffer) { 7803 size_t bytes = buf->getBufferSize(); 7804 switch (buf->getBufferKind()) { 7805 case llvm::MemoryBuffer::MemoryBuffer_Malloc: 7806 sizes.malloc_bytes += bytes; 7807 break; 7808 case llvm::MemoryBuffer::MemoryBuffer_MMap: 7809 sizes.mmap_bytes += bytes; 7810 break; 7811 } 7812 } 7813 } 7814 } 7815 7816 void ASTReader::InitializeSema(Sema &S) { 7817 SemaObj = &S; 7818 S.addExternalSource(this); 7819 7820 // Makes sure any declarations that were deserialized "too early" 7821 // still get added to the identifier's declaration chains. 7822 for (uint64_t ID : PreloadedDeclIDs) { 7823 NamedDecl *D = cast<NamedDecl>(GetDecl(ID)); 7824 pushExternalDeclIntoScope(D, D->getDeclName()); 7825 } 7826 PreloadedDeclIDs.clear(); 7827 7828 // FIXME: What happens if these are changed by a module import? 7829 if (!FPPragmaOptions.empty()) { 7830 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS"); 7831 FPOptionsOverride NewOverrides = 7832 FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]); 7833 SemaObj->CurFPFeatures = 7834 NewOverrides.applyOverrides(SemaObj->getLangOpts()); 7835 } 7836 7837 SemaObj->OpenCLFeatures.copy(OpenCLExtensions); 7838 SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap; 7839 SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap; 7840 7841 UpdateSema(); 7842 } 7843 7844 void ASTReader::UpdateSema() { 7845 assert(SemaObj && "no Sema to update"); 7846 7847 // Load the offsets of the declarations that Sema references. 7848 // They will be lazily deserialized when needed. 7849 if (!SemaDeclRefs.empty()) { 7850 assert(SemaDeclRefs.size() % 3 == 0); 7851 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) { 7852 if (!SemaObj->StdNamespace) 7853 SemaObj->StdNamespace = SemaDeclRefs[I]; 7854 if (!SemaObj->StdBadAlloc) 7855 SemaObj->StdBadAlloc = SemaDeclRefs[I+1]; 7856 if (!SemaObj->StdAlignValT) 7857 SemaObj->StdAlignValT = SemaDeclRefs[I+2]; 7858 } 7859 SemaDeclRefs.clear(); 7860 } 7861 7862 // Update the state of pragmas. Use the same API as if we had encountered the 7863 // pragma in the source. 7864 if(OptimizeOffPragmaLocation.isValid()) 7865 SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation); 7866 if (PragmaMSStructState != -1) 7867 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState); 7868 if (PointersToMembersPragmaLocation.isValid()) { 7869 SemaObj->ActOnPragmaMSPointersToMembers( 7870 (LangOptions::PragmaMSPointersToMembersKind) 7871 PragmaMSPointersToMembersState, 7872 PointersToMembersPragmaLocation); 7873 } 7874 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth; 7875 7876 if (PragmaPackCurrentValue) { 7877 // The bottom of the stack might have a default value. It must be adjusted 7878 // to the current value to ensure that the packing state is preserved after 7879 // popping entries that were included/imported from a PCH/module. 7880 bool DropFirst = false; 7881 if (!PragmaPackStack.empty() && 7882 PragmaPackStack.front().Location.isInvalid()) { 7883 assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue && 7884 "Expected a default alignment value"); 7885 SemaObj->PackStack.Stack.emplace_back( 7886 PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue, 7887 SemaObj->PackStack.CurrentPragmaLocation, 7888 PragmaPackStack.front().PushLocation); 7889 DropFirst = true; 7890 } 7891 for (const auto &Entry : 7892 llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0)) 7893 SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value, 7894 Entry.Location, Entry.PushLocation); 7895 if (PragmaPackCurrentLocation.isInvalid()) { 7896 assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue && 7897 "Expected a default alignment value"); 7898 // Keep the current values. 7899 } else { 7900 SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue; 7901 SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation; 7902 } 7903 } 7904 if (FpPragmaCurrentValue) { 7905 // The bottom of the stack might have a default value. It must be adjusted 7906 // to the current value to ensure that fp-pragma state is preserved after 7907 // popping entries that were included/imported from a PCH/module. 7908 bool DropFirst = false; 7909 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) { 7910 assert(FpPragmaStack.front().Value == 7911 SemaObj->FpPragmaStack.DefaultValue && 7912 "Expected a default pragma float_control value"); 7913 SemaObj->FpPragmaStack.Stack.emplace_back( 7914 FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue, 7915 SemaObj->FpPragmaStack.CurrentPragmaLocation, 7916 FpPragmaStack.front().PushLocation); 7917 DropFirst = true; 7918 } 7919 for (const auto &Entry : 7920 llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0)) 7921 SemaObj->FpPragmaStack.Stack.emplace_back( 7922 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation); 7923 if (FpPragmaCurrentLocation.isInvalid()) { 7924 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue && 7925 "Expected a default pragma float_control value"); 7926 // Keep the current values. 7927 } else { 7928 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue; 7929 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation; 7930 } 7931 } 7932 7933 // For non-modular AST files, restore visiblity of modules. 7934 for (auto &Import : ImportedModules) { 7935 if (Import.ImportLoc.isInvalid()) 7936 continue; 7937 if (Module *Imported = getSubmodule(Import.ID)) { 7938 SemaObj->makeModuleVisible(Imported, Import.ImportLoc); 7939 } 7940 } 7941 } 7942 7943 IdentifierInfo *ASTReader::get(StringRef Name) { 7944 // Note that we are loading an identifier. 7945 Deserializing AnIdentifier(this); 7946 7947 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0, 7948 NumIdentifierLookups, 7949 NumIdentifierLookupHits); 7950 7951 // We don't need to do identifier table lookups in C++ modules (we preload 7952 // all interesting declarations, and don't need to use the scope for name 7953 // lookups). Perform the lookup in PCH files, though, since we don't build 7954 // a complete initial identifier table if we're carrying on from a PCH. 7955 if (PP.getLangOpts().CPlusPlus) { 7956 for (auto F : ModuleMgr.pch_modules()) 7957 if (Visitor(*F)) 7958 break; 7959 } else { 7960 // If there is a global index, look there first to determine which modules 7961 // provably do not have any results for this identifier. 7962 GlobalModuleIndex::HitSet Hits; 7963 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 7964 if (!loadGlobalIndex()) { 7965 if (GlobalIndex->lookupIdentifier(Name, Hits)) { 7966 HitsPtr = &Hits; 7967 } 7968 } 7969 7970 ModuleMgr.visit(Visitor, HitsPtr); 7971 } 7972 7973 IdentifierInfo *II = Visitor.getIdentifierInfo(); 7974 markIdentifierUpToDate(II); 7975 return II; 7976 } 7977 7978 namespace clang { 7979 7980 /// An identifier-lookup iterator that enumerates all of the 7981 /// identifiers stored within a set of AST files. 7982 class ASTIdentifierIterator : public IdentifierIterator { 7983 /// The AST reader whose identifiers are being enumerated. 7984 const ASTReader &Reader; 7985 7986 /// The current index into the chain of AST files stored in 7987 /// the AST reader. 7988 unsigned Index; 7989 7990 /// The current position within the identifier lookup table 7991 /// of the current AST file. 7992 ASTIdentifierLookupTable::key_iterator Current; 7993 7994 /// The end position within the identifier lookup table of 7995 /// the current AST file. 7996 ASTIdentifierLookupTable::key_iterator End; 7997 7998 /// Whether to skip any modules in the ASTReader. 7999 bool SkipModules; 8000 8001 public: 8002 explicit ASTIdentifierIterator(const ASTReader &Reader, 8003 bool SkipModules = false); 8004 8005 StringRef Next() override; 8006 }; 8007 8008 } // namespace clang 8009 8010 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader, 8011 bool SkipModules) 8012 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) { 8013 } 8014 8015 StringRef ASTIdentifierIterator::Next() { 8016 while (Current == End) { 8017 // If we have exhausted all of our AST files, we're done. 8018 if (Index == 0) 8019 return StringRef(); 8020 8021 --Index; 8022 ModuleFile &F = Reader.ModuleMgr[Index]; 8023 if (SkipModules && F.isModule()) 8024 continue; 8025 8026 ASTIdentifierLookupTable *IdTable = 8027 (ASTIdentifierLookupTable *)F.IdentifierLookupTable; 8028 Current = IdTable->key_begin(); 8029 End = IdTable->key_end(); 8030 } 8031 8032 // We have any identifiers remaining in the current AST file; return 8033 // the next one. 8034 StringRef Result = *Current; 8035 ++Current; 8036 return Result; 8037 } 8038 8039 namespace { 8040 8041 /// A utility for appending two IdentifierIterators. 8042 class ChainedIdentifierIterator : public IdentifierIterator { 8043 std::unique_ptr<IdentifierIterator> Current; 8044 std::unique_ptr<IdentifierIterator> Queued; 8045 8046 public: 8047 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First, 8048 std::unique_ptr<IdentifierIterator> Second) 8049 : Current(std::move(First)), Queued(std::move(Second)) {} 8050 8051 StringRef Next() override { 8052 if (!Current) 8053 return StringRef(); 8054 8055 StringRef result = Current->Next(); 8056 if (!result.empty()) 8057 return result; 8058 8059 // Try the queued iterator, which may itself be empty. 8060 Current.reset(); 8061 std::swap(Current, Queued); 8062 return Next(); 8063 } 8064 }; 8065 8066 } // namespace 8067 8068 IdentifierIterator *ASTReader::getIdentifiers() { 8069 if (!loadGlobalIndex()) { 8070 std::unique_ptr<IdentifierIterator> ReaderIter( 8071 new ASTIdentifierIterator(*this, /*SkipModules=*/true)); 8072 std::unique_ptr<IdentifierIterator> ModulesIter( 8073 GlobalIndex->createIdentifierIterator()); 8074 return new ChainedIdentifierIterator(std::move(ReaderIter), 8075 std::move(ModulesIter)); 8076 } 8077 8078 return new ASTIdentifierIterator(*this); 8079 } 8080 8081 namespace clang { 8082 namespace serialization { 8083 8084 class ReadMethodPoolVisitor { 8085 ASTReader &Reader; 8086 Selector Sel; 8087 unsigned PriorGeneration; 8088 unsigned InstanceBits = 0; 8089 unsigned FactoryBits = 0; 8090 bool InstanceHasMoreThanOneDecl = false; 8091 bool FactoryHasMoreThanOneDecl = false; 8092 SmallVector<ObjCMethodDecl *, 4> InstanceMethods; 8093 SmallVector<ObjCMethodDecl *, 4> FactoryMethods; 8094 8095 public: 8096 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel, 8097 unsigned PriorGeneration) 8098 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {} 8099 8100 bool operator()(ModuleFile &M) { 8101 if (!M.SelectorLookupTable) 8102 return false; 8103 8104 // If we've already searched this module file, skip it now. 8105 if (M.Generation <= PriorGeneration) 8106 return true; 8107 8108 ++Reader.NumMethodPoolTableLookups; 8109 ASTSelectorLookupTable *PoolTable 8110 = (ASTSelectorLookupTable*)M.SelectorLookupTable; 8111 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel); 8112 if (Pos == PoolTable->end()) 8113 return false; 8114 8115 ++Reader.NumMethodPoolTableHits; 8116 ++Reader.NumSelectorsRead; 8117 // FIXME: Not quite happy with the statistics here. We probably should 8118 // disable this tracking when called via LoadSelector. 8119 // Also, should entries without methods count as misses? 8120 ++Reader.NumMethodPoolEntriesRead; 8121 ASTSelectorLookupTrait::data_type Data = *Pos; 8122 if (Reader.DeserializationListener) 8123 Reader.DeserializationListener->SelectorRead(Data.ID, Sel); 8124 8125 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end()); 8126 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end()); 8127 InstanceBits = Data.InstanceBits; 8128 FactoryBits = Data.FactoryBits; 8129 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl; 8130 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl; 8131 return true; 8132 } 8133 8134 /// Retrieve the instance methods found by this visitor. 8135 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const { 8136 return InstanceMethods; 8137 } 8138 8139 /// Retrieve the instance methods found by this visitor. 8140 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const { 8141 return FactoryMethods; 8142 } 8143 8144 unsigned getInstanceBits() const { return InstanceBits; } 8145 unsigned getFactoryBits() const { return FactoryBits; } 8146 8147 bool instanceHasMoreThanOneDecl() const { 8148 return InstanceHasMoreThanOneDecl; 8149 } 8150 8151 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; } 8152 }; 8153 8154 } // namespace serialization 8155 } // namespace clang 8156 8157 /// Add the given set of methods to the method list. 8158 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods, 8159 ObjCMethodList &List) { 8160 for (unsigned I = 0, N = Methods.size(); I != N; ++I) { 8161 S.addMethodToGlobalList(&List, Methods[I]); 8162 } 8163 } 8164 8165 void ASTReader::ReadMethodPool(Selector Sel) { 8166 // Get the selector generation and update it to the current generation. 8167 unsigned &Generation = SelectorGeneration[Sel]; 8168 unsigned PriorGeneration = Generation; 8169 Generation = getGeneration(); 8170 SelectorOutOfDate[Sel] = false; 8171 8172 // Search for methods defined with this selector. 8173 ++NumMethodPoolLookups; 8174 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration); 8175 ModuleMgr.visit(Visitor); 8176 8177 if (Visitor.getInstanceMethods().empty() && 8178 Visitor.getFactoryMethods().empty()) 8179 return; 8180 8181 ++NumMethodPoolHits; 8182 8183 if (!getSema()) 8184 return; 8185 8186 Sema &S = *getSema(); 8187 Sema::GlobalMethodPool::iterator Pos 8188 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first; 8189 8190 Pos->second.first.setBits(Visitor.getInstanceBits()); 8191 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl()); 8192 Pos->second.second.setBits(Visitor.getFactoryBits()); 8193 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl()); 8194 8195 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since 8196 // when building a module we keep every method individually and may need to 8197 // update hasMoreThanOneDecl as we add the methods. 8198 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first); 8199 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second); 8200 } 8201 8202 void ASTReader::updateOutOfDateSelector(Selector Sel) { 8203 if (SelectorOutOfDate[Sel]) 8204 ReadMethodPool(Sel); 8205 } 8206 8207 void ASTReader::ReadKnownNamespaces( 8208 SmallVectorImpl<NamespaceDecl *> &Namespaces) { 8209 Namespaces.clear(); 8210 8211 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) { 8212 if (NamespaceDecl *Namespace 8213 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I]))) 8214 Namespaces.push_back(Namespace); 8215 } 8216 } 8217 8218 void ASTReader::ReadUndefinedButUsed( 8219 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) { 8220 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) { 8221 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++])); 8222 SourceLocation Loc = 8223 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]); 8224 Undefined.insert(std::make_pair(D, Loc)); 8225 } 8226 } 8227 8228 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector< 8229 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> & 8230 Exprs) { 8231 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) { 8232 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++])); 8233 uint64_t Count = DelayedDeleteExprs[Idx++]; 8234 for (uint64_t C = 0; C < Count; ++C) { 8235 SourceLocation DeleteLoc = 8236 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]); 8237 const bool IsArrayForm = DelayedDeleteExprs[Idx++]; 8238 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm)); 8239 } 8240 } 8241 } 8242 8243 void ASTReader::ReadTentativeDefinitions( 8244 SmallVectorImpl<VarDecl *> &TentativeDefs) { 8245 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { 8246 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I])); 8247 if (Var) 8248 TentativeDefs.push_back(Var); 8249 } 8250 TentativeDefinitions.clear(); 8251 } 8252 8253 void ASTReader::ReadUnusedFileScopedDecls( 8254 SmallVectorImpl<const DeclaratorDecl *> &Decls) { 8255 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) { 8256 DeclaratorDecl *D 8257 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I])); 8258 if (D) 8259 Decls.push_back(D); 8260 } 8261 UnusedFileScopedDecls.clear(); 8262 } 8263 8264 void ASTReader::ReadDelegatingConstructors( 8265 SmallVectorImpl<CXXConstructorDecl *> &Decls) { 8266 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) { 8267 CXXConstructorDecl *D 8268 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I])); 8269 if (D) 8270 Decls.push_back(D); 8271 } 8272 DelegatingCtorDecls.clear(); 8273 } 8274 8275 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) { 8276 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) { 8277 TypedefNameDecl *D 8278 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])); 8279 if (D) 8280 Decls.push_back(D); 8281 } 8282 ExtVectorDecls.clear(); 8283 } 8284 8285 void ASTReader::ReadUnusedLocalTypedefNameCandidates( 8286 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) { 8287 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N; 8288 ++I) { 8289 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>( 8290 GetDecl(UnusedLocalTypedefNameCandidates[I])); 8291 if (D) 8292 Decls.insert(D); 8293 } 8294 UnusedLocalTypedefNameCandidates.clear(); 8295 } 8296 8297 void ASTReader::ReadDeclsToCheckForDeferredDiags( 8298 llvm::SmallVector<Decl *, 4> &Decls) { 8299 for (unsigned I = 0, N = DeclsToCheckForDeferredDiags.size(); I != N; 8300 ++I) { 8301 auto *D = dyn_cast_or_null<Decl>( 8302 GetDecl(DeclsToCheckForDeferredDiags[I])); 8303 if (D) 8304 Decls.push_back(D); 8305 } 8306 DeclsToCheckForDeferredDiags.clear(); 8307 } 8308 8309 8310 void ASTReader::ReadReferencedSelectors( 8311 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) { 8312 if (ReferencedSelectorsData.empty()) 8313 return; 8314 8315 // If there are @selector references added them to its pool. This is for 8316 // implementation of -Wselector. 8317 unsigned int DataSize = ReferencedSelectorsData.size()-1; 8318 unsigned I = 0; 8319 while (I < DataSize) { 8320 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]); 8321 SourceLocation SelLoc 8322 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]); 8323 Sels.push_back(std::make_pair(Sel, SelLoc)); 8324 } 8325 ReferencedSelectorsData.clear(); 8326 } 8327 8328 void ASTReader::ReadWeakUndeclaredIdentifiers( 8329 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) { 8330 if (WeakUndeclaredIdentifiers.empty()) 8331 return; 8332 8333 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) { 8334 IdentifierInfo *WeakId 8335 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8336 IdentifierInfo *AliasId 8337 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8338 SourceLocation Loc 8339 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]); 8340 bool Used = WeakUndeclaredIdentifiers[I++]; 8341 WeakInfo WI(AliasId, Loc); 8342 WI.setUsed(Used); 8343 WeakIDs.push_back(std::make_pair(WeakId, WI)); 8344 } 8345 WeakUndeclaredIdentifiers.clear(); 8346 } 8347 8348 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) { 8349 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) { 8350 ExternalVTableUse VT; 8351 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); 8352 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]); 8353 VT.DefinitionRequired = VTableUses[Idx++]; 8354 VTables.push_back(VT); 8355 } 8356 8357 VTableUses.clear(); 8358 } 8359 8360 void ASTReader::ReadPendingInstantiations( 8361 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) { 8362 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) { 8363 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++])); 8364 SourceLocation Loc 8365 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]); 8366 8367 Pending.push_back(std::make_pair(D, Loc)); 8368 } 8369 PendingInstantiations.clear(); 8370 } 8371 8372 void ASTReader::ReadLateParsedTemplates( 8373 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>> 8374 &LPTMap) { 8375 for (auto &LPT : LateParsedTemplates) { 8376 ModuleFile *FMod = LPT.first; 8377 RecordDataImpl &LateParsed = LPT.second; 8378 for (unsigned Idx = 0, N = LateParsed.size(); Idx < N; 8379 /* In loop */) { 8380 FunctionDecl *FD = 8381 cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++])); 8382 8383 auto LT = std::make_unique<LateParsedTemplate>(); 8384 LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]); 8385 8386 ModuleFile *F = getOwningModuleFile(LT->D); 8387 assert(F && "No module"); 8388 8389 unsigned TokN = LateParsed[Idx++]; 8390 LT->Toks.reserve(TokN); 8391 for (unsigned T = 0; T < TokN; ++T) 8392 LT->Toks.push_back(ReadToken(*F, LateParsed, Idx)); 8393 8394 LPTMap.insert(std::make_pair(FD, std::move(LT))); 8395 } 8396 } 8397 } 8398 8399 void ASTReader::LoadSelector(Selector Sel) { 8400 // It would be complicated to avoid reading the methods anyway. So don't. 8401 ReadMethodPool(Sel); 8402 } 8403 8404 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) { 8405 assert(ID && "Non-zero identifier ID required"); 8406 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); 8407 IdentifiersLoaded[ID - 1] = II; 8408 if (DeserializationListener) 8409 DeserializationListener->IdentifierRead(ID, II); 8410 } 8411 8412 /// Set the globally-visible declarations associated with the given 8413 /// identifier. 8414 /// 8415 /// If the AST reader is currently in a state where the given declaration IDs 8416 /// cannot safely be resolved, they are queued until it is safe to resolve 8417 /// them. 8418 /// 8419 /// \param II an IdentifierInfo that refers to one or more globally-visible 8420 /// declarations. 8421 /// 8422 /// \param DeclIDs the set of declaration IDs with the name @p II that are 8423 /// visible at global scope. 8424 /// 8425 /// \param Decls if non-null, this vector will be populated with the set of 8426 /// deserialized declarations. These declarations will not be pushed into 8427 /// scope. 8428 void 8429 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, 8430 const SmallVectorImpl<uint32_t> &DeclIDs, 8431 SmallVectorImpl<Decl *> *Decls) { 8432 if (NumCurrentElementsDeserializing && !Decls) { 8433 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end()); 8434 return; 8435 } 8436 8437 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { 8438 if (!SemaObj) { 8439 // Queue this declaration so that it will be added to the 8440 // translation unit scope and identifier's declaration chain 8441 // once a Sema object is known. 8442 PreloadedDeclIDs.push_back(DeclIDs[I]); 8443 continue; 8444 } 8445 8446 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); 8447 8448 // If we're simply supposed to record the declarations, do so now. 8449 if (Decls) { 8450 Decls->push_back(D); 8451 continue; 8452 } 8453 8454 // Introduce this declaration into the translation-unit scope 8455 // and add it to the declaration chain for this identifier, so 8456 // that (unqualified) name lookup will find it. 8457 pushExternalDeclIntoScope(D, II); 8458 } 8459 } 8460 8461 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) { 8462 if (ID == 0) 8463 return nullptr; 8464 8465 if (IdentifiersLoaded.empty()) { 8466 Error("no identifier table in AST file"); 8467 return nullptr; 8468 } 8469 8470 ID -= 1; 8471 if (!IdentifiersLoaded[ID]) { 8472 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1); 8473 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map"); 8474 ModuleFile *M = I->second; 8475 unsigned Index = ID - M->BaseIdentifierID; 8476 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index]; 8477 8478 // All of the strings in the AST file are preceded by a 16-bit length. 8479 // Extract that 16-bit length to avoid having to execute strlen(). 8480 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as 8481 // unsigned integers. This is important to avoid integer overflow when 8482 // we cast them to 'unsigned'. 8483 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2; 8484 unsigned StrLen = (((unsigned) StrLenPtr[0]) 8485 | (((unsigned) StrLenPtr[1]) << 8)) - 1; 8486 auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen)); 8487 IdentifiersLoaded[ID] = &II; 8488 markIdentifierFromAST(*this, II); 8489 if (DeserializationListener) 8490 DeserializationListener->IdentifierRead(ID + 1, &II); 8491 } 8492 8493 return IdentifiersLoaded[ID]; 8494 } 8495 8496 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) { 8497 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID)); 8498 } 8499 8500 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) { 8501 if (LocalID < NUM_PREDEF_IDENT_IDS) 8502 return LocalID; 8503 8504 if (!M.ModuleOffsetMap.empty()) 8505 ReadModuleOffsetMap(M); 8506 8507 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8508 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS); 8509 assert(I != M.IdentifierRemap.end() 8510 && "Invalid index into identifier index remap"); 8511 8512 return LocalID + I->second; 8513 } 8514 8515 MacroInfo *ASTReader::getMacro(MacroID ID) { 8516 if (ID == 0) 8517 return nullptr; 8518 8519 if (MacrosLoaded.empty()) { 8520 Error("no macro table in AST file"); 8521 return nullptr; 8522 } 8523 8524 ID -= NUM_PREDEF_MACRO_IDS; 8525 if (!MacrosLoaded[ID]) { 8526 GlobalMacroMapType::iterator I 8527 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS); 8528 assert(I != GlobalMacroMap.end() && "Corrupted global macro map"); 8529 ModuleFile *M = I->second; 8530 unsigned Index = ID - M->BaseMacroID; 8531 MacrosLoaded[ID] = 8532 ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]); 8533 8534 if (DeserializationListener) 8535 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS, 8536 MacrosLoaded[ID]); 8537 } 8538 8539 return MacrosLoaded[ID]; 8540 } 8541 8542 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) { 8543 if (LocalID < NUM_PREDEF_MACRO_IDS) 8544 return LocalID; 8545 8546 if (!M.ModuleOffsetMap.empty()) 8547 ReadModuleOffsetMap(M); 8548 8549 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8550 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS); 8551 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap"); 8552 8553 return LocalID + I->second; 8554 } 8555 8556 serialization::SubmoduleID 8557 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) { 8558 if (LocalID < NUM_PREDEF_SUBMODULE_IDS) 8559 return LocalID; 8560 8561 if (!M.ModuleOffsetMap.empty()) 8562 ReadModuleOffsetMap(M); 8563 8564 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8565 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS); 8566 assert(I != M.SubmoduleRemap.end() 8567 && "Invalid index into submodule index remap"); 8568 8569 return LocalID + I->second; 8570 } 8571 8572 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) { 8573 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) { 8574 assert(GlobalID == 0 && "Unhandled global submodule ID"); 8575 return nullptr; 8576 } 8577 8578 if (GlobalID > SubmodulesLoaded.size()) { 8579 Error("submodule ID out of range in AST file"); 8580 return nullptr; 8581 } 8582 8583 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS]; 8584 } 8585 8586 Module *ASTReader::getModule(unsigned ID) { 8587 return getSubmodule(ID); 8588 } 8589 8590 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) { 8591 if (ID & 1) { 8592 // It's a module, look it up by submodule ID. 8593 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1)); 8594 return I == GlobalSubmoduleMap.end() ? nullptr : I->second; 8595 } else { 8596 // It's a prefix (preamble, PCH, ...). Look it up by index. 8597 unsigned IndexFromEnd = ID >> 1; 8598 assert(IndexFromEnd && "got reference to unknown module file"); 8599 return getModuleManager().pch_modules().end()[-IndexFromEnd]; 8600 } 8601 } 8602 8603 unsigned ASTReader::getModuleFileID(ModuleFile *F) { 8604 if (!F) 8605 return 1; 8606 8607 // For a file representing a module, use the submodule ID of the top-level 8608 // module as the file ID. For any other kind of file, the number of such 8609 // files loaded beforehand will be the same on reload. 8610 // FIXME: Is this true even if we have an explicit module file and a PCH? 8611 if (F->isModule()) 8612 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1; 8613 8614 auto PCHModules = getModuleManager().pch_modules(); 8615 auto I = llvm::find(PCHModules, F); 8616 assert(I != PCHModules.end() && "emitting reference to unknown file"); 8617 return (I - PCHModules.end()) << 1; 8618 } 8619 8620 llvm::Optional<ASTSourceDescriptor> 8621 ASTReader::getSourceDescriptor(unsigned ID) { 8622 if (Module *M = getSubmodule(ID)) 8623 return ASTSourceDescriptor(*M); 8624 8625 // If there is only a single PCH, return it instead. 8626 // Chained PCH are not supported. 8627 const auto &PCHChain = ModuleMgr.pch_modules(); 8628 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) { 8629 ModuleFile &MF = ModuleMgr.getPrimaryModule(); 8630 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName); 8631 StringRef FileName = llvm::sys::path::filename(MF.FileName); 8632 return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName, 8633 MF.Signature); 8634 } 8635 return None; 8636 } 8637 8638 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) { 8639 auto I = DefinitionSource.find(FD); 8640 if (I == DefinitionSource.end()) 8641 return EK_ReplyHazy; 8642 return I->second ? EK_Never : EK_Always; 8643 } 8644 8645 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) { 8646 return DecodeSelector(getGlobalSelectorID(M, LocalID)); 8647 } 8648 8649 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) { 8650 if (ID == 0) 8651 return Selector(); 8652 8653 if (ID > SelectorsLoaded.size()) { 8654 Error("selector ID out of range in AST file"); 8655 return Selector(); 8656 } 8657 8658 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) { 8659 // Load this selector from the selector table. 8660 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID); 8661 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map"); 8662 ModuleFile &M = *I->second; 8663 ASTSelectorLookupTrait Trait(*this, M); 8664 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS; 8665 SelectorsLoaded[ID - 1] = 8666 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0); 8667 if (DeserializationListener) 8668 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 8669 } 8670 8671 return SelectorsLoaded[ID - 1]; 8672 } 8673 8674 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) { 8675 return DecodeSelector(ID); 8676 } 8677 8678 uint32_t ASTReader::GetNumExternalSelectors() { 8679 // ID 0 (the null selector) is considered an external selector. 8680 return getTotalNumSelectors() + 1; 8681 } 8682 8683 serialization::SelectorID 8684 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const { 8685 if (LocalID < NUM_PREDEF_SELECTOR_IDS) 8686 return LocalID; 8687 8688 if (!M.ModuleOffsetMap.empty()) 8689 ReadModuleOffsetMap(M); 8690 8691 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8692 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS); 8693 assert(I != M.SelectorRemap.end() 8694 && "Invalid index into selector index remap"); 8695 8696 return LocalID + I->second; 8697 } 8698 8699 DeclarationNameLoc 8700 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) { 8701 DeclarationNameLoc DNLoc; 8702 switch (Name.getNameKind()) { 8703 case DeclarationName::CXXConstructorName: 8704 case DeclarationName::CXXDestructorName: 8705 case DeclarationName::CXXConversionFunctionName: 8706 DNLoc.NamedType.TInfo = readTypeSourceInfo(); 8707 break; 8708 8709 case DeclarationName::CXXOperatorName: 8710 DNLoc.CXXOperatorName.BeginOpNameLoc 8711 = readSourceLocation().getRawEncoding(); 8712 DNLoc.CXXOperatorName.EndOpNameLoc 8713 = readSourceLocation().getRawEncoding(); 8714 break; 8715 8716 case DeclarationName::CXXLiteralOperatorName: 8717 DNLoc.CXXLiteralOperatorName.OpNameLoc 8718 = readSourceLocation().getRawEncoding(); 8719 break; 8720 8721 case DeclarationName::Identifier: 8722 case DeclarationName::ObjCZeroArgSelector: 8723 case DeclarationName::ObjCOneArgSelector: 8724 case DeclarationName::ObjCMultiArgSelector: 8725 case DeclarationName::CXXUsingDirective: 8726 case DeclarationName::CXXDeductionGuideName: 8727 break; 8728 } 8729 return DNLoc; 8730 } 8731 8732 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() { 8733 DeclarationNameInfo NameInfo; 8734 NameInfo.setName(readDeclarationName()); 8735 NameInfo.setLoc(readSourceLocation()); 8736 NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName())); 8737 return NameInfo; 8738 } 8739 8740 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) { 8741 Info.QualifierLoc = readNestedNameSpecifierLoc(); 8742 unsigned NumTPLists = readInt(); 8743 Info.NumTemplParamLists = NumTPLists; 8744 if (NumTPLists) { 8745 Info.TemplParamLists = 8746 new (getContext()) TemplateParameterList *[NumTPLists]; 8747 for (unsigned i = 0; i != NumTPLists; ++i) 8748 Info.TemplParamLists[i] = readTemplateParameterList(); 8749 } 8750 } 8751 8752 TemplateParameterList * 8753 ASTRecordReader::readTemplateParameterList() { 8754 SourceLocation TemplateLoc = readSourceLocation(); 8755 SourceLocation LAngleLoc = readSourceLocation(); 8756 SourceLocation RAngleLoc = readSourceLocation(); 8757 8758 unsigned NumParams = readInt(); 8759 SmallVector<NamedDecl *, 16> Params; 8760 Params.reserve(NumParams); 8761 while (NumParams--) 8762 Params.push_back(readDeclAs<NamedDecl>()); 8763 8764 bool HasRequiresClause = readBool(); 8765 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr; 8766 8767 TemplateParameterList *TemplateParams = TemplateParameterList::Create( 8768 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause); 8769 return TemplateParams; 8770 } 8771 8772 void ASTRecordReader::readTemplateArgumentList( 8773 SmallVectorImpl<TemplateArgument> &TemplArgs, 8774 bool Canonicalize) { 8775 unsigned NumTemplateArgs = readInt(); 8776 TemplArgs.reserve(NumTemplateArgs); 8777 while (NumTemplateArgs--) 8778 TemplArgs.push_back(readTemplateArgument(Canonicalize)); 8779 } 8780 8781 /// Read a UnresolvedSet structure. 8782 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) { 8783 unsigned NumDecls = readInt(); 8784 Set.reserve(getContext(), NumDecls); 8785 while (NumDecls--) { 8786 DeclID ID = readDeclID(); 8787 AccessSpecifier AS = (AccessSpecifier) readInt(); 8788 Set.addLazyDecl(getContext(), ID, AS); 8789 } 8790 } 8791 8792 CXXBaseSpecifier 8793 ASTRecordReader::readCXXBaseSpecifier() { 8794 bool isVirtual = readBool(); 8795 bool isBaseOfClass = readBool(); 8796 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt()); 8797 bool inheritConstructors = readBool(); 8798 TypeSourceInfo *TInfo = readTypeSourceInfo(); 8799 SourceRange Range = readSourceRange(); 8800 SourceLocation EllipsisLoc = readSourceLocation(); 8801 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo, 8802 EllipsisLoc); 8803 Result.setInheritConstructors(inheritConstructors); 8804 return Result; 8805 } 8806 8807 CXXCtorInitializer ** 8808 ASTRecordReader::readCXXCtorInitializers() { 8809 ASTContext &Context = getContext(); 8810 unsigned NumInitializers = readInt(); 8811 assert(NumInitializers && "wrote ctor initializers but have no inits"); 8812 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers]; 8813 for (unsigned i = 0; i != NumInitializers; ++i) { 8814 TypeSourceInfo *TInfo = nullptr; 8815 bool IsBaseVirtual = false; 8816 FieldDecl *Member = nullptr; 8817 IndirectFieldDecl *IndirectMember = nullptr; 8818 8819 CtorInitializerType Type = (CtorInitializerType) readInt(); 8820 switch (Type) { 8821 case CTOR_INITIALIZER_BASE: 8822 TInfo = readTypeSourceInfo(); 8823 IsBaseVirtual = readBool(); 8824 break; 8825 8826 case CTOR_INITIALIZER_DELEGATING: 8827 TInfo = readTypeSourceInfo(); 8828 break; 8829 8830 case CTOR_INITIALIZER_MEMBER: 8831 Member = readDeclAs<FieldDecl>(); 8832 break; 8833 8834 case CTOR_INITIALIZER_INDIRECT_MEMBER: 8835 IndirectMember = readDeclAs<IndirectFieldDecl>(); 8836 break; 8837 } 8838 8839 SourceLocation MemberOrEllipsisLoc = readSourceLocation(); 8840 Expr *Init = readExpr(); 8841 SourceLocation LParenLoc = readSourceLocation(); 8842 SourceLocation RParenLoc = readSourceLocation(); 8843 8844 CXXCtorInitializer *BOMInit; 8845 if (Type == CTOR_INITIALIZER_BASE) 8846 BOMInit = new (Context) 8847 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init, 8848 RParenLoc, MemberOrEllipsisLoc); 8849 else if (Type == CTOR_INITIALIZER_DELEGATING) 8850 BOMInit = new (Context) 8851 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc); 8852 else if (Member) 8853 BOMInit = new (Context) 8854 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc, 8855 Init, RParenLoc); 8856 else 8857 BOMInit = new (Context) 8858 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc, 8859 LParenLoc, Init, RParenLoc); 8860 8861 if (/*IsWritten*/readBool()) { 8862 unsigned SourceOrder = readInt(); 8863 BOMInit->setSourceOrder(SourceOrder); 8864 } 8865 8866 CtorInitializers[i] = BOMInit; 8867 } 8868 8869 return CtorInitializers; 8870 } 8871 8872 NestedNameSpecifierLoc 8873 ASTRecordReader::readNestedNameSpecifierLoc() { 8874 ASTContext &Context = getContext(); 8875 unsigned N = readInt(); 8876 NestedNameSpecifierLocBuilder Builder; 8877 for (unsigned I = 0; I != N; ++I) { 8878 auto Kind = readNestedNameSpecifierKind(); 8879 switch (Kind) { 8880 case NestedNameSpecifier::Identifier: { 8881 IdentifierInfo *II = readIdentifier(); 8882 SourceRange Range = readSourceRange(); 8883 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd()); 8884 break; 8885 } 8886 8887 case NestedNameSpecifier::Namespace: { 8888 NamespaceDecl *NS = readDeclAs<NamespaceDecl>(); 8889 SourceRange Range = readSourceRange(); 8890 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd()); 8891 break; 8892 } 8893 8894 case NestedNameSpecifier::NamespaceAlias: { 8895 NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>(); 8896 SourceRange Range = readSourceRange(); 8897 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd()); 8898 break; 8899 } 8900 8901 case NestedNameSpecifier::TypeSpec: 8902 case NestedNameSpecifier::TypeSpecWithTemplate: { 8903 bool Template = readBool(); 8904 TypeSourceInfo *T = readTypeSourceInfo(); 8905 if (!T) 8906 return NestedNameSpecifierLoc(); 8907 SourceLocation ColonColonLoc = readSourceLocation(); 8908 8909 // FIXME: 'template' keyword location not saved anywhere, so we fake it. 8910 Builder.Extend(Context, 8911 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(), 8912 T->getTypeLoc(), ColonColonLoc); 8913 break; 8914 } 8915 8916 case NestedNameSpecifier::Global: { 8917 SourceLocation ColonColonLoc = readSourceLocation(); 8918 Builder.MakeGlobal(Context, ColonColonLoc); 8919 break; 8920 } 8921 8922 case NestedNameSpecifier::Super: { 8923 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>(); 8924 SourceRange Range = readSourceRange(); 8925 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd()); 8926 break; 8927 } 8928 } 8929 } 8930 8931 return Builder.getWithLocInContext(Context); 8932 } 8933 8934 SourceRange 8935 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record, 8936 unsigned &Idx) { 8937 SourceLocation beg = ReadSourceLocation(F, Record, Idx); 8938 SourceLocation end = ReadSourceLocation(F, Record, Idx); 8939 return SourceRange(beg, end); 8940 } 8941 8942 static llvm::FixedPointSemantics 8943 ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record, 8944 unsigned &Idx) { 8945 unsigned Width = Record[Idx++]; 8946 unsigned Scale = Record[Idx++]; 8947 uint64_t Tmp = Record[Idx++]; 8948 bool IsSigned = Tmp & 0x1; 8949 bool IsSaturated = Tmp & 0x2; 8950 bool HasUnsignedPadding = Tmp & 0x4; 8951 return llvm::FixedPointSemantics(Width, Scale, IsSigned, IsSaturated, 8952 HasUnsignedPadding); 8953 } 8954 8955 APValue ASTRecordReader::readAPValue() { 8956 auto Kind = static_cast<APValue::ValueKind>(asImpl().readUInt32()); 8957 switch (Kind) { 8958 case APValue::None: 8959 return APValue(); 8960 case APValue::Indeterminate: 8961 return APValue::IndeterminateValue(); 8962 case APValue::Int: 8963 return APValue(asImpl().readAPSInt()); 8964 case APValue::Float: { 8965 const llvm::fltSemantics &FloatSema = llvm::APFloatBase::EnumToSemantics( 8966 static_cast<llvm::APFloatBase::Semantics>(asImpl().readUInt32())); 8967 return APValue(asImpl().readAPFloat(FloatSema)); 8968 } 8969 case APValue::FixedPoint: { 8970 llvm::FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx); 8971 return APValue(llvm::APFixedPoint(readAPInt(), FPSema)); 8972 } 8973 case APValue::ComplexInt: { 8974 llvm::APSInt First = asImpl().readAPSInt(); 8975 return APValue(std::move(First), asImpl().readAPSInt()); 8976 } 8977 case APValue::ComplexFloat: { 8978 const llvm::fltSemantics &FloatSema = llvm::APFloatBase::EnumToSemantics( 8979 static_cast<llvm::APFloatBase::Semantics>(asImpl().readUInt32())); 8980 llvm::APFloat First = readAPFloat(FloatSema); 8981 return APValue(std::move(First), asImpl().readAPFloat(FloatSema)); 8982 } 8983 case APValue::Vector: { 8984 APValue Result; 8985 Result.MakeVector(); 8986 unsigned Length = asImpl().readUInt32(); 8987 (void)Result.setVectorUninit(Length); 8988 for (unsigned LoopIdx = 0; LoopIdx < Length; LoopIdx++) 8989 Result.getVectorElt(LoopIdx) = asImpl().readAPValue(); 8990 return Result; 8991 } 8992 case APValue::Array: { 8993 APValue Result; 8994 unsigned InitLength = asImpl().readUInt32(); 8995 unsigned TotalLength = asImpl().readUInt32(); 8996 Result.MakeArray(InitLength, TotalLength); 8997 for (unsigned LoopIdx = 0; LoopIdx < InitLength; LoopIdx++) 8998 Result.getArrayInitializedElt(LoopIdx) = asImpl().readAPValue(); 8999 if (Result.hasArrayFiller()) 9000 Result.getArrayFiller() = asImpl().readAPValue(); 9001 return Result; 9002 } 9003 case APValue::Struct: { 9004 APValue Result; 9005 unsigned BasesLength = asImpl().readUInt32(); 9006 unsigned FieldsLength = asImpl().readUInt32(); 9007 Result.MakeStruct(BasesLength, FieldsLength); 9008 for (unsigned LoopIdx = 0; LoopIdx < BasesLength; LoopIdx++) 9009 Result.getStructBase(LoopIdx) = asImpl().readAPValue(); 9010 for (unsigned LoopIdx = 0; LoopIdx < FieldsLength; LoopIdx++) 9011 Result.getStructField(LoopIdx) = asImpl().readAPValue(); 9012 return Result; 9013 } 9014 case APValue::Union: { 9015 auto *FDecl = asImpl().readDeclAs<FieldDecl>(); 9016 APValue Value = asImpl().readAPValue(); 9017 return APValue(FDecl, std::move(Value)); 9018 } 9019 case APValue::AddrLabelDiff: { 9020 auto *LHS = cast<AddrLabelExpr>(asImpl().readExpr()); 9021 auto *RHS = cast<AddrLabelExpr>(asImpl().readExpr()); 9022 return APValue(LHS, RHS); 9023 } 9024 case APValue::MemberPointer: { 9025 APValue Result; 9026 bool IsDerived = asImpl().readUInt32(); 9027 auto *Member = asImpl().readDeclAs<ValueDecl>(); 9028 unsigned PathSize = asImpl().readUInt32(); 9029 const CXXRecordDecl **PathArray = 9030 Result.setMemberPointerUninit(Member, IsDerived, PathSize).data(); 9031 for (unsigned LoopIdx = 0; LoopIdx < PathSize; LoopIdx++) 9032 PathArray[LoopIdx] = 9033 asImpl().readDeclAs<const CXXRecordDecl>()->getCanonicalDecl(); 9034 return Result; 9035 } 9036 case APValue::LValue: { 9037 uint64_t Bits = asImpl().readUInt32(); 9038 bool HasLValuePath = Bits & 0x1; 9039 bool IsLValueOnePastTheEnd = Bits & 0x2; 9040 bool IsExpr = Bits & 0x4; 9041 bool IsTypeInfo = Bits & 0x8; 9042 bool IsNullPtr = Bits & 0x10; 9043 bool HasBase = Bits & 0x20; 9044 APValue::LValueBase Base; 9045 QualType ElemTy; 9046 assert((!IsExpr || !IsTypeInfo) && "LValueBase cannot be both"); 9047 if (HasBase) { 9048 if (!IsTypeInfo) { 9049 unsigned CallIndex = asImpl().readUInt32(); 9050 unsigned Version = asImpl().readUInt32(); 9051 if (IsExpr) { 9052 Base = APValue::LValueBase(asImpl().readExpr(), CallIndex, Version); 9053 ElemTy = Base.get<const Expr *>()->getType(); 9054 } else { 9055 Base = APValue::LValueBase(asImpl().readDeclAs<const ValueDecl>(), 9056 CallIndex, Version); 9057 ElemTy = Base.get<const ValueDecl *>()->getType(); 9058 } 9059 } else { 9060 QualType TypeInfo = asImpl().readType(); 9061 QualType Type = asImpl().readType(); 9062 Base = APValue::LValueBase::getTypeInfo( 9063 TypeInfoLValue(TypeInfo.getTypePtr()), Type); 9064 Base.getTypeInfoType(); 9065 } 9066 } 9067 CharUnits Offset = CharUnits::fromQuantity(asImpl().readUInt32()); 9068 unsigned PathLength = asImpl().readUInt32(); 9069 APValue Result; 9070 Result.MakeLValue(); 9071 if (HasLValuePath) { 9072 APValue::LValuePathEntry *Path = 9073 Result 9074 .setLValueUninit(Base, Offset, PathLength, IsLValueOnePastTheEnd, 9075 IsNullPtr) 9076 .data(); 9077 for (unsigned LoopIdx = 0; LoopIdx < PathLength; LoopIdx++) { 9078 if (ElemTy->getAs<RecordType>()) { 9079 unsigned Int = asImpl().readUInt32(); 9080 Decl *D = asImpl().readDeclAs<Decl>(); 9081 if (auto *RD = dyn_cast<CXXRecordDecl>(D)) 9082 ElemTy = getASTContext().getRecordType(RD); 9083 else 9084 ElemTy = cast<ValueDecl>(D)->getType(); 9085 Path[LoopIdx] = 9086 APValue::LValuePathEntry(APValue::BaseOrMemberType(D, Int)); 9087 } else { 9088 ElemTy = getASTContext().getAsArrayType(ElemTy)->getElementType(); 9089 Path[LoopIdx] = 9090 APValue::LValuePathEntry::ArrayIndex(asImpl().readUInt32()); 9091 } 9092 } 9093 } else 9094 Result.setLValue(Base, Offset, APValue::NoLValuePath{}, IsNullPtr); 9095 return Result; 9096 } 9097 } 9098 llvm_unreachable("Invalid APValue::ValueKind"); 9099 } 9100 9101 /// Read a floating-point value 9102 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) { 9103 return llvm::APFloat(Sem, readAPInt()); 9104 } 9105 9106 // Read a string 9107 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) { 9108 unsigned Len = Record[Idx++]; 9109 std::string Result(Record.data() + Idx, Record.data() + Idx + Len); 9110 Idx += Len; 9111 return Result; 9112 } 9113 9114 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record, 9115 unsigned &Idx) { 9116 std::string Filename = ReadString(Record, Idx); 9117 ResolveImportedPath(F, Filename); 9118 return Filename; 9119 } 9120 9121 std::string ASTReader::ReadPath(StringRef BaseDirectory, 9122 const RecordData &Record, unsigned &Idx) { 9123 std::string Filename = ReadString(Record, Idx); 9124 if (!BaseDirectory.empty()) 9125 ResolveImportedPath(Filename, BaseDirectory); 9126 return Filename; 9127 } 9128 9129 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record, 9130 unsigned &Idx) { 9131 unsigned Major = Record[Idx++]; 9132 unsigned Minor = Record[Idx++]; 9133 unsigned Subminor = Record[Idx++]; 9134 if (Minor == 0) 9135 return VersionTuple(Major); 9136 if (Subminor == 0) 9137 return VersionTuple(Major, Minor - 1); 9138 return VersionTuple(Major, Minor - 1, Subminor - 1); 9139 } 9140 9141 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F, 9142 const RecordData &Record, 9143 unsigned &Idx) { 9144 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx); 9145 return CXXTemporary::Create(getContext(), Decl); 9146 } 9147 9148 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const { 9149 return Diag(CurrentImportLoc, DiagID); 9150 } 9151 9152 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const { 9153 return Diags.Report(Loc, DiagID); 9154 } 9155 9156 /// Retrieve the identifier table associated with the 9157 /// preprocessor. 9158 IdentifierTable &ASTReader::getIdentifierTable() { 9159 return PP.getIdentifierTable(); 9160 } 9161 9162 /// Record that the given ID maps to the given switch-case 9163 /// statement. 9164 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { 9165 assert((*CurrSwitchCaseStmts)[ID] == nullptr && 9166 "Already have a SwitchCase with this ID"); 9167 (*CurrSwitchCaseStmts)[ID] = SC; 9168 } 9169 9170 /// Retrieve the switch-case statement with the given ID. 9171 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) { 9172 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID"); 9173 return (*CurrSwitchCaseStmts)[ID]; 9174 } 9175 9176 void ASTReader::ClearSwitchCaseIDs() { 9177 CurrSwitchCaseStmts->clear(); 9178 } 9179 9180 void ASTReader::ReadComments() { 9181 ASTContext &Context = getContext(); 9182 std::vector<RawComment *> Comments; 9183 for (SmallVectorImpl<std::pair<BitstreamCursor, 9184 serialization::ModuleFile *>>::iterator 9185 I = CommentsCursors.begin(), 9186 E = CommentsCursors.end(); 9187 I != E; ++I) { 9188 Comments.clear(); 9189 BitstreamCursor &Cursor = I->first; 9190 serialization::ModuleFile &F = *I->second; 9191 SavedStreamPosition SavedPosition(Cursor); 9192 9193 RecordData Record; 9194 while (true) { 9195 Expected<llvm::BitstreamEntry> MaybeEntry = 9196 Cursor.advanceSkippingSubblocks( 9197 BitstreamCursor::AF_DontPopBlockAtEnd); 9198 if (!MaybeEntry) { 9199 Error(MaybeEntry.takeError()); 9200 return; 9201 } 9202 llvm::BitstreamEntry Entry = MaybeEntry.get(); 9203 9204 switch (Entry.Kind) { 9205 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 9206 case llvm::BitstreamEntry::Error: 9207 Error("malformed block record in AST file"); 9208 return; 9209 case llvm::BitstreamEntry::EndBlock: 9210 goto NextCursor; 9211 case llvm::BitstreamEntry::Record: 9212 // The interesting case. 9213 break; 9214 } 9215 9216 // Read a record. 9217 Record.clear(); 9218 Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record); 9219 if (!MaybeComment) { 9220 Error(MaybeComment.takeError()); 9221 return; 9222 } 9223 switch ((CommentRecordTypes)MaybeComment.get()) { 9224 case COMMENTS_RAW_COMMENT: { 9225 unsigned Idx = 0; 9226 SourceRange SR = ReadSourceRange(F, Record, Idx); 9227 RawComment::CommentKind Kind = 9228 (RawComment::CommentKind) Record[Idx++]; 9229 bool IsTrailingComment = Record[Idx++]; 9230 bool IsAlmostTrailingComment = Record[Idx++]; 9231 Comments.push_back(new (Context) RawComment( 9232 SR, Kind, IsTrailingComment, IsAlmostTrailingComment)); 9233 break; 9234 } 9235 } 9236 } 9237 NextCursor: 9238 llvm::DenseMap<FileID, std::map<unsigned, RawComment *>> 9239 FileToOffsetToComment; 9240 for (RawComment *C : Comments) { 9241 SourceLocation CommentLoc = C->getBeginLoc(); 9242 if (CommentLoc.isValid()) { 9243 std::pair<FileID, unsigned> Loc = 9244 SourceMgr.getDecomposedLoc(CommentLoc); 9245 if (Loc.first.isValid()) 9246 Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C); 9247 } 9248 } 9249 } 9250 } 9251 9252 void ASTReader::visitInputFiles(serialization::ModuleFile &MF, 9253 bool IncludeSystem, bool Complain, 9254 llvm::function_ref<void(const serialization::InputFile &IF, 9255 bool isSystem)> Visitor) { 9256 unsigned NumUserInputs = MF.NumUserInputFiles; 9257 unsigned NumInputs = MF.InputFilesLoaded.size(); 9258 assert(NumUserInputs <= NumInputs); 9259 unsigned N = IncludeSystem ? NumInputs : NumUserInputs; 9260 for (unsigned I = 0; I < N; ++I) { 9261 bool IsSystem = I >= NumUserInputs; 9262 InputFile IF = getInputFile(MF, I+1, Complain); 9263 Visitor(IF, IsSystem); 9264 } 9265 } 9266 9267 void ASTReader::visitTopLevelModuleMaps( 9268 serialization::ModuleFile &MF, 9269 llvm::function_ref<void(const FileEntry *FE)> Visitor) { 9270 unsigned NumInputs = MF.InputFilesLoaded.size(); 9271 for (unsigned I = 0; I < NumInputs; ++I) { 9272 InputFileInfo IFI = readInputFileInfo(MF, I + 1); 9273 if (IFI.TopLevelModuleMap) 9274 // FIXME: This unnecessarily re-reads the InputFileInfo. 9275 if (auto FE = getInputFile(MF, I + 1).getFile()) 9276 Visitor(FE); 9277 } 9278 } 9279 9280 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) { 9281 // If we know the owning module, use it. 9282 if (Module *M = D->getImportedOwningModule()) 9283 return M->getFullModuleName(); 9284 9285 // Otherwise, use the name of the top-level module the decl is within. 9286 if (ModuleFile *M = getOwningModuleFile(D)) 9287 return M->ModuleName; 9288 9289 // Not from a module. 9290 return {}; 9291 } 9292 9293 void ASTReader::finishPendingActions() { 9294 while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() || 9295 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() || 9296 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() || 9297 !PendingUpdateRecords.empty()) { 9298 // If any identifiers with corresponding top-level declarations have 9299 // been loaded, load those declarations now. 9300 using TopLevelDeclsMap = 9301 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>; 9302 TopLevelDeclsMap TopLevelDecls; 9303 9304 while (!PendingIdentifierInfos.empty()) { 9305 IdentifierInfo *II = PendingIdentifierInfos.back().first; 9306 SmallVector<uint32_t, 4> DeclIDs = 9307 std::move(PendingIdentifierInfos.back().second); 9308 PendingIdentifierInfos.pop_back(); 9309 9310 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]); 9311 } 9312 9313 // Load each function type that we deferred loading because it was a 9314 // deduced type that might refer to a local type declared within itself. 9315 for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) { 9316 auto *FD = PendingFunctionTypes[I].first; 9317 FD->setType(GetType(PendingFunctionTypes[I].second)); 9318 9319 // If we gave a function a deduced return type, remember that we need to 9320 // propagate that along the redeclaration chain. 9321 auto *DT = FD->getReturnType()->getContainedDeducedType(); 9322 if (DT && DT->isDeduced()) 9323 PendingDeducedTypeUpdates.insert( 9324 {FD->getCanonicalDecl(), FD->getReturnType()}); 9325 } 9326 PendingFunctionTypes.clear(); 9327 9328 // For each decl chain that we wanted to complete while deserializing, mark 9329 // it as "still needs to be completed". 9330 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) { 9331 markIncompleteDeclChain(PendingIncompleteDeclChains[I]); 9332 } 9333 PendingIncompleteDeclChains.clear(); 9334 9335 // Load pending declaration chains. 9336 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) 9337 loadPendingDeclChain(PendingDeclChains[I].first, 9338 PendingDeclChains[I].second); 9339 PendingDeclChains.clear(); 9340 9341 // Make the most recent of the top-level declarations visible. 9342 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(), 9343 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) { 9344 IdentifierInfo *II = TLD->first; 9345 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) { 9346 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II); 9347 } 9348 } 9349 9350 // Load any pending macro definitions. 9351 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) { 9352 IdentifierInfo *II = PendingMacroIDs.begin()[I].first; 9353 SmallVector<PendingMacroInfo, 2> GlobalIDs; 9354 GlobalIDs.swap(PendingMacroIDs.begin()[I].second); 9355 // Initialize the macro history from chained-PCHs ahead of module imports. 9356 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9357 ++IDIdx) { 9358 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9359 if (!Info.M->isModule()) 9360 resolvePendingMacro(II, Info); 9361 } 9362 // Handle module imports. 9363 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9364 ++IDIdx) { 9365 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9366 if (Info.M->isModule()) 9367 resolvePendingMacro(II, Info); 9368 } 9369 } 9370 PendingMacroIDs.clear(); 9371 9372 // Wire up the DeclContexts for Decls that we delayed setting until 9373 // recursive loading is completed. 9374 while (!PendingDeclContextInfos.empty()) { 9375 PendingDeclContextInfo Info = PendingDeclContextInfos.front(); 9376 PendingDeclContextInfos.pop_front(); 9377 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC)); 9378 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC)); 9379 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext()); 9380 } 9381 9382 // Perform any pending declaration updates. 9383 while (!PendingUpdateRecords.empty()) { 9384 auto Update = PendingUpdateRecords.pop_back_val(); 9385 ReadingKindTracker ReadingKind(Read_Decl, *this); 9386 loadDeclUpdateRecords(Update); 9387 } 9388 } 9389 9390 // At this point, all update records for loaded decls are in place, so any 9391 // fake class definitions should have become real. 9392 assert(PendingFakeDefinitionData.empty() && 9393 "faked up a class definition but never saw the real one"); 9394 9395 // If we deserialized any C++ or Objective-C class definitions, any 9396 // Objective-C protocol definitions, or any redeclarable templates, make sure 9397 // that all redeclarations point to the definitions. Note that this can only 9398 // happen now, after the redeclaration chains have been fully wired. 9399 for (Decl *D : PendingDefinitions) { 9400 if (TagDecl *TD = dyn_cast<TagDecl>(D)) { 9401 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) { 9402 // Make sure that the TagType points at the definition. 9403 const_cast<TagType*>(TagT)->decl = TD; 9404 } 9405 9406 if (auto RD = dyn_cast<CXXRecordDecl>(D)) { 9407 for (auto *R = getMostRecentExistingDecl(RD); R; 9408 R = R->getPreviousDecl()) { 9409 assert((R == D) == 9410 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() && 9411 "declaration thinks it's the definition but it isn't"); 9412 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData; 9413 } 9414 } 9415 9416 continue; 9417 } 9418 9419 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) { 9420 // Make sure that the ObjCInterfaceType points at the definition. 9421 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl)) 9422 ->Decl = ID; 9423 9424 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl()) 9425 cast<ObjCInterfaceDecl>(R)->Data = ID->Data; 9426 9427 continue; 9428 } 9429 9430 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) { 9431 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl()) 9432 cast<ObjCProtocolDecl>(R)->Data = PD->Data; 9433 9434 continue; 9435 } 9436 9437 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl(); 9438 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl()) 9439 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common; 9440 } 9441 PendingDefinitions.clear(); 9442 9443 // Load the bodies of any functions or methods we've encountered. We do 9444 // this now (delayed) so that we can be sure that the declaration chains 9445 // have been fully wired up (hasBody relies on this). 9446 // FIXME: We shouldn't require complete redeclaration chains here. 9447 for (PendingBodiesMap::iterator PB = PendingBodies.begin(), 9448 PBEnd = PendingBodies.end(); 9449 PB != PBEnd; ++PB) { 9450 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) { 9451 // For a function defined inline within a class template, force the 9452 // canonical definition to be the one inside the canonical definition of 9453 // the template. This ensures that we instantiate from a correct view 9454 // of the template. 9455 // 9456 // Sadly we can't do this more generally: we can't be sure that all 9457 // copies of an arbitrary class definition will have the same members 9458 // defined (eg, some member functions may not be instantiated, and some 9459 // special members may or may not have been implicitly defined). 9460 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent())) 9461 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition()) 9462 continue; 9463 9464 // FIXME: Check for =delete/=default? 9465 // FIXME: Complain about ODR violations here? 9466 const FunctionDecl *Defn = nullptr; 9467 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) { 9468 FD->setLazyBody(PB->second); 9469 } else { 9470 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn); 9471 mergeDefinitionVisibility(NonConstDefn, FD); 9472 9473 if (!FD->isLateTemplateParsed() && 9474 !NonConstDefn->isLateTemplateParsed() && 9475 FD->getODRHash() != NonConstDefn->getODRHash()) { 9476 if (!isa<CXXMethodDecl>(FD)) { 9477 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9478 } else if (FD->getLexicalParent()->isFileContext() && 9479 NonConstDefn->getLexicalParent()->isFileContext()) { 9480 // Only diagnose out-of-line method definitions. If they are 9481 // in class definitions, then an error will be generated when 9482 // processing the class bodies. 9483 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9484 } 9485 } 9486 } 9487 continue; 9488 } 9489 9490 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first); 9491 if (!getContext().getLangOpts().Modules || !MD->hasBody()) 9492 MD->setLazyBody(PB->second); 9493 } 9494 PendingBodies.clear(); 9495 9496 // Do some cleanup. 9497 for (auto *ND : PendingMergedDefinitionsToDeduplicate) 9498 getContext().deduplicateMergedDefinitonsFor(ND); 9499 PendingMergedDefinitionsToDeduplicate.clear(); 9500 } 9501 9502 void ASTReader::diagnoseOdrViolations() { 9503 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() && 9504 PendingFunctionOdrMergeFailures.empty() && 9505 PendingEnumOdrMergeFailures.empty()) 9506 return; 9507 9508 // Trigger the import of the full definition of each class that had any 9509 // odr-merging problems, so we can produce better diagnostics for them. 9510 // These updates may in turn find and diagnose some ODR failures, so take 9511 // ownership of the set first. 9512 auto OdrMergeFailures = std::move(PendingOdrMergeFailures); 9513 PendingOdrMergeFailures.clear(); 9514 for (auto &Merge : OdrMergeFailures) { 9515 Merge.first->buildLookup(); 9516 Merge.first->decls_begin(); 9517 Merge.first->bases_begin(); 9518 Merge.first->vbases_begin(); 9519 for (auto &RecordPair : Merge.second) { 9520 auto *RD = RecordPair.first; 9521 RD->decls_begin(); 9522 RD->bases_begin(); 9523 RD->vbases_begin(); 9524 } 9525 } 9526 9527 // Trigger the import of functions. 9528 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures); 9529 PendingFunctionOdrMergeFailures.clear(); 9530 for (auto &Merge : FunctionOdrMergeFailures) { 9531 Merge.first->buildLookup(); 9532 Merge.first->decls_begin(); 9533 Merge.first->getBody(); 9534 for (auto &FD : Merge.second) { 9535 FD->buildLookup(); 9536 FD->decls_begin(); 9537 FD->getBody(); 9538 } 9539 } 9540 9541 // Trigger the import of enums. 9542 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures); 9543 PendingEnumOdrMergeFailures.clear(); 9544 for (auto &Merge : EnumOdrMergeFailures) { 9545 Merge.first->decls_begin(); 9546 for (auto &Enum : Merge.second) { 9547 Enum->decls_begin(); 9548 } 9549 } 9550 9551 // For each declaration from a merged context, check that the canonical 9552 // definition of that context also contains a declaration of the same 9553 // entity. 9554 // 9555 // Caution: this loop does things that might invalidate iterators into 9556 // PendingOdrMergeChecks. Don't turn this into a range-based for loop! 9557 while (!PendingOdrMergeChecks.empty()) { 9558 NamedDecl *D = PendingOdrMergeChecks.pop_back_val(); 9559 9560 // FIXME: Skip over implicit declarations for now. This matters for things 9561 // like implicitly-declared special member functions. This isn't entirely 9562 // correct; we can end up with multiple unmerged declarations of the same 9563 // implicit entity. 9564 if (D->isImplicit()) 9565 continue; 9566 9567 DeclContext *CanonDef = D->getDeclContext(); 9568 9569 bool Found = false; 9570 const Decl *DCanon = D->getCanonicalDecl(); 9571 9572 for (auto RI : D->redecls()) { 9573 if (RI->getLexicalDeclContext() == CanonDef) { 9574 Found = true; 9575 break; 9576 } 9577 } 9578 if (Found) 9579 continue; 9580 9581 // Quick check failed, time to do the slow thing. Note, we can't just 9582 // look up the name of D in CanonDef here, because the member that is 9583 // in CanonDef might not be found by name lookup (it might have been 9584 // replaced by a more recent declaration in the lookup table), and we 9585 // can't necessarily find it in the redeclaration chain because it might 9586 // be merely mergeable, not redeclarable. 9587 llvm::SmallVector<const NamedDecl*, 4> Candidates; 9588 for (auto *CanonMember : CanonDef->decls()) { 9589 if (CanonMember->getCanonicalDecl() == DCanon) { 9590 // This can happen if the declaration is merely mergeable and not 9591 // actually redeclarable (we looked for redeclarations earlier). 9592 // 9593 // FIXME: We should be able to detect this more efficiently, without 9594 // pulling in all of the members of CanonDef. 9595 Found = true; 9596 break; 9597 } 9598 if (auto *ND = dyn_cast<NamedDecl>(CanonMember)) 9599 if (ND->getDeclName() == D->getDeclName()) 9600 Candidates.push_back(ND); 9601 } 9602 9603 if (!Found) { 9604 // The AST doesn't like TagDecls becoming invalid after they've been 9605 // completed. We only really need to mark FieldDecls as invalid here. 9606 if (!isa<TagDecl>(D)) 9607 D->setInvalidDecl(); 9608 9609 // Ensure we don't accidentally recursively enter deserialization while 9610 // we're producing our diagnostic. 9611 Deserializing RecursionGuard(this); 9612 9613 std::string CanonDefModule = 9614 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef)); 9615 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl) 9616 << D << getOwningModuleNameForDiagnostic(D) 9617 << CanonDef << CanonDefModule.empty() << CanonDefModule; 9618 9619 if (Candidates.empty()) 9620 Diag(cast<Decl>(CanonDef)->getLocation(), 9621 diag::note_module_odr_violation_no_possible_decls) << D; 9622 else { 9623 for (unsigned I = 0, N = Candidates.size(); I != N; ++I) 9624 Diag(Candidates[I]->getLocation(), 9625 diag::note_module_odr_violation_possible_decl) 9626 << Candidates[I]; 9627 } 9628 9629 DiagnosedOdrMergeFailures.insert(CanonDef); 9630 } 9631 } 9632 9633 if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() && 9634 EnumOdrMergeFailures.empty()) 9635 return; 9636 9637 // Ensure we don't accidentally recursively enter deserialization while 9638 // we're producing our diagnostics. 9639 Deserializing RecursionGuard(this); 9640 9641 // Common code for hashing helpers. 9642 ODRHash Hash; 9643 auto ComputeQualTypeODRHash = [&Hash](QualType Ty) { 9644 Hash.clear(); 9645 Hash.AddQualType(Ty); 9646 return Hash.CalculateHash(); 9647 }; 9648 9649 auto ComputeODRHash = [&Hash](const Stmt *S) { 9650 assert(S); 9651 Hash.clear(); 9652 Hash.AddStmt(S); 9653 return Hash.CalculateHash(); 9654 }; 9655 9656 auto ComputeSubDeclODRHash = [&Hash](const Decl *D) { 9657 assert(D); 9658 Hash.clear(); 9659 Hash.AddSubDecl(D); 9660 return Hash.CalculateHash(); 9661 }; 9662 9663 auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) { 9664 Hash.clear(); 9665 Hash.AddTemplateArgument(TA); 9666 return Hash.CalculateHash(); 9667 }; 9668 9669 auto ComputeTemplateParameterListODRHash = 9670 [&Hash](const TemplateParameterList *TPL) { 9671 assert(TPL); 9672 Hash.clear(); 9673 Hash.AddTemplateParameterList(TPL); 9674 return Hash.CalculateHash(); 9675 }; 9676 9677 // Used with err_module_odr_violation_mismatch_decl and 9678 // note_module_odr_violation_mismatch_decl 9679 // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed 9680 enum ODRMismatchDecl { 9681 EndOfClass, 9682 PublicSpecifer, 9683 PrivateSpecifer, 9684 ProtectedSpecifer, 9685 StaticAssert, 9686 Field, 9687 CXXMethod, 9688 TypeAlias, 9689 TypeDef, 9690 Var, 9691 Friend, 9692 FunctionTemplate, 9693 Other 9694 }; 9695 9696 // Used with err_module_odr_violation_mismatch_decl_diff and 9697 // note_module_odr_violation_mismatch_decl_diff 9698 enum ODRMismatchDeclDifference { 9699 StaticAssertCondition, 9700 StaticAssertMessage, 9701 StaticAssertOnlyMessage, 9702 FieldName, 9703 FieldTypeName, 9704 FieldSingleBitField, 9705 FieldDifferentWidthBitField, 9706 FieldSingleMutable, 9707 FieldSingleInitializer, 9708 FieldDifferentInitializers, 9709 MethodName, 9710 MethodDeleted, 9711 MethodDefaulted, 9712 MethodVirtual, 9713 MethodStatic, 9714 MethodVolatile, 9715 MethodConst, 9716 MethodInline, 9717 MethodNumberParameters, 9718 MethodParameterType, 9719 MethodParameterName, 9720 MethodParameterSingleDefaultArgument, 9721 MethodParameterDifferentDefaultArgument, 9722 MethodNoTemplateArguments, 9723 MethodDifferentNumberTemplateArguments, 9724 MethodDifferentTemplateArgument, 9725 MethodSingleBody, 9726 MethodDifferentBody, 9727 TypedefName, 9728 TypedefType, 9729 VarName, 9730 VarType, 9731 VarSingleInitializer, 9732 VarDifferentInitializer, 9733 VarConstexpr, 9734 FriendTypeFunction, 9735 FriendType, 9736 FriendFunction, 9737 FunctionTemplateDifferentNumberParameters, 9738 FunctionTemplateParameterDifferentKind, 9739 FunctionTemplateParameterName, 9740 FunctionTemplateParameterSingleDefaultArgument, 9741 FunctionTemplateParameterDifferentDefaultArgument, 9742 FunctionTemplateParameterDifferentType, 9743 FunctionTemplatePackParameter, 9744 }; 9745 9746 // These lambdas have the common portions of the ODR diagnostics. This 9747 // has the same return as Diag(), so addition parameters can be passed 9748 // in with operator<< 9749 auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule, 9750 SourceLocation Loc, SourceRange Range, 9751 ODRMismatchDeclDifference DiffType) { 9752 return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff) 9753 << FirstRecord << FirstModule.empty() << FirstModule << Range 9754 << DiffType; 9755 }; 9756 auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc, 9757 SourceRange Range, ODRMismatchDeclDifference DiffType) { 9758 return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff) 9759 << SecondModule << Range << DiffType; 9760 }; 9761 9762 auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote, 9763 &ComputeQualTypeODRHash, &ComputeODRHash]( 9764 NamedDecl *FirstRecord, StringRef FirstModule, 9765 StringRef SecondModule, FieldDecl *FirstField, 9766 FieldDecl *SecondField) { 9767 IdentifierInfo *FirstII = FirstField->getIdentifier(); 9768 IdentifierInfo *SecondII = SecondField->getIdentifier(); 9769 if (FirstII->getName() != SecondII->getName()) { 9770 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9771 FirstField->getSourceRange(), FieldName) 9772 << FirstII; 9773 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9774 SecondField->getSourceRange(), FieldName) 9775 << SecondII; 9776 9777 return true; 9778 } 9779 9780 assert(getContext().hasSameType(FirstField->getType(), 9781 SecondField->getType())); 9782 9783 QualType FirstType = FirstField->getType(); 9784 QualType SecondType = SecondField->getType(); 9785 if (ComputeQualTypeODRHash(FirstType) != 9786 ComputeQualTypeODRHash(SecondType)) { 9787 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9788 FirstField->getSourceRange(), FieldTypeName) 9789 << FirstII << FirstType; 9790 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9791 SecondField->getSourceRange(), FieldTypeName) 9792 << SecondII << SecondType; 9793 9794 return true; 9795 } 9796 9797 const bool IsFirstBitField = FirstField->isBitField(); 9798 const bool IsSecondBitField = SecondField->isBitField(); 9799 if (IsFirstBitField != IsSecondBitField) { 9800 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9801 FirstField->getSourceRange(), FieldSingleBitField) 9802 << FirstII << IsFirstBitField; 9803 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9804 SecondField->getSourceRange(), FieldSingleBitField) 9805 << SecondII << IsSecondBitField; 9806 return true; 9807 } 9808 9809 if (IsFirstBitField && IsSecondBitField) { 9810 unsigned FirstBitWidthHash = 9811 ComputeODRHash(FirstField->getBitWidth()); 9812 unsigned SecondBitWidthHash = 9813 ComputeODRHash(SecondField->getBitWidth()); 9814 if (FirstBitWidthHash != SecondBitWidthHash) { 9815 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9816 FirstField->getSourceRange(), 9817 FieldDifferentWidthBitField) 9818 << FirstII << FirstField->getBitWidth()->getSourceRange(); 9819 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9820 SecondField->getSourceRange(), 9821 FieldDifferentWidthBitField) 9822 << SecondII << SecondField->getBitWidth()->getSourceRange(); 9823 return true; 9824 } 9825 } 9826 9827 if (!PP.getLangOpts().CPlusPlus) 9828 return false; 9829 9830 const bool IsFirstMutable = FirstField->isMutable(); 9831 const bool IsSecondMutable = SecondField->isMutable(); 9832 if (IsFirstMutable != IsSecondMutable) { 9833 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9834 FirstField->getSourceRange(), FieldSingleMutable) 9835 << FirstII << IsFirstMutable; 9836 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9837 SecondField->getSourceRange(), FieldSingleMutable) 9838 << SecondII << IsSecondMutable; 9839 return true; 9840 } 9841 9842 const Expr *FirstInitializer = FirstField->getInClassInitializer(); 9843 const Expr *SecondInitializer = SecondField->getInClassInitializer(); 9844 if ((!FirstInitializer && SecondInitializer) || 9845 (FirstInitializer && !SecondInitializer)) { 9846 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9847 FirstField->getSourceRange(), FieldSingleInitializer) 9848 << FirstII << (FirstInitializer != nullptr); 9849 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9850 SecondField->getSourceRange(), FieldSingleInitializer) 9851 << SecondII << (SecondInitializer != nullptr); 9852 return true; 9853 } 9854 9855 if (FirstInitializer && SecondInitializer) { 9856 unsigned FirstInitHash = ComputeODRHash(FirstInitializer); 9857 unsigned SecondInitHash = ComputeODRHash(SecondInitializer); 9858 if (FirstInitHash != SecondInitHash) { 9859 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9860 FirstField->getSourceRange(), 9861 FieldDifferentInitializers) 9862 << FirstII << FirstInitializer->getSourceRange(); 9863 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9864 SecondField->getSourceRange(), 9865 FieldDifferentInitializers) 9866 << SecondII << SecondInitializer->getSourceRange(); 9867 return true; 9868 } 9869 } 9870 9871 return false; 9872 }; 9873 9874 auto ODRDiagTypeDefOrAlias = 9875 [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash]( 9876 NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule, 9877 TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD, 9878 bool IsTypeAlias) { 9879 auto FirstName = FirstTD->getDeclName(); 9880 auto SecondName = SecondTD->getDeclName(); 9881 if (FirstName != SecondName) { 9882 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(), 9883 FirstTD->getSourceRange(), TypedefName) 9884 << IsTypeAlias << FirstName; 9885 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(), 9886 SecondTD->getSourceRange(), TypedefName) 9887 << IsTypeAlias << SecondName; 9888 return true; 9889 } 9890 9891 QualType FirstType = FirstTD->getUnderlyingType(); 9892 QualType SecondType = SecondTD->getUnderlyingType(); 9893 if (ComputeQualTypeODRHash(FirstType) != 9894 ComputeQualTypeODRHash(SecondType)) { 9895 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(), 9896 FirstTD->getSourceRange(), TypedefType) 9897 << IsTypeAlias << FirstName << FirstType; 9898 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(), 9899 SecondTD->getSourceRange(), TypedefType) 9900 << IsTypeAlias << SecondName << SecondType; 9901 return true; 9902 } 9903 9904 return false; 9905 }; 9906 9907 auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote, 9908 &ComputeQualTypeODRHash, &ComputeODRHash, 9909 this](NamedDecl *FirstRecord, StringRef FirstModule, 9910 StringRef SecondModule, VarDecl *FirstVD, 9911 VarDecl *SecondVD) { 9912 auto FirstName = FirstVD->getDeclName(); 9913 auto SecondName = SecondVD->getDeclName(); 9914 if (FirstName != SecondName) { 9915 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9916 FirstVD->getSourceRange(), VarName) 9917 << FirstName; 9918 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9919 SecondVD->getSourceRange(), VarName) 9920 << SecondName; 9921 return true; 9922 } 9923 9924 QualType FirstType = FirstVD->getType(); 9925 QualType SecondType = SecondVD->getType(); 9926 if (ComputeQualTypeODRHash(FirstType) != 9927 ComputeQualTypeODRHash(SecondType)) { 9928 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9929 FirstVD->getSourceRange(), VarType) 9930 << FirstName << FirstType; 9931 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9932 SecondVD->getSourceRange(), VarType) 9933 << SecondName << SecondType; 9934 return true; 9935 } 9936 9937 if (!PP.getLangOpts().CPlusPlus) 9938 return false; 9939 9940 const Expr *FirstInit = FirstVD->getInit(); 9941 const Expr *SecondInit = SecondVD->getInit(); 9942 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 9943 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9944 FirstVD->getSourceRange(), VarSingleInitializer) 9945 << FirstName << (FirstInit == nullptr) 9946 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 9947 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9948 SecondVD->getSourceRange(), VarSingleInitializer) 9949 << SecondName << (SecondInit == nullptr) 9950 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 9951 return true; 9952 } 9953 9954 if (FirstInit && SecondInit && 9955 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 9956 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9957 FirstVD->getSourceRange(), VarDifferentInitializer) 9958 << FirstName << FirstInit->getSourceRange(); 9959 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9960 SecondVD->getSourceRange(), VarDifferentInitializer) 9961 << SecondName << SecondInit->getSourceRange(); 9962 return true; 9963 } 9964 9965 const bool FirstIsConstexpr = FirstVD->isConstexpr(); 9966 const bool SecondIsConstexpr = SecondVD->isConstexpr(); 9967 if (FirstIsConstexpr != SecondIsConstexpr) { 9968 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9969 FirstVD->getSourceRange(), VarConstexpr) 9970 << FirstName << FirstIsConstexpr; 9971 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9972 SecondVD->getSourceRange(), VarConstexpr) 9973 << SecondName << SecondIsConstexpr; 9974 return true; 9975 } 9976 return false; 9977 }; 9978 9979 auto DifferenceSelector = [](Decl *D) { 9980 assert(D && "valid Decl required"); 9981 switch (D->getKind()) { 9982 default: 9983 return Other; 9984 case Decl::AccessSpec: 9985 switch (D->getAccess()) { 9986 case AS_public: 9987 return PublicSpecifer; 9988 case AS_private: 9989 return PrivateSpecifer; 9990 case AS_protected: 9991 return ProtectedSpecifer; 9992 case AS_none: 9993 break; 9994 } 9995 llvm_unreachable("Invalid access specifier"); 9996 case Decl::StaticAssert: 9997 return StaticAssert; 9998 case Decl::Field: 9999 return Field; 10000 case Decl::CXXMethod: 10001 case Decl::CXXConstructor: 10002 case Decl::CXXDestructor: 10003 return CXXMethod; 10004 case Decl::TypeAlias: 10005 return TypeAlias; 10006 case Decl::Typedef: 10007 return TypeDef; 10008 case Decl::Var: 10009 return Var; 10010 case Decl::Friend: 10011 return Friend; 10012 case Decl::FunctionTemplate: 10013 return FunctionTemplate; 10014 } 10015 }; 10016 10017 using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>; 10018 auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes, 10019 RecordDecl *Record, 10020 const DeclContext *DC) { 10021 for (auto *D : Record->decls()) { 10022 if (!ODRHash::isDeclToBeProcessed(D, DC)) 10023 continue; 10024 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 10025 } 10026 }; 10027 10028 struct DiffResult { 10029 Decl *FirstDecl = nullptr, *SecondDecl = nullptr; 10030 ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other; 10031 }; 10032 10033 // If there is a diagnoseable difference, FirstDiffType and 10034 // SecondDiffType will not be Other and FirstDecl and SecondDecl will be 10035 // filled in if not EndOfClass. 10036 auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes, 10037 DeclHashes &SecondHashes) { 10038 DiffResult DR; 10039 auto FirstIt = FirstHashes.begin(); 10040 auto SecondIt = SecondHashes.begin(); 10041 while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) { 10042 if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() && 10043 FirstIt->second == SecondIt->second) { 10044 ++FirstIt; 10045 ++SecondIt; 10046 continue; 10047 } 10048 10049 DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first; 10050 DR.SecondDecl = 10051 SecondIt == SecondHashes.end() ? nullptr : SecondIt->first; 10052 10053 DR.FirstDiffType = 10054 DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass; 10055 DR.SecondDiffType = 10056 DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass; 10057 return DR; 10058 } 10059 return DR; 10060 }; 10061 10062 // Use this to diagnose that an unexpected Decl was encountered 10063 // or no difference was detected. This causes a generic error 10064 // message to be emitted. 10065 auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord, 10066 StringRef FirstModule, 10067 NamedDecl *SecondRecord, 10068 StringRef SecondModule) { 10069 Diag(FirstRecord->getLocation(), 10070 diag::err_module_odr_violation_different_definitions) 10071 << FirstRecord << FirstModule.empty() << FirstModule; 10072 10073 if (DR.FirstDecl) { 10074 Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference) 10075 << FirstRecord << DR.FirstDecl->getSourceRange(); 10076 } 10077 10078 Diag(SecondRecord->getLocation(), 10079 diag::note_module_odr_violation_different_definitions) 10080 << SecondModule; 10081 10082 if (DR.SecondDecl) { 10083 Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference) 10084 << DR.SecondDecl->getSourceRange(); 10085 } 10086 }; 10087 10088 auto DiagnoseODRMismatch = 10089 [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule, 10090 NamedDecl *SecondRecord, StringRef SecondModule) { 10091 SourceLocation FirstLoc; 10092 SourceRange FirstRange; 10093 auto *FirstTag = dyn_cast<TagDecl>(FirstRecord); 10094 if (DR.FirstDiffType == EndOfClass && FirstTag) { 10095 FirstLoc = FirstTag->getBraceRange().getEnd(); 10096 } else { 10097 FirstLoc = DR.FirstDecl->getLocation(); 10098 FirstRange = DR.FirstDecl->getSourceRange(); 10099 } 10100 Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl) 10101 << FirstRecord << FirstModule.empty() << FirstModule << FirstRange 10102 << DR.FirstDiffType; 10103 10104 SourceLocation SecondLoc; 10105 SourceRange SecondRange; 10106 auto *SecondTag = dyn_cast<TagDecl>(SecondRecord); 10107 if (DR.SecondDiffType == EndOfClass && SecondTag) { 10108 SecondLoc = SecondTag->getBraceRange().getEnd(); 10109 } else { 10110 SecondLoc = DR.SecondDecl->getLocation(); 10111 SecondRange = DR.SecondDecl->getSourceRange(); 10112 } 10113 Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl) 10114 << SecondModule << SecondRange << DR.SecondDiffType; 10115 }; 10116 10117 // Issue any pending ODR-failure diagnostics. 10118 for (auto &Merge : OdrMergeFailures) { 10119 // If we've already pointed out a specific problem with this class, don't 10120 // bother issuing a general "something's different" diagnostic. 10121 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 10122 continue; 10123 10124 bool Diagnosed = false; 10125 CXXRecordDecl *FirstRecord = Merge.first; 10126 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord); 10127 for (auto &RecordPair : Merge.second) { 10128 CXXRecordDecl *SecondRecord = RecordPair.first; 10129 // Multiple different declarations got merged together; tell the user 10130 // where they came from. 10131 if (FirstRecord == SecondRecord) 10132 continue; 10133 10134 std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord); 10135 10136 auto *FirstDD = FirstRecord->DefinitionData; 10137 auto *SecondDD = RecordPair.second; 10138 10139 assert(FirstDD && SecondDD && "Definitions without DefinitionData"); 10140 10141 // Diagnostics from DefinitionData are emitted here. 10142 if (FirstDD != SecondDD) { 10143 enum ODRDefinitionDataDifference { 10144 NumBases, 10145 NumVBases, 10146 BaseType, 10147 BaseVirtual, 10148 BaseAccess, 10149 }; 10150 auto ODRDiagBaseError = [FirstRecord, &FirstModule, 10151 this](SourceLocation Loc, SourceRange Range, 10152 ODRDefinitionDataDifference DiffType) { 10153 return Diag(Loc, diag::err_module_odr_violation_definition_data) 10154 << FirstRecord << FirstModule.empty() << FirstModule << Range 10155 << DiffType; 10156 }; 10157 auto ODRDiagBaseNote = [&SecondModule, 10158 this](SourceLocation Loc, SourceRange Range, 10159 ODRDefinitionDataDifference DiffType) { 10160 return Diag(Loc, diag::note_module_odr_violation_definition_data) 10161 << SecondModule << Range << DiffType; 10162 }; 10163 10164 unsigned FirstNumBases = FirstDD->NumBases; 10165 unsigned FirstNumVBases = FirstDD->NumVBases; 10166 unsigned SecondNumBases = SecondDD->NumBases; 10167 unsigned SecondNumVBases = SecondDD->NumVBases; 10168 10169 auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) { 10170 unsigned NumBases = DD->NumBases; 10171 if (NumBases == 0) return SourceRange(); 10172 auto bases = DD->bases(); 10173 return SourceRange(bases[0].getBeginLoc(), 10174 bases[NumBases - 1].getEndLoc()); 10175 }; 10176 10177 if (FirstNumBases != SecondNumBases) { 10178 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10179 NumBases) 10180 << FirstNumBases; 10181 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10182 NumBases) 10183 << SecondNumBases; 10184 Diagnosed = true; 10185 break; 10186 } 10187 10188 if (FirstNumVBases != SecondNumVBases) { 10189 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10190 NumVBases) 10191 << FirstNumVBases; 10192 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10193 NumVBases) 10194 << SecondNumVBases; 10195 Diagnosed = true; 10196 break; 10197 } 10198 10199 auto FirstBases = FirstDD->bases(); 10200 auto SecondBases = SecondDD->bases(); 10201 unsigned i = 0; 10202 for (i = 0; i < FirstNumBases; ++i) { 10203 auto FirstBase = FirstBases[i]; 10204 auto SecondBase = SecondBases[i]; 10205 if (ComputeQualTypeODRHash(FirstBase.getType()) != 10206 ComputeQualTypeODRHash(SecondBase.getType())) { 10207 ODRDiagBaseError(FirstRecord->getLocation(), 10208 FirstBase.getSourceRange(), BaseType) 10209 << (i + 1) << FirstBase.getType(); 10210 ODRDiagBaseNote(SecondRecord->getLocation(), 10211 SecondBase.getSourceRange(), BaseType) 10212 << (i + 1) << SecondBase.getType(); 10213 break; 10214 } 10215 10216 if (FirstBase.isVirtual() != SecondBase.isVirtual()) { 10217 ODRDiagBaseError(FirstRecord->getLocation(), 10218 FirstBase.getSourceRange(), BaseVirtual) 10219 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType(); 10220 ODRDiagBaseNote(SecondRecord->getLocation(), 10221 SecondBase.getSourceRange(), BaseVirtual) 10222 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType(); 10223 break; 10224 } 10225 10226 if (FirstBase.getAccessSpecifierAsWritten() != 10227 SecondBase.getAccessSpecifierAsWritten()) { 10228 ODRDiagBaseError(FirstRecord->getLocation(), 10229 FirstBase.getSourceRange(), BaseAccess) 10230 << (i + 1) << FirstBase.getType() 10231 << (int)FirstBase.getAccessSpecifierAsWritten(); 10232 ODRDiagBaseNote(SecondRecord->getLocation(), 10233 SecondBase.getSourceRange(), BaseAccess) 10234 << (i + 1) << SecondBase.getType() 10235 << (int)SecondBase.getAccessSpecifierAsWritten(); 10236 break; 10237 } 10238 } 10239 10240 if (i != FirstNumBases) { 10241 Diagnosed = true; 10242 break; 10243 } 10244 } 10245 10246 const ClassTemplateDecl *FirstTemplate = 10247 FirstRecord->getDescribedClassTemplate(); 10248 const ClassTemplateDecl *SecondTemplate = 10249 SecondRecord->getDescribedClassTemplate(); 10250 10251 assert(!FirstTemplate == !SecondTemplate && 10252 "Both pointers should be null or non-null"); 10253 10254 enum ODRTemplateDifference { 10255 ParamEmptyName, 10256 ParamName, 10257 ParamSingleDefaultArgument, 10258 ParamDifferentDefaultArgument, 10259 }; 10260 10261 if (FirstTemplate && SecondTemplate) { 10262 DeclHashes FirstTemplateHashes; 10263 DeclHashes SecondTemplateHashes; 10264 10265 auto PopulateTemplateParameterHashs = 10266 [&ComputeSubDeclODRHash](DeclHashes &Hashes, 10267 const ClassTemplateDecl *TD) { 10268 for (auto *D : TD->getTemplateParameters()->asArray()) { 10269 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 10270 } 10271 }; 10272 10273 PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate); 10274 PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate); 10275 10276 assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() && 10277 "Number of template parameters should be equal."); 10278 10279 auto FirstIt = FirstTemplateHashes.begin(); 10280 auto FirstEnd = FirstTemplateHashes.end(); 10281 auto SecondIt = SecondTemplateHashes.begin(); 10282 for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) { 10283 if (FirstIt->second == SecondIt->second) 10284 continue; 10285 10286 auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this]( 10287 SourceLocation Loc, SourceRange Range, 10288 ODRTemplateDifference DiffType) { 10289 return Diag(Loc, diag::err_module_odr_violation_template_parameter) 10290 << FirstRecord << FirstModule.empty() << FirstModule << Range 10291 << DiffType; 10292 }; 10293 auto ODRDiagTemplateNote = [&SecondModule, this]( 10294 SourceLocation Loc, SourceRange Range, 10295 ODRTemplateDifference DiffType) { 10296 return Diag(Loc, diag::note_module_odr_violation_template_parameter) 10297 << SecondModule << Range << DiffType; 10298 }; 10299 10300 const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first); 10301 const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first); 10302 10303 assert(FirstDecl->getKind() == SecondDecl->getKind() && 10304 "Parameter Decl's should be the same kind."); 10305 10306 DeclarationName FirstName = FirstDecl->getDeclName(); 10307 DeclarationName SecondName = SecondDecl->getDeclName(); 10308 10309 if (FirstName != SecondName) { 10310 const bool FirstNameEmpty = 10311 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo(); 10312 const bool SecondNameEmpty = 10313 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo(); 10314 assert((!FirstNameEmpty || !SecondNameEmpty) && 10315 "Both template parameters cannot be unnamed."); 10316 ODRDiagTemplateError(FirstDecl->getLocation(), 10317 FirstDecl->getSourceRange(), 10318 FirstNameEmpty ? ParamEmptyName : ParamName) 10319 << FirstName; 10320 ODRDiagTemplateNote(SecondDecl->getLocation(), 10321 SecondDecl->getSourceRange(), 10322 SecondNameEmpty ? ParamEmptyName : ParamName) 10323 << SecondName; 10324 break; 10325 } 10326 10327 switch (FirstDecl->getKind()) { 10328 default: 10329 llvm_unreachable("Invalid template parameter type."); 10330 case Decl::TemplateTypeParm: { 10331 const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl); 10332 const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl); 10333 const bool HasFirstDefaultArgument = 10334 FirstParam->hasDefaultArgument() && 10335 !FirstParam->defaultArgumentWasInherited(); 10336 const bool HasSecondDefaultArgument = 10337 SecondParam->hasDefaultArgument() && 10338 !SecondParam->defaultArgumentWasInherited(); 10339 10340 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10341 ODRDiagTemplateError(FirstDecl->getLocation(), 10342 FirstDecl->getSourceRange(), 10343 ParamSingleDefaultArgument) 10344 << HasFirstDefaultArgument; 10345 ODRDiagTemplateNote(SecondDecl->getLocation(), 10346 SecondDecl->getSourceRange(), 10347 ParamSingleDefaultArgument) 10348 << HasSecondDefaultArgument; 10349 break; 10350 } 10351 10352 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10353 "Expecting default arguments."); 10354 10355 ODRDiagTemplateError(FirstDecl->getLocation(), 10356 FirstDecl->getSourceRange(), 10357 ParamDifferentDefaultArgument); 10358 ODRDiagTemplateNote(SecondDecl->getLocation(), 10359 SecondDecl->getSourceRange(), 10360 ParamDifferentDefaultArgument); 10361 10362 break; 10363 } 10364 case Decl::NonTypeTemplateParm: { 10365 const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl); 10366 const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl); 10367 const bool HasFirstDefaultArgument = 10368 FirstParam->hasDefaultArgument() && 10369 !FirstParam->defaultArgumentWasInherited(); 10370 const bool HasSecondDefaultArgument = 10371 SecondParam->hasDefaultArgument() && 10372 !SecondParam->defaultArgumentWasInherited(); 10373 10374 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10375 ODRDiagTemplateError(FirstDecl->getLocation(), 10376 FirstDecl->getSourceRange(), 10377 ParamSingleDefaultArgument) 10378 << HasFirstDefaultArgument; 10379 ODRDiagTemplateNote(SecondDecl->getLocation(), 10380 SecondDecl->getSourceRange(), 10381 ParamSingleDefaultArgument) 10382 << HasSecondDefaultArgument; 10383 break; 10384 } 10385 10386 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10387 "Expecting default arguments."); 10388 10389 ODRDiagTemplateError(FirstDecl->getLocation(), 10390 FirstDecl->getSourceRange(), 10391 ParamDifferentDefaultArgument); 10392 ODRDiagTemplateNote(SecondDecl->getLocation(), 10393 SecondDecl->getSourceRange(), 10394 ParamDifferentDefaultArgument); 10395 10396 break; 10397 } 10398 case Decl::TemplateTemplateParm: { 10399 const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl); 10400 const auto *SecondParam = 10401 cast<TemplateTemplateParmDecl>(SecondDecl); 10402 const bool HasFirstDefaultArgument = 10403 FirstParam->hasDefaultArgument() && 10404 !FirstParam->defaultArgumentWasInherited(); 10405 const bool HasSecondDefaultArgument = 10406 SecondParam->hasDefaultArgument() && 10407 !SecondParam->defaultArgumentWasInherited(); 10408 10409 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10410 ODRDiagTemplateError(FirstDecl->getLocation(), 10411 FirstDecl->getSourceRange(), 10412 ParamSingleDefaultArgument) 10413 << HasFirstDefaultArgument; 10414 ODRDiagTemplateNote(SecondDecl->getLocation(), 10415 SecondDecl->getSourceRange(), 10416 ParamSingleDefaultArgument) 10417 << HasSecondDefaultArgument; 10418 break; 10419 } 10420 10421 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10422 "Expecting default arguments."); 10423 10424 ODRDiagTemplateError(FirstDecl->getLocation(), 10425 FirstDecl->getSourceRange(), 10426 ParamDifferentDefaultArgument); 10427 ODRDiagTemplateNote(SecondDecl->getLocation(), 10428 SecondDecl->getSourceRange(), 10429 ParamDifferentDefaultArgument); 10430 10431 break; 10432 } 10433 } 10434 10435 break; 10436 } 10437 10438 if (FirstIt != FirstEnd) { 10439 Diagnosed = true; 10440 break; 10441 } 10442 } 10443 10444 DeclHashes FirstHashes; 10445 DeclHashes SecondHashes; 10446 const DeclContext *DC = FirstRecord; 10447 PopulateHashes(FirstHashes, FirstRecord, DC); 10448 PopulateHashes(SecondHashes, SecondRecord, DC); 10449 10450 auto DR = FindTypeDiffs(FirstHashes, SecondHashes); 10451 ODRMismatchDecl FirstDiffType = DR.FirstDiffType; 10452 ODRMismatchDecl SecondDiffType = DR.SecondDiffType; 10453 Decl *FirstDecl = DR.FirstDecl; 10454 Decl *SecondDecl = DR.SecondDecl; 10455 10456 if (FirstDiffType == Other || SecondDiffType == Other) { 10457 DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord, 10458 SecondModule); 10459 Diagnosed = true; 10460 break; 10461 } 10462 10463 if (FirstDiffType != SecondDiffType) { 10464 DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord, 10465 SecondModule); 10466 Diagnosed = true; 10467 break; 10468 } 10469 10470 assert(FirstDiffType == SecondDiffType); 10471 10472 switch (FirstDiffType) { 10473 case Other: 10474 case EndOfClass: 10475 case PublicSpecifer: 10476 case PrivateSpecifer: 10477 case ProtectedSpecifer: 10478 llvm_unreachable("Invalid diff type"); 10479 10480 case StaticAssert: { 10481 StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl); 10482 StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl); 10483 10484 Expr *FirstExpr = FirstSA->getAssertExpr(); 10485 Expr *SecondExpr = SecondSA->getAssertExpr(); 10486 unsigned FirstODRHash = ComputeODRHash(FirstExpr); 10487 unsigned SecondODRHash = ComputeODRHash(SecondExpr); 10488 if (FirstODRHash != SecondODRHash) { 10489 ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(), 10490 FirstExpr->getSourceRange(), StaticAssertCondition); 10491 ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(), 10492 SecondExpr->getSourceRange(), StaticAssertCondition); 10493 Diagnosed = true; 10494 break; 10495 } 10496 10497 StringLiteral *FirstStr = FirstSA->getMessage(); 10498 StringLiteral *SecondStr = SecondSA->getMessage(); 10499 assert((FirstStr || SecondStr) && "Both messages cannot be empty"); 10500 if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) { 10501 SourceLocation FirstLoc, SecondLoc; 10502 SourceRange FirstRange, SecondRange; 10503 if (FirstStr) { 10504 FirstLoc = FirstStr->getBeginLoc(); 10505 FirstRange = FirstStr->getSourceRange(); 10506 } else { 10507 FirstLoc = FirstSA->getBeginLoc(); 10508 FirstRange = FirstSA->getSourceRange(); 10509 } 10510 if (SecondStr) { 10511 SecondLoc = SecondStr->getBeginLoc(); 10512 SecondRange = SecondStr->getSourceRange(); 10513 } else { 10514 SecondLoc = SecondSA->getBeginLoc(); 10515 SecondRange = SecondSA->getSourceRange(); 10516 } 10517 ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange, 10518 StaticAssertOnlyMessage) 10519 << (FirstStr == nullptr); 10520 ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange, 10521 StaticAssertOnlyMessage) 10522 << (SecondStr == nullptr); 10523 Diagnosed = true; 10524 break; 10525 } 10526 10527 if (FirstStr && SecondStr && 10528 FirstStr->getString() != SecondStr->getString()) { 10529 ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(), 10530 FirstStr->getSourceRange(), StaticAssertMessage); 10531 ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(), 10532 SecondStr->getSourceRange(), StaticAssertMessage); 10533 Diagnosed = true; 10534 break; 10535 } 10536 break; 10537 } 10538 case Field: { 10539 Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule, 10540 cast<FieldDecl>(FirstDecl), 10541 cast<FieldDecl>(SecondDecl)); 10542 break; 10543 } 10544 case CXXMethod: { 10545 enum { 10546 DiagMethod, 10547 DiagConstructor, 10548 DiagDestructor, 10549 } FirstMethodType, 10550 SecondMethodType; 10551 auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) { 10552 if (isa<CXXConstructorDecl>(D)) return DiagConstructor; 10553 if (isa<CXXDestructorDecl>(D)) return DiagDestructor; 10554 return DiagMethod; 10555 }; 10556 const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl); 10557 const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl); 10558 FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod); 10559 SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod); 10560 auto FirstName = FirstMethod->getDeclName(); 10561 auto SecondName = SecondMethod->getDeclName(); 10562 if (FirstMethodType != SecondMethodType || FirstName != SecondName) { 10563 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10564 FirstMethod->getSourceRange(), MethodName) 10565 << FirstMethodType << FirstName; 10566 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10567 SecondMethod->getSourceRange(), MethodName) 10568 << SecondMethodType << SecondName; 10569 10570 Diagnosed = true; 10571 break; 10572 } 10573 10574 const bool FirstDeleted = FirstMethod->isDeletedAsWritten(); 10575 const bool SecondDeleted = SecondMethod->isDeletedAsWritten(); 10576 if (FirstDeleted != SecondDeleted) { 10577 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10578 FirstMethod->getSourceRange(), MethodDeleted) 10579 << FirstMethodType << FirstName << FirstDeleted; 10580 10581 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10582 SecondMethod->getSourceRange(), MethodDeleted) 10583 << SecondMethodType << SecondName << SecondDeleted; 10584 Diagnosed = true; 10585 break; 10586 } 10587 10588 const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted(); 10589 const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted(); 10590 if (FirstDefaulted != SecondDefaulted) { 10591 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10592 FirstMethod->getSourceRange(), MethodDefaulted) 10593 << FirstMethodType << FirstName << FirstDefaulted; 10594 10595 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10596 SecondMethod->getSourceRange(), MethodDefaulted) 10597 << SecondMethodType << SecondName << SecondDefaulted; 10598 Diagnosed = true; 10599 break; 10600 } 10601 10602 const bool FirstVirtual = FirstMethod->isVirtualAsWritten(); 10603 const bool SecondVirtual = SecondMethod->isVirtualAsWritten(); 10604 const bool FirstPure = FirstMethod->isPure(); 10605 const bool SecondPure = SecondMethod->isPure(); 10606 if ((FirstVirtual || SecondVirtual) && 10607 (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) { 10608 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10609 FirstMethod->getSourceRange(), MethodVirtual) 10610 << FirstMethodType << FirstName << FirstPure << FirstVirtual; 10611 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10612 SecondMethod->getSourceRange(), MethodVirtual) 10613 << SecondMethodType << SecondName << SecondPure << SecondVirtual; 10614 Diagnosed = true; 10615 break; 10616 } 10617 10618 // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging, 10619 // FirstDecl is the canonical Decl of SecondDecl, so the storage 10620 // class needs to be checked instead. 10621 const auto FirstStorage = FirstMethod->getStorageClass(); 10622 const auto SecondStorage = SecondMethod->getStorageClass(); 10623 const bool FirstStatic = FirstStorage == SC_Static; 10624 const bool SecondStatic = SecondStorage == SC_Static; 10625 if (FirstStatic != SecondStatic) { 10626 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10627 FirstMethod->getSourceRange(), MethodStatic) 10628 << FirstMethodType << FirstName << FirstStatic; 10629 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10630 SecondMethod->getSourceRange(), MethodStatic) 10631 << SecondMethodType << SecondName << SecondStatic; 10632 Diagnosed = true; 10633 break; 10634 } 10635 10636 const bool FirstVolatile = FirstMethod->isVolatile(); 10637 const bool SecondVolatile = SecondMethod->isVolatile(); 10638 if (FirstVolatile != SecondVolatile) { 10639 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10640 FirstMethod->getSourceRange(), MethodVolatile) 10641 << FirstMethodType << FirstName << FirstVolatile; 10642 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10643 SecondMethod->getSourceRange(), MethodVolatile) 10644 << SecondMethodType << SecondName << SecondVolatile; 10645 Diagnosed = true; 10646 break; 10647 } 10648 10649 const bool FirstConst = FirstMethod->isConst(); 10650 const bool SecondConst = SecondMethod->isConst(); 10651 if (FirstConst != SecondConst) { 10652 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10653 FirstMethod->getSourceRange(), MethodConst) 10654 << FirstMethodType << FirstName << FirstConst; 10655 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10656 SecondMethod->getSourceRange(), MethodConst) 10657 << SecondMethodType << SecondName << SecondConst; 10658 Diagnosed = true; 10659 break; 10660 } 10661 10662 const bool FirstInline = FirstMethod->isInlineSpecified(); 10663 const bool SecondInline = SecondMethod->isInlineSpecified(); 10664 if (FirstInline != SecondInline) { 10665 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10666 FirstMethod->getSourceRange(), MethodInline) 10667 << FirstMethodType << FirstName << FirstInline; 10668 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10669 SecondMethod->getSourceRange(), MethodInline) 10670 << SecondMethodType << SecondName << SecondInline; 10671 Diagnosed = true; 10672 break; 10673 } 10674 10675 const unsigned FirstNumParameters = FirstMethod->param_size(); 10676 const unsigned SecondNumParameters = SecondMethod->param_size(); 10677 if (FirstNumParameters != SecondNumParameters) { 10678 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10679 FirstMethod->getSourceRange(), 10680 MethodNumberParameters) 10681 << FirstMethodType << FirstName << FirstNumParameters; 10682 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10683 SecondMethod->getSourceRange(), 10684 MethodNumberParameters) 10685 << SecondMethodType << SecondName << SecondNumParameters; 10686 Diagnosed = true; 10687 break; 10688 } 10689 10690 // Need this status boolean to know when break out of the switch. 10691 bool ParameterMismatch = false; 10692 for (unsigned I = 0; I < FirstNumParameters; ++I) { 10693 const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I); 10694 const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I); 10695 10696 QualType FirstParamType = FirstParam->getType(); 10697 QualType SecondParamType = SecondParam->getType(); 10698 if (FirstParamType != SecondParamType && 10699 ComputeQualTypeODRHash(FirstParamType) != 10700 ComputeQualTypeODRHash(SecondParamType)) { 10701 if (const DecayedType *ParamDecayedType = 10702 FirstParamType->getAs<DecayedType>()) { 10703 ODRDiagDeclError( 10704 FirstRecord, FirstModule, FirstMethod->getLocation(), 10705 FirstMethod->getSourceRange(), MethodParameterType) 10706 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10707 << true << ParamDecayedType->getOriginalType(); 10708 } else { 10709 ODRDiagDeclError( 10710 FirstRecord, FirstModule, FirstMethod->getLocation(), 10711 FirstMethod->getSourceRange(), MethodParameterType) 10712 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10713 << false; 10714 } 10715 10716 if (const DecayedType *ParamDecayedType = 10717 SecondParamType->getAs<DecayedType>()) { 10718 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10719 SecondMethod->getSourceRange(), 10720 MethodParameterType) 10721 << SecondMethodType << SecondName << (I + 1) 10722 << SecondParamType << true 10723 << ParamDecayedType->getOriginalType(); 10724 } else { 10725 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10726 SecondMethod->getSourceRange(), 10727 MethodParameterType) 10728 << SecondMethodType << SecondName << (I + 1) 10729 << SecondParamType << false; 10730 } 10731 ParameterMismatch = true; 10732 break; 10733 } 10734 10735 DeclarationName FirstParamName = FirstParam->getDeclName(); 10736 DeclarationName SecondParamName = SecondParam->getDeclName(); 10737 if (FirstParamName != SecondParamName) { 10738 ODRDiagDeclError(FirstRecord, FirstModule, 10739 FirstMethod->getLocation(), 10740 FirstMethod->getSourceRange(), MethodParameterName) 10741 << FirstMethodType << FirstName << (I + 1) << FirstParamName; 10742 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10743 SecondMethod->getSourceRange(), MethodParameterName) 10744 << SecondMethodType << SecondName << (I + 1) << SecondParamName; 10745 ParameterMismatch = true; 10746 break; 10747 } 10748 10749 const Expr *FirstInit = FirstParam->getInit(); 10750 const Expr *SecondInit = SecondParam->getInit(); 10751 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 10752 ODRDiagDeclError(FirstRecord, FirstModule, 10753 FirstMethod->getLocation(), 10754 FirstMethod->getSourceRange(), 10755 MethodParameterSingleDefaultArgument) 10756 << FirstMethodType << FirstName << (I + 1) 10757 << (FirstInit == nullptr) 10758 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 10759 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10760 SecondMethod->getSourceRange(), 10761 MethodParameterSingleDefaultArgument) 10762 << SecondMethodType << SecondName << (I + 1) 10763 << (SecondInit == nullptr) 10764 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 10765 ParameterMismatch = true; 10766 break; 10767 } 10768 10769 if (FirstInit && SecondInit && 10770 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 10771 ODRDiagDeclError(FirstRecord, FirstModule, 10772 FirstMethod->getLocation(), 10773 FirstMethod->getSourceRange(), 10774 MethodParameterDifferentDefaultArgument) 10775 << FirstMethodType << FirstName << (I + 1) 10776 << FirstInit->getSourceRange(); 10777 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10778 SecondMethod->getSourceRange(), 10779 MethodParameterDifferentDefaultArgument) 10780 << SecondMethodType << SecondName << (I + 1) 10781 << SecondInit->getSourceRange(); 10782 ParameterMismatch = true; 10783 break; 10784 10785 } 10786 } 10787 10788 if (ParameterMismatch) { 10789 Diagnosed = true; 10790 break; 10791 } 10792 10793 const auto *FirstTemplateArgs = 10794 FirstMethod->getTemplateSpecializationArgs(); 10795 const auto *SecondTemplateArgs = 10796 SecondMethod->getTemplateSpecializationArgs(); 10797 10798 if ((FirstTemplateArgs && !SecondTemplateArgs) || 10799 (!FirstTemplateArgs && SecondTemplateArgs)) { 10800 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10801 FirstMethod->getSourceRange(), 10802 MethodNoTemplateArguments) 10803 << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr); 10804 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10805 SecondMethod->getSourceRange(), 10806 MethodNoTemplateArguments) 10807 << SecondMethodType << SecondName 10808 << (SecondTemplateArgs != nullptr); 10809 10810 Diagnosed = true; 10811 break; 10812 } 10813 10814 if (FirstTemplateArgs && SecondTemplateArgs) { 10815 // Remove pack expansions from argument list. 10816 auto ExpandTemplateArgumentList = 10817 [](const TemplateArgumentList *TAL) { 10818 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList; 10819 for (const TemplateArgument &TA : TAL->asArray()) { 10820 if (TA.getKind() != TemplateArgument::Pack) { 10821 ExpandedList.push_back(&TA); 10822 continue; 10823 } 10824 for (const TemplateArgument &PackTA : TA.getPackAsArray()) { 10825 ExpandedList.push_back(&PackTA); 10826 } 10827 } 10828 return ExpandedList; 10829 }; 10830 llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList = 10831 ExpandTemplateArgumentList(FirstTemplateArgs); 10832 llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList = 10833 ExpandTemplateArgumentList(SecondTemplateArgs); 10834 10835 if (FirstExpandedList.size() != SecondExpandedList.size()) { 10836 ODRDiagDeclError(FirstRecord, FirstModule, 10837 FirstMethod->getLocation(), 10838 FirstMethod->getSourceRange(), 10839 MethodDifferentNumberTemplateArguments) 10840 << FirstMethodType << FirstName 10841 << (unsigned)FirstExpandedList.size(); 10842 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10843 SecondMethod->getSourceRange(), 10844 MethodDifferentNumberTemplateArguments) 10845 << SecondMethodType << SecondName 10846 << (unsigned)SecondExpandedList.size(); 10847 10848 Diagnosed = true; 10849 break; 10850 } 10851 10852 bool TemplateArgumentMismatch = false; 10853 for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) { 10854 const TemplateArgument &FirstTA = *FirstExpandedList[i], 10855 &SecondTA = *SecondExpandedList[i]; 10856 if (ComputeTemplateArgumentODRHash(FirstTA) == 10857 ComputeTemplateArgumentODRHash(SecondTA)) { 10858 continue; 10859 } 10860 10861 ODRDiagDeclError( 10862 FirstRecord, FirstModule, FirstMethod->getLocation(), 10863 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument) 10864 << FirstMethodType << FirstName << FirstTA << i + 1; 10865 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10866 SecondMethod->getSourceRange(), 10867 MethodDifferentTemplateArgument) 10868 << SecondMethodType << SecondName << SecondTA << i + 1; 10869 10870 TemplateArgumentMismatch = true; 10871 break; 10872 } 10873 10874 if (TemplateArgumentMismatch) { 10875 Diagnosed = true; 10876 break; 10877 } 10878 } 10879 10880 // Compute the hash of the method as if it has no body. 10881 auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) { 10882 Hash.clear(); 10883 Hash.AddFunctionDecl(D, true /*SkipBody*/); 10884 return Hash.CalculateHash(); 10885 }; 10886 10887 // Compare the hash generated to the hash stored. A difference means 10888 // that a body was present in the original source. Due to merging, 10889 // the stardard way of detecting a body will not work. 10890 const bool HasFirstBody = 10891 ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash(); 10892 const bool HasSecondBody = 10893 ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash(); 10894 10895 if (HasFirstBody != HasSecondBody) { 10896 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10897 FirstMethod->getSourceRange(), MethodSingleBody) 10898 << FirstMethodType << FirstName << HasFirstBody; 10899 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10900 SecondMethod->getSourceRange(), MethodSingleBody) 10901 << SecondMethodType << SecondName << HasSecondBody; 10902 Diagnosed = true; 10903 break; 10904 } 10905 10906 if (HasFirstBody && HasSecondBody) { 10907 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10908 FirstMethod->getSourceRange(), MethodDifferentBody) 10909 << FirstMethodType << FirstName; 10910 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10911 SecondMethod->getSourceRange(), MethodDifferentBody) 10912 << SecondMethodType << SecondName; 10913 Diagnosed = true; 10914 break; 10915 } 10916 10917 break; 10918 } 10919 case TypeAlias: 10920 case TypeDef: { 10921 Diagnosed = ODRDiagTypeDefOrAlias( 10922 FirstRecord, FirstModule, SecondModule, 10923 cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl), 10924 FirstDiffType == TypeAlias); 10925 break; 10926 } 10927 case Var: { 10928 Diagnosed = 10929 ODRDiagVar(FirstRecord, FirstModule, SecondModule, 10930 cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl)); 10931 break; 10932 } 10933 case Friend: { 10934 FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl); 10935 FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl); 10936 10937 NamedDecl *FirstND = FirstFriend->getFriendDecl(); 10938 NamedDecl *SecondND = SecondFriend->getFriendDecl(); 10939 10940 TypeSourceInfo *FirstTSI = FirstFriend->getFriendType(); 10941 TypeSourceInfo *SecondTSI = SecondFriend->getFriendType(); 10942 10943 if (FirstND && SecondND) { 10944 ODRDiagDeclError(FirstRecord, FirstModule, 10945 FirstFriend->getFriendLoc(), 10946 FirstFriend->getSourceRange(), FriendFunction) 10947 << FirstND; 10948 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10949 SecondFriend->getSourceRange(), FriendFunction) 10950 << SecondND; 10951 10952 Diagnosed = true; 10953 break; 10954 } 10955 10956 if (FirstTSI && SecondTSI) { 10957 QualType FirstFriendType = FirstTSI->getType(); 10958 QualType SecondFriendType = SecondTSI->getType(); 10959 assert(ComputeQualTypeODRHash(FirstFriendType) != 10960 ComputeQualTypeODRHash(SecondFriendType)); 10961 ODRDiagDeclError(FirstRecord, FirstModule, 10962 FirstFriend->getFriendLoc(), 10963 FirstFriend->getSourceRange(), FriendType) 10964 << FirstFriendType; 10965 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10966 SecondFriend->getSourceRange(), FriendType) 10967 << SecondFriendType; 10968 Diagnosed = true; 10969 break; 10970 } 10971 10972 ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(), 10973 FirstFriend->getSourceRange(), FriendTypeFunction) 10974 << (FirstTSI == nullptr); 10975 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10976 SecondFriend->getSourceRange(), FriendTypeFunction) 10977 << (SecondTSI == nullptr); 10978 10979 Diagnosed = true; 10980 break; 10981 } 10982 case FunctionTemplate: { 10983 FunctionTemplateDecl *FirstTemplate = 10984 cast<FunctionTemplateDecl>(FirstDecl); 10985 FunctionTemplateDecl *SecondTemplate = 10986 cast<FunctionTemplateDecl>(SecondDecl); 10987 10988 TemplateParameterList *FirstTPL = 10989 FirstTemplate->getTemplateParameters(); 10990 TemplateParameterList *SecondTPL = 10991 SecondTemplate->getTemplateParameters(); 10992 10993 if (FirstTPL->size() != SecondTPL->size()) { 10994 ODRDiagDeclError(FirstRecord, FirstModule, 10995 FirstTemplate->getLocation(), 10996 FirstTemplate->getSourceRange(), 10997 FunctionTemplateDifferentNumberParameters) 10998 << FirstTemplate << FirstTPL->size(); 10999 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11000 SecondTemplate->getSourceRange(), 11001 FunctionTemplateDifferentNumberParameters) 11002 << SecondTemplate << SecondTPL->size(); 11003 11004 Diagnosed = true; 11005 break; 11006 } 11007 11008 bool ParameterMismatch = false; 11009 for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) { 11010 NamedDecl *FirstParam = FirstTPL->getParam(i); 11011 NamedDecl *SecondParam = SecondTPL->getParam(i); 11012 11013 if (FirstParam->getKind() != SecondParam->getKind()) { 11014 enum { 11015 TemplateTypeParameter, 11016 NonTypeTemplateParameter, 11017 TemplateTemplateParameter, 11018 }; 11019 auto GetParamType = [](NamedDecl *D) { 11020 switch (D->getKind()) { 11021 default: 11022 llvm_unreachable("Unexpected template parameter type"); 11023 case Decl::TemplateTypeParm: 11024 return TemplateTypeParameter; 11025 case Decl::NonTypeTemplateParm: 11026 return NonTypeTemplateParameter; 11027 case Decl::TemplateTemplateParm: 11028 return TemplateTemplateParameter; 11029 } 11030 }; 11031 11032 ODRDiagDeclError(FirstRecord, FirstModule, 11033 FirstTemplate->getLocation(), 11034 FirstTemplate->getSourceRange(), 11035 FunctionTemplateParameterDifferentKind) 11036 << FirstTemplate << (i + 1) << GetParamType(FirstParam); 11037 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11038 SecondTemplate->getSourceRange(), 11039 FunctionTemplateParameterDifferentKind) 11040 << SecondTemplate << (i + 1) << GetParamType(SecondParam); 11041 11042 ParameterMismatch = true; 11043 break; 11044 } 11045 11046 if (FirstParam->getName() != SecondParam->getName()) { 11047 ODRDiagDeclError( 11048 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11049 FirstTemplate->getSourceRange(), FunctionTemplateParameterName) 11050 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier() 11051 << FirstParam; 11052 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11053 SecondTemplate->getSourceRange(), 11054 FunctionTemplateParameterName) 11055 << SecondTemplate << (i + 1) 11056 << (bool)SecondParam->getIdentifier() << SecondParam; 11057 ParameterMismatch = true; 11058 break; 11059 } 11060 11061 if (isa<TemplateTypeParmDecl>(FirstParam) && 11062 isa<TemplateTypeParmDecl>(SecondParam)) { 11063 TemplateTypeParmDecl *FirstTTPD = 11064 cast<TemplateTypeParmDecl>(FirstParam); 11065 TemplateTypeParmDecl *SecondTTPD = 11066 cast<TemplateTypeParmDecl>(SecondParam); 11067 bool HasFirstDefaultArgument = 11068 FirstTTPD->hasDefaultArgument() && 11069 !FirstTTPD->defaultArgumentWasInherited(); 11070 bool HasSecondDefaultArgument = 11071 SecondTTPD->hasDefaultArgument() && 11072 !SecondTTPD->defaultArgumentWasInherited(); 11073 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11074 ODRDiagDeclError(FirstRecord, FirstModule, 11075 FirstTemplate->getLocation(), 11076 FirstTemplate->getSourceRange(), 11077 FunctionTemplateParameterSingleDefaultArgument) 11078 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11079 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11080 SecondTemplate->getSourceRange(), 11081 FunctionTemplateParameterSingleDefaultArgument) 11082 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11083 ParameterMismatch = true; 11084 break; 11085 } 11086 11087 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11088 QualType FirstType = FirstTTPD->getDefaultArgument(); 11089 QualType SecondType = SecondTTPD->getDefaultArgument(); 11090 if (ComputeQualTypeODRHash(FirstType) != 11091 ComputeQualTypeODRHash(SecondType)) { 11092 ODRDiagDeclError( 11093 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11094 FirstTemplate->getSourceRange(), 11095 FunctionTemplateParameterDifferentDefaultArgument) 11096 << FirstTemplate << (i + 1) << FirstType; 11097 ODRDiagDeclNote( 11098 SecondModule, SecondTemplate->getLocation(), 11099 SecondTemplate->getSourceRange(), 11100 FunctionTemplateParameterDifferentDefaultArgument) 11101 << SecondTemplate << (i + 1) << SecondType; 11102 ParameterMismatch = true; 11103 break; 11104 } 11105 } 11106 11107 if (FirstTTPD->isParameterPack() != 11108 SecondTTPD->isParameterPack()) { 11109 ODRDiagDeclError(FirstRecord, FirstModule, 11110 FirstTemplate->getLocation(), 11111 FirstTemplate->getSourceRange(), 11112 FunctionTemplatePackParameter) 11113 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 11114 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11115 SecondTemplate->getSourceRange(), 11116 FunctionTemplatePackParameter) 11117 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 11118 ParameterMismatch = true; 11119 break; 11120 } 11121 } 11122 11123 if (isa<TemplateTemplateParmDecl>(FirstParam) && 11124 isa<TemplateTemplateParmDecl>(SecondParam)) { 11125 TemplateTemplateParmDecl *FirstTTPD = 11126 cast<TemplateTemplateParmDecl>(FirstParam); 11127 TemplateTemplateParmDecl *SecondTTPD = 11128 cast<TemplateTemplateParmDecl>(SecondParam); 11129 11130 TemplateParameterList *FirstTPL = 11131 FirstTTPD->getTemplateParameters(); 11132 TemplateParameterList *SecondTPL = 11133 SecondTTPD->getTemplateParameters(); 11134 11135 if (ComputeTemplateParameterListODRHash(FirstTPL) != 11136 ComputeTemplateParameterListODRHash(SecondTPL)) { 11137 ODRDiagDeclError(FirstRecord, FirstModule, 11138 FirstTemplate->getLocation(), 11139 FirstTemplate->getSourceRange(), 11140 FunctionTemplateParameterDifferentType) 11141 << FirstTemplate << (i + 1); 11142 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11143 SecondTemplate->getSourceRange(), 11144 FunctionTemplateParameterDifferentType) 11145 << SecondTemplate << (i + 1); 11146 ParameterMismatch = true; 11147 break; 11148 } 11149 11150 bool HasFirstDefaultArgument = 11151 FirstTTPD->hasDefaultArgument() && 11152 !FirstTTPD->defaultArgumentWasInherited(); 11153 bool HasSecondDefaultArgument = 11154 SecondTTPD->hasDefaultArgument() && 11155 !SecondTTPD->defaultArgumentWasInherited(); 11156 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11157 ODRDiagDeclError(FirstRecord, FirstModule, 11158 FirstTemplate->getLocation(), 11159 FirstTemplate->getSourceRange(), 11160 FunctionTemplateParameterSingleDefaultArgument) 11161 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11162 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11163 SecondTemplate->getSourceRange(), 11164 FunctionTemplateParameterSingleDefaultArgument) 11165 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11166 ParameterMismatch = true; 11167 break; 11168 } 11169 11170 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11171 TemplateArgument FirstTA = 11172 FirstTTPD->getDefaultArgument().getArgument(); 11173 TemplateArgument SecondTA = 11174 SecondTTPD->getDefaultArgument().getArgument(); 11175 if (ComputeTemplateArgumentODRHash(FirstTA) != 11176 ComputeTemplateArgumentODRHash(SecondTA)) { 11177 ODRDiagDeclError( 11178 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11179 FirstTemplate->getSourceRange(), 11180 FunctionTemplateParameterDifferentDefaultArgument) 11181 << FirstTemplate << (i + 1) << FirstTA; 11182 ODRDiagDeclNote( 11183 SecondModule, SecondTemplate->getLocation(), 11184 SecondTemplate->getSourceRange(), 11185 FunctionTemplateParameterDifferentDefaultArgument) 11186 << SecondTemplate << (i + 1) << SecondTA; 11187 ParameterMismatch = true; 11188 break; 11189 } 11190 } 11191 11192 if (FirstTTPD->isParameterPack() != 11193 SecondTTPD->isParameterPack()) { 11194 ODRDiagDeclError(FirstRecord, FirstModule, 11195 FirstTemplate->getLocation(), 11196 FirstTemplate->getSourceRange(), 11197 FunctionTemplatePackParameter) 11198 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 11199 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11200 SecondTemplate->getSourceRange(), 11201 FunctionTemplatePackParameter) 11202 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 11203 ParameterMismatch = true; 11204 break; 11205 } 11206 } 11207 11208 if (isa<NonTypeTemplateParmDecl>(FirstParam) && 11209 isa<NonTypeTemplateParmDecl>(SecondParam)) { 11210 NonTypeTemplateParmDecl *FirstNTTPD = 11211 cast<NonTypeTemplateParmDecl>(FirstParam); 11212 NonTypeTemplateParmDecl *SecondNTTPD = 11213 cast<NonTypeTemplateParmDecl>(SecondParam); 11214 11215 QualType FirstType = FirstNTTPD->getType(); 11216 QualType SecondType = SecondNTTPD->getType(); 11217 if (ComputeQualTypeODRHash(FirstType) != 11218 ComputeQualTypeODRHash(SecondType)) { 11219 ODRDiagDeclError(FirstRecord, FirstModule, 11220 FirstTemplate->getLocation(), 11221 FirstTemplate->getSourceRange(), 11222 FunctionTemplateParameterDifferentType) 11223 << FirstTemplate << (i + 1); 11224 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11225 SecondTemplate->getSourceRange(), 11226 FunctionTemplateParameterDifferentType) 11227 << SecondTemplate << (i + 1); 11228 ParameterMismatch = true; 11229 break; 11230 } 11231 11232 bool HasFirstDefaultArgument = 11233 FirstNTTPD->hasDefaultArgument() && 11234 !FirstNTTPD->defaultArgumentWasInherited(); 11235 bool HasSecondDefaultArgument = 11236 SecondNTTPD->hasDefaultArgument() && 11237 !SecondNTTPD->defaultArgumentWasInherited(); 11238 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11239 ODRDiagDeclError(FirstRecord, FirstModule, 11240 FirstTemplate->getLocation(), 11241 FirstTemplate->getSourceRange(), 11242 FunctionTemplateParameterSingleDefaultArgument) 11243 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11244 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11245 SecondTemplate->getSourceRange(), 11246 FunctionTemplateParameterSingleDefaultArgument) 11247 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11248 ParameterMismatch = true; 11249 break; 11250 } 11251 11252 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11253 Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument(); 11254 Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument(); 11255 if (ComputeODRHash(FirstDefaultArgument) != 11256 ComputeODRHash(SecondDefaultArgument)) { 11257 ODRDiagDeclError( 11258 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11259 FirstTemplate->getSourceRange(), 11260 FunctionTemplateParameterDifferentDefaultArgument) 11261 << FirstTemplate << (i + 1) << FirstDefaultArgument; 11262 ODRDiagDeclNote( 11263 SecondModule, SecondTemplate->getLocation(), 11264 SecondTemplate->getSourceRange(), 11265 FunctionTemplateParameterDifferentDefaultArgument) 11266 << SecondTemplate << (i + 1) << SecondDefaultArgument; 11267 ParameterMismatch = true; 11268 break; 11269 } 11270 } 11271 11272 if (FirstNTTPD->isParameterPack() != 11273 SecondNTTPD->isParameterPack()) { 11274 ODRDiagDeclError(FirstRecord, FirstModule, 11275 FirstTemplate->getLocation(), 11276 FirstTemplate->getSourceRange(), 11277 FunctionTemplatePackParameter) 11278 << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack(); 11279 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11280 SecondTemplate->getSourceRange(), 11281 FunctionTemplatePackParameter) 11282 << SecondTemplate << (i + 1) 11283 << SecondNTTPD->isParameterPack(); 11284 ParameterMismatch = true; 11285 break; 11286 } 11287 } 11288 } 11289 11290 if (ParameterMismatch) { 11291 Diagnosed = true; 11292 break; 11293 } 11294 11295 break; 11296 } 11297 } 11298 11299 if (Diagnosed) 11300 continue; 11301 11302 Diag(FirstDecl->getLocation(), 11303 diag::err_module_odr_violation_mismatch_decl_unknown) 11304 << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType 11305 << FirstDecl->getSourceRange(); 11306 Diag(SecondDecl->getLocation(), 11307 diag::note_module_odr_violation_mismatch_decl_unknown) 11308 << SecondModule << FirstDiffType << SecondDecl->getSourceRange(); 11309 Diagnosed = true; 11310 } 11311 11312 if (!Diagnosed) { 11313 // All definitions are updates to the same declaration. This happens if a 11314 // module instantiates the declaration of a class template specialization 11315 // and two or more other modules instantiate its definition. 11316 // 11317 // FIXME: Indicate which modules had instantiations of this definition. 11318 // FIXME: How can this even happen? 11319 Diag(Merge.first->getLocation(), 11320 diag::err_module_odr_violation_different_instantiations) 11321 << Merge.first; 11322 } 11323 } 11324 11325 // Issue ODR failures diagnostics for functions. 11326 for (auto &Merge : FunctionOdrMergeFailures) { 11327 enum ODRFunctionDifference { 11328 ReturnType, 11329 ParameterName, 11330 ParameterType, 11331 ParameterSingleDefaultArgument, 11332 ParameterDifferentDefaultArgument, 11333 FunctionBody, 11334 }; 11335 11336 FunctionDecl *FirstFunction = Merge.first; 11337 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction); 11338 11339 bool Diagnosed = false; 11340 for (auto &SecondFunction : Merge.second) { 11341 11342 if (FirstFunction == SecondFunction) 11343 continue; 11344 11345 std::string SecondModule = 11346 getOwningModuleNameForDiagnostic(SecondFunction); 11347 11348 auto ODRDiagError = [FirstFunction, &FirstModule, 11349 this](SourceLocation Loc, SourceRange Range, 11350 ODRFunctionDifference DiffType) { 11351 return Diag(Loc, diag::err_module_odr_violation_function) 11352 << FirstFunction << FirstModule.empty() << FirstModule << Range 11353 << DiffType; 11354 }; 11355 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11356 SourceRange Range, 11357 ODRFunctionDifference DiffType) { 11358 return Diag(Loc, diag::note_module_odr_violation_function) 11359 << SecondModule << Range << DiffType; 11360 }; 11361 11362 if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) != 11363 ComputeQualTypeODRHash(SecondFunction->getReturnType())) { 11364 ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(), 11365 FirstFunction->getReturnTypeSourceRange(), ReturnType) 11366 << FirstFunction->getReturnType(); 11367 ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(), 11368 SecondFunction->getReturnTypeSourceRange(), ReturnType) 11369 << SecondFunction->getReturnType(); 11370 Diagnosed = true; 11371 break; 11372 } 11373 11374 assert(FirstFunction->param_size() == SecondFunction->param_size() && 11375 "Merged functions with different number of parameters"); 11376 11377 auto ParamSize = FirstFunction->param_size(); 11378 bool ParameterMismatch = false; 11379 for (unsigned I = 0; I < ParamSize; ++I) { 11380 auto *FirstParam = FirstFunction->getParamDecl(I); 11381 auto *SecondParam = SecondFunction->getParamDecl(I); 11382 11383 assert(getContext().hasSameType(FirstParam->getType(), 11384 SecondParam->getType()) && 11385 "Merged function has different parameter types."); 11386 11387 if (FirstParam->getDeclName() != SecondParam->getDeclName()) { 11388 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11389 ParameterName) 11390 << I + 1 << FirstParam->getDeclName(); 11391 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11392 ParameterName) 11393 << I + 1 << SecondParam->getDeclName(); 11394 ParameterMismatch = true; 11395 break; 11396 }; 11397 11398 QualType FirstParamType = FirstParam->getType(); 11399 QualType SecondParamType = SecondParam->getType(); 11400 if (FirstParamType != SecondParamType && 11401 ComputeQualTypeODRHash(FirstParamType) != 11402 ComputeQualTypeODRHash(SecondParamType)) { 11403 if (const DecayedType *ParamDecayedType = 11404 FirstParamType->getAs<DecayedType>()) { 11405 ODRDiagError(FirstParam->getLocation(), 11406 FirstParam->getSourceRange(), ParameterType) 11407 << (I + 1) << FirstParamType << true 11408 << ParamDecayedType->getOriginalType(); 11409 } else { 11410 ODRDiagError(FirstParam->getLocation(), 11411 FirstParam->getSourceRange(), ParameterType) 11412 << (I + 1) << FirstParamType << false; 11413 } 11414 11415 if (const DecayedType *ParamDecayedType = 11416 SecondParamType->getAs<DecayedType>()) { 11417 ODRDiagNote(SecondParam->getLocation(), 11418 SecondParam->getSourceRange(), ParameterType) 11419 << (I + 1) << SecondParamType << true 11420 << ParamDecayedType->getOriginalType(); 11421 } else { 11422 ODRDiagNote(SecondParam->getLocation(), 11423 SecondParam->getSourceRange(), ParameterType) 11424 << (I + 1) << SecondParamType << false; 11425 } 11426 ParameterMismatch = true; 11427 break; 11428 } 11429 11430 const Expr *FirstInit = FirstParam->getInit(); 11431 const Expr *SecondInit = SecondParam->getInit(); 11432 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 11433 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11434 ParameterSingleDefaultArgument) 11435 << (I + 1) << (FirstInit == nullptr) 11436 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 11437 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11438 ParameterSingleDefaultArgument) 11439 << (I + 1) << (SecondInit == nullptr) 11440 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 11441 ParameterMismatch = true; 11442 break; 11443 } 11444 11445 if (FirstInit && SecondInit && 11446 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11447 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11448 ParameterDifferentDefaultArgument) 11449 << (I + 1) << FirstInit->getSourceRange(); 11450 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11451 ParameterDifferentDefaultArgument) 11452 << (I + 1) << SecondInit->getSourceRange(); 11453 ParameterMismatch = true; 11454 break; 11455 } 11456 11457 assert(ComputeSubDeclODRHash(FirstParam) == 11458 ComputeSubDeclODRHash(SecondParam) && 11459 "Undiagnosed parameter difference."); 11460 } 11461 11462 if (ParameterMismatch) { 11463 Diagnosed = true; 11464 break; 11465 } 11466 11467 // If no error has been generated before now, assume the problem is in 11468 // the body and generate a message. 11469 ODRDiagError(FirstFunction->getLocation(), 11470 FirstFunction->getSourceRange(), FunctionBody); 11471 ODRDiagNote(SecondFunction->getLocation(), 11472 SecondFunction->getSourceRange(), FunctionBody); 11473 Diagnosed = true; 11474 break; 11475 } 11476 (void)Diagnosed; 11477 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11478 } 11479 11480 // Issue ODR failures diagnostics for enums. 11481 for (auto &Merge : EnumOdrMergeFailures) { 11482 enum ODREnumDifference { 11483 SingleScopedEnum, 11484 EnumTagKeywordMismatch, 11485 SingleSpecifiedType, 11486 DifferentSpecifiedTypes, 11487 DifferentNumberEnumConstants, 11488 EnumConstantName, 11489 EnumConstantSingleInitilizer, 11490 EnumConstantDifferentInitilizer, 11491 }; 11492 11493 // If we've already pointed out a specific problem with this enum, don't 11494 // bother issuing a general "something's different" diagnostic. 11495 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 11496 continue; 11497 11498 EnumDecl *FirstEnum = Merge.first; 11499 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum); 11500 11501 using DeclHashes = 11502 llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>; 11503 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum]( 11504 DeclHashes &Hashes, EnumDecl *Enum) { 11505 for (auto *D : Enum->decls()) { 11506 // Due to decl merging, the first EnumDecl is the parent of 11507 // Decls in both records. 11508 if (!ODRHash::isDeclToBeProcessed(D, FirstEnum)) 11509 continue; 11510 assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind"); 11511 Hashes.emplace_back(cast<EnumConstantDecl>(D), 11512 ComputeSubDeclODRHash(D)); 11513 } 11514 }; 11515 DeclHashes FirstHashes; 11516 PopulateHashes(FirstHashes, FirstEnum); 11517 bool Diagnosed = false; 11518 for (auto &SecondEnum : Merge.second) { 11519 11520 if (FirstEnum == SecondEnum) 11521 continue; 11522 11523 std::string SecondModule = 11524 getOwningModuleNameForDiagnostic(SecondEnum); 11525 11526 auto ODRDiagError = [FirstEnum, &FirstModule, 11527 this](SourceLocation Loc, SourceRange Range, 11528 ODREnumDifference DiffType) { 11529 return Diag(Loc, diag::err_module_odr_violation_enum) 11530 << FirstEnum << FirstModule.empty() << FirstModule << Range 11531 << DiffType; 11532 }; 11533 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11534 SourceRange Range, 11535 ODREnumDifference DiffType) { 11536 return Diag(Loc, diag::note_module_odr_violation_enum) 11537 << SecondModule << Range << DiffType; 11538 }; 11539 11540 if (FirstEnum->isScoped() != SecondEnum->isScoped()) { 11541 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11542 SingleScopedEnum) 11543 << FirstEnum->isScoped(); 11544 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11545 SingleScopedEnum) 11546 << SecondEnum->isScoped(); 11547 Diagnosed = true; 11548 continue; 11549 } 11550 11551 if (FirstEnum->isScoped() && SecondEnum->isScoped()) { 11552 if (FirstEnum->isScopedUsingClassTag() != 11553 SecondEnum->isScopedUsingClassTag()) { 11554 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11555 EnumTagKeywordMismatch) 11556 << FirstEnum->isScopedUsingClassTag(); 11557 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11558 EnumTagKeywordMismatch) 11559 << SecondEnum->isScopedUsingClassTag(); 11560 Diagnosed = true; 11561 continue; 11562 } 11563 } 11564 11565 QualType FirstUnderlyingType = 11566 FirstEnum->getIntegerTypeSourceInfo() 11567 ? FirstEnum->getIntegerTypeSourceInfo()->getType() 11568 : QualType(); 11569 QualType SecondUnderlyingType = 11570 SecondEnum->getIntegerTypeSourceInfo() 11571 ? SecondEnum->getIntegerTypeSourceInfo()->getType() 11572 : QualType(); 11573 if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) { 11574 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11575 SingleSpecifiedType) 11576 << !FirstUnderlyingType.isNull(); 11577 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11578 SingleSpecifiedType) 11579 << !SecondUnderlyingType.isNull(); 11580 Diagnosed = true; 11581 continue; 11582 } 11583 11584 if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) { 11585 if (ComputeQualTypeODRHash(FirstUnderlyingType) != 11586 ComputeQualTypeODRHash(SecondUnderlyingType)) { 11587 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11588 DifferentSpecifiedTypes) 11589 << FirstUnderlyingType; 11590 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11591 DifferentSpecifiedTypes) 11592 << SecondUnderlyingType; 11593 Diagnosed = true; 11594 continue; 11595 } 11596 } 11597 11598 DeclHashes SecondHashes; 11599 PopulateHashes(SecondHashes, SecondEnum); 11600 11601 if (FirstHashes.size() != SecondHashes.size()) { 11602 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11603 DifferentNumberEnumConstants) 11604 << (int)FirstHashes.size(); 11605 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11606 DifferentNumberEnumConstants) 11607 << (int)SecondHashes.size(); 11608 Diagnosed = true; 11609 continue; 11610 } 11611 11612 for (unsigned I = 0; I < FirstHashes.size(); ++I) { 11613 if (FirstHashes[I].second == SecondHashes[I].second) 11614 continue; 11615 const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first; 11616 const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first; 11617 11618 if (FirstEnumConstant->getDeclName() != 11619 SecondEnumConstant->getDeclName()) { 11620 11621 ODRDiagError(FirstEnumConstant->getLocation(), 11622 FirstEnumConstant->getSourceRange(), EnumConstantName) 11623 << I + 1 << FirstEnumConstant; 11624 ODRDiagNote(SecondEnumConstant->getLocation(), 11625 SecondEnumConstant->getSourceRange(), EnumConstantName) 11626 << I + 1 << SecondEnumConstant; 11627 Diagnosed = true; 11628 break; 11629 } 11630 11631 const Expr *FirstInit = FirstEnumConstant->getInitExpr(); 11632 const Expr *SecondInit = SecondEnumConstant->getInitExpr(); 11633 if (!FirstInit && !SecondInit) 11634 continue; 11635 11636 if (!FirstInit || !SecondInit) { 11637 ODRDiagError(FirstEnumConstant->getLocation(), 11638 FirstEnumConstant->getSourceRange(), 11639 EnumConstantSingleInitilizer) 11640 << I + 1 << FirstEnumConstant << (FirstInit != nullptr); 11641 ODRDiagNote(SecondEnumConstant->getLocation(), 11642 SecondEnumConstant->getSourceRange(), 11643 EnumConstantSingleInitilizer) 11644 << I + 1 << SecondEnumConstant << (SecondInit != nullptr); 11645 Diagnosed = true; 11646 break; 11647 } 11648 11649 if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11650 ODRDiagError(FirstEnumConstant->getLocation(), 11651 FirstEnumConstant->getSourceRange(), 11652 EnumConstantDifferentInitilizer) 11653 << I + 1 << FirstEnumConstant; 11654 ODRDiagNote(SecondEnumConstant->getLocation(), 11655 SecondEnumConstant->getSourceRange(), 11656 EnumConstantDifferentInitilizer) 11657 << I + 1 << SecondEnumConstant; 11658 Diagnosed = true; 11659 break; 11660 } 11661 } 11662 } 11663 11664 (void)Diagnosed; 11665 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11666 } 11667 } 11668 11669 void ASTReader::StartedDeserializing() { 11670 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get()) 11671 ReadTimer->startTimer(); 11672 } 11673 11674 void ASTReader::FinishedDeserializing() { 11675 assert(NumCurrentElementsDeserializing && 11676 "FinishedDeserializing not paired with StartedDeserializing"); 11677 if (NumCurrentElementsDeserializing == 1) { 11678 // We decrease NumCurrentElementsDeserializing only after pending actions 11679 // are finished, to avoid recursively re-calling finishPendingActions(). 11680 finishPendingActions(); 11681 } 11682 --NumCurrentElementsDeserializing; 11683 11684 if (NumCurrentElementsDeserializing == 0) { 11685 // Propagate exception specification and deduced type updates along 11686 // redeclaration chains. 11687 // 11688 // We do this now rather than in finishPendingActions because we want to 11689 // be able to walk the complete redeclaration chains of the updated decls. 11690 while (!PendingExceptionSpecUpdates.empty() || 11691 !PendingDeducedTypeUpdates.empty()) { 11692 auto ESUpdates = std::move(PendingExceptionSpecUpdates); 11693 PendingExceptionSpecUpdates.clear(); 11694 for (auto Update : ESUpdates) { 11695 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11696 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>(); 11697 auto ESI = FPT->getExtProtoInfo().ExceptionSpec; 11698 if (auto *Listener = getContext().getASTMutationListener()) 11699 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second)); 11700 for (auto *Redecl : Update.second->redecls()) 11701 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI); 11702 } 11703 11704 auto DTUpdates = std::move(PendingDeducedTypeUpdates); 11705 PendingDeducedTypeUpdates.clear(); 11706 for (auto Update : DTUpdates) { 11707 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11708 // FIXME: If the return type is already deduced, check that it matches. 11709 getContext().adjustDeducedFunctionResultType(Update.first, 11710 Update.second); 11711 } 11712 } 11713 11714 if (ReadTimer) 11715 ReadTimer->stopTimer(); 11716 11717 diagnoseOdrViolations(); 11718 11719 // We are not in recursive loading, so it's safe to pass the "interesting" 11720 // decls to the consumer. 11721 if (Consumer) 11722 PassInterestingDeclsToConsumer(); 11723 } 11724 } 11725 11726 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) { 11727 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) { 11728 // Remove any fake results before adding any real ones. 11729 auto It = PendingFakeLookupResults.find(II); 11730 if (It != PendingFakeLookupResults.end()) { 11731 for (auto *ND : It->second) 11732 SemaObj->IdResolver.RemoveDecl(ND); 11733 // FIXME: this works around module+PCH performance issue. 11734 // Rather than erase the result from the map, which is O(n), just clear 11735 // the vector of NamedDecls. 11736 It->second.clear(); 11737 } 11738 } 11739 11740 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) { 11741 SemaObj->TUScope->AddDecl(D); 11742 } else if (SemaObj->TUScope) { 11743 // Adding the decl to IdResolver may have failed because it was already in 11744 // (even though it was not added in scope). If it is already in, make sure 11745 // it gets in the scope as well. 11746 if (std::find(SemaObj->IdResolver.begin(Name), 11747 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end()) 11748 SemaObj->TUScope->AddDecl(D); 11749 } 11750 } 11751 11752 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache, 11753 ASTContext *Context, 11754 const PCHContainerReader &PCHContainerRdr, 11755 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, 11756 StringRef isysroot, bool DisableValidation, 11757 bool AllowASTWithCompilerErrors, 11758 bool AllowConfigurationMismatch, bool ValidateSystemInputs, 11759 bool ValidateASTInputFilesContent, bool UseGlobalIndex, 11760 std::unique_ptr<llvm::Timer> ReadTimer) 11761 : Listener(DisableValidation 11762 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP)) 11763 : cast<ASTReaderListener>(new PCHValidator(PP, *this))), 11764 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), 11765 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP), 11766 ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache, 11767 PCHContainerRdr, PP.getHeaderSearchInfo()), 11768 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot), 11769 DisableValidation(DisableValidation), 11770 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors), 11771 AllowConfigurationMismatch(AllowConfigurationMismatch), 11772 ValidateSystemInputs(ValidateSystemInputs), 11773 ValidateASTInputFilesContent(ValidateASTInputFilesContent), 11774 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) { 11775 SourceMgr.setExternalSLocEntrySource(this); 11776 11777 for (const auto &Ext : Extensions) { 11778 auto BlockName = Ext->getExtensionMetadata().BlockName; 11779 auto Known = ModuleFileExtensions.find(BlockName); 11780 if (Known != ModuleFileExtensions.end()) { 11781 Diags.Report(diag::warn_duplicate_module_file_extension) 11782 << BlockName; 11783 continue; 11784 } 11785 11786 ModuleFileExtensions.insert({BlockName, Ext}); 11787 } 11788 } 11789 11790 ASTReader::~ASTReader() { 11791 if (OwnsDeserializationListener) 11792 delete DeserializationListener; 11793 } 11794 11795 IdentifierResolver &ASTReader::getIdResolver() { 11796 return SemaObj ? SemaObj->IdResolver : DummyIdResolver; 11797 } 11798 11799 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor, 11800 unsigned AbbrevID) { 11801 Idx = 0; 11802 Record.clear(); 11803 return Cursor.readRecord(AbbrevID, Record); 11804 } 11805 //===----------------------------------------------------------------------===// 11806 //// OMPClauseReader implementation 11807 ////===----------------------------------------------------------------------===// 11808 11809 // This has to be in namespace clang because it's friended by all 11810 // of the OMP clauses. 11811 namespace clang { 11812 11813 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> { 11814 ASTRecordReader &Record; 11815 ASTContext &Context; 11816 11817 public: 11818 OMPClauseReader(ASTRecordReader &Record) 11819 : Record(Record), Context(Record.getContext()) {} 11820 11821 #define OMP_CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C); 11822 #include "llvm/Frontend/OpenMP/OMPKinds.def" 11823 OMPClause *readClause(); 11824 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C); 11825 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C); 11826 }; 11827 11828 } // end namespace clang 11829 11830 OMPClause *ASTRecordReader::readOMPClause() { 11831 return OMPClauseReader(*this).readClause(); 11832 } 11833 11834 OMPClause *OMPClauseReader::readClause() { 11835 OMPClause *C = nullptr; 11836 switch (llvm::omp::Clause(Record.readInt())) { 11837 case llvm::omp::OMPC_if: 11838 C = new (Context) OMPIfClause(); 11839 break; 11840 case llvm::omp::OMPC_final: 11841 C = new (Context) OMPFinalClause(); 11842 break; 11843 case llvm::omp::OMPC_num_threads: 11844 C = new (Context) OMPNumThreadsClause(); 11845 break; 11846 case llvm::omp::OMPC_safelen: 11847 C = new (Context) OMPSafelenClause(); 11848 break; 11849 case llvm::omp::OMPC_simdlen: 11850 C = new (Context) OMPSimdlenClause(); 11851 break; 11852 case llvm::omp::OMPC_allocator: 11853 C = new (Context) OMPAllocatorClause(); 11854 break; 11855 case llvm::omp::OMPC_collapse: 11856 C = new (Context) OMPCollapseClause(); 11857 break; 11858 case llvm::omp::OMPC_default: 11859 C = new (Context) OMPDefaultClause(); 11860 break; 11861 case llvm::omp::OMPC_proc_bind: 11862 C = new (Context) OMPProcBindClause(); 11863 break; 11864 case llvm::omp::OMPC_schedule: 11865 C = new (Context) OMPScheduleClause(); 11866 break; 11867 case llvm::omp::OMPC_ordered: 11868 C = OMPOrderedClause::CreateEmpty(Context, Record.readInt()); 11869 break; 11870 case llvm::omp::OMPC_nowait: 11871 C = new (Context) OMPNowaitClause(); 11872 break; 11873 case llvm::omp::OMPC_untied: 11874 C = new (Context) OMPUntiedClause(); 11875 break; 11876 case llvm::omp::OMPC_mergeable: 11877 C = new (Context) OMPMergeableClause(); 11878 break; 11879 case llvm::omp::OMPC_read: 11880 C = new (Context) OMPReadClause(); 11881 break; 11882 case llvm::omp::OMPC_write: 11883 C = new (Context) OMPWriteClause(); 11884 break; 11885 case llvm::omp::OMPC_update: 11886 C = OMPUpdateClause::CreateEmpty(Context, Record.readInt()); 11887 break; 11888 case llvm::omp::OMPC_capture: 11889 C = new (Context) OMPCaptureClause(); 11890 break; 11891 case llvm::omp::OMPC_seq_cst: 11892 C = new (Context) OMPSeqCstClause(); 11893 break; 11894 case llvm::omp::OMPC_acq_rel: 11895 C = new (Context) OMPAcqRelClause(); 11896 break; 11897 case llvm::omp::OMPC_acquire: 11898 C = new (Context) OMPAcquireClause(); 11899 break; 11900 case llvm::omp::OMPC_release: 11901 C = new (Context) OMPReleaseClause(); 11902 break; 11903 case llvm::omp::OMPC_relaxed: 11904 C = new (Context) OMPRelaxedClause(); 11905 break; 11906 case llvm::omp::OMPC_threads: 11907 C = new (Context) OMPThreadsClause(); 11908 break; 11909 case llvm::omp::OMPC_simd: 11910 C = new (Context) OMPSIMDClause(); 11911 break; 11912 case llvm::omp::OMPC_nogroup: 11913 C = new (Context) OMPNogroupClause(); 11914 break; 11915 case llvm::omp::OMPC_unified_address: 11916 C = new (Context) OMPUnifiedAddressClause(); 11917 break; 11918 case llvm::omp::OMPC_unified_shared_memory: 11919 C = new (Context) OMPUnifiedSharedMemoryClause(); 11920 break; 11921 case llvm::omp::OMPC_reverse_offload: 11922 C = new (Context) OMPReverseOffloadClause(); 11923 break; 11924 case llvm::omp::OMPC_dynamic_allocators: 11925 C = new (Context) OMPDynamicAllocatorsClause(); 11926 break; 11927 case llvm::omp::OMPC_atomic_default_mem_order: 11928 C = new (Context) OMPAtomicDefaultMemOrderClause(); 11929 break; 11930 case llvm::omp::OMPC_private: 11931 C = OMPPrivateClause::CreateEmpty(Context, Record.readInt()); 11932 break; 11933 case llvm::omp::OMPC_firstprivate: 11934 C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt()); 11935 break; 11936 case llvm::omp::OMPC_lastprivate: 11937 C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt()); 11938 break; 11939 case llvm::omp::OMPC_shared: 11940 C = OMPSharedClause::CreateEmpty(Context, Record.readInt()); 11941 break; 11942 case llvm::omp::OMPC_reduction: { 11943 unsigned N = Record.readInt(); 11944 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>(); 11945 C = OMPReductionClause::CreateEmpty(Context, N, Modifier); 11946 break; 11947 } 11948 case llvm::omp::OMPC_task_reduction: 11949 C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt()); 11950 break; 11951 case llvm::omp::OMPC_in_reduction: 11952 C = OMPInReductionClause::CreateEmpty(Context, Record.readInt()); 11953 break; 11954 case llvm::omp::OMPC_linear: 11955 C = OMPLinearClause::CreateEmpty(Context, Record.readInt()); 11956 break; 11957 case llvm::omp::OMPC_aligned: 11958 C = OMPAlignedClause::CreateEmpty(Context, Record.readInt()); 11959 break; 11960 case llvm::omp::OMPC_copyin: 11961 C = OMPCopyinClause::CreateEmpty(Context, Record.readInt()); 11962 break; 11963 case llvm::omp::OMPC_copyprivate: 11964 C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt()); 11965 break; 11966 case llvm::omp::OMPC_flush: 11967 C = OMPFlushClause::CreateEmpty(Context, Record.readInt()); 11968 break; 11969 case llvm::omp::OMPC_depobj: 11970 C = OMPDepobjClause::CreateEmpty(Context); 11971 break; 11972 case llvm::omp::OMPC_depend: { 11973 unsigned NumVars = Record.readInt(); 11974 unsigned NumLoops = Record.readInt(); 11975 C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops); 11976 break; 11977 } 11978 case llvm::omp::OMPC_device: 11979 C = new (Context) OMPDeviceClause(); 11980 break; 11981 case llvm::omp::OMPC_map: { 11982 OMPMappableExprListSizeTy Sizes; 11983 Sizes.NumVars = Record.readInt(); 11984 Sizes.NumUniqueDeclarations = Record.readInt(); 11985 Sizes.NumComponentLists = Record.readInt(); 11986 Sizes.NumComponents = Record.readInt(); 11987 C = OMPMapClause::CreateEmpty(Context, Sizes); 11988 break; 11989 } 11990 case llvm::omp::OMPC_num_teams: 11991 C = new (Context) OMPNumTeamsClause(); 11992 break; 11993 case llvm::omp::OMPC_thread_limit: 11994 C = new (Context) OMPThreadLimitClause(); 11995 break; 11996 case llvm::omp::OMPC_priority: 11997 C = new (Context) OMPPriorityClause(); 11998 break; 11999 case llvm::omp::OMPC_grainsize: 12000 C = new (Context) OMPGrainsizeClause(); 12001 break; 12002 case llvm::omp::OMPC_num_tasks: 12003 C = new (Context) OMPNumTasksClause(); 12004 break; 12005 case llvm::omp::OMPC_hint: 12006 C = new (Context) OMPHintClause(); 12007 break; 12008 case llvm::omp::OMPC_dist_schedule: 12009 C = new (Context) OMPDistScheduleClause(); 12010 break; 12011 case llvm::omp::OMPC_defaultmap: 12012 C = new (Context) OMPDefaultmapClause(); 12013 break; 12014 case llvm::omp::OMPC_to: { 12015 OMPMappableExprListSizeTy Sizes; 12016 Sizes.NumVars = Record.readInt(); 12017 Sizes.NumUniqueDeclarations = Record.readInt(); 12018 Sizes.NumComponentLists = Record.readInt(); 12019 Sizes.NumComponents = Record.readInt(); 12020 C = OMPToClause::CreateEmpty(Context, Sizes); 12021 break; 12022 } 12023 case llvm::omp::OMPC_from: { 12024 OMPMappableExprListSizeTy Sizes; 12025 Sizes.NumVars = Record.readInt(); 12026 Sizes.NumUniqueDeclarations = Record.readInt(); 12027 Sizes.NumComponentLists = Record.readInt(); 12028 Sizes.NumComponents = Record.readInt(); 12029 C = OMPFromClause::CreateEmpty(Context, Sizes); 12030 break; 12031 } 12032 case llvm::omp::OMPC_use_device_ptr: { 12033 OMPMappableExprListSizeTy Sizes; 12034 Sizes.NumVars = Record.readInt(); 12035 Sizes.NumUniqueDeclarations = Record.readInt(); 12036 Sizes.NumComponentLists = Record.readInt(); 12037 Sizes.NumComponents = Record.readInt(); 12038 C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes); 12039 break; 12040 } 12041 case llvm::omp::OMPC_use_device_addr: { 12042 OMPMappableExprListSizeTy Sizes; 12043 Sizes.NumVars = Record.readInt(); 12044 Sizes.NumUniqueDeclarations = Record.readInt(); 12045 Sizes.NumComponentLists = Record.readInt(); 12046 Sizes.NumComponents = Record.readInt(); 12047 C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes); 12048 break; 12049 } 12050 case llvm::omp::OMPC_is_device_ptr: { 12051 OMPMappableExprListSizeTy Sizes; 12052 Sizes.NumVars = Record.readInt(); 12053 Sizes.NumUniqueDeclarations = Record.readInt(); 12054 Sizes.NumComponentLists = Record.readInt(); 12055 Sizes.NumComponents = Record.readInt(); 12056 C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes); 12057 break; 12058 } 12059 case llvm::omp::OMPC_allocate: 12060 C = OMPAllocateClause::CreateEmpty(Context, Record.readInt()); 12061 break; 12062 case llvm::omp::OMPC_nontemporal: 12063 C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt()); 12064 break; 12065 case llvm::omp::OMPC_inclusive: 12066 C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt()); 12067 break; 12068 case llvm::omp::OMPC_exclusive: 12069 C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt()); 12070 break; 12071 case llvm::omp::OMPC_order: 12072 C = new (Context) OMPOrderClause(); 12073 break; 12074 case llvm::omp::OMPC_destroy: 12075 C = new (Context) OMPDestroyClause(); 12076 break; 12077 case llvm::omp::OMPC_detach: 12078 C = new (Context) OMPDetachClause(); 12079 break; 12080 case llvm::omp::OMPC_uses_allocators: 12081 C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt()); 12082 break; 12083 case llvm::omp::OMPC_affinity: 12084 C = OMPAffinityClause::CreateEmpty(Context, Record.readInt()); 12085 break; 12086 #define OMP_CLAUSE_NO_CLASS(Enum, Str) \ 12087 case llvm::omp::Enum: \ 12088 break; 12089 #include "llvm/Frontend/OpenMP/OMPKinds.def" 12090 default: 12091 break; 12092 } 12093 assert(C && "Unknown OMPClause type"); 12094 12095 Visit(C); 12096 C->setLocStart(Record.readSourceLocation()); 12097 C->setLocEnd(Record.readSourceLocation()); 12098 12099 return C; 12100 } 12101 12102 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) { 12103 C->setPreInitStmt(Record.readSubStmt(), 12104 static_cast<OpenMPDirectiveKind>(Record.readInt())); 12105 } 12106 12107 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) { 12108 VisitOMPClauseWithPreInit(C); 12109 C->setPostUpdateExpr(Record.readSubExpr()); 12110 } 12111 12112 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) { 12113 VisitOMPClauseWithPreInit(C); 12114 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt())); 12115 C->setNameModifierLoc(Record.readSourceLocation()); 12116 C->setColonLoc(Record.readSourceLocation()); 12117 C->setCondition(Record.readSubExpr()); 12118 C->setLParenLoc(Record.readSourceLocation()); 12119 } 12120 12121 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) { 12122 VisitOMPClauseWithPreInit(C); 12123 C->setCondition(Record.readSubExpr()); 12124 C->setLParenLoc(Record.readSourceLocation()); 12125 } 12126 12127 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) { 12128 VisitOMPClauseWithPreInit(C); 12129 C->setNumThreads(Record.readSubExpr()); 12130 C->setLParenLoc(Record.readSourceLocation()); 12131 } 12132 12133 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) { 12134 C->setSafelen(Record.readSubExpr()); 12135 C->setLParenLoc(Record.readSourceLocation()); 12136 } 12137 12138 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) { 12139 C->setSimdlen(Record.readSubExpr()); 12140 C->setLParenLoc(Record.readSourceLocation()); 12141 } 12142 12143 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) { 12144 C->setAllocator(Record.readExpr()); 12145 C->setLParenLoc(Record.readSourceLocation()); 12146 } 12147 12148 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) { 12149 C->setNumForLoops(Record.readSubExpr()); 12150 C->setLParenLoc(Record.readSourceLocation()); 12151 } 12152 12153 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) { 12154 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt())); 12155 C->setLParenLoc(Record.readSourceLocation()); 12156 C->setDefaultKindKwLoc(Record.readSourceLocation()); 12157 } 12158 12159 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) { 12160 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt())); 12161 C->setLParenLoc(Record.readSourceLocation()); 12162 C->setProcBindKindKwLoc(Record.readSourceLocation()); 12163 } 12164 12165 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) { 12166 VisitOMPClauseWithPreInit(C); 12167 C->setScheduleKind( 12168 static_cast<OpenMPScheduleClauseKind>(Record.readInt())); 12169 C->setFirstScheduleModifier( 12170 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 12171 C->setSecondScheduleModifier( 12172 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 12173 C->setChunkSize(Record.readSubExpr()); 12174 C->setLParenLoc(Record.readSourceLocation()); 12175 C->setFirstScheduleModifierLoc(Record.readSourceLocation()); 12176 C->setSecondScheduleModifierLoc(Record.readSourceLocation()); 12177 C->setScheduleKindLoc(Record.readSourceLocation()); 12178 C->setCommaLoc(Record.readSourceLocation()); 12179 } 12180 12181 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) { 12182 C->setNumForLoops(Record.readSubExpr()); 12183 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 12184 C->setLoopNumIterations(I, Record.readSubExpr()); 12185 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 12186 C->setLoopCounter(I, Record.readSubExpr()); 12187 C->setLParenLoc(Record.readSourceLocation()); 12188 } 12189 12190 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) { 12191 C->setEventHandler(Record.readSubExpr()); 12192 C->setLParenLoc(Record.readSourceLocation()); 12193 } 12194 12195 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {} 12196 12197 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {} 12198 12199 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {} 12200 12201 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {} 12202 12203 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {} 12204 12205 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) { 12206 if (C->isExtended()) { 12207 C->setLParenLoc(Record.readSourceLocation()); 12208 C->setArgumentLoc(Record.readSourceLocation()); 12209 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>()); 12210 } 12211 } 12212 12213 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {} 12214 12215 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {} 12216 12217 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {} 12218 12219 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {} 12220 12221 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {} 12222 12223 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {} 12224 12225 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {} 12226 12227 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {} 12228 12229 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {} 12230 12231 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *) {} 12232 12233 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {} 12234 12235 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause( 12236 OMPUnifiedSharedMemoryClause *) {} 12237 12238 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {} 12239 12240 void 12241 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) { 12242 } 12243 12244 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause( 12245 OMPAtomicDefaultMemOrderClause *C) { 12246 C->setAtomicDefaultMemOrderKind( 12247 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt())); 12248 C->setLParenLoc(Record.readSourceLocation()); 12249 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation()); 12250 } 12251 12252 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *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 Vars.clear(); 12261 for (unsigned i = 0; i != NumVars; ++i) 12262 Vars.push_back(Record.readSubExpr()); 12263 C->setPrivateCopies(Vars); 12264 } 12265 12266 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) { 12267 VisitOMPClauseWithPreInit(C); 12268 C->setLParenLoc(Record.readSourceLocation()); 12269 unsigned NumVars = C->varlist_size(); 12270 SmallVector<Expr *, 16> Vars; 12271 Vars.reserve(NumVars); 12272 for (unsigned i = 0; i != NumVars; ++i) 12273 Vars.push_back(Record.readSubExpr()); 12274 C->setVarRefs(Vars); 12275 Vars.clear(); 12276 for (unsigned i = 0; i != NumVars; ++i) 12277 Vars.push_back(Record.readSubExpr()); 12278 C->setPrivateCopies(Vars); 12279 Vars.clear(); 12280 for (unsigned i = 0; i != NumVars; ++i) 12281 Vars.push_back(Record.readSubExpr()); 12282 C->setInits(Vars); 12283 } 12284 12285 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) { 12286 VisitOMPClauseWithPostUpdate(C); 12287 C->setLParenLoc(Record.readSourceLocation()); 12288 C->setKind(Record.readEnum<OpenMPLastprivateModifier>()); 12289 C->setKindLoc(Record.readSourceLocation()); 12290 C->setColonLoc(Record.readSourceLocation()); 12291 unsigned NumVars = C->varlist_size(); 12292 SmallVector<Expr *, 16> Vars; 12293 Vars.reserve(NumVars); 12294 for (unsigned i = 0; i != NumVars; ++i) 12295 Vars.push_back(Record.readSubExpr()); 12296 C->setVarRefs(Vars); 12297 Vars.clear(); 12298 for (unsigned i = 0; i != NumVars; ++i) 12299 Vars.push_back(Record.readSubExpr()); 12300 C->setPrivateCopies(Vars); 12301 Vars.clear(); 12302 for (unsigned i = 0; i != NumVars; ++i) 12303 Vars.push_back(Record.readSubExpr()); 12304 C->setSourceExprs(Vars); 12305 Vars.clear(); 12306 for (unsigned i = 0; i != NumVars; ++i) 12307 Vars.push_back(Record.readSubExpr()); 12308 C->setDestinationExprs(Vars); 12309 Vars.clear(); 12310 for (unsigned i = 0; i != NumVars; ++i) 12311 Vars.push_back(Record.readSubExpr()); 12312 C->setAssignmentOps(Vars); 12313 } 12314 12315 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) { 12316 C->setLParenLoc(Record.readSourceLocation()); 12317 unsigned NumVars = C->varlist_size(); 12318 SmallVector<Expr *, 16> Vars; 12319 Vars.reserve(NumVars); 12320 for (unsigned i = 0; i != NumVars; ++i) 12321 Vars.push_back(Record.readSubExpr()); 12322 C->setVarRefs(Vars); 12323 } 12324 12325 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) { 12326 VisitOMPClauseWithPostUpdate(C); 12327 C->setLParenLoc(Record.readSourceLocation()); 12328 C->setModifierLoc(Record.readSourceLocation()); 12329 C->setColonLoc(Record.readSourceLocation()); 12330 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12331 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12332 C->setQualifierLoc(NNSL); 12333 C->setNameInfo(DNI); 12334 12335 unsigned NumVars = C->varlist_size(); 12336 SmallVector<Expr *, 16> Vars; 12337 Vars.reserve(NumVars); 12338 for (unsigned i = 0; i != NumVars; ++i) 12339 Vars.push_back(Record.readSubExpr()); 12340 C->setVarRefs(Vars); 12341 Vars.clear(); 12342 for (unsigned i = 0; i != NumVars; ++i) 12343 Vars.push_back(Record.readSubExpr()); 12344 C->setPrivates(Vars); 12345 Vars.clear(); 12346 for (unsigned i = 0; i != NumVars; ++i) 12347 Vars.push_back(Record.readSubExpr()); 12348 C->setLHSExprs(Vars); 12349 Vars.clear(); 12350 for (unsigned i = 0; i != NumVars; ++i) 12351 Vars.push_back(Record.readSubExpr()); 12352 C->setRHSExprs(Vars); 12353 Vars.clear(); 12354 for (unsigned i = 0; i != NumVars; ++i) 12355 Vars.push_back(Record.readSubExpr()); 12356 C->setReductionOps(Vars); 12357 if (C->getModifier() == OMPC_REDUCTION_inscan) { 12358 Vars.clear(); 12359 for (unsigned i = 0; i != NumVars; ++i) 12360 Vars.push_back(Record.readSubExpr()); 12361 C->setInscanCopyOps(Vars); 12362 Vars.clear(); 12363 for (unsigned i = 0; i != NumVars; ++i) 12364 Vars.push_back(Record.readSubExpr()); 12365 C->setInscanCopyArrayTemps(Vars); 12366 Vars.clear(); 12367 for (unsigned i = 0; i != NumVars; ++i) 12368 Vars.push_back(Record.readSubExpr()); 12369 C->setInscanCopyArrayElems(Vars); 12370 } 12371 } 12372 12373 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) { 12374 VisitOMPClauseWithPostUpdate(C); 12375 C->setLParenLoc(Record.readSourceLocation()); 12376 C->setColonLoc(Record.readSourceLocation()); 12377 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12378 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12379 C->setQualifierLoc(NNSL); 12380 C->setNameInfo(DNI); 12381 12382 unsigned NumVars = C->varlist_size(); 12383 SmallVector<Expr *, 16> Vars; 12384 Vars.reserve(NumVars); 12385 for (unsigned I = 0; I != NumVars; ++I) 12386 Vars.push_back(Record.readSubExpr()); 12387 C->setVarRefs(Vars); 12388 Vars.clear(); 12389 for (unsigned I = 0; I != NumVars; ++I) 12390 Vars.push_back(Record.readSubExpr()); 12391 C->setPrivates(Vars); 12392 Vars.clear(); 12393 for (unsigned I = 0; I != NumVars; ++I) 12394 Vars.push_back(Record.readSubExpr()); 12395 C->setLHSExprs(Vars); 12396 Vars.clear(); 12397 for (unsigned I = 0; I != NumVars; ++I) 12398 Vars.push_back(Record.readSubExpr()); 12399 C->setRHSExprs(Vars); 12400 Vars.clear(); 12401 for (unsigned I = 0; I != NumVars; ++I) 12402 Vars.push_back(Record.readSubExpr()); 12403 C->setReductionOps(Vars); 12404 } 12405 12406 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) { 12407 VisitOMPClauseWithPostUpdate(C); 12408 C->setLParenLoc(Record.readSourceLocation()); 12409 C->setColonLoc(Record.readSourceLocation()); 12410 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12411 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12412 C->setQualifierLoc(NNSL); 12413 C->setNameInfo(DNI); 12414 12415 unsigned NumVars = C->varlist_size(); 12416 SmallVector<Expr *, 16> Vars; 12417 Vars.reserve(NumVars); 12418 for (unsigned I = 0; I != NumVars; ++I) 12419 Vars.push_back(Record.readSubExpr()); 12420 C->setVarRefs(Vars); 12421 Vars.clear(); 12422 for (unsigned I = 0; I != NumVars; ++I) 12423 Vars.push_back(Record.readSubExpr()); 12424 C->setPrivates(Vars); 12425 Vars.clear(); 12426 for (unsigned I = 0; I != NumVars; ++I) 12427 Vars.push_back(Record.readSubExpr()); 12428 C->setLHSExprs(Vars); 12429 Vars.clear(); 12430 for (unsigned I = 0; I != NumVars; ++I) 12431 Vars.push_back(Record.readSubExpr()); 12432 C->setRHSExprs(Vars); 12433 Vars.clear(); 12434 for (unsigned I = 0; I != NumVars; ++I) 12435 Vars.push_back(Record.readSubExpr()); 12436 C->setReductionOps(Vars); 12437 Vars.clear(); 12438 for (unsigned I = 0; I != NumVars; ++I) 12439 Vars.push_back(Record.readSubExpr()); 12440 C->setTaskgroupDescriptors(Vars); 12441 } 12442 12443 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) { 12444 VisitOMPClauseWithPostUpdate(C); 12445 C->setLParenLoc(Record.readSourceLocation()); 12446 C->setColonLoc(Record.readSourceLocation()); 12447 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt())); 12448 C->setModifierLoc(Record.readSourceLocation()); 12449 unsigned NumVars = C->varlist_size(); 12450 SmallVector<Expr *, 16> Vars; 12451 Vars.reserve(NumVars); 12452 for (unsigned i = 0; i != NumVars; ++i) 12453 Vars.push_back(Record.readSubExpr()); 12454 C->setVarRefs(Vars); 12455 Vars.clear(); 12456 for (unsigned i = 0; i != NumVars; ++i) 12457 Vars.push_back(Record.readSubExpr()); 12458 C->setPrivates(Vars); 12459 Vars.clear(); 12460 for (unsigned i = 0; i != NumVars; ++i) 12461 Vars.push_back(Record.readSubExpr()); 12462 C->setInits(Vars); 12463 Vars.clear(); 12464 for (unsigned i = 0; i != NumVars; ++i) 12465 Vars.push_back(Record.readSubExpr()); 12466 C->setUpdates(Vars); 12467 Vars.clear(); 12468 for (unsigned i = 0; i != NumVars; ++i) 12469 Vars.push_back(Record.readSubExpr()); 12470 C->setFinals(Vars); 12471 C->setStep(Record.readSubExpr()); 12472 C->setCalcStep(Record.readSubExpr()); 12473 Vars.clear(); 12474 for (unsigned I = 0; I != NumVars + 1; ++I) 12475 Vars.push_back(Record.readSubExpr()); 12476 C->setUsedExprs(Vars); 12477 } 12478 12479 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) { 12480 C->setLParenLoc(Record.readSourceLocation()); 12481 C->setColonLoc(Record.readSourceLocation()); 12482 unsigned NumVars = C->varlist_size(); 12483 SmallVector<Expr *, 16> Vars; 12484 Vars.reserve(NumVars); 12485 for (unsigned i = 0; i != NumVars; ++i) 12486 Vars.push_back(Record.readSubExpr()); 12487 C->setVarRefs(Vars); 12488 C->setAlignment(Record.readSubExpr()); 12489 } 12490 12491 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) { 12492 C->setLParenLoc(Record.readSourceLocation()); 12493 unsigned NumVars = C->varlist_size(); 12494 SmallVector<Expr *, 16> Exprs; 12495 Exprs.reserve(NumVars); 12496 for (unsigned i = 0; i != NumVars; ++i) 12497 Exprs.push_back(Record.readSubExpr()); 12498 C->setVarRefs(Exprs); 12499 Exprs.clear(); 12500 for (unsigned i = 0; i != NumVars; ++i) 12501 Exprs.push_back(Record.readSubExpr()); 12502 C->setSourceExprs(Exprs); 12503 Exprs.clear(); 12504 for (unsigned i = 0; i != NumVars; ++i) 12505 Exprs.push_back(Record.readSubExpr()); 12506 C->setDestinationExprs(Exprs); 12507 Exprs.clear(); 12508 for (unsigned i = 0; i != NumVars; ++i) 12509 Exprs.push_back(Record.readSubExpr()); 12510 C->setAssignmentOps(Exprs); 12511 } 12512 12513 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) { 12514 C->setLParenLoc(Record.readSourceLocation()); 12515 unsigned NumVars = C->varlist_size(); 12516 SmallVector<Expr *, 16> Exprs; 12517 Exprs.reserve(NumVars); 12518 for (unsigned i = 0; i != NumVars; ++i) 12519 Exprs.push_back(Record.readSubExpr()); 12520 C->setVarRefs(Exprs); 12521 Exprs.clear(); 12522 for (unsigned i = 0; i != NumVars; ++i) 12523 Exprs.push_back(Record.readSubExpr()); 12524 C->setSourceExprs(Exprs); 12525 Exprs.clear(); 12526 for (unsigned i = 0; i != NumVars; ++i) 12527 Exprs.push_back(Record.readSubExpr()); 12528 C->setDestinationExprs(Exprs); 12529 Exprs.clear(); 12530 for (unsigned i = 0; i != NumVars; ++i) 12531 Exprs.push_back(Record.readSubExpr()); 12532 C->setAssignmentOps(Exprs); 12533 } 12534 12535 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) { 12536 C->setLParenLoc(Record.readSourceLocation()); 12537 unsigned NumVars = C->varlist_size(); 12538 SmallVector<Expr *, 16> Vars; 12539 Vars.reserve(NumVars); 12540 for (unsigned i = 0; i != NumVars; ++i) 12541 Vars.push_back(Record.readSubExpr()); 12542 C->setVarRefs(Vars); 12543 } 12544 12545 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) { 12546 C->setDepobj(Record.readSubExpr()); 12547 C->setLParenLoc(Record.readSourceLocation()); 12548 } 12549 12550 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) { 12551 C->setLParenLoc(Record.readSourceLocation()); 12552 C->setModifier(Record.readSubExpr()); 12553 C->setDependencyKind( 12554 static_cast<OpenMPDependClauseKind>(Record.readInt())); 12555 C->setDependencyLoc(Record.readSourceLocation()); 12556 C->setColonLoc(Record.readSourceLocation()); 12557 unsigned NumVars = C->varlist_size(); 12558 SmallVector<Expr *, 16> Vars; 12559 Vars.reserve(NumVars); 12560 for (unsigned I = 0; I != NumVars; ++I) 12561 Vars.push_back(Record.readSubExpr()); 12562 C->setVarRefs(Vars); 12563 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I) 12564 C->setLoopData(I, Record.readSubExpr()); 12565 } 12566 12567 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) { 12568 VisitOMPClauseWithPreInit(C); 12569 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>()); 12570 C->setDevice(Record.readSubExpr()); 12571 C->setModifierLoc(Record.readSourceLocation()); 12572 C->setLParenLoc(Record.readSourceLocation()); 12573 } 12574 12575 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) { 12576 C->setLParenLoc(Record.readSourceLocation()); 12577 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) { 12578 C->setMapTypeModifier( 12579 I, static_cast<OpenMPMapModifierKind>(Record.readInt())); 12580 C->setMapTypeModifierLoc(I, Record.readSourceLocation()); 12581 } 12582 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12583 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12584 C->setMapType( 12585 static_cast<OpenMPMapClauseKind>(Record.readInt())); 12586 C->setMapLoc(Record.readSourceLocation()); 12587 C->setColonLoc(Record.readSourceLocation()); 12588 auto NumVars = C->varlist_size(); 12589 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12590 auto TotalLists = C->getTotalComponentListNum(); 12591 auto TotalComponents = C->getTotalComponentsNum(); 12592 12593 SmallVector<Expr *, 16> Vars; 12594 Vars.reserve(NumVars); 12595 for (unsigned i = 0; i != NumVars; ++i) 12596 Vars.push_back(Record.readExpr()); 12597 C->setVarRefs(Vars); 12598 12599 SmallVector<Expr *, 16> UDMappers; 12600 UDMappers.reserve(NumVars); 12601 for (unsigned I = 0; I < NumVars; ++I) 12602 UDMappers.push_back(Record.readExpr()); 12603 C->setUDMapperRefs(UDMappers); 12604 12605 SmallVector<ValueDecl *, 16> Decls; 12606 Decls.reserve(UniqueDecls); 12607 for (unsigned i = 0; i < UniqueDecls; ++i) 12608 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12609 C->setUniqueDecls(Decls); 12610 12611 SmallVector<unsigned, 16> ListsPerDecl; 12612 ListsPerDecl.reserve(UniqueDecls); 12613 for (unsigned i = 0; i < UniqueDecls; ++i) 12614 ListsPerDecl.push_back(Record.readInt()); 12615 C->setDeclNumLists(ListsPerDecl); 12616 12617 SmallVector<unsigned, 32> ListSizes; 12618 ListSizes.reserve(TotalLists); 12619 for (unsigned i = 0; i < TotalLists; ++i) 12620 ListSizes.push_back(Record.readInt()); 12621 C->setComponentListSizes(ListSizes); 12622 12623 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12624 Components.reserve(TotalComponents); 12625 for (unsigned i = 0; i < TotalComponents; ++i) { 12626 Expr *AssociatedExprPr = Record.readExpr(); 12627 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12628 Components.emplace_back(AssociatedExprPr, AssociatedDecl, 12629 /*IsNonContiguous=*/false); 12630 } 12631 C->setComponents(Components, ListSizes); 12632 } 12633 12634 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) { 12635 C->setLParenLoc(Record.readSourceLocation()); 12636 C->setColonLoc(Record.readSourceLocation()); 12637 C->setAllocator(Record.readSubExpr()); 12638 unsigned NumVars = C->varlist_size(); 12639 SmallVector<Expr *, 16> Vars; 12640 Vars.reserve(NumVars); 12641 for (unsigned i = 0; i != NumVars; ++i) 12642 Vars.push_back(Record.readSubExpr()); 12643 C->setVarRefs(Vars); 12644 } 12645 12646 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) { 12647 VisitOMPClauseWithPreInit(C); 12648 C->setNumTeams(Record.readSubExpr()); 12649 C->setLParenLoc(Record.readSourceLocation()); 12650 } 12651 12652 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) { 12653 VisitOMPClauseWithPreInit(C); 12654 C->setThreadLimit(Record.readSubExpr()); 12655 C->setLParenLoc(Record.readSourceLocation()); 12656 } 12657 12658 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) { 12659 VisitOMPClauseWithPreInit(C); 12660 C->setPriority(Record.readSubExpr()); 12661 C->setLParenLoc(Record.readSourceLocation()); 12662 } 12663 12664 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) { 12665 VisitOMPClauseWithPreInit(C); 12666 C->setGrainsize(Record.readSubExpr()); 12667 C->setLParenLoc(Record.readSourceLocation()); 12668 } 12669 12670 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) { 12671 VisitOMPClauseWithPreInit(C); 12672 C->setNumTasks(Record.readSubExpr()); 12673 C->setLParenLoc(Record.readSourceLocation()); 12674 } 12675 12676 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) { 12677 C->setHint(Record.readSubExpr()); 12678 C->setLParenLoc(Record.readSourceLocation()); 12679 } 12680 12681 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) { 12682 VisitOMPClauseWithPreInit(C); 12683 C->setDistScheduleKind( 12684 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt())); 12685 C->setChunkSize(Record.readSubExpr()); 12686 C->setLParenLoc(Record.readSourceLocation()); 12687 C->setDistScheduleKindLoc(Record.readSourceLocation()); 12688 C->setCommaLoc(Record.readSourceLocation()); 12689 } 12690 12691 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) { 12692 C->setDefaultmapKind( 12693 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt())); 12694 C->setDefaultmapModifier( 12695 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt())); 12696 C->setLParenLoc(Record.readSourceLocation()); 12697 C->setDefaultmapModifierLoc(Record.readSourceLocation()); 12698 C->setDefaultmapKindLoc(Record.readSourceLocation()); 12699 } 12700 12701 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) { 12702 C->setLParenLoc(Record.readSourceLocation()); 12703 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) { 12704 C->setMotionModifier( 12705 I, static_cast<OpenMPMotionModifierKind>(Record.readInt())); 12706 C->setMotionModifierLoc(I, Record.readSourceLocation()); 12707 } 12708 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12709 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12710 C->setColonLoc(Record.readSourceLocation()); 12711 auto NumVars = C->varlist_size(); 12712 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12713 auto TotalLists = C->getTotalComponentListNum(); 12714 auto TotalComponents = C->getTotalComponentsNum(); 12715 12716 SmallVector<Expr *, 16> Vars; 12717 Vars.reserve(NumVars); 12718 for (unsigned i = 0; i != NumVars; ++i) 12719 Vars.push_back(Record.readSubExpr()); 12720 C->setVarRefs(Vars); 12721 12722 SmallVector<Expr *, 16> UDMappers; 12723 UDMappers.reserve(NumVars); 12724 for (unsigned I = 0; I < NumVars; ++I) 12725 UDMappers.push_back(Record.readSubExpr()); 12726 C->setUDMapperRefs(UDMappers); 12727 12728 SmallVector<ValueDecl *, 16> Decls; 12729 Decls.reserve(UniqueDecls); 12730 for (unsigned i = 0; i < UniqueDecls; ++i) 12731 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12732 C->setUniqueDecls(Decls); 12733 12734 SmallVector<unsigned, 16> ListsPerDecl; 12735 ListsPerDecl.reserve(UniqueDecls); 12736 for (unsigned i = 0; i < UniqueDecls; ++i) 12737 ListsPerDecl.push_back(Record.readInt()); 12738 C->setDeclNumLists(ListsPerDecl); 12739 12740 SmallVector<unsigned, 32> ListSizes; 12741 ListSizes.reserve(TotalLists); 12742 for (unsigned i = 0; i < TotalLists; ++i) 12743 ListSizes.push_back(Record.readInt()); 12744 C->setComponentListSizes(ListSizes); 12745 12746 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12747 Components.reserve(TotalComponents); 12748 for (unsigned i = 0; i < TotalComponents; ++i) { 12749 Expr *AssociatedExprPr = Record.readSubExpr(); 12750 bool IsNonContiguous = Record.readBool(); 12751 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12752 Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous); 12753 } 12754 C->setComponents(Components, ListSizes); 12755 } 12756 12757 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) { 12758 C->setLParenLoc(Record.readSourceLocation()); 12759 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) { 12760 C->setMotionModifier( 12761 I, static_cast<OpenMPMotionModifierKind>(Record.readInt())); 12762 C->setMotionModifierLoc(I, Record.readSourceLocation()); 12763 } 12764 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12765 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12766 C->setColonLoc(Record.readSourceLocation()); 12767 auto NumVars = C->varlist_size(); 12768 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12769 auto TotalLists = C->getTotalComponentListNum(); 12770 auto TotalComponents = C->getTotalComponentsNum(); 12771 12772 SmallVector<Expr *, 16> Vars; 12773 Vars.reserve(NumVars); 12774 for (unsigned i = 0; i != NumVars; ++i) 12775 Vars.push_back(Record.readSubExpr()); 12776 C->setVarRefs(Vars); 12777 12778 SmallVector<Expr *, 16> UDMappers; 12779 UDMappers.reserve(NumVars); 12780 for (unsigned I = 0; I < NumVars; ++I) 12781 UDMappers.push_back(Record.readSubExpr()); 12782 C->setUDMapperRefs(UDMappers); 12783 12784 SmallVector<ValueDecl *, 16> Decls; 12785 Decls.reserve(UniqueDecls); 12786 for (unsigned i = 0; i < UniqueDecls; ++i) 12787 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12788 C->setUniqueDecls(Decls); 12789 12790 SmallVector<unsigned, 16> ListsPerDecl; 12791 ListsPerDecl.reserve(UniqueDecls); 12792 for (unsigned i = 0; i < UniqueDecls; ++i) 12793 ListsPerDecl.push_back(Record.readInt()); 12794 C->setDeclNumLists(ListsPerDecl); 12795 12796 SmallVector<unsigned, 32> ListSizes; 12797 ListSizes.reserve(TotalLists); 12798 for (unsigned i = 0; i < TotalLists; ++i) 12799 ListSizes.push_back(Record.readInt()); 12800 C->setComponentListSizes(ListSizes); 12801 12802 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12803 Components.reserve(TotalComponents); 12804 for (unsigned i = 0; i < TotalComponents; ++i) { 12805 Expr *AssociatedExprPr = Record.readSubExpr(); 12806 bool IsNonContiguous = Record.readBool(); 12807 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12808 Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous); 12809 } 12810 C->setComponents(Components, ListSizes); 12811 } 12812 12813 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) { 12814 C->setLParenLoc(Record.readSourceLocation()); 12815 auto NumVars = C->varlist_size(); 12816 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12817 auto TotalLists = C->getTotalComponentListNum(); 12818 auto TotalComponents = C->getTotalComponentsNum(); 12819 12820 SmallVector<Expr *, 16> Vars; 12821 Vars.reserve(NumVars); 12822 for (unsigned i = 0; i != NumVars; ++i) 12823 Vars.push_back(Record.readSubExpr()); 12824 C->setVarRefs(Vars); 12825 Vars.clear(); 12826 for (unsigned i = 0; i != NumVars; ++i) 12827 Vars.push_back(Record.readSubExpr()); 12828 C->setPrivateCopies(Vars); 12829 Vars.clear(); 12830 for (unsigned i = 0; i != NumVars; ++i) 12831 Vars.push_back(Record.readSubExpr()); 12832 C->setInits(Vars); 12833 12834 SmallVector<ValueDecl *, 16> Decls; 12835 Decls.reserve(UniqueDecls); 12836 for (unsigned i = 0; i < UniqueDecls; ++i) 12837 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12838 C->setUniqueDecls(Decls); 12839 12840 SmallVector<unsigned, 16> ListsPerDecl; 12841 ListsPerDecl.reserve(UniqueDecls); 12842 for (unsigned i = 0; i < UniqueDecls; ++i) 12843 ListsPerDecl.push_back(Record.readInt()); 12844 C->setDeclNumLists(ListsPerDecl); 12845 12846 SmallVector<unsigned, 32> ListSizes; 12847 ListSizes.reserve(TotalLists); 12848 for (unsigned i = 0; i < TotalLists; ++i) 12849 ListSizes.push_back(Record.readInt()); 12850 C->setComponentListSizes(ListSizes); 12851 12852 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12853 Components.reserve(TotalComponents); 12854 for (unsigned i = 0; i < TotalComponents; ++i) { 12855 auto *AssociatedExprPr = Record.readSubExpr(); 12856 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12857 Components.emplace_back(AssociatedExprPr, AssociatedDecl, 12858 /*IsNonContiguous=*/false); 12859 } 12860 C->setComponents(Components, ListSizes); 12861 } 12862 12863 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) { 12864 C->setLParenLoc(Record.readSourceLocation()); 12865 auto NumVars = C->varlist_size(); 12866 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12867 auto TotalLists = C->getTotalComponentListNum(); 12868 auto TotalComponents = C->getTotalComponentsNum(); 12869 12870 SmallVector<Expr *, 16> Vars; 12871 Vars.reserve(NumVars); 12872 for (unsigned i = 0; i != NumVars; ++i) 12873 Vars.push_back(Record.readSubExpr()); 12874 C->setVarRefs(Vars); 12875 12876 SmallVector<ValueDecl *, 16> Decls; 12877 Decls.reserve(UniqueDecls); 12878 for (unsigned i = 0; i < UniqueDecls; ++i) 12879 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12880 C->setUniqueDecls(Decls); 12881 12882 SmallVector<unsigned, 16> ListsPerDecl; 12883 ListsPerDecl.reserve(UniqueDecls); 12884 for (unsigned i = 0; i < UniqueDecls; ++i) 12885 ListsPerDecl.push_back(Record.readInt()); 12886 C->setDeclNumLists(ListsPerDecl); 12887 12888 SmallVector<unsigned, 32> ListSizes; 12889 ListSizes.reserve(TotalLists); 12890 for (unsigned i = 0; i < TotalLists; ++i) 12891 ListSizes.push_back(Record.readInt()); 12892 C->setComponentListSizes(ListSizes); 12893 12894 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12895 Components.reserve(TotalComponents); 12896 for (unsigned i = 0; i < TotalComponents; ++i) { 12897 Expr *AssociatedExpr = Record.readSubExpr(); 12898 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12899 Components.emplace_back(AssociatedExpr, AssociatedDecl, 12900 /*IsNonContiguous*/ false); 12901 } 12902 C->setComponents(Components, ListSizes); 12903 } 12904 12905 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) { 12906 C->setLParenLoc(Record.readSourceLocation()); 12907 auto NumVars = C->varlist_size(); 12908 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12909 auto TotalLists = C->getTotalComponentListNum(); 12910 auto TotalComponents = C->getTotalComponentsNum(); 12911 12912 SmallVector<Expr *, 16> Vars; 12913 Vars.reserve(NumVars); 12914 for (unsigned i = 0; i != NumVars; ++i) 12915 Vars.push_back(Record.readSubExpr()); 12916 C->setVarRefs(Vars); 12917 Vars.clear(); 12918 12919 SmallVector<ValueDecl *, 16> Decls; 12920 Decls.reserve(UniqueDecls); 12921 for (unsigned i = 0; i < UniqueDecls; ++i) 12922 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12923 C->setUniqueDecls(Decls); 12924 12925 SmallVector<unsigned, 16> ListsPerDecl; 12926 ListsPerDecl.reserve(UniqueDecls); 12927 for (unsigned i = 0; i < UniqueDecls; ++i) 12928 ListsPerDecl.push_back(Record.readInt()); 12929 C->setDeclNumLists(ListsPerDecl); 12930 12931 SmallVector<unsigned, 32> ListSizes; 12932 ListSizes.reserve(TotalLists); 12933 for (unsigned i = 0; i < TotalLists; ++i) 12934 ListSizes.push_back(Record.readInt()); 12935 C->setComponentListSizes(ListSizes); 12936 12937 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12938 Components.reserve(TotalComponents); 12939 for (unsigned i = 0; i < TotalComponents; ++i) { 12940 Expr *AssociatedExpr = Record.readSubExpr(); 12941 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12942 Components.emplace_back(AssociatedExpr, AssociatedDecl, 12943 /*IsNonContiguous=*/false); 12944 } 12945 C->setComponents(Components, ListSizes); 12946 } 12947 12948 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) { 12949 C->setLParenLoc(Record.readSourceLocation()); 12950 unsigned NumVars = C->varlist_size(); 12951 SmallVector<Expr *, 16> Vars; 12952 Vars.reserve(NumVars); 12953 for (unsigned i = 0; i != NumVars; ++i) 12954 Vars.push_back(Record.readSubExpr()); 12955 C->setVarRefs(Vars); 12956 Vars.clear(); 12957 Vars.reserve(NumVars); 12958 for (unsigned i = 0; i != NumVars; ++i) 12959 Vars.push_back(Record.readSubExpr()); 12960 C->setPrivateRefs(Vars); 12961 } 12962 12963 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) { 12964 C->setLParenLoc(Record.readSourceLocation()); 12965 unsigned NumVars = C->varlist_size(); 12966 SmallVector<Expr *, 16> Vars; 12967 Vars.reserve(NumVars); 12968 for (unsigned i = 0; i != NumVars; ++i) 12969 Vars.push_back(Record.readSubExpr()); 12970 C->setVarRefs(Vars); 12971 } 12972 12973 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) { 12974 C->setLParenLoc(Record.readSourceLocation()); 12975 unsigned NumVars = C->varlist_size(); 12976 SmallVector<Expr *, 16> Vars; 12977 Vars.reserve(NumVars); 12978 for (unsigned i = 0; i != NumVars; ++i) 12979 Vars.push_back(Record.readSubExpr()); 12980 C->setVarRefs(Vars); 12981 } 12982 12983 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) { 12984 C->setLParenLoc(Record.readSourceLocation()); 12985 unsigned NumOfAllocators = C->getNumberOfAllocators(); 12986 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data; 12987 Data.reserve(NumOfAllocators); 12988 for (unsigned I = 0; I != NumOfAllocators; ++I) { 12989 OMPUsesAllocatorsClause::Data &D = Data.emplace_back(); 12990 D.Allocator = Record.readSubExpr(); 12991 D.AllocatorTraits = Record.readSubExpr(); 12992 D.LParenLoc = Record.readSourceLocation(); 12993 D.RParenLoc = Record.readSourceLocation(); 12994 } 12995 C->setAllocatorsData(Data); 12996 } 12997 12998 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) { 12999 C->setLParenLoc(Record.readSourceLocation()); 13000 C->setModifier(Record.readSubExpr()); 13001 C->setColonLoc(Record.readSourceLocation()); 13002 unsigned NumOfLocators = C->varlist_size(); 13003 SmallVector<Expr *, 4> Locators; 13004 Locators.reserve(NumOfLocators); 13005 for (unsigned I = 0; I != NumOfLocators; ++I) 13006 Locators.push_back(Record.readSubExpr()); 13007 C->setVarRefs(Locators); 13008 } 13009 13010 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) { 13011 C->setKind(Record.readEnum<OpenMPOrderClauseKind>()); 13012 C->setLParenLoc(Record.readSourceLocation()); 13013 C->setKindKwLoc(Record.readSourceLocation()); 13014 } 13015 13016 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() { 13017 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo(); 13018 TI.Sets.resize(readUInt32()); 13019 for (auto &Set : TI.Sets) { 13020 Set.Kind = readEnum<llvm::omp::TraitSet>(); 13021 Set.Selectors.resize(readUInt32()); 13022 for (auto &Selector : Set.Selectors) { 13023 Selector.Kind = readEnum<llvm::omp::TraitSelector>(); 13024 Selector.ScoreOrCondition = nullptr; 13025 if (readBool()) 13026 Selector.ScoreOrCondition = readExprRef(); 13027 Selector.Properties.resize(readUInt32()); 13028 for (auto &Property : Selector.Properties) 13029 Property.Kind = readEnum<llvm::omp::TraitProperty>(); 13030 } 13031 } 13032 return &TI; 13033 } 13034 13035 void ASTRecordReader::readOMPChildren(OMPChildren *Data) { 13036 if (!Data) 13037 return; 13038 if (Reader->ReadingKind == ASTReader::Read_Stmt) { 13039 // Skip NumClauses, NumChildren and HasAssociatedStmt fields. 13040 skipInts(3); 13041 } 13042 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses()); 13043 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I) 13044 Clauses[I] = readOMPClause(); 13045 Data->setClauses(Clauses); 13046 if (Data->hasAssociatedStmt()) 13047 Data->setAssociatedStmt(readStmt()); 13048 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I) 13049 Data->getChildren()[I] = readStmt(); 13050 } 13051