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 396 #undef CHECK_TARGET_OPT 397 398 // Compare feature sets. 399 SmallVector<StringRef, 4> ExistingFeatures( 400 ExistingTargetOpts.FeaturesAsWritten.begin(), 401 ExistingTargetOpts.FeaturesAsWritten.end()); 402 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(), 403 TargetOpts.FeaturesAsWritten.end()); 404 llvm::sort(ExistingFeatures); 405 llvm::sort(ReadFeatures); 406 407 // We compute the set difference in both directions explicitly so that we can 408 // diagnose the differences differently. 409 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures; 410 std::set_difference( 411 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(), 412 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures)); 413 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(), 414 ExistingFeatures.begin(), ExistingFeatures.end(), 415 std::back_inserter(UnmatchedReadFeatures)); 416 417 // If we are allowing compatible differences and the read feature set is 418 // a strict subset of the existing feature set, there is nothing to diagnose. 419 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty()) 420 return false; 421 422 if (Diags) { 423 for (StringRef Feature : UnmatchedReadFeatures) 424 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 425 << /* is-existing-feature */ false << Feature; 426 for (StringRef Feature : UnmatchedExistingFeatures) 427 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 428 << /* is-existing-feature */ true << Feature; 429 } 430 431 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty(); 432 } 433 434 bool 435 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts, 436 bool Complain, 437 bool AllowCompatibleDifferences) { 438 const LangOptions &ExistingLangOpts = PP.getLangOpts(); 439 return checkLanguageOptions(LangOpts, ExistingLangOpts, 440 Complain ? &Reader.Diags : nullptr, 441 AllowCompatibleDifferences); 442 } 443 444 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts, 445 bool Complain, 446 bool AllowCompatibleDifferences) { 447 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts(); 448 return checkTargetOptions(TargetOpts, ExistingTargetOpts, 449 Complain ? &Reader.Diags : nullptr, 450 AllowCompatibleDifferences); 451 } 452 453 namespace { 454 455 using MacroDefinitionsMap = 456 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>; 457 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>; 458 459 } // namespace 460 461 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags, 462 DiagnosticsEngine &Diags, 463 bool Complain) { 464 using Level = DiagnosticsEngine::Level; 465 466 // Check current mappings for new -Werror mappings, and the stored mappings 467 // for cases that were explicitly mapped to *not* be errors that are now 468 // errors because of options like -Werror. 469 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags }; 470 471 for (DiagnosticsEngine *MappingSource : MappingSources) { 472 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) { 473 diag::kind DiagID = DiagIDMappingPair.first; 474 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation()); 475 if (CurLevel < DiagnosticsEngine::Error) 476 continue; // not significant 477 Level StoredLevel = 478 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation()); 479 if (StoredLevel < DiagnosticsEngine::Error) { 480 if (Complain) 481 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" + 482 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str(); 483 return true; 484 } 485 } 486 } 487 488 return false; 489 } 490 491 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) { 492 diag::Severity Ext = Diags.getExtensionHandlingBehavior(); 493 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors()) 494 return true; 495 return Ext >= diag::Severity::Error; 496 } 497 498 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags, 499 DiagnosticsEngine &Diags, 500 bool IsSystem, bool Complain) { 501 // Top-level options 502 if (IsSystem) { 503 if (Diags.getSuppressSystemWarnings()) 504 return false; 505 // If -Wsystem-headers was not enabled before, be conservative 506 if (StoredDiags.getSuppressSystemWarnings()) { 507 if (Complain) 508 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers"; 509 return true; 510 } 511 } 512 513 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) { 514 if (Complain) 515 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror"; 516 return true; 517 } 518 519 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() && 520 !StoredDiags.getEnableAllWarnings()) { 521 if (Complain) 522 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror"; 523 return true; 524 } 525 526 if (isExtHandlingFromDiagsError(Diags) && 527 !isExtHandlingFromDiagsError(StoredDiags)) { 528 if (Complain) 529 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors"; 530 return true; 531 } 532 533 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain); 534 } 535 536 /// Return the top import module if it is implicit, nullptr otherwise. 537 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr, 538 Preprocessor &PP) { 539 // If the original import came from a file explicitly generated by the user, 540 // don't check the diagnostic mappings. 541 // FIXME: currently this is approximated by checking whether this is not a 542 // module import of an implicitly-loaded module file. 543 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in 544 // the transitive closure of its imports, since unrelated modules cannot be 545 // imported until after this module finishes validation. 546 ModuleFile *TopImport = &*ModuleMgr.rbegin(); 547 while (!TopImport->ImportedBy.empty()) 548 TopImport = TopImport->ImportedBy[0]; 549 if (TopImport->Kind != MK_ImplicitModule) 550 return nullptr; 551 552 StringRef ModuleName = TopImport->ModuleName; 553 assert(!ModuleName.empty() && "diagnostic options read before module name"); 554 555 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName); 556 assert(M && "missing module"); 557 return M; 558 } 559 560 bool PCHValidator::ReadDiagnosticOptions( 561 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 562 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics(); 563 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs()); 564 IntrusiveRefCntPtr<DiagnosticsEngine> Diags( 565 new DiagnosticsEngine(DiagIDs, DiagOpts.get())); 566 // This should never fail, because we would have processed these options 567 // before writing them to an ASTFile. 568 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false); 569 570 ModuleManager &ModuleMgr = Reader.getModuleManager(); 571 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then"); 572 573 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP); 574 if (!TopM) 575 return false; 576 577 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that 578 // contains the union of their flags. 579 return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem, 580 Complain); 581 } 582 583 /// Collect the macro definitions provided by the given preprocessor 584 /// options. 585 static void 586 collectMacroDefinitions(const PreprocessorOptions &PPOpts, 587 MacroDefinitionsMap &Macros, 588 SmallVectorImpl<StringRef> *MacroNames = nullptr) { 589 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) { 590 StringRef Macro = PPOpts.Macros[I].first; 591 bool IsUndef = PPOpts.Macros[I].second; 592 593 std::pair<StringRef, StringRef> MacroPair = Macro.split('='); 594 StringRef MacroName = MacroPair.first; 595 StringRef MacroBody = MacroPair.second; 596 597 // For an #undef'd macro, we only care about the name. 598 if (IsUndef) { 599 if (MacroNames && !Macros.count(MacroName)) 600 MacroNames->push_back(MacroName); 601 602 Macros[MacroName] = std::make_pair("", true); 603 continue; 604 } 605 606 // For a #define'd macro, figure out the actual definition. 607 if (MacroName.size() == Macro.size()) 608 MacroBody = "1"; 609 else { 610 // Note: GCC drops anything following an end-of-line character. 611 StringRef::size_type End = MacroBody.find_first_of("\n\r"); 612 MacroBody = MacroBody.substr(0, End); 613 } 614 615 if (MacroNames && !Macros.count(MacroName)) 616 MacroNames->push_back(MacroName); 617 Macros[MacroName] = std::make_pair(MacroBody, false); 618 } 619 } 620 621 /// Check the preprocessor options deserialized from the control block 622 /// against the preprocessor options in an existing preprocessor. 623 /// 624 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 625 /// \param Validate If true, validate preprocessor options. If false, allow 626 /// macros defined by \p ExistingPPOpts to override those defined by 627 /// \p PPOpts in SuggestedPredefines. 628 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts, 629 const PreprocessorOptions &ExistingPPOpts, 630 DiagnosticsEngine *Diags, 631 FileManager &FileMgr, 632 std::string &SuggestedPredefines, 633 const LangOptions &LangOpts, 634 bool Validate = true) { 635 // Check macro definitions. 636 MacroDefinitionsMap ASTFileMacros; 637 collectMacroDefinitions(PPOpts, ASTFileMacros); 638 MacroDefinitionsMap ExistingMacros; 639 SmallVector<StringRef, 4> ExistingMacroNames; 640 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames); 641 642 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) { 643 // Dig out the macro definition in the existing preprocessor options. 644 StringRef MacroName = ExistingMacroNames[I]; 645 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName]; 646 647 // Check whether we know anything about this macro name or not. 648 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known = 649 ASTFileMacros.find(MacroName); 650 if (!Validate || Known == ASTFileMacros.end()) { 651 // FIXME: Check whether this identifier was referenced anywhere in the 652 // AST file. If so, we should reject the AST file. Unfortunately, this 653 // information isn't in the control block. What shall we do about it? 654 655 if (Existing.second) { 656 SuggestedPredefines += "#undef "; 657 SuggestedPredefines += MacroName.str(); 658 SuggestedPredefines += '\n'; 659 } else { 660 SuggestedPredefines += "#define "; 661 SuggestedPredefines += MacroName.str(); 662 SuggestedPredefines += ' '; 663 SuggestedPredefines += Existing.first.str(); 664 SuggestedPredefines += '\n'; 665 } 666 continue; 667 } 668 669 // If the macro was defined in one but undef'd in the other, we have a 670 // conflict. 671 if (Existing.second != Known->second.second) { 672 if (Diags) { 673 Diags->Report(diag::err_pch_macro_def_undef) 674 << MacroName << Known->second.second; 675 } 676 return true; 677 } 678 679 // If the macro was #undef'd in both, or if the macro bodies are identical, 680 // it's fine. 681 if (Existing.second || Existing.first == Known->second.first) 682 continue; 683 684 // The macro bodies differ; complain. 685 if (Diags) { 686 Diags->Report(diag::err_pch_macro_def_conflict) 687 << MacroName << Known->second.first << Existing.first; 688 } 689 return true; 690 } 691 692 // Check whether we're using predefines. 693 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) { 694 if (Diags) { 695 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines; 696 } 697 return true; 698 } 699 700 // Detailed record is important since it is used for the module cache hash. 701 if (LangOpts.Modules && 702 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) { 703 if (Diags) { 704 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord; 705 } 706 return true; 707 } 708 709 // Compute the #include and #include_macros lines we need. 710 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) { 711 StringRef File = ExistingPPOpts.Includes[I]; 712 713 if (!ExistingPPOpts.ImplicitPCHInclude.empty() && 714 !ExistingPPOpts.PCHThroughHeader.empty()) { 715 // In case the through header is an include, we must add all the includes 716 // to the predefines so the start point can be determined. 717 SuggestedPredefines += "#include \""; 718 SuggestedPredefines += File; 719 SuggestedPredefines += "\"\n"; 720 continue; 721 } 722 723 if (File == ExistingPPOpts.ImplicitPCHInclude) 724 continue; 725 726 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File) 727 != PPOpts.Includes.end()) 728 continue; 729 730 SuggestedPredefines += "#include \""; 731 SuggestedPredefines += File; 732 SuggestedPredefines += "\"\n"; 733 } 734 735 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) { 736 StringRef File = ExistingPPOpts.MacroIncludes[I]; 737 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(), 738 File) 739 != PPOpts.MacroIncludes.end()) 740 continue; 741 742 SuggestedPredefines += "#__include_macros \""; 743 SuggestedPredefines += File; 744 SuggestedPredefines += "\"\n##\n"; 745 } 746 747 return false; 748 } 749 750 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 751 bool Complain, 752 std::string &SuggestedPredefines) { 753 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts(); 754 755 return checkPreprocessorOptions(PPOpts, ExistingPPOpts, 756 Complain? &Reader.Diags : nullptr, 757 PP.getFileManager(), 758 SuggestedPredefines, 759 PP.getLangOpts()); 760 } 761 762 bool SimpleASTReaderListener::ReadPreprocessorOptions( 763 const PreprocessorOptions &PPOpts, 764 bool Complain, 765 std::string &SuggestedPredefines) { 766 return checkPreprocessorOptions(PPOpts, 767 PP.getPreprocessorOpts(), 768 nullptr, 769 PP.getFileManager(), 770 SuggestedPredefines, 771 PP.getLangOpts(), 772 false); 773 } 774 775 /// Check the header search options deserialized from the control block 776 /// against the header search options in an existing preprocessor. 777 /// 778 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 779 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 780 StringRef SpecificModuleCachePath, 781 StringRef ExistingModuleCachePath, 782 DiagnosticsEngine *Diags, 783 const LangOptions &LangOpts) { 784 if (LangOpts.Modules) { 785 if (SpecificModuleCachePath != ExistingModuleCachePath) { 786 if (Diags) 787 Diags->Report(diag::err_pch_modulecache_mismatch) 788 << SpecificModuleCachePath << ExistingModuleCachePath; 789 return true; 790 } 791 } 792 793 return false; 794 } 795 796 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 797 StringRef SpecificModuleCachePath, 798 bool Complain) { 799 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 800 PP.getHeaderSearchInfo().getModuleCachePath(), 801 Complain ? &Reader.Diags : nullptr, 802 PP.getLangOpts()); 803 } 804 805 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) { 806 PP.setCounterValue(Value); 807 } 808 809 //===----------------------------------------------------------------------===// 810 // AST reader implementation 811 //===----------------------------------------------------------------------===// 812 813 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener, 814 bool TakeOwnership) { 815 DeserializationListener = Listener; 816 OwnsDeserializationListener = TakeOwnership; 817 } 818 819 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) { 820 return serialization::ComputeHash(Sel); 821 } 822 823 std::pair<unsigned, unsigned> 824 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 825 using namespace llvm::support; 826 827 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 828 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 829 return std::make_pair(KeyLen, DataLen); 830 } 831 832 ASTSelectorLookupTrait::internal_key_type 833 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) { 834 using namespace llvm::support; 835 836 SelectorTable &SelTable = Reader.getContext().Selectors; 837 unsigned N = endian::readNext<uint16_t, little, unaligned>(d); 838 IdentifierInfo *FirstII = Reader.getLocalIdentifier( 839 F, endian::readNext<uint32_t, little, unaligned>(d)); 840 if (N == 0) 841 return SelTable.getNullarySelector(FirstII); 842 else if (N == 1) 843 return SelTable.getUnarySelector(FirstII); 844 845 SmallVector<IdentifierInfo *, 16> Args; 846 Args.push_back(FirstII); 847 for (unsigned I = 1; I != N; ++I) 848 Args.push_back(Reader.getLocalIdentifier( 849 F, endian::readNext<uint32_t, little, unaligned>(d))); 850 851 return SelTable.getSelector(N, Args.data()); 852 } 853 854 ASTSelectorLookupTrait::data_type 855 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d, 856 unsigned DataLen) { 857 using namespace llvm::support; 858 859 data_type Result; 860 861 Result.ID = Reader.getGlobalSelectorID( 862 F, endian::readNext<uint32_t, little, unaligned>(d)); 863 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d); 864 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d); 865 Result.InstanceBits = FullInstanceBits & 0x3; 866 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1; 867 Result.FactoryBits = FullFactoryBits & 0x3; 868 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1; 869 unsigned NumInstanceMethods = FullInstanceBits >> 3; 870 unsigned NumFactoryMethods = FullFactoryBits >> 3; 871 872 // Load instance methods 873 for (unsigned I = 0; I != NumInstanceMethods; ++I) { 874 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 875 F, endian::readNext<uint32_t, little, unaligned>(d))) 876 Result.Instance.push_back(Method); 877 } 878 879 // Load factory methods 880 for (unsigned I = 0; I != NumFactoryMethods; ++I) { 881 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 882 F, endian::readNext<uint32_t, little, unaligned>(d))) 883 Result.Factory.push_back(Method); 884 } 885 886 return Result; 887 } 888 889 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) { 890 return llvm::djbHash(a); 891 } 892 893 std::pair<unsigned, unsigned> 894 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) { 895 using namespace llvm::support; 896 897 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 898 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 899 return std::make_pair(KeyLen, DataLen); 900 } 901 902 ASTIdentifierLookupTraitBase::internal_key_type 903 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) { 904 assert(n >= 2 && d[n-1] == '\0'); 905 return StringRef((const char*) d, n-1); 906 } 907 908 /// Whether the given identifier is "interesting". 909 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II, 910 bool IsModule) { 911 return II.hadMacroDefinition() || 912 II.isPoisoned() || 913 (IsModule ? II.hasRevertedBuiltin() : II.getObjCOrBuiltinID()) || 914 II.hasRevertedTokenIDToIdentifier() || 915 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) && 916 II.getFETokenInfo()); 917 } 918 919 static bool readBit(unsigned &Bits) { 920 bool Value = Bits & 0x1; 921 Bits >>= 1; 922 return Value; 923 } 924 925 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) { 926 using namespace llvm::support; 927 928 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 929 return Reader.getGlobalIdentifierID(F, RawID >> 1); 930 } 931 932 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) { 933 if (!II.isFromAST()) { 934 II.setIsFromAST(); 935 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr; 936 if (isInterestingIdentifier(Reader, II, IsModule)) 937 II.setChangedSinceDeserialization(); 938 } 939 } 940 941 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k, 942 const unsigned char* d, 943 unsigned DataLen) { 944 using namespace llvm::support; 945 946 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 947 bool IsInteresting = RawID & 0x01; 948 949 // Wipe out the "is interesting" bit. 950 RawID = RawID >> 1; 951 952 // Build the IdentifierInfo and link the identifier ID with it. 953 IdentifierInfo *II = KnownII; 954 if (!II) { 955 II = &Reader.getIdentifierTable().getOwn(k); 956 KnownII = II; 957 } 958 markIdentifierFromAST(Reader, *II); 959 Reader.markIdentifierUpToDate(II); 960 961 IdentID ID = Reader.getGlobalIdentifierID(F, RawID); 962 if (!IsInteresting) { 963 // For uninteresting identifiers, there's nothing else to do. Just notify 964 // the reader that we've finished loading this identifier. 965 Reader.SetIdentifierInfo(ID, II); 966 return II; 967 } 968 969 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d); 970 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d); 971 bool CPlusPlusOperatorKeyword = readBit(Bits); 972 bool HasRevertedTokenIDToIdentifier = readBit(Bits); 973 bool HasRevertedBuiltin = 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 else if (HasRevertedBuiltin && II->getBuiltinID()) { 988 II->revertBuiltin(); 989 assert((II->hasRevertedBuiltin() || 990 II->getObjCOrBuiltinID() == ObjCOrBuiltinID) && 991 "Incorrect ObjC keyword or builtin ID"); 992 } 993 assert(II->isExtensionToken() == ExtensionToken && 994 "Incorrect extension token flag"); 995 (void)ExtensionToken; 996 if (Poisoned) 997 II->setIsPoisoned(true); 998 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword && 999 "Incorrect C++ operator keyword flag"); 1000 (void)CPlusPlusOperatorKeyword; 1001 1002 // If this identifier is a macro, deserialize the macro 1003 // definition. 1004 if (HadMacroDefinition) { 1005 uint32_t MacroDirectivesOffset = 1006 endian::readNext<uint32_t, little, unaligned>(d); 1007 DataLen -= 4; 1008 1009 Reader.addPendingMacro(II, &F, MacroDirectivesOffset); 1010 } 1011 1012 Reader.SetIdentifierInfo(ID, II); 1013 1014 // Read all of the declarations visible at global scope with this 1015 // name. 1016 if (DataLen > 0) { 1017 SmallVector<uint32_t, 4> DeclIDs; 1018 for (; DataLen > 0; DataLen -= 4) 1019 DeclIDs.push_back(Reader.getGlobalDeclID( 1020 F, endian::readNext<uint32_t, little, unaligned>(d))); 1021 Reader.SetGloballyVisibleDecls(II, DeclIDs); 1022 } 1023 1024 return II; 1025 } 1026 1027 DeclarationNameKey::DeclarationNameKey(DeclarationName Name) 1028 : Kind(Name.getNameKind()) { 1029 switch (Kind) { 1030 case DeclarationName::Identifier: 1031 Data = (uint64_t)Name.getAsIdentifierInfo(); 1032 break; 1033 case DeclarationName::ObjCZeroArgSelector: 1034 case DeclarationName::ObjCOneArgSelector: 1035 case DeclarationName::ObjCMultiArgSelector: 1036 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr(); 1037 break; 1038 case DeclarationName::CXXOperatorName: 1039 Data = Name.getCXXOverloadedOperator(); 1040 break; 1041 case DeclarationName::CXXLiteralOperatorName: 1042 Data = (uint64_t)Name.getCXXLiteralIdentifier(); 1043 break; 1044 case DeclarationName::CXXDeductionGuideName: 1045 Data = (uint64_t)Name.getCXXDeductionGuideTemplate() 1046 ->getDeclName().getAsIdentifierInfo(); 1047 break; 1048 case DeclarationName::CXXConstructorName: 1049 case DeclarationName::CXXDestructorName: 1050 case DeclarationName::CXXConversionFunctionName: 1051 case DeclarationName::CXXUsingDirective: 1052 Data = 0; 1053 break; 1054 } 1055 } 1056 1057 unsigned DeclarationNameKey::getHash() const { 1058 llvm::FoldingSetNodeID ID; 1059 ID.AddInteger(Kind); 1060 1061 switch (Kind) { 1062 case DeclarationName::Identifier: 1063 case DeclarationName::CXXLiteralOperatorName: 1064 case DeclarationName::CXXDeductionGuideName: 1065 ID.AddString(((IdentifierInfo*)Data)->getName()); 1066 break; 1067 case DeclarationName::ObjCZeroArgSelector: 1068 case DeclarationName::ObjCOneArgSelector: 1069 case DeclarationName::ObjCMultiArgSelector: 1070 ID.AddInteger(serialization::ComputeHash(Selector(Data))); 1071 break; 1072 case DeclarationName::CXXOperatorName: 1073 ID.AddInteger((OverloadedOperatorKind)Data); 1074 break; 1075 case DeclarationName::CXXConstructorName: 1076 case DeclarationName::CXXDestructorName: 1077 case DeclarationName::CXXConversionFunctionName: 1078 case DeclarationName::CXXUsingDirective: 1079 break; 1080 } 1081 1082 return ID.ComputeHash(); 1083 } 1084 1085 ModuleFile * 1086 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) { 1087 using namespace llvm::support; 1088 1089 uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d); 1090 return Reader.getLocalModuleFile(F, ModuleFileID); 1091 } 1092 1093 std::pair<unsigned, unsigned> 1094 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) { 1095 using namespace llvm::support; 1096 1097 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 1098 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 1099 return std::make_pair(KeyLen, DataLen); 1100 } 1101 1102 ASTDeclContextNameLookupTrait::internal_key_type 1103 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) { 1104 using namespace llvm::support; 1105 1106 auto Kind = (DeclarationName::NameKind)*d++; 1107 uint64_t Data; 1108 switch (Kind) { 1109 case DeclarationName::Identifier: 1110 case DeclarationName::CXXLiteralOperatorName: 1111 case DeclarationName::CXXDeductionGuideName: 1112 Data = (uint64_t)Reader.getLocalIdentifier( 1113 F, endian::readNext<uint32_t, little, unaligned>(d)); 1114 break; 1115 case DeclarationName::ObjCZeroArgSelector: 1116 case DeclarationName::ObjCOneArgSelector: 1117 case DeclarationName::ObjCMultiArgSelector: 1118 Data = 1119 (uint64_t)Reader.getLocalSelector( 1120 F, endian::readNext<uint32_t, little, unaligned>( 1121 d)).getAsOpaquePtr(); 1122 break; 1123 case DeclarationName::CXXOperatorName: 1124 Data = *d++; // OverloadedOperatorKind 1125 break; 1126 case DeclarationName::CXXConstructorName: 1127 case DeclarationName::CXXDestructorName: 1128 case DeclarationName::CXXConversionFunctionName: 1129 case DeclarationName::CXXUsingDirective: 1130 Data = 0; 1131 break; 1132 } 1133 1134 return DeclarationNameKey(Kind, Data); 1135 } 1136 1137 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type, 1138 const unsigned char *d, 1139 unsigned DataLen, 1140 data_type_builder &Val) { 1141 using namespace llvm::support; 1142 1143 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) { 1144 uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d); 1145 Val.insert(Reader.getGlobalDeclID(F, LocalID)); 1146 } 1147 } 1148 1149 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M, 1150 BitstreamCursor &Cursor, 1151 uint64_t Offset, 1152 DeclContext *DC) { 1153 assert(Offset != 0); 1154 1155 SavedStreamPosition SavedPosition(Cursor); 1156 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1157 Error(std::move(Err)); 1158 return true; 1159 } 1160 1161 RecordData Record; 1162 StringRef Blob; 1163 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1164 if (!MaybeCode) { 1165 Error(MaybeCode.takeError()); 1166 return true; 1167 } 1168 unsigned Code = MaybeCode.get(); 1169 1170 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1171 if (!MaybeRecCode) { 1172 Error(MaybeRecCode.takeError()); 1173 return true; 1174 } 1175 unsigned RecCode = MaybeRecCode.get(); 1176 if (RecCode != DECL_CONTEXT_LEXICAL) { 1177 Error("Expected lexical block"); 1178 return true; 1179 } 1180 1181 assert(!isa<TranslationUnitDecl>(DC) && 1182 "expected a TU_UPDATE_LEXICAL record for TU"); 1183 // If we are handling a C++ class template instantiation, we can see multiple 1184 // lexical updates for the same record. It's important that we select only one 1185 // of them, so that field numbering works properly. Just pick the first one we 1186 // see. 1187 auto &Lex = LexicalDecls[DC]; 1188 if (!Lex.first) { 1189 Lex = std::make_pair( 1190 &M, llvm::makeArrayRef( 1191 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 1192 Blob.data()), 1193 Blob.size() / 4)); 1194 } 1195 DC->setHasExternalLexicalStorage(true); 1196 return false; 1197 } 1198 1199 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M, 1200 BitstreamCursor &Cursor, 1201 uint64_t Offset, 1202 DeclID ID) { 1203 assert(Offset != 0); 1204 1205 SavedStreamPosition SavedPosition(Cursor); 1206 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1207 Error(std::move(Err)); 1208 return true; 1209 } 1210 1211 RecordData Record; 1212 StringRef Blob; 1213 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1214 if (!MaybeCode) { 1215 Error(MaybeCode.takeError()); 1216 return true; 1217 } 1218 unsigned Code = MaybeCode.get(); 1219 1220 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1221 if (!MaybeRecCode) { 1222 Error(MaybeRecCode.takeError()); 1223 return true; 1224 } 1225 unsigned RecCode = MaybeRecCode.get(); 1226 if (RecCode != DECL_CONTEXT_VISIBLE) { 1227 Error("Expected visible lookup table block"); 1228 return true; 1229 } 1230 1231 // We can't safely determine the primary context yet, so delay attaching the 1232 // lookup table until we're done with recursive deserialization. 1233 auto *Data = (const unsigned char*)Blob.data(); 1234 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data}); 1235 return false; 1236 } 1237 1238 void ASTReader::Error(StringRef Msg) const { 1239 Error(diag::err_fe_pch_malformed, Msg); 1240 if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() && 1241 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) { 1242 Diag(diag::note_module_cache_path) 1243 << PP.getHeaderSearchInfo().getModuleCachePath(); 1244 } 1245 } 1246 1247 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2, 1248 StringRef Arg3) const { 1249 if (Diags.isDiagnosticInFlight()) 1250 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3); 1251 else 1252 Diag(DiagID) << Arg1 << Arg2 << Arg3; 1253 } 1254 1255 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2, 1256 unsigned Select) const { 1257 if (!Diags.isDiagnosticInFlight()) 1258 Diag(DiagID) << Arg1 << Arg2 << Select; 1259 } 1260 1261 void ASTReader::Error(llvm::Error &&Err) const { 1262 Error(toString(std::move(Err))); 1263 } 1264 1265 //===----------------------------------------------------------------------===// 1266 // Source Manager Deserialization 1267 //===----------------------------------------------------------------------===// 1268 1269 /// Read the line table in the source manager block. 1270 /// \returns true if there was an error. 1271 bool ASTReader::ParseLineTable(ModuleFile &F, 1272 const RecordData &Record) { 1273 unsigned Idx = 0; 1274 LineTableInfo &LineTable = SourceMgr.getLineTable(); 1275 1276 // Parse the file names 1277 std::map<int, int> FileIDs; 1278 FileIDs[-1] = -1; // For unspecified filenames. 1279 for (unsigned I = 0; Record[Idx]; ++I) { 1280 // Extract the file name 1281 auto Filename = ReadPath(F, Record, Idx); 1282 FileIDs[I] = LineTable.getLineTableFilenameID(Filename); 1283 } 1284 ++Idx; 1285 1286 // Parse the line entries 1287 std::vector<LineEntry> Entries; 1288 while (Idx < Record.size()) { 1289 int FID = Record[Idx++]; 1290 assert(FID >= 0 && "Serialized line entries for non-local file."); 1291 // Remap FileID from 1-based old view. 1292 FID += F.SLocEntryBaseID - 1; 1293 1294 // Extract the line entries 1295 unsigned NumEntries = Record[Idx++]; 1296 assert(NumEntries && "no line entries for file ID"); 1297 Entries.clear(); 1298 Entries.reserve(NumEntries); 1299 for (unsigned I = 0; I != NumEntries; ++I) { 1300 unsigned FileOffset = Record[Idx++]; 1301 unsigned LineNo = Record[Idx++]; 1302 int FilenameID = FileIDs[Record[Idx++]]; 1303 SrcMgr::CharacteristicKind FileKind 1304 = (SrcMgr::CharacteristicKind)Record[Idx++]; 1305 unsigned IncludeOffset = Record[Idx++]; 1306 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, 1307 FileKind, IncludeOffset)); 1308 } 1309 LineTable.AddEntry(FileID::get(FID), Entries); 1310 } 1311 1312 return false; 1313 } 1314 1315 /// Read a source manager block 1316 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) { 1317 using namespace SrcMgr; 1318 1319 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor; 1320 1321 // Set the source-location entry cursor to the current position in 1322 // the stream. This cursor will be used to read the contents of the 1323 // source manager block initially, and then lazily read 1324 // source-location entries as needed. 1325 SLocEntryCursor = F.Stream; 1326 1327 // The stream itself is going to skip over the source manager block. 1328 if (llvm::Error Err = F.Stream.SkipBlock()) { 1329 Error(std::move(Err)); 1330 return true; 1331 } 1332 1333 // Enter the source manager block. 1334 if (llvm::Error Err = 1335 SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) { 1336 Error(std::move(Err)); 1337 return true; 1338 } 1339 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo(); 1340 1341 RecordData Record; 1342 while (true) { 1343 Expected<llvm::BitstreamEntry> MaybeE = 1344 SLocEntryCursor.advanceSkippingSubblocks(); 1345 if (!MaybeE) { 1346 Error(MaybeE.takeError()); 1347 return true; 1348 } 1349 llvm::BitstreamEntry E = MaybeE.get(); 1350 1351 switch (E.Kind) { 1352 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1353 case llvm::BitstreamEntry::Error: 1354 Error("malformed block record in AST file"); 1355 return true; 1356 case llvm::BitstreamEntry::EndBlock: 1357 return false; 1358 case llvm::BitstreamEntry::Record: 1359 // The interesting case. 1360 break; 1361 } 1362 1363 // Read a record. 1364 Record.clear(); 1365 StringRef Blob; 1366 Expected<unsigned> MaybeRecord = 1367 SLocEntryCursor.readRecord(E.ID, Record, &Blob); 1368 if (!MaybeRecord) { 1369 Error(MaybeRecord.takeError()); 1370 return true; 1371 } 1372 switch (MaybeRecord.get()) { 1373 default: // Default behavior: ignore. 1374 break; 1375 1376 case SM_SLOC_FILE_ENTRY: 1377 case SM_SLOC_BUFFER_ENTRY: 1378 case SM_SLOC_EXPANSION_ENTRY: 1379 // Once we hit one of the source location entries, we're done. 1380 return false; 1381 } 1382 } 1383 } 1384 1385 /// If a header file is not found at the path that we expect it to be 1386 /// and the PCH file was moved from its original location, try to resolve the 1387 /// file by assuming that header+PCH were moved together and the header is in 1388 /// the same place relative to the PCH. 1389 static std::string 1390 resolveFileRelativeToOriginalDir(const std::string &Filename, 1391 const std::string &OriginalDir, 1392 const std::string &CurrDir) { 1393 assert(OriginalDir != CurrDir && 1394 "No point trying to resolve the file if the PCH dir didn't change"); 1395 1396 using namespace llvm::sys; 1397 1398 SmallString<128> filePath(Filename); 1399 fs::make_absolute(filePath); 1400 assert(path::is_absolute(OriginalDir)); 1401 SmallString<128> currPCHPath(CurrDir); 1402 1403 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)), 1404 fileDirE = path::end(path::parent_path(filePath)); 1405 path::const_iterator origDirI = path::begin(OriginalDir), 1406 origDirE = path::end(OriginalDir); 1407 // Skip the common path components from filePath and OriginalDir. 1408 while (fileDirI != fileDirE && origDirI != origDirE && 1409 *fileDirI == *origDirI) { 1410 ++fileDirI; 1411 ++origDirI; 1412 } 1413 for (; origDirI != origDirE; ++origDirI) 1414 path::append(currPCHPath, ".."); 1415 path::append(currPCHPath, fileDirI, fileDirE); 1416 path::append(currPCHPath, path::filename(Filename)); 1417 return std::string(currPCHPath.str()); 1418 } 1419 1420 bool ASTReader::ReadSLocEntry(int ID) { 1421 if (ID == 0) 1422 return false; 1423 1424 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1425 Error("source location entry ID out-of-range for AST file"); 1426 return true; 1427 } 1428 1429 // Local helper to read the (possibly-compressed) buffer data following the 1430 // entry record. 1431 auto ReadBuffer = [this]( 1432 BitstreamCursor &SLocEntryCursor, 1433 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> { 1434 RecordData Record; 1435 StringRef Blob; 1436 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode(); 1437 if (!MaybeCode) { 1438 Error(MaybeCode.takeError()); 1439 return nullptr; 1440 } 1441 unsigned Code = MaybeCode.get(); 1442 1443 Expected<unsigned> MaybeRecCode = 1444 SLocEntryCursor.readRecord(Code, Record, &Blob); 1445 if (!MaybeRecCode) { 1446 Error(MaybeRecCode.takeError()); 1447 return nullptr; 1448 } 1449 unsigned RecCode = MaybeRecCode.get(); 1450 1451 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) { 1452 if (!llvm::zlib::isAvailable()) { 1453 Error("zlib is not available"); 1454 return nullptr; 1455 } 1456 SmallString<0> Uncompressed; 1457 if (llvm::Error E = 1458 llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) { 1459 Error("could not decompress embedded file contents: " + 1460 llvm::toString(std::move(E))); 1461 return nullptr; 1462 } 1463 return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name); 1464 } else if (RecCode == SM_SLOC_BUFFER_BLOB) { 1465 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true); 1466 } else { 1467 Error("AST record has invalid code"); 1468 return nullptr; 1469 } 1470 }; 1471 1472 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second; 1473 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit( 1474 F->SLocEntryOffsetsBase + 1475 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) { 1476 Error(std::move(Err)); 1477 return true; 1478 } 1479 1480 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor; 1481 unsigned BaseOffset = F->SLocEntryBaseOffset; 1482 1483 ++NumSLocEntriesRead; 1484 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance(); 1485 if (!MaybeEntry) { 1486 Error(MaybeEntry.takeError()); 1487 return true; 1488 } 1489 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1490 1491 if (Entry.Kind != llvm::BitstreamEntry::Record) { 1492 Error("incorrectly-formatted source location entry in AST file"); 1493 return true; 1494 } 1495 1496 RecordData Record; 1497 StringRef Blob; 1498 Expected<unsigned> MaybeSLOC = 1499 SLocEntryCursor.readRecord(Entry.ID, Record, &Blob); 1500 if (!MaybeSLOC) { 1501 Error(MaybeSLOC.takeError()); 1502 return true; 1503 } 1504 switch (MaybeSLOC.get()) { 1505 default: 1506 Error("incorrectly-formatted source location entry in AST file"); 1507 return true; 1508 1509 case SM_SLOC_FILE_ENTRY: { 1510 // We will detect whether a file changed and return 'Failure' for it, but 1511 // we will also try to fail gracefully by setting up the SLocEntry. 1512 unsigned InputID = Record[4]; 1513 InputFile IF = getInputFile(*F, InputID); 1514 const FileEntry *File = IF.getFile(); 1515 bool OverriddenBuffer = IF.isOverridden(); 1516 1517 // Note that we only check if a File was returned. If it was out-of-date 1518 // we have complained but we will continue creating a FileID to recover 1519 // gracefully. 1520 if (!File) 1521 return true; 1522 1523 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1524 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) { 1525 // This is the module's main file. 1526 IncludeLoc = getImportLocation(F); 1527 } 1528 SrcMgr::CharacteristicKind 1529 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1530 // FIXME: The FileID should be created from the FileEntryRef. 1531 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter, 1532 ID, BaseOffset + Record[0]); 1533 SrcMgr::FileInfo &FileInfo = 1534 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()); 1535 FileInfo.NumCreatedFIDs = Record[5]; 1536 if (Record[3]) 1537 FileInfo.setHasLineDirectives(); 1538 1539 unsigned NumFileDecls = Record[7]; 1540 if (NumFileDecls && ContextObj) { 1541 const DeclID *FirstDecl = F->FileSortedDecls + Record[6]; 1542 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?"); 1543 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl, 1544 NumFileDecls)); 1545 } 1546 1547 const SrcMgr::ContentCache *ContentCache 1548 = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter)); 1549 if (OverriddenBuffer && !ContentCache->BufferOverridden && 1550 ContentCache->ContentsEntry == ContentCache->OrigEntry && 1551 !ContentCache->getRawBuffer()) { 1552 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName()); 1553 if (!Buffer) 1554 return true; 1555 SourceMgr.overrideFileContents(File, std::move(Buffer)); 1556 } 1557 1558 break; 1559 } 1560 1561 case SM_SLOC_BUFFER_ENTRY: { 1562 const char *Name = Blob.data(); 1563 unsigned Offset = Record[0]; 1564 SrcMgr::CharacteristicKind 1565 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1566 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1567 if (IncludeLoc.isInvalid() && F->isModule()) { 1568 IncludeLoc = getImportLocation(F); 1569 } 1570 1571 auto Buffer = ReadBuffer(SLocEntryCursor, Name); 1572 if (!Buffer) 1573 return true; 1574 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID, 1575 BaseOffset + Offset, IncludeLoc); 1576 break; 1577 } 1578 1579 case SM_SLOC_EXPANSION_ENTRY: { 1580 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]); 1581 SourceMgr.createExpansionLoc(SpellingLoc, 1582 ReadSourceLocation(*F, Record[2]), 1583 ReadSourceLocation(*F, Record[3]), 1584 Record[5], 1585 Record[4], 1586 ID, 1587 BaseOffset + Record[0]); 1588 break; 1589 } 1590 } 1591 1592 return false; 1593 } 1594 1595 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) { 1596 if (ID == 0) 1597 return std::make_pair(SourceLocation(), ""); 1598 1599 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1600 Error("source location entry ID out-of-range for AST file"); 1601 return std::make_pair(SourceLocation(), ""); 1602 } 1603 1604 // Find which module file this entry lands in. 1605 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second; 1606 if (!M->isModule()) 1607 return std::make_pair(SourceLocation(), ""); 1608 1609 // FIXME: Can we map this down to a particular submodule? That would be 1610 // ideal. 1611 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName)); 1612 } 1613 1614 /// Find the location where the module F is imported. 1615 SourceLocation ASTReader::getImportLocation(ModuleFile *F) { 1616 if (F->ImportLoc.isValid()) 1617 return F->ImportLoc; 1618 1619 // Otherwise we have a PCH. It's considered to be "imported" at the first 1620 // location of its includer. 1621 if (F->ImportedBy.empty() || !F->ImportedBy[0]) { 1622 // Main file is the importer. 1623 assert(SourceMgr.getMainFileID().isValid() && "missing main file"); 1624 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); 1625 } 1626 return F->ImportedBy[0]->FirstLoc; 1627 } 1628 1629 /// Enter a subblock of the specified BlockID with the specified cursor. Read 1630 /// the abbreviations that are at the top of the block and then leave the cursor 1631 /// pointing into the block. 1632 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID, 1633 uint64_t *StartOfBlockOffset) { 1634 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) { 1635 // FIXME this drops errors on the floor. 1636 consumeError(std::move(Err)); 1637 return true; 1638 } 1639 1640 if (StartOfBlockOffset) 1641 *StartOfBlockOffset = Cursor.GetCurrentBitNo(); 1642 1643 while (true) { 1644 uint64_t Offset = Cursor.GetCurrentBitNo(); 1645 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1646 if (!MaybeCode) { 1647 // FIXME this drops errors on the floor. 1648 consumeError(MaybeCode.takeError()); 1649 return true; 1650 } 1651 unsigned Code = MaybeCode.get(); 1652 1653 // We expect all abbrevs to be at the start of the block. 1654 if (Code != llvm::bitc::DEFINE_ABBREV) { 1655 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1656 // FIXME this drops errors on the floor. 1657 consumeError(std::move(Err)); 1658 return true; 1659 } 1660 return false; 1661 } 1662 if (llvm::Error Err = Cursor.ReadAbbrevRecord()) { 1663 // FIXME this drops errors on the floor. 1664 consumeError(std::move(Err)); 1665 return true; 1666 } 1667 } 1668 } 1669 1670 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record, 1671 unsigned &Idx) { 1672 Token Tok; 1673 Tok.startToken(); 1674 Tok.setLocation(ReadSourceLocation(F, Record, Idx)); 1675 Tok.setLength(Record[Idx++]); 1676 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++])) 1677 Tok.setIdentifierInfo(II); 1678 Tok.setKind((tok::TokenKind)Record[Idx++]); 1679 Tok.setFlag((Token::TokenFlags)Record[Idx++]); 1680 return Tok; 1681 } 1682 1683 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) { 1684 BitstreamCursor &Stream = F.MacroCursor; 1685 1686 // Keep track of where we are in the stream, then jump back there 1687 // after reading this macro. 1688 SavedStreamPosition SavedPosition(Stream); 1689 1690 if (llvm::Error Err = Stream.JumpToBit(Offset)) { 1691 // FIXME this drops errors on the floor. 1692 consumeError(std::move(Err)); 1693 return nullptr; 1694 } 1695 RecordData Record; 1696 SmallVector<IdentifierInfo*, 16> MacroParams; 1697 MacroInfo *Macro = nullptr; 1698 1699 while (true) { 1700 // Advance to the next record, but if we get to the end of the block, don't 1701 // pop it (removing all the abbreviations from the cursor) since we want to 1702 // be able to reseek within the block and read entries. 1703 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd; 1704 Expected<llvm::BitstreamEntry> MaybeEntry = 1705 Stream.advanceSkippingSubblocks(Flags); 1706 if (!MaybeEntry) { 1707 Error(MaybeEntry.takeError()); 1708 return Macro; 1709 } 1710 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1711 1712 switch (Entry.Kind) { 1713 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1714 case llvm::BitstreamEntry::Error: 1715 Error("malformed block record in AST file"); 1716 return Macro; 1717 case llvm::BitstreamEntry::EndBlock: 1718 return Macro; 1719 case llvm::BitstreamEntry::Record: 1720 // The interesting case. 1721 break; 1722 } 1723 1724 // Read a record. 1725 Record.clear(); 1726 PreprocessorRecordTypes RecType; 1727 if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record)) 1728 RecType = (PreprocessorRecordTypes)MaybeRecType.get(); 1729 else { 1730 Error(MaybeRecType.takeError()); 1731 return Macro; 1732 } 1733 switch (RecType) { 1734 case PP_MODULE_MACRO: 1735 case PP_MACRO_DIRECTIVE_HISTORY: 1736 return Macro; 1737 1738 case PP_MACRO_OBJECT_LIKE: 1739 case PP_MACRO_FUNCTION_LIKE: { 1740 // If we already have a macro, that means that we've hit the end 1741 // of the definition of the macro we were looking for. We're 1742 // done. 1743 if (Macro) 1744 return Macro; 1745 1746 unsigned NextIndex = 1; // Skip identifier ID. 1747 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex); 1748 MacroInfo *MI = PP.AllocateMacroInfo(Loc); 1749 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex)); 1750 MI->setIsUsed(Record[NextIndex++]); 1751 MI->setUsedForHeaderGuard(Record[NextIndex++]); 1752 1753 if (RecType == PP_MACRO_FUNCTION_LIKE) { 1754 // Decode function-like macro info. 1755 bool isC99VarArgs = Record[NextIndex++]; 1756 bool isGNUVarArgs = Record[NextIndex++]; 1757 bool hasCommaPasting = Record[NextIndex++]; 1758 MacroParams.clear(); 1759 unsigned NumArgs = Record[NextIndex++]; 1760 for (unsigned i = 0; i != NumArgs; ++i) 1761 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++])); 1762 1763 // Install function-like macro info. 1764 MI->setIsFunctionLike(); 1765 if (isC99VarArgs) MI->setIsC99Varargs(); 1766 if (isGNUVarArgs) MI->setIsGNUVarargs(); 1767 if (hasCommaPasting) MI->setHasCommaPasting(); 1768 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator()); 1769 } 1770 1771 // Remember that we saw this macro last so that we add the tokens that 1772 // form its body to it. 1773 Macro = MI; 1774 1775 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() && 1776 Record[NextIndex]) { 1777 // We have a macro definition. Register the association 1778 PreprocessedEntityID 1779 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]); 1780 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 1781 PreprocessingRecord::PPEntityID PPID = 1782 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true); 1783 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>( 1784 PPRec.getPreprocessedEntity(PPID)); 1785 if (PPDef) 1786 PPRec.RegisterMacroDefinition(Macro, PPDef); 1787 } 1788 1789 ++NumMacrosRead; 1790 break; 1791 } 1792 1793 case PP_TOKEN: { 1794 // If we see a TOKEN before a PP_MACRO_*, then the file is 1795 // erroneous, just pretend we didn't see this. 1796 if (!Macro) break; 1797 1798 unsigned Idx = 0; 1799 Token Tok = ReadToken(F, Record, Idx); 1800 Macro->AddTokenToBody(Tok); 1801 break; 1802 } 1803 } 1804 } 1805 } 1806 1807 PreprocessedEntityID 1808 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, 1809 unsigned LocalID) const { 1810 if (!M.ModuleOffsetMap.empty()) 1811 ReadModuleOffsetMap(M); 1812 1813 ContinuousRangeMap<uint32_t, int, 2>::const_iterator 1814 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS); 1815 assert(I != M.PreprocessedEntityRemap.end() 1816 && "Invalid index into preprocessed entity index remap"); 1817 1818 return LocalID + I->second; 1819 } 1820 1821 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) { 1822 return llvm::hash_combine(ikey.Size, ikey.ModTime); 1823 } 1824 1825 HeaderFileInfoTrait::internal_key_type 1826 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) { 1827 internal_key_type ikey = {FE->getSize(), 1828 M.HasTimestamps ? FE->getModificationTime() : 0, 1829 FE->getName(), /*Imported*/ false}; 1830 return ikey; 1831 } 1832 1833 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) { 1834 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime)) 1835 return false; 1836 1837 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename) 1838 return true; 1839 1840 // Determine whether the actual files are equivalent. 1841 FileManager &FileMgr = Reader.getFileManager(); 1842 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* { 1843 if (!Key.Imported) { 1844 if (auto File = FileMgr.getFile(Key.Filename)) 1845 return *File; 1846 return nullptr; 1847 } 1848 1849 std::string Resolved = std::string(Key.Filename); 1850 Reader.ResolveImportedPath(M, Resolved); 1851 if (auto File = FileMgr.getFile(Resolved)) 1852 return *File; 1853 return nullptr; 1854 }; 1855 1856 const FileEntry *FEA = GetFile(a); 1857 const FileEntry *FEB = GetFile(b); 1858 return FEA && FEA == FEB; 1859 } 1860 1861 std::pair<unsigned, unsigned> 1862 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) { 1863 using namespace llvm::support; 1864 1865 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d); 1866 unsigned DataLen = (unsigned) *d++; 1867 return std::make_pair(KeyLen, DataLen); 1868 } 1869 1870 HeaderFileInfoTrait::internal_key_type 1871 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) { 1872 using namespace llvm::support; 1873 1874 internal_key_type ikey; 1875 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d)); 1876 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d)); 1877 ikey.Filename = (const char *)d; 1878 ikey.Imported = true; 1879 return ikey; 1880 } 1881 1882 HeaderFileInfoTrait::data_type 1883 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d, 1884 unsigned DataLen) { 1885 using namespace llvm::support; 1886 1887 const unsigned char *End = d + DataLen; 1888 HeaderFileInfo HFI; 1889 unsigned Flags = *d++; 1890 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp. 1891 HFI.isImport |= (Flags >> 5) & 0x01; 1892 HFI.isPragmaOnce |= (Flags >> 4) & 0x01; 1893 HFI.DirInfo = (Flags >> 1) & 0x07; 1894 HFI.IndexHeaderMapHeader = Flags & 0x01; 1895 // FIXME: Find a better way to handle this. Maybe just store a 1896 // "has been included" flag? 1897 HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d), 1898 HFI.NumIncludes); 1899 HFI.ControllingMacroID = Reader.getGlobalIdentifierID( 1900 M, endian::readNext<uint32_t, little, unaligned>(d)); 1901 if (unsigned FrameworkOffset = 1902 endian::readNext<uint32_t, little, unaligned>(d)) { 1903 // The framework offset is 1 greater than the actual offset, 1904 // since 0 is used as an indicator for "no framework name". 1905 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1); 1906 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName); 1907 } 1908 1909 assert((End - d) % 4 == 0 && 1910 "Wrong data length in HeaderFileInfo deserialization"); 1911 while (d != End) { 1912 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d); 1913 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3); 1914 LocalSMID >>= 2; 1915 1916 // This header is part of a module. Associate it with the module to enable 1917 // implicit module import. 1918 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID); 1919 Module *Mod = Reader.getSubmodule(GlobalSMID); 1920 FileManager &FileMgr = Reader.getFileManager(); 1921 ModuleMap &ModMap = 1922 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap(); 1923 1924 std::string Filename = std::string(key.Filename); 1925 if (key.Imported) 1926 Reader.ResolveImportedPath(M, Filename); 1927 // FIXME: This is not always the right filename-as-written, but we're not 1928 // going to use this information to rebuild the module, so it doesn't make 1929 // a lot of difference. 1930 Module::Header H = {std::string(key.Filename), *FileMgr.getFile(Filename)}; 1931 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true); 1932 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader); 1933 } 1934 1935 // This HeaderFileInfo was externally loaded. 1936 HFI.External = true; 1937 HFI.IsValid = true; 1938 return HFI; 1939 } 1940 1941 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M, 1942 uint32_t MacroDirectivesOffset) { 1943 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard"); 1944 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset)); 1945 } 1946 1947 void ASTReader::ReadDefinedMacros() { 1948 // Note that we are loading defined macros. 1949 Deserializing Macros(this); 1950 1951 for (ModuleFile &I : llvm::reverse(ModuleMgr)) { 1952 BitstreamCursor &MacroCursor = I.MacroCursor; 1953 1954 // If there was no preprocessor block, skip this file. 1955 if (MacroCursor.getBitcodeBytes().empty()) 1956 continue; 1957 1958 BitstreamCursor Cursor = MacroCursor; 1959 if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) { 1960 Error(std::move(Err)); 1961 return; 1962 } 1963 1964 RecordData Record; 1965 while (true) { 1966 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks(); 1967 if (!MaybeE) { 1968 Error(MaybeE.takeError()); 1969 return; 1970 } 1971 llvm::BitstreamEntry E = MaybeE.get(); 1972 1973 switch (E.Kind) { 1974 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1975 case llvm::BitstreamEntry::Error: 1976 Error("malformed block record in AST file"); 1977 return; 1978 case llvm::BitstreamEntry::EndBlock: 1979 goto NextCursor; 1980 1981 case llvm::BitstreamEntry::Record: { 1982 Record.clear(); 1983 Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record); 1984 if (!MaybeRecord) { 1985 Error(MaybeRecord.takeError()); 1986 return; 1987 } 1988 switch (MaybeRecord.get()) { 1989 default: // Default behavior: ignore. 1990 break; 1991 1992 case PP_MACRO_OBJECT_LIKE: 1993 case PP_MACRO_FUNCTION_LIKE: { 1994 IdentifierInfo *II = getLocalIdentifier(I, Record[0]); 1995 if (II->isOutOfDate()) 1996 updateOutOfDateIdentifier(*II); 1997 break; 1998 } 1999 2000 case PP_TOKEN: 2001 // Ignore tokens. 2002 break; 2003 } 2004 break; 2005 } 2006 } 2007 } 2008 NextCursor: ; 2009 } 2010 } 2011 2012 namespace { 2013 2014 /// Visitor class used to look up identifirs in an AST file. 2015 class IdentifierLookupVisitor { 2016 StringRef Name; 2017 unsigned NameHash; 2018 unsigned PriorGeneration; 2019 unsigned &NumIdentifierLookups; 2020 unsigned &NumIdentifierLookupHits; 2021 IdentifierInfo *Found = nullptr; 2022 2023 public: 2024 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration, 2025 unsigned &NumIdentifierLookups, 2026 unsigned &NumIdentifierLookupHits) 2027 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)), 2028 PriorGeneration(PriorGeneration), 2029 NumIdentifierLookups(NumIdentifierLookups), 2030 NumIdentifierLookupHits(NumIdentifierLookupHits) {} 2031 2032 bool operator()(ModuleFile &M) { 2033 // If we've already searched this module file, skip it now. 2034 if (M.Generation <= PriorGeneration) 2035 return true; 2036 2037 ASTIdentifierLookupTable *IdTable 2038 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable; 2039 if (!IdTable) 2040 return false; 2041 2042 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M, 2043 Found); 2044 ++NumIdentifierLookups; 2045 ASTIdentifierLookupTable::iterator Pos = 2046 IdTable->find_hashed(Name, NameHash, &Trait); 2047 if (Pos == IdTable->end()) 2048 return false; 2049 2050 // Dereferencing the iterator has the effect of building the 2051 // IdentifierInfo node and populating it with the various 2052 // declarations it needs. 2053 ++NumIdentifierLookupHits; 2054 Found = *Pos; 2055 return true; 2056 } 2057 2058 // Retrieve the identifier info found within the module 2059 // files. 2060 IdentifierInfo *getIdentifierInfo() const { return Found; } 2061 }; 2062 2063 } // namespace 2064 2065 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) { 2066 // Note that we are loading an identifier. 2067 Deserializing AnIdentifier(this); 2068 2069 unsigned PriorGeneration = 0; 2070 if (getContext().getLangOpts().Modules) 2071 PriorGeneration = IdentifierGeneration[&II]; 2072 2073 // If there is a global index, look there first to determine which modules 2074 // provably do not have any results for this identifier. 2075 GlobalModuleIndex::HitSet Hits; 2076 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 2077 if (!loadGlobalIndex()) { 2078 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) { 2079 HitsPtr = &Hits; 2080 } 2081 } 2082 2083 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration, 2084 NumIdentifierLookups, 2085 NumIdentifierLookupHits); 2086 ModuleMgr.visit(Visitor, HitsPtr); 2087 markIdentifierUpToDate(&II); 2088 } 2089 2090 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) { 2091 if (!II) 2092 return; 2093 2094 II->setOutOfDate(false); 2095 2096 // Update the generation for this identifier. 2097 if (getContext().getLangOpts().Modules) 2098 IdentifierGeneration[II] = getGeneration(); 2099 } 2100 2101 void ASTReader::resolvePendingMacro(IdentifierInfo *II, 2102 const PendingMacroInfo &PMInfo) { 2103 ModuleFile &M = *PMInfo.M; 2104 2105 BitstreamCursor &Cursor = M.MacroCursor; 2106 SavedStreamPosition SavedPosition(Cursor); 2107 if (llvm::Error Err = 2108 Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) { 2109 Error(std::move(Err)); 2110 return; 2111 } 2112 2113 struct ModuleMacroRecord { 2114 SubmoduleID SubModID; 2115 MacroInfo *MI; 2116 SmallVector<SubmoduleID, 8> Overrides; 2117 }; 2118 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros; 2119 2120 // We expect to see a sequence of PP_MODULE_MACRO records listing exported 2121 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete 2122 // macro histroy. 2123 RecordData Record; 2124 while (true) { 2125 Expected<llvm::BitstreamEntry> MaybeEntry = 2126 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 2127 if (!MaybeEntry) { 2128 Error(MaybeEntry.takeError()); 2129 return; 2130 } 2131 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2132 2133 if (Entry.Kind != llvm::BitstreamEntry::Record) { 2134 Error("malformed block record in AST file"); 2135 return; 2136 } 2137 2138 Record.clear(); 2139 Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record); 2140 if (!MaybePP) { 2141 Error(MaybePP.takeError()); 2142 return; 2143 } 2144 switch ((PreprocessorRecordTypes)MaybePP.get()) { 2145 case PP_MACRO_DIRECTIVE_HISTORY: 2146 break; 2147 2148 case PP_MODULE_MACRO: { 2149 ModuleMacros.push_back(ModuleMacroRecord()); 2150 auto &Info = ModuleMacros.back(); 2151 Info.SubModID = getGlobalSubmoduleID(M, Record[0]); 2152 Info.MI = getMacro(getGlobalMacroID(M, Record[1])); 2153 for (int I = 2, N = Record.size(); I != N; ++I) 2154 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I])); 2155 continue; 2156 } 2157 2158 default: 2159 Error("malformed block record in AST file"); 2160 return; 2161 } 2162 2163 // We found the macro directive history; that's the last record 2164 // for this macro. 2165 break; 2166 } 2167 2168 // Module macros are listed in reverse dependency order. 2169 { 2170 std::reverse(ModuleMacros.begin(), ModuleMacros.end()); 2171 llvm::SmallVector<ModuleMacro*, 8> Overrides; 2172 for (auto &MMR : ModuleMacros) { 2173 Overrides.clear(); 2174 for (unsigned ModID : MMR.Overrides) { 2175 Module *Mod = getSubmodule(ModID); 2176 auto *Macro = PP.getModuleMacro(Mod, II); 2177 assert(Macro && "missing definition for overridden macro"); 2178 Overrides.push_back(Macro); 2179 } 2180 2181 bool Inserted = false; 2182 Module *Owner = getSubmodule(MMR.SubModID); 2183 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted); 2184 } 2185 } 2186 2187 // Don't read the directive history for a module; we don't have anywhere 2188 // to put it. 2189 if (M.isModule()) 2190 return; 2191 2192 // Deserialize the macro directives history in reverse source-order. 2193 MacroDirective *Latest = nullptr, *Earliest = nullptr; 2194 unsigned Idx = 0, N = Record.size(); 2195 while (Idx < N) { 2196 MacroDirective *MD = nullptr; 2197 SourceLocation Loc = ReadSourceLocation(M, Record, Idx); 2198 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++]; 2199 switch (K) { 2200 case MacroDirective::MD_Define: { 2201 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++])); 2202 MD = PP.AllocateDefMacroDirective(MI, Loc); 2203 break; 2204 } 2205 case MacroDirective::MD_Undefine: 2206 MD = PP.AllocateUndefMacroDirective(Loc); 2207 break; 2208 case MacroDirective::MD_Visibility: 2209 bool isPublic = Record[Idx++]; 2210 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic); 2211 break; 2212 } 2213 2214 if (!Latest) 2215 Latest = MD; 2216 if (Earliest) 2217 Earliest->setPrevious(MD); 2218 Earliest = MD; 2219 } 2220 2221 if (Latest) 2222 PP.setLoadedMacroDirective(II, Earliest, Latest); 2223 } 2224 2225 ASTReader::InputFileInfo 2226 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) { 2227 // Go find this input file. 2228 BitstreamCursor &Cursor = F.InputFilesCursor; 2229 SavedStreamPosition SavedPosition(Cursor); 2230 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2231 // FIXME this drops errors on the floor. 2232 consumeError(std::move(Err)); 2233 } 2234 2235 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 2236 if (!MaybeCode) { 2237 // FIXME this drops errors on the floor. 2238 consumeError(MaybeCode.takeError()); 2239 } 2240 unsigned Code = MaybeCode.get(); 2241 RecordData Record; 2242 StringRef Blob; 2243 2244 if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob)) 2245 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE && 2246 "invalid record type for input file"); 2247 else { 2248 // FIXME this drops errors on the floor. 2249 consumeError(Maybe.takeError()); 2250 } 2251 2252 assert(Record[0] == ID && "Bogus stored ID or offset"); 2253 InputFileInfo R; 2254 R.StoredSize = static_cast<off_t>(Record[1]); 2255 R.StoredTime = static_cast<time_t>(Record[2]); 2256 R.Overridden = static_cast<bool>(Record[3]); 2257 R.Transient = static_cast<bool>(Record[4]); 2258 R.TopLevelModuleMap = static_cast<bool>(Record[5]); 2259 R.Filename = std::string(Blob); 2260 ResolveImportedPath(F, R.Filename); 2261 2262 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 2263 if (!MaybeEntry) // FIXME this drops errors on the floor. 2264 consumeError(MaybeEntry.takeError()); 2265 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2266 assert(Entry.Kind == llvm::BitstreamEntry::Record && 2267 "expected record type for input file hash"); 2268 2269 Record.clear(); 2270 if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record)) 2271 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH && 2272 "invalid record type for input file hash"); 2273 else { 2274 // FIXME this drops errors on the floor. 2275 consumeError(Maybe.takeError()); 2276 } 2277 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) | 2278 static_cast<uint64_t>(Record[0]); 2279 return R; 2280 } 2281 2282 static unsigned moduleKindForDiagnostic(ModuleKind Kind); 2283 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) { 2284 // If this ID is bogus, just return an empty input file. 2285 if (ID == 0 || ID > F.InputFilesLoaded.size()) 2286 return InputFile(); 2287 2288 // If we've already loaded this input file, return it. 2289 if (F.InputFilesLoaded[ID-1].getFile()) 2290 return F.InputFilesLoaded[ID-1]; 2291 2292 if (F.InputFilesLoaded[ID-1].isNotFound()) 2293 return InputFile(); 2294 2295 // Go find this input file. 2296 BitstreamCursor &Cursor = F.InputFilesCursor; 2297 SavedStreamPosition SavedPosition(Cursor); 2298 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2299 // FIXME this drops errors on the floor. 2300 consumeError(std::move(Err)); 2301 } 2302 2303 InputFileInfo FI = readInputFileInfo(F, ID); 2304 off_t StoredSize = FI.StoredSize; 2305 time_t StoredTime = FI.StoredTime; 2306 bool Overridden = FI.Overridden; 2307 bool Transient = FI.Transient; 2308 StringRef Filename = FI.Filename; 2309 uint64_t StoredContentHash = FI.ContentHash; 2310 2311 const FileEntry *File = nullptr; 2312 if (auto FE = FileMgr.getFile(Filename, /*OpenFile=*/false)) 2313 File = *FE; 2314 2315 // If we didn't find the file, resolve it relative to the 2316 // original directory from which this AST file was created. 2317 if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() && 2318 F.OriginalDir != F.BaseDirectory) { 2319 std::string Resolved = resolveFileRelativeToOriginalDir( 2320 std::string(Filename), F.OriginalDir, F.BaseDirectory); 2321 if (!Resolved.empty()) 2322 if (auto FE = FileMgr.getFile(Resolved)) 2323 File = *FE; 2324 } 2325 2326 // For an overridden file, create a virtual file with the stored 2327 // size/timestamp. 2328 if ((Overridden || Transient) && File == nullptr) 2329 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime); 2330 2331 if (File == nullptr) { 2332 if (Complain) { 2333 std::string ErrorStr = "could not find file '"; 2334 ErrorStr += Filename; 2335 ErrorStr += "' referenced by AST file '"; 2336 ErrorStr += F.FileName; 2337 ErrorStr += "'"; 2338 Error(ErrorStr); 2339 } 2340 // Record that we didn't find the file. 2341 F.InputFilesLoaded[ID-1] = InputFile::getNotFound(); 2342 return InputFile(); 2343 } 2344 2345 // Check if there was a request to override the contents of the file 2346 // that was part of the precompiled header. Overriding such a file 2347 // can lead to problems when lexing using the source locations from the 2348 // PCH. 2349 SourceManager &SM = getSourceManager(); 2350 // FIXME: Reject if the overrides are different. 2351 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) { 2352 if (Complain) 2353 Error(diag::err_fe_pch_file_overridden, Filename); 2354 2355 // After emitting the diagnostic, bypass the overriding file to recover 2356 // (this creates a separate FileEntry). 2357 File = SM.bypassFileContentsOverride(*File); 2358 if (!File) { 2359 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound(); 2360 return InputFile(); 2361 } 2362 } 2363 2364 enum ModificationType { 2365 Size, 2366 ModTime, 2367 Content, 2368 None, 2369 }; 2370 auto HasInputFileChanged = [&]() { 2371 if (StoredSize != File->getSize()) 2372 return ModificationType::Size; 2373 if (!DisableValidation && StoredTime && 2374 StoredTime != File->getModificationTime()) { 2375 // In case the modification time changes but not the content, 2376 // accept the cached file as legit. 2377 if (ValidateASTInputFilesContent && 2378 StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) { 2379 auto MemBuffOrError = FileMgr.getBufferForFile(File); 2380 if (!MemBuffOrError) { 2381 if (!Complain) 2382 return ModificationType::ModTime; 2383 std::string ErrorStr = "could not get buffer for file '"; 2384 ErrorStr += File->getName(); 2385 ErrorStr += "'"; 2386 Error(ErrorStr); 2387 return ModificationType::ModTime; 2388 } 2389 2390 auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer()); 2391 if (StoredContentHash == static_cast<uint64_t>(ContentHash)) 2392 return ModificationType::None; 2393 return ModificationType::Content; 2394 } 2395 return ModificationType::ModTime; 2396 } 2397 return ModificationType::None; 2398 }; 2399 2400 bool IsOutOfDate = false; 2401 auto FileChange = HasInputFileChanged(); 2402 // For an overridden file, there is nothing to validate. 2403 if (!Overridden && FileChange != ModificationType::None) { 2404 if (Complain) { 2405 // Build a list of the PCH imports that got us here (in reverse). 2406 SmallVector<ModuleFile *, 4> ImportStack(1, &F); 2407 while (!ImportStack.back()->ImportedBy.empty()) 2408 ImportStack.push_back(ImportStack.back()->ImportedBy[0]); 2409 2410 // The top-level PCH is stale. 2411 StringRef TopLevelPCHName(ImportStack.back()->FileName); 2412 unsigned DiagnosticKind = 2413 moduleKindForDiagnostic(ImportStack.back()->Kind); 2414 if (DiagnosticKind == 0) 2415 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName, 2416 (unsigned)FileChange); 2417 else if (DiagnosticKind == 1) 2418 Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName, 2419 (unsigned)FileChange); 2420 else 2421 Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName, 2422 (unsigned)FileChange); 2423 2424 // Print the import stack. 2425 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) { 2426 Diag(diag::note_pch_required_by) 2427 << Filename << ImportStack[0]->FileName; 2428 for (unsigned I = 1; I < ImportStack.size(); ++I) 2429 Diag(diag::note_pch_required_by) 2430 << ImportStack[I-1]->FileName << ImportStack[I]->FileName; 2431 } 2432 2433 if (!Diags.isDiagnosticInFlight()) 2434 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName; 2435 } 2436 2437 IsOutOfDate = true; 2438 } 2439 // FIXME: If the file is overridden and we've already opened it, 2440 // issue an error (or split it into a separate FileEntry). 2441 2442 InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate); 2443 2444 // Note that we've loaded this input file. 2445 F.InputFilesLoaded[ID-1] = IF; 2446 return IF; 2447 } 2448 2449 /// If we are loading a relocatable PCH or module file, and the filename 2450 /// is not an absolute path, add the system or module root to the beginning of 2451 /// the file name. 2452 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) { 2453 // Resolve relative to the base directory, if we have one. 2454 if (!M.BaseDirectory.empty()) 2455 return ResolveImportedPath(Filename, M.BaseDirectory); 2456 } 2457 2458 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) { 2459 if (Filename.empty() || llvm::sys::path::is_absolute(Filename)) 2460 return; 2461 2462 SmallString<128> Buffer; 2463 llvm::sys::path::append(Buffer, Prefix, Filename); 2464 Filename.assign(Buffer.begin(), Buffer.end()); 2465 } 2466 2467 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) { 2468 switch (ARR) { 2469 case ASTReader::Failure: return true; 2470 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing); 2471 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate); 2472 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch); 2473 case ASTReader::ConfigurationMismatch: 2474 return !(Caps & ASTReader::ARR_ConfigurationMismatch); 2475 case ASTReader::HadErrors: return true; 2476 case ASTReader::Success: return false; 2477 } 2478 2479 llvm_unreachable("unknown ASTReadResult"); 2480 } 2481 2482 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock( 2483 BitstreamCursor &Stream, unsigned ClientLoadCapabilities, 2484 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener, 2485 std::string &SuggestedPredefines) { 2486 if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) { 2487 // FIXME this drops errors on the floor. 2488 consumeError(std::move(Err)); 2489 return Failure; 2490 } 2491 2492 // Read all of the records in the options block. 2493 RecordData Record; 2494 ASTReadResult Result = Success; 2495 while (true) { 2496 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2497 if (!MaybeEntry) { 2498 // FIXME this drops errors on the floor. 2499 consumeError(MaybeEntry.takeError()); 2500 return Failure; 2501 } 2502 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2503 2504 switch (Entry.Kind) { 2505 case llvm::BitstreamEntry::Error: 2506 case llvm::BitstreamEntry::SubBlock: 2507 return Failure; 2508 2509 case llvm::BitstreamEntry::EndBlock: 2510 return Result; 2511 2512 case llvm::BitstreamEntry::Record: 2513 // The interesting case. 2514 break; 2515 } 2516 2517 // Read and process a record. 2518 Record.clear(); 2519 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record); 2520 if (!MaybeRecordType) { 2521 // FIXME this drops errors on the floor. 2522 consumeError(MaybeRecordType.takeError()); 2523 return Failure; 2524 } 2525 switch ((OptionsRecordTypes)MaybeRecordType.get()) { 2526 case LANGUAGE_OPTIONS: { 2527 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2528 if (ParseLanguageOptions(Record, Complain, Listener, 2529 AllowCompatibleConfigurationMismatch)) 2530 Result = ConfigurationMismatch; 2531 break; 2532 } 2533 2534 case TARGET_OPTIONS: { 2535 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2536 if (ParseTargetOptions(Record, Complain, Listener, 2537 AllowCompatibleConfigurationMismatch)) 2538 Result = ConfigurationMismatch; 2539 break; 2540 } 2541 2542 case FILE_SYSTEM_OPTIONS: { 2543 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2544 if (!AllowCompatibleConfigurationMismatch && 2545 ParseFileSystemOptions(Record, Complain, Listener)) 2546 Result = ConfigurationMismatch; 2547 break; 2548 } 2549 2550 case HEADER_SEARCH_OPTIONS: { 2551 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2552 if (!AllowCompatibleConfigurationMismatch && 2553 ParseHeaderSearchOptions(Record, Complain, Listener)) 2554 Result = ConfigurationMismatch; 2555 break; 2556 } 2557 2558 case PREPROCESSOR_OPTIONS: 2559 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2560 if (!AllowCompatibleConfigurationMismatch && 2561 ParsePreprocessorOptions(Record, Complain, Listener, 2562 SuggestedPredefines)) 2563 Result = ConfigurationMismatch; 2564 break; 2565 } 2566 } 2567 } 2568 2569 ASTReader::ASTReadResult 2570 ASTReader::ReadControlBlock(ModuleFile &F, 2571 SmallVectorImpl<ImportedModule> &Loaded, 2572 const ModuleFile *ImportedBy, 2573 unsigned ClientLoadCapabilities) { 2574 BitstreamCursor &Stream = F.Stream; 2575 2576 if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) { 2577 Error(std::move(Err)); 2578 return Failure; 2579 } 2580 2581 // Lambda to read the unhashed control block the first time it's called. 2582 // 2583 // For PCM files, the unhashed control block cannot be read until after the 2584 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still 2585 // need to look ahead before reading the IMPORTS record. For consistency, 2586 // this block is always read somehow (see BitstreamEntry::EndBlock). 2587 bool HasReadUnhashedControlBlock = false; 2588 auto readUnhashedControlBlockOnce = [&]() { 2589 if (!HasReadUnhashedControlBlock) { 2590 HasReadUnhashedControlBlock = true; 2591 if (ASTReadResult Result = 2592 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities)) 2593 return Result; 2594 } 2595 return Success; 2596 }; 2597 2598 // Read all of the records and blocks in the control block. 2599 RecordData Record; 2600 unsigned NumInputs = 0; 2601 unsigned NumUserInputs = 0; 2602 StringRef BaseDirectoryAsWritten; 2603 while (true) { 2604 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2605 if (!MaybeEntry) { 2606 Error(MaybeEntry.takeError()); 2607 return Failure; 2608 } 2609 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2610 2611 switch (Entry.Kind) { 2612 case llvm::BitstreamEntry::Error: 2613 Error("malformed block record in AST file"); 2614 return Failure; 2615 case llvm::BitstreamEntry::EndBlock: { 2616 // Validate the module before returning. This call catches an AST with 2617 // no module name and no imports. 2618 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2619 return Result; 2620 2621 // Validate input files. 2622 const HeaderSearchOptions &HSOpts = 2623 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 2624 2625 // All user input files reside at the index range [0, NumUserInputs), and 2626 // system input files reside at [NumUserInputs, NumInputs). For explicitly 2627 // loaded module files, ignore missing inputs. 2628 if (!DisableValidation && F.Kind != MK_ExplicitModule && 2629 F.Kind != MK_PrebuiltModule) { 2630 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 2631 2632 // If we are reading a module, we will create a verification timestamp, 2633 // so we verify all input files. Otherwise, verify only user input 2634 // files. 2635 2636 unsigned N = NumUserInputs; 2637 if (ValidateSystemInputs || 2638 (HSOpts.ModulesValidateOncePerBuildSession && 2639 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp && 2640 F.Kind == MK_ImplicitModule)) 2641 N = NumInputs; 2642 2643 for (unsigned I = 0; I < N; ++I) { 2644 InputFile IF = getInputFile(F, I+1, Complain); 2645 if (!IF.getFile() || IF.isOutOfDate()) 2646 return OutOfDate; 2647 } 2648 } 2649 2650 if (Listener) 2651 Listener->visitModuleFile(F.FileName, F.Kind); 2652 2653 if (Listener && Listener->needsInputFileVisitation()) { 2654 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs 2655 : NumUserInputs; 2656 for (unsigned I = 0; I < N; ++I) { 2657 bool IsSystem = I >= NumUserInputs; 2658 InputFileInfo FI = readInputFileInfo(F, I+1); 2659 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden, 2660 F.Kind == MK_ExplicitModule || 2661 F.Kind == MK_PrebuiltModule); 2662 } 2663 } 2664 2665 return Success; 2666 } 2667 2668 case llvm::BitstreamEntry::SubBlock: 2669 switch (Entry.ID) { 2670 case INPUT_FILES_BLOCK_ID: 2671 F.InputFilesCursor = Stream; 2672 if (llvm::Error Err = Stream.SkipBlock()) { 2673 Error(std::move(Err)); 2674 return Failure; 2675 } 2676 if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) { 2677 Error("malformed block record in AST file"); 2678 return Failure; 2679 } 2680 continue; 2681 2682 case OPTIONS_BLOCK_ID: 2683 // If we're reading the first module for this group, check its options 2684 // are compatible with ours. For modules it imports, no further checking 2685 // is required, because we checked them when we built it. 2686 if (Listener && !ImportedBy) { 2687 // Should we allow the configuration of the module file to differ from 2688 // the configuration of the current translation unit in a compatible 2689 // way? 2690 // 2691 // FIXME: Allow this for files explicitly specified with -include-pch. 2692 bool AllowCompatibleConfigurationMismatch = 2693 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 2694 2695 ASTReadResult Result = 2696 ReadOptionsBlock(Stream, ClientLoadCapabilities, 2697 AllowCompatibleConfigurationMismatch, *Listener, 2698 SuggestedPredefines); 2699 if (Result == Failure) { 2700 Error("malformed block record in AST file"); 2701 return Result; 2702 } 2703 2704 if (DisableValidation || 2705 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 2706 Result = Success; 2707 2708 // If we can't load the module, exit early since we likely 2709 // will rebuild the module anyway. The stream may be in the 2710 // middle of a block. 2711 if (Result != Success) 2712 return Result; 2713 } else if (llvm::Error Err = Stream.SkipBlock()) { 2714 Error(std::move(Err)); 2715 return Failure; 2716 } 2717 continue; 2718 2719 default: 2720 if (llvm::Error Err = Stream.SkipBlock()) { 2721 Error(std::move(Err)); 2722 return Failure; 2723 } 2724 continue; 2725 } 2726 2727 case llvm::BitstreamEntry::Record: 2728 // The interesting case. 2729 break; 2730 } 2731 2732 // Read and process a record. 2733 Record.clear(); 2734 StringRef Blob; 2735 Expected<unsigned> MaybeRecordType = 2736 Stream.readRecord(Entry.ID, Record, &Blob); 2737 if (!MaybeRecordType) { 2738 Error(MaybeRecordType.takeError()); 2739 return Failure; 2740 } 2741 switch ((ControlRecordTypes)MaybeRecordType.get()) { 2742 case METADATA: { 2743 if (Record[0] != VERSION_MAJOR && !DisableValidation) { 2744 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2745 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old 2746 : diag::err_pch_version_too_new); 2747 return VersionMismatch; 2748 } 2749 2750 bool hasErrors = Record[6]; 2751 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) { 2752 Diag(diag::err_pch_with_compiler_errors); 2753 return HadErrors; 2754 } 2755 if (hasErrors) { 2756 Diags.ErrorOccurred = true; 2757 Diags.UncompilableErrorOccurred = true; 2758 Diags.UnrecoverableErrorOccurred = true; 2759 } 2760 2761 F.RelocatablePCH = Record[4]; 2762 // Relative paths in a relocatable PCH are relative to our sysroot. 2763 if (F.RelocatablePCH) 2764 F.BaseDirectory = isysroot.empty() ? "/" : isysroot; 2765 2766 F.HasTimestamps = Record[5]; 2767 2768 const std::string &CurBranch = getClangFullRepositoryVersion(); 2769 StringRef ASTBranch = Blob; 2770 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) { 2771 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2772 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch; 2773 return VersionMismatch; 2774 } 2775 break; 2776 } 2777 2778 case IMPORTS: { 2779 // Validate the AST before processing any imports (otherwise, untangling 2780 // them can be error-prone and expensive). A module will have a name and 2781 // will already have been validated, but this catches the PCH case. 2782 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2783 return Result; 2784 2785 // Load each of the imported PCH files. 2786 unsigned Idx = 0, N = Record.size(); 2787 while (Idx < N) { 2788 // Read information about the AST file. 2789 ModuleKind ImportedKind = (ModuleKind)Record[Idx++]; 2790 // The import location will be the local one for now; we will adjust 2791 // all import locations of module imports after the global source 2792 // location info are setup, in ReadAST. 2793 SourceLocation ImportLoc = 2794 ReadUntranslatedSourceLocation(Record[Idx++]); 2795 off_t StoredSize = (off_t)Record[Idx++]; 2796 time_t StoredModTime = (time_t)Record[Idx++]; 2797 auto FirstSignatureByte = Record.begin() + Idx; 2798 ASTFileSignature StoredSignature = ASTFileSignature::create( 2799 FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size); 2800 Idx += ASTFileSignature::size; 2801 2802 std::string ImportedName = ReadString(Record, Idx); 2803 std::string ImportedFile; 2804 2805 // For prebuilt and explicit modules first consult the file map for 2806 // an override. Note that here we don't search prebuilt module 2807 // directories, only the explicit name to file mappings. Also, we will 2808 // still verify the size/signature making sure it is essentially the 2809 // same file but perhaps in a different location. 2810 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule) 2811 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName( 2812 ImportedName, /*FileMapOnly*/ true); 2813 2814 if (ImportedFile.empty()) 2815 // Use BaseDirectoryAsWritten to ensure we use the same path in the 2816 // ModuleCache as when writing. 2817 ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx); 2818 else 2819 SkipPath(Record, Idx); 2820 2821 // If our client can't cope with us being out of date, we can't cope with 2822 // our dependency being missing. 2823 unsigned Capabilities = ClientLoadCapabilities; 2824 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 2825 Capabilities &= ~ARR_Missing; 2826 2827 // Load the AST file. 2828 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, 2829 Loaded, StoredSize, StoredModTime, 2830 StoredSignature, Capabilities); 2831 2832 // If we diagnosed a problem, produce a backtrace. 2833 if (isDiagnosedResult(Result, Capabilities)) 2834 Diag(diag::note_module_file_imported_by) 2835 << F.FileName << !F.ModuleName.empty() << F.ModuleName; 2836 2837 switch (Result) { 2838 case Failure: return Failure; 2839 // If we have to ignore the dependency, we'll have to ignore this too. 2840 case Missing: 2841 case OutOfDate: return OutOfDate; 2842 case VersionMismatch: return VersionMismatch; 2843 case ConfigurationMismatch: return ConfigurationMismatch; 2844 case HadErrors: return HadErrors; 2845 case Success: break; 2846 } 2847 } 2848 break; 2849 } 2850 2851 case ORIGINAL_FILE: 2852 F.OriginalSourceFileID = FileID::get(Record[0]); 2853 F.ActualOriginalSourceFileName = std::string(Blob); 2854 F.OriginalSourceFileName = F.ActualOriginalSourceFileName; 2855 ResolveImportedPath(F, F.OriginalSourceFileName); 2856 break; 2857 2858 case ORIGINAL_FILE_ID: 2859 F.OriginalSourceFileID = FileID::get(Record[0]); 2860 break; 2861 2862 case ORIGINAL_PCH_DIR: 2863 F.OriginalDir = std::string(Blob); 2864 break; 2865 2866 case MODULE_NAME: 2867 F.ModuleName = std::string(Blob); 2868 Diag(diag::remark_module_import) 2869 << F.ModuleName << F.FileName << (ImportedBy ? true : false) 2870 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 2871 if (Listener) 2872 Listener->ReadModuleName(F.ModuleName); 2873 2874 // Validate the AST as soon as we have a name so we can exit early on 2875 // failure. 2876 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2877 return Result; 2878 2879 break; 2880 2881 case MODULE_DIRECTORY: { 2882 // Save the BaseDirectory as written in the PCM for computing the module 2883 // filename for the ModuleCache. 2884 BaseDirectoryAsWritten = Blob; 2885 assert(!F.ModuleName.empty() && 2886 "MODULE_DIRECTORY found before MODULE_NAME"); 2887 // If we've already loaded a module map file covering this module, we may 2888 // have a better path for it (relative to the current build). 2889 Module *M = PP.getHeaderSearchInfo().lookupModule( 2890 F.ModuleName, /*AllowSearch*/ true, 2891 /*AllowExtraModuleMapSearch*/ true); 2892 if (M && M->Directory) { 2893 // If we're implicitly loading a module, the base directory can't 2894 // change between the build and use. 2895 // Don't emit module relocation error if we have -fno-validate-pch 2896 if (!PP.getPreprocessorOpts().DisablePCHValidation && 2897 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) { 2898 auto BuildDir = PP.getFileManager().getDirectory(Blob); 2899 if (!BuildDir || *BuildDir != M->Directory) { 2900 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 2901 Diag(diag::err_imported_module_relocated) 2902 << F.ModuleName << Blob << M->Directory->getName(); 2903 return OutOfDate; 2904 } 2905 } 2906 F.BaseDirectory = std::string(M->Directory->getName()); 2907 } else { 2908 F.BaseDirectory = std::string(Blob); 2909 } 2910 break; 2911 } 2912 2913 case MODULE_MAP_FILE: 2914 if (ASTReadResult Result = 2915 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities)) 2916 return Result; 2917 break; 2918 2919 case INPUT_FILE_OFFSETS: 2920 NumInputs = Record[0]; 2921 NumUserInputs = Record[1]; 2922 F.InputFileOffsets = 2923 (const llvm::support::unaligned_uint64_t *)Blob.data(); 2924 F.InputFilesLoaded.resize(NumInputs); 2925 F.NumUserInputFiles = NumUserInputs; 2926 break; 2927 } 2928 } 2929 } 2930 2931 ASTReader::ASTReadResult 2932 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) { 2933 BitstreamCursor &Stream = F.Stream; 2934 2935 if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) { 2936 Error(std::move(Err)); 2937 return Failure; 2938 } 2939 F.ASTBlockStartOffset = Stream.GetCurrentBitNo(); 2940 2941 // Read all of the records and blocks for the AST file. 2942 RecordData Record; 2943 while (true) { 2944 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2945 if (!MaybeEntry) { 2946 Error(MaybeEntry.takeError()); 2947 return Failure; 2948 } 2949 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2950 2951 switch (Entry.Kind) { 2952 case llvm::BitstreamEntry::Error: 2953 Error("error at end of module block in AST file"); 2954 return Failure; 2955 case llvm::BitstreamEntry::EndBlock: 2956 // Outside of C++, we do not store a lookup map for the translation unit. 2957 // Instead, mark it as needing a lookup map to be built if this module 2958 // contains any declarations lexically within it (which it always does!). 2959 // This usually has no cost, since we very rarely need the lookup map for 2960 // the translation unit outside C++. 2961 if (ASTContext *Ctx = ContextObj) { 2962 DeclContext *DC = Ctx->getTranslationUnitDecl(); 2963 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus) 2964 DC->setMustBuildLookupTable(); 2965 } 2966 2967 return Success; 2968 case llvm::BitstreamEntry::SubBlock: 2969 switch (Entry.ID) { 2970 case DECLTYPES_BLOCK_ID: 2971 // We lazily load the decls block, but we want to set up the 2972 // DeclsCursor cursor to point into it. Clone our current bitcode 2973 // cursor to it, enter the block and read the abbrevs in that block. 2974 // With the main cursor, we just skip over it. 2975 F.DeclsCursor = Stream; 2976 if (llvm::Error Err = Stream.SkipBlock()) { 2977 Error(std::move(Err)); 2978 return Failure; 2979 } 2980 if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID, 2981 &F.DeclsBlockStartOffset)) { 2982 Error("malformed block record in AST file"); 2983 return Failure; 2984 } 2985 break; 2986 2987 case PREPROCESSOR_BLOCK_ID: 2988 F.MacroCursor = Stream; 2989 if (!PP.getExternalSource()) 2990 PP.setExternalSource(this); 2991 2992 if (llvm::Error Err = Stream.SkipBlock()) { 2993 Error(std::move(Err)); 2994 return Failure; 2995 } 2996 if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) { 2997 Error("malformed block record in AST file"); 2998 return Failure; 2999 } 3000 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo(); 3001 break; 3002 3003 case PREPROCESSOR_DETAIL_BLOCK_ID: 3004 F.PreprocessorDetailCursor = Stream; 3005 3006 if (llvm::Error Err = Stream.SkipBlock()) { 3007 Error(std::move(Err)); 3008 return Failure; 3009 } 3010 if (ReadBlockAbbrevs(F.PreprocessorDetailCursor, 3011 PREPROCESSOR_DETAIL_BLOCK_ID)) { 3012 Error("malformed preprocessor detail record in AST file"); 3013 return Failure; 3014 } 3015 F.PreprocessorDetailStartOffset 3016 = F.PreprocessorDetailCursor.GetCurrentBitNo(); 3017 3018 if (!PP.getPreprocessingRecord()) 3019 PP.createPreprocessingRecord(); 3020 if (!PP.getPreprocessingRecord()->getExternalSource()) 3021 PP.getPreprocessingRecord()->SetExternalSource(*this); 3022 break; 3023 3024 case SOURCE_MANAGER_BLOCK_ID: 3025 if (ReadSourceManagerBlock(F)) 3026 return Failure; 3027 break; 3028 3029 case SUBMODULE_BLOCK_ID: 3030 if (ASTReadResult Result = 3031 ReadSubmoduleBlock(F, ClientLoadCapabilities)) 3032 return Result; 3033 break; 3034 3035 case COMMENTS_BLOCK_ID: { 3036 BitstreamCursor C = Stream; 3037 3038 if (llvm::Error Err = Stream.SkipBlock()) { 3039 Error(std::move(Err)); 3040 return Failure; 3041 } 3042 if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) { 3043 Error("malformed comments block in AST file"); 3044 return Failure; 3045 } 3046 CommentsCursors.push_back(std::make_pair(C, &F)); 3047 break; 3048 } 3049 3050 default: 3051 if (llvm::Error Err = Stream.SkipBlock()) { 3052 Error(std::move(Err)); 3053 return Failure; 3054 } 3055 break; 3056 } 3057 continue; 3058 3059 case llvm::BitstreamEntry::Record: 3060 // The interesting case. 3061 break; 3062 } 3063 3064 // Read and process a record. 3065 Record.clear(); 3066 StringRef Blob; 3067 Expected<unsigned> MaybeRecordType = 3068 Stream.readRecord(Entry.ID, Record, &Blob); 3069 if (!MaybeRecordType) { 3070 Error(MaybeRecordType.takeError()); 3071 return Failure; 3072 } 3073 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get(); 3074 3075 // If we're not loading an AST context, we don't care about most records. 3076 if (!ContextObj) { 3077 switch (RecordType) { 3078 case IDENTIFIER_TABLE: 3079 case IDENTIFIER_OFFSET: 3080 case INTERESTING_IDENTIFIERS: 3081 case STATISTICS: 3082 case PP_CONDITIONAL_STACK: 3083 case PP_COUNTER_VALUE: 3084 case SOURCE_LOCATION_OFFSETS: 3085 case MODULE_OFFSET_MAP: 3086 case SOURCE_MANAGER_LINE_TABLE: 3087 case SOURCE_LOCATION_PRELOADS: 3088 case PPD_ENTITIES_OFFSETS: 3089 case HEADER_SEARCH_TABLE: 3090 case IMPORTED_MODULES: 3091 case MACRO_OFFSET: 3092 break; 3093 default: 3094 continue; 3095 } 3096 } 3097 3098 switch (RecordType) { 3099 default: // Default behavior: ignore. 3100 break; 3101 3102 case TYPE_OFFSET: { 3103 if (F.LocalNumTypes != 0) { 3104 Error("duplicate TYPE_OFFSET record in AST file"); 3105 return Failure; 3106 } 3107 F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data()); 3108 F.LocalNumTypes = Record[0]; 3109 unsigned LocalBaseTypeIndex = Record[1]; 3110 F.BaseTypeIndex = getTotalNumTypes(); 3111 3112 if (F.LocalNumTypes > 0) { 3113 // Introduce the global -> local mapping for types within this module. 3114 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F)); 3115 3116 // Introduce the local -> global mapping for types within this module. 3117 F.TypeRemap.insertOrReplace( 3118 std::make_pair(LocalBaseTypeIndex, 3119 F.BaseTypeIndex - LocalBaseTypeIndex)); 3120 3121 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes); 3122 } 3123 break; 3124 } 3125 3126 case DECL_OFFSET: { 3127 if (F.LocalNumDecls != 0) { 3128 Error("duplicate DECL_OFFSET record in AST file"); 3129 return Failure; 3130 } 3131 F.DeclOffsets = (const DeclOffset *)Blob.data(); 3132 F.LocalNumDecls = Record[0]; 3133 unsigned LocalBaseDeclID = Record[1]; 3134 F.BaseDeclID = getTotalNumDecls(); 3135 3136 if (F.LocalNumDecls > 0) { 3137 // Introduce the global -> local mapping for declarations within this 3138 // module. 3139 GlobalDeclMap.insert( 3140 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F)); 3141 3142 // Introduce the local -> global mapping for declarations within this 3143 // module. 3144 F.DeclRemap.insertOrReplace( 3145 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID)); 3146 3147 // Introduce the global -> local mapping for declarations within this 3148 // module. 3149 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID; 3150 3151 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls); 3152 } 3153 break; 3154 } 3155 3156 case TU_UPDATE_LEXICAL: { 3157 DeclContext *TU = ContextObj->getTranslationUnitDecl(); 3158 LexicalContents Contents( 3159 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 3160 Blob.data()), 3161 static_cast<unsigned int>(Blob.size() / 4)); 3162 TULexicalDecls.push_back(std::make_pair(&F, Contents)); 3163 TU->setHasExternalLexicalStorage(true); 3164 break; 3165 } 3166 3167 case UPDATE_VISIBLE: { 3168 unsigned Idx = 0; 3169 serialization::DeclID ID = ReadDeclID(F, Record, Idx); 3170 auto *Data = (const unsigned char*)Blob.data(); 3171 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data}); 3172 // If we've already loaded the decl, perform the updates when we finish 3173 // loading this block. 3174 if (Decl *D = GetExistingDecl(ID)) 3175 PendingUpdateRecords.push_back( 3176 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3177 break; 3178 } 3179 3180 case IDENTIFIER_TABLE: 3181 F.IdentifierTableData = Blob.data(); 3182 if (Record[0]) { 3183 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create( 3184 (const unsigned char *)F.IdentifierTableData + Record[0], 3185 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t), 3186 (const unsigned char *)F.IdentifierTableData, 3187 ASTIdentifierLookupTrait(*this, F)); 3188 3189 PP.getIdentifierTable().setExternalIdentifierLookup(this); 3190 } 3191 break; 3192 3193 case IDENTIFIER_OFFSET: { 3194 if (F.LocalNumIdentifiers != 0) { 3195 Error("duplicate IDENTIFIER_OFFSET record in AST file"); 3196 return Failure; 3197 } 3198 F.IdentifierOffsets = (const uint32_t *)Blob.data(); 3199 F.LocalNumIdentifiers = Record[0]; 3200 unsigned LocalBaseIdentifierID = Record[1]; 3201 F.BaseIdentifierID = getTotalNumIdentifiers(); 3202 3203 if (F.LocalNumIdentifiers > 0) { 3204 // Introduce the global -> local mapping for identifiers within this 3205 // module. 3206 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1, 3207 &F)); 3208 3209 // Introduce the local -> global mapping for identifiers within this 3210 // module. 3211 F.IdentifierRemap.insertOrReplace( 3212 std::make_pair(LocalBaseIdentifierID, 3213 F.BaseIdentifierID - LocalBaseIdentifierID)); 3214 3215 IdentifiersLoaded.resize(IdentifiersLoaded.size() 3216 + F.LocalNumIdentifiers); 3217 } 3218 break; 3219 } 3220 3221 case INTERESTING_IDENTIFIERS: 3222 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end()); 3223 break; 3224 3225 case EAGERLY_DESERIALIZED_DECLS: 3226 // FIXME: Skip reading this record if our ASTConsumer doesn't care 3227 // about "interesting" decls (for instance, if we're building a module). 3228 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3229 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3230 break; 3231 3232 case MODULAR_CODEGEN_DECLS: 3233 // FIXME: Skip reading this record if our ASTConsumer doesn't care about 3234 // them (ie: if we're not codegenerating this module). 3235 if (F.Kind == MK_MainFile || 3236 getContext().getLangOpts().BuildingPCHWithObjectFile) 3237 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3238 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3239 break; 3240 3241 case SPECIAL_TYPES: 3242 if (SpecialTypes.empty()) { 3243 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3244 SpecialTypes.push_back(getGlobalTypeID(F, Record[I])); 3245 break; 3246 } 3247 3248 if (SpecialTypes.size() != Record.size()) { 3249 Error("invalid special-types record"); 3250 return Failure; 3251 } 3252 3253 for (unsigned I = 0, N = Record.size(); I != N; ++I) { 3254 serialization::TypeID ID = getGlobalTypeID(F, Record[I]); 3255 if (!SpecialTypes[I]) 3256 SpecialTypes[I] = ID; 3257 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate 3258 // merge step? 3259 } 3260 break; 3261 3262 case STATISTICS: 3263 TotalNumStatements += Record[0]; 3264 TotalNumMacros += Record[1]; 3265 TotalLexicalDeclContexts += Record[2]; 3266 TotalVisibleDeclContexts += Record[3]; 3267 break; 3268 3269 case UNUSED_FILESCOPED_DECLS: 3270 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3271 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I])); 3272 break; 3273 3274 case DELEGATING_CTORS: 3275 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3276 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I])); 3277 break; 3278 3279 case WEAK_UNDECLARED_IDENTIFIERS: 3280 if (Record.size() % 4 != 0) { 3281 Error("invalid weak identifiers record"); 3282 return Failure; 3283 } 3284 3285 // FIXME: Ignore weak undeclared identifiers from non-original PCH 3286 // files. This isn't the way to do it :) 3287 WeakUndeclaredIdentifiers.clear(); 3288 3289 // Translate the weak, undeclared identifiers into global IDs. 3290 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) { 3291 WeakUndeclaredIdentifiers.push_back( 3292 getGlobalIdentifierID(F, Record[I++])); 3293 WeakUndeclaredIdentifiers.push_back( 3294 getGlobalIdentifierID(F, Record[I++])); 3295 WeakUndeclaredIdentifiers.push_back( 3296 ReadSourceLocation(F, Record, I).getRawEncoding()); 3297 WeakUndeclaredIdentifiers.push_back(Record[I++]); 3298 } 3299 break; 3300 3301 case SELECTOR_OFFSETS: { 3302 F.SelectorOffsets = (const uint32_t *)Blob.data(); 3303 F.LocalNumSelectors = Record[0]; 3304 unsigned LocalBaseSelectorID = Record[1]; 3305 F.BaseSelectorID = getTotalNumSelectors(); 3306 3307 if (F.LocalNumSelectors > 0) { 3308 // Introduce the global -> local mapping for selectors within this 3309 // module. 3310 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F)); 3311 3312 // Introduce the local -> global mapping for selectors within this 3313 // module. 3314 F.SelectorRemap.insertOrReplace( 3315 std::make_pair(LocalBaseSelectorID, 3316 F.BaseSelectorID - LocalBaseSelectorID)); 3317 3318 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors); 3319 } 3320 break; 3321 } 3322 3323 case METHOD_POOL: 3324 F.SelectorLookupTableData = (const unsigned char *)Blob.data(); 3325 if (Record[0]) 3326 F.SelectorLookupTable 3327 = ASTSelectorLookupTable::Create( 3328 F.SelectorLookupTableData + Record[0], 3329 F.SelectorLookupTableData, 3330 ASTSelectorLookupTrait(*this, F)); 3331 TotalNumMethodPoolEntries += Record[1]; 3332 break; 3333 3334 case REFERENCED_SELECTOR_POOL: 3335 if (!Record.empty()) { 3336 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) { 3337 ReferencedSelectorsData.push_back(getGlobalSelectorID(F, 3338 Record[Idx++])); 3339 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx). 3340 getRawEncoding()); 3341 } 3342 } 3343 break; 3344 3345 case PP_CONDITIONAL_STACK: 3346 if (!Record.empty()) { 3347 unsigned Idx = 0, End = Record.size() - 1; 3348 bool ReachedEOFWhileSkipping = Record[Idx++]; 3349 llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo; 3350 if (ReachedEOFWhileSkipping) { 3351 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx); 3352 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx); 3353 bool FoundNonSkipPortion = Record[Idx++]; 3354 bool FoundElse = Record[Idx++]; 3355 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx); 3356 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion, 3357 FoundElse, ElseLoc); 3358 } 3359 SmallVector<PPConditionalInfo, 4> ConditionalStack; 3360 while (Idx < End) { 3361 auto Loc = ReadSourceLocation(F, Record, Idx); 3362 bool WasSkipping = Record[Idx++]; 3363 bool FoundNonSkip = Record[Idx++]; 3364 bool FoundElse = Record[Idx++]; 3365 ConditionalStack.push_back( 3366 {Loc, WasSkipping, FoundNonSkip, FoundElse}); 3367 } 3368 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo); 3369 } 3370 break; 3371 3372 case PP_COUNTER_VALUE: 3373 if (!Record.empty() && Listener) 3374 Listener->ReadCounter(F, Record[0]); 3375 break; 3376 3377 case FILE_SORTED_DECLS: 3378 F.FileSortedDecls = (const DeclID *)Blob.data(); 3379 F.NumFileSortedDecls = Record[0]; 3380 break; 3381 3382 case SOURCE_LOCATION_OFFSETS: { 3383 F.SLocEntryOffsets = (const uint32_t *)Blob.data(); 3384 F.LocalNumSLocEntries = Record[0]; 3385 unsigned SLocSpaceSize = Record[1]; 3386 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset; 3387 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) = 3388 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, 3389 SLocSpaceSize); 3390 if (!F.SLocEntryBaseID) { 3391 Error("ran out of source locations"); 3392 break; 3393 } 3394 // Make our entry in the range map. BaseID is negative and growing, so 3395 // we invert it. Because we invert it, though, we need the other end of 3396 // the range. 3397 unsigned RangeStart = 3398 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1; 3399 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F)); 3400 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset); 3401 3402 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing. 3403 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0); 3404 GlobalSLocOffsetMap.insert( 3405 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset 3406 - SLocSpaceSize,&F)); 3407 3408 // Initialize the remapping table. 3409 // Invalid stays invalid. 3410 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0)); 3411 // This module. Base was 2 when being compiled. 3412 F.SLocRemap.insertOrReplace(std::make_pair(2U, 3413 static_cast<int>(F.SLocEntryBaseOffset - 2))); 3414 3415 TotalNumSLocEntries += F.LocalNumSLocEntries; 3416 break; 3417 } 3418 3419 case MODULE_OFFSET_MAP: 3420 F.ModuleOffsetMap = Blob; 3421 break; 3422 3423 case SOURCE_MANAGER_LINE_TABLE: 3424 if (ParseLineTable(F, Record)) { 3425 Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file"); 3426 return Failure; 3427 } 3428 break; 3429 3430 case SOURCE_LOCATION_PRELOADS: { 3431 // Need to transform from the local view (1-based IDs) to the global view, 3432 // which is based off F.SLocEntryBaseID. 3433 if (!F.PreloadSLocEntries.empty()) { 3434 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file"); 3435 return Failure; 3436 } 3437 3438 F.PreloadSLocEntries.swap(Record); 3439 break; 3440 } 3441 3442 case EXT_VECTOR_DECLS: 3443 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3444 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I])); 3445 break; 3446 3447 case VTABLE_USES: 3448 if (Record.size() % 3 != 0) { 3449 Error("Invalid VTABLE_USES record"); 3450 return Failure; 3451 } 3452 3453 // Later tables overwrite earlier ones. 3454 // FIXME: Modules will have some trouble with this. This is clearly not 3455 // the right way to do this. 3456 VTableUses.clear(); 3457 3458 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) { 3459 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++])); 3460 VTableUses.push_back( 3461 ReadSourceLocation(F, Record, Idx).getRawEncoding()); 3462 VTableUses.push_back(Record[Idx++]); 3463 } 3464 break; 3465 3466 case PENDING_IMPLICIT_INSTANTIATIONS: 3467 if (PendingInstantiations.size() % 2 != 0) { 3468 Error("Invalid existing PendingInstantiations"); 3469 return Failure; 3470 } 3471 3472 if (Record.size() % 2 != 0) { 3473 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block"); 3474 return Failure; 3475 } 3476 3477 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3478 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++])); 3479 PendingInstantiations.push_back( 3480 ReadSourceLocation(F, Record, I).getRawEncoding()); 3481 } 3482 break; 3483 3484 case SEMA_DECL_REFS: 3485 if (Record.size() != 3) { 3486 Error("Invalid SEMA_DECL_REFS block"); 3487 return Failure; 3488 } 3489 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3490 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3491 break; 3492 3493 case PPD_ENTITIES_OFFSETS: { 3494 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data(); 3495 assert(Blob.size() % sizeof(PPEntityOffset) == 0); 3496 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset); 3497 3498 unsigned LocalBasePreprocessedEntityID = Record[0]; 3499 3500 unsigned StartingID; 3501 if (!PP.getPreprocessingRecord()) 3502 PP.createPreprocessingRecord(); 3503 if (!PP.getPreprocessingRecord()->getExternalSource()) 3504 PP.getPreprocessingRecord()->SetExternalSource(*this); 3505 StartingID 3506 = PP.getPreprocessingRecord() 3507 ->allocateLoadedEntities(F.NumPreprocessedEntities); 3508 F.BasePreprocessedEntityID = StartingID; 3509 3510 if (F.NumPreprocessedEntities > 0) { 3511 // Introduce the global -> local mapping for preprocessed entities in 3512 // this module. 3513 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F)); 3514 3515 // Introduce the local -> global mapping for preprocessed entities in 3516 // this module. 3517 F.PreprocessedEntityRemap.insertOrReplace( 3518 std::make_pair(LocalBasePreprocessedEntityID, 3519 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID)); 3520 } 3521 3522 break; 3523 } 3524 3525 case PPD_SKIPPED_RANGES: { 3526 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data(); 3527 assert(Blob.size() % sizeof(PPSkippedRange) == 0); 3528 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange); 3529 3530 if (!PP.getPreprocessingRecord()) 3531 PP.createPreprocessingRecord(); 3532 if (!PP.getPreprocessingRecord()->getExternalSource()) 3533 PP.getPreprocessingRecord()->SetExternalSource(*this); 3534 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord() 3535 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges); 3536 3537 if (F.NumPreprocessedSkippedRanges > 0) 3538 GlobalSkippedRangeMap.insert( 3539 std::make_pair(F.BasePreprocessedSkippedRangeID, &F)); 3540 break; 3541 } 3542 3543 case DECL_UPDATE_OFFSETS: 3544 if (Record.size() % 2 != 0) { 3545 Error("invalid DECL_UPDATE_OFFSETS block in AST file"); 3546 return Failure; 3547 } 3548 for (unsigned I = 0, N = Record.size(); I != N; I += 2) { 3549 GlobalDeclID ID = getGlobalDeclID(F, Record[I]); 3550 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1])); 3551 3552 // If we've already loaded the decl, perform the updates when we finish 3553 // loading this block. 3554 if (Decl *D = GetExistingDecl(ID)) 3555 PendingUpdateRecords.push_back( 3556 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3557 } 3558 break; 3559 3560 case OBJC_CATEGORIES_MAP: 3561 if (F.LocalNumObjCCategoriesInMap != 0) { 3562 Error("duplicate OBJC_CATEGORIES_MAP record in AST file"); 3563 return Failure; 3564 } 3565 3566 F.LocalNumObjCCategoriesInMap = Record[0]; 3567 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data(); 3568 break; 3569 3570 case OBJC_CATEGORIES: 3571 F.ObjCCategories.swap(Record); 3572 break; 3573 3574 case CUDA_SPECIAL_DECL_REFS: 3575 // Later tables overwrite earlier ones. 3576 // FIXME: Modules will have trouble with this. 3577 CUDASpecialDeclRefs.clear(); 3578 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3579 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3580 break; 3581 3582 case HEADER_SEARCH_TABLE: 3583 F.HeaderFileInfoTableData = Blob.data(); 3584 F.LocalNumHeaderFileInfos = Record[1]; 3585 if (Record[0]) { 3586 F.HeaderFileInfoTable 3587 = HeaderFileInfoLookupTable::Create( 3588 (const unsigned char *)F.HeaderFileInfoTableData + Record[0], 3589 (const unsigned char *)F.HeaderFileInfoTableData, 3590 HeaderFileInfoTrait(*this, F, 3591 &PP.getHeaderSearchInfo(), 3592 Blob.data() + Record[2])); 3593 3594 PP.getHeaderSearchInfo().SetExternalSource(this); 3595 if (!PP.getHeaderSearchInfo().getExternalLookup()) 3596 PP.getHeaderSearchInfo().SetExternalLookup(this); 3597 } 3598 break; 3599 3600 case FP_PRAGMA_OPTIONS: 3601 // Later tables overwrite earlier ones. 3602 FPPragmaOptions.swap(Record); 3603 break; 3604 3605 case OPENCL_EXTENSIONS: 3606 for (unsigned I = 0, E = Record.size(); I != E; ) { 3607 auto Name = ReadString(Record, I); 3608 auto &Opt = OpenCLExtensions.OptMap[Name]; 3609 Opt.Supported = Record[I++] != 0; 3610 Opt.Enabled = Record[I++] != 0; 3611 Opt.Avail = Record[I++]; 3612 Opt.Core = Record[I++]; 3613 } 3614 break; 3615 3616 case OPENCL_EXTENSION_TYPES: 3617 for (unsigned I = 0, E = Record.size(); I != E;) { 3618 auto TypeID = static_cast<::TypeID>(Record[I++]); 3619 auto *Type = GetType(TypeID).getTypePtr(); 3620 auto NumExt = static_cast<unsigned>(Record[I++]); 3621 for (unsigned II = 0; II != NumExt; ++II) { 3622 auto Ext = ReadString(Record, I); 3623 OpenCLTypeExtMap[Type].insert(Ext); 3624 } 3625 } 3626 break; 3627 3628 case OPENCL_EXTENSION_DECLS: 3629 for (unsigned I = 0, E = Record.size(); I != E;) { 3630 auto DeclID = static_cast<::DeclID>(Record[I++]); 3631 auto *Decl = GetDecl(DeclID); 3632 auto NumExt = static_cast<unsigned>(Record[I++]); 3633 for (unsigned II = 0; II != NumExt; ++II) { 3634 auto Ext = ReadString(Record, I); 3635 OpenCLDeclExtMap[Decl].insert(Ext); 3636 } 3637 } 3638 break; 3639 3640 case TENTATIVE_DEFINITIONS: 3641 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3642 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I])); 3643 break; 3644 3645 case KNOWN_NAMESPACES: 3646 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3647 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I])); 3648 break; 3649 3650 case UNDEFINED_BUT_USED: 3651 if (UndefinedButUsed.size() % 2 != 0) { 3652 Error("Invalid existing UndefinedButUsed"); 3653 return Failure; 3654 } 3655 3656 if (Record.size() % 2 != 0) { 3657 Error("invalid undefined-but-used record"); 3658 return Failure; 3659 } 3660 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3661 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++])); 3662 UndefinedButUsed.push_back( 3663 ReadSourceLocation(F, Record, I).getRawEncoding()); 3664 } 3665 break; 3666 3667 case DELETE_EXPRS_TO_ANALYZE: 3668 for (unsigned I = 0, N = Record.size(); I != N;) { 3669 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++])); 3670 const uint64_t Count = Record[I++]; 3671 DelayedDeleteExprs.push_back(Count); 3672 for (uint64_t C = 0; C < Count; ++C) { 3673 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding()); 3674 bool IsArrayForm = Record[I++] == 1; 3675 DelayedDeleteExprs.push_back(IsArrayForm); 3676 } 3677 } 3678 break; 3679 3680 case IMPORTED_MODULES: 3681 if (!F.isModule()) { 3682 // If we aren't loading a module (which has its own exports), make 3683 // all of the imported modules visible. 3684 // FIXME: Deal with macros-only imports. 3685 for (unsigned I = 0, N = Record.size(); I != N; /**/) { 3686 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]); 3687 SourceLocation Loc = ReadSourceLocation(F, Record, I); 3688 if (GlobalID) { 3689 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc)); 3690 if (DeserializationListener) 3691 DeserializationListener->ModuleImportRead(GlobalID, Loc); 3692 } 3693 } 3694 } 3695 break; 3696 3697 case MACRO_OFFSET: { 3698 if (F.LocalNumMacros != 0) { 3699 Error("duplicate MACRO_OFFSET record in AST file"); 3700 return Failure; 3701 } 3702 F.MacroOffsets = (const uint32_t *)Blob.data(); 3703 F.LocalNumMacros = Record[0]; 3704 unsigned LocalBaseMacroID = Record[1]; 3705 F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset; 3706 F.BaseMacroID = getTotalNumMacros(); 3707 3708 if (F.LocalNumMacros > 0) { 3709 // Introduce the global -> local mapping for macros within this module. 3710 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F)); 3711 3712 // Introduce the local -> global mapping for macros within this module. 3713 F.MacroRemap.insertOrReplace( 3714 std::make_pair(LocalBaseMacroID, 3715 F.BaseMacroID - LocalBaseMacroID)); 3716 3717 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros); 3718 } 3719 break; 3720 } 3721 3722 case LATE_PARSED_TEMPLATE: 3723 LateParsedTemplates.append(Record.begin(), Record.end()); 3724 break; 3725 3726 case OPTIMIZE_PRAGMA_OPTIONS: 3727 if (Record.size() != 1) { 3728 Error("invalid pragma optimize record"); 3729 return Failure; 3730 } 3731 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]); 3732 break; 3733 3734 case MSSTRUCT_PRAGMA_OPTIONS: 3735 if (Record.size() != 1) { 3736 Error("invalid pragma ms_struct record"); 3737 return Failure; 3738 } 3739 PragmaMSStructState = Record[0]; 3740 break; 3741 3742 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS: 3743 if (Record.size() != 2) { 3744 Error("invalid pragma ms_struct record"); 3745 return Failure; 3746 } 3747 PragmaMSPointersToMembersState = Record[0]; 3748 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]); 3749 break; 3750 3751 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES: 3752 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3753 UnusedLocalTypedefNameCandidates.push_back( 3754 getGlobalDeclID(F, Record[I])); 3755 break; 3756 3757 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH: 3758 if (Record.size() != 1) { 3759 Error("invalid cuda pragma options record"); 3760 return Failure; 3761 } 3762 ForceCUDAHostDeviceDepth = Record[0]; 3763 break; 3764 3765 case PACK_PRAGMA_OPTIONS: { 3766 if (Record.size() < 3) { 3767 Error("invalid pragma pack record"); 3768 return Failure; 3769 } 3770 PragmaPackCurrentValue = Record[0]; 3771 PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]); 3772 unsigned NumStackEntries = Record[2]; 3773 unsigned Idx = 3; 3774 // Reset the stack when importing a new module. 3775 PragmaPackStack.clear(); 3776 for (unsigned I = 0; I < NumStackEntries; ++I) { 3777 PragmaPackStackEntry Entry; 3778 Entry.Value = Record[Idx++]; 3779 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3780 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3781 PragmaPackStrings.push_back(ReadString(Record, Idx)); 3782 Entry.SlotLabel = PragmaPackStrings.back(); 3783 PragmaPackStack.push_back(Entry); 3784 } 3785 break; 3786 } 3787 3788 case FLOAT_CONTROL_PRAGMA_OPTIONS: { 3789 if (Record.size() < 3) { 3790 Error("invalid pragma pack record"); 3791 return Failure; 3792 } 3793 FpPragmaCurrentValue = Record[0]; 3794 FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]); 3795 unsigned NumStackEntries = Record[2]; 3796 unsigned Idx = 3; 3797 // Reset the stack when importing a new module. 3798 FpPragmaStack.clear(); 3799 for (unsigned I = 0; I < NumStackEntries; ++I) { 3800 FpPragmaStackEntry Entry; 3801 Entry.Value = Record[Idx++]; 3802 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3803 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3804 FpPragmaStrings.push_back(ReadString(Record, Idx)); 3805 Entry.SlotLabel = FpPragmaStrings.back(); 3806 FpPragmaStack.push_back(Entry); 3807 } 3808 break; 3809 } 3810 3811 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS: 3812 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3813 DeclsToCheckForDeferredDiags.push_back(getGlobalDeclID(F, Record[I])); 3814 break; 3815 } 3816 } 3817 } 3818 3819 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const { 3820 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read"); 3821 3822 // Additional remapping information. 3823 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data(); 3824 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size(); 3825 F.ModuleOffsetMap = StringRef(); 3826 3827 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders. 3828 if (F.SLocRemap.find(0) == F.SLocRemap.end()) { 3829 F.SLocRemap.insert(std::make_pair(0U, 0)); 3830 F.SLocRemap.insert(std::make_pair(2U, 1)); 3831 } 3832 3833 // Continuous range maps we may be updating in our module. 3834 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder; 3835 RemapBuilder SLocRemap(F.SLocRemap); 3836 RemapBuilder IdentifierRemap(F.IdentifierRemap); 3837 RemapBuilder MacroRemap(F.MacroRemap); 3838 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap); 3839 RemapBuilder SubmoduleRemap(F.SubmoduleRemap); 3840 RemapBuilder SelectorRemap(F.SelectorRemap); 3841 RemapBuilder DeclRemap(F.DeclRemap); 3842 RemapBuilder TypeRemap(F.TypeRemap); 3843 3844 while (Data < DataEnd) { 3845 // FIXME: Looking up dependency modules by filename is horrible. Let's 3846 // start fixing this with prebuilt, explicit and implicit modules and see 3847 // how it goes... 3848 using namespace llvm::support; 3849 ModuleKind Kind = static_cast<ModuleKind>( 3850 endian::readNext<uint8_t, little, unaligned>(Data)); 3851 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data); 3852 StringRef Name = StringRef((const char*)Data, Len); 3853 Data += Len; 3854 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule || 3855 Kind == MK_ImplicitModule 3856 ? ModuleMgr.lookupByModuleName(Name) 3857 : ModuleMgr.lookupByFileName(Name)); 3858 if (!OM) { 3859 std::string Msg = 3860 "SourceLocation remap refers to unknown module, cannot find "; 3861 Msg.append(std::string(Name)); 3862 Error(Msg); 3863 return; 3864 } 3865 3866 uint32_t SLocOffset = 3867 endian::readNext<uint32_t, little, unaligned>(Data); 3868 uint32_t IdentifierIDOffset = 3869 endian::readNext<uint32_t, little, unaligned>(Data); 3870 uint32_t MacroIDOffset = 3871 endian::readNext<uint32_t, little, unaligned>(Data); 3872 uint32_t PreprocessedEntityIDOffset = 3873 endian::readNext<uint32_t, little, unaligned>(Data); 3874 uint32_t SubmoduleIDOffset = 3875 endian::readNext<uint32_t, little, unaligned>(Data); 3876 uint32_t SelectorIDOffset = 3877 endian::readNext<uint32_t, little, unaligned>(Data); 3878 uint32_t DeclIDOffset = 3879 endian::readNext<uint32_t, little, unaligned>(Data); 3880 uint32_t TypeIndexOffset = 3881 endian::readNext<uint32_t, little, unaligned>(Data); 3882 3883 uint32_t None = std::numeric_limits<uint32_t>::max(); 3884 3885 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset, 3886 RemapBuilder &Remap) { 3887 if (Offset != None) 3888 Remap.insert(std::make_pair(Offset, 3889 static_cast<int>(BaseOffset - Offset))); 3890 }; 3891 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap); 3892 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap); 3893 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap); 3894 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID, 3895 PreprocessedEntityRemap); 3896 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap); 3897 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap); 3898 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap); 3899 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap); 3900 3901 // Global -> local mappings. 3902 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset; 3903 } 3904 } 3905 3906 ASTReader::ASTReadResult 3907 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F, 3908 const ModuleFile *ImportedBy, 3909 unsigned ClientLoadCapabilities) { 3910 unsigned Idx = 0; 3911 F.ModuleMapPath = ReadPath(F, Record, Idx); 3912 3913 // Try to resolve ModuleName in the current header search context and 3914 // verify that it is found in the same module map file as we saved. If the 3915 // top-level AST file is a main file, skip this check because there is no 3916 // usable header search context. 3917 assert(!F.ModuleName.empty() && 3918 "MODULE_NAME should come before MODULE_MAP_FILE"); 3919 if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) { 3920 // An implicitly-loaded module file should have its module listed in some 3921 // module map file that we've already loaded. 3922 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName); 3923 auto &Map = PP.getHeaderSearchInfo().getModuleMap(); 3924 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr; 3925 // Don't emit module relocation error if we have -fno-validate-pch 3926 if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) { 3927 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) { 3928 if (auto *ASTFE = M ? M->getASTFile() : nullptr) { 3929 // This module was defined by an imported (explicit) module. 3930 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName 3931 << ASTFE->getName(); 3932 } else { 3933 // This module was built with a different module map. 3934 Diag(diag::err_imported_module_not_found) 3935 << F.ModuleName << F.FileName 3936 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath 3937 << !ImportedBy; 3938 // In case it was imported by a PCH, there's a chance the user is 3939 // just missing to include the search path to the directory containing 3940 // the modulemap. 3941 if (ImportedBy && ImportedBy->Kind == MK_PCH) 3942 Diag(diag::note_imported_by_pch_module_not_found) 3943 << llvm::sys::path::parent_path(F.ModuleMapPath); 3944 } 3945 } 3946 return OutOfDate; 3947 } 3948 3949 assert(M->Name == F.ModuleName && "found module with different name"); 3950 3951 // Check the primary module map file. 3952 auto StoredModMap = FileMgr.getFile(F.ModuleMapPath); 3953 if (!StoredModMap || *StoredModMap != ModMap) { 3954 assert(ModMap && "found module is missing module map file"); 3955 assert((ImportedBy || F.Kind == MK_ImplicitModule) && 3956 "top-level import should be verified"); 3957 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy; 3958 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3959 Diag(diag::err_imported_module_modmap_changed) 3960 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName) 3961 << ModMap->getName() << F.ModuleMapPath << NotImported; 3962 return OutOfDate; 3963 } 3964 3965 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps; 3966 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) { 3967 // FIXME: we should use input files rather than storing names. 3968 std::string Filename = ReadPath(F, Record, Idx); 3969 auto F = FileMgr.getFile(Filename, false, false); 3970 if (!F) { 3971 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3972 Error("could not find file '" + Filename +"' referenced by AST file"); 3973 return OutOfDate; 3974 } 3975 AdditionalStoredMaps.insert(*F); 3976 } 3977 3978 // Check any additional module map files (e.g. module.private.modulemap) 3979 // that are not in the pcm. 3980 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) { 3981 for (const FileEntry *ModMap : *AdditionalModuleMaps) { 3982 // Remove files that match 3983 // Note: SmallPtrSet::erase is really remove 3984 if (!AdditionalStoredMaps.erase(ModMap)) { 3985 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3986 Diag(diag::err_module_different_modmap) 3987 << F.ModuleName << /*new*/0 << ModMap->getName(); 3988 return OutOfDate; 3989 } 3990 } 3991 } 3992 3993 // Check any additional module map files that are in the pcm, but not 3994 // found in header search. Cases that match are already removed. 3995 for (const FileEntry *ModMap : AdditionalStoredMaps) { 3996 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3997 Diag(diag::err_module_different_modmap) 3998 << F.ModuleName << /*not new*/1 << ModMap->getName(); 3999 return OutOfDate; 4000 } 4001 } 4002 4003 if (Listener) 4004 Listener->ReadModuleMapFile(F.ModuleMapPath); 4005 return Success; 4006 } 4007 4008 /// Move the given method to the back of the global list of methods. 4009 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) { 4010 // Find the entry for this selector in the method pool. 4011 Sema::GlobalMethodPool::iterator Known 4012 = S.MethodPool.find(Method->getSelector()); 4013 if (Known == S.MethodPool.end()) 4014 return; 4015 4016 // Retrieve the appropriate method list. 4017 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first 4018 : Known->second.second; 4019 bool Found = false; 4020 for (ObjCMethodList *List = &Start; List; List = List->getNext()) { 4021 if (!Found) { 4022 if (List->getMethod() == Method) { 4023 Found = true; 4024 } else { 4025 // Keep searching. 4026 continue; 4027 } 4028 } 4029 4030 if (List->getNext()) 4031 List->setMethod(List->getNext()->getMethod()); 4032 else 4033 List->setMethod(Method); 4034 } 4035 } 4036 4037 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) { 4038 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?"); 4039 for (Decl *D : Names) { 4040 bool wasHidden = !D->isUnconditionallyVisible(); 4041 D->setVisibleDespiteOwningModule(); 4042 4043 if (wasHidden && SemaObj) { 4044 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) { 4045 moveMethodToBackOfGlobalList(*SemaObj, Method); 4046 } 4047 } 4048 } 4049 } 4050 4051 void ASTReader::makeModuleVisible(Module *Mod, 4052 Module::NameVisibilityKind NameVisibility, 4053 SourceLocation ImportLoc) { 4054 llvm::SmallPtrSet<Module *, 4> Visited; 4055 SmallVector<Module *, 4> Stack; 4056 Stack.push_back(Mod); 4057 while (!Stack.empty()) { 4058 Mod = Stack.pop_back_val(); 4059 4060 if (NameVisibility <= Mod->NameVisibility) { 4061 // This module already has this level of visibility (or greater), so 4062 // there is nothing more to do. 4063 continue; 4064 } 4065 4066 if (Mod->isUnimportable()) { 4067 // Modules that aren't importable cannot be made visible. 4068 continue; 4069 } 4070 4071 // Update the module's name visibility. 4072 Mod->NameVisibility = NameVisibility; 4073 4074 // If we've already deserialized any names from this module, 4075 // mark them as visible. 4076 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod); 4077 if (Hidden != HiddenNamesMap.end()) { 4078 auto HiddenNames = std::move(*Hidden); 4079 HiddenNamesMap.erase(Hidden); 4080 makeNamesVisible(HiddenNames.second, HiddenNames.first); 4081 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() && 4082 "making names visible added hidden names"); 4083 } 4084 4085 // Push any exported modules onto the stack to be marked as visible. 4086 SmallVector<Module *, 16> Exports; 4087 Mod->getExportedModules(Exports); 4088 for (SmallVectorImpl<Module *>::iterator 4089 I = Exports.begin(), E = Exports.end(); I != E; ++I) { 4090 Module *Exported = *I; 4091 if (Visited.insert(Exported).second) 4092 Stack.push_back(Exported); 4093 } 4094 } 4095 } 4096 4097 /// We've merged the definition \p MergedDef into the existing definition 4098 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made 4099 /// visible. 4100 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def, 4101 NamedDecl *MergedDef) { 4102 if (!Def->isUnconditionallyVisible()) { 4103 // If MergedDef is visible or becomes visible, make the definition visible. 4104 if (MergedDef->isUnconditionallyVisible()) 4105 Def->setVisibleDespiteOwningModule(); 4106 else { 4107 getContext().mergeDefinitionIntoModule( 4108 Def, MergedDef->getImportedOwningModule(), 4109 /*NotifyListeners*/ false); 4110 PendingMergedDefinitionsToDeduplicate.insert(Def); 4111 } 4112 } 4113 } 4114 4115 bool ASTReader::loadGlobalIndex() { 4116 if (GlobalIndex) 4117 return false; 4118 4119 if (TriedLoadingGlobalIndex || !UseGlobalIndex || 4120 !PP.getLangOpts().Modules) 4121 return true; 4122 4123 // Try to load the global index. 4124 TriedLoadingGlobalIndex = true; 4125 StringRef ModuleCachePath 4126 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath(); 4127 std::pair<GlobalModuleIndex *, llvm::Error> Result = 4128 GlobalModuleIndex::readIndex(ModuleCachePath); 4129 if (llvm::Error Err = std::move(Result.second)) { 4130 assert(!Result.first); 4131 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 4132 return true; 4133 } 4134 4135 GlobalIndex.reset(Result.first); 4136 ModuleMgr.setGlobalIndex(GlobalIndex.get()); 4137 return false; 4138 } 4139 4140 bool ASTReader::isGlobalIndexUnavailable() const { 4141 return PP.getLangOpts().Modules && UseGlobalIndex && 4142 !hasGlobalIndex() && TriedLoadingGlobalIndex; 4143 } 4144 4145 static void updateModuleTimestamp(ModuleFile &MF) { 4146 // Overwrite the timestamp file contents so that file's mtime changes. 4147 std::string TimestampFilename = MF.getTimestampFilename(); 4148 std::error_code EC; 4149 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text); 4150 if (EC) 4151 return; 4152 OS << "Timestamp file\n"; 4153 OS.close(); 4154 OS.clear_error(); // Avoid triggering a fatal error. 4155 } 4156 4157 /// Given a cursor at the start of an AST file, scan ahead and drop the 4158 /// cursor into the start of the given block ID, returning false on success and 4159 /// true on failure. 4160 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) { 4161 while (true) { 4162 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 4163 if (!MaybeEntry) { 4164 // FIXME this drops errors on the floor. 4165 consumeError(MaybeEntry.takeError()); 4166 return true; 4167 } 4168 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4169 4170 switch (Entry.Kind) { 4171 case llvm::BitstreamEntry::Error: 4172 case llvm::BitstreamEntry::EndBlock: 4173 return true; 4174 4175 case llvm::BitstreamEntry::Record: 4176 // Ignore top-level records. 4177 if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID)) 4178 break; 4179 else { 4180 // FIXME this drops errors on the floor. 4181 consumeError(Skipped.takeError()); 4182 return true; 4183 } 4184 4185 case llvm::BitstreamEntry::SubBlock: 4186 if (Entry.ID == BlockID) { 4187 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) { 4188 // FIXME this drops the error on the floor. 4189 consumeError(std::move(Err)); 4190 return true; 4191 } 4192 // Found it! 4193 return false; 4194 } 4195 4196 if (llvm::Error Err = Cursor.SkipBlock()) { 4197 // FIXME this drops the error on the floor. 4198 consumeError(std::move(Err)); 4199 return true; 4200 } 4201 } 4202 } 4203 } 4204 4205 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, 4206 ModuleKind Type, 4207 SourceLocation ImportLoc, 4208 unsigned ClientLoadCapabilities, 4209 SmallVectorImpl<ImportedSubmodule> *Imported) { 4210 llvm::SaveAndRestore<SourceLocation> 4211 SetCurImportLocRAII(CurrentImportLoc, ImportLoc); 4212 4213 // Defer any pending actions until we get to the end of reading the AST file. 4214 Deserializing AnASTFile(this); 4215 4216 // Bump the generation number. 4217 unsigned PreviousGeneration = 0; 4218 if (ContextObj) 4219 PreviousGeneration = incrementGeneration(*ContextObj); 4220 4221 unsigned NumModules = ModuleMgr.size(); 4222 auto removeModulesAndReturn = [&](ASTReadResult ReadResult) { 4223 assert(ReadResult && "expected to return error"); 4224 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, 4225 PP.getLangOpts().Modules 4226 ? &PP.getHeaderSearchInfo().getModuleMap() 4227 : nullptr); 4228 4229 // If we find that any modules are unusable, the global index is going 4230 // to be out-of-date. Just remove it. 4231 GlobalIndex.reset(); 4232 ModuleMgr.setGlobalIndex(nullptr); 4233 return ReadResult; 4234 }; 4235 4236 SmallVector<ImportedModule, 4> Loaded; 4237 switch (ASTReadResult ReadResult = 4238 ReadASTCore(FileName, Type, ImportLoc, 4239 /*ImportedBy=*/nullptr, Loaded, 0, 0, 4240 ASTFileSignature(), ClientLoadCapabilities)) { 4241 case Failure: 4242 case Missing: 4243 case OutOfDate: 4244 case VersionMismatch: 4245 case ConfigurationMismatch: 4246 case HadErrors: 4247 return removeModulesAndReturn(ReadResult); 4248 case Success: 4249 break; 4250 } 4251 4252 // Here comes stuff that we only do once the entire chain is loaded. 4253 4254 // Load the AST blocks of all of the modules that we loaded. We can still 4255 // hit errors parsing the ASTs at this point. 4256 for (ImportedModule &M : Loaded) { 4257 ModuleFile &F = *M.Mod; 4258 4259 // Read the AST block. 4260 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities)) 4261 return removeModulesAndReturn(Result); 4262 4263 // The AST block should always have a definition for the main module. 4264 if (F.isModule() && !F.DidReadTopLevelSubmodule) { 4265 Error(diag::err_module_file_missing_top_level_submodule, F.FileName); 4266 return removeModulesAndReturn(Failure); 4267 } 4268 4269 // Read the extension blocks. 4270 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) { 4271 if (ASTReadResult Result = ReadExtensionBlock(F)) 4272 return removeModulesAndReturn(Result); 4273 } 4274 4275 // Once read, set the ModuleFile bit base offset and update the size in 4276 // bits of all files we've seen. 4277 F.GlobalBitOffset = TotalModulesSizeInBits; 4278 TotalModulesSizeInBits += F.SizeInBits; 4279 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F)); 4280 } 4281 4282 // Preload source locations and interesting indentifiers. 4283 for (ImportedModule &M : Loaded) { 4284 ModuleFile &F = *M.Mod; 4285 4286 // Preload SLocEntries. 4287 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) { 4288 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID; 4289 // Load it through the SourceManager and don't call ReadSLocEntry() 4290 // directly because the entry may have already been loaded in which case 4291 // calling ReadSLocEntry() directly would trigger an assertion in 4292 // SourceManager. 4293 SourceMgr.getLoadedSLocEntryByID(Index); 4294 } 4295 4296 // Map the original source file ID into the ID space of the current 4297 // compilation. 4298 if (F.OriginalSourceFileID.isValid()) { 4299 F.OriginalSourceFileID = FileID::get( 4300 F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1); 4301 } 4302 4303 // Preload all the pending interesting identifiers by marking them out of 4304 // date. 4305 for (auto Offset : F.PreloadIdentifierOffsets) { 4306 const unsigned char *Data = reinterpret_cast<const unsigned char *>( 4307 F.IdentifierTableData + Offset); 4308 4309 ASTIdentifierLookupTrait Trait(*this, F); 4310 auto KeyDataLen = Trait.ReadKeyDataLength(Data); 4311 auto Key = Trait.ReadKey(Data, KeyDataLen.first); 4312 auto &II = PP.getIdentifierTable().getOwn(Key); 4313 II.setOutOfDate(true); 4314 4315 // Mark this identifier as being from an AST file so that we can track 4316 // whether we need to serialize it. 4317 markIdentifierFromAST(*this, II); 4318 4319 // Associate the ID with the identifier so that the writer can reuse it. 4320 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first); 4321 SetIdentifierInfo(ID, &II); 4322 } 4323 } 4324 4325 // Setup the import locations and notify the module manager that we've 4326 // committed to these module files. 4327 for (ImportedModule &M : Loaded) { 4328 ModuleFile &F = *M.Mod; 4329 4330 ModuleMgr.moduleFileAccepted(&F); 4331 4332 // Set the import location. 4333 F.DirectImportLoc = ImportLoc; 4334 // FIXME: We assume that locations from PCH / preamble do not need 4335 // any translation. 4336 if (!M.ImportedBy) 4337 F.ImportLoc = M.ImportLoc; 4338 else 4339 F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc); 4340 } 4341 4342 if (!PP.getLangOpts().CPlusPlus || 4343 (Type != MK_ImplicitModule && Type != MK_ExplicitModule && 4344 Type != MK_PrebuiltModule)) { 4345 // Mark all of the identifiers in the identifier table as being out of date, 4346 // so that various accessors know to check the loaded modules when the 4347 // identifier is used. 4348 // 4349 // For C++ modules, we don't need information on many identifiers (just 4350 // those that provide macros or are poisoned), so we mark all of 4351 // the interesting ones via PreloadIdentifierOffsets. 4352 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(), 4353 IdEnd = PP.getIdentifierTable().end(); 4354 Id != IdEnd; ++Id) 4355 Id->second->setOutOfDate(true); 4356 } 4357 // Mark selectors as out of date. 4358 for (auto Sel : SelectorGeneration) 4359 SelectorOutOfDate[Sel.first] = true; 4360 4361 // Resolve any unresolved module exports. 4362 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) { 4363 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I]; 4364 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID); 4365 Module *ResolvedMod = getSubmodule(GlobalID); 4366 4367 switch (Unresolved.Kind) { 4368 case UnresolvedModuleRef::Conflict: 4369 if (ResolvedMod) { 4370 Module::Conflict Conflict; 4371 Conflict.Other = ResolvedMod; 4372 Conflict.Message = Unresolved.String.str(); 4373 Unresolved.Mod->Conflicts.push_back(Conflict); 4374 } 4375 continue; 4376 4377 case UnresolvedModuleRef::Import: 4378 if (ResolvedMod) 4379 Unresolved.Mod->Imports.insert(ResolvedMod); 4380 continue; 4381 4382 case UnresolvedModuleRef::Export: 4383 if (ResolvedMod || Unresolved.IsWildcard) 4384 Unresolved.Mod->Exports.push_back( 4385 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard)); 4386 continue; 4387 } 4388 } 4389 UnresolvedModuleRefs.clear(); 4390 4391 if (Imported) 4392 Imported->append(ImportedModules.begin(), 4393 ImportedModules.end()); 4394 4395 // FIXME: How do we load the 'use'd modules? They may not be submodules. 4396 // Might be unnecessary as use declarations are only used to build the 4397 // module itself. 4398 4399 if (ContextObj) 4400 InitializeContext(); 4401 4402 if (SemaObj) 4403 UpdateSema(); 4404 4405 if (DeserializationListener) 4406 DeserializationListener->ReaderInitialized(this); 4407 4408 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule(); 4409 if (PrimaryModule.OriginalSourceFileID.isValid()) { 4410 // If this AST file is a precompiled preamble, then set the 4411 // preamble file ID of the source manager to the file source file 4412 // from which the preamble was built. 4413 if (Type == MK_Preamble) { 4414 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID); 4415 } else if (Type == MK_MainFile) { 4416 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID); 4417 } 4418 } 4419 4420 // For any Objective-C class definitions we have already loaded, make sure 4421 // that we load any additional categories. 4422 if (ContextObj) { 4423 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) { 4424 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(), 4425 ObjCClassesLoaded[I], 4426 PreviousGeneration); 4427 } 4428 } 4429 4430 if (PP.getHeaderSearchInfo() 4431 .getHeaderSearchOpts() 4432 .ModulesValidateOncePerBuildSession) { 4433 // Now we are certain that the module and all modules it depends on are 4434 // up to date. Create or update timestamp files for modules that are 4435 // located in the module cache (not for PCH files that could be anywhere 4436 // in the filesystem). 4437 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) { 4438 ImportedModule &M = Loaded[I]; 4439 if (M.Mod->Kind == MK_ImplicitModule) { 4440 updateModuleTimestamp(*M.Mod); 4441 } 4442 } 4443 } 4444 4445 return Success; 4446 } 4447 4448 static ASTFileSignature readASTFileSignature(StringRef PCH); 4449 4450 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'. 4451 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) { 4452 // FIXME checking magic headers is done in other places such as 4453 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't 4454 // always done the same. Unify it all with a helper. 4455 if (!Stream.canSkipToPos(4)) 4456 return llvm::createStringError(std::errc::illegal_byte_sequence, 4457 "file too small to contain AST file magic"); 4458 for (unsigned C : {'C', 'P', 'C', 'H'}) 4459 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) { 4460 if (Res.get() != C) 4461 return llvm::createStringError( 4462 std::errc::illegal_byte_sequence, 4463 "file doesn't start with AST file magic"); 4464 } else 4465 return Res.takeError(); 4466 return llvm::Error::success(); 4467 } 4468 4469 static unsigned moduleKindForDiagnostic(ModuleKind Kind) { 4470 switch (Kind) { 4471 case MK_PCH: 4472 return 0; // PCH 4473 case MK_ImplicitModule: 4474 case MK_ExplicitModule: 4475 case MK_PrebuiltModule: 4476 return 1; // module 4477 case MK_MainFile: 4478 case MK_Preamble: 4479 return 2; // main source file 4480 } 4481 llvm_unreachable("unknown module kind"); 4482 } 4483 4484 ASTReader::ASTReadResult 4485 ASTReader::ReadASTCore(StringRef FileName, 4486 ModuleKind Type, 4487 SourceLocation ImportLoc, 4488 ModuleFile *ImportedBy, 4489 SmallVectorImpl<ImportedModule> &Loaded, 4490 off_t ExpectedSize, time_t ExpectedModTime, 4491 ASTFileSignature ExpectedSignature, 4492 unsigned ClientLoadCapabilities) { 4493 ModuleFile *M; 4494 std::string ErrorStr; 4495 ModuleManager::AddModuleResult AddResult 4496 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy, 4497 getGeneration(), ExpectedSize, ExpectedModTime, 4498 ExpectedSignature, readASTFileSignature, 4499 M, ErrorStr); 4500 4501 switch (AddResult) { 4502 case ModuleManager::AlreadyLoaded: 4503 Diag(diag::remark_module_import) 4504 << M->ModuleName << M->FileName << (ImportedBy ? true : false) 4505 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 4506 return Success; 4507 4508 case ModuleManager::NewlyLoaded: 4509 // Load module file below. 4510 break; 4511 4512 case ModuleManager::Missing: 4513 // The module file was missing; if the client can handle that, return 4514 // it. 4515 if (ClientLoadCapabilities & ARR_Missing) 4516 return Missing; 4517 4518 // Otherwise, return an error. 4519 Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type) 4520 << FileName << !ErrorStr.empty() 4521 << ErrorStr; 4522 return Failure; 4523 4524 case ModuleManager::OutOfDate: 4525 // We couldn't load the module file because it is out-of-date. If the 4526 // client can handle out-of-date, return it. 4527 if (ClientLoadCapabilities & ARR_OutOfDate) 4528 return OutOfDate; 4529 4530 // Otherwise, return an error. 4531 Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type) 4532 << FileName << !ErrorStr.empty() 4533 << ErrorStr; 4534 return Failure; 4535 } 4536 4537 assert(M && "Missing module file"); 4538 4539 bool ShouldFinalizePCM = false; 4540 auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() { 4541 auto &MC = getModuleManager().getModuleCache(); 4542 if (ShouldFinalizePCM) 4543 MC.finalizePCM(FileName); 4544 else 4545 MC.tryToDropPCM(FileName); 4546 }); 4547 ModuleFile &F = *M; 4548 BitstreamCursor &Stream = F.Stream; 4549 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer)); 4550 F.SizeInBits = F.Buffer->getBufferSize() * 8; 4551 4552 // Sniff for the signature. 4553 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4554 Diag(diag::err_module_file_invalid) 4555 << moduleKindForDiagnostic(Type) << FileName << std::move(Err); 4556 return Failure; 4557 } 4558 4559 // This is used for compatibility with older PCH formats. 4560 bool HaveReadControlBlock = false; 4561 while (true) { 4562 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4563 if (!MaybeEntry) { 4564 Error(MaybeEntry.takeError()); 4565 return Failure; 4566 } 4567 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4568 4569 switch (Entry.Kind) { 4570 case llvm::BitstreamEntry::Error: 4571 case llvm::BitstreamEntry::Record: 4572 case llvm::BitstreamEntry::EndBlock: 4573 Error("invalid record at top-level of AST file"); 4574 return Failure; 4575 4576 case llvm::BitstreamEntry::SubBlock: 4577 break; 4578 } 4579 4580 switch (Entry.ID) { 4581 case CONTROL_BLOCK_ID: 4582 HaveReadControlBlock = true; 4583 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) { 4584 case Success: 4585 // Check that we didn't try to load a non-module AST file as a module. 4586 // 4587 // FIXME: Should we also perform the converse check? Loading a module as 4588 // a PCH file sort of works, but it's a bit wonky. 4589 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule || 4590 Type == MK_PrebuiltModule) && 4591 F.ModuleName.empty()) { 4592 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure; 4593 if (Result != OutOfDate || 4594 (ClientLoadCapabilities & ARR_OutOfDate) == 0) 4595 Diag(diag::err_module_file_not_module) << FileName; 4596 return Result; 4597 } 4598 break; 4599 4600 case Failure: return Failure; 4601 case Missing: return Missing; 4602 case OutOfDate: return OutOfDate; 4603 case VersionMismatch: return VersionMismatch; 4604 case ConfigurationMismatch: return ConfigurationMismatch; 4605 case HadErrors: return HadErrors; 4606 } 4607 break; 4608 4609 case AST_BLOCK_ID: 4610 if (!HaveReadControlBlock) { 4611 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 4612 Diag(diag::err_pch_version_too_old); 4613 return VersionMismatch; 4614 } 4615 4616 // Record that we've loaded this module. 4617 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc)); 4618 ShouldFinalizePCM = true; 4619 return Success; 4620 4621 case UNHASHED_CONTROL_BLOCK_ID: 4622 // This block is handled using look-ahead during ReadControlBlock. We 4623 // shouldn't get here! 4624 Error("malformed block record in AST file"); 4625 return Failure; 4626 4627 default: 4628 if (llvm::Error Err = Stream.SkipBlock()) { 4629 Error(std::move(Err)); 4630 return Failure; 4631 } 4632 break; 4633 } 4634 } 4635 4636 llvm_unreachable("unexpected break; expected return"); 4637 } 4638 4639 ASTReader::ASTReadResult 4640 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy, 4641 unsigned ClientLoadCapabilities) { 4642 const HeaderSearchOptions &HSOpts = 4643 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 4644 bool AllowCompatibleConfigurationMismatch = 4645 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 4646 4647 ASTReadResult Result = readUnhashedControlBlockImpl( 4648 &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch, 4649 Listener.get(), 4650 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions); 4651 4652 // If F was directly imported by another module, it's implicitly validated by 4653 // the importing module. 4654 if (DisableValidation || WasImportedBy || 4655 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 4656 return Success; 4657 4658 if (Result == Failure) { 4659 Error("malformed block record in AST file"); 4660 return Failure; 4661 } 4662 4663 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) { 4664 // If this module has already been finalized in the ModuleCache, we're stuck 4665 // with it; we can only load a single version of each module. 4666 // 4667 // This can happen when a module is imported in two contexts: in one, as a 4668 // user module; in another, as a system module (due to an import from 4669 // another module marked with the [system] flag). It usually indicates a 4670 // bug in the module map: this module should also be marked with [system]. 4671 // 4672 // If -Wno-system-headers (the default), and the first import is as a 4673 // system module, then validation will fail during the as-user import, 4674 // since -Werror flags won't have been validated. However, it's reasonable 4675 // to treat this consistently as a system module. 4676 // 4677 // If -Wsystem-headers, the PCM on disk was built with 4678 // -Wno-system-headers, and the first import is as a user module, then 4679 // validation will fail during the as-system import since the PCM on disk 4680 // doesn't guarantee that -Werror was respected. However, the -Werror 4681 // flags were checked during the initial as-user import. 4682 if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) { 4683 Diag(diag::warn_module_system_bit_conflict) << F.FileName; 4684 return Success; 4685 } 4686 } 4687 4688 return Result; 4689 } 4690 4691 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl( 4692 ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities, 4693 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener, 4694 bool ValidateDiagnosticOptions) { 4695 // Initialize a stream. 4696 BitstreamCursor Stream(StreamData); 4697 4698 // Sniff for the signature. 4699 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4700 // FIXME this drops the error on the floor. 4701 consumeError(std::move(Err)); 4702 return Failure; 4703 } 4704 4705 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 4706 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 4707 return Failure; 4708 4709 // Read all of the records in the options block. 4710 RecordData Record; 4711 ASTReadResult Result = Success; 4712 while (true) { 4713 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4714 if (!MaybeEntry) { 4715 // FIXME this drops the error on the floor. 4716 consumeError(MaybeEntry.takeError()); 4717 return Failure; 4718 } 4719 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4720 4721 switch (Entry.Kind) { 4722 case llvm::BitstreamEntry::Error: 4723 case llvm::BitstreamEntry::SubBlock: 4724 return Failure; 4725 4726 case llvm::BitstreamEntry::EndBlock: 4727 return Result; 4728 4729 case llvm::BitstreamEntry::Record: 4730 // The interesting case. 4731 break; 4732 } 4733 4734 // Read and process a record. 4735 Record.clear(); 4736 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record); 4737 if (!MaybeRecordType) { 4738 // FIXME this drops the error. 4739 return Failure; 4740 } 4741 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) { 4742 case SIGNATURE: 4743 if (F) 4744 F->Signature = ASTFileSignature::create(Record.begin(), Record.end()); 4745 break; 4746 case AST_BLOCK_HASH: 4747 if (F) 4748 F->ASTBlockHash = 4749 ASTFileSignature::create(Record.begin(), Record.end()); 4750 break; 4751 case DIAGNOSTIC_OPTIONS: { 4752 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 4753 if (Listener && ValidateDiagnosticOptions && 4754 !AllowCompatibleConfigurationMismatch && 4755 ParseDiagnosticOptions(Record, Complain, *Listener)) 4756 Result = OutOfDate; // Don't return early. Read the signature. 4757 break; 4758 } 4759 case DIAG_PRAGMA_MAPPINGS: 4760 if (!F) 4761 break; 4762 if (F->PragmaDiagMappings.empty()) 4763 F->PragmaDiagMappings.swap(Record); 4764 else 4765 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(), 4766 Record.begin(), Record.end()); 4767 break; 4768 } 4769 } 4770 } 4771 4772 /// Parse a record and blob containing module file extension metadata. 4773 static bool parseModuleFileExtensionMetadata( 4774 const SmallVectorImpl<uint64_t> &Record, 4775 StringRef Blob, 4776 ModuleFileExtensionMetadata &Metadata) { 4777 if (Record.size() < 4) return true; 4778 4779 Metadata.MajorVersion = Record[0]; 4780 Metadata.MinorVersion = Record[1]; 4781 4782 unsigned BlockNameLen = Record[2]; 4783 unsigned UserInfoLen = Record[3]; 4784 4785 if (BlockNameLen + UserInfoLen > Blob.size()) return true; 4786 4787 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen); 4788 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen, 4789 Blob.data() + BlockNameLen + UserInfoLen); 4790 return false; 4791 } 4792 4793 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) { 4794 BitstreamCursor &Stream = F.Stream; 4795 4796 RecordData Record; 4797 while (true) { 4798 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4799 if (!MaybeEntry) { 4800 Error(MaybeEntry.takeError()); 4801 return Failure; 4802 } 4803 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4804 4805 switch (Entry.Kind) { 4806 case llvm::BitstreamEntry::SubBlock: 4807 if (llvm::Error Err = Stream.SkipBlock()) { 4808 Error(std::move(Err)); 4809 return Failure; 4810 } 4811 continue; 4812 4813 case llvm::BitstreamEntry::EndBlock: 4814 return Success; 4815 4816 case llvm::BitstreamEntry::Error: 4817 return HadErrors; 4818 4819 case llvm::BitstreamEntry::Record: 4820 break; 4821 } 4822 4823 Record.clear(); 4824 StringRef Blob; 4825 Expected<unsigned> MaybeRecCode = 4826 Stream.readRecord(Entry.ID, Record, &Blob); 4827 if (!MaybeRecCode) { 4828 Error(MaybeRecCode.takeError()); 4829 return Failure; 4830 } 4831 switch (MaybeRecCode.get()) { 4832 case EXTENSION_METADATA: { 4833 ModuleFileExtensionMetadata Metadata; 4834 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) { 4835 Error("malformed EXTENSION_METADATA in AST file"); 4836 return Failure; 4837 } 4838 4839 // Find a module file extension with this block name. 4840 auto Known = ModuleFileExtensions.find(Metadata.BlockName); 4841 if (Known == ModuleFileExtensions.end()) break; 4842 4843 // Form a reader. 4844 if (auto Reader = Known->second->createExtensionReader(Metadata, *this, 4845 F, Stream)) { 4846 F.ExtensionReaders.push_back(std::move(Reader)); 4847 } 4848 4849 break; 4850 } 4851 } 4852 } 4853 4854 return Success; 4855 } 4856 4857 void ASTReader::InitializeContext() { 4858 assert(ContextObj && "no context to initialize"); 4859 ASTContext &Context = *ContextObj; 4860 4861 // If there's a listener, notify them that we "read" the translation unit. 4862 if (DeserializationListener) 4863 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, 4864 Context.getTranslationUnitDecl()); 4865 4866 // FIXME: Find a better way to deal with collisions between these 4867 // built-in types. Right now, we just ignore the problem. 4868 4869 // Load the special types. 4870 if (SpecialTypes.size() >= NumSpecialTypeIDs) { 4871 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) { 4872 if (!Context.CFConstantStringTypeDecl) 4873 Context.setCFConstantStringType(GetType(String)); 4874 } 4875 4876 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) { 4877 QualType FileType = GetType(File); 4878 if (FileType.isNull()) { 4879 Error("FILE type is NULL"); 4880 return; 4881 } 4882 4883 if (!Context.FILEDecl) { 4884 if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) 4885 Context.setFILEDecl(Typedef->getDecl()); 4886 else { 4887 const TagType *Tag = FileType->getAs<TagType>(); 4888 if (!Tag) { 4889 Error("Invalid FILE type in AST file"); 4890 return; 4891 } 4892 Context.setFILEDecl(Tag->getDecl()); 4893 } 4894 } 4895 } 4896 4897 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) { 4898 QualType Jmp_bufType = GetType(Jmp_buf); 4899 if (Jmp_bufType.isNull()) { 4900 Error("jmp_buf type is NULL"); 4901 return; 4902 } 4903 4904 if (!Context.jmp_bufDecl) { 4905 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) 4906 Context.setjmp_bufDecl(Typedef->getDecl()); 4907 else { 4908 const TagType *Tag = Jmp_bufType->getAs<TagType>(); 4909 if (!Tag) { 4910 Error("Invalid jmp_buf type in AST file"); 4911 return; 4912 } 4913 Context.setjmp_bufDecl(Tag->getDecl()); 4914 } 4915 } 4916 } 4917 4918 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) { 4919 QualType Sigjmp_bufType = GetType(Sigjmp_buf); 4920 if (Sigjmp_bufType.isNull()) { 4921 Error("sigjmp_buf type is NULL"); 4922 return; 4923 } 4924 4925 if (!Context.sigjmp_bufDecl) { 4926 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) 4927 Context.setsigjmp_bufDecl(Typedef->getDecl()); 4928 else { 4929 const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); 4930 assert(Tag && "Invalid sigjmp_buf type in AST file"); 4931 Context.setsigjmp_bufDecl(Tag->getDecl()); 4932 } 4933 } 4934 } 4935 4936 if (unsigned ObjCIdRedef 4937 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) { 4938 if (Context.ObjCIdRedefinitionType.isNull()) 4939 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef); 4940 } 4941 4942 if (unsigned ObjCClassRedef 4943 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) { 4944 if (Context.ObjCClassRedefinitionType.isNull()) 4945 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef); 4946 } 4947 4948 if (unsigned ObjCSelRedef 4949 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) { 4950 if (Context.ObjCSelRedefinitionType.isNull()) 4951 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef); 4952 } 4953 4954 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) { 4955 QualType Ucontext_tType = GetType(Ucontext_t); 4956 if (Ucontext_tType.isNull()) { 4957 Error("ucontext_t type is NULL"); 4958 return; 4959 } 4960 4961 if (!Context.ucontext_tDecl) { 4962 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>()) 4963 Context.setucontext_tDecl(Typedef->getDecl()); 4964 else { 4965 const TagType *Tag = Ucontext_tType->getAs<TagType>(); 4966 assert(Tag && "Invalid ucontext_t type in AST file"); 4967 Context.setucontext_tDecl(Tag->getDecl()); 4968 } 4969 } 4970 } 4971 } 4972 4973 ReadPragmaDiagnosticMappings(Context.getDiagnostics()); 4974 4975 // If there were any CUDA special declarations, deserialize them. 4976 if (!CUDASpecialDeclRefs.empty()) { 4977 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!"); 4978 Context.setcudaConfigureCallDecl( 4979 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0]))); 4980 } 4981 4982 // Re-export any modules that were imported by a non-module AST file. 4983 // FIXME: This does not make macro-only imports visible again. 4984 for (auto &Import : ImportedModules) { 4985 if (Module *Imported = getSubmodule(Import.ID)) { 4986 makeModuleVisible(Imported, Module::AllVisible, 4987 /*ImportLoc=*/Import.ImportLoc); 4988 if (Import.ImportLoc.isValid()) 4989 PP.makeModuleVisible(Imported, Import.ImportLoc); 4990 // FIXME: should we tell Sema to make the module visible too? 4991 } 4992 } 4993 ImportedModules.clear(); 4994 } 4995 4996 void ASTReader::finalizeForWriting() { 4997 // Nothing to do for now. 4998 } 4999 5000 /// Reads and return the signature record from \p PCH's control block, or 5001 /// else returns 0. 5002 static ASTFileSignature readASTFileSignature(StringRef PCH) { 5003 BitstreamCursor Stream(PCH); 5004 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5005 // FIXME this drops the error on the floor. 5006 consumeError(std::move(Err)); 5007 return ASTFileSignature(); 5008 } 5009 5010 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 5011 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 5012 return ASTFileSignature(); 5013 5014 // Scan for SIGNATURE inside the diagnostic options block. 5015 ASTReader::RecordData Record; 5016 while (true) { 5017 Expected<llvm::BitstreamEntry> MaybeEntry = 5018 Stream.advanceSkippingSubblocks(); 5019 if (!MaybeEntry) { 5020 // FIXME this drops the error on the floor. 5021 consumeError(MaybeEntry.takeError()); 5022 return ASTFileSignature(); 5023 } 5024 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5025 5026 if (Entry.Kind != llvm::BitstreamEntry::Record) 5027 return ASTFileSignature(); 5028 5029 Record.clear(); 5030 StringRef Blob; 5031 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 5032 if (!MaybeRecord) { 5033 // FIXME this drops the error on the floor. 5034 consumeError(MaybeRecord.takeError()); 5035 return ASTFileSignature(); 5036 } 5037 if (SIGNATURE == MaybeRecord.get()) 5038 return ASTFileSignature::create(Record.begin(), 5039 Record.begin() + ASTFileSignature::size); 5040 } 5041 } 5042 5043 /// Retrieve the name of the original source file name 5044 /// directly from the AST file, without actually loading the AST 5045 /// file. 5046 std::string ASTReader::getOriginalSourceFile( 5047 const std::string &ASTFileName, FileManager &FileMgr, 5048 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) { 5049 // Open the AST file. 5050 auto Buffer = FileMgr.getBufferForFile(ASTFileName); 5051 if (!Buffer) { 5052 Diags.Report(diag::err_fe_unable_to_read_pch_file) 5053 << ASTFileName << Buffer.getError().message(); 5054 return std::string(); 5055 } 5056 5057 // Initialize the stream 5058 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer)); 5059 5060 // Sniff for the signature. 5061 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5062 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err); 5063 return std::string(); 5064 } 5065 5066 // Scan for the CONTROL_BLOCK_ID block. 5067 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) { 5068 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 5069 return std::string(); 5070 } 5071 5072 // Scan for ORIGINAL_FILE inside the control block. 5073 RecordData Record; 5074 while (true) { 5075 Expected<llvm::BitstreamEntry> MaybeEntry = 5076 Stream.advanceSkippingSubblocks(); 5077 if (!MaybeEntry) { 5078 // FIXME this drops errors on the floor. 5079 consumeError(MaybeEntry.takeError()); 5080 return std::string(); 5081 } 5082 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5083 5084 if (Entry.Kind == llvm::BitstreamEntry::EndBlock) 5085 return std::string(); 5086 5087 if (Entry.Kind != llvm::BitstreamEntry::Record) { 5088 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 5089 return std::string(); 5090 } 5091 5092 Record.clear(); 5093 StringRef Blob; 5094 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 5095 if (!MaybeRecord) { 5096 // FIXME this drops the errors on the floor. 5097 consumeError(MaybeRecord.takeError()); 5098 return std::string(); 5099 } 5100 if (ORIGINAL_FILE == MaybeRecord.get()) 5101 return Blob.str(); 5102 } 5103 } 5104 5105 namespace { 5106 5107 class SimplePCHValidator : public ASTReaderListener { 5108 const LangOptions &ExistingLangOpts; 5109 const TargetOptions &ExistingTargetOpts; 5110 const PreprocessorOptions &ExistingPPOpts; 5111 std::string ExistingModuleCachePath; 5112 FileManager &FileMgr; 5113 5114 public: 5115 SimplePCHValidator(const LangOptions &ExistingLangOpts, 5116 const TargetOptions &ExistingTargetOpts, 5117 const PreprocessorOptions &ExistingPPOpts, 5118 StringRef ExistingModuleCachePath, FileManager &FileMgr) 5119 : ExistingLangOpts(ExistingLangOpts), 5120 ExistingTargetOpts(ExistingTargetOpts), 5121 ExistingPPOpts(ExistingPPOpts), 5122 ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {} 5123 5124 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain, 5125 bool AllowCompatibleDifferences) override { 5126 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr, 5127 AllowCompatibleDifferences); 5128 } 5129 5130 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain, 5131 bool AllowCompatibleDifferences) override { 5132 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr, 5133 AllowCompatibleDifferences); 5134 } 5135 5136 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 5137 StringRef SpecificModuleCachePath, 5138 bool Complain) override { 5139 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5140 ExistingModuleCachePath, 5141 nullptr, ExistingLangOpts); 5142 } 5143 5144 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 5145 bool Complain, 5146 std::string &SuggestedPredefines) override { 5147 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr, 5148 SuggestedPredefines, ExistingLangOpts); 5149 } 5150 }; 5151 5152 } // namespace 5153 5154 bool ASTReader::readASTFileControlBlock( 5155 StringRef Filename, FileManager &FileMgr, 5156 const PCHContainerReader &PCHContainerRdr, 5157 bool FindModuleFileExtensions, 5158 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) { 5159 // Open the AST file. 5160 // FIXME: This allows use of the VFS; we do not allow use of the 5161 // VFS when actually loading a module. 5162 auto Buffer = FileMgr.getBufferForFile(Filename); 5163 if (!Buffer) { 5164 return true; 5165 } 5166 5167 // Initialize the stream 5168 StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer); 5169 BitstreamCursor Stream(Bytes); 5170 5171 // Sniff for the signature. 5172 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5173 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 5174 return true; 5175 } 5176 5177 // Scan for the CONTROL_BLOCK_ID block. 5178 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) 5179 return true; 5180 5181 bool NeedsInputFiles = Listener.needsInputFileVisitation(); 5182 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation(); 5183 bool NeedsImports = Listener.needsImportVisitation(); 5184 BitstreamCursor InputFilesCursor; 5185 5186 RecordData Record; 5187 std::string ModuleDir; 5188 bool DoneWithControlBlock = false; 5189 while (!DoneWithControlBlock) { 5190 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5191 if (!MaybeEntry) { 5192 // FIXME this drops the error on the floor. 5193 consumeError(MaybeEntry.takeError()); 5194 return true; 5195 } 5196 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5197 5198 switch (Entry.Kind) { 5199 case llvm::BitstreamEntry::SubBlock: { 5200 switch (Entry.ID) { 5201 case OPTIONS_BLOCK_ID: { 5202 std::string IgnoredSuggestedPredefines; 5203 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate, 5204 /*AllowCompatibleConfigurationMismatch*/ false, 5205 Listener, IgnoredSuggestedPredefines) != Success) 5206 return true; 5207 break; 5208 } 5209 5210 case INPUT_FILES_BLOCK_ID: 5211 InputFilesCursor = Stream; 5212 if (llvm::Error Err = Stream.SkipBlock()) { 5213 // FIXME this drops the error on the floor. 5214 consumeError(std::move(Err)); 5215 return true; 5216 } 5217 if (NeedsInputFiles && 5218 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID)) 5219 return true; 5220 break; 5221 5222 default: 5223 if (llvm::Error Err = Stream.SkipBlock()) { 5224 // FIXME this drops the error on the floor. 5225 consumeError(std::move(Err)); 5226 return true; 5227 } 5228 break; 5229 } 5230 5231 continue; 5232 } 5233 5234 case llvm::BitstreamEntry::EndBlock: 5235 DoneWithControlBlock = true; 5236 break; 5237 5238 case llvm::BitstreamEntry::Error: 5239 return true; 5240 5241 case llvm::BitstreamEntry::Record: 5242 break; 5243 } 5244 5245 if (DoneWithControlBlock) break; 5246 5247 Record.clear(); 5248 StringRef Blob; 5249 Expected<unsigned> MaybeRecCode = 5250 Stream.readRecord(Entry.ID, Record, &Blob); 5251 if (!MaybeRecCode) { 5252 // FIXME this drops the error. 5253 return Failure; 5254 } 5255 switch ((ControlRecordTypes)MaybeRecCode.get()) { 5256 case METADATA: 5257 if (Record[0] != VERSION_MAJOR) 5258 return true; 5259 if (Listener.ReadFullVersionInformation(Blob)) 5260 return true; 5261 break; 5262 case MODULE_NAME: 5263 Listener.ReadModuleName(Blob); 5264 break; 5265 case MODULE_DIRECTORY: 5266 ModuleDir = std::string(Blob); 5267 break; 5268 case MODULE_MAP_FILE: { 5269 unsigned Idx = 0; 5270 auto Path = ReadString(Record, Idx); 5271 ResolveImportedPath(Path, ModuleDir); 5272 Listener.ReadModuleMapFile(Path); 5273 break; 5274 } 5275 case INPUT_FILE_OFFSETS: { 5276 if (!NeedsInputFiles) 5277 break; 5278 5279 unsigned NumInputFiles = Record[0]; 5280 unsigned NumUserFiles = Record[1]; 5281 const llvm::support::unaligned_uint64_t *InputFileOffs = 5282 (const llvm::support::unaligned_uint64_t *)Blob.data(); 5283 for (unsigned I = 0; I != NumInputFiles; ++I) { 5284 // Go find this input file. 5285 bool isSystemFile = I >= NumUserFiles; 5286 5287 if (isSystemFile && !NeedsSystemInputFiles) 5288 break; // the rest are system input files 5289 5290 BitstreamCursor &Cursor = InputFilesCursor; 5291 SavedStreamPosition SavedPosition(Cursor); 5292 if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) { 5293 // FIXME this drops errors on the floor. 5294 consumeError(std::move(Err)); 5295 } 5296 5297 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 5298 if (!MaybeCode) { 5299 // FIXME this drops errors on the floor. 5300 consumeError(MaybeCode.takeError()); 5301 } 5302 unsigned Code = MaybeCode.get(); 5303 5304 RecordData Record; 5305 StringRef Blob; 5306 bool shouldContinue = false; 5307 Expected<unsigned> MaybeRecordType = 5308 Cursor.readRecord(Code, Record, &Blob); 5309 if (!MaybeRecordType) { 5310 // FIXME this drops errors on the floor. 5311 consumeError(MaybeRecordType.takeError()); 5312 } 5313 switch ((InputFileRecordTypes)MaybeRecordType.get()) { 5314 case INPUT_FILE_HASH: 5315 break; 5316 case INPUT_FILE: 5317 bool Overridden = static_cast<bool>(Record[3]); 5318 std::string Filename = std::string(Blob); 5319 ResolveImportedPath(Filename, ModuleDir); 5320 shouldContinue = Listener.visitInputFile( 5321 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false); 5322 break; 5323 } 5324 if (!shouldContinue) 5325 break; 5326 } 5327 break; 5328 } 5329 5330 case IMPORTS: { 5331 if (!NeedsImports) 5332 break; 5333 5334 unsigned Idx = 0, N = Record.size(); 5335 while (Idx < N) { 5336 // Read information about the AST file. 5337 Idx += 5338 1 + 1 + 1 + 1 + 5339 ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature 5340 std::string ModuleName = ReadString(Record, Idx); 5341 std::string Filename = ReadString(Record, Idx); 5342 ResolveImportedPath(Filename, ModuleDir); 5343 Listener.visitImport(ModuleName, Filename); 5344 } 5345 break; 5346 } 5347 5348 default: 5349 // No other validation to perform. 5350 break; 5351 } 5352 } 5353 5354 // Look for module file extension blocks, if requested. 5355 if (FindModuleFileExtensions) { 5356 BitstreamCursor SavedStream = Stream; 5357 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) { 5358 bool DoneWithExtensionBlock = false; 5359 while (!DoneWithExtensionBlock) { 5360 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5361 if (!MaybeEntry) { 5362 // FIXME this drops the error. 5363 return true; 5364 } 5365 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5366 5367 switch (Entry.Kind) { 5368 case llvm::BitstreamEntry::SubBlock: 5369 if (llvm::Error Err = Stream.SkipBlock()) { 5370 // FIXME this drops the error on the floor. 5371 consumeError(std::move(Err)); 5372 return true; 5373 } 5374 continue; 5375 5376 case llvm::BitstreamEntry::EndBlock: 5377 DoneWithExtensionBlock = true; 5378 continue; 5379 5380 case llvm::BitstreamEntry::Error: 5381 return true; 5382 5383 case llvm::BitstreamEntry::Record: 5384 break; 5385 } 5386 5387 Record.clear(); 5388 StringRef Blob; 5389 Expected<unsigned> MaybeRecCode = 5390 Stream.readRecord(Entry.ID, Record, &Blob); 5391 if (!MaybeRecCode) { 5392 // FIXME this drops the error. 5393 return true; 5394 } 5395 switch (MaybeRecCode.get()) { 5396 case EXTENSION_METADATA: { 5397 ModuleFileExtensionMetadata Metadata; 5398 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) 5399 return true; 5400 5401 Listener.readModuleFileExtension(Metadata); 5402 break; 5403 } 5404 } 5405 } 5406 } 5407 Stream = SavedStream; 5408 } 5409 5410 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 5411 if (readUnhashedControlBlockImpl( 5412 nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate, 5413 /*AllowCompatibleConfigurationMismatch*/ false, &Listener, 5414 ValidateDiagnosticOptions) != Success) 5415 return true; 5416 5417 return false; 5418 } 5419 5420 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr, 5421 const PCHContainerReader &PCHContainerRdr, 5422 const LangOptions &LangOpts, 5423 const TargetOptions &TargetOpts, 5424 const PreprocessorOptions &PPOpts, 5425 StringRef ExistingModuleCachePath) { 5426 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, 5427 ExistingModuleCachePath, FileMgr); 5428 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr, 5429 /*FindModuleFileExtensions=*/false, 5430 validator, 5431 /*ValidateDiagnosticOptions=*/true); 5432 } 5433 5434 ASTReader::ASTReadResult 5435 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) { 5436 // Enter the submodule block. 5437 if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) { 5438 Error(std::move(Err)); 5439 return Failure; 5440 } 5441 5442 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap(); 5443 bool First = true; 5444 Module *CurrentModule = nullptr; 5445 RecordData Record; 5446 while (true) { 5447 Expected<llvm::BitstreamEntry> MaybeEntry = 5448 F.Stream.advanceSkippingSubblocks(); 5449 if (!MaybeEntry) { 5450 Error(MaybeEntry.takeError()); 5451 return Failure; 5452 } 5453 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5454 5455 switch (Entry.Kind) { 5456 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 5457 case llvm::BitstreamEntry::Error: 5458 Error("malformed block record in AST file"); 5459 return Failure; 5460 case llvm::BitstreamEntry::EndBlock: 5461 return Success; 5462 case llvm::BitstreamEntry::Record: 5463 // The interesting case. 5464 break; 5465 } 5466 5467 // Read a record. 5468 StringRef Blob; 5469 Record.clear(); 5470 Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob); 5471 if (!MaybeKind) { 5472 Error(MaybeKind.takeError()); 5473 return Failure; 5474 } 5475 unsigned Kind = MaybeKind.get(); 5476 5477 if ((Kind == SUBMODULE_METADATA) != First) { 5478 Error("submodule metadata record should be at beginning of block"); 5479 return Failure; 5480 } 5481 First = false; 5482 5483 // Submodule information is only valid if we have a current module. 5484 // FIXME: Should we error on these cases? 5485 if (!CurrentModule && Kind != SUBMODULE_METADATA && 5486 Kind != SUBMODULE_DEFINITION) 5487 continue; 5488 5489 switch (Kind) { 5490 default: // Default behavior: ignore. 5491 break; 5492 5493 case SUBMODULE_DEFINITION: { 5494 if (Record.size() < 12) { 5495 Error("malformed module definition"); 5496 return Failure; 5497 } 5498 5499 StringRef Name = Blob; 5500 unsigned Idx = 0; 5501 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]); 5502 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]); 5503 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++]; 5504 bool IsFramework = Record[Idx++]; 5505 bool IsExplicit = Record[Idx++]; 5506 bool IsSystem = Record[Idx++]; 5507 bool IsExternC = Record[Idx++]; 5508 bool InferSubmodules = Record[Idx++]; 5509 bool InferExplicitSubmodules = Record[Idx++]; 5510 bool InferExportWildcard = Record[Idx++]; 5511 bool ConfigMacrosExhaustive = Record[Idx++]; 5512 bool ModuleMapIsPrivate = Record[Idx++]; 5513 5514 Module *ParentModule = nullptr; 5515 if (Parent) 5516 ParentModule = getSubmodule(Parent); 5517 5518 // Retrieve this (sub)module from the module map, creating it if 5519 // necessary. 5520 CurrentModule = 5521 ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit) 5522 .first; 5523 5524 // FIXME: set the definition loc for CurrentModule, or call 5525 // ModMap.setInferredModuleAllowedBy() 5526 5527 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS; 5528 if (GlobalIndex >= SubmodulesLoaded.size() || 5529 SubmodulesLoaded[GlobalIndex]) { 5530 Error("too many submodules"); 5531 return Failure; 5532 } 5533 5534 if (!ParentModule) { 5535 if (const FileEntry *CurFile = CurrentModule->getASTFile()) { 5536 // Don't emit module relocation error if we have -fno-validate-pch 5537 if (!PP.getPreprocessorOpts().DisablePCHValidation && 5538 CurFile != F.File) { 5539 Error(diag::err_module_file_conflict, 5540 CurrentModule->getTopLevelModuleName(), CurFile->getName(), 5541 F.File->getName()); 5542 return Failure; 5543 } 5544 } 5545 5546 F.DidReadTopLevelSubmodule = true; 5547 CurrentModule->setASTFile(F.File); 5548 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath; 5549 } 5550 5551 CurrentModule->Kind = Kind; 5552 CurrentModule->Signature = F.Signature; 5553 CurrentModule->IsFromModuleFile = true; 5554 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem; 5555 CurrentModule->IsExternC = IsExternC; 5556 CurrentModule->InferSubmodules = InferSubmodules; 5557 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules; 5558 CurrentModule->InferExportWildcard = InferExportWildcard; 5559 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive; 5560 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate; 5561 if (DeserializationListener) 5562 DeserializationListener->ModuleRead(GlobalID, CurrentModule); 5563 5564 SubmodulesLoaded[GlobalIndex] = CurrentModule; 5565 5566 // Clear out data that will be replaced by what is in the module file. 5567 CurrentModule->LinkLibraries.clear(); 5568 CurrentModule->ConfigMacros.clear(); 5569 CurrentModule->UnresolvedConflicts.clear(); 5570 CurrentModule->Conflicts.clear(); 5571 5572 // The module is available unless it's missing a requirement; relevant 5573 // requirements will be (re-)added by SUBMODULE_REQUIRES records. 5574 // Missing headers that were present when the module was built do not 5575 // make it unavailable -- if we got this far, this must be an explicitly 5576 // imported module file. 5577 CurrentModule->Requirements.clear(); 5578 CurrentModule->MissingHeaders.clear(); 5579 CurrentModule->IsUnimportable = 5580 ParentModule && ParentModule->IsUnimportable; 5581 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable; 5582 break; 5583 } 5584 5585 case SUBMODULE_UMBRELLA_HEADER: { 5586 std::string Filename = std::string(Blob); 5587 ResolveImportedPath(F, Filename); 5588 if (auto Umbrella = PP.getFileManager().getFile(Filename)) { 5589 if (!CurrentModule->getUmbrellaHeader()) 5590 ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob); 5591 else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) { 5592 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 5593 Error("mismatched umbrella headers in submodule"); 5594 return OutOfDate; 5595 } 5596 } 5597 break; 5598 } 5599 5600 case SUBMODULE_HEADER: 5601 case SUBMODULE_EXCLUDED_HEADER: 5602 case SUBMODULE_PRIVATE_HEADER: 5603 // We lazily associate headers with their modules via the HeaderInfo table. 5604 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead 5605 // of complete filenames or remove it entirely. 5606 break; 5607 5608 case SUBMODULE_TEXTUAL_HEADER: 5609 case SUBMODULE_PRIVATE_TEXTUAL_HEADER: 5610 // FIXME: Textual headers are not marked in the HeaderInfo table. Load 5611 // them here. 5612 break; 5613 5614 case SUBMODULE_TOPHEADER: 5615 CurrentModule->addTopHeaderFilename(Blob); 5616 break; 5617 5618 case SUBMODULE_UMBRELLA_DIR: { 5619 std::string Dirname = std::string(Blob); 5620 ResolveImportedPath(F, Dirname); 5621 if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) { 5622 if (!CurrentModule->getUmbrellaDir()) 5623 ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob); 5624 else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) { 5625 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 5626 Error("mismatched umbrella directories in submodule"); 5627 return OutOfDate; 5628 } 5629 } 5630 break; 5631 } 5632 5633 case SUBMODULE_METADATA: { 5634 F.BaseSubmoduleID = getTotalNumSubmodules(); 5635 F.LocalNumSubmodules = Record[0]; 5636 unsigned LocalBaseSubmoduleID = Record[1]; 5637 if (F.LocalNumSubmodules > 0) { 5638 // Introduce the global -> local mapping for submodules within this 5639 // module. 5640 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F)); 5641 5642 // Introduce the local -> global mapping for submodules within this 5643 // module. 5644 F.SubmoduleRemap.insertOrReplace( 5645 std::make_pair(LocalBaseSubmoduleID, 5646 F.BaseSubmoduleID - LocalBaseSubmoduleID)); 5647 5648 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules); 5649 } 5650 break; 5651 } 5652 5653 case SUBMODULE_IMPORTS: 5654 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) { 5655 UnresolvedModuleRef Unresolved; 5656 Unresolved.File = &F; 5657 Unresolved.Mod = CurrentModule; 5658 Unresolved.ID = Record[Idx]; 5659 Unresolved.Kind = UnresolvedModuleRef::Import; 5660 Unresolved.IsWildcard = false; 5661 UnresolvedModuleRefs.push_back(Unresolved); 5662 } 5663 break; 5664 5665 case SUBMODULE_EXPORTS: 5666 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) { 5667 UnresolvedModuleRef Unresolved; 5668 Unresolved.File = &F; 5669 Unresolved.Mod = CurrentModule; 5670 Unresolved.ID = Record[Idx]; 5671 Unresolved.Kind = UnresolvedModuleRef::Export; 5672 Unresolved.IsWildcard = Record[Idx + 1]; 5673 UnresolvedModuleRefs.push_back(Unresolved); 5674 } 5675 5676 // Once we've loaded the set of exports, there's no reason to keep 5677 // the parsed, unresolved exports around. 5678 CurrentModule->UnresolvedExports.clear(); 5679 break; 5680 5681 case SUBMODULE_REQUIRES: 5682 CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(), 5683 PP.getTargetInfo()); 5684 break; 5685 5686 case SUBMODULE_LINK_LIBRARY: 5687 ModMap.resolveLinkAsDependencies(CurrentModule); 5688 CurrentModule->LinkLibraries.push_back( 5689 Module::LinkLibrary(std::string(Blob), Record[0])); 5690 break; 5691 5692 case SUBMODULE_CONFIG_MACRO: 5693 CurrentModule->ConfigMacros.push_back(Blob.str()); 5694 break; 5695 5696 case SUBMODULE_CONFLICT: { 5697 UnresolvedModuleRef Unresolved; 5698 Unresolved.File = &F; 5699 Unresolved.Mod = CurrentModule; 5700 Unresolved.ID = Record[0]; 5701 Unresolved.Kind = UnresolvedModuleRef::Conflict; 5702 Unresolved.IsWildcard = false; 5703 Unresolved.String = Blob; 5704 UnresolvedModuleRefs.push_back(Unresolved); 5705 break; 5706 } 5707 5708 case SUBMODULE_INITIALIZERS: { 5709 if (!ContextObj) 5710 break; 5711 SmallVector<uint32_t, 16> Inits; 5712 for (auto &ID : Record) 5713 Inits.push_back(getGlobalDeclID(F, ID)); 5714 ContextObj->addLazyModuleInitializers(CurrentModule, Inits); 5715 break; 5716 } 5717 5718 case SUBMODULE_EXPORT_AS: 5719 CurrentModule->ExportAsModule = Blob.str(); 5720 ModMap.addLinkAsDependency(CurrentModule); 5721 break; 5722 } 5723 } 5724 } 5725 5726 /// Parse the record that corresponds to a LangOptions data 5727 /// structure. 5728 /// 5729 /// This routine parses the language options from the AST file and then gives 5730 /// them to the AST listener if one is set. 5731 /// 5732 /// \returns true if the listener deems the file unacceptable, false otherwise. 5733 bool ASTReader::ParseLanguageOptions(const RecordData &Record, 5734 bool Complain, 5735 ASTReaderListener &Listener, 5736 bool AllowCompatibleDifferences) { 5737 LangOptions LangOpts; 5738 unsigned Idx = 0; 5739 #define LANGOPT(Name, Bits, Default, Description) \ 5740 LangOpts.Name = Record[Idx++]; 5741 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 5742 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++])); 5743 #include "clang/Basic/LangOptions.def" 5744 #define SANITIZER(NAME, ID) \ 5745 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]); 5746 #include "clang/Basic/Sanitizers.def" 5747 5748 for (unsigned N = Record[Idx++]; N; --N) 5749 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx)); 5750 5751 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++]; 5752 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx); 5753 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion); 5754 5755 LangOpts.CurrentModule = ReadString(Record, Idx); 5756 5757 // Comment options. 5758 for (unsigned N = Record[Idx++]; N; --N) { 5759 LangOpts.CommentOpts.BlockCommandNames.push_back( 5760 ReadString(Record, Idx)); 5761 } 5762 LangOpts.CommentOpts.ParseAllComments = Record[Idx++]; 5763 5764 // OpenMP offloading options. 5765 for (unsigned N = Record[Idx++]; N; --N) { 5766 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx))); 5767 } 5768 5769 LangOpts.OMPHostIRFile = ReadString(Record, Idx); 5770 5771 return Listener.ReadLanguageOptions(LangOpts, Complain, 5772 AllowCompatibleDifferences); 5773 } 5774 5775 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain, 5776 ASTReaderListener &Listener, 5777 bool AllowCompatibleDifferences) { 5778 unsigned Idx = 0; 5779 TargetOptions TargetOpts; 5780 TargetOpts.Triple = ReadString(Record, Idx); 5781 TargetOpts.CPU = ReadString(Record, Idx); 5782 TargetOpts.ABI = ReadString(Record, Idx); 5783 for (unsigned N = Record[Idx++]; N; --N) { 5784 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx)); 5785 } 5786 for (unsigned N = Record[Idx++]; N; --N) { 5787 TargetOpts.Features.push_back(ReadString(Record, Idx)); 5788 } 5789 5790 return Listener.ReadTargetOptions(TargetOpts, Complain, 5791 AllowCompatibleDifferences); 5792 } 5793 5794 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain, 5795 ASTReaderListener &Listener) { 5796 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions); 5797 unsigned Idx = 0; 5798 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++]; 5799 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \ 5800 DiagOpts->set##Name(static_cast<Type>(Record[Idx++])); 5801 #include "clang/Basic/DiagnosticOptions.def" 5802 5803 for (unsigned N = Record[Idx++]; N; --N) 5804 DiagOpts->Warnings.push_back(ReadString(Record, Idx)); 5805 for (unsigned N = Record[Idx++]; N; --N) 5806 DiagOpts->Remarks.push_back(ReadString(Record, Idx)); 5807 5808 return Listener.ReadDiagnosticOptions(DiagOpts, Complain); 5809 } 5810 5811 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain, 5812 ASTReaderListener &Listener) { 5813 FileSystemOptions FSOpts; 5814 unsigned Idx = 0; 5815 FSOpts.WorkingDir = ReadString(Record, Idx); 5816 return Listener.ReadFileSystemOptions(FSOpts, Complain); 5817 } 5818 5819 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record, 5820 bool Complain, 5821 ASTReaderListener &Listener) { 5822 HeaderSearchOptions HSOpts; 5823 unsigned Idx = 0; 5824 HSOpts.Sysroot = ReadString(Record, Idx); 5825 5826 // Include entries. 5827 for (unsigned N = Record[Idx++]; N; --N) { 5828 std::string Path = ReadString(Record, Idx); 5829 frontend::IncludeDirGroup Group 5830 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]); 5831 bool IsFramework = Record[Idx++]; 5832 bool IgnoreSysRoot = Record[Idx++]; 5833 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework, 5834 IgnoreSysRoot); 5835 } 5836 5837 // System header prefixes. 5838 for (unsigned N = Record[Idx++]; N; --N) { 5839 std::string Prefix = ReadString(Record, Idx); 5840 bool IsSystemHeader = Record[Idx++]; 5841 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader); 5842 } 5843 5844 HSOpts.ResourceDir = ReadString(Record, Idx); 5845 HSOpts.ModuleCachePath = ReadString(Record, Idx); 5846 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx); 5847 HSOpts.DisableModuleHash = Record[Idx++]; 5848 HSOpts.ImplicitModuleMaps = Record[Idx++]; 5849 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++]; 5850 HSOpts.UseBuiltinIncludes = Record[Idx++]; 5851 HSOpts.UseStandardSystemIncludes = Record[Idx++]; 5852 HSOpts.UseStandardCXXIncludes = Record[Idx++]; 5853 HSOpts.UseLibcxx = Record[Idx++]; 5854 std::string SpecificModuleCachePath = ReadString(Record, Idx); 5855 5856 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5857 Complain); 5858 } 5859 5860 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record, 5861 bool Complain, 5862 ASTReaderListener &Listener, 5863 std::string &SuggestedPredefines) { 5864 PreprocessorOptions PPOpts; 5865 unsigned Idx = 0; 5866 5867 // Macro definitions/undefs 5868 for (unsigned N = Record[Idx++]; N; --N) { 5869 std::string Macro = ReadString(Record, Idx); 5870 bool IsUndef = Record[Idx++]; 5871 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef)); 5872 } 5873 5874 // Includes 5875 for (unsigned N = Record[Idx++]; N; --N) { 5876 PPOpts.Includes.push_back(ReadString(Record, Idx)); 5877 } 5878 5879 // Macro Includes 5880 for (unsigned N = Record[Idx++]; N; --N) { 5881 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx)); 5882 } 5883 5884 PPOpts.UsePredefines = Record[Idx++]; 5885 PPOpts.DetailedRecord = Record[Idx++]; 5886 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx); 5887 PPOpts.ObjCXXARCStandardLibrary = 5888 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]); 5889 SuggestedPredefines.clear(); 5890 return Listener.ReadPreprocessorOptions(PPOpts, Complain, 5891 SuggestedPredefines); 5892 } 5893 5894 std::pair<ModuleFile *, unsigned> 5895 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) { 5896 GlobalPreprocessedEntityMapType::iterator 5897 I = GlobalPreprocessedEntityMap.find(GlobalIndex); 5898 assert(I != GlobalPreprocessedEntityMap.end() && 5899 "Corrupted global preprocessed entity map"); 5900 ModuleFile *M = I->second; 5901 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID; 5902 return std::make_pair(M, LocalIndex); 5903 } 5904 5905 llvm::iterator_range<PreprocessingRecord::iterator> 5906 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const { 5907 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord()) 5908 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID, 5909 Mod.NumPreprocessedEntities); 5910 5911 return llvm::make_range(PreprocessingRecord::iterator(), 5912 PreprocessingRecord::iterator()); 5913 } 5914 5915 llvm::iterator_range<ASTReader::ModuleDeclIterator> 5916 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) { 5917 return llvm::make_range( 5918 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls), 5919 ModuleDeclIterator(this, &Mod, 5920 Mod.FileSortedDecls + Mod.NumFileSortedDecls)); 5921 } 5922 5923 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) { 5924 auto I = GlobalSkippedRangeMap.find(GlobalIndex); 5925 assert(I != GlobalSkippedRangeMap.end() && 5926 "Corrupted global skipped range map"); 5927 ModuleFile *M = I->second; 5928 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID; 5929 assert(LocalIndex < M->NumPreprocessedSkippedRanges); 5930 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex]; 5931 SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()), 5932 TranslateSourceLocation(*M, RawRange.getEnd())); 5933 assert(Range.isValid()); 5934 return Range; 5935 } 5936 5937 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) { 5938 PreprocessedEntityID PPID = Index+1; 5939 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 5940 ModuleFile &M = *PPInfo.first; 5941 unsigned LocalIndex = PPInfo.second; 5942 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 5943 5944 if (!PP.getPreprocessingRecord()) { 5945 Error("no preprocessing record"); 5946 return nullptr; 5947 } 5948 5949 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor); 5950 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit( 5951 M.MacroOffsetsBase + PPOffs.BitOffset)) { 5952 Error(std::move(Err)); 5953 return nullptr; 5954 } 5955 5956 Expected<llvm::BitstreamEntry> MaybeEntry = 5957 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 5958 if (!MaybeEntry) { 5959 Error(MaybeEntry.takeError()); 5960 return nullptr; 5961 } 5962 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5963 5964 if (Entry.Kind != llvm::BitstreamEntry::Record) 5965 return nullptr; 5966 5967 // Read the record. 5968 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()), 5969 TranslateSourceLocation(M, PPOffs.getEnd())); 5970 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 5971 StringRef Blob; 5972 RecordData Record; 5973 Expected<unsigned> MaybeRecType = 5974 M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob); 5975 if (!MaybeRecType) { 5976 Error(MaybeRecType.takeError()); 5977 return nullptr; 5978 } 5979 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) { 5980 case PPD_MACRO_EXPANSION: { 5981 bool isBuiltin = Record[0]; 5982 IdentifierInfo *Name = nullptr; 5983 MacroDefinitionRecord *Def = nullptr; 5984 if (isBuiltin) 5985 Name = getLocalIdentifier(M, Record[1]); 5986 else { 5987 PreprocessedEntityID GlobalID = 5988 getGlobalPreprocessedEntityID(M, Record[1]); 5989 Def = cast<MacroDefinitionRecord>( 5990 PPRec.getLoadedPreprocessedEntity(GlobalID - 1)); 5991 } 5992 5993 MacroExpansion *ME; 5994 if (isBuiltin) 5995 ME = new (PPRec) MacroExpansion(Name, Range); 5996 else 5997 ME = new (PPRec) MacroExpansion(Def, Range); 5998 5999 return ME; 6000 } 6001 6002 case PPD_MACRO_DEFINITION: { 6003 // Decode the identifier info and then check again; if the macro is 6004 // still defined and associated with the identifier, 6005 IdentifierInfo *II = getLocalIdentifier(M, Record[0]); 6006 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range); 6007 6008 if (DeserializationListener) 6009 DeserializationListener->MacroDefinitionRead(PPID, MD); 6010 6011 return MD; 6012 } 6013 6014 case PPD_INCLUSION_DIRECTIVE: { 6015 const char *FullFileNameStart = Blob.data() + Record[0]; 6016 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]); 6017 const FileEntry *File = nullptr; 6018 if (!FullFileName.empty()) 6019 if (auto FE = PP.getFileManager().getFile(FullFileName)) 6020 File = *FE; 6021 6022 // FIXME: Stable encoding 6023 InclusionDirective::InclusionKind Kind 6024 = static_cast<InclusionDirective::InclusionKind>(Record[2]); 6025 InclusionDirective *ID 6026 = new (PPRec) InclusionDirective(PPRec, Kind, 6027 StringRef(Blob.data(), Record[0]), 6028 Record[1], Record[3], 6029 File, 6030 Range); 6031 return ID; 6032 } 6033 } 6034 6035 llvm_unreachable("Invalid PreprocessorDetailRecordTypes"); 6036 } 6037 6038 /// Find the next module that contains entities and return the ID 6039 /// of the first entry. 6040 /// 6041 /// \param SLocMapI points at a chunk of a module that contains no 6042 /// preprocessed entities or the entities it contains are not the ones we are 6043 /// looking for. 6044 PreprocessedEntityID ASTReader::findNextPreprocessedEntity( 6045 GlobalSLocOffsetMapType::const_iterator SLocMapI) const { 6046 ++SLocMapI; 6047 for (GlobalSLocOffsetMapType::const_iterator 6048 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) { 6049 ModuleFile &M = *SLocMapI->second; 6050 if (M.NumPreprocessedEntities) 6051 return M.BasePreprocessedEntityID; 6052 } 6053 6054 return getTotalNumPreprocessedEntities(); 6055 } 6056 6057 namespace { 6058 6059 struct PPEntityComp { 6060 const ASTReader &Reader; 6061 ModuleFile &M; 6062 6063 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {} 6064 6065 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const { 6066 SourceLocation LHS = getLoc(L); 6067 SourceLocation RHS = getLoc(R); 6068 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6069 } 6070 6071 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const { 6072 SourceLocation LHS = getLoc(L); 6073 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6074 } 6075 6076 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const { 6077 SourceLocation RHS = getLoc(R); 6078 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6079 } 6080 6081 SourceLocation getLoc(const PPEntityOffset &PPE) const { 6082 return Reader.TranslateSourceLocation(M, PPE.getBegin()); 6083 } 6084 }; 6085 6086 } // namespace 6087 6088 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc, 6089 bool EndsAfter) const { 6090 if (SourceMgr.isLocalSourceLocation(Loc)) 6091 return getTotalNumPreprocessedEntities(); 6092 6093 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find( 6094 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1); 6095 assert(SLocMapI != GlobalSLocOffsetMap.end() && 6096 "Corrupted global sloc offset map"); 6097 6098 if (SLocMapI->second->NumPreprocessedEntities == 0) 6099 return findNextPreprocessedEntity(SLocMapI); 6100 6101 ModuleFile &M = *SLocMapI->second; 6102 6103 using pp_iterator = const PPEntityOffset *; 6104 6105 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 6106 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 6107 6108 size_t Count = M.NumPreprocessedEntities; 6109 size_t Half; 6110 pp_iterator First = pp_begin; 6111 pp_iterator PPI; 6112 6113 if (EndsAfter) { 6114 PPI = std::upper_bound(pp_begin, pp_end, Loc, 6115 PPEntityComp(*this, M)); 6116 } else { 6117 // Do a binary search manually instead of using std::lower_bound because 6118 // The end locations of entities may be unordered (when a macro expansion 6119 // is inside another macro argument), but for this case it is not important 6120 // whether we get the first macro expansion or its containing macro. 6121 while (Count > 0) { 6122 Half = Count / 2; 6123 PPI = First; 6124 std::advance(PPI, Half); 6125 if (SourceMgr.isBeforeInTranslationUnit( 6126 TranslateSourceLocation(M, PPI->getEnd()), Loc)) { 6127 First = PPI; 6128 ++First; 6129 Count = Count - Half - 1; 6130 } else 6131 Count = Half; 6132 } 6133 } 6134 6135 if (PPI == pp_end) 6136 return findNextPreprocessedEntity(SLocMapI); 6137 6138 return M.BasePreprocessedEntityID + (PPI - pp_begin); 6139 } 6140 6141 /// Returns a pair of [Begin, End) indices of preallocated 6142 /// preprocessed entities that \arg Range encompasses. 6143 std::pair<unsigned, unsigned> 6144 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) { 6145 if (Range.isInvalid()) 6146 return std::make_pair(0,0); 6147 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); 6148 6149 PreprocessedEntityID BeginID = 6150 findPreprocessedEntity(Range.getBegin(), false); 6151 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true); 6152 return std::make_pair(BeginID, EndID); 6153 } 6154 6155 /// Optionally returns true or false if the preallocated preprocessed 6156 /// entity with index \arg Index came from file \arg FID. 6157 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index, 6158 FileID FID) { 6159 if (FID.isInvalid()) 6160 return false; 6161 6162 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 6163 ModuleFile &M = *PPInfo.first; 6164 unsigned LocalIndex = PPInfo.second; 6165 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 6166 6167 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin()); 6168 if (Loc.isInvalid()) 6169 return false; 6170 6171 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID)) 6172 return true; 6173 else 6174 return false; 6175 } 6176 6177 namespace { 6178 6179 /// Visitor used to search for information about a header file. 6180 class HeaderFileInfoVisitor { 6181 const FileEntry *FE; 6182 Optional<HeaderFileInfo> HFI; 6183 6184 public: 6185 explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {} 6186 6187 bool operator()(ModuleFile &M) { 6188 HeaderFileInfoLookupTable *Table 6189 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable); 6190 if (!Table) 6191 return false; 6192 6193 // Look in the on-disk hash table for an entry for this file name. 6194 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE); 6195 if (Pos == Table->end()) 6196 return false; 6197 6198 HFI = *Pos; 6199 return true; 6200 } 6201 6202 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; } 6203 }; 6204 6205 } // namespace 6206 6207 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) { 6208 HeaderFileInfoVisitor Visitor(FE); 6209 ModuleMgr.visit(Visitor); 6210 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) 6211 return *HFI; 6212 6213 return HeaderFileInfo(); 6214 } 6215 6216 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) { 6217 using DiagState = DiagnosticsEngine::DiagState; 6218 SmallVector<DiagState *, 32> DiagStates; 6219 6220 for (ModuleFile &F : ModuleMgr) { 6221 unsigned Idx = 0; 6222 auto &Record = F.PragmaDiagMappings; 6223 if (Record.empty()) 6224 continue; 6225 6226 DiagStates.clear(); 6227 6228 auto ReadDiagState = 6229 [&](const DiagState &BasedOn, SourceLocation Loc, 6230 bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * { 6231 unsigned BackrefID = Record[Idx++]; 6232 if (BackrefID != 0) 6233 return DiagStates[BackrefID - 1]; 6234 6235 // A new DiagState was created here. 6236 Diag.DiagStates.push_back(BasedOn); 6237 DiagState *NewState = &Diag.DiagStates.back(); 6238 DiagStates.push_back(NewState); 6239 unsigned Size = Record[Idx++]; 6240 assert(Idx + Size * 2 <= Record.size() && 6241 "Invalid data, not enough diag/map pairs"); 6242 while (Size--) { 6243 unsigned DiagID = Record[Idx++]; 6244 DiagnosticMapping NewMapping = 6245 DiagnosticMapping::deserialize(Record[Idx++]); 6246 if (!NewMapping.isPragma() && !IncludeNonPragmaStates) 6247 continue; 6248 6249 DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID); 6250 6251 // If this mapping was specified as a warning but the severity was 6252 // upgraded due to diagnostic settings, simulate the current diagnostic 6253 // settings (and use a warning). 6254 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) { 6255 NewMapping.setSeverity(diag::Severity::Warning); 6256 NewMapping.setUpgradedFromWarning(false); 6257 } 6258 6259 Mapping = NewMapping; 6260 } 6261 return NewState; 6262 }; 6263 6264 // Read the first state. 6265 DiagState *FirstState; 6266 if (F.Kind == MK_ImplicitModule) { 6267 // Implicitly-built modules are reused with different diagnostic 6268 // settings. Use the initial diagnostic state from Diag to simulate this 6269 // compilation's diagnostic settings. 6270 FirstState = Diag.DiagStatesByLoc.FirstDiagState; 6271 DiagStates.push_back(FirstState); 6272 6273 // Skip the initial diagnostic state from the serialized module. 6274 assert(Record[1] == 0 && 6275 "Invalid data, unexpected backref in initial state"); 6276 Idx = 3 + Record[2] * 2; 6277 assert(Idx < Record.size() && 6278 "Invalid data, not enough state change pairs in initial state"); 6279 } else if (F.isModule()) { 6280 // For an explicit module, preserve the flags from the module build 6281 // command line (-w, -Weverything, -Werror, ...) along with any explicit 6282 // -Wblah flags. 6283 unsigned Flags = Record[Idx++]; 6284 DiagState Initial; 6285 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1; 6286 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1; 6287 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1; 6288 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1; 6289 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1; 6290 Initial.ExtBehavior = (diag::Severity)Flags; 6291 FirstState = ReadDiagState(Initial, SourceLocation(), true); 6292 6293 assert(F.OriginalSourceFileID.isValid()); 6294 6295 // Set up the root buffer of the module to start with the initial 6296 // diagnostic state of the module itself, to cover files that contain no 6297 // explicit transitions (for which we did not serialize anything). 6298 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID] 6299 .StateTransitions.push_back({FirstState, 0}); 6300 } else { 6301 // For prefix ASTs, start with whatever the user configured on the 6302 // command line. 6303 Idx++; // Skip flags. 6304 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, 6305 SourceLocation(), false); 6306 } 6307 6308 // Read the state transitions. 6309 unsigned NumLocations = Record[Idx++]; 6310 while (NumLocations--) { 6311 assert(Idx < Record.size() && 6312 "Invalid data, missing pragma diagnostic states"); 6313 SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]); 6314 auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc); 6315 assert(IDAndOffset.first.isValid() && "invalid FileID for transition"); 6316 assert(IDAndOffset.second == 0 && "not a start location for a FileID"); 6317 unsigned Transitions = Record[Idx++]; 6318 6319 // Note that we don't need to set up Parent/ParentOffset here, because 6320 // we won't be changing the diagnostic state within imported FileIDs 6321 // (other than perhaps appending to the main source file, which has no 6322 // parent). 6323 auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first]; 6324 F.StateTransitions.reserve(F.StateTransitions.size() + Transitions); 6325 for (unsigned I = 0; I != Transitions; ++I) { 6326 unsigned Offset = Record[Idx++]; 6327 auto *State = 6328 ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false); 6329 F.StateTransitions.push_back({State, Offset}); 6330 } 6331 } 6332 6333 // Read the final state. 6334 assert(Idx < Record.size() && 6335 "Invalid data, missing final pragma diagnostic state"); 6336 SourceLocation CurStateLoc = 6337 ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]); 6338 auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false); 6339 6340 if (!F.isModule()) { 6341 Diag.DiagStatesByLoc.CurDiagState = CurState; 6342 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc; 6343 6344 // Preserve the property that the imaginary root file describes the 6345 // current state. 6346 FileID NullFile; 6347 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions; 6348 if (T.empty()) 6349 T.push_back({CurState, 0}); 6350 else 6351 T[0].State = CurState; 6352 } 6353 6354 // Don't try to read these mappings again. 6355 Record.clear(); 6356 } 6357 } 6358 6359 /// Get the correct cursor and offset for loading a type. 6360 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) { 6361 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index); 6362 assert(I != GlobalTypeMap.end() && "Corrupted global type map"); 6363 ModuleFile *M = I->second; 6364 return RecordLocation( 6365 M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() + 6366 M->DeclsBlockStartOffset); 6367 } 6368 6369 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) { 6370 switch (code) { 6371 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \ 6372 case TYPE_##CODE_ID: return Type::CLASS_ID; 6373 #include "clang/Serialization/TypeBitCodes.def" 6374 default: return llvm::None; 6375 } 6376 } 6377 6378 /// Read and return the type with the given index.. 6379 /// 6380 /// The index is the type ID, shifted and minus the number of predefs. This 6381 /// routine actually reads the record corresponding to the type at the given 6382 /// location. It is a helper routine for GetType, which deals with reading type 6383 /// IDs. 6384 QualType ASTReader::readTypeRecord(unsigned Index) { 6385 assert(ContextObj && "reading type with no AST context"); 6386 ASTContext &Context = *ContextObj; 6387 RecordLocation Loc = TypeCursorForIndex(Index); 6388 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 6389 6390 // Keep track of where we are in the stream, then jump back there 6391 // after reading this type. 6392 SavedStreamPosition SavedPosition(DeclsCursor); 6393 6394 ReadingKindTracker ReadingKind(Read_Type, *this); 6395 6396 // Note that we are loading a type record. 6397 Deserializing AType(this); 6398 6399 if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) { 6400 Error(std::move(Err)); 6401 return QualType(); 6402 } 6403 Expected<unsigned> RawCode = DeclsCursor.ReadCode(); 6404 if (!RawCode) { 6405 Error(RawCode.takeError()); 6406 return QualType(); 6407 } 6408 6409 ASTRecordReader Record(*this, *Loc.F); 6410 Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get()); 6411 if (!Code) { 6412 Error(Code.takeError()); 6413 return QualType(); 6414 } 6415 if (Code.get() == TYPE_EXT_QUAL) { 6416 QualType baseType = Record.readQualType(); 6417 Qualifiers quals = Record.readQualifiers(); 6418 return Context.getQualifiedType(baseType, quals); 6419 } 6420 6421 auto maybeClass = getTypeClassForCode((TypeCode) Code.get()); 6422 if (!maybeClass) { 6423 Error("Unexpected code for type"); 6424 return QualType(); 6425 } 6426 6427 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record); 6428 return TypeReader.read(*maybeClass); 6429 } 6430 6431 namespace clang { 6432 6433 class TypeLocReader : public TypeLocVisitor<TypeLocReader> { 6434 ASTRecordReader &Reader; 6435 6436 SourceLocation readSourceLocation() { 6437 return Reader.readSourceLocation(); 6438 } 6439 6440 TypeSourceInfo *GetTypeSourceInfo() { 6441 return Reader.readTypeSourceInfo(); 6442 } 6443 6444 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() { 6445 return Reader.readNestedNameSpecifierLoc(); 6446 } 6447 6448 Attr *ReadAttr() { 6449 return Reader.readAttr(); 6450 } 6451 6452 public: 6453 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {} 6454 6455 // We want compile-time assurance that we've enumerated all of 6456 // these, so unfortunately we have to declare them first, then 6457 // define them out-of-line. 6458 #define ABSTRACT_TYPELOC(CLASS, PARENT) 6459 #define TYPELOC(CLASS, PARENT) \ 6460 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 6461 #include "clang/AST/TypeLocNodes.def" 6462 6463 void VisitFunctionTypeLoc(FunctionTypeLoc); 6464 void VisitArrayTypeLoc(ArrayTypeLoc); 6465 }; 6466 6467 } // namespace clang 6468 6469 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 6470 // nothing to do 6471 } 6472 6473 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 6474 TL.setBuiltinLoc(readSourceLocation()); 6475 if (TL.needsExtraLocalData()) { 6476 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt())); 6477 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Reader.readInt())); 6478 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Reader.readInt())); 6479 TL.setModeAttr(Reader.readInt()); 6480 } 6481 } 6482 6483 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { 6484 TL.setNameLoc(readSourceLocation()); 6485 } 6486 6487 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { 6488 TL.setStarLoc(readSourceLocation()); 6489 } 6490 6491 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) { 6492 // nothing to do 6493 } 6494 6495 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { 6496 // nothing to do 6497 } 6498 6499 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) { 6500 TL.setExpansionLoc(readSourceLocation()); 6501 } 6502 6503 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 6504 TL.setCaretLoc(readSourceLocation()); 6505 } 6506 6507 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 6508 TL.setAmpLoc(readSourceLocation()); 6509 } 6510 6511 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 6512 TL.setAmpAmpLoc(readSourceLocation()); 6513 } 6514 6515 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 6516 TL.setStarLoc(readSourceLocation()); 6517 TL.setClassTInfo(GetTypeSourceInfo()); 6518 } 6519 6520 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { 6521 TL.setLBracketLoc(readSourceLocation()); 6522 TL.setRBracketLoc(readSourceLocation()); 6523 if (Reader.readBool()) 6524 TL.setSizeExpr(Reader.readExpr()); 6525 else 6526 TL.setSizeExpr(nullptr); 6527 } 6528 6529 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 6530 VisitArrayTypeLoc(TL); 6531 } 6532 6533 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 6534 VisitArrayTypeLoc(TL); 6535 } 6536 6537 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 6538 VisitArrayTypeLoc(TL); 6539 } 6540 6541 void TypeLocReader::VisitDependentSizedArrayTypeLoc( 6542 DependentSizedArrayTypeLoc TL) { 6543 VisitArrayTypeLoc(TL); 6544 } 6545 6546 void TypeLocReader::VisitDependentAddressSpaceTypeLoc( 6547 DependentAddressSpaceTypeLoc TL) { 6548 6549 TL.setAttrNameLoc(readSourceLocation()); 6550 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6551 TL.setAttrExprOperand(Reader.readExpr()); 6552 } 6553 6554 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( 6555 DependentSizedExtVectorTypeLoc TL) { 6556 TL.setNameLoc(readSourceLocation()); 6557 } 6558 6559 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { 6560 TL.setNameLoc(readSourceLocation()); 6561 } 6562 6563 void TypeLocReader::VisitDependentVectorTypeLoc( 6564 DependentVectorTypeLoc TL) { 6565 TL.setNameLoc(readSourceLocation()); 6566 } 6567 6568 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 6569 TL.setNameLoc(readSourceLocation()); 6570 } 6571 6572 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) { 6573 TL.setAttrNameLoc(readSourceLocation()); 6574 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6575 TL.setAttrRowOperand(Reader.readExpr()); 6576 TL.setAttrColumnOperand(Reader.readExpr()); 6577 } 6578 6579 void TypeLocReader::VisitDependentSizedMatrixTypeLoc( 6580 DependentSizedMatrixTypeLoc TL) { 6581 TL.setAttrNameLoc(readSourceLocation()); 6582 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6583 TL.setAttrRowOperand(Reader.readExpr()); 6584 TL.setAttrColumnOperand(Reader.readExpr()); 6585 } 6586 6587 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 6588 TL.setLocalRangeBegin(readSourceLocation()); 6589 TL.setLParenLoc(readSourceLocation()); 6590 TL.setRParenLoc(readSourceLocation()); 6591 TL.setExceptionSpecRange(Reader.readSourceRange()); 6592 TL.setLocalRangeEnd(readSourceLocation()); 6593 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) { 6594 TL.setParam(i, Reader.readDeclAs<ParmVarDecl>()); 6595 } 6596 } 6597 6598 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 6599 VisitFunctionTypeLoc(TL); 6600 } 6601 6602 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 6603 VisitFunctionTypeLoc(TL); 6604 } 6605 6606 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 6607 TL.setNameLoc(readSourceLocation()); 6608 } 6609 6610 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 6611 TL.setNameLoc(readSourceLocation()); 6612 } 6613 6614 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 6615 TL.setTypeofLoc(readSourceLocation()); 6616 TL.setLParenLoc(readSourceLocation()); 6617 TL.setRParenLoc(readSourceLocation()); 6618 } 6619 6620 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 6621 TL.setTypeofLoc(readSourceLocation()); 6622 TL.setLParenLoc(readSourceLocation()); 6623 TL.setRParenLoc(readSourceLocation()); 6624 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6625 } 6626 6627 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 6628 TL.setNameLoc(readSourceLocation()); 6629 } 6630 6631 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 6632 TL.setKWLoc(readSourceLocation()); 6633 TL.setLParenLoc(readSourceLocation()); 6634 TL.setRParenLoc(readSourceLocation()); 6635 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6636 } 6637 6638 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) { 6639 TL.setNameLoc(readSourceLocation()); 6640 if (Reader.readBool()) { 6641 TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc()); 6642 TL.setTemplateKWLoc(readSourceLocation()); 6643 TL.setConceptNameLoc(readSourceLocation()); 6644 TL.setFoundDecl(Reader.readDeclAs<NamedDecl>()); 6645 TL.setLAngleLoc(readSourceLocation()); 6646 TL.setRAngleLoc(readSourceLocation()); 6647 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6648 TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo( 6649 TL.getTypePtr()->getArg(i).getKind())); 6650 } 6651 } 6652 6653 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc( 6654 DeducedTemplateSpecializationTypeLoc TL) { 6655 TL.setTemplateNameLoc(readSourceLocation()); 6656 } 6657 6658 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { 6659 TL.setNameLoc(readSourceLocation()); 6660 } 6661 6662 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { 6663 TL.setNameLoc(readSourceLocation()); 6664 } 6665 6666 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 6667 TL.setAttr(ReadAttr()); 6668 } 6669 6670 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 6671 TL.setNameLoc(readSourceLocation()); 6672 } 6673 6674 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( 6675 SubstTemplateTypeParmTypeLoc TL) { 6676 TL.setNameLoc(readSourceLocation()); 6677 } 6678 6679 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc( 6680 SubstTemplateTypeParmPackTypeLoc TL) { 6681 TL.setNameLoc(readSourceLocation()); 6682 } 6683 6684 void TypeLocReader::VisitTemplateSpecializationTypeLoc( 6685 TemplateSpecializationTypeLoc TL) { 6686 TL.setTemplateKeywordLoc(readSourceLocation()); 6687 TL.setTemplateNameLoc(readSourceLocation()); 6688 TL.setLAngleLoc(readSourceLocation()); 6689 TL.setRAngleLoc(readSourceLocation()); 6690 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6691 TL.setArgLocInfo( 6692 i, 6693 Reader.readTemplateArgumentLocInfo( 6694 TL.getTypePtr()->getArg(i).getKind())); 6695 } 6696 6697 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) { 6698 TL.setLParenLoc(readSourceLocation()); 6699 TL.setRParenLoc(readSourceLocation()); 6700 } 6701 6702 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 6703 TL.setElaboratedKeywordLoc(readSourceLocation()); 6704 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6705 } 6706 6707 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 6708 TL.setNameLoc(readSourceLocation()); 6709 } 6710 6711 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 6712 TL.setElaboratedKeywordLoc(readSourceLocation()); 6713 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6714 TL.setNameLoc(readSourceLocation()); 6715 } 6716 6717 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( 6718 DependentTemplateSpecializationTypeLoc TL) { 6719 TL.setElaboratedKeywordLoc(readSourceLocation()); 6720 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6721 TL.setTemplateKeywordLoc(readSourceLocation()); 6722 TL.setTemplateNameLoc(readSourceLocation()); 6723 TL.setLAngleLoc(readSourceLocation()); 6724 TL.setRAngleLoc(readSourceLocation()); 6725 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 6726 TL.setArgLocInfo( 6727 I, 6728 Reader.readTemplateArgumentLocInfo( 6729 TL.getTypePtr()->getArg(I).getKind())); 6730 } 6731 6732 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 6733 TL.setEllipsisLoc(readSourceLocation()); 6734 } 6735 6736 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 6737 TL.setNameLoc(readSourceLocation()); 6738 } 6739 6740 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) { 6741 if (TL.getNumProtocols()) { 6742 TL.setProtocolLAngleLoc(readSourceLocation()); 6743 TL.setProtocolRAngleLoc(readSourceLocation()); 6744 } 6745 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6746 TL.setProtocolLoc(i, readSourceLocation()); 6747 } 6748 6749 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 6750 TL.setHasBaseTypeAsWritten(Reader.readBool()); 6751 TL.setTypeArgsLAngleLoc(readSourceLocation()); 6752 TL.setTypeArgsRAngleLoc(readSourceLocation()); 6753 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i) 6754 TL.setTypeArgTInfo(i, GetTypeSourceInfo()); 6755 TL.setProtocolLAngleLoc(readSourceLocation()); 6756 TL.setProtocolRAngleLoc(readSourceLocation()); 6757 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6758 TL.setProtocolLoc(i, readSourceLocation()); 6759 } 6760 6761 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 6762 TL.setStarLoc(readSourceLocation()); 6763 } 6764 6765 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) { 6766 TL.setKWLoc(readSourceLocation()); 6767 TL.setLParenLoc(readSourceLocation()); 6768 TL.setRParenLoc(readSourceLocation()); 6769 } 6770 6771 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) { 6772 TL.setKWLoc(readSourceLocation()); 6773 } 6774 6775 void TypeLocReader::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) { 6776 TL.setNameLoc(readSourceLocation()); 6777 } 6778 void TypeLocReader::VisitDependentExtIntTypeLoc( 6779 clang::DependentExtIntTypeLoc TL) { 6780 TL.setNameLoc(readSourceLocation()); 6781 } 6782 6783 6784 void ASTRecordReader::readTypeLoc(TypeLoc TL) { 6785 TypeLocReader TLR(*this); 6786 for (; !TL.isNull(); TL = TL.getNextTypeLoc()) 6787 TLR.Visit(TL); 6788 } 6789 6790 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() { 6791 QualType InfoTy = readType(); 6792 if (InfoTy.isNull()) 6793 return nullptr; 6794 6795 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy); 6796 readTypeLoc(TInfo->getTypeLoc()); 6797 return TInfo; 6798 } 6799 6800 QualType ASTReader::GetType(TypeID ID) { 6801 assert(ContextObj && "reading type with no AST context"); 6802 ASTContext &Context = *ContextObj; 6803 6804 unsigned FastQuals = ID & Qualifiers::FastMask; 6805 unsigned Index = ID >> Qualifiers::FastWidth; 6806 6807 if (Index < NUM_PREDEF_TYPE_IDS) { 6808 QualType T; 6809 switch ((PredefinedTypeIDs)Index) { 6810 case PREDEF_TYPE_NULL_ID: 6811 return QualType(); 6812 case PREDEF_TYPE_VOID_ID: 6813 T = Context.VoidTy; 6814 break; 6815 case PREDEF_TYPE_BOOL_ID: 6816 T = Context.BoolTy; 6817 break; 6818 case PREDEF_TYPE_CHAR_U_ID: 6819 case PREDEF_TYPE_CHAR_S_ID: 6820 // FIXME: Check that the signedness of CharTy is correct! 6821 T = Context.CharTy; 6822 break; 6823 case PREDEF_TYPE_UCHAR_ID: 6824 T = Context.UnsignedCharTy; 6825 break; 6826 case PREDEF_TYPE_USHORT_ID: 6827 T = Context.UnsignedShortTy; 6828 break; 6829 case PREDEF_TYPE_UINT_ID: 6830 T = Context.UnsignedIntTy; 6831 break; 6832 case PREDEF_TYPE_ULONG_ID: 6833 T = Context.UnsignedLongTy; 6834 break; 6835 case PREDEF_TYPE_ULONGLONG_ID: 6836 T = Context.UnsignedLongLongTy; 6837 break; 6838 case PREDEF_TYPE_UINT128_ID: 6839 T = Context.UnsignedInt128Ty; 6840 break; 6841 case PREDEF_TYPE_SCHAR_ID: 6842 T = Context.SignedCharTy; 6843 break; 6844 case PREDEF_TYPE_WCHAR_ID: 6845 T = Context.WCharTy; 6846 break; 6847 case PREDEF_TYPE_SHORT_ID: 6848 T = Context.ShortTy; 6849 break; 6850 case PREDEF_TYPE_INT_ID: 6851 T = Context.IntTy; 6852 break; 6853 case PREDEF_TYPE_LONG_ID: 6854 T = Context.LongTy; 6855 break; 6856 case PREDEF_TYPE_LONGLONG_ID: 6857 T = Context.LongLongTy; 6858 break; 6859 case PREDEF_TYPE_INT128_ID: 6860 T = Context.Int128Ty; 6861 break; 6862 case PREDEF_TYPE_BFLOAT16_ID: 6863 T = Context.BFloat16Ty; 6864 break; 6865 case PREDEF_TYPE_HALF_ID: 6866 T = Context.HalfTy; 6867 break; 6868 case PREDEF_TYPE_FLOAT_ID: 6869 T = Context.FloatTy; 6870 break; 6871 case PREDEF_TYPE_DOUBLE_ID: 6872 T = Context.DoubleTy; 6873 break; 6874 case PREDEF_TYPE_LONGDOUBLE_ID: 6875 T = Context.LongDoubleTy; 6876 break; 6877 case PREDEF_TYPE_SHORT_ACCUM_ID: 6878 T = Context.ShortAccumTy; 6879 break; 6880 case PREDEF_TYPE_ACCUM_ID: 6881 T = Context.AccumTy; 6882 break; 6883 case PREDEF_TYPE_LONG_ACCUM_ID: 6884 T = Context.LongAccumTy; 6885 break; 6886 case PREDEF_TYPE_USHORT_ACCUM_ID: 6887 T = Context.UnsignedShortAccumTy; 6888 break; 6889 case PREDEF_TYPE_UACCUM_ID: 6890 T = Context.UnsignedAccumTy; 6891 break; 6892 case PREDEF_TYPE_ULONG_ACCUM_ID: 6893 T = Context.UnsignedLongAccumTy; 6894 break; 6895 case PREDEF_TYPE_SHORT_FRACT_ID: 6896 T = Context.ShortFractTy; 6897 break; 6898 case PREDEF_TYPE_FRACT_ID: 6899 T = Context.FractTy; 6900 break; 6901 case PREDEF_TYPE_LONG_FRACT_ID: 6902 T = Context.LongFractTy; 6903 break; 6904 case PREDEF_TYPE_USHORT_FRACT_ID: 6905 T = Context.UnsignedShortFractTy; 6906 break; 6907 case PREDEF_TYPE_UFRACT_ID: 6908 T = Context.UnsignedFractTy; 6909 break; 6910 case PREDEF_TYPE_ULONG_FRACT_ID: 6911 T = Context.UnsignedLongFractTy; 6912 break; 6913 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID: 6914 T = Context.SatShortAccumTy; 6915 break; 6916 case PREDEF_TYPE_SAT_ACCUM_ID: 6917 T = Context.SatAccumTy; 6918 break; 6919 case PREDEF_TYPE_SAT_LONG_ACCUM_ID: 6920 T = Context.SatLongAccumTy; 6921 break; 6922 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID: 6923 T = Context.SatUnsignedShortAccumTy; 6924 break; 6925 case PREDEF_TYPE_SAT_UACCUM_ID: 6926 T = Context.SatUnsignedAccumTy; 6927 break; 6928 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID: 6929 T = Context.SatUnsignedLongAccumTy; 6930 break; 6931 case PREDEF_TYPE_SAT_SHORT_FRACT_ID: 6932 T = Context.SatShortFractTy; 6933 break; 6934 case PREDEF_TYPE_SAT_FRACT_ID: 6935 T = Context.SatFractTy; 6936 break; 6937 case PREDEF_TYPE_SAT_LONG_FRACT_ID: 6938 T = Context.SatLongFractTy; 6939 break; 6940 case PREDEF_TYPE_SAT_USHORT_FRACT_ID: 6941 T = Context.SatUnsignedShortFractTy; 6942 break; 6943 case PREDEF_TYPE_SAT_UFRACT_ID: 6944 T = Context.SatUnsignedFractTy; 6945 break; 6946 case PREDEF_TYPE_SAT_ULONG_FRACT_ID: 6947 T = Context.SatUnsignedLongFractTy; 6948 break; 6949 case PREDEF_TYPE_FLOAT16_ID: 6950 T = Context.Float16Ty; 6951 break; 6952 case PREDEF_TYPE_FLOAT128_ID: 6953 T = Context.Float128Ty; 6954 break; 6955 case PREDEF_TYPE_OVERLOAD_ID: 6956 T = Context.OverloadTy; 6957 break; 6958 case PREDEF_TYPE_BOUND_MEMBER: 6959 T = Context.BoundMemberTy; 6960 break; 6961 case PREDEF_TYPE_PSEUDO_OBJECT: 6962 T = Context.PseudoObjectTy; 6963 break; 6964 case PREDEF_TYPE_DEPENDENT_ID: 6965 T = Context.DependentTy; 6966 break; 6967 case PREDEF_TYPE_UNKNOWN_ANY: 6968 T = Context.UnknownAnyTy; 6969 break; 6970 case PREDEF_TYPE_NULLPTR_ID: 6971 T = Context.NullPtrTy; 6972 break; 6973 case PREDEF_TYPE_CHAR8_ID: 6974 T = Context.Char8Ty; 6975 break; 6976 case PREDEF_TYPE_CHAR16_ID: 6977 T = Context.Char16Ty; 6978 break; 6979 case PREDEF_TYPE_CHAR32_ID: 6980 T = Context.Char32Ty; 6981 break; 6982 case PREDEF_TYPE_OBJC_ID: 6983 T = Context.ObjCBuiltinIdTy; 6984 break; 6985 case PREDEF_TYPE_OBJC_CLASS: 6986 T = Context.ObjCBuiltinClassTy; 6987 break; 6988 case PREDEF_TYPE_OBJC_SEL: 6989 T = Context.ObjCBuiltinSelTy; 6990 break; 6991 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 6992 case PREDEF_TYPE_##Id##_ID: \ 6993 T = Context.SingletonId; \ 6994 break; 6995 #include "clang/Basic/OpenCLImageTypes.def" 6996 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ 6997 case PREDEF_TYPE_##Id##_ID: \ 6998 T = Context.Id##Ty; \ 6999 break; 7000 #include "clang/Basic/OpenCLExtensionTypes.def" 7001 case PREDEF_TYPE_SAMPLER_ID: 7002 T = Context.OCLSamplerTy; 7003 break; 7004 case PREDEF_TYPE_EVENT_ID: 7005 T = Context.OCLEventTy; 7006 break; 7007 case PREDEF_TYPE_CLK_EVENT_ID: 7008 T = Context.OCLClkEventTy; 7009 break; 7010 case PREDEF_TYPE_QUEUE_ID: 7011 T = Context.OCLQueueTy; 7012 break; 7013 case PREDEF_TYPE_RESERVE_ID_ID: 7014 T = Context.OCLReserveIDTy; 7015 break; 7016 case PREDEF_TYPE_AUTO_DEDUCT: 7017 T = Context.getAutoDeductType(); 7018 break; 7019 case PREDEF_TYPE_AUTO_RREF_DEDUCT: 7020 T = Context.getAutoRRefDeductType(); 7021 break; 7022 case PREDEF_TYPE_ARC_UNBRIDGED_CAST: 7023 T = Context.ARCUnbridgedCastTy; 7024 break; 7025 case PREDEF_TYPE_BUILTIN_FN: 7026 T = Context.BuiltinFnTy; 7027 break; 7028 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX: 7029 T = Context.IncompleteMatrixIdxTy; 7030 break; 7031 case PREDEF_TYPE_OMP_ARRAY_SECTION: 7032 T = Context.OMPArraySectionTy; 7033 break; 7034 case PREDEF_TYPE_OMP_ARRAY_SHAPING: 7035 T = Context.OMPArraySectionTy; 7036 break; 7037 case PREDEF_TYPE_OMP_ITERATOR: 7038 T = Context.OMPIteratorTy; 7039 break; 7040 #define SVE_TYPE(Name, Id, SingletonId) \ 7041 case PREDEF_TYPE_##Id##_ID: \ 7042 T = Context.SingletonId; \ 7043 break; 7044 #include "clang/Basic/AArch64SVEACLETypes.def" 7045 } 7046 7047 assert(!T.isNull() && "Unknown predefined type"); 7048 return T.withFastQualifiers(FastQuals); 7049 } 7050 7051 Index -= NUM_PREDEF_TYPE_IDS; 7052 assert(Index < TypesLoaded.size() && "Type index out-of-range"); 7053 if (TypesLoaded[Index].isNull()) { 7054 TypesLoaded[Index] = readTypeRecord(Index); 7055 if (TypesLoaded[Index].isNull()) 7056 return QualType(); 7057 7058 TypesLoaded[Index]->setFromAST(); 7059 if (DeserializationListener) 7060 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID), 7061 TypesLoaded[Index]); 7062 } 7063 7064 return TypesLoaded[Index].withFastQualifiers(FastQuals); 7065 } 7066 7067 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) { 7068 return GetType(getGlobalTypeID(F, LocalID)); 7069 } 7070 7071 serialization::TypeID 7072 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const { 7073 unsigned FastQuals = LocalID & Qualifiers::FastMask; 7074 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth; 7075 7076 if (LocalIndex < NUM_PREDEF_TYPE_IDS) 7077 return LocalID; 7078 7079 if (!F.ModuleOffsetMap.empty()) 7080 ReadModuleOffsetMap(F); 7081 7082 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7083 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS); 7084 assert(I != F.TypeRemap.end() && "Invalid index into type index remap"); 7085 7086 unsigned GlobalIndex = LocalIndex + I->second; 7087 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals; 7088 } 7089 7090 TemplateArgumentLocInfo 7091 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) { 7092 switch (Kind) { 7093 case TemplateArgument::Expression: 7094 return readExpr(); 7095 case TemplateArgument::Type: 7096 return readTypeSourceInfo(); 7097 case TemplateArgument::Template: { 7098 NestedNameSpecifierLoc QualifierLoc = 7099 readNestedNameSpecifierLoc(); 7100 SourceLocation TemplateNameLoc = readSourceLocation(); 7101 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 7102 SourceLocation()); 7103 } 7104 case TemplateArgument::TemplateExpansion: { 7105 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc(); 7106 SourceLocation TemplateNameLoc = readSourceLocation(); 7107 SourceLocation EllipsisLoc = readSourceLocation(); 7108 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 7109 EllipsisLoc); 7110 } 7111 case TemplateArgument::Null: 7112 case TemplateArgument::Integral: 7113 case TemplateArgument::Declaration: 7114 case TemplateArgument::NullPtr: 7115 case TemplateArgument::Pack: 7116 // FIXME: Is this right? 7117 return TemplateArgumentLocInfo(); 7118 } 7119 llvm_unreachable("unexpected template argument loc"); 7120 } 7121 7122 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() { 7123 TemplateArgument Arg = readTemplateArgument(); 7124 7125 if (Arg.getKind() == TemplateArgument::Expression) { 7126 if (readBool()) // bool InfoHasSameExpr. 7127 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); 7128 } 7129 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind())); 7130 } 7131 7132 const ASTTemplateArgumentListInfo * 7133 ASTRecordReader::readASTTemplateArgumentListInfo() { 7134 SourceLocation LAngleLoc = readSourceLocation(); 7135 SourceLocation RAngleLoc = readSourceLocation(); 7136 unsigned NumArgsAsWritten = readInt(); 7137 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc); 7138 for (unsigned i = 0; i != NumArgsAsWritten; ++i) 7139 TemplArgsInfo.addArgument(readTemplateArgumentLoc()); 7140 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo); 7141 } 7142 7143 Decl *ASTReader::GetExternalDecl(uint32_t ID) { 7144 return GetDecl(ID); 7145 } 7146 7147 void ASTReader::CompleteRedeclChain(const Decl *D) { 7148 if (NumCurrentElementsDeserializing) { 7149 // We arrange to not care about the complete redeclaration chain while we're 7150 // deserializing. Just remember that the AST has marked this one as complete 7151 // but that it's not actually complete yet, so we know we still need to 7152 // complete it later. 7153 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D)); 7154 return; 7155 } 7156 7157 const DeclContext *DC = D->getDeclContext()->getRedeclContext(); 7158 7159 // If this is a named declaration, complete it by looking it up 7160 // within its context. 7161 // 7162 // FIXME: Merging a function definition should merge 7163 // all mergeable entities within it. 7164 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) || 7165 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) { 7166 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) { 7167 if (!getContext().getLangOpts().CPlusPlus && 7168 isa<TranslationUnitDecl>(DC)) { 7169 // Outside of C++, we don't have a lookup table for the TU, so update 7170 // the identifier instead. (For C++ modules, we don't store decls 7171 // in the serialized identifier table, so we do the lookup in the TU.) 7172 auto *II = Name.getAsIdentifierInfo(); 7173 assert(II && "non-identifier name in C?"); 7174 if (II->isOutOfDate()) 7175 updateOutOfDateIdentifier(*II); 7176 } else 7177 DC->lookup(Name); 7178 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) { 7179 // Find all declarations of this kind from the relevant context. 7180 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) { 7181 auto *DC = cast<DeclContext>(DCDecl); 7182 SmallVector<Decl*, 8> Decls; 7183 FindExternalLexicalDecls( 7184 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls); 7185 } 7186 } 7187 } 7188 7189 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) 7190 CTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7191 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) 7192 VTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7193 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 7194 if (auto *Template = FD->getPrimaryTemplate()) 7195 Template->LoadLazySpecializations(); 7196 } 7197 } 7198 7199 CXXCtorInitializer ** 7200 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) { 7201 RecordLocation Loc = getLocalBitOffset(Offset); 7202 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7203 SavedStreamPosition SavedPosition(Cursor); 7204 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7205 Error(std::move(Err)); 7206 return nullptr; 7207 } 7208 ReadingKindTracker ReadingKind(Read_Decl, *this); 7209 7210 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7211 if (!MaybeCode) { 7212 Error(MaybeCode.takeError()); 7213 return nullptr; 7214 } 7215 unsigned Code = MaybeCode.get(); 7216 7217 ASTRecordReader Record(*this, *Loc.F); 7218 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7219 if (!MaybeRecCode) { 7220 Error(MaybeRecCode.takeError()); 7221 return nullptr; 7222 } 7223 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) { 7224 Error("malformed AST file: missing C++ ctor initializers"); 7225 return nullptr; 7226 } 7227 7228 return Record.readCXXCtorInitializers(); 7229 } 7230 7231 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) { 7232 assert(ContextObj && "reading base specifiers with no AST context"); 7233 ASTContext &Context = *ContextObj; 7234 7235 RecordLocation Loc = getLocalBitOffset(Offset); 7236 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7237 SavedStreamPosition SavedPosition(Cursor); 7238 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7239 Error(std::move(Err)); 7240 return nullptr; 7241 } 7242 ReadingKindTracker ReadingKind(Read_Decl, *this); 7243 7244 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7245 if (!MaybeCode) { 7246 Error(MaybeCode.takeError()); 7247 return nullptr; 7248 } 7249 unsigned Code = MaybeCode.get(); 7250 7251 ASTRecordReader Record(*this, *Loc.F); 7252 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7253 if (!MaybeRecCode) { 7254 Error(MaybeCode.takeError()); 7255 return nullptr; 7256 } 7257 unsigned RecCode = MaybeRecCode.get(); 7258 7259 if (RecCode != DECL_CXX_BASE_SPECIFIERS) { 7260 Error("malformed AST file: missing C++ base specifiers"); 7261 return nullptr; 7262 } 7263 7264 unsigned NumBases = Record.readInt(); 7265 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases); 7266 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases]; 7267 for (unsigned I = 0; I != NumBases; ++I) 7268 Bases[I] = Record.readCXXBaseSpecifier(); 7269 return Bases; 7270 } 7271 7272 serialization::DeclID 7273 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const { 7274 if (LocalID < NUM_PREDEF_DECL_IDS) 7275 return LocalID; 7276 7277 if (!F.ModuleOffsetMap.empty()) 7278 ReadModuleOffsetMap(F); 7279 7280 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7281 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS); 7282 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap"); 7283 7284 return LocalID + I->second; 7285 } 7286 7287 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID, 7288 ModuleFile &M) const { 7289 // Predefined decls aren't from any module. 7290 if (ID < NUM_PREDEF_DECL_IDS) 7291 return false; 7292 7293 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID && 7294 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls; 7295 } 7296 7297 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) { 7298 if (!D->isFromASTFile()) 7299 return nullptr; 7300 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID()); 7301 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7302 return I->second; 7303 } 7304 7305 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) { 7306 if (ID < NUM_PREDEF_DECL_IDS) 7307 return SourceLocation(); 7308 7309 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7310 7311 if (Index > DeclsLoaded.size()) { 7312 Error("declaration ID out-of-range for AST file"); 7313 return SourceLocation(); 7314 } 7315 7316 if (Decl *D = DeclsLoaded[Index]) 7317 return D->getLocation(); 7318 7319 SourceLocation Loc; 7320 DeclCursorForID(ID, Loc); 7321 return Loc; 7322 } 7323 7324 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) { 7325 switch (ID) { 7326 case PREDEF_DECL_NULL_ID: 7327 return nullptr; 7328 7329 case PREDEF_DECL_TRANSLATION_UNIT_ID: 7330 return Context.getTranslationUnitDecl(); 7331 7332 case PREDEF_DECL_OBJC_ID_ID: 7333 return Context.getObjCIdDecl(); 7334 7335 case PREDEF_DECL_OBJC_SEL_ID: 7336 return Context.getObjCSelDecl(); 7337 7338 case PREDEF_DECL_OBJC_CLASS_ID: 7339 return Context.getObjCClassDecl(); 7340 7341 case PREDEF_DECL_OBJC_PROTOCOL_ID: 7342 return Context.getObjCProtocolDecl(); 7343 7344 case PREDEF_DECL_INT_128_ID: 7345 return Context.getInt128Decl(); 7346 7347 case PREDEF_DECL_UNSIGNED_INT_128_ID: 7348 return Context.getUInt128Decl(); 7349 7350 case PREDEF_DECL_OBJC_INSTANCETYPE_ID: 7351 return Context.getObjCInstanceTypeDecl(); 7352 7353 case PREDEF_DECL_BUILTIN_VA_LIST_ID: 7354 return Context.getBuiltinVaListDecl(); 7355 7356 case PREDEF_DECL_VA_LIST_TAG: 7357 return Context.getVaListTagDecl(); 7358 7359 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID: 7360 return Context.getBuiltinMSVaListDecl(); 7361 7362 case PREDEF_DECL_BUILTIN_MS_GUID_ID: 7363 return Context.getMSGuidTagDecl(); 7364 7365 case PREDEF_DECL_EXTERN_C_CONTEXT_ID: 7366 return Context.getExternCContextDecl(); 7367 7368 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID: 7369 return Context.getMakeIntegerSeqDecl(); 7370 7371 case PREDEF_DECL_CF_CONSTANT_STRING_ID: 7372 return Context.getCFConstantStringDecl(); 7373 7374 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID: 7375 return Context.getCFConstantStringTagDecl(); 7376 7377 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID: 7378 return Context.getTypePackElementDecl(); 7379 } 7380 llvm_unreachable("PredefinedDeclIDs unknown enum value"); 7381 } 7382 7383 Decl *ASTReader::GetExistingDecl(DeclID ID) { 7384 assert(ContextObj && "reading decl with no AST context"); 7385 if (ID < NUM_PREDEF_DECL_IDS) { 7386 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID); 7387 if (D) { 7388 // Track that we have merged the declaration with ID \p ID into the 7389 // pre-existing predefined declaration \p D. 7390 auto &Merged = KeyDecls[D->getCanonicalDecl()]; 7391 if (Merged.empty()) 7392 Merged.push_back(ID); 7393 } 7394 return D; 7395 } 7396 7397 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7398 7399 if (Index >= DeclsLoaded.size()) { 7400 assert(0 && "declaration ID out-of-range for AST file"); 7401 Error("declaration ID out-of-range for AST file"); 7402 return nullptr; 7403 } 7404 7405 return DeclsLoaded[Index]; 7406 } 7407 7408 Decl *ASTReader::GetDecl(DeclID ID) { 7409 if (ID < NUM_PREDEF_DECL_IDS) 7410 return GetExistingDecl(ID); 7411 7412 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7413 7414 if (Index >= DeclsLoaded.size()) { 7415 assert(0 && "declaration ID out-of-range for AST file"); 7416 Error("declaration ID out-of-range for AST file"); 7417 return nullptr; 7418 } 7419 7420 if (!DeclsLoaded[Index]) { 7421 ReadDeclRecord(ID); 7422 if (DeserializationListener) 7423 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); 7424 } 7425 7426 return DeclsLoaded[Index]; 7427 } 7428 7429 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M, 7430 DeclID GlobalID) { 7431 if (GlobalID < NUM_PREDEF_DECL_IDS) 7432 return GlobalID; 7433 7434 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID); 7435 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7436 ModuleFile *Owner = I->second; 7437 7438 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos 7439 = M.GlobalToLocalDeclIDs.find(Owner); 7440 if (Pos == M.GlobalToLocalDeclIDs.end()) 7441 return 0; 7442 7443 return GlobalID - Owner->BaseDeclID + Pos->second; 7444 } 7445 7446 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F, 7447 const RecordData &Record, 7448 unsigned &Idx) { 7449 if (Idx >= Record.size()) { 7450 Error("Corrupted AST file"); 7451 return 0; 7452 } 7453 7454 return getGlobalDeclID(F, Record[Idx++]); 7455 } 7456 7457 /// Resolve the offset of a statement into a statement. 7458 /// 7459 /// This operation will read a new statement from the external 7460 /// source each time it is called, and is meant to be used via a 7461 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 7462 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) { 7463 // Switch case IDs are per Decl. 7464 ClearSwitchCaseIDs(); 7465 7466 // Offset here is a global offset across the entire chain. 7467 RecordLocation Loc = getLocalBitOffset(Offset); 7468 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) { 7469 Error(std::move(Err)); 7470 return nullptr; 7471 } 7472 assert(NumCurrentElementsDeserializing == 0 && 7473 "should not be called while already deserializing"); 7474 Deserializing D(this); 7475 return ReadStmtFromStream(*Loc.F); 7476 } 7477 7478 void ASTReader::FindExternalLexicalDecls( 7479 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant, 7480 SmallVectorImpl<Decl *> &Decls) { 7481 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {}; 7482 7483 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) { 7484 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries"); 7485 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) { 7486 auto K = (Decl::Kind)+LexicalDecls[I]; 7487 if (!IsKindWeWant(K)) 7488 continue; 7489 7490 auto ID = (serialization::DeclID)+LexicalDecls[I + 1]; 7491 7492 // Don't add predefined declarations to the lexical context more 7493 // than once. 7494 if (ID < NUM_PREDEF_DECL_IDS) { 7495 if (PredefsVisited[ID]) 7496 continue; 7497 7498 PredefsVisited[ID] = true; 7499 } 7500 7501 if (Decl *D = GetLocalDecl(*M, ID)) { 7502 assert(D->getKind() == K && "wrong kind for lexical decl"); 7503 if (!DC->isDeclInLexicalTraversal(D)) 7504 Decls.push_back(D); 7505 } 7506 } 7507 }; 7508 7509 if (isa<TranslationUnitDecl>(DC)) { 7510 for (auto Lexical : TULexicalDecls) 7511 Visit(Lexical.first, Lexical.second); 7512 } else { 7513 auto I = LexicalDecls.find(DC); 7514 if (I != LexicalDecls.end()) 7515 Visit(I->second.first, I->second.second); 7516 } 7517 7518 ++NumLexicalDeclContextsRead; 7519 } 7520 7521 namespace { 7522 7523 class DeclIDComp { 7524 ASTReader &Reader; 7525 ModuleFile &Mod; 7526 7527 public: 7528 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {} 7529 7530 bool operator()(LocalDeclID L, LocalDeclID R) const { 7531 SourceLocation LHS = getLocation(L); 7532 SourceLocation RHS = getLocation(R); 7533 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7534 } 7535 7536 bool operator()(SourceLocation LHS, LocalDeclID R) const { 7537 SourceLocation RHS = getLocation(R); 7538 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7539 } 7540 7541 bool operator()(LocalDeclID L, SourceLocation RHS) const { 7542 SourceLocation LHS = getLocation(L); 7543 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7544 } 7545 7546 SourceLocation getLocation(LocalDeclID ID) const { 7547 return Reader.getSourceManager().getFileLoc( 7548 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID))); 7549 } 7550 }; 7551 7552 } // namespace 7553 7554 void ASTReader::FindFileRegionDecls(FileID File, 7555 unsigned Offset, unsigned Length, 7556 SmallVectorImpl<Decl *> &Decls) { 7557 SourceManager &SM = getSourceManager(); 7558 7559 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File); 7560 if (I == FileDeclIDs.end()) 7561 return; 7562 7563 FileDeclsInfo &DInfo = I->second; 7564 if (DInfo.Decls.empty()) 7565 return; 7566 7567 SourceLocation 7568 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset); 7569 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length); 7570 7571 DeclIDComp DIDComp(*this, *DInfo.Mod); 7572 ArrayRef<serialization::LocalDeclID>::iterator BeginIt = 7573 llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp); 7574 if (BeginIt != DInfo.Decls.begin()) 7575 --BeginIt; 7576 7577 // If we are pointing at a top-level decl inside an objc container, we need 7578 // to backtrack until we find it otherwise we will fail to report that the 7579 // region overlaps with an objc container. 7580 while (BeginIt != DInfo.Decls.begin() && 7581 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt)) 7582 ->isTopLevelDeclInObjCContainer()) 7583 --BeginIt; 7584 7585 ArrayRef<serialization::LocalDeclID>::iterator EndIt = 7586 llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp); 7587 if (EndIt != DInfo.Decls.end()) 7588 ++EndIt; 7589 7590 for (ArrayRef<serialization::LocalDeclID>::iterator 7591 DIt = BeginIt; DIt != EndIt; ++DIt) 7592 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt))); 7593 } 7594 7595 bool 7596 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, 7597 DeclarationName Name) { 7598 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() && 7599 "DeclContext has no visible decls in storage"); 7600 if (!Name) 7601 return false; 7602 7603 auto It = Lookups.find(DC); 7604 if (It == Lookups.end()) 7605 return false; 7606 7607 Deserializing LookupResults(this); 7608 7609 // Load the list of declarations. 7610 SmallVector<NamedDecl *, 64> Decls; 7611 for (DeclID ID : It->second.Table.find(Name)) { 7612 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7613 if (ND->getDeclName() == Name) 7614 Decls.push_back(ND); 7615 } 7616 7617 ++NumVisibleDeclContextsRead; 7618 SetExternalVisibleDeclsForName(DC, Name, Decls); 7619 return !Decls.empty(); 7620 } 7621 7622 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) { 7623 if (!DC->hasExternalVisibleStorage()) 7624 return; 7625 7626 auto It = Lookups.find(DC); 7627 assert(It != Lookups.end() && 7628 "have external visible storage but no lookup tables"); 7629 7630 DeclsMap Decls; 7631 7632 for (DeclID ID : It->second.Table.findAll()) { 7633 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7634 Decls[ND->getDeclName()].push_back(ND); 7635 } 7636 7637 ++NumVisibleDeclContextsRead; 7638 7639 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) { 7640 SetExternalVisibleDeclsForName(DC, I->first, I->second); 7641 } 7642 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false); 7643 } 7644 7645 const serialization::reader::DeclContextLookupTable * 7646 ASTReader::getLoadedLookupTables(DeclContext *Primary) const { 7647 auto I = Lookups.find(Primary); 7648 return I == Lookups.end() ? nullptr : &I->second; 7649 } 7650 7651 /// Under non-PCH compilation the consumer receives the objc methods 7652 /// before receiving the implementation, and codegen depends on this. 7653 /// We simulate this by deserializing and passing to consumer the methods of the 7654 /// implementation before passing the deserialized implementation decl. 7655 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD, 7656 ASTConsumer *Consumer) { 7657 assert(ImplD && Consumer); 7658 7659 for (auto *I : ImplD->methods()) 7660 Consumer->HandleInterestingDecl(DeclGroupRef(I)); 7661 7662 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD)); 7663 } 7664 7665 void ASTReader::PassInterestingDeclToConsumer(Decl *D) { 7666 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) 7667 PassObjCImplDeclToConsumer(ImplD, Consumer); 7668 else 7669 Consumer->HandleInterestingDecl(DeclGroupRef(D)); 7670 } 7671 7672 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) { 7673 this->Consumer = Consumer; 7674 7675 if (Consumer) 7676 PassInterestingDeclsToConsumer(); 7677 7678 if (DeserializationListener) 7679 DeserializationListener->ReaderInitialized(this); 7680 } 7681 7682 void ASTReader::PrintStats() { 7683 std::fprintf(stderr, "*** AST File Statistics:\n"); 7684 7685 unsigned NumTypesLoaded 7686 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), 7687 QualType()); 7688 unsigned NumDeclsLoaded 7689 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), 7690 (Decl *)nullptr); 7691 unsigned NumIdentifiersLoaded 7692 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), 7693 IdentifiersLoaded.end(), 7694 (IdentifierInfo *)nullptr); 7695 unsigned NumMacrosLoaded 7696 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(), 7697 MacrosLoaded.end(), 7698 (MacroInfo *)nullptr); 7699 unsigned NumSelectorsLoaded 7700 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), 7701 SelectorsLoaded.end(), 7702 Selector()); 7703 7704 if (unsigned TotalNumSLocEntries = getTotalNumSLocs()) 7705 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", 7706 NumSLocEntriesRead, TotalNumSLocEntries, 7707 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); 7708 if (!TypesLoaded.empty()) 7709 std::fprintf(stderr, " %u/%u types read (%f%%)\n", 7710 NumTypesLoaded, (unsigned)TypesLoaded.size(), 7711 ((float)NumTypesLoaded/TypesLoaded.size() * 100)); 7712 if (!DeclsLoaded.empty()) 7713 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", 7714 NumDeclsLoaded, (unsigned)DeclsLoaded.size(), 7715 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); 7716 if (!IdentifiersLoaded.empty()) 7717 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", 7718 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), 7719 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); 7720 if (!MacrosLoaded.empty()) 7721 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7722 NumMacrosLoaded, (unsigned)MacrosLoaded.size(), 7723 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100)); 7724 if (!SelectorsLoaded.empty()) 7725 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", 7726 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), 7727 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); 7728 if (TotalNumStatements) 7729 std::fprintf(stderr, " %u/%u statements read (%f%%)\n", 7730 NumStatementsRead, TotalNumStatements, 7731 ((float)NumStatementsRead/TotalNumStatements * 100)); 7732 if (TotalNumMacros) 7733 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7734 NumMacrosRead, TotalNumMacros, 7735 ((float)NumMacrosRead/TotalNumMacros * 100)); 7736 if (TotalLexicalDeclContexts) 7737 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", 7738 NumLexicalDeclContextsRead, TotalLexicalDeclContexts, 7739 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts 7740 * 100)); 7741 if (TotalVisibleDeclContexts) 7742 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", 7743 NumVisibleDeclContextsRead, TotalVisibleDeclContexts, 7744 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts 7745 * 100)); 7746 if (TotalNumMethodPoolEntries) 7747 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", 7748 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, 7749 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries 7750 * 100)); 7751 if (NumMethodPoolLookups) 7752 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n", 7753 NumMethodPoolHits, NumMethodPoolLookups, 7754 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0)); 7755 if (NumMethodPoolTableLookups) 7756 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n", 7757 NumMethodPoolTableHits, NumMethodPoolTableLookups, 7758 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups 7759 * 100.0)); 7760 if (NumIdentifierLookupHits) 7761 std::fprintf(stderr, 7762 " %u / %u identifier table lookups succeeded (%f%%)\n", 7763 NumIdentifierLookupHits, NumIdentifierLookups, 7764 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups); 7765 7766 if (GlobalIndex) { 7767 std::fprintf(stderr, "\n"); 7768 GlobalIndex->printStats(); 7769 } 7770 7771 std::fprintf(stderr, "\n"); 7772 dump(); 7773 std::fprintf(stderr, "\n"); 7774 } 7775 7776 template<typename Key, typename ModuleFile, unsigned InitialCapacity> 7777 LLVM_DUMP_METHOD static void 7778 dumpModuleIDMap(StringRef Name, 7779 const ContinuousRangeMap<Key, ModuleFile *, 7780 InitialCapacity> &Map) { 7781 if (Map.begin() == Map.end()) 7782 return; 7783 7784 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>; 7785 7786 llvm::errs() << Name << ":\n"; 7787 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end(); 7788 I != IEnd; ++I) { 7789 llvm::errs() << " " << I->first << " -> " << I->second->FileName 7790 << "\n"; 7791 } 7792 } 7793 7794 LLVM_DUMP_METHOD void ASTReader::dump() { 7795 llvm::errs() << "*** PCH/ModuleFile Remappings:\n"; 7796 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap); 7797 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap); 7798 dumpModuleIDMap("Global type map", GlobalTypeMap); 7799 dumpModuleIDMap("Global declaration map", GlobalDeclMap); 7800 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap); 7801 dumpModuleIDMap("Global macro map", GlobalMacroMap); 7802 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap); 7803 dumpModuleIDMap("Global selector map", GlobalSelectorMap); 7804 dumpModuleIDMap("Global preprocessed entity map", 7805 GlobalPreprocessedEntityMap); 7806 7807 llvm::errs() << "\n*** PCH/Modules Loaded:"; 7808 for (ModuleFile &M : ModuleMgr) 7809 M.dump(); 7810 } 7811 7812 /// Return the amount of memory used by memory buffers, breaking down 7813 /// by heap-backed versus mmap'ed memory. 7814 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const { 7815 for (ModuleFile &I : ModuleMgr) { 7816 if (llvm::MemoryBuffer *buf = I.Buffer) { 7817 size_t bytes = buf->getBufferSize(); 7818 switch (buf->getBufferKind()) { 7819 case llvm::MemoryBuffer::MemoryBuffer_Malloc: 7820 sizes.malloc_bytes += bytes; 7821 break; 7822 case llvm::MemoryBuffer::MemoryBuffer_MMap: 7823 sizes.mmap_bytes += bytes; 7824 break; 7825 } 7826 } 7827 } 7828 } 7829 7830 void ASTReader::InitializeSema(Sema &S) { 7831 SemaObj = &S; 7832 S.addExternalSource(this); 7833 7834 // Makes sure any declarations that were deserialized "too early" 7835 // still get added to the identifier's declaration chains. 7836 for (uint64_t ID : PreloadedDeclIDs) { 7837 NamedDecl *D = cast<NamedDecl>(GetDecl(ID)); 7838 pushExternalDeclIntoScope(D, D->getDeclName()); 7839 } 7840 PreloadedDeclIDs.clear(); 7841 7842 // FIXME: What happens if these are changed by a module import? 7843 if (!FPPragmaOptions.empty()) { 7844 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS"); 7845 FPOptionsOverride NewOverrides(FPPragmaOptions[0]); 7846 SemaObj->CurFPFeatures = 7847 NewOverrides.applyOverrides(SemaObj->getLangOpts()); 7848 } 7849 7850 SemaObj->OpenCLFeatures.copy(OpenCLExtensions); 7851 SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap; 7852 SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap; 7853 7854 UpdateSema(); 7855 } 7856 7857 void ASTReader::UpdateSema() { 7858 assert(SemaObj && "no Sema to update"); 7859 7860 // Load the offsets of the declarations that Sema references. 7861 // They will be lazily deserialized when needed. 7862 if (!SemaDeclRefs.empty()) { 7863 assert(SemaDeclRefs.size() % 3 == 0); 7864 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) { 7865 if (!SemaObj->StdNamespace) 7866 SemaObj->StdNamespace = SemaDeclRefs[I]; 7867 if (!SemaObj->StdBadAlloc) 7868 SemaObj->StdBadAlloc = SemaDeclRefs[I+1]; 7869 if (!SemaObj->StdAlignValT) 7870 SemaObj->StdAlignValT = SemaDeclRefs[I+2]; 7871 } 7872 SemaDeclRefs.clear(); 7873 } 7874 7875 // Update the state of pragmas. Use the same API as if we had encountered the 7876 // pragma in the source. 7877 if(OptimizeOffPragmaLocation.isValid()) 7878 SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation); 7879 if (PragmaMSStructState != -1) 7880 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState); 7881 if (PointersToMembersPragmaLocation.isValid()) { 7882 SemaObj->ActOnPragmaMSPointersToMembers( 7883 (LangOptions::PragmaMSPointersToMembersKind) 7884 PragmaMSPointersToMembersState, 7885 PointersToMembersPragmaLocation); 7886 } 7887 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth; 7888 7889 if (PragmaPackCurrentValue) { 7890 // The bottom of the stack might have a default value. It must be adjusted 7891 // to the current value to ensure that the packing state is preserved after 7892 // popping entries that were included/imported from a PCH/module. 7893 bool DropFirst = false; 7894 if (!PragmaPackStack.empty() && 7895 PragmaPackStack.front().Location.isInvalid()) { 7896 assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue && 7897 "Expected a default alignment value"); 7898 SemaObj->PackStack.Stack.emplace_back( 7899 PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue, 7900 SemaObj->PackStack.CurrentPragmaLocation, 7901 PragmaPackStack.front().PushLocation); 7902 DropFirst = true; 7903 } 7904 for (const auto &Entry : 7905 llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0)) 7906 SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value, 7907 Entry.Location, Entry.PushLocation); 7908 if (PragmaPackCurrentLocation.isInvalid()) { 7909 assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue && 7910 "Expected a default alignment value"); 7911 // Keep the current values. 7912 } else { 7913 SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue; 7914 SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation; 7915 } 7916 } 7917 if (FpPragmaCurrentValue) { 7918 // The bottom of the stack might have a default value. It must be adjusted 7919 // to the current value to ensure that fp-pragma state is preserved after 7920 // popping entries that were included/imported from a PCH/module. 7921 bool DropFirst = false; 7922 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) { 7923 assert(FpPragmaStack.front().Value == 7924 SemaObj->FpPragmaStack.DefaultValue && 7925 "Expected a default pragma float_control value"); 7926 SemaObj->FpPragmaStack.Stack.emplace_back( 7927 FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue, 7928 SemaObj->FpPragmaStack.CurrentPragmaLocation, 7929 FpPragmaStack.front().PushLocation); 7930 DropFirst = true; 7931 } 7932 for (const auto &Entry : 7933 llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0)) 7934 SemaObj->FpPragmaStack.Stack.emplace_back( 7935 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation); 7936 if (FpPragmaCurrentLocation.isInvalid()) { 7937 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue && 7938 "Expected a default pragma float_control value"); 7939 // Keep the current values. 7940 } else { 7941 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue; 7942 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation; 7943 } 7944 } 7945 } 7946 7947 IdentifierInfo *ASTReader::get(StringRef Name) { 7948 // Note that we are loading an identifier. 7949 Deserializing AnIdentifier(this); 7950 7951 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0, 7952 NumIdentifierLookups, 7953 NumIdentifierLookupHits); 7954 7955 // We don't need to do identifier table lookups in C++ modules (we preload 7956 // all interesting declarations, and don't need to use the scope for name 7957 // lookups). Perform the lookup in PCH files, though, since we don't build 7958 // a complete initial identifier table if we're carrying on from a PCH. 7959 if (PP.getLangOpts().CPlusPlus) { 7960 for (auto F : ModuleMgr.pch_modules()) 7961 if (Visitor(*F)) 7962 break; 7963 } else { 7964 // If there is a global index, look there first to determine which modules 7965 // provably do not have any results for this identifier. 7966 GlobalModuleIndex::HitSet Hits; 7967 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 7968 if (!loadGlobalIndex()) { 7969 if (GlobalIndex->lookupIdentifier(Name, Hits)) { 7970 HitsPtr = &Hits; 7971 } 7972 } 7973 7974 ModuleMgr.visit(Visitor, HitsPtr); 7975 } 7976 7977 IdentifierInfo *II = Visitor.getIdentifierInfo(); 7978 markIdentifierUpToDate(II); 7979 return II; 7980 } 7981 7982 namespace clang { 7983 7984 /// An identifier-lookup iterator that enumerates all of the 7985 /// identifiers stored within a set of AST files. 7986 class ASTIdentifierIterator : public IdentifierIterator { 7987 /// The AST reader whose identifiers are being enumerated. 7988 const ASTReader &Reader; 7989 7990 /// The current index into the chain of AST files stored in 7991 /// the AST reader. 7992 unsigned Index; 7993 7994 /// The current position within the identifier lookup table 7995 /// of the current AST file. 7996 ASTIdentifierLookupTable::key_iterator Current; 7997 7998 /// The end position within the identifier lookup table of 7999 /// the current AST file. 8000 ASTIdentifierLookupTable::key_iterator End; 8001 8002 /// Whether to skip any modules in the ASTReader. 8003 bool SkipModules; 8004 8005 public: 8006 explicit ASTIdentifierIterator(const ASTReader &Reader, 8007 bool SkipModules = false); 8008 8009 StringRef Next() override; 8010 }; 8011 8012 } // namespace clang 8013 8014 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader, 8015 bool SkipModules) 8016 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) { 8017 } 8018 8019 StringRef ASTIdentifierIterator::Next() { 8020 while (Current == End) { 8021 // If we have exhausted all of our AST files, we're done. 8022 if (Index == 0) 8023 return StringRef(); 8024 8025 --Index; 8026 ModuleFile &F = Reader.ModuleMgr[Index]; 8027 if (SkipModules && F.isModule()) 8028 continue; 8029 8030 ASTIdentifierLookupTable *IdTable = 8031 (ASTIdentifierLookupTable *)F.IdentifierLookupTable; 8032 Current = IdTable->key_begin(); 8033 End = IdTable->key_end(); 8034 } 8035 8036 // We have any identifiers remaining in the current AST file; return 8037 // the next one. 8038 StringRef Result = *Current; 8039 ++Current; 8040 return Result; 8041 } 8042 8043 namespace { 8044 8045 /// A utility for appending two IdentifierIterators. 8046 class ChainedIdentifierIterator : public IdentifierIterator { 8047 std::unique_ptr<IdentifierIterator> Current; 8048 std::unique_ptr<IdentifierIterator> Queued; 8049 8050 public: 8051 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First, 8052 std::unique_ptr<IdentifierIterator> Second) 8053 : Current(std::move(First)), Queued(std::move(Second)) {} 8054 8055 StringRef Next() override { 8056 if (!Current) 8057 return StringRef(); 8058 8059 StringRef result = Current->Next(); 8060 if (!result.empty()) 8061 return result; 8062 8063 // Try the queued iterator, which may itself be empty. 8064 Current.reset(); 8065 std::swap(Current, Queued); 8066 return Next(); 8067 } 8068 }; 8069 8070 } // namespace 8071 8072 IdentifierIterator *ASTReader::getIdentifiers() { 8073 if (!loadGlobalIndex()) { 8074 std::unique_ptr<IdentifierIterator> ReaderIter( 8075 new ASTIdentifierIterator(*this, /*SkipModules=*/true)); 8076 std::unique_ptr<IdentifierIterator> ModulesIter( 8077 GlobalIndex->createIdentifierIterator()); 8078 return new ChainedIdentifierIterator(std::move(ReaderIter), 8079 std::move(ModulesIter)); 8080 } 8081 8082 return new ASTIdentifierIterator(*this); 8083 } 8084 8085 namespace clang { 8086 namespace serialization { 8087 8088 class ReadMethodPoolVisitor { 8089 ASTReader &Reader; 8090 Selector Sel; 8091 unsigned PriorGeneration; 8092 unsigned InstanceBits = 0; 8093 unsigned FactoryBits = 0; 8094 bool InstanceHasMoreThanOneDecl = false; 8095 bool FactoryHasMoreThanOneDecl = false; 8096 SmallVector<ObjCMethodDecl *, 4> InstanceMethods; 8097 SmallVector<ObjCMethodDecl *, 4> FactoryMethods; 8098 8099 public: 8100 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel, 8101 unsigned PriorGeneration) 8102 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {} 8103 8104 bool operator()(ModuleFile &M) { 8105 if (!M.SelectorLookupTable) 8106 return false; 8107 8108 // If we've already searched this module file, skip it now. 8109 if (M.Generation <= PriorGeneration) 8110 return true; 8111 8112 ++Reader.NumMethodPoolTableLookups; 8113 ASTSelectorLookupTable *PoolTable 8114 = (ASTSelectorLookupTable*)M.SelectorLookupTable; 8115 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel); 8116 if (Pos == PoolTable->end()) 8117 return false; 8118 8119 ++Reader.NumMethodPoolTableHits; 8120 ++Reader.NumSelectorsRead; 8121 // FIXME: Not quite happy with the statistics here. We probably should 8122 // disable this tracking when called via LoadSelector. 8123 // Also, should entries without methods count as misses? 8124 ++Reader.NumMethodPoolEntriesRead; 8125 ASTSelectorLookupTrait::data_type Data = *Pos; 8126 if (Reader.DeserializationListener) 8127 Reader.DeserializationListener->SelectorRead(Data.ID, Sel); 8128 8129 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end()); 8130 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end()); 8131 InstanceBits = Data.InstanceBits; 8132 FactoryBits = Data.FactoryBits; 8133 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl; 8134 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl; 8135 return true; 8136 } 8137 8138 /// Retrieve the instance methods found by this visitor. 8139 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const { 8140 return InstanceMethods; 8141 } 8142 8143 /// Retrieve the instance methods found by this visitor. 8144 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const { 8145 return FactoryMethods; 8146 } 8147 8148 unsigned getInstanceBits() const { return InstanceBits; } 8149 unsigned getFactoryBits() const { return FactoryBits; } 8150 8151 bool instanceHasMoreThanOneDecl() const { 8152 return InstanceHasMoreThanOneDecl; 8153 } 8154 8155 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; } 8156 }; 8157 8158 } // namespace serialization 8159 } // namespace clang 8160 8161 /// Add the given set of methods to the method list. 8162 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods, 8163 ObjCMethodList &List) { 8164 for (unsigned I = 0, N = Methods.size(); I != N; ++I) { 8165 S.addMethodToGlobalList(&List, Methods[I]); 8166 } 8167 } 8168 8169 void ASTReader::ReadMethodPool(Selector Sel) { 8170 // Get the selector generation and update it to the current generation. 8171 unsigned &Generation = SelectorGeneration[Sel]; 8172 unsigned PriorGeneration = Generation; 8173 Generation = getGeneration(); 8174 SelectorOutOfDate[Sel] = false; 8175 8176 // Search for methods defined with this selector. 8177 ++NumMethodPoolLookups; 8178 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration); 8179 ModuleMgr.visit(Visitor); 8180 8181 if (Visitor.getInstanceMethods().empty() && 8182 Visitor.getFactoryMethods().empty()) 8183 return; 8184 8185 ++NumMethodPoolHits; 8186 8187 if (!getSema()) 8188 return; 8189 8190 Sema &S = *getSema(); 8191 Sema::GlobalMethodPool::iterator Pos 8192 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first; 8193 8194 Pos->second.first.setBits(Visitor.getInstanceBits()); 8195 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl()); 8196 Pos->second.second.setBits(Visitor.getFactoryBits()); 8197 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl()); 8198 8199 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since 8200 // when building a module we keep every method individually and may need to 8201 // update hasMoreThanOneDecl as we add the methods. 8202 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first); 8203 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second); 8204 } 8205 8206 void ASTReader::updateOutOfDateSelector(Selector Sel) { 8207 if (SelectorOutOfDate[Sel]) 8208 ReadMethodPool(Sel); 8209 } 8210 8211 void ASTReader::ReadKnownNamespaces( 8212 SmallVectorImpl<NamespaceDecl *> &Namespaces) { 8213 Namespaces.clear(); 8214 8215 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) { 8216 if (NamespaceDecl *Namespace 8217 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I]))) 8218 Namespaces.push_back(Namespace); 8219 } 8220 } 8221 8222 void ASTReader::ReadUndefinedButUsed( 8223 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) { 8224 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) { 8225 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++])); 8226 SourceLocation Loc = 8227 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]); 8228 Undefined.insert(std::make_pair(D, Loc)); 8229 } 8230 } 8231 8232 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector< 8233 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> & 8234 Exprs) { 8235 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) { 8236 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++])); 8237 uint64_t Count = DelayedDeleteExprs[Idx++]; 8238 for (uint64_t C = 0; C < Count; ++C) { 8239 SourceLocation DeleteLoc = 8240 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]); 8241 const bool IsArrayForm = DelayedDeleteExprs[Idx++]; 8242 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm)); 8243 } 8244 } 8245 } 8246 8247 void ASTReader::ReadTentativeDefinitions( 8248 SmallVectorImpl<VarDecl *> &TentativeDefs) { 8249 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { 8250 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I])); 8251 if (Var) 8252 TentativeDefs.push_back(Var); 8253 } 8254 TentativeDefinitions.clear(); 8255 } 8256 8257 void ASTReader::ReadUnusedFileScopedDecls( 8258 SmallVectorImpl<const DeclaratorDecl *> &Decls) { 8259 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) { 8260 DeclaratorDecl *D 8261 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I])); 8262 if (D) 8263 Decls.push_back(D); 8264 } 8265 UnusedFileScopedDecls.clear(); 8266 } 8267 8268 void ASTReader::ReadDelegatingConstructors( 8269 SmallVectorImpl<CXXConstructorDecl *> &Decls) { 8270 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) { 8271 CXXConstructorDecl *D 8272 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I])); 8273 if (D) 8274 Decls.push_back(D); 8275 } 8276 DelegatingCtorDecls.clear(); 8277 } 8278 8279 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) { 8280 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) { 8281 TypedefNameDecl *D 8282 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])); 8283 if (D) 8284 Decls.push_back(D); 8285 } 8286 ExtVectorDecls.clear(); 8287 } 8288 8289 void ASTReader::ReadUnusedLocalTypedefNameCandidates( 8290 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) { 8291 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N; 8292 ++I) { 8293 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>( 8294 GetDecl(UnusedLocalTypedefNameCandidates[I])); 8295 if (D) 8296 Decls.insert(D); 8297 } 8298 UnusedLocalTypedefNameCandidates.clear(); 8299 } 8300 8301 void ASTReader::ReadDeclsToCheckForDeferredDiags( 8302 llvm::SmallVector<Decl *, 4> &Decls) { 8303 for (unsigned I = 0, N = DeclsToCheckForDeferredDiags.size(); I != N; 8304 ++I) { 8305 auto *D = dyn_cast_or_null<Decl>( 8306 GetDecl(DeclsToCheckForDeferredDiags[I])); 8307 if (D) 8308 Decls.push_back(D); 8309 } 8310 DeclsToCheckForDeferredDiags.clear(); 8311 } 8312 8313 8314 void ASTReader::ReadReferencedSelectors( 8315 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) { 8316 if (ReferencedSelectorsData.empty()) 8317 return; 8318 8319 // If there are @selector references added them to its pool. This is for 8320 // implementation of -Wselector. 8321 unsigned int DataSize = ReferencedSelectorsData.size()-1; 8322 unsigned I = 0; 8323 while (I < DataSize) { 8324 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]); 8325 SourceLocation SelLoc 8326 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]); 8327 Sels.push_back(std::make_pair(Sel, SelLoc)); 8328 } 8329 ReferencedSelectorsData.clear(); 8330 } 8331 8332 void ASTReader::ReadWeakUndeclaredIdentifiers( 8333 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) { 8334 if (WeakUndeclaredIdentifiers.empty()) 8335 return; 8336 8337 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) { 8338 IdentifierInfo *WeakId 8339 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8340 IdentifierInfo *AliasId 8341 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8342 SourceLocation Loc 8343 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]); 8344 bool Used = WeakUndeclaredIdentifiers[I++]; 8345 WeakInfo WI(AliasId, Loc); 8346 WI.setUsed(Used); 8347 WeakIDs.push_back(std::make_pair(WeakId, WI)); 8348 } 8349 WeakUndeclaredIdentifiers.clear(); 8350 } 8351 8352 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) { 8353 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) { 8354 ExternalVTableUse VT; 8355 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); 8356 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]); 8357 VT.DefinitionRequired = VTableUses[Idx++]; 8358 VTables.push_back(VT); 8359 } 8360 8361 VTableUses.clear(); 8362 } 8363 8364 void ASTReader::ReadPendingInstantiations( 8365 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) { 8366 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) { 8367 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++])); 8368 SourceLocation Loc 8369 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]); 8370 8371 Pending.push_back(std::make_pair(D, Loc)); 8372 } 8373 PendingInstantiations.clear(); 8374 } 8375 8376 void ASTReader::ReadLateParsedTemplates( 8377 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>> 8378 &LPTMap) { 8379 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N; 8380 /* In loop */) { 8381 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++])); 8382 8383 auto LT = std::make_unique<LateParsedTemplate>(); 8384 LT->D = GetDecl(LateParsedTemplates[Idx++]); 8385 8386 ModuleFile *F = getOwningModuleFile(LT->D); 8387 assert(F && "No module"); 8388 8389 unsigned TokN = LateParsedTemplates[Idx++]; 8390 LT->Toks.reserve(TokN); 8391 for (unsigned T = 0; T < TokN; ++T) 8392 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx)); 8393 8394 LPTMap.insert(std::make_pair(FD, std::move(LT))); 8395 } 8396 8397 LateParsedTemplates.clear(); 8398 } 8399 8400 void ASTReader::LoadSelector(Selector Sel) { 8401 // It would be complicated to avoid reading the methods anyway. So don't. 8402 ReadMethodPool(Sel); 8403 } 8404 8405 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) { 8406 assert(ID && "Non-zero identifier ID required"); 8407 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); 8408 IdentifiersLoaded[ID - 1] = II; 8409 if (DeserializationListener) 8410 DeserializationListener->IdentifierRead(ID, II); 8411 } 8412 8413 /// Set the globally-visible declarations associated with the given 8414 /// identifier. 8415 /// 8416 /// If the AST reader is currently in a state where the given declaration IDs 8417 /// cannot safely be resolved, they are queued until it is safe to resolve 8418 /// them. 8419 /// 8420 /// \param II an IdentifierInfo that refers to one or more globally-visible 8421 /// declarations. 8422 /// 8423 /// \param DeclIDs the set of declaration IDs with the name @p II that are 8424 /// visible at global scope. 8425 /// 8426 /// \param Decls if non-null, this vector will be populated with the set of 8427 /// deserialized declarations. These declarations will not be pushed into 8428 /// scope. 8429 void 8430 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, 8431 const SmallVectorImpl<uint32_t> &DeclIDs, 8432 SmallVectorImpl<Decl *> *Decls) { 8433 if (NumCurrentElementsDeserializing && !Decls) { 8434 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end()); 8435 return; 8436 } 8437 8438 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { 8439 if (!SemaObj) { 8440 // Queue this declaration so that it will be added to the 8441 // translation unit scope and identifier's declaration chain 8442 // once a Sema object is known. 8443 PreloadedDeclIDs.push_back(DeclIDs[I]); 8444 continue; 8445 } 8446 8447 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); 8448 8449 // If we're simply supposed to record the declarations, do so now. 8450 if (Decls) { 8451 Decls->push_back(D); 8452 continue; 8453 } 8454 8455 // Introduce this declaration into the translation-unit scope 8456 // and add it to the declaration chain for this identifier, so 8457 // that (unqualified) name lookup will find it. 8458 pushExternalDeclIntoScope(D, II); 8459 } 8460 } 8461 8462 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) { 8463 if (ID == 0) 8464 return nullptr; 8465 8466 if (IdentifiersLoaded.empty()) { 8467 Error("no identifier table in AST file"); 8468 return nullptr; 8469 } 8470 8471 ID -= 1; 8472 if (!IdentifiersLoaded[ID]) { 8473 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1); 8474 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map"); 8475 ModuleFile *M = I->second; 8476 unsigned Index = ID - M->BaseIdentifierID; 8477 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index]; 8478 8479 // All of the strings in the AST file are preceded by a 16-bit length. 8480 // Extract that 16-bit length to avoid having to execute strlen(). 8481 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as 8482 // unsigned integers. This is important to avoid integer overflow when 8483 // we cast them to 'unsigned'. 8484 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2; 8485 unsigned StrLen = (((unsigned) StrLenPtr[0]) 8486 | (((unsigned) StrLenPtr[1]) << 8)) - 1; 8487 auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen)); 8488 IdentifiersLoaded[ID] = &II; 8489 markIdentifierFromAST(*this, II); 8490 if (DeserializationListener) 8491 DeserializationListener->IdentifierRead(ID + 1, &II); 8492 } 8493 8494 return IdentifiersLoaded[ID]; 8495 } 8496 8497 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) { 8498 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID)); 8499 } 8500 8501 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) { 8502 if (LocalID < NUM_PREDEF_IDENT_IDS) 8503 return LocalID; 8504 8505 if (!M.ModuleOffsetMap.empty()) 8506 ReadModuleOffsetMap(M); 8507 8508 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8509 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS); 8510 assert(I != M.IdentifierRemap.end() 8511 && "Invalid index into identifier index remap"); 8512 8513 return LocalID + I->second; 8514 } 8515 8516 MacroInfo *ASTReader::getMacro(MacroID ID) { 8517 if (ID == 0) 8518 return nullptr; 8519 8520 if (MacrosLoaded.empty()) { 8521 Error("no macro table in AST file"); 8522 return nullptr; 8523 } 8524 8525 ID -= NUM_PREDEF_MACRO_IDS; 8526 if (!MacrosLoaded[ID]) { 8527 GlobalMacroMapType::iterator I 8528 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS); 8529 assert(I != GlobalMacroMap.end() && "Corrupted global macro map"); 8530 ModuleFile *M = I->second; 8531 unsigned Index = ID - M->BaseMacroID; 8532 MacrosLoaded[ID] = 8533 ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]); 8534 8535 if (DeserializationListener) 8536 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS, 8537 MacrosLoaded[ID]); 8538 } 8539 8540 return MacrosLoaded[ID]; 8541 } 8542 8543 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) { 8544 if (LocalID < NUM_PREDEF_MACRO_IDS) 8545 return LocalID; 8546 8547 if (!M.ModuleOffsetMap.empty()) 8548 ReadModuleOffsetMap(M); 8549 8550 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8551 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS); 8552 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap"); 8553 8554 return LocalID + I->second; 8555 } 8556 8557 serialization::SubmoduleID 8558 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) { 8559 if (LocalID < NUM_PREDEF_SUBMODULE_IDS) 8560 return LocalID; 8561 8562 if (!M.ModuleOffsetMap.empty()) 8563 ReadModuleOffsetMap(M); 8564 8565 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8566 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS); 8567 assert(I != M.SubmoduleRemap.end() 8568 && "Invalid index into submodule index remap"); 8569 8570 return LocalID + I->second; 8571 } 8572 8573 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) { 8574 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) { 8575 assert(GlobalID == 0 && "Unhandled global submodule ID"); 8576 return nullptr; 8577 } 8578 8579 if (GlobalID > SubmodulesLoaded.size()) { 8580 Error("submodule ID out of range in AST file"); 8581 return nullptr; 8582 } 8583 8584 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS]; 8585 } 8586 8587 Module *ASTReader::getModule(unsigned ID) { 8588 return getSubmodule(ID); 8589 } 8590 8591 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) { 8592 if (ID & 1) { 8593 // It's a module, look it up by submodule ID. 8594 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1)); 8595 return I == GlobalSubmoduleMap.end() ? nullptr : I->second; 8596 } else { 8597 // It's a prefix (preamble, PCH, ...). Look it up by index. 8598 unsigned IndexFromEnd = ID >> 1; 8599 assert(IndexFromEnd && "got reference to unknown module file"); 8600 return getModuleManager().pch_modules().end()[-IndexFromEnd]; 8601 } 8602 } 8603 8604 unsigned ASTReader::getModuleFileID(ModuleFile *F) { 8605 if (!F) 8606 return 1; 8607 8608 // For a file representing a module, use the submodule ID of the top-level 8609 // module as the file ID. For any other kind of file, the number of such 8610 // files loaded beforehand will be the same on reload. 8611 // FIXME: Is this true even if we have an explicit module file and a PCH? 8612 if (F->isModule()) 8613 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1; 8614 8615 auto PCHModules = getModuleManager().pch_modules(); 8616 auto I = llvm::find(PCHModules, F); 8617 assert(I != PCHModules.end() && "emitting reference to unknown file"); 8618 return (I - PCHModules.end()) << 1; 8619 } 8620 8621 llvm::Optional<ASTSourceDescriptor> 8622 ASTReader::getSourceDescriptor(unsigned ID) { 8623 if (Module *M = getSubmodule(ID)) 8624 return ASTSourceDescriptor(*M); 8625 8626 // If there is only a single PCH, return it instead. 8627 // Chained PCH are not supported. 8628 const auto &PCHChain = ModuleMgr.pch_modules(); 8629 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) { 8630 ModuleFile &MF = ModuleMgr.getPrimaryModule(); 8631 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName); 8632 StringRef FileName = llvm::sys::path::filename(MF.FileName); 8633 return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName, 8634 MF.Signature); 8635 } 8636 return None; 8637 } 8638 8639 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) { 8640 auto I = DefinitionSource.find(FD); 8641 if (I == DefinitionSource.end()) 8642 return EK_ReplyHazy; 8643 return I->second ? EK_Never : EK_Always; 8644 } 8645 8646 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) { 8647 return DecodeSelector(getGlobalSelectorID(M, LocalID)); 8648 } 8649 8650 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) { 8651 if (ID == 0) 8652 return Selector(); 8653 8654 if (ID > SelectorsLoaded.size()) { 8655 Error("selector ID out of range in AST file"); 8656 return Selector(); 8657 } 8658 8659 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) { 8660 // Load this selector from the selector table. 8661 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID); 8662 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map"); 8663 ModuleFile &M = *I->second; 8664 ASTSelectorLookupTrait Trait(*this, M); 8665 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS; 8666 SelectorsLoaded[ID - 1] = 8667 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0); 8668 if (DeserializationListener) 8669 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 8670 } 8671 8672 return SelectorsLoaded[ID - 1]; 8673 } 8674 8675 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) { 8676 return DecodeSelector(ID); 8677 } 8678 8679 uint32_t ASTReader::GetNumExternalSelectors() { 8680 // ID 0 (the null selector) is considered an external selector. 8681 return getTotalNumSelectors() + 1; 8682 } 8683 8684 serialization::SelectorID 8685 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const { 8686 if (LocalID < NUM_PREDEF_SELECTOR_IDS) 8687 return LocalID; 8688 8689 if (!M.ModuleOffsetMap.empty()) 8690 ReadModuleOffsetMap(M); 8691 8692 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8693 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS); 8694 assert(I != M.SelectorRemap.end() 8695 && "Invalid index into selector index remap"); 8696 8697 return LocalID + I->second; 8698 } 8699 8700 DeclarationNameLoc 8701 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) { 8702 DeclarationNameLoc DNLoc; 8703 switch (Name.getNameKind()) { 8704 case DeclarationName::CXXConstructorName: 8705 case DeclarationName::CXXDestructorName: 8706 case DeclarationName::CXXConversionFunctionName: 8707 DNLoc.NamedType.TInfo = readTypeSourceInfo(); 8708 break; 8709 8710 case DeclarationName::CXXOperatorName: 8711 DNLoc.CXXOperatorName.BeginOpNameLoc 8712 = readSourceLocation().getRawEncoding(); 8713 DNLoc.CXXOperatorName.EndOpNameLoc 8714 = readSourceLocation().getRawEncoding(); 8715 break; 8716 8717 case DeclarationName::CXXLiteralOperatorName: 8718 DNLoc.CXXLiteralOperatorName.OpNameLoc 8719 = readSourceLocation().getRawEncoding(); 8720 break; 8721 8722 case DeclarationName::Identifier: 8723 case DeclarationName::ObjCZeroArgSelector: 8724 case DeclarationName::ObjCOneArgSelector: 8725 case DeclarationName::ObjCMultiArgSelector: 8726 case DeclarationName::CXXUsingDirective: 8727 case DeclarationName::CXXDeductionGuideName: 8728 break; 8729 } 8730 return DNLoc; 8731 } 8732 8733 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() { 8734 DeclarationNameInfo NameInfo; 8735 NameInfo.setName(readDeclarationName()); 8736 NameInfo.setLoc(readSourceLocation()); 8737 NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName())); 8738 return NameInfo; 8739 } 8740 8741 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) { 8742 Info.QualifierLoc = readNestedNameSpecifierLoc(); 8743 unsigned NumTPLists = readInt(); 8744 Info.NumTemplParamLists = NumTPLists; 8745 if (NumTPLists) { 8746 Info.TemplParamLists = 8747 new (getContext()) TemplateParameterList *[NumTPLists]; 8748 for (unsigned i = 0; i != NumTPLists; ++i) 8749 Info.TemplParamLists[i] = readTemplateParameterList(); 8750 } 8751 } 8752 8753 TemplateParameterList * 8754 ASTRecordReader::readTemplateParameterList() { 8755 SourceLocation TemplateLoc = readSourceLocation(); 8756 SourceLocation LAngleLoc = readSourceLocation(); 8757 SourceLocation RAngleLoc = readSourceLocation(); 8758 8759 unsigned NumParams = readInt(); 8760 SmallVector<NamedDecl *, 16> Params; 8761 Params.reserve(NumParams); 8762 while (NumParams--) 8763 Params.push_back(readDeclAs<NamedDecl>()); 8764 8765 bool HasRequiresClause = readBool(); 8766 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr; 8767 8768 TemplateParameterList *TemplateParams = TemplateParameterList::Create( 8769 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause); 8770 return TemplateParams; 8771 } 8772 8773 void ASTRecordReader::readTemplateArgumentList( 8774 SmallVectorImpl<TemplateArgument> &TemplArgs, 8775 bool Canonicalize) { 8776 unsigned NumTemplateArgs = readInt(); 8777 TemplArgs.reserve(NumTemplateArgs); 8778 while (NumTemplateArgs--) 8779 TemplArgs.push_back(readTemplateArgument(Canonicalize)); 8780 } 8781 8782 /// Read a UnresolvedSet structure. 8783 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) { 8784 unsigned NumDecls = readInt(); 8785 Set.reserve(getContext(), NumDecls); 8786 while (NumDecls--) { 8787 DeclID ID = readDeclID(); 8788 AccessSpecifier AS = (AccessSpecifier) readInt(); 8789 Set.addLazyDecl(getContext(), ID, AS); 8790 } 8791 } 8792 8793 CXXBaseSpecifier 8794 ASTRecordReader::readCXXBaseSpecifier() { 8795 bool isVirtual = readBool(); 8796 bool isBaseOfClass = readBool(); 8797 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt()); 8798 bool inheritConstructors = readBool(); 8799 TypeSourceInfo *TInfo = readTypeSourceInfo(); 8800 SourceRange Range = readSourceRange(); 8801 SourceLocation EllipsisLoc = readSourceLocation(); 8802 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo, 8803 EllipsisLoc); 8804 Result.setInheritConstructors(inheritConstructors); 8805 return Result; 8806 } 8807 8808 CXXCtorInitializer ** 8809 ASTRecordReader::readCXXCtorInitializers() { 8810 ASTContext &Context = getContext(); 8811 unsigned NumInitializers = readInt(); 8812 assert(NumInitializers && "wrote ctor initializers but have no inits"); 8813 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers]; 8814 for (unsigned i = 0; i != NumInitializers; ++i) { 8815 TypeSourceInfo *TInfo = nullptr; 8816 bool IsBaseVirtual = false; 8817 FieldDecl *Member = nullptr; 8818 IndirectFieldDecl *IndirectMember = nullptr; 8819 8820 CtorInitializerType Type = (CtorInitializerType) readInt(); 8821 switch (Type) { 8822 case CTOR_INITIALIZER_BASE: 8823 TInfo = readTypeSourceInfo(); 8824 IsBaseVirtual = readBool(); 8825 break; 8826 8827 case CTOR_INITIALIZER_DELEGATING: 8828 TInfo = readTypeSourceInfo(); 8829 break; 8830 8831 case CTOR_INITIALIZER_MEMBER: 8832 Member = readDeclAs<FieldDecl>(); 8833 break; 8834 8835 case CTOR_INITIALIZER_INDIRECT_MEMBER: 8836 IndirectMember = readDeclAs<IndirectFieldDecl>(); 8837 break; 8838 } 8839 8840 SourceLocation MemberOrEllipsisLoc = readSourceLocation(); 8841 Expr *Init = readExpr(); 8842 SourceLocation LParenLoc = readSourceLocation(); 8843 SourceLocation RParenLoc = readSourceLocation(); 8844 8845 CXXCtorInitializer *BOMInit; 8846 if (Type == CTOR_INITIALIZER_BASE) 8847 BOMInit = new (Context) 8848 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init, 8849 RParenLoc, MemberOrEllipsisLoc); 8850 else if (Type == CTOR_INITIALIZER_DELEGATING) 8851 BOMInit = new (Context) 8852 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc); 8853 else if (Member) 8854 BOMInit = new (Context) 8855 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc, 8856 Init, RParenLoc); 8857 else 8858 BOMInit = new (Context) 8859 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc, 8860 LParenLoc, Init, RParenLoc); 8861 8862 if (/*IsWritten*/readBool()) { 8863 unsigned SourceOrder = readInt(); 8864 BOMInit->setSourceOrder(SourceOrder); 8865 } 8866 8867 CtorInitializers[i] = BOMInit; 8868 } 8869 8870 return CtorInitializers; 8871 } 8872 8873 NestedNameSpecifierLoc 8874 ASTRecordReader::readNestedNameSpecifierLoc() { 8875 ASTContext &Context = getContext(); 8876 unsigned N = readInt(); 8877 NestedNameSpecifierLocBuilder Builder; 8878 for (unsigned I = 0; I != N; ++I) { 8879 auto Kind = readNestedNameSpecifierKind(); 8880 switch (Kind) { 8881 case NestedNameSpecifier::Identifier: { 8882 IdentifierInfo *II = readIdentifier(); 8883 SourceRange Range = readSourceRange(); 8884 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd()); 8885 break; 8886 } 8887 8888 case NestedNameSpecifier::Namespace: { 8889 NamespaceDecl *NS = readDeclAs<NamespaceDecl>(); 8890 SourceRange Range = readSourceRange(); 8891 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd()); 8892 break; 8893 } 8894 8895 case NestedNameSpecifier::NamespaceAlias: { 8896 NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>(); 8897 SourceRange Range = readSourceRange(); 8898 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd()); 8899 break; 8900 } 8901 8902 case NestedNameSpecifier::TypeSpec: 8903 case NestedNameSpecifier::TypeSpecWithTemplate: { 8904 bool Template = readBool(); 8905 TypeSourceInfo *T = readTypeSourceInfo(); 8906 if (!T) 8907 return NestedNameSpecifierLoc(); 8908 SourceLocation ColonColonLoc = readSourceLocation(); 8909 8910 // FIXME: 'template' keyword location not saved anywhere, so we fake it. 8911 Builder.Extend(Context, 8912 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(), 8913 T->getTypeLoc(), ColonColonLoc); 8914 break; 8915 } 8916 8917 case NestedNameSpecifier::Global: { 8918 SourceLocation ColonColonLoc = readSourceLocation(); 8919 Builder.MakeGlobal(Context, ColonColonLoc); 8920 break; 8921 } 8922 8923 case NestedNameSpecifier::Super: { 8924 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>(); 8925 SourceRange Range = readSourceRange(); 8926 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd()); 8927 break; 8928 } 8929 } 8930 } 8931 8932 return Builder.getWithLocInContext(Context); 8933 } 8934 8935 SourceRange 8936 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record, 8937 unsigned &Idx) { 8938 SourceLocation beg = ReadSourceLocation(F, Record, Idx); 8939 SourceLocation end = ReadSourceLocation(F, Record, Idx); 8940 return SourceRange(beg, end); 8941 } 8942 8943 static FixedPointSemantics 8944 ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record, 8945 unsigned &Idx) { 8946 unsigned Width = Record[Idx++]; 8947 unsigned Scale = Record[Idx++]; 8948 uint64_t Tmp = Record[Idx++]; 8949 bool IsSigned = Tmp & 0x1; 8950 bool IsSaturated = Tmp & 0x2; 8951 bool HasUnsignedPadding = Tmp & 0x4; 8952 return FixedPointSemantics(Width, Scale, IsSigned, IsSaturated, 8953 HasUnsignedPadding); 8954 } 8955 8956 static const llvm::fltSemantics & 8957 readAPFloatSemantics(ASTRecordReader &reader) { 8958 return llvm::APFloatBase::EnumToSemantics( 8959 static_cast<llvm::APFloatBase::Semantics>(reader.readInt())); 8960 } 8961 8962 APValue ASTRecordReader::readAPValue() { 8963 unsigned Kind = readInt(); 8964 switch ((APValue::ValueKind) Kind) { 8965 case APValue::None: 8966 return APValue(); 8967 case APValue::Indeterminate: 8968 return APValue::IndeterminateValue(); 8969 case APValue::Int: 8970 return APValue(readAPSInt()); 8971 case APValue::Float: { 8972 const llvm::fltSemantics &FloatSema = readAPFloatSemantics(*this); 8973 return APValue(readAPFloat(FloatSema)); 8974 } 8975 case APValue::FixedPoint: { 8976 FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx); 8977 return APValue(APFixedPoint(readAPInt(), FPSema)); 8978 } 8979 case APValue::ComplexInt: { 8980 llvm::APSInt First = readAPSInt(); 8981 return APValue(std::move(First), readAPSInt()); 8982 } 8983 case APValue::ComplexFloat: { 8984 const llvm::fltSemantics &FloatSema1 = readAPFloatSemantics(*this); 8985 llvm::APFloat First = readAPFloat(FloatSema1); 8986 const llvm::fltSemantics &FloatSema2 = readAPFloatSemantics(*this); 8987 return APValue(std::move(First), readAPFloat(FloatSema2)); 8988 } 8989 case APValue::LValue: 8990 case APValue::Vector: 8991 case APValue::Array: 8992 case APValue::Struct: 8993 case APValue::Union: 8994 case APValue::MemberPointer: 8995 case APValue::AddrLabelDiff: 8996 // TODO : Handle all these APValue::ValueKind. 8997 return APValue(); 8998 } 8999 llvm_unreachable("Invalid APValue::ValueKind"); 9000 } 9001 9002 /// Read a floating-point value 9003 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) { 9004 return llvm::APFloat(Sem, readAPInt()); 9005 } 9006 9007 // Read a string 9008 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) { 9009 unsigned Len = Record[Idx++]; 9010 std::string Result(Record.data() + Idx, Record.data() + Idx + Len); 9011 Idx += Len; 9012 return Result; 9013 } 9014 9015 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record, 9016 unsigned &Idx) { 9017 std::string Filename = ReadString(Record, Idx); 9018 ResolveImportedPath(F, Filename); 9019 return Filename; 9020 } 9021 9022 std::string ASTReader::ReadPath(StringRef BaseDirectory, 9023 const RecordData &Record, unsigned &Idx) { 9024 std::string Filename = ReadString(Record, Idx); 9025 if (!BaseDirectory.empty()) 9026 ResolveImportedPath(Filename, BaseDirectory); 9027 return Filename; 9028 } 9029 9030 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record, 9031 unsigned &Idx) { 9032 unsigned Major = Record[Idx++]; 9033 unsigned Minor = Record[Idx++]; 9034 unsigned Subminor = Record[Idx++]; 9035 if (Minor == 0) 9036 return VersionTuple(Major); 9037 if (Subminor == 0) 9038 return VersionTuple(Major, Minor - 1); 9039 return VersionTuple(Major, Minor - 1, Subminor - 1); 9040 } 9041 9042 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F, 9043 const RecordData &Record, 9044 unsigned &Idx) { 9045 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx); 9046 return CXXTemporary::Create(getContext(), Decl); 9047 } 9048 9049 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const { 9050 return Diag(CurrentImportLoc, DiagID); 9051 } 9052 9053 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const { 9054 return Diags.Report(Loc, DiagID); 9055 } 9056 9057 /// Retrieve the identifier table associated with the 9058 /// preprocessor. 9059 IdentifierTable &ASTReader::getIdentifierTable() { 9060 return PP.getIdentifierTable(); 9061 } 9062 9063 /// Record that the given ID maps to the given switch-case 9064 /// statement. 9065 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { 9066 assert((*CurrSwitchCaseStmts)[ID] == nullptr && 9067 "Already have a SwitchCase with this ID"); 9068 (*CurrSwitchCaseStmts)[ID] = SC; 9069 } 9070 9071 /// Retrieve the switch-case statement with the given ID. 9072 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) { 9073 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID"); 9074 return (*CurrSwitchCaseStmts)[ID]; 9075 } 9076 9077 void ASTReader::ClearSwitchCaseIDs() { 9078 CurrSwitchCaseStmts->clear(); 9079 } 9080 9081 void ASTReader::ReadComments() { 9082 ASTContext &Context = getContext(); 9083 std::vector<RawComment *> Comments; 9084 for (SmallVectorImpl<std::pair<BitstreamCursor, 9085 serialization::ModuleFile *>>::iterator 9086 I = CommentsCursors.begin(), 9087 E = CommentsCursors.end(); 9088 I != E; ++I) { 9089 Comments.clear(); 9090 BitstreamCursor &Cursor = I->first; 9091 serialization::ModuleFile &F = *I->second; 9092 SavedStreamPosition SavedPosition(Cursor); 9093 9094 RecordData Record; 9095 while (true) { 9096 Expected<llvm::BitstreamEntry> MaybeEntry = 9097 Cursor.advanceSkippingSubblocks( 9098 BitstreamCursor::AF_DontPopBlockAtEnd); 9099 if (!MaybeEntry) { 9100 Error(MaybeEntry.takeError()); 9101 return; 9102 } 9103 llvm::BitstreamEntry Entry = MaybeEntry.get(); 9104 9105 switch (Entry.Kind) { 9106 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 9107 case llvm::BitstreamEntry::Error: 9108 Error("malformed block record in AST file"); 9109 return; 9110 case llvm::BitstreamEntry::EndBlock: 9111 goto NextCursor; 9112 case llvm::BitstreamEntry::Record: 9113 // The interesting case. 9114 break; 9115 } 9116 9117 // Read a record. 9118 Record.clear(); 9119 Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record); 9120 if (!MaybeComment) { 9121 Error(MaybeComment.takeError()); 9122 return; 9123 } 9124 switch ((CommentRecordTypes)MaybeComment.get()) { 9125 case COMMENTS_RAW_COMMENT: { 9126 unsigned Idx = 0; 9127 SourceRange SR = ReadSourceRange(F, Record, Idx); 9128 RawComment::CommentKind Kind = 9129 (RawComment::CommentKind) Record[Idx++]; 9130 bool IsTrailingComment = Record[Idx++]; 9131 bool IsAlmostTrailingComment = Record[Idx++]; 9132 Comments.push_back(new (Context) RawComment( 9133 SR, Kind, IsTrailingComment, IsAlmostTrailingComment)); 9134 break; 9135 } 9136 } 9137 } 9138 NextCursor: 9139 llvm::DenseMap<FileID, std::map<unsigned, RawComment *>> 9140 FileToOffsetToComment; 9141 for (RawComment *C : Comments) { 9142 SourceLocation CommentLoc = C->getBeginLoc(); 9143 if (CommentLoc.isValid()) { 9144 std::pair<FileID, unsigned> Loc = 9145 SourceMgr.getDecomposedLoc(CommentLoc); 9146 if (Loc.first.isValid()) 9147 Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C); 9148 } 9149 } 9150 } 9151 } 9152 9153 void ASTReader::visitInputFiles(serialization::ModuleFile &MF, 9154 bool IncludeSystem, bool Complain, 9155 llvm::function_ref<void(const serialization::InputFile &IF, 9156 bool isSystem)> Visitor) { 9157 unsigned NumUserInputs = MF.NumUserInputFiles; 9158 unsigned NumInputs = MF.InputFilesLoaded.size(); 9159 assert(NumUserInputs <= NumInputs); 9160 unsigned N = IncludeSystem ? NumInputs : NumUserInputs; 9161 for (unsigned I = 0; I < N; ++I) { 9162 bool IsSystem = I >= NumUserInputs; 9163 InputFile IF = getInputFile(MF, I+1, Complain); 9164 Visitor(IF, IsSystem); 9165 } 9166 } 9167 9168 void ASTReader::visitTopLevelModuleMaps( 9169 serialization::ModuleFile &MF, 9170 llvm::function_ref<void(const FileEntry *FE)> Visitor) { 9171 unsigned NumInputs = MF.InputFilesLoaded.size(); 9172 for (unsigned I = 0; I < NumInputs; ++I) { 9173 InputFileInfo IFI = readInputFileInfo(MF, I + 1); 9174 if (IFI.TopLevelModuleMap) 9175 // FIXME: This unnecessarily re-reads the InputFileInfo. 9176 if (auto *FE = getInputFile(MF, I + 1).getFile()) 9177 Visitor(FE); 9178 } 9179 } 9180 9181 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) { 9182 // If we know the owning module, use it. 9183 if (Module *M = D->getImportedOwningModule()) 9184 return M->getFullModuleName(); 9185 9186 // Otherwise, use the name of the top-level module the decl is within. 9187 if (ModuleFile *M = getOwningModuleFile(D)) 9188 return M->ModuleName; 9189 9190 // Not from a module. 9191 return {}; 9192 } 9193 9194 void ASTReader::finishPendingActions() { 9195 while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() || 9196 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() || 9197 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() || 9198 !PendingUpdateRecords.empty()) { 9199 // If any identifiers with corresponding top-level declarations have 9200 // been loaded, load those declarations now. 9201 using TopLevelDeclsMap = 9202 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>; 9203 TopLevelDeclsMap TopLevelDecls; 9204 9205 while (!PendingIdentifierInfos.empty()) { 9206 IdentifierInfo *II = PendingIdentifierInfos.back().first; 9207 SmallVector<uint32_t, 4> DeclIDs = 9208 std::move(PendingIdentifierInfos.back().second); 9209 PendingIdentifierInfos.pop_back(); 9210 9211 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]); 9212 } 9213 9214 // Load each function type that we deferred loading because it was a 9215 // deduced type that might refer to a local type declared within itself. 9216 for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) { 9217 auto *FD = PendingFunctionTypes[I].first; 9218 FD->setType(GetType(PendingFunctionTypes[I].second)); 9219 9220 // If we gave a function a deduced return type, remember that we need to 9221 // propagate that along the redeclaration chain. 9222 auto *DT = FD->getReturnType()->getContainedDeducedType(); 9223 if (DT && DT->isDeduced()) 9224 PendingDeducedTypeUpdates.insert( 9225 {FD->getCanonicalDecl(), FD->getReturnType()}); 9226 } 9227 PendingFunctionTypes.clear(); 9228 9229 // For each decl chain that we wanted to complete while deserializing, mark 9230 // it as "still needs to be completed". 9231 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) { 9232 markIncompleteDeclChain(PendingIncompleteDeclChains[I]); 9233 } 9234 PendingIncompleteDeclChains.clear(); 9235 9236 // Load pending declaration chains. 9237 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) 9238 loadPendingDeclChain(PendingDeclChains[I].first, 9239 PendingDeclChains[I].second); 9240 PendingDeclChains.clear(); 9241 9242 // Make the most recent of the top-level declarations visible. 9243 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(), 9244 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) { 9245 IdentifierInfo *II = TLD->first; 9246 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) { 9247 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II); 9248 } 9249 } 9250 9251 // Load any pending macro definitions. 9252 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) { 9253 IdentifierInfo *II = PendingMacroIDs.begin()[I].first; 9254 SmallVector<PendingMacroInfo, 2> GlobalIDs; 9255 GlobalIDs.swap(PendingMacroIDs.begin()[I].second); 9256 // Initialize the macro history from chained-PCHs ahead of module imports. 9257 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9258 ++IDIdx) { 9259 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9260 if (!Info.M->isModule()) 9261 resolvePendingMacro(II, Info); 9262 } 9263 // Handle module imports. 9264 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9265 ++IDIdx) { 9266 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9267 if (Info.M->isModule()) 9268 resolvePendingMacro(II, Info); 9269 } 9270 } 9271 PendingMacroIDs.clear(); 9272 9273 // Wire up the DeclContexts for Decls that we delayed setting until 9274 // recursive loading is completed. 9275 while (!PendingDeclContextInfos.empty()) { 9276 PendingDeclContextInfo Info = PendingDeclContextInfos.front(); 9277 PendingDeclContextInfos.pop_front(); 9278 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC)); 9279 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC)); 9280 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext()); 9281 } 9282 9283 // Perform any pending declaration updates. 9284 while (!PendingUpdateRecords.empty()) { 9285 auto Update = PendingUpdateRecords.pop_back_val(); 9286 ReadingKindTracker ReadingKind(Read_Decl, *this); 9287 loadDeclUpdateRecords(Update); 9288 } 9289 } 9290 9291 // At this point, all update records for loaded decls are in place, so any 9292 // fake class definitions should have become real. 9293 assert(PendingFakeDefinitionData.empty() && 9294 "faked up a class definition but never saw the real one"); 9295 9296 // If we deserialized any C++ or Objective-C class definitions, any 9297 // Objective-C protocol definitions, or any redeclarable templates, make sure 9298 // that all redeclarations point to the definitions. Note that this can only 9299 // happen now, after the redeclaration chains have been fully wired. 9300 for (Decl *D : PendingDefinitions) { 9301 if (TagDecl *TD = dyn_cast<TagDecl>(D)) { 9302 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) { 9303 // Make sure that the TagType points at the definition. 9304 const_cast<TagType*>(TagT)->decl = TD; 9305 } 9306 9307 if (auto RD = dyn_cast<CXXRecordDecl>(D)) { 9308 for (auto *R = getMostRecentExistingDecl(RD); R; 9309 R = R->getPreviousDecl()) { 9310 assert((R == D) == 9311 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() && 9312 "declaration thinks it's the definition but it isn't"); 9313 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData; 9314 } 9315 } 9316 9317 continue; 9318 } 9319 9320 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) { 9321 // Make sure that the ObjCInterfaceType points at the definition. 9322 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl)) 9323 ->Decl = ID; 9324 9325 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl()) 9326 cast<ObjCInterfaceDecl>(R)->Data = ID->Data; 9327 9328 continue; 9329 } 9330 9331 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) { 9332 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl()) 9333 cast<ObjCProtocolDecl>(R)->Data = PD->Data; 9334 9335 continue; 9336 } 9337 9338 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl(); 9339 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl()) 9340 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common; 9341 } 9342 PendingDefinitions.clear(); 9343 9344 // Load the bodies of any functions or methods we've encountered. We do 9345 // this now (delayed) so that we can be sure that the declaration chains 9346 // have been fully wired up (hasBody relies on this). 9347 // FIXME: We shouldn't require complete redeclaration chains here. 9348 for (PendingBodiesMap::iterator PB = PendingBodies.begin(), 9349 PBEnd = PendingBodies.end(); 9350 PB != PBEnd; ++PB) { 9351 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) { 9352 // For a function defined inline within a class template, force the 9353 // canonical definition to be the one inside the canonical definition of 9354 // the template. This ensures that we instantiate from a correct view 9355 // of the template. 9356 // 9357 // Sadly we can't do this more generally: we can't be sure that all 9358 // copies of an arbitrary class definition will have the same members 9359 // defined (eg, some member functions may not be instantiated, and some 9360 // special members may or may not have been implicitly defined). 9361 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent())) 9362 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition()) 9363 continue; 9364 9365 // FIXME: Check for =delete/=default? 9366 // FIXME: Complain about ODR violations here? 9367 const FunctionDecl *Defn = nullptr; 9368 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) { 9369 FD->setLazyBody(PB->second); 9370 } else { 9371 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn); 9372 mergeDefinitionVisibility(NonConstDefn, FD); 9373 9374 if (!FD->isLateTemplateParsed() && 9375 !NonConstDefn->isLateTemplateParsed() && 9376 FD->getODRHash() != NonConstDefn->getODRHash()) { 9377 if (!isa<CXXMethodDecl>(FD)) { 9378 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9379 } else if (FD->getLexicalParent()->isFileContext() && 9380 NonConstDefn->getLexicalParent()->isFileContext()) { 9381 // Only diagnose out-of-line method definitions. If they are 9382 // in class definitions, then an error will be generated when 9383 // processing the class bodies. 9384 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9385 } 9386 } 9387 } 9388 continue; 9389 } 9390 9391 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first); 9392 if (!getContext().getLangOpts().Modules || !MD->hasBody()) 9393 MD->setLazyBody(PB->second); 9394 } 9395 PendingBodies.clear(); 9396 9397 // Do some cleanup. 9398 for (auto *ND : PendingMergedDefinitionsToDeduplicate) 9399 getContext().deduplicateMergedDefinitonsFor(ND); 9400 PendingMergedDefinitionsToDeduplicate.clear(); 9401 } 9402 9403 void ASTReader::diagnoseOdrViolations() { 9404 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() && 9405 PendingFunctionOdrMergeFailures.empty() && 9406 PendingEnumOdrMergeFailures.empty()) 9407 return; 9408 9409 // Trigger the import of the full definition of each class that had any 9410 // odr-merging problems, so we can produce better diagnostics for them. 9411 // These updates may in turn find and diagnose some ODR failures, so take 9412 // ownership of the set first. 9413 auto OdrMergeFailures = std::move(PendingOdrMergeFailures); 9414 PendingOdrMergeFailures.clear(); 9415 for (auto &Merge : OdrMergeFailures) { 9416 Merge.first->buildLookup(); 9417 Merge.first->decls_begin(); 9418 Merge.first->bases_begin(); 9419 Merge.first->vbases_begin(); 9420 for (auto &RecordPair : Merge.second) { 9421 auto *RD = RecordPair.first; 9422 RD->decls_begin(); 9423 RD->bases_begin(); 9424 RD->vbases_begin(); 9425 } 9426 } 9427 9428 // Trigger the import of functions. 9429 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures); 9430 PendingFunctionOdrMergeFailures.clear(); 9431 for (auto &Merge : FunctionOdrMergeFailures) { 9432 Merge.first->buildLookup(); 9433 Merge.first->decls_begin(); 9434 Merge.first->getBody(); 9435 for (auto &FD : Merge.second) { 9436 FD->buildLookup(); 9437 FD->decls_begin(); 9438 FD->getBody(); 9439 } 9440 } 9441 9442 // Trigger the import of enums. 9443 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures); 9444 PendingEnumOdrMergeFailures.clear(); 9445 for (auto &Merge : EnumOdrMergeFailures) { 9446 Merge.first->decls_begin(); 9447 for (auto &Enum : Merge.second) { 9448 Enum->decls_begin(); 9449 } 9450 } 9451 9452 // For each declaration from a merged context, check that the canonical 9453 // definition of that context also contains a declaration of the same 9454 // entity. 9455 // 9456 // Caution: this loop does things that might invalidate iterators into 9457 // PendingOdrMergeChecks. Don't turn this into a range-based for loop! 9458 while (!PendingOdrMergeChecks.empty()) { 9459 NamedDecl *D = PendingOdrMergeChecks.pop_back_val(); 9460 9461 // FIXME: Skip over implicit declarations for now. This matters for things 9462 // like implicitly-declared special member functions. This isn't entirely 9463 // correct; we can end up with multiple unmerged declarations of the same 9464 // implicit entity. 9465 if (D->isImplicit()) 9466 continue; 9467 9468 DeclContext *CanonDef = D->getDeclContext(); 9469 9470 bool Found = false; 9471 const Decl *DCanon = D->getCanonicalDecl(); 9472 9473 for (auto RI : D->redecls()) { 9474 if (RI->getLexicalDeclContext() == CanonDef) { 9475 Found = true; 9476 break; 9477 } 9478 } 9479 if (Found) 9480 continue; 9481 9482 // Quick check failed, time to do the slow thing. Note, we can't just 9483 // look up the name of D in CanonDef here, because the member that is 9484 // in CanonDef might not be found by name lookup (it might have been 9485 // replaced by a more recent declaration in the lookup table), and we 9486 // can't necessarily find it in the redeclaration chain because it might 9487 // be merely mergeable, not redeclarable. 9488 llvm::SmallVector<const NamedDecl*, 4> Candidates; 9489 for (auto *CanonMember : CanonDef->decls()) { 9490 if (CanonMember->getCanonicalDecl() == DCanon) { 9491 // This can happen if the declaration is merely mergeable and not 9492 // actually redeclarable (we looked for redeclarations earlier). 9493 // 9494 // FIXME: We should be able to detect this more efficiently, without 9495 // pulling in all of the members of CanonDef. 9496 Found = true; 9497 break; 9498 } 9499 if (auto *ND = dyn_cast<NamedDecl>(CanonMember)) 9500 if (ND->getDeclName() == D->getDeclName()) 9501 Candidates.push_back(ND); 9502 } 9503 9504 if (!Found) { 9505 // The AST doesn't like TagDecls becoming invalid after they've been 9506 // completed. We only really need to mark FieldDecls as invalid here. 9507 if (!isa<TagDecl>(D)) 9508 D->setInvalidDecl(); 9509 9510 // Ensure we don't accidentally recursively enter deserialization while 9511 // we're producing our diagnostic. 9512 Deserializing RecursionGuard(this); 9513 9514 std::string CanonDefModule = 9515 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef)); 9516 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl) 9517 << D << getOwningModuleNameForDiagnostic(D) 9518 << CanonDef << CanonDefModule.empty() << CanonDefModule; 9519 9520 if (Candidates.empty()) 9521 Diag(cast<Decl>(CanonDef)->getLocation(), 9522 diag::note_module_odr_violation_no_possible_decls) << D; 9523 else { 9524 for (unsigned I = 0, N = Candidates.size(); I != N; ++I) 9525 Diag(Candidates[I]->getLocation(), 9526 diag::note_module_odr_violation_possible_decl) 9527 << Candidates[I]; 9528 } 9529 9530 DiagnosedOdrMergeFailures.insert(CanonDef); 9531 } 9532 } 9533 9534 if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() && 9535 EnumOdrMergeFailures.empty()) 9536 return; 9537 9538 // Ensure we don't accidentally recursively enter deserialization while 9539 // we're producing our diagnostics. 9540 Deserializing RecursionGuard(this); 9541 9542 // Common code for hashing helpers. 9543 ODRHash Hash; 9544 auto ComputeQualTypeODRHash = [&Hash](QualType Ty) { 9545 Hash.clear(); 9546 Hash.AddQualType(Ty); 9547 return Hash.CalculateHash(); 9548 }; 9549 9550 auto ComputeODRHash = [&Hash](const Stmt *S) { 9551 assert(S); 9552 Hash.clear(); 9553 Hash.AddStmt(S); 9554 return Hash.CalculateHash(); 9555 }; 9556 9557 auto ComputeSubDeclODRHash = [&Hash](const Decl *D) { 9558 assert(D); 9559 Hash.clear(); 9560 Hash.AddSubDecl(D); 9561 return Hash.CalculateHash(); 9562 }; 9563 9564 auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) { 9565 Hash.clear(); 9566 Hash.AddTemplateArgument(TA); 9567 return Hash.CalculateHash(); 9568 }; 9569 9570 auto ComputeTemplateParameterListODRHash = 9571 [&Hash](const TemplateParameterList *TPL) { 9572 assert(TPL); 9573 Hash.clear(); 9574 Hash.AddTemplateParameterList(TPL); 9575 return Hash.CalculateHash(); 9576 }; 9577 9578 // Used with err_module_odr_violation_mismatch_decl and 9579 // note_module_odr_violation_mismatch_decl 9580 // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed 9581 enum ODRMismatchDecl { 9582 EndOfClass, 9583 PublicSpecifer, 9584 PrivateSpecifer, 9585 ProtectedSpecifer, 9586 StaticAssert, 9587 Field, 9588 CXXMethod, 9589 TypeAlias, 9590 TypeDef, 9591 Var, 9592 Friend, 9593 FunctionTemplate, 9594 Other 9595 }; 9596 9597 // Used with err_module_odr_violation_mismatch_decl_diff and 9598 // note_module_odr_violation_mismatch_decl_diff 9599 enum ODRMismatchDeclDifference { 9600 StaticAssertCondition, 9601 StaticAssertMessage, 9602 StaticAssertOnlyMessage, 9603 FieldName, 9604 FieldTypeName, 9605 FieldSingleBitField, 9606 FieldDifferentWidthBitField, 9607 FieldSingleMutable, 9608 FieldSingleInitializer, 9609 FieldDifferentInitializers, 9610 MethodName, 9611 MethodDeleted, 9612 MethodDefaulted, 9613 MethodVirtual, 9614 MethodStatic, 9615 MethodVolatile, 9616 MethodConst, 9617 MethodInline, 9618 MethodNumberParameters, 9619 MethodParameterType, 9620 MethodParameterName, 9621 MethodParameterSingleDefaultArgument, 9622 MethodParameterDifferentDefaultArgument, 9623 MethodNoTemplateArguments, 9624 MethodDifferentNumberTemplateArguments, 9625 MethodDifferentTemplateArgument, 9626 MethodSingleBody, 9627 MethodDifferentBody, 9628 TypedefName, 9629 TypedefType, 9630 VarName, 9631 VarType, 9632 VarSingleInitializer, 9633 VarDifferentInitializer, 9634 VarConstexpr, 9635 FriendTypeFunction, 9636 FriendType, 9637 FriendFunction, 9638 FunctionTemplateDifferentNumberParameters, 9639 FunctionTemplateParameterDifferentKind, 9640 FunctionTemplateParameterName, 9641 FunctionTemplateParameterSingleDefaultArgument, 9642 FunctionTemplateParameterDifferentDefaultArgument, 9643 FunctionTemplateParameterDifferentType, 9644 FunctionTemplatePackParameter, 9645 }; 9646 9647 // These lambdas have the common portions of the ODR diagnostics. This 9648 // has the same return as Diag(), so addition parameters can be passed 9649 // in with operator<< 9650 auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule, 9651 SourceLocation Loc, SourceRange Range, 9652 ODRMismatchDeclDifference DiffType) { 9653 return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff) 9654 << FirstRecord << FirstModule.empty() << FirstModule << Range 9655 << DiffType; 9656 }; 9657 auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc, 9658 SourceRange Range, ODRMismatchDeclDifference DiffType) { 9659 return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff) 9660 << SecondModule << Range << DiffType; 9661 }; 9662 9663 auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote, 9664 &ComputeQualTypeODRHash, &ComputeODRHash]( 9665 NamedDecl *FirstRecord, StringRef FirstModule, 9666 StringRef SecondModule, FieldDecl *FirstField, 9667 FieldDecl *SecondField) { 9668 IdentifierInfo *FirstII = FirstField->getIdentifier(); 9669 IdentifierInfo *SecondII = SecondField->getIdentifier(); 9670 if (FirstII->getName() != SecondII->getName()) { 9671 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9672 FirstField->getSourceRange(), FieldName) 9673 << FirstII; 9674 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9675 SecondField->getSourceRange(), FieldName) 9676 << SecondII; 9677 9678 return true; 9679 } 9680 9681 assert(getContext().hasSameType(FirstField->getType(), 9682 SecondField->getType())); 9683 9684 QualType FirstType = FirstField->getType(); 9685 QualType SecondType = SecondField->getType(); 9686 if (ComputeQualTypeODRHash(FirstType) != 9687 ComputeQualTypeODRHash(SecondType)) { 9688 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9689 FirstField->getSourceRange(), FieldTypeName) 9690 << FirstII << FirstType; 9691 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9692 SecondField->getSourceRange(), FieldTypeName) 9693 << SecondII << SecondType; 9694 9695 return true; 9696 } 9697 9698 const bool IsFirstBitField = FirstField->isBitField(); 9699 const bool IsSecondBitField = SecondField->isBitField(); 9700 if (IsFirstBitField != IsSecondBitField) { 9701 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9702 FirstField->getSourceRange(), FieldSingleBitField) 9703 << FirstII << IsFirstBitField; 9704 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9705 SecondField->getSourceRange(), FieldSingleBitField) 9706 << SecondII << IsSecondBitField; 9707 return true; 9708 } 9709 9710 if (IsFirstBitField && IsSecondBitField) { 9711 unsigned FirstBitWidthHash = 9712 ComputeODRHash(FirstField->getBitWidth()); 9713 unsigned SecondBitWidthHash = 9714 ComputeODRHash(SecondField->getBitWidth()); 9715 if (FirstBitWidthHash != SecondBitWidthHash) { 9716 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9717 FirstField->getSourceRange(), 9718 FieldDifferentWidthBitField) 9719 << FirstII << FirstField->getBitWidth()->getSourceRange(); 9720 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9721 SecondField->getSourceRange(), 9722 FieldDifferentWidthBitField) 9723 << SecondII << SecondField->getBitWidth()->getSourceRange(); 9724 return true; 9725 } 9726 } 9727 9728 if (!PP.getLangOpts().CPlusPlus) 9729 return false; 9730 9731 const bool IsFirstMutable = FirstField->isMutable(); 9732 const bool IsSecondMutable = SecondField->isMutable(); 9733 if (IsFirstMutable != IsSecondMutable) { 9734 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9735 FirstField->getSourceRange(), FieldSingleMutable) 9736 << FirstII << IsFirstMutable; 9737 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9738 SecondField->getSourceRange(), FieldSingleMutable) 9739 << SecondII << IsSecondMutable; 9740 return true; 9741 } 9742 9743 const Expr *FirstInitializer = FirstField->getInClassInitializer(); 9744 const Expr *SecondInitializer = SecondField->getInClassInitializer(); 9745 if ((!FirstInitializer && SecondInitializer) || 9746 (FirstInitializer && !SecondInitializer)) { 9747 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9748 FirstField->getSourceRange(), FieldSingleInitializer) 9749 << FirstII << (FirstInitializer != nullptr); 9750 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9751 SecondField->getSourceRange(), FieldSingleInitializer) 9752 << SecondII << (SecondInitializer != nullptr); 9753 return true; 9754 } 9755 9756 if (FirstInitializer && SecondInitializer) { 9757 unsigned FirstInitHash = ComputeODRHash(FirstInitializer); 9758 unsigned SecondInitHash = ComputeODRHash(SecondInitializer); 9759 if (FirstInitHash != SecondInitHash) { 9760 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9761 FirstField->getSourceRange(), 9762 FieldDifferentInitializers) 9763 << FirstII << FirstInitializer->getSourceRange(); 9764 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9765 SecondField->getSourceRange(), 9766 FieldDifferentInitializers) 9767 << SecondII << SecondInitializer->getSourceRange(); 9768 return true; 9769 } 9770 } 9771 9772 return false; 9773 }; 9774 9775 auto ODRDiagTypeDefOrAlias = 9776 [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash]( 9777 NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule, 9778 TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD, 9779 bool IsTypeAlias) { 9780 auto FirstName = FirstTD->getDeclName(); 9781 auto SecondName = SecondTD->getDeclName(); 9782 if (FirstName != SecondName) { 9783 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(), 9784 FirstTD->getSourceRange(), TypedefName) 9785 << IsTypeAlias << FirstName; 9786 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(), 9787 SecondTD->getSourceRange(), TypedefName) 9788 << IsTypeAlias << SecondName; 9789 return true; 9790 } 9791 9792 QualType FirstType = FirstTD->getUnderlyingType(); 9793 QualType SecondType = SecondTD->getUnderlyingType(); 9794 if (ComputeQualTypeODRHash(FirstType) != 9795 ComputeQualTypeODRHash(SecondType)) { 9796 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(), 9797 FirstTD->getSourceRange(), TypedefType) 9798 << IsTypeAlias << FirstName << FirstType; 9799 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(), 9800 SecondTD->getSourceRange(), TypedefType) 9801 << IsTypeAlias << SecondName << SecondType; 9802 return true; 9803 } 9804 9805 return false; 9806 }; 9807 9808 auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote, 9809 &ComputeQualTypeODRHash, &ComputeODRHash, 9810 this](NamedDecl *FirstRecord, StringRef FirstModule, 9811 StringRef SecondModule, VarDecl *FirstVD, 9812 VarDecl *SecondVD) { 9813 auto FirstName = FirstVD->getDeclName(); 9814 auto SecondName = SecondVD->getDeclName(); 9815 if (FirstName != SecondName) { 9816 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9817 FirstVD->getSourceRange(), VarName) 9818 << FirstName; 9819 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9820 SecondVD->getSourceRange(), VarName) 9821 << SecondName; 9822 return true; 9823 } 9824 9825 QualType FirstType = FirstVD->getType(); 9826 QualType SecondType = SecondVD->getType(); 9827 if (ComputeQualTypeODRHash(FirstType) != 9828 ComputeQualTypeODRHash(SecondType)) { 9829 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9830 FirstVD->getSourceRange(), VarType) 9831 << FirstName << FirstType; 9832 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9833 SecondVD->getSourceRange(), VarType) 9834 << SecondName << SecondType; 9835 return true; 9836 } 9837 9838 if (!PP.getLangOpts().CPlusPlus) 9839 return false; 9840 9841 const Expr *FirstInit = FirstVD->getInit(); 9842 const Expr *SecondInit = SecondVD->getInit(); 9843 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 9844 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9845 FirstVD->getSourceRange(), VarSingleInitializer) 9846 << FirstName << (FirstInit == nullptr) 9847 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 9848 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9849 SecondVD->getSourceRange(), VarSingleInitializer) 9850 << SecondName << (SecondInit == nullptr) 9851 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 9852 return true; 9853 } 9854 9855 if (FirstInit && SecondInit && 9856 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 9857 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9858 FirstVD->getSourceRange(), VarDifferentInitializer) 9859 << FirstName << FirstInit->getSourceRange(); 9860 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9861 SecondVD->getSourceRange(), VarDifferentInitializer) 9862 << SecondName << SecondInit->getSourceRange(); 9863 return true; 9864 } 9865 9866 const bool FirstIsConstexpr = FirstVD->isConstexpr(); 9867 const bool SecondIsConstexpr = SecondVD->isConstexpr(); 9868 if (FirstIsConstexpr != SecondIsConstexpr) { 9869 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9870 FirstVD->getSourceRange(), VarConstexpr) 9871 << FirstName << FirstIsConstexpr; 9872 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9873 SecondVD->getSourceRange(), VarConstexpr) 9874 << SecondName << SecondIsConstexpr; 9875 return true; 9876 } 9877 return false; 9878 }; 9879 9880 auto DifferenceSelector = [](Decl *D) { 9881 assert(D && "valid Decl required"); 9882 switch (D->getKind()) { 9883 default: 9884 return Other; 9885 case Decl::AccessSpec: 9886 switch (D->getAccess()) { 9887 case AS_public: 9888 return PublicSpecifer; 9889 case AS_private: 9890 return PrivateSpecifer; 9891 case AS_protected: 9892 return ProtectedSpecifer; 9893 case AS_none: 9894 break; 9895 } 9896 llvm_unreachable("Invalid access specifier"); 9897 case Decl::StaticAssert: 9898 return StaticAssert; 9899 case Decl::Field: 9900 return Field; 9901 case Decl::CXXMethod: 9902 case Decl::CXXConstructor: 9903 case Decl::CXXDestructor: 9904 return CXXMethod; 9905 case Decl::TypeAlias: 9906 return TypeAlias; 9907 case Decl::Typedef: 9908 return TypeDef; 9909 case Decl::Var: 9910 return Var; 9911 case Decl::Friend: 9912 return Friend; 9913 case Decl::FunctionTemplate: 9914 return FunctionTemplate; 9915 } 9916 }; 9917 9918 using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>; 9919 auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes, 9920 RecordDecl *Record, 9921 const DeclContext *DC) { 9922 for (auto *D : Record->decls()) { 9923 if (!ODRHash::isDeclToBeProcessed(D, DC)) 9924 continue; 9925 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 9926 } 9927 }; 9928 9929 struct DiffResult { 9930 Decl *FirstDecl = nullptr, *SecondDecl = nullptr; 9931 ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other; 9932 }; 9933 9934 // If there is a diagnoseable difference, FirstDiffType and 9935 // SecondDiffType will not be Other and FirstDecl and SecondDecl will be 9936 // filled in if not EndOfClass. 9937 auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes, 9938 DeclHashes &SecondHashes) { 9939 DiffResult DR; 9940 auto FirstIt = FirstHashes.begin(); 9941 auto SecondIt = SecondHashes.begin(); 9942 while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) { 9943 if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() && 9944 FirstIt->second == SecondIt->second) { 9945 ++FirstIt; 9946 ++SecondIt; 9947 continue; 9948 } 9949 9950 DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first; 9951 DR.SecondDecl = 9952 SecondIt == SecondHashes.end() ? nullptr : SecondIt->first; 9953 9954 DR.FirstDiffType = 9955 DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass; 9956 DR.SecondDiffType = 9957 DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass; 9958 return DR; 9959 } 9960 return DR; 9961 }; 9962 9963 // Use this to diagnose that an unexpected Decl was encountered 9964 // or no difference was detected. This causes a generic error 9965 // message to be emitted. 9966 auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord, 9967 StringRef FirstModule, 9968 NamedDecl *SecondRecord, 9969 StringRef SecondModule) { 9970 Diag(FirstRecord->getLocation(), 9971 diag::err_module_odr_violation_different_definitions) 9972 << FirstRecord << FirstModule.empty() << FirstModule; 9973 9974 if (DR.FirstDecl) { 9975 Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference) 9976 << FirstRecord << DR.FirstDecl->getSourceRange(); 9977 } 9978 9979 Diag(SecondRecord->getLocation(), 9980 diag::note_module_odr_violation_different_definitions) 9981 << SecondModule; 9982 9983 if (DR.SecondDecl) { 9984 Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference) 9985 << DR.SecondDecl->getSourceRange(); 9986 } 9987 }; 9988 9989 auto DiagnoseODRMismatch = 9990 [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule, 9991 NamedDecl *SecondRecord, StringRef SecondModule) { 9992 SourceLocation FirstLoc; 9993 SourceRange FirstRange; 9994 auto *FirstTag = dyn_cast<TagDecl>(FirstRecord); 9995 if (DR.FirstDiffType == EndOfClass && FirstTag) { 9996 FirstLoc = FirstTag->getBraceRange().getEnd(); 9997 } else { 9998 FirstLoc = DR.FirstDecl->getLocation(); 9999 FirstRange = DR.FirstDecl->getSourceRange(); 10000 } 10001 Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl) 10002 << FirstRecord << FirstModule.empty() << FirstModule << FirstRange 10003 << DR.FirstDiffType; 10004 10005 SourceLocation SecondLoc; 10006 SourceRange SecondRange; 10007 auto *SecondTag = dyn_cast<TagDecl>(SecondRecord); 10008 if (DR.SecondDiffType == EndOfClass && SecondTag) { 10009 SecondLoc = SecondTag->getBraceRange().getEnd(); 10010 } else { 10011 SecondLoc = DR.SecondDecl->getLocation(); 10012 SecondRange = DR.SecondDecl->getSourceRange(); 10013 } 10014 Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl) 10015 << SecondModule << SecondRange << DR.SecondDiffType; 10016 }; 10017 10018 // Issue any pending ODR-failure diagnostics. 10019 for (auto &Merge : OdrMergeFailures) { 10020 // If we've already pointed out a specific problem with this class, don't 10021 // bother issuing a general "something's different" diagnostic. 10022 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 10023 continue; 10024 10025 bool Diagnosed = false; 10026 CXXRecordDecl *FirstRecord = Merge.first; 10027 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord); 10028 for (auto &RecordPair : Merge.second) { 10029 CXXRecordDecl *SecondRecord = RecordPair.first; 10030 // Multiple different declarations got merged together; tell the user 10031 // where they came from. 10032 if (FirstRecord == SecondRecord) 10033 continue; 10034 10035 std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord); 10036 10037 auto *FirstDD = FirstRecord->DefinitionData; 10038 auto *SecondDD = RecordPair.second; 10039 10040 assert(FirstDD && SecondDD && "Definitions without DefinitionData"); 10041 10042 // Diagnostics from DefinitionData are emitted here. 10043 if (FirstDD != SecondDD) { 10044 enum ODRDefinitionDataDifference { 10045 NumBases, 10046 NumVBases, 10047 BaseType, 10048 BaseVirtual, 10049 BaseAccess, 10050 }; 10051 auto ODRDiagBaseError = [FirstRecord, &FirstModule, 10052 this](SourceLocation Loc, SourceRange Range, 10053 ODRDefinitionDataDifference DiffType) { 10054 return Diag(Loc, diag::err_module_odr_violation_definition_data) 10055 << FirstRecord << FirstModule.empty() << FirstModule << Range 10056 << DiffType; 10057 }; 10058 auto ODRDiagBaseNote = [&SecondModule, 10059 this](SourceLocation Loc, SourceRange Range, 10060 ODRDefinitionDataDifference DiffType) { 10061 return Diag(Loc, diag::note_module_odr_violation_definition_data) 10062 << SecondModule << Range << DiffType; 10063 }; 10064 10065 unsigned FirstNumBases = FirstDD->NumBases; 10066 unsigned FirstNumVBases = FirstDD->NumVBases; 10067 unsigned SecondNumBases = SecondDD->NumBases; 10068 unsigned SecondNumVBases = SecondDD->NumVBases; 10069 10070 auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) { 10071 unsigned NumBases = DD->NumBases; 10072 if (NumBases == 0) return SourceRange(); 10073 auto bases = DD->bases(); 10074 return SourceRange(bases[0].getBeginLoc(), 10075 bases[NumBases - 1].getEndLoc()); 10076 }; 10077 10078 if (FirstNumBases != SecondNumBases) { 10079 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10080 NumBases) 10081 << FirstNumBases; 10082 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10083 NumBases) 10084 << SecondNumBases; 10085 Diagnosed = true; 10086 break; 10087 } 10088 10089 if (FirstNumVBases != SecondNumVBases) { 10090 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10091 NumVBases) 10092 << FirstNumVBases; 10093 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10094 NumVBases) 10095 << SecondNumVBases; 10096 Diagnosed = true; 10097 break; 10098 } 10099 10100 auto FirstBases = FirstDD->bases(); 10101 auto SecondBases = SecondDD->bases(); 10102 unsigned i = 0; 10103 for (i = 0; i < FirstNumBases; ++i) { 10104 auto FirstBase = FirstBases[i]; 10105 auto SecondBase = SecondBases[i]; 10106 if (ComputeQualTypeODRHash(FirstBase.getType()) != 10107 ComputeQualTypeODRHash(SecondBase.getType())) { 10108 ODRDiagBaseError(FirstRecord->getLocation(), 10109 FirstBase.getSourceRange(), BaseType) 10110 << (i + 1) << FirstBase.getType(); 10111 ODRDiagBaseNote(SecondRecord->getLocation(), 10112 SecondBase.getSourceRange(), BaseType) 10113 << (i + 1) << SecondBase.getType(); 10114 break; 10115 } 10116 10117 if (FirstBase.isVirtual() != SecondBase.isVirtual()) { 10118 ODRDiagBaseError(FirstRecord->getLocation(), 10119 FirstBase.getSourceRange(), BaseVirtual) 10120 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType(); 10121 ODRDiagBaseNote(SecondRecord->getLocation(), 10122 SecondBase.getSourceRange(), BaseVirtual) 10123 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType(); 10124 break; 10125 } 10126 10127 if (FirstBase.getAccessSpecifierAsWritten() != 10128 SecondBase.getAccessSpecifierAsWritten()) { 10129 ODRDiagBaseError(FirstRecord->getLocation(), 10130 FirstBase.getSourceRange(), BaseAccess) 10131 << (i + 1) << FirstBase.getType() 10132 << (int)FirstBase.getAccessSpecifierAsWritten(); 10133 ODRDiagBaseNote(SecondRecord->getLocation(), 10134 SecondBase.getSourceRange(), BaseAccess) 10135 << (i + 1) << SecondBase.getType() 10136 << (int)SecondBase.getAccessSpecifierAsWritten(); 10137 break; 10138 } 10139 } 10140 10141 if (i != FirstNumBases) { 10142 Diagnosed = true; 10143 break; 10144 } 10145 } 10146 10147 const ClassTemplateDecl *FirstTemplate = 10148 FirstRecord->getDescribedClassTemplate(); 10149 const ClassTemplateDecl *SecondTemplate = 10150 SecondRecord->getDescribedClassTemplate(); 10151 10152 assert(!FirstTemplate == !SecondTemplate && 10153 "Both pointers should be null or non-null"); 10154 10155 enum ODRTemplateDifference { 10156 ParamEmptyName, 10157 ParamName, 10158 ParamSingleDefaultArgument, 10159 ParamDifferentDefaultArgument, 10160 }; 10161 10162 if (FirstTemplate && SecondTemplate) { 10163 DeclHashes FirstTemplateHashes; 10164 DeclHashes SecondTemplateHashes; 10165 10166 auto PopulateTemplateParameterHashs = 10167 [&ComputeSubDeclODRHash](DeclHashes &Hashes, 10168 const ClassTemplateDecl *TD) { 10169 for (auto *D : TD->getTemplateParameters()->asArray()) { 10170 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 10171 } 10172 }; 10173 10174 PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate); 10175 PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate); 10176 10177 assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() && 10178 "Number of template parameters should be equal."); 10179 10180 auto FirstIt = FirstTemplateHashes.begin(); 10181 auto FirstEnd = FirstTemplateHashes.end(); 10182 auto SecondIt = SecondTemplateHashes.begin(); 10183 for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) { 10184 if (FirstIt->second == SecondIt->second) 10185 continue; 10186 10187 auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this]( 10188 SourceLocation Loc, SourceRange Range, 10189 ODRTemplateDifference DiffType) { 10190 return Diag(Loc, diag::err_module_odr_violation_template_parameter) 10191 << FirstRecord << FirstModule.empty() << FirstModule << Range 10192 << DiffType; 10193 }; 10194 auto ODRDiagTemplateNote = [&SecondModule, this]( 10195 SourceLocation Loc, SourceRange Range, 10196 ODRTemplateDifference DiffType) { 10197 return Diag(Loc, diag::note_module_odr_violation_template_parameter) 10198 << SecondModule << Range << DiffType; 10199 }; 10200 10201 const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first); 10202 const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first); 10203 10204 assert(FirstDecl->getKind() == SecondDecl->getKind() && 10205 "Parameter Decl's should be the same kind."); 10206 10207 DeclarationName FirstName = FirstDecl->getDeclName(); 10208 DeclarationName SecondName = SecondDecl->getDeclName(); 10209 10210 if (FirstName != SecondName) { 10211 const bool FirstNameEmpty = 10212 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo(); 10213 const bool SecondNameEmpty = 10214 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo(); 10215 assert((!FirstNameEmpty || !SecondNameEmpty) && 10216 "Both template parameters cannot be unnamed."); 10217 ODRDiagTemplateError(FirstDecl->getLocation(), 10218 FirstDecl->getSourceRange(), 10219 FirstNameEmpty ? ParamEmptyName : ParamName) 10220 << FirstName; 10221 ODRDiagTemplateNote(SecondDecl->getLocation(), 10222 SecondDecl->getSourceRange(), 10223 SecondNameEmpty ? ParamEmptyName : ParamName) 10224 << SecondName; 10225 break; 10226 } 10227 10228 switch (FirstDecl->getKind()) { 10229 default: 10230 llvm_unreachable("Invalid template parameter type."); 10231 case Decl::TemplateTypeParm: { 10232 const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl); 10233 const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl); 10234 const bool HasFirstDefaultArgument = 10235 FirstParam->hasDefaultArgument() && 10236 !FirstParam->defaultArgumentWasInherited(); 10237 const bool HasSecondDefaultArgument = 10238 SecondParam->hasDefaultArgument() && 10239 !SecondParam->defaultArgumentWasInherited(); 10240 10241 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10242 ODRDiagTemplateError(FirstDecl->getLocation(), 10243 FirstDecl->getSourceRange(), 10244 ParamSingleDefaultArgument) 10245 << HasFirstDefaultArgument; 10246 ODRDiagTemplateNote(SecondDecl->getLocation(), 10247 SecondDecl->getSourceRange(), 10248 ParamSingleDefaultArgument) 10249 << HasSecondDefaultArgument; 10250 break; 10251 } 10252 10253 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10254 "Expecting default arguments."); 10255 10256 ODRDiagTemplateError(FirstDecl->getLocation(), 10257 FirstDecl->getSourceRange(), 10258 ParamDifferentDefaultArgument); 10259 ODRDiagTemplateNote(SecondDecl->getLocation(), 10260 SecondDecl->getSourceRange(), 10261 ParamDifferentDefaultArgument); 10262 10263 break; 10264 } 10265 case Decl::NonTypeTemplateParm: { 10266 const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl); 10267 const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl); 10268 const bool HasFirstDefaultArgument = 10269 FirstParam->hasDefaultArgument() && 10270 !FirstParam->defaultArgumentWasInherited(); 10271 const bool HasSecondDefaultArgument = 10272 SecondParam->hasDefaultArgument() && 10273 !SecondParam->defaultArgumentWasInherited(); 10274 10275 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10276 ODRDiagTemplateError(FirstDecl->getLocation(), 10277 FirstDecl->getSourceRange(), 10278 ParamSingleDefaultArgument) 10279 << HasFirstDefaultArgument; 10280 ODRDiagTemplateNote(SecondDecl->getLocation(), 10281 SecondDecl->getSourceRange(), 10282 ParamSingleDefaultArgument) 10283 << HasSecondDefaultArgument; 10284 break; 10285 } 10286 10287 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10288 "Expecting default arguments."); 10289 10290 ODRDiagTemplateError(FirstDecl->getLocation(), 10291 FirstDecl->getSourceRange(), 10292 ParamDifferentDefaultArgument); 10293 ODRDiagTemplateNote(SecondDecl->getLocation(), 10294 SecondDecl->getSourceRange(), 10295 ParamDifferentDefaultArgument); 10296 10297 break; 10298 } 10299 case Decl::TemplateTemplateParm: { 10300 const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl); 10301 const auto *SecondParam = 10302 cast<TemplateTemplateParmDecl>(SecondDecl); 10303 const bool HasFirstDefaultArgument = 10304 FirstParam->hasDefaultArgument() && 10305 !FirstParam->defaultArgumentWasInherited(); 10306 const bool HasSecondDefaultArgument = 10307 SecondParam->hasDefaultArgument() && 10308 !SecondParam->defaultArgumentWasInherited(); 10309 10310 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10311 ODRDiagTemplateError(FirstDecl->getLocation(), 10312 FirstDecl->getSourceRange(), 10313 ParamSingleDefaultArgument) 10314 << HasFirstDefaultArgument; 10315 ODRDiagTemplateNote(SecondDecl->getLocation(), 10316 SecondDecl->getSourceRange(), 10317 ParamSingleDefaultArgument) 10318 << HasSecondDefaultArgument; 10319 break; 10320 } 10321 10322 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10323 "Expecting default arguments."); 10324 10325 ODRDiagTemplateError(FirstDecl->getLocation(), 10326 FirstDecl->getSourceRange(), 10327 ParamDifferentDefaultArgument); 10328 ODRDiagTemplateNote(SecondDecl->getLocation(), 10329 SecondDecl->getSourceRange(), 10330 ParamDifferentDefaultArgument); 10331 10332 break; 10333 } 10334 } 10335 10336 break; 10337 } 10338 10339 if (FirstIt != FirstEnd) { 10340 Diagnosed = true; 10341 break; 10342 } 10343 } 10344 10345 DeclHashes FirstHashes; 10346 DeclHashes SecondHashes; 10347 const DeclContext *DC = FirstRecord; 10348 PopulateHashes(FirstHashes, FirstRecord, DC); 10349 PopulateHashes(SecondHashes, SecondRecord, DC); 10350 10351 auto DR = FindTypeDiffs(FirstHashes, SecondHashes); 10352 ODRMismatchDecl FirstDiffType = DR.FirstDiffType; 10353 ODRMismatchDecl SecondDiffType = DR.SecondDiffType; 10354 Decl *FirstDecl = DR.FirstDecl; 10355 Decl *SecondDecl = DR.SecondDecl; 10356 10357 if (FirstDiffType == Other || SecondDiffType == Other) { 10358 DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord, 10359 SecondModule); 10360 Diagnosed = true; 10361 break; 10362 } 10363 10364 if (FirstDiffType != SecondDiffType) { 10365 DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord, 10366 SecondModule); 10367 Diagnosed = true; 10368 break; 10369 } 10370 10371 assert(FirstDiffType == SecondDiffType); 10372 10373 switch (FirstDiffType) { 10374 case Other: 10375 case EndOfClass: 10376 case PublicSpecifer: 10377 case PrivateSpecifer: 10378 case ProtectedSpecifer: 10379 llvm_unreachable("Invalid diff type"); 10380 10381 case StaticAssert: { 10382 StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl); 10383 StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl); 10384 10385 Expr *FirstExpr = FirstSA->getAssertExpr(); 10386 Expr *SecondExpr = SecondSA->getAssertExpr(); 10387 unsigned FirstODRHash = ComputeODRHash(FirstExpr); 10388 unsigned SecondODRHash = ComputeODRHash(SecondExpr); 10389 if (FirstODRHash != SecondODRHash) { 10390 ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(), 10391 FirstExpr->getSourceRange(), StaticAssertCondition); 10392 ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(), 10393 SecondExpr->getSourceRange(), StaticAssertCondition); 10394 Diagnosed = true; 10395 break; 10396 } 10397 10398 StringLiteral *FirstStr = FirstSA->getMessage(); 10399 StringLiteral *SecondStr = SecondSA->getMessage(); 10400 assert((FirstStr || SecondStr) && "Both messages cannot be empty"); 10401 if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) { 10402 SourceLocation FirstLoc, SecondLoc; 10403 SourceRange FirstRange, SecondRange; 10404 if (FirstStr) { 10405 FirstLoc = FirstStr->getBeginLoc(); 10406 FirstRange = FirstStr->getSourceRange(); 10407 } else { 10408 FirstLoc = FirstSA->getBeginLoc(); 10409 FirstRange = FirstSA->getSourceRange(); 10410 } 10411 if (SecondStr) { 10412 SecondLoc = SecondStr->getBeginLoc(); 10413 SecondRange = SecondStr->getSourceRange(); 10414 } else { 10415 SecondLoc = SecondSA->getBeginLoc(); 10416 SecondRange = SecondSA->getSourceRange(); 10417 } 10418 ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange, 10419 StaticAssertOnlyMessage) 10420 << (FirstStr == nullptr); 10421 ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange, 10422 StaticAssertOnlyMessage) 10423 << (SecondStr == nullptr); 10424 Diagnosed = true; 10425 break; 10426 } 10427 10428 if (FirstStr && SecondStr && 10429 FirstStr->getString() != SecondStr->getString()) { 10430 ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(), 10431 FirstStr->getSourceRange(), StaticAssertMessage); 10432 ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(), 10433 SecondStr->getSourceRange(), StaticAssertMessage); 10434 Diagnosed = true; 10435 break; 10436 } 10437 break; 10438 } 10439 case Field: { 10440 Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule, 10441 cast<FieldDecl>(FirstDecl), 10442 cast<FieldDecl>(SecondDecl)); 10443 break; 10444 } 10445 case CXXMethod: { 10446 enum { 10447 DiagMethod, 10448 DiagConstructor, 10449 DiagDestructor, 10450 } FirstMethodType, 10451 SecondMethodType; 10452 auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) { 10453 if (isa<CXXConstructorDecl>(D)) return DiagConstructor; 10454 if (isa<CXXDestructorDecl>(D)) return DiagDestructor; 10455 return DiagMethod; 10456 }; 10457 const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl); 10458 const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl); 10459 FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod); 10460 SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod); 10461 auto FirstName = FirstMethod->getDeclName(); 10462 auto SecondName = SecondMethod->getDeclName(); 10463 if (FirstMethodType != SecondMethodType || FirstName != SecondName) { 10464 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10465 FirstMethod->getSourceRange(), MethodName) 10466 << FirstMethodType << FirstName; 10467 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10468 SecondMethod->getSourceRange(), MethodName) 10469 << SecondMethodType << SecondName; 10470 10471 Diagnosed = true; 10472 break; 10473 } 10474 10475 const bool FirstDeleted = FirstMethod->isDeletedAsWritten(); 10476 const bool SecondDeleted = SecondMethod->isDeletedAsWritten(); 10477 if (FirstDeleted != SecondDeleted) { 10478 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10479 FirstMethod->getSourceRange(), MethodDeleted) 10480 << FirstMethodType << FirstName << FirstDeleted; 10481 10482 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10483 SecondMethod->getSourceRange(), MethodDeleted) 10484 << SecondMethodType << SecondName << SecondDeleted; 10485 Diagnosed = true; 10486 break; 10487 } 10488 10489 const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted(); 10490 const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted(); 10491 if (FirstDefaulted != SecondDefaulted) { 10492 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10493 FirstMethod->getSourceRange(), MethodDefaulted) 10494 << FirstMethodType << FirstName << FirstDefaulted; 10495 10496 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10497 SecondMethod->getSourceRange(), MethodDefaulted) 10498 << SecondMethodType << SecondName << SecondDefaulted; 10499 Diagnosed = true; 10500 break; 10501 } 10502 10503 const bool FirstVirtual = FirstMethod->isVirtualAsWritten(); 10504 const bool SecondVirtual = SecondMethod->isVirtualAsWritten(); 10505 const bool FirstPure = FirstMethod->isPure(); 10506 const bool SecondPure = SecondMethod->isPure(); 10507 if ((FirstVirtual || SecondVirtual) && 10508 (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) { 10509 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10510 FirstMethod->getSourceRange(), MethodVirtual) 10511 << FirstMethodType << FirstName << FirstPure << FirstVirtual; 10512 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10513 SecondMethod->getSourceRange(), MethodVirtual) 10514 << SecondMethodType << SecondName << SecondPure << SecondVirtual; 10515 Diagnosed = true; 10516 break; 10517 } 10518 10519 // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging, 10520 // FirstDecl is the canonical Decl of SecondDecl, so the storage 10521 // class needs to be checked instead. 10522 const auto FirstStorage = FirstMethod->getStorageClass(); 10523 const auto SecondStorage = SecondMethod->getStorageClass(); 10524 const bool FirstStatic = FirstStorage == SC_Static; 10525 const bool SecondStatic = SecondStorage == SC_Static; 10526 if (FirstStatic != SecondStatic) { 10527 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10528 FirstMethod->getSourceRange(), MethodStatic) 10529 << FirstMethodType << FirstName << FirstStatic; 10530 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10531 SecondMethod->getSourceRange(), MethodStatic) 10532 << SecondMethodType << SecondName << SecondStatic; 10533 Diagnosed = true; 10534 break; 10535 } 10536 10537 const bool FirstVolatile = FirstMethod->isVolatile(); 10538 const bool SecondVolatile = SecondMethod->isVolatile(); 10539 if (FirstVolatile != SecondVolatile) { 10540 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10541 FirstMethod->getSourceRange(), MethodVolatile) 10542 << FirstMethodType << FirstName << FirstVolatile; 10543 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10544 SecondMethod->getSourceRange(), MethodVolatile) 10545 << SecondMethodType << SecondName << SecondVolatile; 10546 Diagnosed = true; 10547 break; 10548 } 10549 10550 const bool FirstConst = FirstMethod->isConst(); 10551 const bool SecondConst = SecondMethod->isConst(); 10552 if (FirstConst != SecondConst) { 10553 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10554 FirstMethod->getSourceRange(), MethodConst) 10555 << FirstMethodType << FirstName << FirstConst; 10556 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10557 SecondMethod->getSourceRange(), MethodConst) 10558 << SecondMethodType << SecondName << SecondConst; 10559 Diagnosed = true; 10560 break; 10561 } 10562 10563 const bool FirstInline = FirstMethod->isInlineSpecified(); 10564 const bool SecondInline = SecondMethod->isInlineSpecified(); 10565 if (FirstInline != SecondInline) { 10566 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10567 FirstMethod->getSourceRange(), MethodInline) 10568 << FirstMethodType << FirstName << FirstInline; 10569 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10570 SecondMethod->getSourceRange(), MethodInline) 10571 << SecondMethodType << SecondName << SecondInline; 10572 Diagnosed = true; 10573 break; 10574 } 10575 10576 const unsigned FirstNumParameters = FirstMethod->param_size(); 10577 const unsigned SecondNumParameters = SecondMethod->param_size(); 10578 if (FirstNumParameters != SecondNumParameters) { 10579 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10580 FirstMethod->getSourceRange(), 10581 MethodNumberParameters) 10582 << FirstMethodType << FirstName << FirstNumParameters; 10583 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10584 SecondMethod->getSourceRange(), 10585 MethodNumberParameters) 10586 << SecondMethodType << SecondName << SecondNumParameters; 10587 Diagnosed = true; 10588 break; 10589 } 10590 10591 // Need this status boolean to know when break out of the switch. 10592 bool ParameterMismatch = false; 10593 for (unsigned I = 0; I < FirstNumParameters; ++I) { 10594 const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I); 10595 const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I); 10596 10597 QualType FirstParamType = FirstParam->getType(); 10598 QualType SecondParamType = SecondParam->getType(); 10599 if (FirstParamType != SecondParamType && 10600 ComputeQualTypeODRHash(FirstParamType) != 10601 ComputeQualTypeODRHash(SecondParamType)) { 10602 if (const DecayedType *ParamDecayedType = 10603 FirstParamType->getAs<DecayedType>()) { 10604 ODRDiagDeclError( 10605 FirstRecord, FirstModule, FirstMethod->getLocation(), 10606 FirstMethod->getSourceRange(), MethodParameterType) 10607 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10608 << true << ParamDecayedType->getOriginalType(); 10609 } else { 10610 ODRDiagDeclError( 10611 FirstRecord, FirstModule, FirstMethod->getLocation(), 10612 FirstMethod->getSourceRange(), MethodParameterType) 10613 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10614 << false; 10615 } 10616 10617 if (const DecayedType *ParamDecayedType = 10618 SecondParamType->getAs<DecayedType>()) { 10619 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10620 SecondMethod->getSourceRange(), 10621 MethodParameterType) 10622 << SecondMethodType << SecondName << (I + 1) 10623 << SecondParamType << true 10624 << ParamDecayedType->getOriginalType(); 10625 } else { 10626 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10627 SecondMethod->getSourceRange(), 10628 MethodParameterType) 10629 << SecondMethodType << SecondName << (I + 1) 10630 << SecondParamType << false; 10631 } 10632 ParameterMismatch = true; 10633 break; 10634 } 10635 10636 DeclarationName FirstParamName = FirstParam->getDeclName(); 10637 DeclarationName SecondParamName = SecondParam->getDeclName(); 10638 if (FirstParamName != SecondParamName) { 10639 ODRDiagDeclError(FirstRecord, FirstModule, 10640 FirstMethod->getLocation(), 10641 FirstMethod->getSourceRange(), MethodParameterName) 10642 << FirstMethodType << FirstName << (I + 1) << FirstParamName; 10643 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10644 SecondMethod->getSourceRange(), MethodParameterName) 10645 << SecondMethodType << SecondName << (I + 1) << SecondParamName; 10646 ParameterMismatch = true; 10647 break; 10648 } 10649 10650 const Expr *FirstInit = FirstParam->getInit(); 10651 const Expr *SecondInit = SecondParam->getInit(); 10652 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 10653 ODRDiagDeclError(FirstRecord, FirstModule, 10654 FirstMethod->getLocation(), 10655 FirstMethod->getSourceRange(), 10656 MethodParameterSingleDefaultArgument) 10657 << FirstMethodType << FirstName << (I + 1) 10658 << (FirstInit == nullptr) 10659 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 10660 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10661 SecondMethod->getSourceRange(), 10662 MethodParameterSingleDefaultArgument) 10663 << SecondMethodType << SecondName << (I + 1) 10664 << (SecondInit == nullptr) 10665 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 10666 ParameterMismatch = true; 10667 break; 10668 } 10669 10670 if (FirstInit && SecondInit && 10671 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 10672 ODRDiagDeclError(FirstRecord, FirstModule, 10673 FirstMethod->getLocation(), 10674 FirstMethod->getSourceRange(), 10675 MethodParameterDifferentDefaultArgument) 10676 << FirstMethodType << FirstName << (I + 1) 10677 << FirstInit->getSourceRange(); 10678 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10679 SecondMethod->getSourceRange(), 10680 MethodParameterDifferentDefaultArgument) 10681 << SecondMethodType << SecondName << (I + 1) 10682 << SecondInit->getSourceRange(); 10683 ParameterMismatch = true; 10684 break; 10685 10686 } 10687 } 10688 10689 if (ParameterMismatch) { 10690 Diagnosed = true; 10691 break; 10692 } 10693 10694 const auto *FirstTemplateArgs = 10695 FirstMethod->getTemplateSpecializationArgs(); 10696 const auto *SecondTemplateArgs = 10697 SecondMethod->getTemplateSpecializationArgs(); 10698 10699 if ((FirstTemplateArgs && !SecondTemplateArgs) || 10700 (!FirstTemplateArgs && SecondTemplateArgs)) { 10701 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10702 FirstMethod->getSourceRange(), 10703 MethodNoTemplateArguments) 10704 << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr); 10705 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10706 SecondMethod->getSourceRange(), 10707 MethodNoTemplateArguments) 10708 << SecondMethodType << SecondName 10709 << (SecondTemplateArgs != nullptr); 10710 10711 Diagnosed = true; 10712 break; 10713 } 10714 10715 if (FirstTemplateArgs && SecondTemplateArgs) { 10716 // Remove pack expansions from argument list. 10717 auto ExpandTemplateArgumentList = 10718 [](const TemplateArgumentList *TAL) { 10719 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList; 10720 for (const TemplateArgument &TA : TAL->asArray()) { 10721 if (TA.getKind() != TemplateArgument::Pack) { 10722 ExpandedList.push_back(&TA); 10723 continue; 10724 } 10725 for (const TemplateArgument &PackTA : TA.getPackAsArray()) { 10726 ExpandedList.push_back(&PackTA); 10727 } 10728 } 10729 return ExpandedList; 10730 }; 10731 llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList = 10732 ExpandTemplateArgumentList(FirstTemplateArgs); 10733 llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList = 10734 ExpandTemplateArgumentList(SecondTemplateArgs); 10735 10736 if (FirstExpandedList.size() != SecondExpandedList.size()) { 10737 ODRDiagDeclError(FirstRecord, FirstModule, 10738 FirstMethod->getLocation(), 10739 FirstMethod->getSourceRange(), 10740 MethodDifferentNumberTemplateArguments) 10741 << FirstMethodType << FirstName 10742 << (unsigned)FirstExpandedList.size(); 10743 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10744 SecondMethod->getSourceRange(), 10745 MethodDifferentNumberTemplateArguments) 10746 << SecondMethodType << SecondName 10747 << (unsigned)SecondExpandedList.size(); 10748 10749 Diagnosed = true; 10750 break; 10751 } 10752 10753 bool TemplateArgumentMismatch = false; 10754 for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) { 10755 const TemplateArgument &FirstTA = *FirstExpandedList[i], 10756 &SecondTA = *SecondExpandedList[i]; 10757 if (ComputeTemplateArgumentODRHash(FirstTA) == 10758 ComputeTemplateArgumentODRHash(SecondTA)) { 10759 continue; 10760 } 10761 10762 ODRDiagDeclError( 10763 FirstRecord, FirstModule, FirstMethod->getLocation(), 10764 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument) 10765 << FirstMethodType << FirstName << FirstTA << i + 1; 10766 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10767 SecondMethod->getSourceRange(), 10768 MethodDifferentTemplateArgument) 10769 << SecondMethodType << SecondName << SecondTA << i + 1; 10770 10771 TemplateArgumentMismatch = true; 10772 break; 10773 } 10774 10775 if (TemplateArgumentMismatch) { 10776 Diagnosed = true; 10777 break; 10778 } 10779 } 10780 10781 // Compute the hash of the method as if it has no body. 10782 auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) { 10783 Hash.clear(); 10784 Hash.AddFunctionDecl(D, true /*SkipBody*/); 10785 return Hash.CalculateHash(); 10786 }; 10787 10788 // Compare the hash generated to the hash stored. A difference means 10789 // that a body was present in the original source. Due to merging, 10790 // the stardard way of detecting a body will not work. 10791 const bool HasFirstBody = 10792 ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash(); 10793 const bool HasSecondBody = 10794 ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash(); 10795 10796 if (HasFirstBody != HasSecondBody) { 10797 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10798 FirstMethod->getSourceRange(), MethodSingleBody) 10799 << FirstMethodType << FirstName << HasFirstBody; 10800 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10801 SecondMethod->getSourceRange(), MethodSingleBody) 10802 << SecondMethodType << SecondName << HasSecondBody; 10803 Diagnosed = true; 10804 break; 10805 } 10806 10807 if (HasFirstBody && HasSecondBody) { 10808 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10809 FirstMethod->getSourceRange(), MethodDifferentBody) 10810 << FirstMethodType << FirstName; 10811 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10812 SecondMethod->getSourceRange(), MethodDifferentBody) 10813 << SecondMethodType << SecondName; 10814 Diagnosed = true; 10815 break; 10816 } 10817 10818 break; 10819 } 10820 case TypeAlias: 10821 case TypeDef: { 10822 Diagnosed = ODRDiagTypeDefOrAlias( 10823 FirstRecord, FirstModule, SecondModule, 10824 cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl), 10825 FirstDiffType == TypeAlias); 10826 break; 10827 } 10828 case Var: { 10829 Diagnosed = 10830 ODRDiagVar(FirstRecord, FirstModule, SecondModule, 10831 cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl)); 10832 break; 10833 } 10834 case Friend: { 10835 FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl); 10836 FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl); 10837 10838 NamedDecl *FirstND = FirstFriend->getFriendDecl(); 10839 NamedDecl *SecondND = SecondFriend->getFriendDecl(); 10840 10841 TypeSourceInfo *FirstTSI = FirstFriend->getFriendType(); 10842 TypeSourceInfo *SecondTSI = SecondFriend->getFriendType(); 10843 10844 if (FirstND && SecondND) { 10845 ODRDiagDeclError(FirstRecord, FirstModule, 10846 FirstFriend->getFriendLoc(), 10847 FirstFriend->getSourceRange(), FriendFunction) 10848 << FirstND; 10849 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10850 SecondFriend->getSourceRange(), FriendFunction) 10851 << SecondND; 10852 10853 Diagnosed = true; 10854 break; 10855 } 10856 10857 if (FirstTSI && SecondTSI) { 10858 QualType FirstFriendType = FirstTSI->getType(); 10859 QualType SecondFriendType = SecondTSI->getType(); 10860 assert(ComputeQualTypeODRHash(FirstFriendType) != 10861 ComputeQualTypeODRHash(SecondFriendType)); 10862 ODRDiagDeclError(FirstRecord, FirstModule, 10863 FirstFriend->getFriendLoc(), 10864 FirstFriend->getSourceRange(), FriendType) 10865 << FirstFriendType; 10866 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10867 SecondFriend->getSourceRange(), FriendType) 10868 << SecondFriendType; 10869 Diagnosed = true; 10870 break; 10871 } 10872 10873 ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(), 10874 FirstFriend->getSourceRange(), FriendTypeFunction) 10875 << (FirstTSI == nullptr); 10876 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10877 SecondFriend->getSourceRange(), FriendTypeFunction) 10878 << (SecondTSI == nullptr); 10879 10880 Diagnosed = true; 10881 break; 10882 } 10883 case FunctionTemplate: { 10884 FunctionTemplateDecl *FirstTemplate = 10885 cast<FunctionTemplateDecl>(FirstDecl); 10886 FunctionTemplateDecl *SecondTemplate = 10887 cast<FunctionTemplateDecl>(SecondDecl); 10888 10889 TemplateParameterList *FirstTPL = 10890 FirstTemplate->getTemplateParameters(); 10891 TemplateParameterList *SecondTPL = 10892 SecondTemplate->getTemplateParameters(); 10893 10894 if (FirstTPL->size() != SecondTPL->size()) { 10895 ODRDiagDeclError(FirstRecord, FirstModule, 10896 FirstTemplate->getLocation(), 10897 FirstTemplate->getSourceRange(), 10898 FunctionTemplateDifferentNumberParameters) 10899 << FirstTemplate << FirstTPL->size(); 10900 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10901 SecondTemplate->getSourceRange(), 10902 FunctionTemplateDifferentNumberParameters) 10903 << SecondTemplate << SecondTPL->size(); 10904 10905 Diagnosed = true; 10906 break; 10907 } 10908 10909 bool ParameterMismatch = false; 10910 for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) { 10911 NamedDecl *FirstParam = FirstTPL->getParam(i); 10912 NamedDecl *SecondParam = SecondTPL->getParam(i); 10913 10914 if (FirstParam->getKind() != SecondParam->getKind()) { 10915 enum { 10916 TemplateTypeParameter, 10917 NonTypeTemplateParameter, 10918 TemplateTemplateParameter, 10919 }; 10920 auto GetParamType = [](NamedDecl *D) { 10921 switch (D->getKind()) { 10922 default: 10923 llvm_unreachable("Unexpected template parameter type"); 10924 case Decl::TemplateTypeParm: 10925 return TemplateTypeParameter; 10926 case Decl::NonTypeTemplateParm: 10927 return NonTypeTemplateParameter; 10928 case Decl::TemplateTemplateParm: 10929 return TemplateTemplateParameter; 10930 } 10931 }; 10932 10933 ODRDiagDeclError(FirstRecord, FirstModule, 10934 FirstTemplate->getLocation(), 10935 FirstTemplate->getSourceRange(), 10936 FunctionTemplateParameterDifferentKind) 10937 << FirstTemplate << (i + 1) << GetParamType(FirstParam); 10938 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10939 SecondTemplate->getSourceRange(), 10940 FunctionTemplateParameterDifferentKind) 10941 << SecondTemplate << (i + 1) << GetParamType(SecondParam); 10942 10943 ParameterMismatch = true; 10944 break; 10945 } 10946 10947 if (FirstParam->getName() != SecondParam->getName()) { 10948 ODRDiagDeclError( 10949 FirstRecord, FirstModule, FirstTemplate->getLocation(), 10950 FirstTemplate->getSourceRange(), FunctionTemplateParameterName) 10951 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier() 10952 << FirstParam; 10953 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10954 SecondTemplate->getSourceRange(), 10955 FunctionTemplateParameterName) 10956 << SecondTemplate << (i + 1) 10957 << (bool)SecondParam->getIdentifier() << SecondParam; 10958 ParameterMismatch = true; 10959 break; 10960 } 10961 10962 if (isa<TemplateTypeParmDecl>(FirstParam) && 10963 isa<TemplateTypeParmDecl>(SecondParam)) { 10964 TemplateTypeParmDecl *FirstTTPD = 10965 cast<TemplateTypeParmDecl>(FirstParam); 10966 TemplateTypeParmDecl *SecondTTPD = 10967 cast<TemplateTypeParmDecl>(SecondParam); 10968 bool HasFirstDefaultArgument = 10969 FirstTTPD->hasDefaultArgument() && 10970 !FirstTTPD->defaultArgumentWasInherited(); 10971 bool HasSecondDefaultArgument = 10972 SecondTTPD->hasDefaultArgument() && 10973 !SecondTTPD->defaultArgumentWasInherited(); 10974 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10975 ODRDiagDeclError(FirstRecord, FirstModule, 10976 FirstTemplate->getLocation(), 10977 FirstTemplate->getSourceRange(), 10978 FunctionTemplateParameterSingleDefaultArgument) 10979 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 10980 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10981 SecondTemplate->getSourceRange(), 10982 FunctionTemplateParameterSingleDefaultArgument) 10983 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 10984 ParameterMismatch = true; 10985 break; 10986 } 10987 10988 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 10989 QualType FirstType = FirstTTPD->getDefaultArgument(); 10990 QualType SecondType = SecondTTPD->getDefaultArgument(); 10991 if (ComputeQualTypeODRHash(FirstType) != 10992 ComputeQualTypeODRHash(SecondType)) { 10993 ODRDiagDeclError( 10994 FirstRecord, FirstModule, FirstTemplate->getLocation(), 10995 FirstTemplate->getSourceRange(), 10996 FunctionTemplateParameterDifferentDefaultArgument) 10997 << FirstTemplate << (i + 1) << FirstType; 10998 ODRDiagDeclNote( 10999 SecondModule, SecondTemplate->getLocation(), 11000 SecondTemplate->getSourceRange(), 11001 FunctionTemplateParameterDifferentDefaultArgument) 11002 << SecondTemplate << (i + 1) << SecondType; 11003 ParameterMismatch = true; 11004 break; 11005 } 11006 } 11007 11008 if (FirstTTPD->isParameterPack() != 11009 SecondTTPD->isParameterPack()) { 11010 ODRDiagDeclError(FirstRecord, FirstModule, 11011 FirstTemplate->getLocation(), 11012 FirstTemplate->getSourceRange(), 11013 FunctionTemplatePackParameter) 11014 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 11015 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11016 SecondTemplate->getSourceRange(), 11017 FunctionTemplatePackParameter) 11018 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 11019 ParameterMismatch = true; 11020 break; 11021 } 11022 } 11023 11024 if (isa<TemplateTemplateParmDecl>(FirstParam) && 11025 isa<TemplateTemplateParmDecl>(SecondParam)) { 11026 TemplateTemplateParmDecl *FirstTTPD = 11027 cast<TemplateTemplateParmDecl>(FirstParam); 11028 TemplateTemplateParmDecl *SecondTTPD = 11029 cast<TemplateTemplateParmDecl>(SecondParam); 11030 11031 TemplateParameterList *FirstTPL = 11032 FirstTTPD->getTemplateParameters(); 11033 TemplateParameterList *SecondTPL = 11034 SecondTTPD->getTemplateParameters(); 11035 11036 if (ComputeTemplateParameterListODRHash(FirstTPL) != 11037 ComputeTemplateParameterListODRHash(SecondTPL)) { 11038 ODRDiagDeclError(FirstRecord, FirstModule, 11039 FirstTemplate->getLocation(), 11040 FirstTemplate->getSourceRange(), 11041 FunctionTemplateParameterDifferentType) 11042 << FirstTemplate << (i + 1); 11043 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11044 SecondTemplate->getSourceRange(), 11045 FunctionTemplateParameterDifferentType) 11046 << SecondTemplate << (i + 1); 11047 ParameterMismatch = true; 11048 break; 11049 } 11050 11051 bool HasFirstDefaultArgument = 11052 FirstTTPD->hasDefaultArgument() && 11053 !FirstTTPD->defaultArgumentWasInherited(); 11054 bool HasSecondDefaultArgument = 11055 SecondTTPD->hasDefaultArgument() && 11056 !SecondTTPD->defaultArgumentWasInherited(); 11057 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11058 ODRDiagDeclError(FirstRecord, FirstModule, 11059 FirstTemplate->getLocation(), 11060 FirstTemplate->getSourceRange(), 11061 FunctionTemplateParameterSingleDefaultArgument) 11062 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11063 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11064 SecondTemplate->getSourceRange(), 11065 FunctionTemplateParameterSingleDefaultArgument) 11066 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11067 ParameterMismatch = true; 11068 break; 11069 } 11070 11071 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11072 TemplateArgument FirstTA = 11073 FirstTTPD->getDefaultArgument().getArgument(); 11074 TemplateArgument SecondTA = 11075 SecondTTPD->getDefaultArgument().getArgument(); 11076 if (ComputeTemplateArgumentODRHash(FirstTA) != 11077 ComputeTemplateArgumentODRHash(SecondTA)) { 11078 ODRDiagDeclError( 11079 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11080 FirstTemplate->getSourceRange(), 11081 FunctionTemplateParameterDifferentDefaultArgument) 11082 << FirstTemplate << (i + 1) << FirstTA; 11083 ODRDiagDeclNote( 11084 SecondModule, SecondTemplate->getLocation(), 11085 SecondTemplate->getSourceRange(), 11086 FunctionTemplateParameterDifferentDefaultArgument) 11087 << SecondTemplate << (i + 1) << SecondTA; 11088 ParameterMismatch = true; 11089 break; 11090 } 11091 } 11092 11093 if (FirstTTPD->isParameterPack() != 11094 SecondTTPD->isParameterPack()) { 11095 ODRDiagDeclError(FirstRecord, FirstModule, 11096 FirstTemplate->getLocation(), 11097 FirstTemplate->getSourceRange(), 11098 FunctionTemplatePackParameter) 11099 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 11100 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11101 SecondTemplate->getSourceRange(), 11102 FunctionTemplatePackParameter) 11103 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 11104 ParameterMismatch = true; 11105 break; 11106 } 11107 } 11108 11109 if (isa<NonTypeTemplateParmDecl>(FirstParam) && 11110 isa<NonTypeTemplateParmDecl>(SecondParam)) { 11111 NonTypeTemplateParmDecl *FirstNTTPD = 11112 cast<NonTypeTemplateParmDecl>(FirstParam); 11113 NonTypeTemplateParmDecl *SecondNTTPD = 11114 cast<NonTypeTemplateParmDecl>(SecondParam); 11115 11116 QualType FirstType = FirstNTTPD->getType(); 11117 QualType SecondType = SecondNTTPD->getType(); 11118 if (ComputeQualTypeODRHash(FirstType) != 11119 ComputeQualTypeODRHash(SecondType)) { 11120 ODRDiagDeclError(FirstRecord, FirstModule, 11121 FirstTemplate->getLocation(), 11122 FirstTemplate->getSourceRange(), 11123 FunctionTemplateParameterDifferentType) 11124 << FirstTemplate << (i + 1); 11125 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11126 SecondTemplate->getSourceRange(), 11127 FunctionTemplateParameterDifferentType) 11128 << SecondTemplate << (i + 1); 11129 ParameterMismatch = true; 11130 break; 11131 } 11132 11133 bool HasFirstDefaultArgument = 11134 FirstNTTPD->hasDefaultArgument() && 11135 !FirstNTTPD->defaultArgumentWasInherited(); 11136 bool HasSecondDefaultArgument = 11137 SecondNTTPD->hasDefaultArgument() && 11138 !SecondNTTPD->defaultArgumentWasInherited(); 11139 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11140 ODRDiagDeclError(FirstRecord, FirstModule, 11141 FirstTemplate->getLocation(), 11142 FirstTemplate->getSourceRange(), 11143 FunctionTemplateParameterSingleDefaultArgument) 11144 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11145 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11146 SecondTemplate->getSourceRange(), 11147 FunctionTemplateParameterSingleDefaultArgument) 11148 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11149 ParameterMismatch = true; 11150 break; 11151 } 11152 11153 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11154 Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument(); 11155 Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument(); 11156 if (ComputeODRHash(FirstDefaultArgument) != 11157 ComputeODRHash(SecondDefaultArgument)) { 11158 ODRDiagDeclError( 11159 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11160 FirstTemplate->getSourceRange(), 11161 FunctionTemplateParameterDifferentDefaultArgument) 11162 << FirstTemplate << (i + 1) << FirstDefaultArgument; 11163 ODRDiagDeclNote( 11164 SecondModule, SecondTemplate->getLocation(), 11165 SecondTemplate->getSourceRange(), 11166 FunctionTemplateParameterDifferentDefaultArgument) 11167 << SecondTemplate << (i + 1) << SecondDefaultArgument; 11168 ParameterMismatch = true; 11169 break; 11170 } 11171 } 11172 11173 if (FirstNTTPD->isParameterPack() != 11174 SecondNTTPD->isParameterPack()) { 11175 ODRDiagDeclError(FirstRecord, FirstModule, 11176 FirstTemplate->getLocation(), 11177 FirstTemplate->getSourceRange(), 11178 FunctionTemplatePackParameter) 11179 << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack(); 11180 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11181 SecondTemplate->getSourceRange(), 11182 FunctionTemplatePackParameter) 11183 << SecondTemplate << (i + 1) 11184 << SecondNTTPD->isParameterPack(); 11185 ParameterMismatch = true; 11186 break; 11187 } 11188 } 11189 } 11190 11191 if (ParameterMismatch) { 11192 Diagnosed = true; 11193 break; 11194 } 11195 11196 break; 11197 } 11198 } 11199 11200 if (Diagnosed) 11201 continue; 11202 11203 Diag(FirstDecl->getLocation(), 11204 diag::err_module_odr_violation_mismatch_decl_unknown) 11205 << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType 11206 << FirstDecl->getSourceRange(); 11207 Diag(SecondDecl->getLocation(), 11208 diag::note_module_odr_violation_mismatch_decl_unknown) 11209 << SecondModule << FirstDiffType << SecondDecl->getSourceRange(); 11210 Diagnosed = true; 11211 } 11212 11213 if (!Diagnosed) { 11214 // All definitions are updates to the same declaration. This happens if a 11215 // module instantiates the declaration of a class template specialization 11216 // and two or more other modules instantiate its definition. 11217 // 11218 // FIXME: Indicate which modules had instantiations of this definition. 11219 // FIXME: How can this even happen? 11220 Diag(Merge.first->getLocation(), 11221 diag::err_module_odr_violation_different_instantiations) 11222 << Merge.first; 11223 } 11224 } 11225 11226 // Issue ODR failures diagnostics for functions. 11227 for (auto &Merge : FunctionOdrMergeFailures) { 11228 enum ODRFunctionDifference { 11229 ReturnType, 11230 ParameterName, 11231 ParameterType, 11232 ParameterSingleDefaultArgument, 11233 ParameterDifferentDefaultArgument, 11234 FunctionBody, 11235 }; 11236 11237 FunctionDecl *FirstFunction = Merge.first; 11238 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction); 11239 11240 bool Diagnosed = false; 11241 for (auto &SecondFunction : Merge.second) { 11242 11243 if (FirstFunction == SecondFunction) 11244 continue; 11245 11246 std::string SecondModule = 11247 getOwningModuleNameForDiagnostic(SecondFunction); 11248 11249 auto ODRDiagError = [FirstFunction, &FirstModule, 11250 this](SourceLocation Loc, SourceRange Range, 11251 ODRFunctionDifference DiffType) { 11252 return Diag(Loc, diag::err_module_odr_violation_function) 11253 << FirstFunction << FirstModule.empty() << FirstModule << Range 11254 << DiffType; 11255 }; 11256 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11257 SourceRange Range, 11258 ODRFunctionDifference DiffType) { 11259 return Diag(Loc, diag::note_module_odr_violation_function) 11260 << SecondModule << Range << DiffType; 11261 }; 11262 11263 if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) != 11264 ComputeQualTypeODRHash(SecondFunction->getReturnType())) { 11265 ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(), 11266 FirstFunction->getReturnTypeSourceRange(), ReturnType) 11267 << FirstFunction->getReturnType(); 11268 ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(), 11269 SecondFunction->getReturnTypeSourceRange(), ReturnType) 11270 << SecondFunction->getReturnType(); 11271 Diagnosed = true; 11272 break; 11273 } 11274 11275 assert(FirstFunction->param_size() == SecondFunction->param_size() && 11276 "Merged functions with different number of parameters"); 11277 11278 auto ParamSize = FirstFunction->param_size(); 11279 bool ParameterMismatch = false; 11280 for (unsigned I = 0; I < ParamSize; ++I) { 11281 auto *FirstParam = FirstFunction->getParamDecl(I); 11282 auto *SecondParam = SecondFunction->getParamDecl(I); 11283 11284 assert(getContext().hasSameType(FirstParam->getType(), 11285 SecondParam->getType()) && 11286 "Merged function has different parameter types."); 11287 11288 if (FirstParam->getDeclName() != SecondParam->getDeclName()) { 11289 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11290 ParameterName) 11291 << I + 1 << FirstParam->getDeclName(); 11292 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11293 ParameterName) 11294 << I + 1 << SecondParam->getDeclName(); 11295 ParameterMismatch = true; 11296 break; 11297 }; 11298 11299 QualType FirstParamType = FirstParam->getType(); 11300 QualType SecondParamType = SecondParam->getType(); 11301 if (FirstParamType != SecondParamType && 11302 ComputeQualTypeODRHash(FirstParamType) != 11303 ComputeQualTypeODRHash(SecondParamType)) { 11304 if (const DecayedType *ParamDecayedType = 11305 FirstParamType->getAs<DecayedType>()) { 11306 ODRDiagError(FirstParam->getLocation(), 11307 FirstParam->getSourceRange(), ParameterType) 11308 << (I + 1) << FirstParamType << true 11309 << ParamDecayedType->getOriginalType(); 11310 } else { 11311 ODRDiagError(FirstParam->getLocation(), 11312 FirstParam->getSourceRange(), ParameterType) 11313 << (I + 1) << FirstParamType << false; 11314 } 11315 11316 if (const DecayedType *ParamDecayedType = 11317 SecondParamType->getAs<DecayedType>()) { 11318 ODRDiagNote(SecondParam->getLocation(), 11319 SecondParam->getSourceRange(), ParameterType) 11320 << (I + 1) << SecondParamType << true 11321 << ParamDecayedType->getOriginalType(); 11322 } else { 11323 ODRDiagNote(SecondParam->getLocation(), 11324 SecondParam->getSourceRange(), ParameterType) 11325 << (I + 1) << SecondParamType << false; 11326 } 11327 ParameterMismatch = true; 11328 break; 11329 } 11330 11331 const Expr *FirstInit = FirstParam->getInit(); 11332 const Expr *SecondInit = SecondParam->getInit(); 11333 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 11334 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11335 ParameterSingleDefaultArgument) 11336 << (I + 1) << (FirstInit == nullptr) 11337 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 11338 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11339 ParameterSingleDefaultArgument) 11340 << (I + 1) << (SecondInit == nullptr) 11341 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 11342 ParameterMismatch = true; 11343 break; 11344 } 11345 11346 if (FirstInit && SecondInit && 11347 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11348 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11349 ParameterDifferentDefaultArgument) 11350 << (I + 1) << FirstInit->getSourceRange(); 11351 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11352 ParameterDifferentDefaultArgument) 11353 << (I + 1) << SecondInit->getSourceRange(); 11354 ParameterMismatch = true; 11355 break; 11356 } 11357 11358 assert(ComputeSubDeclODRHash(FirstParam) == 11359 ComputeSubDeclODRHash(SecondParam) && 11360 "Undiagnosed parameter difference."); 11361 } 11362 11363 if (ParameterMismatch) { 11364 Diagnosed = true; 11365 break; 11366 } 11367 11368 // If no error has been generated before now, assume the problem is in 11369 // the body and generate a message. 11370 ODRDiagError(FirstFunction->getLocation(), 11371 FirstFunction->getSourceRange(), FunctionBody); 11372 ODRDiagNote(SecondFunction->getLocation(), 11373 SecondFunction->getSourceRange(), FunctionBody); 11374 Diagnosed = true; 11375 break; 11376 } 11377 (void)Diagnosed; 11378 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11379 } 11380 11381 // Issue ODR failures diagnostics for enums. 11382 for (auto &Merge : EnumOdrMergeFailures) { 11383 enum ODREnumDifference { 11384 SingleScopedEnum, 11385 EnumTagKeywordMismatch, 11386 SingleSpecifiedType, 11387 DifferentSpecifiedTypes, 11388 DifferentNumberEnumConstants, 11389 EnumConstantName, 11390 EnumConstantSingleInitilizer, 11391 EnumConstantDifferentInitilizer, 11392 }; 11393 11394 // If we've already pointed out a specific problem with this enum, don't 11395 // bother issuing a general "something's different" diagnostic. 11396 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 11397 continue; 11398 11399 EnumDecl *FirstEnum = Merge.first; 11400 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum); 11401 11402 using DeclHashes = 11403 llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>; 11404 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum]( 11405 DeclHashes &Hashes, EnumDecl *Enum) { 11406 for (auto *D : Enum->decls()) { 11407 // Due to decl merging, the first EnumDecl is the parent of 11408 // Decls in both records. 11409 if (!ODRHash::isDeclToBeProcessed(D, FirstEnum)) 11410 continue; 11411 assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind"); 11412 Hashes.emplace_back(cast<EnumConstantDecl>(D), 11413 ComputeSubDeclODRHash(D)); 11414 } 11415 }; 11416 DeclHashes FirstHashes; 11417 PopulateHashes(FirstHashes, FirstEnum); 11418 bool Diagnosed = false; 11419 for (auto &SecondEnum : Merge.second) { 11420 11421 if (FirstEnum == SecondEnum) 11422 continue; 11423 11424 std::string SecondModule = 11425 getOwningModuleNameForDiagnostic(SecondEnum); 11426 11427 auto ODRDiagError = [FirstEnum, &FirstModule, 11428 this](SourceLocation Loc, SourceRange Range, 11429 ODREnumDifference DiffType) { 11430 return Diag(Loc, diag::err_module_odr_violation_enum) 11431 << FirstEnum << FirstModule.empty() << FirstModule << Range 11432 << DiffType; 11433 }; 11434 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11435 SourceRange Range, 11436 ODREnumDifference DiffType) { 11437 return Diag(Loc, diag::note_module_odr_violation_enum) 11438 << SecondModule << Range << DiffType; 11439 }; 11440 11441 if (FirstEnum->isScoped() != SecondEnum->isScoped()) { 11442 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11443 SingleScopedEnum) 11444 << FirstEnum->isScoped(); 11445 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11446 SingleScopedEnum) 11447 << SecondEnum->isScoped(); 11448 Diagnosed = true; 11449 continue; 11450 } 11451 11452 if (FirstEnum->isScoped() && SecondEnum->isScoped()) { 11453 if (FirstEnum->isScopedUsingClassTag() != 11454 SecondEnum->isScopedUsingClassTag()) { 11455 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11456 EnumTagKeywordMismatch) 11457 << FirstEnum->isScopedUsingClassTag(); 11458 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11459 EnumTagKeywordMismatch) 11460 << SecondEnum->isScopedUsingClassTag(); 11461 Diagnosed = true; 11462 continue; 11463 } 11464 } 11465 11466 QualType FirstUnderlyingType = 11467 FirstEnum->getIntegerTypeSourceInfo() 11468 ? FirstEnum->getIntegerTypeSourceInfo()->getType() 11469 : QualType(); 11470 QualType SecondUnderlyingType = 11471 SecondEnum->getIntegerTypeSourceInfo() 11472 ? SecondEnum->getIntegerTypeSourceInfo()->getType() 11473 : QualType(); 11474 if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) { 11475 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11476 SingleSpecifiedType) 11477 << !FirstUnderlyingType.isNull(); 11478 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11479 SingleSpecifiedType) 11480 << !SecondUnderlyingType.isNull(); 11481 Diagnosed = true; 11482 continue; 11483 } 11484 11485 if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) { 11486 if (ComputeQualTypeODRHash(FirstUnderlyingType) != 11487 ComputeQualTypeODRHash(SecondUnderlyingType)) { 11488 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11489 DifferentSpecifiedTypes) 11490 << FirstUnderlyingType; 11491 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11492 DifferentSpecifiedTypes) 11493 << SecondUnderlyingType; 11494 Diagnosed = true; 11495 continue; 11496 } 11497 } 11498 11499 DeclHashes SecondHashes; 11500 PopulateHashes(SecondHashes, SecondEnum); 11501 11502 if (FirstHashes.size() != SecondHashes.size()) { 11503 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11504 DifferentNumberEnumConstants) 11505 << (int)FirstHashes.size(); 11506 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11507 DifferentNumberEnumConstants) 11508 << (int)SecondHashes.size(); 11509 Diagnosed = true; 11510 continue; 11511 } 11512 11513 for (unsigned I = 0; I < FirstHashes.size(); ++I) { 11514 if (FirstHashes[I].second == SecondHashes[I].second) 11515 continue; 11516 const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first; 11517 const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first; 11518 11519 if (FirstEnumConstant->getDeclName() != 11520 SecondEnumConstant->getDeclName()) { 11521 11522 ODRDiagError(FirstEnumConstant->getLocation(), 11523 FirstEnumConstant->getSourceRange(), EnumConstantName) 11524 << I + 1 << FirstEnumConstant; 11525 ODRDiagNote(SecondEnumConstant->getLocation(), 11526 SecondEnumConstant->getSourceRange(), EnumConstantName) 11527 << I + 1 << SecondEnumConstant; 11528 Diagnosed = true; 11529 break; 11530 } 11531 11532 const Expr *FirstInit = FirstEnumConstant->getInitExpr(); 11533 const Expr *SecondInit = SecondEnumConstant->getInitExpr(); 11534 if (!FirstInit && !SecondInit) 11535 continue; 11536 11537 if (!FirstInit || !SecondInit) { 11538 ODRDiagError(FirstEnumConstant->getLocation(), 11539 FirstEnumConstant->getSourceRange(), 11540 EnumConstantSingleInitilizer) 11541 << I + 1 << FirstEnumConstant << (FirstInit != nullptr); 11542 ODRDiagNote(SecondEnumConstant->getLocation(), 11543 SecondEnumConstant->getSourceRange(), 11544 EnumConstantSingleInitilizer) 11545 << I + 1 << SecondEnumConstant << (SecondInit != nullptr); 11546 Diagnosed = true; 11547 break; 11548 } 11549 11550 if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11551 ODRDiagError(FirstEnumConstant->getLocation(), 11552 FirstEnumConstant->getSourceRange(), 11553 EnumConstantDifferentInitilizer) 11554 << I + 1 << FirstEnumConstant; 11555 ODRDiagNote(SecondEnumConstant->getLocation(), 11556 SecondEnumConstant->getSourceRange(), 11557 EnumConstantDifferentInitilizer) 11558 << I + 1 << SecondEnumConstant; 11559 Diagnosed = true; 11560 break; 11561 } 11562 } 11563 } 11564 11565 (void)Diagnosed; 11566 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11567 } 11568 } 11569 11570 void ASTReader::StartedDeserializing() { 11571 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get()) 11572 ReadTimer->startTimer(); 11573 } 11574 11575 void ASTReader::FinishedDeserializing() { 11576 assert(NumCurrentElementsDeserializing && 11577 "FinishedDeserializing not paired with StartedDeserializing"); 11578 if (NumCurrentElementsDeserializing == 1) { 11579 // We decrease NumCurrentElementsDeserializing only after pending actions 11580 // are finished, to avoid recursively re-calling finishPendingActions(). 11581 finishPendingActions(); 11582 } 11583 --NumCurrentElementsDeserializing; 11584 11585 if (NumCurrentElementsDeserializing == 0) { 11586 // Propagate exception specification and deduced type updates along 11587 // redeclaration chains. 11588 // 11589 // We do this now rather than in finishPendingActions because we want to 11590 // be able to walk the complete redeclaration chains of the updated decls. 11591 while (!PendingExceptionSpecUpdates.empty() || 11592 !PendingDeducedTypeUpdates.empty()) { 11593 auto ESUpdates = std::move(PendingExceptionSpecUpdates); 11594 PendingExceptionSpecUpdates.clear(); 11595 for (auto Update : ESUpdates) { 11596 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11597 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>(); 11598 auto ESI = FPT->getExtProtoInfo().ExceptionSpec; 11599 if (auto *Listener = getContext().getASTMutationListener()) 11600 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second)); 11601 for (auto *Redecl : Update.second->redecls()) 11602 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI); 11603 } 11604 11605 auto DTUpdates = std::move(PendingDeducedTypeUpdates); 11606 PendingDeducedTypeUpdates.clear(); 11607 for (auto Update : DTUpdates) { 11608 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11609 // FIXME: If the return type is already deduced, check that it matches. 11610 getContext().adjustDeducedFunctionResultType(Update.first, 11611 Update.second); 11612 } 11613 } 11614 11615 if (ReadTimer) 11616 ReadTimer->stopTimer(); 11617 11618 diagnoseOdrViolations(); 11619 11620 // We are not in recursive loading, so it's safe to pass the "interesting" 11621 // decls to the consumer. 11622 if (Consumer) 11623 PassInterestingDeclsToConsumer(); 11624 } 11625 } 11626 11627 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) { 11628 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) { 11629 // Remove any fake results before adding any real ones. 11630 auto It = PendingFakeLookupResults.find(II); 11631 if (It != PendingFakeLookupResults.end()) { 11632 for (auto *ND : It->second) 11633 SemaObj->IdResolver.RemoveDecl(ND); 11634 // FIXME: this works around module+PCH performance issue. 11635 // Rather than erase the result from the map, which is O(n), just clear 11636 // the vector of NamedDecls. 11637 It->second.clear(); 11638 } 11639 } 11640 11641 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) { 11642 SemaObj->TUScope->AddDecl(D); 11643 } else if (SemaObj->TUScope) { 11644 // Adding the decl to IdResolver may have failed because it was already in 11645 // (even though it was not added in scope). If it is already in, make sure 11646 // it gets in the scope as well. 11647 if (std::find(SemaObj->IdResolver.begin(Name), 11648 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end()) 11649 SemaObj->TUScope->AddDecl(D); 11650 } 11651 } 11652 11653 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache, 11654 ASTContext *Context, 11655 const PCHContainerReader &PCHContainerRdr, 11656 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, 11657 StringRef isysroot, bool DisableValidation, 11658 bool AllowASTWithCompilerErrors, 11659 bool AllowConfigurationMismatch, bool ValidateSystemInputs, 11660 bool ValidateASTInputFilesContent, bool UseGlobalIndex, 11661 std::unique_ptr<llvm::Timer> ReadTimer) 11662 : Listener(DisableValidation 11663 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP)) 11664 : cast<ASTReaderListener>(new PCHValidator(PP, *this))), 11665 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), 11666 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP), 11667 ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache, 11668 PCHContainerRdr, PP.getHeaderSearchInfo()), 11669 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot), 11670 DisableValidation(DisableValidation), 11671 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors), 11672 AllowConfigurationMismatch(AllowConfigurationMismatch), 11673 ValidateSystemInputs(ValidateSystemInputs), 11674 ValidateASTInputFilesContent(ValidateASTInputFilesContent), 11675 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) { 11676 SourceMgr.setExternalSLocEntrySource(this); 11677 11678 for (const auto &Ext : Extensions) { 11679 auto BlockName = Ext->getExtensionMetadata().BlockName; 11680 auto Known = ModuleFileExtensions.find(BlockName); 11681 if (Known != ModuleFileExtensions.end()) { 11682 Diags.Report(diag::warn_duplicate_module_file_extension) 11683 << BlockName; 11684 continue; 11685 } 11686 11687 ModuleFileExtensions.insert({BlockName, Ext}); 11688 } 11689 } 11690 11691 ASTReader::~ASTReader() { 11692 if (OwnsDeserializationListener) 11693 delete DeserializationListener; 11694 } 11695 11696 IdentifierResolver &ASTReader::getIdResolver() { 11697 return SemaObj ? SemaObj->IdResolver : DummyIdResolver; 11698 } 11699 11700 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor, 11701 unsigned AbbrevID) { 11702 Idx = 0; 11703 Record.clear(); 11704 return Cursor.readRecord(AbbrevID, Record); 11705 } 11706 //===----------------------------------------------------------------------===// 11707 //// OMPClauseReader implementation 11708 ////===----------------------------------------------------------------------===// 11709 11710 // This has to be in namespace clang because it's friended by all 11711 // of the OMP clauses. 11712 namespace clang { 11713 11714 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> { 11715 ASTRecordReader &Record; 11716 ASTContext &Context; 11717 11718 public: 11719 OMPClauseReader(ASTRecordReader &Record) 11720 : Record(Record), Context(Record.getContext()) {} 11721 11722 #define OMP_CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C); 11723 #include "llvm/Frontend/OpenMP/OMPKinds.def" 11724 OMPClause *readClause(); 11725 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C); 11726 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C); 11727 }; 11728 11729 } // end namespace clang 11730 11731 OMPClause *ASTRecordReader::readOMPClause() { 11732 return OMPClauseReader(*this).readClause(); 11733 } 11734 11735 OMPClause *OMPClauseReader::readClause() { 11736 OMPClause *C = nullptr; 11737 switch (llvm::omp::Clause(Record.readInt())) { 11738 case llvm::omp::OMPC_if: 11739 C = new (Context) OMPIfClause(); 11740 break; 11741 case llvm::omp::OMPC_final: 11742 C = new (Context) OMPFinalClause(); 11743 break; 11744 case llvm::omp::OMPC_num_threads: 11745 C = new (Context) OMPNumThreadsClause(); 11746 break; 11747 case llvm::omp::OMPC_safelen: 11748 C = new (Context) OMPSafelenClause(); 11749 break; 11750 case llvm::omp::OMPC_simdlen: 11751 C = new (Context) OMPSimdlenClause(); 11752 break; 11753 case llvm::omp::OMPC_allocator: 11754 C = new (Context) OMPAllocatorClause(); 11755 break; 11756 case llvm::omp::OMPC_collapse: 11757 C = new (Context) OMPCollapseClause(); 11758 break; 11759 case llvm::omp::OMPC_default: 11760 C = new (Context) OMPDefaultClause(); 11761 break; 11762 case llvm::omp::OMPC_proc_bind: 11763 C = new (Context) OMPProcBindClause(); 11764 break; 11765 case llvm::omp::OMPC_schedule: 11766 C = new (Context) OMPScheduleClause(); 11767 break; 11768 case llvm::omp::OMPC_ordered: 11769 C = OMPOrderedClause::CreateEmpty(Context, Record.readInt()); 11770 break; 11771 case llvm::omp::OMPC_nowait: 11772 C = new (Context) OMPNowaitClause(); 11773 break; 11774 case llvm::omp::OMPC_untied: 11775 C = new (Context) OMPUntiedClause(); 11776 break; 11777 case llvm::omp::OMPC_mergeable: 11778 C = new (Context) OMPMergeableClause(); 11779 break; 11780 case llvm::omp::OMPC_read: 11781 C = new (Context) OMPReadClause(); 11782 break; 11783 case llvm::omp::OMPC_write: 11784 C = new (Context) OMPWriteClause(); 11785 break; 11786 case llvm::omp::OMPC_update: 11787 C = OMPUpdateClause::CreateEmpty(Context, Record.readInt()); 11788 break; 11789 case llvm::omp::OMPC_capture: 11790 C = new (Context) OMPCaptureClause(); 11791 break; 11792 case llvm::omp::OMPC_seq_cst: 11793 C = new (Context) OMPSeqCstClause(); 11794 break; 11795 case llvm::omp::OMPC_acq_rel: 11796 C = new (Context) OMPAcqRelClause(); 11797 break; 11798 case llvm::omp::OMPC_acquire: 11799 C = new (Context) OMPAcquireClause(); 11800 break; 11801 case llvm::omp::OMPC_release: 11802 C = new (Context) OMPReleaseClause(); 11803 break; 11804 case llvm::omp::OMPC_relaxed: 11805 C = new (Context) OMPRelaxedClause(); 11806 break; 11807 case llvm::omp::OMPC_threads: 11808 C = new (Context) OMPThreadsClause(); 11809 break; 11810 case llvm::omp::OMPC_simd: 11811 C = new (Context) OMPSIMDClause(); 11812 break; 11813 case llvm::omp::OMPC_nogroup: 11814 C = new (Context) OMPNogroupClause(); 11815 break; 11816 case llvm::omp::OMPC_unified_address: 11817 C = new (Context) OMPUnifiedAddressClause(); 11818 break; 11819 case llvm::omp::OMPC_unified_shared_memory: 11820 C = new (Context) OMPUnifiedSharedMemoryClause(); 11821 break; 11822 case llvm::omp::OMPC_reverse_offload: 11823 C = new (Context) OMPReverseOffloadClause(); 11824 break; 11825 case llvm::omp::OMPC_dynamic_allocators: 11826 C = new (Context) OMPDynamicAllocatorsClause(); 11827 break; 11828 case llvm::omp::OMPC_atomic_default_mem_order: 11829 C = new (Context) OMPAtomicDefaultMemOrderClause(); 11830 break; 11831 case llvm::omp::OMPC_private: 11832 C = OMPPrivateClause::CreateEmpty(Context, Record.readInt()); 11833 break; 11834 case llvm::omp::OMPC_firstprivate: 11835 C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt()); 11836 break; 11837 case llvm::omp::OMPC_lastprivate: 11838 C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt()); 11839 break; 11840 case llvm::omp::OMPC_shared: 11841 C = OMPSharedClause::CreateEmpty(Context, Record.readInt()); 11842 break; 11843 case llvm::omp::OMPC_reduction: { 11844 unsigned N = Record.readInt(); 11845 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>(); 11846 C = OMPReductionClause::CreateEmpty(Context, N, Modifier); 11847 break; 11848 } 11849 case llvm::omp::OMPC_task_reduction: 11850 C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt()); 11851 break; 11852 case llvm::omp::OMPC_in_reduction: 11853 C = OMPInReductionClause::CreateEmpty(Context, Record.readInt()); 11854 break; 11855 case llvm::omp::OMPC_linear: 11856 C = OMPLinearClause::CreateEmpty(Context, Record.readInt()); 11857 break; 11858 case llvm::omp::OMPC_aligned: 11859 C = OMPAlignedClause::CreateEmpty(Context, Record.readInt()); 11860 break; 11861 case llvm::omp::OMPC_copyin: 11862 C = OMPCopyinClause::CreateEmpty(Context, Record.readInt()); 11863 break; 11864 case llvm::omp::OMPC_copyprivate: 11865 C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt()); 11866 break; 11867 case llvm::omp::OMPC_flush: 11868 C = OMPFlushClause::CreateEmpty(Context, Record.readInt()); 11869 break; 11870 case llvm::omp::OMPC_depobj: 11871 C = OMPDepobjClause::CreateEmpty(Context); 11872 break; 11873 case llvm::omp::OMPC_depend: { 11874 unsigned NumVars = Record.readInt(); 11875 unsigned NumLoops = Record.readInt(); 11876 C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops); 11877 break; 11878 } 11879 case llvm::omp::OMPC_device: 11880 C = new (Context) OMPDeviceClause(); 11881 break; 11882 case llvm::omp::OMPC_map: { 11883 OMPMappableExprListSizeTy Sizes; 11884 Sizes.NumVars = Record.readInt(); 11885 Sizes.NumUniqueDeclarations = Record.readInt(); 11886 Sizes.NumComponentLists = Record.readInt(); 11887 Sizes.NumComponents = Record.readInt(); 11888 C = OMPMapClause::CreateEmpty(Context, Sizes); 11889 break; 11890 } 11891 case llvm::omp::OMPC_num_teams: 11892 C = new (Context) OMPNumTeamsClause(); 11893 break; 11894 case llvm::omp::OMPC_thread_limit: 11895 C = new (Context) OMPThreadLimitClause(); 11896 break; 11897 case llvm::omp::OMPC_priority: 11898 C = new (Context) OMPPriorityClause(); 11899 break; 11900 case llvm::omp::OMPC_grainsize: 11901 C = new (Context) OMPGrainsizeClause(); 11902 break; 11903 case llvm::omp::OMPC_num_tasks: 11904 C = new (Context) OMPNumTasksClause(); 11905 break; 11906 case llvm::omp::OMPC_hint: 11907 C = new (Context) OMPHintClause(); 11908 break; 11909 case llvm::omp::OMPC_dist_schedule: 11910 C = new (Context) OMPDistScheduleClause(); 11911 break; 11912 case llvm::omp::OMPC_defaultmap: 11913 C = new (Context) OMPDefaultmapClause(); 11914 break; 11915 case llvm::omp::OMPC_to: { 11916 OMPMappableExprListSizeTy Sizes; 11917 Sizes.NumVars = Record.readInt(); 11918 Sizes.NumUniqueDeclarations = Record.readInt(); 11919 Sizes.NumComponentLists = Record.readInt(); 11920 Sizes.NumComponents = Record.readInt(); 11921 C = OMPToClause::CreateEmpty(Context, Sizes); 11922 break; 11923 } 11924 case llvm::omp::OMPC_from: { 11925 OMPMappableExprListSizeTy Sizes; 11926 Sizes.NumVars = Record.readInt(); 11927 Sizes.NumUniqueDeclarations = Record.readInt(); 11928 Sizes.NumComponentLists = Record.readInt(); 11929 Sizes.NumComponents = Record.readInt(); 11930 C = OMPFromClause::CreateEmpty(Context, Sizes); 11931 break; 11932 } 11933 case llvm::omp::OMPC_use_device_ptr: { 11934 OMPMappableExprListSizeTy Sizes; 11935 Sizes.NumVars = Record.readInt(); 11936 Sizes.NumUniqueDeclarations = Record.readInt(); 11937 Sizes.NumComponentLists = Record.readInt(); 11938 Sizes.NumComponents = Record.readInt(); 11939 C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes); 11940 break; 11941 } 11942 case llvm::omp::OMPC_use_device_addr: { 11943 OMPMappableExprListSizeTy Sizes; 11944 Sizes.NumVars = Record.readInt(); 11945 Sizes.NumUniqueDeclarations = Record.readInt(); 11946 Sizes.NumComponentLists = Record.readInt(); 11947 Sizes.NumComponents = Record.readInt(); 11948 C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes); 11949 break; 11950 } 11951 case llvm::omp::OMPC_is_device_ptr: { 11952 OMPMappableExprListSizeTy Sizes; 11953 Sizes.NumVars = Record.readInt(); 11954 Sizes.NumUniqueDeclarations = Record.readInt(); 11955 Sizes.NumComponentLists = Record.readInt(); 11956 Sizes.NumComponents = Record.readInt(); 11957 C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes); 11958 break; 11959 } 11960 case llvm::omp::OMPC_allocate: 11961 C = OMPAllocateClause::CreateEmpty(Context, Record.readInt()); 11962 break; 11963 case llvm::omp::OMPC_nontemporal: 11964 C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt()); 11965 break; 11966 case llvm::omp::OMPC_inclusive: 11967 C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt()); 11968 break; 11969 case llvm::omp::OMPC_exclusive: 11970 C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt()); 11971 break; 11972 case llvm::omp::OMPC_order: 11973 C = new (Context) OMPOrderClause(); 11974 break; 11975 case llvm::omp::OMPC_destroy: 11976 C = new (Context) OMPDestroyClause(); 11977 break; 11978 case llvm::omp::OMPC_detach: 11979 C = new (Context) OMPDetachClause(); 11980 break; 11981 case llvm::omp::OMPC_uses_allocators: 11982 C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt()); 11983 break; 11984 case llvm::omp::OMPC_affinity: 11985 C = OMPAffinityClause::CreateEmpty(Context, Record.readInt()); 11986 break; 11987 #define OMP_CLAUSE_NO_CLASS(Enum, Str) \ 11988 case llvm::omp::Enum: \ 11989 break; 11990 #include "llvm/Frontend/OpenMP/OMPKinds.def" 11991 default: 11992 break; 11993 } 11994 assert(C && "Unknown OMPClause type"); 11995 11996 Visit(C); 11997 C->setLocStart(Record.readSourceLocation()); 11998 C->setLocEnd(Record.readSourceLocation()); 11999 12000 return C; 12001 } 12002 12003 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) { 12004 C->setPreInitStmt(Record.readSubStmt(), 12005 static_cast<OpenMPDirectiveKind>(Record.readInt())); 12006 } 12007 12008 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) { 12009 VisitOMPClauseWithPreInit(C); 12010 C->setPostUpdateExpr(Record.readSubExpr()); 12011 } 12012 12013 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) { 12014 VisitOMPClauseWithPreInit(C); 12015 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt())); 12016 C->setNameModifierLoc(Record.readSourceLocation()); 12017 C->setColonLoc(Record.readSourceLocation()); 12018 C->setCondition(Record.readSubExpr()); 12019 C->setLParenLoc(Record.readSourceLocation()); 12020 } 12021 12022 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) { 12023 VisitOMPClauseWithPreInit(C); 12024 C->setCondition(Record.readSubExpr()); 12025 C->setLParenLoc(Record.readSourceLocation()); 12026 } 12027 12028 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) { 12029 VisitOMPClauseWithPreInit(C); 12030 C->setNumThreads(Record.readSubExpr()); 12031 C->setLParenLoc(Record.readSourceLocation()); 12032 } 12033 12034 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) { 12035 C->setSafelen(Record.readSubExpr()); 12036 C->setLParenLoc(Record.readSourceLocation()); 12037 } 12038 12039 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) { 12040 C->setSimdlen(Record.readSubExpr()); 12041 C->setLParenLoc(Record.readSourceLocation()); 12042 } 12043 12044 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) { 12045 C->setAllocator(Record.readExpr()); 12046 C->setLParenLoc(Record.readSourceLocation()); 12047 } 12048 12049 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) { 12050 C->setNumForLoops(Record.readSubExpr()); 12051 C->setLParenLoc(Record.readSourceLocation()); 12052 } 12053 12054 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) { 12055 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt())); 12056 C->setLParenLoc(Record.readSourceLocation()); 12057 C->setDefaultKindKwLoc(Record.readSourceLocation()); 12058 } 12059 12060 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) { 12061 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt())); 12062 C->setLParenLoc(Record.readSourceLocation()); 12063 C->setProcBindKindKwLoc(Record.readSourceLocation()); 12064 } 12065 12066 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) { 12067 VisitOMPClauseWithPreInit(C); 12068 C->setScheduleKind( 12069 static_cast<OpenMPScheduleClauseKind>(Record.readInt())); 12070 C->setFirstScheduleModifier( 12071 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 12072 C->setSecondScheduleModifier( 12073 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 12074 C->setChunkSize(Record.readSubExpr()); 12075 C->setLParenLoc(Record.readSourceLocation()); 12076 C->setFirstScheduleModifierLoc(Record.readSourceLocation()); 12077 C->setSecondScheduleModifierLoc(Record.readSourceLocation()); 12078 C->setScheduleKindLoc(Record.readSourceLocation()); 12079 C->setCommaLoc(Record.readSourceLocation()); 12080 } 12081 12082 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) { 12083 C->setNumForLoops(Record.readSubExpr()); 12084 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 12085 C->setLoopNumIterations(I, Record.readSubExpr()); 12086 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 12087 C->setLoopCounter(I, Record.readSubExpr()); 12088 C->setLParenLoc(Record.readSourceLocation()); 12089 } 12090 12091 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) { 12092 C->setEventHandler(Record.readSubExpr()); 12093 C->setLParenLoc(Record.readSourceLocation()); 12094 } 12095 12096 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {} 12097 12098 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {} 12099 12100 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {} 12101 12102 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {} 12103 12104 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {} 12105 12106 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) { 12107 if (C->isExtended()) { 12108 C->setLParenLoc(Record.readSourceLocation()); 12109 C->setArgumentLoc(Record.readSourceLocation()); 12110 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>()); 12111 } 12112 } 12113 12114 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {} 12115 12116 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {} 12117 12118 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {} 12119 12120 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {} 12121 12122 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {} 12123 12124 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {} 12125 12126 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {} 12127 12128 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {} 12129 12130 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {} 12131 12132 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *) {} 12133 12134 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {} 12135 12136 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause( 12137 OMPUnifiedSharedMemoryClause *) {} 12138 12139 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {} 12140 12141 void 12142 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) { 12143 } 12144 12145 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause( 12146 OMPAtomicDefaultMemOrderClause *C) { 12147 C->setAtomicDefaultMemOrderKind( 12148 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt())); 12149 C->setLParenLoc(Record.readSourceLocation()); 12150 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation()); 12151 } 12152 12153 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) { 12154 C->setLParenLoc(Record.readSourceLocation()); 12155 unsigned NumVars = C->varlist_size(); 12156 SmallVector<Expr *, 16> Vars; 12157 Vars.reserve(NumVars); 12158 for (unsigned i = 0; i != NumVars; ++i) 12159 Vars.push_back(Record.readSubExpr()); 12160 C->setVarRefs(Vars); 12161 Vars.clear(); 12162 for (unsigned i = 0; i != NumVars; ++i) 12163 Vars.push_back(Record.readSubExpr()); 12164 C->setPrivateCopies(Vars); 12165 } 12166 12167 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) { 12168 VisitOMPClauseWithPreInit(C); 12169 C->setLParenLoc(Record.readSourceLocation()); 12170 unsigned NumVars = C->varlist_size(); 12171 SmallVector<Expr *, 16> Vars; 12172 Vars.reserve(NumVars); 12173 for (unsigned i = 0; i != NumVars; ++i) 12174 Vars.push_back(Record.readSubExpr()); 12175 C->setVarRefs(Vars); 12176 Vars.clear(); 12177 for (unsigned i = 0; i != NumVars; ++i) 12178 Vars.push_back(Record.readSubExpr()); 12179 C->setPrivateCopies(Vars); 12180 Vars.clear(); 12181 for (unsigned i = 0; i != NumVars; ++i) 12182 Vars.push_back(Record.readSubExpr()); 12183 C->setInits(Vars); 12184 } 12185 12186 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) { 12187 VisitOMPClauseWithPostUpdate(C); 12188 C->setLParenLoc(Record.readSourceLocation()); 12189 C->setKind(Record.readEnum<OpenMPLastprivateModifier>()); 12190 C->setKindLoc(Record.readSourceLocation()); 12191 C->setColonLoc(Record.readSourceLocation()); 12192 unsigned NumVars = C->varlist_size(); 12193 SmallVector<Expr *, 16> Vars; 12194 Vars.reserve(NumVars); 12195 for (unsigned i = 0; i != NumVars; ++i) 12196 Vars.push_back(Record.readSubExpr()); 12197 C->setVarRefs(Vars); 12198 Vars.clear(); 12199 for (unsigned i = 0; i != NumVars; ++i) 12200 Vars.push_back(Record.readSubExpr()); 12201 C->setPrivateCopies(Vars); 12202 Vars.clear(); 12203 for (unsigned i = 0; i != NumVars; ++i) 12204 Vars.push_back(Record.readSubExpr()); 12205 C->setSourceExprs(Vars); 12206 Vars.clear(); 12207 for (unsigned i = 0; i != NumVars; ++i) 12208 Vars.push_back(Record.readSubExpr()); 12209 C->setDestinationExprs(Vars); 12210 Vars.clear(); 12211 for (unsigned i = 0; i != NumVars; ++i) 12212 Vars.push_back(Record.readSubExpr()); 12213 C->setAssignmentOps(Vars); 12214 } 12215 12216 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) { 12217 C->setLParenLoc(Record.readSourceLocation()); 12218 unsigned NumVars = C->varlist_size(); 12219 SmallVector<Expr *, 16> Vars; 12220 Vars.reserve(NumVars); 12221 for (unsigned i = 0; i != NumVars; ++i) 12222 Vars.push_back(Record.readSubExpr()); 12223 C->setVarRefs(Vars); 12224 } 12225 12226 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) { 12227 VisitOMPClauseWithPostUpdate(C); 12228 C->setLParenLoc(Record.readSourceLocation()); 12229 C->setModifierLoc(Record.readSourceLocation()); 12230 C->setColonLoc(Record.readSourceLocation()); 12231 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12232 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12233 C->setQualifierLoc(NNSL); 12234 C->setNameInfo(DNI); 12235 12236 unsigned NumVars = C->varlist_size(); 12237 SmallVector<Expr *, 16> Vars; 12238 Vars.reserve(NumVars); 12239 for (unsigned i = 0; i != NumVars; ++i) 12240 Vars.push_back(Record.readSubExpr()); 12241 C->setVarRefs(Vars); 12242 Vars.clear(); 12243 for (unsigned i = 0; i != NumVars; ++i) 12244 Vars.push_back(Record.readSubExpr()); 12245 C->setPrivates(Vars); 12246 Vars.clear(); 12247 for (unsigned i = 0; i != NumVars; ++i) 12248 Vars.push_back(Record.readSubExpr()); 12249 C->setLHSExprs(Vars); 12250 Vars.clear(); 12251 for (unsigned i = 0; i != NumVars; ++i) 12252 Vars.push_back(Record.readSubExpr()); 12253 C->setRHSExprs(Vars); 12254 Vars.clear(); 12255 for (unsigned i = 0; i != NumVars; ++i) 12256 Vars.push_back(Record.readSubExpr()); 12257 C->setReductionOps(Vars); 12258 if (C->getModifier() == OMPC_REDUCTION_inscan) { 12259 Vars.clear(); 12260 for (unsigned i = 0; i != NumVars; ++i) 12261 Vars.push_back(Record.readSubExpr()); 12262 C->setInscanCopyOps(Vars); 12263 Vars.clear(); 12264 for (unsigned i = 0; i != NumVars; ++i) 12265 Vars.push_back(Record.readSubExpr()); 12266 C->setInscanCopyArrayTemps(Vars); 12267 Vars.clear(); 12268 for (unsigned i = 0; i != NumVars; ++i) 12269 Vars.push_back(Record.readSubExpr()); 12270 C->setInscanCopyArrayElems(Vars); 12271 } 12272 } 12273 12274 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) { 12275 VisitOMPClauseWithPostUpdate(C); 12276 C->setLParenLoc(Record.readSourceLocation()); 12277 C->setColonLoc(Record.readSourceLocation()); 12278 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12279 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12280 C->setQualifierLoc(NNSL); 12281 C->setNameInfo(DNI); 12282 12283 unsigned NumVars = C->varlist_size(); 12284 SmallVector<Expr *, 16> Vars; 12285 Vars.reserve(NumVars); 12286 for (unsigned I = 0; I != NumVars; ++I) 12287 Vars.push_back(Record.readSubExpr()); 12288 C->setVarRefs(Vars); 12289 Vars.clear(); 12290 for (unsigned I = 0; I != NumVars; ++I) 12291 Vars.push_back(Record.readSubExpr()); 12292 C->setPrivates(Vars); 12293 Vars.clear(); 12294 for (unsigned I = 0; I != NumVars; ++I) 12295 Vars.push_back(Record.readSubExpr()); 12296 C->setLHSExprs(Vars); 12297 Vars.clear(); 12298 for (unsigned I = 0; I != NumVars; ++I) 12299 Vars.push_back(Record.readSubExpr()); 12300 C->setRHSExprs(Vars); 12301 Vars.clear(); 12302 for (unsigned I = 0; I != NumVars; ++I) 12303 Vars.push_back(Record.readSubExpr()); 12304 C->setReductionOps(Vars); 12305 } 12306 12307 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) { 12308 VisitOMPClauseWithPostUpdate(C); 12309 C->setLParenLoc(Record.readSourceLocation()); 12310 C->setColonLoc(Record.readSourceLocation()); 12311 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12312 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12313 C->setQualifierLoc(NNSL); 12314 C->setNameInfo(DNI); 12315 12316 unsigned NumVars = C->varlist_size(); 12317 SmallVector<Expr *, 16> Vars; 12318 Vars.reserve(NumVars); 12319 for (unsigned I = 0; I != NumVars; ++I) 12320 Vars.push_back(Record.readSubExpr()); 12321 C->setVarRefs(Vars); 12322 Vars.clear(); 12323 for (unsigned I = 0; I != NumVars; ++I) 12324 Vars.push_back(Record.readSubExpr()); 12325 C->setPrivates(Vars); 12326 Vars.clear(); 12327 for (unsigned I = 0; I != NumVars; ++I) 12328 Vars.push_back(Record.readSubExpr()); 12329 C->setLHSExprs(Vars); 12330 Vars.clear(); 12331 for (unsigned I = 0; I != NumVars; ++I) 12332 Vars.push_back(Record.readSubExpr()); 12333 C->setRHSExprs(Vars); 12334 Vars.clear(); 12335 for (unsigned I = 0; I != NumVars; ++I) 12336 Vars.push_back(Record.readSubExpr()); 12337 C->setReductionOps(Vars); 12338 Vars.clear(); 12339 for (unsigned I = 0; I != NumVars; ++I) 12340 Vars.push_back(Record.readSubExpr()); 12341 C->setTaskgroupDescriptors(Vars); 12342 } 12343 12344 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) { 12345 VisitOMPClauseWithPostUpdate(C); 12346 C->setLParenLoc(Record.readSourceLocation()); 12347 C->setColonLoc(Record.readSourceLocation()); 12348 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt())); 12349 C->setModifierLoc(Record.readSourceLocation()); 12350 unsigned NumVars = C->varlist_size(); 12351 SmallVector<Expr *, 16> Vars; 12352 Vars.reserve(NumVars); 12353 for (unsigned i = 0; i != NumVars; ++i) 12354 Vars.push_back(Record.readSubExpr()); 12355 C->setVarRefs(Vars); 12356 Vars.clear(); 12357 for (unsigned i = 0; i != NumVars; ++i) 12358 Vars.push_back(Record.readSubExpr()); 12359 C->setPrivates(Vars); 12360 Vars.clear(); 12361 for (unsigned i = 0; i != NumVars; ++i) 12362 Vars.push_back(Record.readSubExpr()); 12363 C->setInits(Vars); 12364 Vars.clear(); 12365 for (unsigned i = 0; i != NumVars; ++i) 12366 Vars.push_back(Record.readSubExpr()); 12367 C->setUpdates(Vars); 12368 Vars.clear(); 12369 for (unsigned i = 0; i != NumVars; ++i) 12370 Vars.push_back(Record.readSubExpr()); 12371 C->setFinals(Vars); 12372 C->setStep(Record.readSubExpr()); 12373 C->setCalcStep(Record.readSubExpr()); 12374 Vars.clear(); 12375 for (unsigned I = 0; I != NumVars + 1; ++I) 12376 Vars.push_back(Record.readSubExpr()); 12377 C->setUsedExprs(Vars); 12378 } 12379 12380 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) { 12381 C->setLParenLoc(Record.readSourceLocation()); 12382 C->setColonLoc(Record.readSourceLocation()); 12383 unsigned NumVars = C->varlist_size(); 12384 SmallVector<Expr *, 16> Vars; 12385 Vars.reserve(NumVars); 12386 for (unsigned i = 0; i != NumVars; ++i) 12387 Vars.push_back(Record.readSubExpr()); 12388 C->setVarRefs(Vars); 12389 C->setAlignment(Record.readSubExpr()); 12390 } 12391 12392 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) { 12393 C->setLParenLoc(Record.readSourceLocation()); 12394 unsigned NumVars = C->varlist_size(); 12395 SmallVector<Expr *, 16> Exprs; 12396 Exprs.reserve(NumVars); 12397 for (unsigned i = 0; i != NumVars; ++i) 12398 Exprs.push_back(Record.readSubExpr()); 12399 C->setVarRefs(Exprs); 12400 Exprs.clear(); 12401 for (unsigned i = 0; i != NumVars; ++i) 12402 Exprs.push_back(Record.readSubExpr()); 12403 C->setSourceExprs(Exprs); 12404 Exprs.clear(); 12405 for (unsigned i = 0; i != NumVars; ++i) 12406 Exprs.push_back(Record.readSubExpr()); 12407 C->setDestinationExprs(Exprs); 12408 Exprs.clear(); 12409 for (unsigned i = 0; i != NumVars; ++i) 12410 Exprs.push_back(Record.readSubExpr()); 12411 C->setAssignmentOps(Exprs); 12412 } 12413 12414 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) { 12415 C->setLParenLoc(Record.readSourceLocation()); 12416 unsigned NumVars = C->varlist_size(); 12417 SmallVector<Expr *, 16> Exprs; 12418 Exprs.reserve(NumVars); 12419 for (unsigned i = 0; i != NumVars; ++i) 12420 Exprs.push_back(Record.readSubExpr()); 12421 C->setVarRefs(Exprs); 12422 Exprs.clear(); 12423 for (unsigned i = 0; i != NumVars; ++i) 12424 Exprs.push_back(Record.readSubExpr()); 12425 C->setSourceExprs(Exprs); 12426 Exprs.clear(); 12427 for (unsigned i = 0; i != NumVars; ++i) 12428 Exprs.push_back(Record.readSubExpr()); 12429 C->setDestinationExprs(Exprs); 12430 Exprs.clear(); 12431 for (unsigned i = 0; i != NumVars; ++i) 12432 Exprs.push_back(Record.readSubExpr()); 12433 C->setAssignmentOps(Exprs); 12434 } 12435 12436 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) { 12437 C->setLParenLoc(Record.readSourceLocation()); 12438 unsigned NumVars = C->varlist_size(); 12439 SmallVector<Expr *, 16> Vars; 12440 Vars.reserve(NumVars); 12441 for (unsigned i = 0; i != NumVars; ++i) 12442 Vars.push_back(Record.readSubExpr()); 12443 C->setVarRefs(Vars); 12444 } 12445 12446 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) { 12447 C->setDepobj(Record.readSubExpr()); 12448 C->setLParenLoc(Record.readSourceLocation()); 12449 } 12450 12451 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) { 12452 C->setLParenLoc(Record.readSourceLocation()); 12453 C->setModifier(Record.readSubExpr()); 12454 C->setDependencyKind( 12455 static_cast<OpenMPDependClauseKind>(Record.readInt())); 12456 C->setDependencyLoc(Record.readSourceLocation()); 12457 C->setColonLoc(Record.readSourceLocation()); 12458 unsigned NumVars = C->varlist_size(); 12459 SmallVector<Expr *, 16> Vars; 12460 Vars.reserve(NumVars); 12461 for (unsigned I = 0; I != NumVars; ++I) 12462 Vars.push_back(Record.readSubExpr()); 12463 C->setVarRefs(Vars); 12464 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I) 12465 C->setLoopData(I, Record.readSubExpr()); 12466 } 12467 12468 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) { 12469 VisitOMPClauseWithPreInit(C); 12470 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>()); 12471 C->setDevice(Record.readSubExpr()); 12472 C->setModifierLoc(Record.readSourceLocation()); 12473 C->setLParenLoc(Record.readSourceLocation()); 12474 } 12475 12476 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) { 12477 C->setLParenLoc(Record.readSourceLocation()); 12478 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) { 12479 C->setMapTypeModifier( 12480 I, static_cast<OpenMPMapModifierKind>(Record.readInt())); 12481 C->setMapTypeModifierLoc(I, Record.readSourceLocation()); 12482 } 12483 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12484 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12485 C->setMapType( 12486 static_cast<OpenMPMapClauseKind>(Record.readInt())); 12487 C->setMapLoc(Record.readSourceLocation()); 12488 C->setColonLoc(Record.readSourceLocation()); 12489 auto NumVars = C->varlist_size(); 12490 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12491 auto TotalLists = C->getTotalComponentListNum(); 12492 auto TotalComponents = C->getTotalComponentsNum(); 12493 12494 SmallVector<Expr *, 16> Vars; 12495 Vars.reserve(NumVars); 12496 for (unsigned i = 0; i != NumVars; ++i) 12497 Vars.push_back(Record.readExpr()); 12498 C->setVarRefs(Vars); 12499 12500 SmallVector<Expr *, 16> UDMappers; 12501 UDMappers.reserve(NumVars); 12502 for (unsigned I = 0; I < NumVars; ++I) 12503 UDMappers.push_back(Record.readExpr()); 12504 C->setUDMapperRefs(UDMappers); 12505 12506 SmallVector<ValueDecl *, 16> Decls; 12507 Decls.reserve(UniqueDecls); 12508 for (unsigned i = 0; i < UniqueDecls; ++i) 12509 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12510 C->setUniqueDecls(Decls); 12511 12512 SmallVector<unsigned, 16> ListsPerDecl; 12513 ListsPerDecl.reserve(UniqueDecls); 12514 for (unsigned i = 0; i < UniqueDecls; ++i) 12515 ListsPerDecl.push_back(Record.readInt()); 12516 C->setDeclNumLists(ListsPerDecl); 12517 12518 SmallVector<unsigned, 32> ListSizes; 12519 ListSizes.reserve(TotalLists); 12520 for (unsigned i = 0; i < TotalLists; ++i) 12521 ListSizes.push_back(Record.readInt()); 12522 C->setComponentListSizes(ListSizes); 12523 12524 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12525 Components.reserve(TotalComponents); 12526 for (unsigned i = 0; i < TotalComponents; ++i) { 12527 Expr *AssociatedExpr = Record.readExpr(); 12528 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12529 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12530 AssociatedExpr, AssociatedDecl)); 12531 } 12532 C->setComponents(Components, ListSizes); 12533 } 12534 12535 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) { 12536 C->setLParenLoc(Record.readSourceLocation()); 12537 C->setColonLoc(Record.readSourceLocation()); 12538 C->setAllocator(Record.readSubExpr()); 12539 unsigned NumVars = C->varlist_size(); 12540 SmallVector<Expr *, 16> Vars; 12541 Vars.reserve(NumVars); 12542 for (unsigned i = 0; i != NumVars; ++i) 12543 Vars.push_back(Record.readSubExpr()); 12544 C->setVarRefs(Vars); 12545 } 12546 12547 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) { 12548 VisitOMPClauseWithPreInit(C); 12549 C->setNumTeams(Record.readSubExpr()); 12550 C->setLParenLoc(Record.readSourceLocation()); 12551 } 12552 12553 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) { 12554 VisitOMPClauseWithPreInit(C); 12555 C->setThreadLimit(Record.readSubExpr()); 12556 C->setLParenLoc(Record.readSourceLocation()); 12557 } 12558 12559 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) { 12560 VisitOMPClauseWithPreInit(C); 12561 C->setPriority(Record.readSubExpr()); 12562 C->setLParenLoc(Record.readSourceLocation()); 12563 } 12564 12565 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) { 12566 VisitOMPClauseWithPreInit(C); 12567 C->setGrainsize(Record.readSubExpr()); 12568 C->setLParenLoc(Record.readSourceLocation()); 12569 } 12570 12571 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) { 12572 VisitOMPClauseWithPreInit(C); 12573 C->setNumTasks(Record.readSubExpr()); 12574 C->setLParenLoc(Record.readSourceLocation()); 12575 } 12576 12577 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) { 12578 C->setHint(Record.readSubExpr()); 12579 C->setLParenLoc(Record.readSourceLocation()); 12580 } 12581 12582 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) { 12583 VisitOMPClauseWithPreInit(C); 12584 C->setDistScheduleKind( 12585 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt())); 12586 C->setChunkSize(Record.readSubExpr()); 12587 C->setLParenLoc(Record.readSourceLocation()); 12588 C->setDistScheduleKindLoc(Record.readSourceLocation()); 12589 C->setCommaLoc(Record.readSourceLocation()); 12590 } 12591 12592 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) { 12593 C->setDefaultmapKind( 12594 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt())); 12595 C->setDefaultmapModifier( 12596 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt())); 12597 C->setLParenLoc(Record.readSourceLocation()); 12598 C->setDefaultmapModifierLoc(Record.readSourceLocation()); 12599 C->setDefaultmapKindLoc(Record.readSourceLocation()); 12600 } 12601 12602 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) { 12603 C->setLParenLoc(Record.readSourceLocation()); 12604 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) { 12605 C->setMotionModifier( 12606 I, static_cast<OpenMPMotionModifierKind>(Record.readInt())); 12607 C->setMotionModifierLoc(I, Record.readSourceLocation()); 12608 } 12609 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12610 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12611 C->setColonLoc(Record.readSourceLocation()); 12612 auto NumVars = C->varlist_size(); 12613 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12614 auto TotalLists = C->getTotalComponentListNum(); 12615 auto TotalComponents = C->getTotalComponentsNum(); 12616 12617 SmallVector<Expr *, 16> Vars; 12618 Vars.reserve(NumVars); 12619 for (unsigned i = 0; i != NumVars; ++i) 12620 Vars.push_back(Record.readSubExpr()); 12621 C->setVarRefs(Vars); 12622 12623 SmallVector<Expr *, 16> UDMappers; 12624 UDMappers.reserve(NumVars); 12625 for (unsigned I = 0; I < NumVars; ++I) 12626 UDMappers.push_back(Record.readSubExpr()); 12627 C->setUDMapperRefs(UDMappers); 12628 12629 SmallVector<ValueDecl *, 16> Decls; 12630 Decls.reserve(UniqueDecls); 12631 for (unsigned i = 0; i < UniqueDecls; ++i) 12632 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12633 C->setUniqueDecls(Decls); 12634 12635 SmallVector<unsigned, 16> ListsPerDecl; 12636 ListsPerDecl.reserve(UniqueDecls); 12637 for (unsigned i = 0; i < UniqueDecls; ++i) 12638 ListsPerDecl.push_back(Record.readInt()); 12639 C->setDeclNumLists(ListsPerDecl); 12640 12641 SmallVector<unsigned, 32> ListSizes; 12642 ListSizes.reserve(TotalLists); 12643 for (unsigned i = 0; i < TotalLists; ++i) 12644 ListSizes.push_back(Record.readInt()); 12645 C->setComponentListSizes(ListSizes); 12646 12647 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12648 Components.reserve(TotalComponents); 12649 for (unsigned i = 0; i < TotalComponents; ++i) { 12650 Expr *AssociatedExpr = Record.readSubExpr(); 12651 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12652 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12653 AssociatedExpr, AssociatedDecl)); 12654 } 12655 C->setComponents(Components, ListSizes); 12656 } 12657 12658 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) { 12659 C->setLParenLoc(Record.readSourceLocation()); 12660 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) { 12661 C->setMotionModifier( 12662 I, static_cast<OpenMPMotionModifierKind>(Record.readInt())); 12663 C->setMotionModifierLoc(I, Record.readSourceLocation()); 12664 } 12665 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12666 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12667 C->setColonLoc(Record.readSourceLocation()); 12668 auto NumVars = C->varlist_size(); 12669 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12670 auto TotalLists = C->getTotalComponentListNum(); 12671 auto TotalComponents = C->getTotalComponentsNum(); 12672 12673 SmallVector<Expr *, 16> Vars; 12674 Vars.reserve(NumVars); 12675 for (unsigned i = 0; i != NumVars; ++i) 12676 Vars.push_back(Record.readSubExpr()); 12677 C->setVarRefs(Vars); 12678 12679 SmallVector<Expr *, 16> UDMappers; 12680 UDMappers.reserve(NumVars); 12681 for (unsigned I = 0; I < NumVars; ++I) 12682 UDMappers.push_back(Record.readSubExpr()); 12683 C->setUDMapperRefs(UDMappers); 12684 12685 SmallVector<ValueDecl *, 16> Decls; 12686 Decls.reserve(UniqueDecls); 12687 for (unsigned i = 0; i < UniqueDecls; ++i) 12688 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12689 C->setUniqueDecls(Decls); 12690 12691 SmallVector<unsigned, 16> ListsPerDecl; 12692 ListsPerDecl.reserve(UniqueDecls); 12693 for (unsigned i = 0; i < UniqueDecls; ++i) 12694 ListsPerDecl.push_back(Record.readInt()); 12695 C->setDeclNumLists(ListsPerDecl); 12696 12697 SmallVector<unsigned, 32> ListSizes; 12698 ListSizes.reserve(TotalLists); 12699 for (unsigned i = 0; i < TotalLists; ++i) 12700 ListSizes.push_back(Record.readInt()); 12701 C->setComponentListSizes(ListSizes); 12702 12703 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12704 Components.reserve(TotalComponents); 12705 for (unsigned i = 0; i < TotalComponents; ++i) { 12706 Expr *AssociatedExpr = Record.readSubExpr(); 12707 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12708 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12709 AssociatedExpr, AssociatedDecl)); 12710 } 12711 C->setComponents(Components, ListSizes); 12712 } 12713 12714 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) { 12715 C->setLParenLoc(Record.readSourceLocation()); 12716 auto NumVars = C->varlist_size(); 12717 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12718 auto TotalLists = C->getTotalComponentListNum(); 12719 auto TotalComponents = C->getTotalComponentsNum(); 12720 12721 SmallVector<Expr *, 16> Vars; 12722 Vars.reserve(NumVars); 12723 for (unsigned i = 0; i != NumVars; ++i) 12724 Vars.push_back(Record.readSubExpr()); 12725 C->setVarRefs(Vars); 12726 Vars.clear(); 12727 for (unsigned i = 0; i != NumVars; ++i) 12728 Vars.push_back(Record.readSubExpr()); 12729 C->setPrivateCopies(Vars); 12730 Vars.clear(); 12731 for (unsigned i = 0; i != NumVars; ++i) 12732 Vars.push_back(Record.readSubExpr()); 12733 C->setInits(Vars); 12734 12735 SmallVector<ValueDecl *, 16> Decls; 12736 Decls.reserve(UniqueDecls); 12737 for (unsigned i = 0; i < UniqueDecls; ++i) 12738 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12739 C->setUniqueDecls(Decls); 12740 12741 SmallVector<unsigned, 16> ListsPerDecl; 12742 ListsPerDecl.reserve(UniqueDecls); 12743 for (unsigned i = 0; i < UniqueDecls; ++i) 12744 ListsPerDecl.push_back(Record.readInt()); 12745 C->setDeclNumLists(ListsPerDecl); 12746 12747 SmallVector<unsigned, 32> ListSizes; 12748 ListSizes.reserve(TotalLists); 12749 for (unsigned i = 0; i < TotalLists; ++i) 12750 ListSizes.push_back(Record.readInt()); 12751 C->setComponentListSizes(ListSizes); 12752 12753 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12754 Components.reserve(TotalComponents); 12755 for (unsigned i = 0; i < TotalComponents; ++i) { 12756 Expr *AssociatedExpr = Record.readSubExpr(); 12757 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12758 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12759 AssociatedExpr, AssociatedDecl)); 12760 } 12761 C->setComponents(Components, ListSizes); 12762 } 12763 12764 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) { 12765 C->setLParenLoc(Record.readSourceLocation()); 12766 auto NumVars = C->varlist_size(); 12767 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12768 auto TotalLists = C->getTotalComponentListNum(); 12769 auto TotalComponents = C->getTotalComponentsNum(); 12770 12771 SmallVector<Expr *, 16> Vars; 12772 Vars.reserve(NumVars); 12773 for (unsigned i = 0; i != NumVars; ++i) 12774 Vars.push_back(Record.readSubExpr()); 12775 C->setVarRefs(Vars); 12776 12777 SmallVector<ValueDecl *, 16> Decls; 12778 Decls.reserve(UniqueDecls); 12779 for (unsigned i = 0; i < UniqueDecls; ++i) 12780 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12781 C->setUniqueDecls(Decls); 12782 12783 SmallVector<unsigned, 16> ListsPerDecl; 12784 ListsPerDecl.reserve(UniqueDecls); 12785 for (unsigned i = 0; i < UniqueDecls; ++i) 12786 ListsPerDecl.push_back(Record.readInt()); 12787 C->setDeclNumLists(ListsPerDecl); 12788 12789 SmallVector<unsigned, 32> ListSizes; 12790 ListSizes.reserve(TotalLists); 12791 for (unsigned i = 0; i < TotalLists; ++i) 12792 ListSizes.push_back(Record.readInt()); 12793 C->setComponentListSizes(ListSizes); 12794 12795 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12796 Components.reserve(TotalComponents); 12797 for (unsigned i = 0; i < TotalComponents; ++i) { 12798 Expr *AssociatedExpr = Record.readSubExpr(); 12799 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12800 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12801 AssociatedExpr, AssociatedDecl)); 12802 } 12803 C->setComponents(Components, ListSizes); 12804 } 12805 12806 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) { 12807 C->setLParenLoc(Record.readSourceLocation()); 12808 auto NumVars = C->varlist_size(); 12809 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12810 auto TotalLists = C->getTotalComponentListNum(); 12811 auto TotalComponents = C->getTotalComponentsNum(); 12812 12813 SmallVector<Expr *, 16> Vars; 12814 Vars.reserve(NumVars); 12815 for (unsigned i = 0; i != NumVars; ++i) 12816 Vars.push_back(Record.readSubExpr()); 12817 C->setVarRefs(Vars); 12818 Vars.clear(); 12819 12820 SmallVector<ValueDecl *, 16> Decls; 12821 Decls.reserve(UniqueDecls); 12822 for (unsigned i = 0; i < UniqueDecls; ++i) 12823 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12824 C->setUniqueDecls(Decls); 12825 12826 SmallVector<unsigned, 16> ListsPerDecl; 12827 ListsPerDecl.reserve(UniqueDecls); 12828 for (unsigned i = 0; i < UniqueDecls; ++i) 12829 ListsPerDecl.push_back(Record.readInt()); 12830 C->setDeclNumLists(ListsPerDecl); 12831 12832 SmallVector<unsigned, 32> ListSizes; 12833 ListSizes.reserve(TotalLists); 12834 for (unsigned i = 0; i < TotalLists; ++i) 12835 ListSizes.push_back(Record.readInt()); 12836 C->setComponentListSizes(ListSizes); 12837 12838 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12839 Components.reserve(TotalComponents); 12840 for (unsigned i = 0; i < TotalComponents; ++i) { 12841 Expr *AssociatedExpr = Record.readSubExpr(); 12842 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12843 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12844 AssociatedExpr, AssociatedDecl)); 12845 } 12846 C->setComponents(Components, ListSizes); 12847 } 12848 12849 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) { 12850 C->setLParenLoc(Record.readSourceLocation()); 12851 unsigned NumVars = C->varlist_size(); 12852 SmallVector<Expr *, 16> Vars; 12853 Vars.reserve(NumVars); 12854 for (unsigned i = 0; i != NumVars; ++i) 12855 Vars.push_back(Record.readSubExpr()); 12856 C->setVarRefs(Vars); 12857 Vars.clear(); 12858 Vars.reserve(NumVars); 12859 for (unsigned i = 0; i != NumVars; ++i) 12860 Vars.push_back(Record.readSubExpr()); 12861 C->setPrivateRefs(Vars); 12862 } 12863 12864 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) { 12865 C->setLParenLoc(Record.readSourceLocation()); 12866 unsigned NumVars = C->varlist_size(); 12867 SmallVector<Expr *, 16> Vars; 12868 Vars.reserve(NumVars); 12869 for (unsigned i = 0; i != NumVars; ++i) 12870 Vars.push_back(Record.readSubExpr()); 12871 C->setVarRefs(Vars); 12872 } 12873 12874 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) { 12875 C->setLParenLoc(Record.readSourceLocation()); 12876 unsigned NumVars = C->varlist_size(); 12877 SmallVector<Expr *, 16> Vars; 12878 Vars.reserve(NumVars); 12879 for (unsigned i = 0; i != NumVars; ++i) 12880 Vars.push_back(Record.readSubExpr()); 12881 C->setVarRefs(Vars); 12882 } 12883 12884 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) { 12885 C->setLParenLoc(Record.readSourceLocation()); 12886 unsigned NumOfAllocators = C->getNumberOfAllocators(); 12887 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data; 12888 Data.reserve(NumOfAllocators); 12889 for (unsigned I = 0; I != NumOfAllocators; ++I) { 12890 OMPUsesAllocatorsClause::Data &D = Data.emplace_back(); 12891 D.Allocator = Record.readSubExpr(); 12892 D.AllocatorTraits = Record.readSubExpr(); 12893 D.LParenLoc = Record.readSourceLocation(); 12894 D.RParenLoc = Record.readSourceLocation(); 12895 } 12896 C->setAllocatorsData(Data); 12897 } 12898 12899 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) { 12900 C->setLParenLoc(Record.readSourceLocation()); 12901 C->setModifier(Record.readSubExpr()); 12902 C->setColonLoc(Record.readSourceLocation()); 12903 unsigned NumOfLocators = C->varlist_size(); 12904 SmallVector<Expr *, 4> Locators; 12905 Locators.reserve(NumOfLocators); 12906 for (unsigned I = 0; I != NumOfLocators; ++I) 12907 Locators.push_back(Record.readSubExpr()); 12908 C->setVarRefs(Locators); 12909 } 12910 12911 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) { 12912 C->setKind(Record.readEnum<OpenMPOrderClauseKind>()); 12913 C->setLParenLoc(Record.readSourceLocation()); 12914 C->setKindKwLoc(Record.readSourceLocation()); 12915 } 12916 12917 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() { 12918 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo(); 12919 TI.Sets.resize(readUInt32()); 12920 for (auto &Set : TI.Sets) { 12921 Set.Kind = readEnum<llvm::omp::TraitSet>(); 12922 Set.Selectors.resize(readUInt32()); 12923 for (auto &Selector : Set.Selectors) { 12924 Selector.Kind = readEnum<llvm::omp::TraitSelector>(); 12925 Selector.ScoreOrCondition = nullptr; 12926 if (readBool()) 12927 Selector.ScoreOrCondition = readExprRef(); 12928 Selector.Properties.resize(readUInt32()); 12929 for (auto &Property : Selector.Properties) 12930 Property.Kind = readEnum<llvm::omp::TraitProperty>(); 12931 } 12932 } 12933 return &TI; 12934 } 12935 12936 void ASTRecordReader::readOMPChildren(OMPChildren *Data) { 12937 if (!Data) 12938 return; 12939 if (Reader->ReadingKind == ASTReader::Read_Stmt) { 12940 // Skip NumClauses, NumChildren and HasAssociatedStmt fields. 12941 skipInts(3); 12942 } 12943 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses()); 12944 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I) 12945 Clauses[I] = readOMPClause(); 12946 Data->setClauses(Clauses); 12947 if (Data->hasAssociatedStmt()) 12948 Data->setAssociatedStmt(readStmt()); 12949 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I) 12950 Data->getChildren()[I] = readStmt(); 12951 } 12952