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