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 "ASTCommon.h" 14 #include "ASTReaderInternals.h" 15 #include "clang/AST/ASTConsumer.h" 16 #include "clang/AST/ASTContext.h" 17 #include "clang/AST/ASTMutationListener.h" 18 #include "clang/AST/ASTStructuralEquivalence.h" 19 #include "clang/AST/ASTUnresolvedSet.h" 20 #include "clang/AST/AbstractTypeReader.h" 21 #include "clang/AST/Decl.h" 22 #include "clang/AST/DeclBase.h" 23 #include "clang/AST/DeclCXX.h" 24 #include "clang/AST/DeclFriend.h" 25 #include "clang/AST/DeclGroup.h" 26 #include "clang/AST/DeclObjC.h" 27 #include "clang/AST/DeclTemplate.h" 28 #include "clang/AST/DeclarationName.h" 29 #include "clang/AST/Expr.h" 30 #include "clang/AST/ExprCXX.h" 31 #include "clang/AST/ExternalASTSource.h" 32 #include "clang/AST/NestedNameSpecifier.h" 33 #include "clang/AST/ODRHash.h" 34 #include "clang/AST/OpenMPClause.h" 35 #include "clang/AST/RawCommentList.h" 36 #include "clang/AST/TemplateBase.h" 37 #include "clang/AST/TemplateName.h" 38 #include "clang/AST/Type.h" 39 #include "clang/AST/TypeLoc.h" 40 #include "clang/AST/TypeLocVisitor.h" 41 #include "clang/AST/UnresolvedSet.h" 42 #include "clang/Basic/CommentOptions.h" 43 #include "clang/Basic/Diagnostic.h" 44 #include "clang/Basic/DiagnosticError.h" 45 #include "clang/Basic/DiagnosticOptions.h" 46 #include "clang/Basic/DiagnosticSema.h" 47 #include "clang/Basic/ExceptionSpecificationType.h" 48 #include "clang/Basic/FileManager.h" 49 #include "clang/Basic/FileSystemOptions.h" 50 #include "clang/Basic/IdentifierTable.h" 51 #include "clang/Basic/LLVM.h" 52 #include "clang/Basic/LangOptions.h" 53 #include "clang/Basic/Module.h" 54 #include "clang/Basic/ObjCRuntime.h" 55 #include "clang/Basic/OpenMPKinds.h" 56 #include "clang/Basic/OperatorKinds.h" 57 #include "clang/Basic/PragmaKinds.h" 58 #include "clang/Basic/Sanitizers.h" 59 #include "clang/Basic/SourceLocation.h" 60 #include "clang/Basic/SourceManager.h" 61 #include "clang/Basic/SourceManagerInternals.h" 62 #include "clang/Basic/Specifiers.h" 63 #include "clang/Basic/TargetInfo.h" 64 #include "clang/Basic/TargetOptions.h" 65 #include "clang/Basic/TokenKinds.h" 66 #include "clang/Basic/Version.h" 67 #include "clang/Lex/HeaderSearch.h" 68 #include "clang/Lex/HeaderSearchOptions.h" 69 #include "clang/Lex/MacroInfo.h" 70 #include "clang/Lex/ModuleMap.h" 71 #include "clang/Lex/PreprocessingRecord.h" 72 #include "clang/Lex/Preprocessor.h" 73 #include "clang/Lex/PreprocessorOptions.h" 74 #include "clang/Lex/Token.h" 75 #include "clang/Sema/ObjCMethodList.h" 76 #include "clang/Sema/Scope.h" 77 #include "clang/Sema/Sema.h" 78 #include "clang/Sema/Weak.h" 79 #include "clang/Serialization/ASTBitCodes.h" 80 #include "clang/Serialization/ASTDeserializationListener.h" 81 #include "clang/Serialization/ASTRecordReader.h" 82 #include "clang/Serialization/ContinuousRangeMap.h" 83 #include "clang/Serialization/GlobalModuleIndex.h" 84 #include "clang/Serialization/InMemoryModuleCache.h" 85 #include "clang/Serialization/ModuleFile.h" 86 #include "clang/Serialization/ModuleFileExtension.h" 87 #include "clang/Serialization/ModuleManager.h" 88 #include "clang/Serialization/PCHContainerOperations.h" 89 #include "clang/Serialization/SerializationDiagnostic.h" 90 #include "llvm/ADT/APFloat.h" 91 #include "llvm/ADT/APInt.h" 92 #include "llvm/ADT/APSInt.h" 93 #include "llvm/ADT/ArrayRef.h" 94 #include "llvm/ADT/DenseMap.h" 95 #include "llvm/ADT/FloatingPointMode.h" 96 #include "llvm/ADT/FoldingSet.h" 97 #include "llvm/ADT/Hashing.h" 98 #include "llvm/ADT/IntrusiveRefCntPtr.h" 99 #include "llvm/ADT/None.h" 100 #include "llvm/ADT/Optional.h" 101 #include "llvm/ADT/STLExtras.h" 102 #include "llvm/ADT/ScopeExit.h" 103 #include "llvm/ADT/SmallPtrSet.h" 104 #include "llvm/ADT/SmallString.h" 105 #include "llvm/ADT/SmallVector.h" 106 #include "llvm/ADT/StringExtras.h" 107 #include "llvm/ADT/StringMap.h" 108 #include "llvm/ADT/StringRef.h" 109 #include "llvm/ADT/Triple.h" 110 #include "llvm/ADT/iterator_range.h" 111 #include "llvm/Bitstream/BitstreamReader.h" 112 #include "llvm/Support/Casting.h" 113 #include "llvm/Support/Compiler.h" 114 #include "llvm/Support/Compression.h" 115 #include "llvm/Support/DJB.h" 116 #include "llvm/Support/Endian.h" 117 #include "llvm/Support/Error.h" 118 #include "llvm/Support/ErrorHandling.h" 119 #include "llvm/Support/FileSystem.h" 120 #include "llvm/Support/LEB128.h" 121 #include "llvm/Support/MemoryBuffer.h" 122 #include "llvm/Support/Path.h" 123 #include "llvm/Support/SaveAndRestore.h" 124 #include "llvm/Support/Timer.h" 125 #include "llvm/Support/VersionTuple.h" 126 #include "llvm/Support/raw_ostream.h" 127 #include <algorithm> 128 #include <cassert> 129 #include <cstddef> 130 #include <cstdint> 131 #include <cstdio> 132 #include <ctime> 133 #include <iterator> 134 #include <limits> 135 #include <map> 136 #include <memory> 137 #include <string> 138 #include <system_error> 139 #include <tuple> 140 #include <utility> 141 #include <vector> 142 143 using namespace clang; 144 using namespace clang::serialization; 145 using namespace clang::serialization::reader; 146 using llvm::BitstreamCursor; 147 148 //===----------------------------------------------------------------------===// 149 // ChainedASTReaderListener implementation 150 //===----------------------------------------------------------------------===// 151 152 bool 153 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) { 154 return First->ReadFullVersionInformation(FullVersion) || 155 Second->ReadFullVersionInformation(FullVersion); 156 } 157 158 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) { 159 First->ReadModuleName(ModuleName); 160 Second->ReadModuleName(ModuleName); 161 } 162 163 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) { 164 First->ReadModuleMapFile(ModuleMapPath); 165 Second->ReadModuleMapFile(ModuleMapPath); 166 } 167 168 bool 169 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts, 170 bool Complain, 171 bool AllowCompatibleDifferences) { 172 return First->ReadLanguageOptions(LangOpts, Complain, 173 AllowCompatibleDifferences) || 174 Second->ReadLanguageOptions(LangOpts, Complain, 175 AllowCompatibleDifferences); 176 } 177 178 bool ChainedASTReaderListener::ReadTargetOptions( 179 const TargetOptions &TargetOpts, bool Complain, 180 bool AllowCompatibleDifferences) { 181 return First->ReadTargetOptions(TargetOpts, Complain, 182 AllowCompatibleDifferences) || 183 Second->ReadTargetOptions(TargetOpts, Complain, 184 AllowCompatibleDifferences); 185 } 186 187 bool ChainedASTReaderListener::ReadDiagnosticOptions( 188 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 189 return First->ReadDiagnosticOptions(DiagOpts, Complain) || 190 Second->ReadDiagnosticOptions(DiagOpts, Complain); 191 } 192 193 bool 194 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts, 195 bool Complain) { 196 return First->ReadFileSystemOptions(FSOpts, Complain) || 197 Second->ReadFileSystemOptions(FSOpts, Complain); 198 } 199 200 bool ChainedASTReaderListener::ReadHeaderSearchOptions( 201 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath, 202 bool Complain) { 203 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 204 Complain) || 205 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 206 Complain); 207 } 208 209 bool ChainedASTReaderListener::ReadPreprocessorOptions( 210 const PreprocessorOptions &PPOpts, bool Complain, 211 std::string &SuggestedPredefines) { 212 return First->ReadPreprocessorOptions(PPOpts, Complain, 213 SuggestedPredefines) || 214 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines); 215 } 216 217 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M, 218 unsigned Value) { 219 First->ReadCounter(M, Value); 220 Second->ReadCounter(M, Value); 221 } 222 223 bool ChainedASTReaderListener::needsInputFileVisitation() { 224 return First->needsInputFileVisitation() || 225 Second->needsInputFileVisitation(); 226 } 227 228 bool ChainedASTReaderListener::needsSystemInputFileVisitation() { 229 return First->needsSystemInputFileVisitation() || 230 Second->needsSystemInputFileVisitation(); 231 } 232 233 void ChainedASTReaderListener::visitModuleFile(StringRef Filename, 234 ModuleKind Kind) { 235 First->visitModuleFile(Filename, Kind); 236 Second->visitModuleFile(Filename, Kind); 237 } 238 239 bool ChainedASTReaderListener::visitInputFile(StringRef Filename, 240 bool isSystem, 241 bool isOverridden, 242 bool isExplicitModule) { 243 bool Continue = false; 244 if (First->needsInputFileVisitation() && 245 (!isSystem || First->needsSystemInputFileVisitation())) 246 Continue |= First->visitInputFile(Filename, isSystem, isOverridden, 247 isExplicitModule); 248 if (Second->needsInputFileVisitation() && 249 (!isSystem || Second->needsSystemInputFileVisitation())) 250 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden, 251 isExplicitModule); 252 return Continue; 253 } 254 255 void ChainedASTReaderListener::readModuleFileExtension( 256 const ModuleFileExtensionMetadata &Metadata) { 257 First->readModuleFileExtension(Metadata); 258 Second->readModuleFileExtension(Metadata); 259 } 260 261 //===----------------------------------------------------------------------===// 262 // PCH validator implementation 263 //===----------------------------------------------------------------------===// 264 265 ASTReaderListener::~ASTReaderListener() = default; 266 267 /// Compare the given set of language options against an existing set of 268 /// language options. 269 /// 270 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 271 /// \param AllowCompatibleDifferences If true, differences between compatible 272 /// language options will be permitted. 273 /// 274 /// \returns true if the languagae options mis-match, false otherwise. 275 static bool checkLanguageOptions(const LangOptions &LangOpts, 276 const LangOptions &ExistingLangOpts, 277 DiagnosticsEngine *Diags, 278 bool AllowCompatibleDifferences = true) { 279 #define LANGOPT(Name, Bits, Default, Description) \ 280 if (ExistingLangOpts.Name != LangOpts.Name) { \ 281 if (Diags) \ 282 Diags->Report(diag::err_pch_langopt_mismatch) \ 283 << Description << LangOpts.Name << ExistingLangOpts.Name; \ 284 return true; \ 285 } 286 287 #define VALUE_LANGOPT(Name, Bits, Default, Description) \ 288 if (ExistingLangOpts.Name != LangOpts.Name) { \ 289 if (Diags) \ 290 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 291 << Description; \ 292 return true; \ 293 } 294 295 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 296 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \ 297 if (Diags) \ 298 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 299 << Description; \ 300 return true; \ 301 } 302 303 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \ 304 if (!AllowCompatibleDifferences) \ 305 LANGOPT(Name, Bits, Default, Description) 306 307 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \ 308 if (!AllowCompatibleDifferences) \ 309 ENUM_LANGOPT(Name, Bits, Default, Description) 310 311 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \ 312 if (!AllowCompatibleDifferences) \ 313 VALUE_LANGOPT(Name, Bits, Default, Description) 314 315 #define BENIGN_LANGOPT(Name, Bits, Default, Description) 316 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) 317 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description) 318 #include "clang/Basic/LangOptions.def" 319 320 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) { 321 if (Diags) 322 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features"; 323 return true; 324 } 325 326 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) { 327 if (Diags) 328 Diags->Report(diag::err_pch_langopt_value_mismatch) 329 << "target Objective-C runtime"; 330 return true; 331 } 332 333 if (ExistingLangOpts.CommentOpts.BlockCommandNames != 334 LangOpts.CommentOpts.BlockCommandNames) { 335 if (Diags) 336 Diags->Report(diag::err_pch_langopt_value_mismatch) 337 << "block command names"; 338 return true; 339 } 340 341 // Sanitizer feature mismatches are treated as compatible differences. If 342 // compatible differences aren't allowed, we still only want to check for 343 // mismatches of non-modular sanitizers (the only ones which can affect AST 344 // generation). 345 if (!AllowCompatibleDifferences) { 346 SanitizerMask ModularSanitizers = getPPTransparentSanitizers(); 347 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize; 348 SanitizerSet ImportedSanitizers = LangOpts.Sanitize; 349 ExistingSanitizers.clear(ModularSanitizers); 350 ImportedSanitizers.clear(ModularSanitizers); 351 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) { 352 const std::string Flag = "-fsanitize="; 353 if (Diags) { 354 #define SANITIZER(NAME, ID) \ 355 { \ 356 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \ 357 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \ 358 if (InExistingModule != InImportedModule) \ 359 Diags->Report(diag::err_pch_targetopt_feature_mismatch) \ 360 << InExistingModule << (Flag + NAME); \ 361 } 362 #include "clang/Basic/Sanitizers.def" 363 } 364 return true; 365 } 366 } 367 368 return false; 369 } 370 371 /// Compare the given set of target options against an existing set of 372 /// target options. 373 /// 374 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 375 /// 376 /// \returns true if the target options mis-match, false otherwise. 377 static bool checkTargetOptions(const TargetOptions &TargetOpts, 378 const TargetOptions &ExistingTargetOpts, 379 DiagnosticsEngine *Diags, 380 bool AllowCompatibleDifferences = true) { 381 #define CHECK_TARGET_OPT(Field, Name) \ 382 if (TargetOpts.Field != ExistingTargetOpts.Field) { \ 383 if (Diags) \ 384 Diags->Report(diag::err_pch_targetopt_mismatch) \ 385 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \ 386 return true; \ 387 } 388 389 // The triple and ABI must match exactly. 390 CHECK_TARGET_OPT(Triple, "target"); 391 CHECK_TARGET_OPT(ABI, "target ABI"); 392 393 // We can tolerate different CPUs in many cases, notably when one CPU 394 // supports a strict superset of another. When allowing compatible 395 // differences skip this check. 396 if (!AllowCompatibleDifferences) { 397 CHECK_TARGET_OPT(CPU, "target CPU"); 398 CHECK_TARGET_OPT(TuneCPU, "tune CPU"); 399 } 400 401 #undef CHECK_TARGET_OPT 402 403 // Compare feature sets. 404 SmallVector<StringRef, 4> ExistingFeatures( 405 ExistingTargetOpts.FeaturesAsWritten.begin(), 406 ExistingTargetOpts.FeaturesAsWritten.end()); 407 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(), 408 TargetOpts.FeaturesAsWritten.end()); 409 llvm::sort(ExistingFeatures); 410 llvm::sort(ReadFeatures); 411 412 // We compute the set difference in both directions explicitly so that we can 413 // diagnose the differences differently. 414 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures; 415 std::set_difference( 416 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(), 417 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures)); 418 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(), 419 ExistingFeatures.begin(), ExistingFeatures.end(), 420 std::back_inserter(UnmatchedReadFeatures)); 421 422 // If we are allowing compatible differences and the read feature set is 423 // a strict subset of the existing feature set, there is nothing to diagnose. 424 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty()) 425 return false; 426 427 if (Diags) { 428 for (StringRef Feature : UnmatchedReadFeatures) 429 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 430 << /* is-existing-feature */ false << Feature; 431 for (StringRef Feature : UnmatchedExistingFeatures) 432 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 433 << /* is-existing-feature */ true << Feature; 434 } 435 436 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty(); 437 } 438 439 bool 440 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts, 441 bool Complain, 442 bool AllowCompatibleDifferences) { 443 const LangOptions &ExistingLangOpts = PP.getLangOpts(); 444 return checkLanguageOptions(LangOpts, ExistingLangOpts, 445 Complain ? &Reader.Diags : nullptr, 446 AllowCompatibleDifferences); 447 } 448 449 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts, 450 bool Complain, 451 bool AllowCompatibleDifferences) { 452 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts(); 453 return checkTargetOptions(TargetOpts, ExistingTargetOpts, 454 Complain ? &Reader.Diags : nullptr, 455 AllowCompatibleDifferences); 456 } 457 458 namespace { 459 460 using MacroDefinitionsMap = 461 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>; 462 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>; 463 464 } // namespace 465 466 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags, 467 DiagnosticsEngine &Diags, 468 bool Complain) { 469 using Level = DiagnosticsEngine::Level; 470 471 // Check current mappings for new -Werror mappings, and the stored mappings 472 // for cases that were explicitly mapped to *not* be errors that are now 473 // errors because of options like -Werror. 474 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags }; 475 476 for (DiagnosticsEngine *MappingSource : MappingSources) { 477 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) { 478 diag::kind DiagID = DiagIDMappingPair.first; 479 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation()); 480 if (CurLevel < DiagnosticsEngine::Error) 481 continue; // not significant 482 Level StoredLevel = 483 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation()); 484 if (StoredLevel < DiagnosticsEngine::Error) { 485 if (Complain) 486 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" + 487 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str(); 488 return true; 489 } 490 } 491 } 492 493 return false; 494 } 495 496 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) { 497 diag::Severity Ext = Diags.getExtensionHandlingBehavior(); 498 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors()) 499 return true; 500 return Ext >= diag::Severity::Error; 501 } 502 503 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags, 504 DiagnosticsEngine &Diags, 505 bool IsSystem, bool Complain) { 506 // Top-level options 507 if (IsSystem) { 508 if (Diags.getSuppressSystemWarnings()) 509 return false; 510 // If -Wsystem-headers was not enabled before, be conservative 511 if (StoredDiags.getSuppressSystemWarnings()) { 512 if (Complain) 513 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers"; 514 return true; 515 } 516 } 517 518 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) { 519 if (Complain) 520 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror"; 521 return true; 522 } 523 524 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() && 525 !StoredDiags.getEnableAllWarnings()) { 526 if (Complain) 527 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror"; 528 return true; 529 } 530 531 if (isExtHandlingFromDiagsError(Diags) && 532 !isExtHandlingFromDiagsError(StoredDiags)) { 533 if (Complain) 534 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors"; 535 return true; 536 } 537 538 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain); 539 } 540 541 /// Return the top import module if it is implicit, nullptr otherwise. 542 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr, 543 Preprocessor &PP) { 544 // If the original import came from a file explicitly generated by the user, 545 // don't check the diagnostic mappings. 546 // FIXME: currently this is approximated by checking whether this is not a 547 // module import of an implicitly-loaded module file. 548 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in 549 // the transitive closure of its imports, since unrelated modules cannot be 550 // imported until after this module finishes validation. 551 ModuleFile *TopImport = &*ModuleMgr.rbegin(); 552 while (!TopImport->ImportedBy.empty()) 553 TopImport = TopImport->ImportedBy[0]; 554 if (TopImport->Kind != MK_ImplicitModule) 555 return nullptr; 556 557 StringRef ModuleName = TopImport->ModuleName; 558 assert(!ModuleName.empty() && "diagnostic options read before module name"); 559 560 Module *M = 561 PP.getHeaderSearchInfo().lookupModule(ModuleName, TopImport->ImportLoc); 562 assert(M && "missing module"); 563 return M; 564 } 565 566 bool PCHValidator::ReadDiagnosticOptions( 567 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 568 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics(); 569 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs()); 570 IntrusiveRefCntPtr<DiagnosticsEngine> Diags( 571 new DiagnosticsEngine(DiagIDs, DiagOpts.get())); 572 // This should never fail, because we would have processed these options 573 // before writing them to an ASTFile. 574 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false); 575 576 ModuleManager &ModuleMgr = Reader.getModuleManager(); 577 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then"); 578 579 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP); 580 if (!TopM) 581 return false; 582 583 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that 584 // contains the union of their flags. 585 return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem, 586 Complain); 587 } 588 589 /// Collect the macro definitions provided by the given preprocessor 590 /// options. 591 static void 592 collectMacroDefinitions(const PreprocessorOptions &PPOpts, 593 MacroDefinitionsMap &Macros, 594 SmallVectorImpl<StringRef> *MacroNames = nullptr) { 595 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) { 596 StringRef Macro = PPOpts.Macros[I].first; 597 bool IsUndef = PPOpts.Macros[I].second; 598 599 std::pair<StringRef, StringRef> MacroPair = Macro.split('='); 600 StringRef MacroName = MacroPair.first; 601 StringRef MacroBody = MacroPair.second; 602 603 // For an #undef'd macro, we only care about the name. 604 if (IsUndef) { 605 if (MacroNames && !Macros.count(MacroName)) 606 MacroNames->push_back(MacroName); 607 608 Macros[MacroName] = std::make_pair("", true); 609 continue; 610 } 611 612 // For a #define'd macro, figure out the actual definition. 613 if (MacroName.size() == Macro.size()) 614 MacroBody = "1"; 615 else { 616 // Note: GCC drops anything following an end-of-line character. 617 StringRef::size_type End = MacroBody.find_first_of("\n\r"); 618 MacroBody = MacroBody.substr(0, End); 619 } 620 621 if (MacroNames && !Macros.count(MacroName)) 622 MacroNames->push_back(MacroName); 623 Macros[MacroName] = std::make_pair(MacroBody, false); 624 } 625 } 626 627 /// Check the preprocessor options deserialized from the control block 628 /// against the preprocessor options in an existing preprocessor. 629 /// 630 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 631 /// \param Validate If true, validate preprocessor options. If false, allow 632 /// macros defined by \p ExistingPPOpts to override those defined by 633 /// \p PPOpts in SuggestedPredefines. 634 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts, 635 const PreprocessorOptions &ExistingPPOpts, 636 DiagnosticsEngine *Diags, 637 FileManager &FileMgr, 638 std::string &SuggestedPredefines, 639 const LangOptions &LangOpts, 640 bool Validate = true) { 641 // Check macro definitions. 642 MacroDefinitionsMap ASTFileMacros; 643 collectMacroDefinitions(PPOpts, ASTFileMacros); 644 MacroDefinitionsMap ExistingMacros; 645 SmallVector<StringRef, 4> ExistingMacroNames; 646 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames); 647 648 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) { 649 // Dig out the macro definition in the existing preprocessor options. 650 StringRef MacroName = ExistingMacroNames[I]; 651 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName]; 652 653 // Check whether we know anything about this macro name or not. 654 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known = 655 ASTFileMacros.find(MacroName); 656 if (!Validate || Known == ASTFileMacros.end()) { 657 // FIXME: Check whether this identifier was referenced anywhere in the 658 // AST file. If so, we should reject the AST file. Unfortunately, this 659 // information isn't in the control block. What shall we do about it? 660 661 if (Existing.second) { 662 SuggestedPredefines += "#undef "; 663 SuggestedPredefines += MacroName.str(); 664 SuggestedPredefines += '\n'; 665 } else { 666 SuggestedPredefines += "#define "; 667 SuggestedPredefines += MacroName.str(); 668 SuggestedPredefines += ' '; 669 SuggestedPredefines += Existing.first.str(); 670 SuggestedPredefines += '\n'; 671 } 672 continue; 673 } 674 675 // If the macro was defined in one but undef'd in the other, we have a 676 // conflict. 677 if (Existing.second != Known->second.second) { 678 if (Diags) { 679 Diags->Report(diag::err_pch_macro_def_undef) 680 << MacroName << Known->second.second; 681 } 682 return true; 683 } 684 685 // If the macro was #undef'd in both, or if the macro bodies are identical, 686 // it's fine. 687 if (Existing.second || Existing.first == Known->second.first) 688 continue; 689 690 // The macro bodies differ; complain. 691 if (Diags) { 692 Diags->Report(diag::err_pch_macro_def_conflict) 693 << MacroName << Known->second.first << Existing.first; 694 } 695 return true; 696 } 697 698 // Check whether we're using predefines. 699 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) { 700 if (Diags) { 701 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines; 702 } 703 return true; 704 } 705 706 // Detailed record is important since it is used for the module cache hash. 707 if (LangOpts.Modules && 708 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) { 709 if (Diags) { 710 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord; 711 } 712 return true; 713 } 714 715 // Compute the #include and #include_macros lines we need. 716 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) { 717 StringRef File = ExistingPPOpts.Includes[I]; 718 719 if (!ExistingPPOpts.ImplicitPCHInclude.empty() && 720 !ExistingPPOpts.PCHThroughHeader.empty()) { 721 // In case the through header is an include, we must add all the includes 722 // to the predefines so the start point can be determined. 723 SuggestedPredefines += "#include \""; 724 SuggestedPredefines += File; 725 SuggestedPredefines += "\"\n"; 726 continue; 727 } 728 729 if (File == ExistingPPOpts.ImplicitPCHInclude) 730 continue; 731 732 if (llvm::is_contained(PPOpts.Includes, File)) 733 continue; 734 735 SuggestedPredefines += "#include \""; 736 SuggestedPredefines += File; 737 SuggestedPredefines += "\"\n"; 738 } 739 740 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) { 741 StringRef File = ExistingPPOpts.MacroIncludes[I]; 742 if (llvm::is_contained(PPOpts.MacroIncludes, File)) 743 continue; 744 745 SuggestedPredefines += "#__include_macros \""; 746 SuggestedPredefines += File; 747 SuggestedPredefines += "\"\n##\n"; 748 } 749 750 return false; 751 } 752 753 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 754 bool Complain, 755 std::string &SuggestedPredefines) { 756 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts(); 757 758 return checkPreprocessorOptions(PPOpts, ExistingPPOpts, 759 Complain? &Reader.Diags : nullptr, 760 PP.getFileManager(), 761 SuggestedPredefines, 762 PP.getLangOpts()); 763 } 764 765 bool SimpleASTReaderListener::ReadPreprocessorOptions( 766 const PreprocessorOptions &PPOpts, 767 bool Complain, 768 std::string &SuggestedPredefines) { 769 return checkPreprocessorOptions(PPOpts, 770 PP.getPreprocessorOpts(), 771 nullptr, 772 PP.getFileManager(), 773 SuggestedPredefines, 774 PP.getLangOpts(), 775 false); 776 } 777 778 /// Check the header search options deserialized from the control block 779 /// against the header search options in an existing preprocessor. 780 /// 781 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 782 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 783 StringRef SpecificModuleCachePath, 784 StringRef ExistingModuleCachePath, 785 DiagnosticsEngine *Diags, 786 const LangOptions &LangOpts, 787 const PreprocessorOptions &PPOpts) { 788 if (LangOpts.Modules) { 789 if (SpecificModuleCachePath != ExistingModuleCachePath && 790 !PPOpts.AllowPCHWithDifferentModulesCachePath) { 791 if (Diags) 792 Diags->Report(diag::err_pch_modulecache_mismatch) 793 << SpecificModuleCachePath << ExistingModuleCachePath; 794 return true; 795 } 796 } 797 798 return false; 799 } 800 801 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 802 StringRef SpecificModuleCachePath, 803 bool Complain) { 804 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 805 PP.getHeaderSearchInfo().getModuleCachePath(), 806 Complain ? &Reader.Diags : nullptr, 807 PP.getLangOpts(), PP.getPreprocessorOpts()); 808 } 809 810 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) { 811 PP.setCounterValue(Value); 812 } 813 814 //===----------------------------------------------------------------------===// 815 // AST reader implementation 816 //===----------------------------------------------------------------------===// 817 818 static uint64_t readULEB(const unsigned char *&P) { 819 unsigned Length = 0; 820 const char *Error = nullptr; 821 822 uint64_t Val = llvm::decodeULEB128(P, &Length, nullptr, &Error); 823 if (Error) 824 llvm::report_fatal_error(Error); 825 P += Length; 826 return Val; 827 } 828 829 /// Read ULEB-encoded key length and data length. 830 static std::pair<unsigned, unsigned> 831 readULEBKeyDataLength(const unsigned char *&P) { 832 unsigned KeyLen = readULEB(P); 833 if ((unsigned)KeyLen != KeyLen) 834 llvm::report_fatal_error("key too large"); 835 836 unsigned DataLen = readULEB(P); 837 if ((unsigned)DataLen != DataLen) 838 llvm::report_fatal_error("data too large"); 839 840 return std::make_pair(KeyLen, DataLen); 841 } 842 843 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener, 844 bool TakeOwnership) { 845 DeserializationListener = Listener; 846 OwnsDeserializationListener = TakeOwnership; 847 } 848 849 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) { 850 return serialization::ComputeHash(Sel); 851 } 852 853 std::pair<unsigned, unsigned> 854 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 855 return readULEBKeyDataLength(d); 856 } 857 858 ASTSelectorLookupTrait::internal_key_type 859 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) { 860 using namespace llvm::support; 861 862 SelectorTable &SelTable = Reader.getContext().Selectors; 863 unsigned N = endian::readNext<uint16_t, little, unaligned>(d); 864 IdentifierInfo *FirstII = Reader.getLocalIdentifier( 865 F, endian::readNext<uint32_t, little, unaligned>(d)); 866 if (N == 0) 867 return SelTable.getNullarySelector(FirstII); 868 else if (N == 1) 869 return SelTable.getUnarySelector(FirstII); 870 871 SmallVector<IdentifierInfo *, 16> Args; 872 Args.push_back(FirstII); 873 for (unsigned I = 1; I != N; ++I) 874 Args.push_back(Reader.getLocalIdentifier( 875 F, endian::readNext<uint32_t, little, unaligned>(d))); 876 877 return SelTable.getSelector(N, Args.data()); 878 } 879 880 ASTSelectorLookupTrait::data_type 881 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d, 882 unsigned DataLen) { 883 using namespace llvm::support; 884 885 data_type Result; 886 887 Result.ID = Reader.getGlobalSelectorID( 888 F, endian::readNext<uint32_t, little, unaligned>(d)); 889 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d); 890 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d); 891 Result.InstanceBits = FullInstanceBits & 0x3; 892 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1; 893 Result.FactoryBits = FullFactoryBits & 0x3; 894 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1; 895 unsigned NumInstanceMethods = FullInstanceBits >> 3; 896 unsigned NumFactoryMethods = FullFactoryBits >> 3; 897 898 // Load instance methods 899 for (unsigned I = 0; I != NumInstanceMethods; ++I) { 900 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 901 F, endian::readNext<uint32_t, little, unaligned>(d))) 902 Result.Instance.push_back(Method); 903 } 904 905 // Load factory methods 906 for (unsigned I = 0; I != NumFactoryMethods; ++I) { 907 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 908 F, endian::readNext<uint32_t, little, unaligned>(d))) 909 Result.Factory.push_back(Method); 910 } 911 912 return Result; 913 } 914 915 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) { 916 return llvm::djbHash(a); 917 } 918 919 std::pair<unsigned, unsigned> 920 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) { 921 return readULEBKeyDataLength(d); 922 } 923 924 ASTIdentifierLookupTraitBase::internal_key_type 925 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) { 926 assert(n >= 2 && d[n-1] == '\0'); 927 return StringRef((const char*) d, n-1); 928 } 929 930 /// Whether the given identifier is "interesting". 931 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II, 932 bool IsModule) { 933 return II.hadMacroDefinition() || II.isPoisoned() || 934 (!IsModule && II.getObjCOrBuiltinID()) || 935 II.hasRevertedTokenIDToIdentifier() || 936 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) && 937 II.getFETokenInfo()); 938 } 939 940 static bool readBit(unsigned &Bits) { 941 bool Value = Bits & 0x1; 942 Bits >>= 1; 943 return Value; 944 } 945 946 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) { 947 using namespace llvm::support; 948 949 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 950 return Reader.getGlobalIdentifierID(F, RawID >> 1); 951 } 952 953 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) { 954 if (!II.isFromAST()) { 955 II.setIsFromAST(); 956 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr; 957 if (isInterestingIdentifier(Reader, II, IsModule)) 958 II.setChangedSinceDeserialization(); 959 } 960 } 961 962 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k, 963 const unsigned char* d, 964 unsigned DataLen) { 965 using namespace llvm::support; 966 967 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 968 bool IsInteresting = RawID & 0x01; 969 970 // Wipe out the "is interesting" bit. 971 RawID = RawID >> 1; 972 973 // Build the IdentifierInfo and link the identifier ID with it. 974 IdentifierInfo *II = KnownII; 975 if (!II) { 976 II = &Reader.getIdentifierTable().getOwn(k); 977 KnownII = II; 978 } 979 markIdentifierFromAST(Reader, *II); 980 Reader.markIdentifierUpToDate(II); 981 982 IdentID ID = Reader.getGlobalIdentifierID(F, RawID); 983 if (!IsInteresting) { 984 // For uninteresting identifiers, there's nothing else to do. Just notify 985 // the reader that we've finished loading this identifier. 986 Reader.SetIdentifierInfo(ID, II); 987 return II; 988 } 989 990 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d); 991 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d); 992 bool CPlusPlusOperatorKeyword = readBit(Bits); 993 bool HasRevertedTokenIDToIdentifier = readBit(Bits); 994 bool Poisoned = readBit(Bits); 995 bool ExtensionToken = readBit(Bits); 996 bool HadMacroDefinition = readBit(Bits); 997 998 assert(Bits == 0 && "Extra bits in the identifier?"); 999 DataLen -= 8; 1000 1001 // Set or check the various bits in the IdentifierInfo structure. 1002 // Token IDs are read-only. 1003 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier) 1004 II->revertTokenIDToIdentifier(); 1005 if (!F.isModule()) 1006 II->setObjCOrBuiltinID(ObjCOrBuiltinID); 1007 assert(II->isExtensionToken() == ExtensionToken && 1008 "Incorrect extension token flag"); 1009 (void)ExtensionToken; 1010 if (Poisoned) 1011 II->setIsPoisoned(true); 1012 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword && 1013 "Incorrect C++ operator keyword flag"); 1014 (void)CPlusPlusOperatorKeyword; 1015 1016 // If this identifier is a macro, deserialize the macro 1017 // definition. 1018 if (HadMacroDefinition) { 1019 uint32_t MacroDirectivesOffset = 1020 endian::readNext<uint32_t, little, unaligned>(d); 1021 DataLen -= 4; 1022 1023 Reader.addPendingMacro(II, &F, MacroDirectivesOffset); 1024 } 1025 1026 Reader.SetIdentifierInfo(ID, II); 1027 1028 // Read all of the declarations visible at global scope with this 1029 // name. 1030 if (DataLen > 0) { 1031 SmallVector<uint32_t, 4> DeclIDs; 1032 for (; DataLen > 0; DataLen -= 4) 1033 DeclIDs.push_back(Reader.getGlobalDeclID( 1034 F, endian::readNext<uint32_t, little, unaligned>(d))); 1035 Reader.SetGloballyVisibleDecls(II, DeclIDs); 1036 } 1037 1038 return II; 1039 } 1040 1041 DeclarationNameKey::DeclarationNameKey(DeclarationName Name) 1042 : Kind(Name.getNameKind()) { 1043 switch (Kind) { 1044 case DeclarationName::Identifier: 1045 Data = (uint64_t)Name.getAsIdentifierInfo(); 1046 break; 1047 case DeclarationName::ObjCZeroArgSelector: 1048 case DeclarationName::ObjCOneArgSelector: 1049 case DeclarationName::ObjCMultiArgSelector: 1050 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr(); 1051 break; 1052 case DeclarationName::CXXOperatorName: 1053 Data = Name.getCXXOverloadedOperator(); 1054 break; 1055 case DeclarationName::CXXLiteralOperatorName: 1056 Data = (uint64_t)Name.getCXXLiteralIdentifier(); 1057 break; 1058 case DeclarationName::CXXDeductionGuideName: 1059 Data = (uint64_t)Name.getCXXDeductionGuideTemplate() 1060 ->getDeclName().getAsIdentifierInfo(); 1061 break; 1062 case DeclarationName::CXXConstructorName: 1063 case DeclarationName::CXXDestructorName: 1064 case DeclarationName::CXXConversionFunctionName: 1065 case DeclarationName::CXXUsingDirective: 1066 Data = 0; 1067 break; 1068 } 1069 } 1070 1071 unsigned DeclarationNameKey::getHash() const { 1072 llvm::FoldingSetNodeID ID; 1073 ID.AddInteger(Kind); 1074 1075 switch (Kind) { 1076 case DeclarationName::Identifier: 1077 case DeclarationName::CXXLiteralOperatorName: 1078 case DeclarationName::CXXDeductionGuideName: 1079 ID.AddString(((IdentifierInfo*)Data)->getName()); 1080 break; 1081 case DeclarationName::ObjCZeroArgSelector: 1082 case DeclarationName::ObjCOneArgSelector: 1083 case DeclarationName::ObjCMultiArgSelector: 1084 ID.AddInteger(serialization::ComputeHash(Selector(Data))); 1085 break; 1086 case DeclarationName::CXXOperatorName: 1087 ID.AddInteger((OverloadedOperatorKind)Data); 1088 break; 1089 case DeclarationName::CXXConstructorName: 1090 case DeclarationName::CXXDestructorName: 1091 case DeclarationName::CXXConversionFunctionName: 1092 case DeclarationName::CXXUsingDirective: 1093 break; 1094 } 1095 1096 return ID.ComputeHash(); 1097 } 1098 1099 ModuleFile * 1100 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) { 1101 using namespace llvm::support; 1102 1103 uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d); 1104 return Reader.getLocalModuleFile(F, ModuleFileID); 1105 } 1106 1107 std::pair<unsigned, unsigned> 1108 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) { 1109 return readULEBKeyDataLength(d); 1110 } 1111 1112 ASTDeclContextNameLookupTrait::internal_key_type 1113 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) { 1114 using namespace llvm::support; 1115 1116 auto Kind = (DeclarationName::NameKind)*d++; 1117 uint64_t Data; 1118 switch (Kind) { 1119 case DeclarationName::Identifier: 1120 case DeclarationName::CXXLiteralOperatorName: 1121 case DeclarationName::CXXDeductionGuideName: 1122 Data = (uint64_t)Reader.getLocalIdentifier( 1123 F, endian::readNext<uint32_t, little, unaligned>(d)); 1124 break; 1125 case DeclarationName::ObjCZeroArgSelector: 1126 case DeclarationName::ObjCOneArgSelector: 1127 case DeclarationName::ObjCMultiArgSelector: 1128 Data = 1129 (uint64_t)Reader.getLocalSelector( 1130 F, endian::readNext<uint32_t, little, unaligned>( 1131 d)).getAsOpaquePtr(); 1132 break; 1133 case DeclarationName::CXXOperatorName: 1134 Data = *d++; // OverloadedOperatorKind 1135 break; 1136 case DeclarationName::CXXConstructorName: 1137 case DeclarationName::CXXDestructorName: 1138 case DeclarationName::CXXConversionFunctionName: 1139 case DeclarationName::CXXUsingDirective: 1140 Data = 0; 1141 break; 1142 } 1143 1144 return DeclarationNameKey(Kind, Data); 1145 } 1146 1147 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type, 1148 const unsigned char *d, 1149 unsigned DataLen, 1150 data_type_builder &Val) { 1151 using namespace llvm::support; 1152 1153 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) { 1154 uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d); 1155 Val.insert(Reader.getGlobalDeclID(F, LocalID)); 1156 } 1157 } 1158 1159 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M, 1160 BitstreamCursor &Cursor, 1161 uint64_t Offset, 1162 DeclContext *DC) { 1163 assert(Offset != 0); 1164 1165 SavedStreamPosition SavedPosition(Cursor); 1166 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1167 Error(std::move(Err)); 1168 return true; 1169 } 1170 1171 RecordData Record; 1172 StringRef Blob; 1173 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1174 if (!MaybeCode) { 1175 Error(MaybeCode.takeError()); 1176 return true; 1177 } 1178 unsigned Code = MaybeCode.get(); 1179 1180 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1181 if (!MaybeRecCode) { 1182 Error(MaybeRecCode.takeError()); 1183 return true; 1184 } 1185 unsigned RecCode = MaybeRecCode.get(); 1186 if (RecCode != DECL_CONTEXT_LEXICAL) { 1187 Error("Expected lexical block"); 1188 return true; 1189 } 1190 1191 assert(!isa<TranslationUnitDecl>(DC) && 1192 "expected a TU_UPDATE_LEXICAL record for TU"); 1193 // If we are handling a C++ class template instantiation, we can see multiple 1194 // lexical updates for the same record. It's important that we select only one 1195 // of them, so that field numbering works properly. Just pick the first one we 1196 // see. 1197 auto &Lex = LexicalDecls[DC]; 1198 if (!Lex.first) { 1199 Lex = std::make_pair( 1200 &M, llvm::makeArrayRef( 1201 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 1202 Blob.data()), 1203 Blob.size() / 4)); 1204 } 1205 DC->setHasExternalLexicalStorage(true); 1206 return false; 1207 } 1208 1209 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M, 1210 BitstreamCursor &Cursor, 1211 uint64_t Offset, 1212 DeclID ID) { 1213 assert(Offset != 0); 1214 1215 SavedStreamPosition SavedPosition(Cursor); 1216 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1217 Error(std::move(Err)); 1218 return true; 1219 } 1220 1221 RecordData Record; 1222 StringRef Blob; 1223 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1224 if (!MaybeCode) { 1225 Error(MaybeCode.takeError()); 1226 return true; 1227 } 1228 unsigned Code = MaybeCode.get(); 1229 1230 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1231 if (!MaybeRecCode) { 1232 Error(MaybeRecCode.takeError()); 1233 return true; 1234 } 1235 unsigned RecCode = MaybeRecCode.get(); 1236 if (RecCode != DECL_CONTEXT_VISIBLE) { 1237 Error("Expected visible lookup table block"); 1238 return true; 1239 } 1240 1241 // We can't safely determine the primary context yet, so delay attaching the 1242 // lookup table until we're done with recursive deserialization. 1243 auto *Data = (const unsigned char*)Blob.data(); 1244 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data}); 1245 return false; 1246 } 1247 1248 void ASTReader::Error(StringRef Msg) const { 1249 Error(diag::err_fe_pch_malformed, Msg); 1250 if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() && 1251 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) { 1252 Diag(diag::note_module_cache_path) 1253 << PP.getHeaderSearchInfo().getModuleCachePath(); 1254 } 1255 } 1256 1257 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2, 1258 StringRef Arg3) const { 1259 if (Diags.isDiagnosticInFlight()) 1260 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3); 1261 else 1262 Diag(DiagID) << Arg1 << Arg2 << Arg3; 1263 } 1264 1265 void ASTReader::Error(llvm::Error &&Err) const { 1266 llvm::Error RemainingErr = 1267 handleErrors(std::move(Err), [this](const DiagnosticError &E) { 1268 auto Diag = E.getDiagnostic().second; 1269 1270 // Ideally we'd just emit it, but have to handle a possible in-flight 1271 // diagnostic. Note that the location is currently ignored as well. 1272 auto NumArgs = Diag.getStorage()->NumDiagArgs; 1273 assert(NumArgs <= 3 && "Can only have up to 3 arguments"); 1274 StringRef Arg1, Arg2, Arg3; 1275 switch (NumArgs) { 1276 case 3: 1277 Arg3 = Diag.getStringArg(2); 1278 LLVM_FALLTHROUGH; 1279 case 2: 1280 Arg2 = Diag.getStringArg(1); 1281 LLVM_FALLTHROUGH; 1282 case 1: 1283 Arg1 = Diag.getStringArg(0); 1284 } 1285 Error(Diag.getDiagID(), Arg1, Arg2, Arg3); 1286 }); 1287 if (RemainingErr) 1288 Error(toString(std::move(RemainingErr))); 1289 } 1290 1291 //===----------------------------------------------------------------------===// 1292 // Source Manager Deserialization 1293 //===----------------------------------------------------------------------===// 1294 1295 /// Read the line table in the source manager block. 1296 void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) { 1297 unsigned Idx = 0; 1298 LineTableInfo &LineTable = SourceMgr.getLineTable(); 1299 1300 // Parse the file names 1301 std::map<int, int> FileIDs; 1302 FileIDs[-1] = -1; // For unspecified filenames. 1303 for (unsigned I = 0; Record[Idx]; ++I) { 1304 // Extract the file name 1305 auto Filename = ReadPath(F, Record, Idx); 1306 FileIDs[I] = LineTable.getLineTableFilenameID(Filename); 1307 } 1308 ++Idx; 1309 1310 // Parse the line entries 1311 std::vector<LineEntry> Entries; 1312 while (Idx < Record.size()) { 1313 int FID = Record[Idx++]; 1314 assert(FID >= 0 && "Serialized line entries for non-local file."); 1315 // Remap FileID from 1-based old view. 1316 FID += F.SLocEntryBaseID - 1; 1317 1318 // Extract the line entries 1319 unsigned NumEntries = Record[Idx++]; 1320 assert(NumEntries && "no line entries for file ID"); 1321 Entries.clear(); 1322 Entries.reserve(NumEntries); 1323 for (unsigned I = 0; I != NumEntries; ++I) { 1324 unsigned FileOffset = Record[Idx++]; 1325 unsigned LineNo = Record[Idx++]; 1326 int FilenameID = FileIDs[Record[Idx++]]; 1327 SrcMgr::CharacteristicKind FileKind 1328 = (SrcMgr::CharacteristicKind)Record[Idx++]; 1329 unsigned IncludeOffset = Record[Idx++]; 1330 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, 1331 FileKind, IncludeOffset)); 1332 } 1333 LineTable.AddEntry(FileID::get(FID), Entries); 1334 } 1335 } 1336 1337 /// Read a source manager block 1338 llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) { 1339 using namespace SrcMgr; 1340 1341 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor; 1342 1343 // Set the source-location entry cursor to the current position in 1344 // the stream. This cursor will be used to read the contents of the 1345 // source manager block initially, and then lazily read 1346 // source-location entries as needed. 1347 SLocEntryCursor = F.Stream; 1348 1349 // The stream itself is going to skip over the source manager block. 1350 if (llvm::Error Err = F.Stream.SkipBlock()) 1351 return Err; 1352 1353 // Enter the source manager block. 1354 if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) 1355 return Err; 1356 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo(); 1357 1358 RecordData Record; 1359 while (true) { 1360 Expected<llvm::BitstreamEntry> MaybeE = 1361 SLocEntryCursor.advanceSkippingSubblocks(); 1362 if (!MaybeE) 1363 return MaybeE.takeError(); 1364 llvm::BitstreamEntry E = MaybeE.get(); 1365 1366 switch (E.Kind) { 1367 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1368 case llvm::BitstreamEntry::Error: 1369 return llvm::createStringError(std::errc::illegal_byte_sequence, 1370 "malformed block record in AST file"); 1371 case llvm::BitstreamEntry::EndBlock: 1372 return llvm::Error::success(); 1373 case llvm::BitstreamEntry::Record: 1374 // The interesting case. 1375 break; 1376 } 1377 1378 // Read a record. 1379 Record.clear(); 1380 StringRef Blob; 1381 Expected<unsigned> MaybeRecord = 1382 SLocEntryCursor.readRecord(E.ID, Record, &Blob); 1383 if (!MaybeRecord) 1384 return MaybeRecord.takeError(); 1385 switch (MaybeRecord.get()) { 1386 default: // Default behavior: ignore. 1387 break; 1388 1389 case SM_SLOC_FILE_ENTRY: 1390 case SM_SLOC_BUFFER_ENTRY: 1391 case SM_SLOC_EXPANSION_ENTRY: 1392 // Once we hit one of the source location entries, we're done. 1393 return llvm::Error::success(); 1394 } 1395 } 1396 } 1397 1398 /// If a header file is not found at the path that we expect it to be 1399 /// and the PCH file was moved from its original location, try to resolve the 1400 /// file by assuming that header+PCH were moved together and the header is in 1401 /// the same place relative to the PCH. 1402 static std::string 1403 resolveFileRelativeToOriginalDir(const std::string &Filename, 1404 const std::string &OriginalDir, 1405 const std::string &CurrDir) { 1406 assert(OriginalDir != CurrDir && 1407 "No point trying to resolve the file if the PCH dir didn't change"); 1408 1409 using namespace llvm::sys; 1410 1411 SmallString<128> filePath(Filename); 1412 fs::make_absolute(filePath); 1413 assert(path::is_absolute(OriginalDir)); 1414 SmallString<128> currPCHPath(CurrDir); 1415 1416 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)), 1417 fileDirE = path::end(path::parent_path(filePath)); 1418 path::const_iterator origDirI = path::begin(OriginalDir), 1419 origDirE = path::end(OriginalDir); 1420 // Skip the common path components from filePath and OriginalDir. 1421 while (fileDirI != fileDirE && origDirI != origDirE && 1422 *fileDirI == *origDirI) { 1423 ++fileDirI; 1424 ++origDirI; 1425 } 1426 for (; origDirI != origDirE; ++origDirI) 1427 path::append(currPCHPath, ".."); 1428 path::append(currPCHPath, fileDirI, fileDirE); 1429 path::append(currPCHPath, path::filename(Filename)); 1430 return std::string(currPCHPath.str()); 1431 } 1432 1433 bool ASTReader::ReadSLocEntry(int ID) { 1434 if (ID == 0) 1435 return false; 1436 1437 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1438 Error("source location entry ID out-of-range for AST file"); 1439 return true; 1440 } 1441 1442 // Local helper to read the (possibly-compressed) buffer data following the 1443 // entry record. 1444 auto ReadBuffer = [this]( 1445 BitstreamCursor &SLocEntryCursor, 1446 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> { 1447 RecordData Record; 1448 StringRef Blob; 1449 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode(); 1450 if (!MaybeCode) { 1451 Error(MaybeCode.takeError()); 1452 return nullptr; 1453 } 1454 unsigned Code = MaybeCode.get(); 1455 1456 Expected<unsigned> MaybeRecCode = 1457 SLocEntryCursor.readRecord(Code, Record, &Blob); 1458 if (!MaybeRecCode) { 1459 Error(MaybeRecCode.takeError()); 1460 return nullptr; 1461 } 1462 unsigned RecCode = MaybeRecCode.get(); 1463 1464 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) { 1465 if (!llvm::zlib::isAvailable()) { 1466 Error("zlib is not available"); 1467 return nullptr; 1468 } 1469 SmallString<0> Uncompressed; 1470 if (llvm::Error E = 1471 llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) { 1472 Error("could not decompress embedded file contents: " + 1473 llvm::toString(std::move(E))); 1474 return nullptr; 1475 } 1476 return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name); 1477 } else if (RecCode == SM_SLOC_BUFFER_BLOB) { 1478 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true); 1479 } else { 1480 Error("AST record has invalid code"); 1481 return nullptr; 1482 } 1483 }; 1484 1485 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second; 1486 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit( 1487 F->SLocEntryOffsetsBase + 1488 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) { 1489 Error(std::move(Err)); 1490 return true; 1491 } 1492 1493 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor; 1494 SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset; 1495 1496 ++NumSLocEntriesRead; 1497 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance(); 1498 if (!MaybeEntry) { 1499 Error(MaybeEntry.takeError()); 1500 return true; 1501 } 1502 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1503 1504 if (Entry.Kind != llvm::BitstreamEntry::Record) { 1505 Error("incorrectly-formatted source location entry in AST file"); 1506 return true; 1507 } 1508 1509 RecordData Record; 1510 StringRef Blob; 1511 Expected<unsigned> MaybeSLOC = 1512 SLocEntryCursor.readRecord(Entry.ID, Record, &Blob); 1513 if (!MaybeSLOC) { 1514 Error(MaybeSLOC.takeError()); 1515 return true; 1516 } 1517 switch (MaybeSLOC.get()) { 1518 default: 1519 Error("incorrectly-formatted source location entry in AST file"); 1520 return true; 1521 1522 case SM_SLOC_FILE_ENTRY: { 1523 // We will detect whether a file changed and return 'Failure' for it, but 1524 // we will also try to fail gracefully by setting up the SLocEntry. 1525 unsigned InputID = Record[4]; 1526 InputFile IF = getInputFile(*F, InputID); 1527 Optional<FileEntryRef> File = IF.getFile(); 1528 bool OverriddenBuffer = IF.isOverridden(); 1529 1530 // Note that we only check if a File was returned. If it was out-of-date 1531 // we have complained but we will continue creating a FileID to recover 1532 // gracefully. 1533 if (!File) 1534 return true; 1535 1536 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1537 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) { 1538 // This is the module's main file. 1539 IncludeLoc = getImportLocation(F); 1540 } 1541 SrcMgr::CharacteristicKind 1542 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1543 FileID FID = SourceMgr.createFileID(*File, IncludeLoc, FileCharacter, ID, 1544 BaseOffset + Record[0]); 1545 SrcMgr::FileInfo &FileInfo = 1546 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()); 1547 FileInfo.NumCreatedFIDs = Record[5]; 1548 if (Record[3]) 1549 FileInfo.setHasLineDirectives(); 1550 1551 unsigned NumFileDecls = Record[7]; 1552 if (NumFileDecls && ContextObj) { 1553 const DeclID *FirstDecl = F->FileSortedDecls + Record[6]; 1554 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?"); 1555 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl, 1556 NumFileDecls)); 1557 } 1558 1559 const SrcMgr::ContentCache &ContentCache = 1560 SourceMgr.getOrCreateContentCache(*File, isSystem(FileCharacter)); 1561 if (OverriddenBuffer && !ContentCache.BufferOverridden && 1562 ContentCache.ContentsEntry == ContentCache.OrigEntry && 1563 !ContentCache.getBufferIfLoaded()) { 1564 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName()); 1565 if (!Buffer) 1566 return true; 1567 SourceMgr.overrideFileContents(*File, std::move(Buffer)); 1568 } 1569 1570 break; 1571 } 1572 1573 case SM_SLOC_BUFFER_ENTRY: { 1574 const char *Name = Blob.data(); 1575 unsigned Offset = Record[0]; 1576 SrcMgr::CharacteristicKind 1577 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1578 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1579 if (IncludeLoc.isInvalid() && F->isModule()) { 1580 IncludeLoc = getImportLocation(F); 1581 } 1582 1583 auto Buffer = ReadBuffer(SLocEntryCursor, Name); 1584 if (!Buffer) 1585 return true; 1586 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID, 1587 BaseOffset + Offset, IncludeLoc); 1588 break; 1589 } 1590 1591 case SM_SLOC_EXPANSION_ENTRY: { 1592 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]); 1593 SourceMgr.createExpansionLoc(SpellingLoc, 1594 ReadSourceLocation(*F, Record[2]), 1595 ReadSourceLocation(*F, Record[3]), 1596 Record[5], 1597 Record[4], 1598 ID, 1599 BaseOffset + Record[0]); 1600 break; 1601 } 1602 } 1603 1604 return false; 1605 } 1606 1607 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) { 1608 if (ID == 0) 1609 return std::make_pair(SourceLocation(), ""); 1610 1611 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1612 Error("source location entry ID out-of-range for AST file"); 1613 return std::make_pair(SourceLocation(), ""); 1614 } 1615 1616 // Find which module file this entry lands in. 1617 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second; 1618 if (!M->isModule()) 1619 return std::make_pair(SourceLocation(), ""); 1620 1621 // FIXME: Can we map this down to a particular submodule? That would be 1622 // ideal. 1623 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName)); 1624 } 1625 1626 /// Find the location where the module F is imported. 1627 SourceLocation ASTReader::getImportLocation(ModuleFile *F) { 1628 if (F->ImportLoc.isValid()) 1629 return F->ImportLoc; 1630 1631 // Otherwise we have a PCH. It's considered to be "imported" at the first 1632 // location of its includer. 1633 if (F->ImportedBy.empty() || !F->ImportedBy[0]) { 1634 // Main file is the importer. 1635 assert(SourceMgr.getMainFileID().isValid() && "missing main file"); 1636 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); 1637 } 1638 return F->ImportedBy[0]->FirstLoc; 1639 } 1640 1641 /// Enter a subblock of the specified BlockID with the specified cursor. Read 1642 /// the abbreviations that are at the top of the block and then leave the cursor 1643 /// pointing into the block. 1644 llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, 1645 unsigned BlockID, 1646 uint64_t *StartOfBlockOffset) { 1647 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) 1648 return Err; 1649 1650 if (StartOfBlockOffset) 1651 *StartOfBlockOffset = Cursor.GetCurrentBitNo(); 1652 1653 while (true) { 1654 uint64_t Offset = Cursor.GetCurrentBitNo(); 1655 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1656 if (!MaybeCode) 1657 return MaybeCode.takeError(); 1658 unsigned Code = MaybeCode.get(); 1659 1660 // We expect all abbrevs to be at the start of the block. 1661 if (Code != llvm::bitc::DEFINE_ABBREV) { 1662 if (llvm::Error Err = Cursor.JumpToBit(Offset)) 1663 return Err; 1664 return llvm::Error::success(); 1665 } 1666 if (llvm::Error Err = Cursor.ReadAbbrevRecord()) 1667 return Err; 1668 } 1669 } 1670 1671 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record, 1672 unsigned &Idx) { 1673 Token Tok; 1674 Tok.startToken(); 1675 Tok.setLocation(ReadSourceLocation(F, Record, Idx)); 1676 Tok.setLength(Record[Idx++]); 1677 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++])) 1678 Tok.setIdentifierInfo(II); 1679 Tok.setKind((tok::TokenKind)Record[Idx++]); 1680 Tok.setFlag((Token::TokenFlags)Record[Idx++]); 1681 return Tok; 1682 } 1683 1684 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) { 1685 BitstreamCursor &Stream = F.MacroCursor; 1686 1687 // Keep track of where we are in the stream, then jump back there 1688 // after reading this macro. 1689 SavedStreamPosition SavedPosition(Stream); 1690 1691 if (llvm::Error Err = Stream.JumpToBit(Offset)) { 1692 // FIXME this drops errors on the floor. 1693 consumeError(std::move(Err)); 1694 return nullptr; 1695 } 1696 RecordData Record; 1697 SmallVector<IdentifierInfo*, 16> MacroParams; 1698 MacroInfo *Macro = nullptr; 1699 llvm::MutableArrayRef<Token> MacroTokens; 1700 1701 while (true) { 1702 // Advance to the next record, but if we get to the end of the block, don't 1703 // pop it (removing all the abbreviations from the cursor) since we want to 1704 // be able to reseek within the block and read entries. 1705 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd; 1706 Expected<llvm::BitstreamEntry> MaybeEntry = 1707 Stream.advanceSkippingSubblocks(Flags); 1708 if (!MaybeEntry) { 1709 Error(MaybeEntry.takeError()); 1710 return Macro; 1711 } 1712 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1713 1714 switch (Entry.Kind) { 1715 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1716 case llvm::BitstreamEntry::Error: 1717 Error("malformed block record in AST file"); 1718 return Macro; 1719 case llvm::BitstreamEntry::EndBlock: 1720 return Macro; 1721 case llvm::BitstreamEntry::Record: 1722 // The interesting case. 1723 break; 1724 } 1725 1726 // Read a record. 1727 Record.clear(); 1728 PreprocessorRecordTypes RecType; 1729 if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record)) 1730 RecType = (PreprocessorRecordTypes)MaybeRecType.get(); 1731 else { 1732 Error(MaybeRecType.takeError()); 1733 return Macro; 1734 } 1735 switch (RecType) { 1736 case PP_MODULE_MACRO: 1737 case PP_MACRO_DIRECTIVE_HISTORY: 1738 return Macro; 1739 1740 case PP_MACRO_OBJECT_LIKE: 1741 case PP_MACRO_FUNCTION_LIKE: { 1742 // If we already have a macro, that means that we've hit the end 1743 // of the definition of the macro we were looking for. We're 1744 // done. 1745 if (Macro) 1746 return Macro; 1747 1748 unsigned NextIndex = 1; // Skip identifier ID. 1749 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex); 1750 MacroInfo *MI = PP.AllocateMacroInfo(Loc); 1751 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex)); 1752 MI->setIsUsed(Record[NextIndex++]); 1753 MI->setUsedForHeaderGuard(Record[NextIndex++]); 1754 MacroTokens = MI->allocateTokens(Record[NextIndex++], 1755 PP.getPreprocessorAllocator()); 1756 if (RecType == PP_MACRO_FUNCTION_LIKE) { 1757 // Decode function-like macro info. 1758 bool isC99VarArgs = Record[NextIndex++]; 1759 bool isGNUVarArgs = Record[NextIndex++]; 1760 bool hasCommaPasting = Record[NextIndex++]; 1761 MacroParams.clear(); 1762 unsigned NumArgs = Record[NextIndex++]; 1763 for (unsigned i = 0; i != NumArgs; ++i) 1764 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++])); 1765 1766 // Install function-like macro info. 1767 MI->setIsFunctionLike(); 1768 if (isC99VarArgs) MI->setIsC99Varargs(); 1769 if (isGNUVarArgs) MI->setIsGNUVarargs(); 1770 if (hasCommaPasting) MI->setHasCommaPasting(); 1771 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator()); 1772 } 1773 1774 // Remember that we saw this macro last so that we add the tokens that 1775 // form its body to it. 1776 Macro = MI; 1777 1778 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() && 1779 Record[NextIndex]) { 1780 // We have a macro definition. Register the association 1781 PreprocessedEntityID 1782 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]); 1783 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 1784 PreprocessingRecord::PPEntityID PPID = 1785 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true); 1786 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>( 1787 PPRec.getPreprocessedEntity(PPID)); 1788 if (PPDef) 1789 PPRec.RegisterMacroDefinition(Macro, PPDef); 1790 } 1791 1792 ++NumMacrosRead; 1793 break; 1794 } 1795 1796 case PP_TOKEN: { 1797 // If we see a TOKEN before a PP_MACRO_*, then the file is 1798 // erroneous, just pretend we didn't see this. 1799 if (!Macro) break; 1800 if (MacroTokens.empty()) { 1801 Error("unexpected number of macro tokens for a macro in AST file"); 1802 return Macro; 1803 } 1804 1805 unsigned Idx = 0; 1806 MacroTokens[0] = ReadToken(F, Record, Idx); 1807 MacroTokens = MacroTokens.drop_front(); 1808 break; 1809 } 1810 } 1811 } 1812 } 1813 1814 PreprocessedEntityID 1815 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, 1816 unsigned LocalID) const { 1817 if (!M.ModuleOffsetMap.empty()) 1818 ReadModuleOffsetMap(M); 1819 1820 ContinuousRangeMap<uint32_t, int, 2>::const_iterator 1821 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS); 1822 assert(I != M.PreprocessedEntityRemap.end() 1823 && "Invalid index into preprocessed entity index remap"); 1824 1825 return LocalID + I->second; 1826 } 1827 1828 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) { 1829 return llvm::hash_combine(ikey.Size, ikey.ModTime); 1830 } 1831 1832 HeaderFileInfoTrait::internal_key_type 1833 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) { 1834 internal_key_type ikey = {FE->getSize(), 1835 M.HasTimestamps ? FE->getModificationTime() : 0, 1836 FE->getName(), /*Imported*/ false}; 1837 return ikey; 1838 } 1839 1840 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) { 1841 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime)) 1842 return false; 1843 1844 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename) 1845 return true; 1846 1847 // Determine whether the actual files are equivalent. 1848 FileManager &FileMgr = Reader.getFileManager(); 1849 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* { 1850 if (!Key.Imported) { 1851 if (auto File = FileMgr.getFile(Key.Filename)) 1852 return *File; 1853 return nullptr; 1854 } 1855 1856 std::string Resolved = std::string(Key.Filename); 1857 Reader.ResolveImportedPath(M, Resolved); 1858 if (auto File = FileMgr.getFile(Resolved)) 1859 return *File; 1860 return nullptr; 1861 }; 1862 1863 const FileEntry *FEA = GetFile(a); 1864 const FileEntry *FEB = GetFile(b); 1865 return FEA && FEA == FEB; 1866 } 1867 1868 std::pair<unsigned, unsigned> 1869 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) { 1870 return readULEBKeyDataLength(d); 1871 } 1872 1873 HeaderFileInfoTrait::internal_key_type 1874 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) { 1875 using namespace llvm::support; 1876 1877 internal_key_type ikey; 1878 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d)); 1879 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d)); 1880 ikey.Filename = (const char *)d; 1881 ikey.Imported = true; 1882 return ikey; 1883 } 1884 1885 HeaderFileInfoTrait::data_type 1886 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d, 1887 unsigned DataLen) { 1888 using namespace llvm::support; 1889 1890 const unsigned char *End = d + DataLen; 1891 HeaderFileInfo HFI; 1892 unsigned Flags = *d++; 1893 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp. 1894 HFI.isImport |= (Flags >> 5) & 0x01; 1895 HFI.isPragmaOnce |= (Flags >> 4) & 0x01; 1896 HFI.DirInfo = (Flags >> 1) & 0x07; 1897 HFI.IndexHeaderMapHeader = Flags & 0x01; 1898 HFI.ControllingMacroID = Reader.getGlobalIdentifierID( 1899 M, endian::readNext<uint32_t, little, unaligned>(d)); 1900 if (unsigned FrameworkOffset = 1901 endian::readNext<uint32_t, little, unaligned>(d)) { 1902 // The framework offset is 1 greater than the actual offset, 1903 // since 0 is used as an indicator for "no framework name". 1904 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1); 1905 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName); 1906 } 1907 1908 assert((End - d) % 4 == 0 && 1909 "Wrong data length in HeaderFileInfo deserialization"); 1910 while (d != End) { 1911 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d); 1912 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3); 1913 LocalSMID >>= 2; 1914 1915 // This header is part of a module. Associate it with the module to enable 1916 // implicit module import. 1917 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID); 1918 Module *Mod = Reader.getSubmodule(GlobalSMID); 1919 FileManager &FileMgr = Reader.getFileManager(); 1920 ModuleMap &ModMap = 1921 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap(); 1922 1923 std::string Filename = std::string(key.Filename); 1924 if (key.Imported) 1925 Reader.ResolveImportedPath(M, Filename); 1926 // FIXME: NameAsWritten 1927 Module::Header H = {std::string(key.Filename), "", 1928 *FileMgr.getFile(Filename)}; 1929 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true); 1930 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader); 1931 } 1932 1933 // This HeaderFileInfo was externally loaded. 1934 HFI.External = true; 1935 HFI.IsValid = true; 1936 return HFI; 1937 } 1938 1939 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M, 1940 uint32_t MacroDirectivesOffset) { 1941 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard"); 1942 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset)); 1943 } 1944 1945 void ASTReader::ReadDefinedMacros() { 1946 // Note that we are loading defined macros. 1947 Deserializing Macros(this); 1948 1949 for (ModuleFile &I : llvm::reverse(ModuleMgr)) { 1950 BitstreamCursor &MacroCursor = I.MacroCursor; 1951 1952 // If there was no preprocessor block, skip this file. 1953 if (MacroCursor.getBitcodeBytes().empty()) 1954 continue; 1955 1956 BitstreamCursor Cursor = MacroCursor; 1957 if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) { 1958 Error(std::move(Err)); 1959 return; 1960 } 1961 1962 RecordData Record; 1963 while (true) { 1964 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks(); 1965 if (!MaybeE) { 1966 Error(MaybeE.takeError()); 1967 return; 1968 } 1969 llvm::BitstreamEntry E = MaybeE.get(); 1970 1971 switch (E.Kind) { 1972 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1973 case llvm::BitstreamEntry::Error: 1974 Error("malformed block record in AST file"); 1975 return; 1976 case llvm::BitstreamEntry::EndBlock: 1977 goto NextCursor; 1978 1979 case llvm::BitstreamEntry::Record: { 1980 Record.clear(); 1981 Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record); 1982 if (!MaybeRecord) { 1983 Error(MaybeRecord.takeError()); 1984 return; 1985 } 1986 switch (MaybeRecord.get()) { 1987 default: // Default behavior: ignore. 1988 break; 1989 1990 case PP_MACRO_OBJECT_LIKE: 1991 case PP_MACRO_FUNCTION_LIKE: { 1992 IdentifierInfo *II = getLocalIdentifier(I, Record[0]); 1993 if (II->isOutOfDate()) 1994 updateOutOfDateIdentifier(*II); 1995 break; 1996 } 1997 1998 case PP_TOKEN: 1999 // Ignore tokens. 2000 break; 2001 } 2002 break; 2003 } 2004 } 2005 } 2006 NextCursor: ; 2007 } 2008 } 2009 2010 namespace { 2011 2012 /// Visitor class used to look up identifirs in an AST file. 2013 class IdentifierLookupVisitor { 2014 StringRef Name; 2015 unsigned NameHash; 2016 unsigned PriorGeneration; 2017 unsigned &NumIdentifierLookups; 2018 unsigned &NumIdentifierLookupHits; 2019 IdentifierInfo *Found = nullptr; 2020 2021 public: 2022 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration, 2023 unsigned &NumIdentifierLookups, 2024 unsigned &NumIdentifierLookupHits) 2025 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)), 2026 PriorGeneration(PriorGeneration), 2027 NumIdentifierLookups(NumIdentifierLookups), 2028 NumIdentifierLookupHits(NumIdentifierLookupHits) {} 2029 2030 bool operator()(ModuleFile &M) { 2031 // If we've already searched this module file, skip it now. 2032 if (M.Generation <= PriorGeneration) 2033 return true; 2034 2035 ASTIdentifierLookupTable *IdTable 2036 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable; 2037 if (!IdTable) 2038 return false; 2039 2040 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M, 2041 Found); 2042 ++NumIdentifierLookups; 2043 ASTIdentifierLookupTable::iterator Pos = 2044 IdTable->find_hashed(Name, NameHash, &Trait); 2045 if (Pos == IdTable->end()) 2046 return false; 2047 2048 // Dereferencing the iterator has the effect of building the 2049 // IdentifierInfo node and populating it with the various 2050 // declarations it needs. 2051 ++NumIdentifierLookupHits; 2052 Found = *Pos; 2053 return true; 2054 } 2055 2056 // Retrieve the identifier info found within the module 2057 // files. 2058 IdentifierInfo *getIdentifierInfo() const { return Found; } 2059 }; 2060 2061 } // namespace 2062 2063 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) { 2064 // Note that we are loading an identifier. 2065 Deserializing AnIdentifier(this); 2066 2067 unsigned PriorGeneration = 0; 2068 if (getContext().getLangOpts().Modules) 2069 PriorGeneration = IdentifierGeneration[&II]; 2070 2071 // If there is a global index, look there first to determine which modules 2072 // provably do not have any results for this identifier. 2073 GlobalModuleIndex::HitSet Hits; 2074 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 2075 if (!loadGlobalIndex()) { 2076 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) { 2077 HitsPtr = &Hits; 2078 } 2079 } 2080 2081 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration, 2082 NumIdentifierLookups, 2083 NumIdentifierLookupHits); 2084 ModuleMgr.visit(Visitor, HitsPtr); 2085 markIdentifierUpToDate(&II); 2086 } 2087 2088 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) { 2089 if (!II) 2090 return; 2091 2092 II->setOutOfDate(false); 2093 2094 // Update the generation for this identifier. 2095 if (getContext().getLangOpts().Modules) 2096 IdentifierGeneration[II] = getGeneration(); 2097 } 2098 2099 void ASTReader::resolvePendingMacro(IdentifierInfo *II, 2100 const PendingMacroInfo &PMInfo) { 2101 ModuleFile &M = *PMInfo.M; 2102 2103 BitstreamCursor &Cursor = M.MacroCursor; 2104 SavedStreamPosition SavedPosition(Cursor); 2105 if (llvm::Error Err = 2106 Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) { 2107 Error(std::move(Err)); 2108 return; 2109 } 2110 2111 struct ModuleMacroRecord { 2112 SubmoduleID SubModID; 2113 MacroInfo *MI; 2114 SmallVector<SubmoduleID, 8> Overrides; 2115 }; 2116 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros; 2117 2118 // We expect to see a sequence of PP_MODULE_MACRO records listing exported 2119 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete 2120 // macro histroy. 2121 RecordData Record; 2122 while (true) { 2123 Expected<llvm::BitstreamEntry> MaybeEntry = 2124 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 2125 if (!MaybeEntry) { 2126 Error(MaybeEntry.takeError()); 2127 return; 2128 } 2129 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2130 2131 if (Entry.Kind != llvm::BitstreamEntry::Record) { 2132 Error("malformed block record in AST file"); 2133 return; 2134 } 2135 2136 Record.clear(); 2137 Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record); 2138 if (!MaybePP) { 2139 Error(MaybePP.takeError()); 2140 return; 2141 } 2142 switch ((PreprocessorRecordTypes)MaybePP.get()) { 2143 case PP_MACRO_DIRECTIVE_HISTORY: 2144 break; 2145 2146 case PP_MODULE_MACRO: { 2147 ModuleMacros.push_back(ModuleMacroRecord()); 2148 auto &Info = ModuleMacros.back(); 2149 Info.SubModID = getGlobalSubmoduleID(M, Record[0]); 2150 Info.MI = getMacro(getGlobalMacroID(M, Record[1])); 2151 for (int I = 2, N = Record.size(); I != N; ++I) 2152 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I])); 2153 continue; 2154 } 2155 2156 default: 2157 Error("malformed block record in AST file"); 2158 return; 2159 } 2160 2161 // We found the macro directive history; that's the last record 2162 // for this macro. 2163 break; 2164 } 2165 2166 // Module macros are listed in reverse dependency order. 2167 { 2168 std::reverse(ModuleMacros.begin(), ModuleMacros.end()); 2169 llvm::SmallVector<ModuleMacro*, 8> Overrides; 2170 for (auto &MMR : ModuleMacros) { 2171 Overrides.clear(); 2172 for (unsigned ModID : MMR.Overrides) { 2173 Module *Mod = getSubmodule(ModID); 2174 auto *Macro = PP.getModuleMacro(Mod, II); 2175 assert(Macro && "missing definition for overridden macro"); 2176 Overrides.push_back(Macro); 2177 } 2178 2179 bool Inserted = false; 2180 Module *Owner = getSubmodule(MMR.SubModID); 2181 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted); 2182 } 2183 } 2184 2185 // Don't read the directive history for a module; we don't have anywhere 2186 // to put it. 2187 if (M.isModule()) 2188 return; 2189 2190 // Deserialize the macro directives history in reverse source-order. 2191 MacroDirective *Latest = nullptr, *Earliest = nullptr; 2192 unsigned Idx = 0, N = Record.size(); 2193 while (Idx < N) { 2194 MacroDirective *MD = nullptr; 2195 SourceLocation Loc = ReadSourceLocation(M, Record, Idx); 2196 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++]; 2197 switch (K) { 2198 case MacroDirective::MD_Define: { 2199 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++])); 2200 MD = PP.AllocateDefMacroDirective(MI, Loc); 2201 break; 2202 } 2203 case MacroDirective::MD_Undefine: 2204 MD = PP.AllocateUndefMacroDirective(Loc); 2205 break; 2206 case MacroDirective::MD_Visibility: 2207 bool isPublic = Record[Idx++]; 2208 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic); 2209 break; 2210 } 2211 2212 if (!Latest) 2213 Latest = MD; 2214 if (Earliest) 2215 Earliest->setPrevious(MD); 2216 Earliest = MD; 2217 } 2218 2219 if (Latest) 2220 PP.setLoadedMacroDirective(II, Earliest, Latest); 2221 } 2222 2223 bool ASTReader::shouldDisableValidationForFile( 2224 const serialization::ModuleFile &M) const { 2225 if (DisableValidationKind == DisableValidationForModuleKind::None) 2226 return false; 2227 2228 // If a PCH is loaded and validation is disabled for PCH then disable 2229 // validation for the PCH and the modules it loads. 2230 ModuleKind K = CurrentDeserializingModuleKind.getValueOr(M.Kind); 2231 2232 switch (K) { 2233 case MK_MainFile: 2234 case MK_Preamble: 2235 case MK_PCH: 2236 return bool(DisableValidationKind & DisableValidationForModuleKind::PCH); 2237 case MK_ImplicitModule: 2238 case MK_ExplicitModule: 2239 case MK_PrebuiltModule: 2240 return bool(DisableValidationKind & DisableValidationForModuleKind::Module); 2241 } 2242 2243 return false; 2244 } 2245 2246 ASTReader::InputFileInfo 2247 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) { 2248 // Go find this input file. 2249 BitstreamCursor &Cursor = F.InputFilesCursor; 2250 SavedStreamPosition SavedPosition(Cursor); 2251 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2252 // FIXME this drops errors on the floor. 2253 consumeError(std::move(Err)); 2254 } 2255 2256 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 2257 if (!MaybeCode) { 2258 // FIXME this drops errors on the floor. 2259 consumeError(MaybeCode.takeError()); 2260 } 2261 unsigned Code = MaybeCode.get(); 2262 RecordData Record; 2263 StringRef Blob; 2264 2265 if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob)) 2266 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE && 2267 "invalid record type for input file"); 2268 else { 2269 // FIXME this drops errors on the floor. 2270 consumeError(Maybe.takeError()); 2271 } 2272 2273 assert(Record[0] == ID && "Bogus stored ID or offset"); 2274 InputFileInfo R; 2275 R.StoredSize = static_cast<off_t>(Record[1]); 2276 R.StoredTime = static_cast<time_t>(Record[2]); 2277 R.Overridden = static_cast<bool>(Record[3]); 2278 R.Transient = static_cast<bool>(Record[4]); 2279 R.TopLevelModuleMap = static_cast<bool>(Record[5]); 2280 R.Filename = std::string(Blob); 2281 ResolveImportedPath(F, R.Filename); 2282 2283 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 2284 if (!MaybeEntry) // FIXME this drops errors on the floor. 2285 consumeError(MaybeEntry.takeError()); 2286 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2287 assert(Entry.Kind == llvm::BitstreamEntry::Record && 2288 "expected record type for input file hash"); 2289 2290 Record.clear(); 2291 if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record)) 2292 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH && 2293 "invalid record type for input file hash"); 2294 else { 2295 // FIXME this drops errors on the floor. 2296 consumeError(Maybe.takeError()); 2297 } 2298 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) | 2299 static_cast<uint64_t>(Record[0]); 2300 return R; 2301 } 2302 2303 static unsigned moduleKindForDiagnostic(ModuleKind Kind); 2304 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) { 2305 // If this ID is bogus, just return an empty input file. 2306 if (ID == 0 || ID > F.InputFilesLoaded.size()) 2307 return InputFile(); 2308 2309 // If we've already loaded this input file, return it. 2310 if (F.InputFilesLoaded[ID-1].getFile()) 2311 return F.InputFilesLoaded[ID-1]; 2312 2313 if (F.InputFilesLoaded[ID-1].isNotFound()) 2314 return InputFile(); 2315 2316 // Go find this input file. 2317 BitstreamCursor &Cursor = F.InputFilesCursor; 2318 SavedStreamPosition SavedPosition(Cursor); 2319 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2320 // FIXME this drops errors on the floor. 2321 consumeError(std::move(Err)); 2322 } 2323 2324 InputFileInfo FI = readInputFileInfo(F, ID); 2325 off_t StoredSize = FI.StoredSize; 2326 time_t StoredTime = FI.StoredTime; 2327 bool Overridden = FI.Overridden; 2328 bool Transient = FI.Transient; 2329 StringRef Filename = FI.Filename; 2330 uint64_t StoredContentHash = FI.ContentHash; 2331 2332 OptionalFileEntryRefDegradesToFileEntryPtr File = 2333 expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false)); 2334 2335 // If we didn't find the file, resolve it relative to the 2336 // original directory from which this AST file was created. 2337 if (!File && !F.OriginalDir.empty() && !F.BaseDirectory.empty() && 2338 F.OriginalDir != F.BaseDirectory) { 2339 std::string Resolved = resolveFileRelativeToOriginalDir( 2340 std::string(Filename), F.OriginalDir, F.BaseDirectory); 2341 if (!Resolved.empty()) 2342 File = expectedToOptional(FileMgr.getFileRef(Resolved)); 2343 } 2344 2345 // For an overridden file, create a virtual file with the stored 2346 // size/timestamp. 2347 if ((Overridden || Transient) && !File) 2348 File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime); 2349 2350 if (!File) { 2351 if (Complain) { 2352 std::string ErrorStr = "could not find file '"; 2353 ErrorStr += Filename; 2354 ErrorStr += "' referenced by AST file '"; 2355 ErrorStr += F.FileName; 2356 ErrorStr += "'"; 2357 Error(ErrorStr); 2358 } 2359 // Record that we didn't find the file. 2360 F.InputFilesLoaded[ID-1] = InputFile::getNotFound(); 2361 return InputFile(); 2362 } 2363 2364 // Check if there was a request to override the contents of the file 2365 // that was part of the precompiled header. Overriding such a file 2366 // can lead to problems when lexing using the source locations from the 2367 // PCH. 2368 SourceManager &SM = getSourceManager(); 2369 // FIXME: Reject if the overrides are different. 2370 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) { 2371 if (Complain) 2372 Error(diag::err_fe_pch_file_overridden, Filename); 2373 2374 // After emitting the diagnostic, bypass the overriding file to recover 2375 // (this creates a separate FileEntry). 2376 File = SM.bypassFileContentsOverride(*File); 2377 if (!File) { 2378 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound(); 2379 return InputFile(); 2380 } 2381 } 2382 2383 struct Change { 2384 enum ModificationKind { 2385 Size, 2386 ModTime, 2387 Content, 2388 None, 2389 } Kind; 2390 llvm::Optional<int64_t> Old = llvm::None; 2391 llvm::Optional<int64_t> New = llvm::None; 2392 }; 2393 auto HasInputFileChanged = [&]() { 2394 if (StoredSize != File->getSize()) 2395 return Change{Change::Size, StoredSize, File->getSize()}; 2396 if (!shouldDisableValidationForFile(F) && StoredTime && 2397 StoredTime != File->getModificationTime()) { 2398 Change MTimeChange = {Change::ModTime, StoredTime, 2399 File->getModificationTime()}; 2400 2401 // In case the modification time changes but not the content, 2402 // accept the cached file as legit. 2403 if (ValidateASTInputFilesContent && 2404 StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) { 2405 auto MemBuffOrError = FileMgr.getBufferForFile(File); 2406 if (!MemBuffOrError) { 2407 if (!Complain) 2408 return MTimeChange; 2409 std::string ErrorStr = "could not get buffer for file '"; 2410 ErrorStr += File->getName(); 2411 ErrorStr += "'"; 2412 Error(ErrorStr); 2413 return MTimeChange; 2414 } 2415 2416 // FIXME: hash_value is not guaranteed to be stable! 2417 auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer()); 2418 if (StoredContentHash == static_cast<uint64_t>(ContentHash)) 2419 return Change{Change::None}; 2420 2421 return Change{Change::Content}; 2422 } 2423 return MTimeChange; 2424 } 2425 return Change{Change::None}; 2426 }; 2427 2428 bool IsOutOfDate = false; 2429 auto FileChange = HasInputFileChanged(); 2430 // For an overridden file, there is nothing to validate. 2431 if (!Overridden && FileChange.Kind != Change::None) { 2432 if (Complain && !Diags.isDiagnosticInFlight()) { 2433 // Build a list of the PCH imports that got us here (in reverse). 2434 SmallVector<ModuleFile *, 4> ImportStack(1, &F); 2435 while (!ImportStack.back()->ImportedBy.empty()) 2436 ImportStack.push_back(ImportStack.back()->ImportedBy[0]); 2437 2438 // The top-level PCH is stale. 2439 StringRef TopLevelPCHName(ImportStack.back()->FileName); 2440 Diag(diag::err_fe_ast_file_modified) 2441 << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind) 2442 << TopLevelPCHName << FileChange.Kind 2443 << (FileChange.Old && FileChange.New) 2444 << llvm::itostr(FileChange.Old.getValueOr(0)) 2445 << llvm::itostr(FileChange.New.getValueOr(0)); 2446 2447 // Print the import stack. 2448 if (ImportStack.size() > 1) { 2449 Diag(diag::note_pch_required_by) 2450 << Filename << ImportStack[0]->FileName; 2451 for (unsigned I = 1; I < ImportStack.size(); ++I) 2452 Diag(diag::note_pch_required_by) 2453 << ImportStack[I-1]->FileName << ImportStack[I]->FileName; 2454 } 2455 2456 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName; 2457 } 2458 2459 IsOutOfDate = true; 2460 } 2461 // FIXME: If the file is overridden and we've already opened it, 2462 // issue an error (or split it into a separate FileEntry). 2463 2464 InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate); 2465 2466 // Note that we've loaded this input file. 2467 F.InputFilesLoaded[ID-1] = IF; 2468 return IF; 2469 } 2470 2471 /// If we are loading a relocatable PCH or module file, and the filename 2472 /// is not an absolute path, add the system or module root to the beginning of 2473 /// the file name. 2474 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) { 2475 // Resolve relative to the base directory, if we have one. 2476 if (!M.BaseDirectory.empty()) 2477 return ResolveImportedPath(Filename, M.BaseDirectory); 2478 } 2479 2480 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) { 2481 if (Filename.empty() || llvm::sys::path::is_absolute(Filename)) 2482 return; 2483 2484 SmallString<128> Buffer; 2485 llvm::sys::path::append(Buffer, Prefix, Filename); 2486 Filename.assign(Buffer.begin(), Buffer.end()); 2487 } 2488 2489 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) { 2490 switch (ARR) { 2491 case ASTReader::Failure: return true; 2492 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing); 2493 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate); 2494 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch); 2495 case ASTReader::ConfigurationMismatch: 2496 return !(Caps & ASTReader::ARR_ConfigurationMismatch); 2497 case ASTReader::HadErrors: return true; 2498 case ASTReader::Success: return false; 2499 } 2500 2501 llvm_unreachable("unknown ASTReadResult"); 2502 } 2503 2504 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock( 2505 BitstreamCursor &Stream, unsigned ClientLoadCapabilities, 2506 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener, 2507 std::string &SuggestedPredefines) { 2508 if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) { 2509 // FIXME this drops errors on the floor. 2510 consumeError(std::move(Err)); 2511 return Failure; 2512 } 2513 2514 // Read all of the records in the options block. 2515 RecordData Record; 2516 ASTReadResult Result = Success; 2517 while (true) { 2518 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2519 if (!MaybeEntry) { 2520 // FIXME this drops errors on the floor. 2521 consumeError(MaybeEntry.takeError()); 2522 return Failure; 2523 } 2524 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2525 2526 switch (Entry.Kind) { 2527 case llvm::BitstreamEntry::Error: 2528 case llvm::BitstreamEntry::SubBlock: 2529 return Failure; 2530 2531 case llvm::BitstreamEntry::EndBlock: 2532 return Result; 2533 2534 case llvm::BitstreamEntry::Record: 2535 // The interesting case. 2536 break; 2537 } 2538 2539 // Read and process a record. 2540 Record.clear(); 2541 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record); 2542 if (!MaybeRecordType) { 2543 // FIXME this drops errors on the floor. 2544 consumeError(MaybeRecordType.takeError()); 2545 return Failure; 2546 } 2547 switch ((OptionsRecordTypes)MaybeRecordType.get()) { 2548 case LANGUAGE_OPTIONS: { 2549 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2550 if (ParseLanguageOptions(Record, Complain, Listener, 2551 AllowCompatibleConfigurationMismatch)) 2552 Result = ConfigurationMismatch; 2553 break; 2554 } 2555 2556 case TARGET_OPTIONS: { 2557 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2558 if (ParseTargetOptions(Record, Complain, Listener, 2559 AllowCompatibleConfigurationMismatch)) 2560 Result = ConfigurationMismatch; 2561 break; 2562 } 2563 2564 case FILE_SYSTEM_OPTIONS: { 2565 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2566 if (!AllowCompatibleConfigurationMismatch && 2567 ParseFileSystemOptions(Record, Complain, Listener)) 2568 Result = ConfigurationMismatch; 2569 break; 2570 } 2571 2572 case HEADER_SEARCH_OPTIONS: { 2573 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2574 if (!AllowCompatibleConfigurationMismatch && 2575 ParseHeaderSearchOptions(Record, Complain, Listener)) 2576 Result = ConfigurationMismatch; 2577 break; 2578 } 2579 2580 case PREPROCESSOR_OPTIONS: 2581 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2582 if (!AllowCompatibleConfigurationMismatch && 2583 ParsePreprocessorOptions(Record, Complain, Listener, 2584 SuggestedPredefines)) 2585 Result = ConfigurationMismatch; 2586 break; 2587 } 2588 } 2589 } 2590 2591 ASTReader::ASTReadResult 2592 ASTReader::ReadControlBlock(ModuleFile &F, 2593 SmallVectorImpl<ImportedModule> &Loaded, 2594 const ModuleFile *ImportedBy, 2595 unsigned ClientLoadCapabilities) { 2596 BitstreamCursor &Stream = F.Stream; 2597 2598 if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) { 2599 Error(std::move(Err)); 2600 return Failure; 2601 } 2602 2603 // Lambda to read the unhashed control block the first time it's called. 2604 // 2605 // For PCM files, the unhashed control block cannot be read until after the 2606 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still 2607 // need to look ahead before reading the IMPORTS record. For consistency, 2608 // this block is always read somehow (see BitstreamEntry::EndBlock). 2609 bool HasReadUnhashedControlBlock = false; 2610 auto readUnhashedControlBlockOnce = [&]() { 2611 if (!HasReadUnhashedControlBlock) { 2612 HasReadUnhashedControlBlock = true; 2613 if (ASTReadResult Result = 2614 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities)) 2615 return Result; 2616 } 2617 return Success; 2618 }; 2619 2620 bool DisableValidation = shouldDisableValidationForFile(F); 2621 2622 // Read all of the records and blocks in the control block. 2623 RecordData Record; 2624 unsigned NumInputs = 0; 2625 unsigned NumUserInputs = 0; 2626 StringRef BaseDirectoryAsWritten; 2627 while (true) { 2628 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2629 if (!MaybeEntry) { 2630 Error(MaybeEntry.takeError()); 2631 return Failure; 2632 } 2633 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2634 2635 switch (Entry.Kind) { 2636 case llvm::BitstreamEntry::Error: 2637 Error("malformed block record in AST file"); 2638 return Failure; 2639 case llvm::BitstreamEntry::EndBlock: { 2640 // Validate the module before returning. This call catches an AST with 2641 // no module name and no imports. 2642 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2643 return Result; 2644 2645 // Validate input files. 2646 const HeaderSearchOptions &HSOpts = 2647 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 2648 2649 // All user input files reside at the index range [0, NumUserInputs), and 2650 // system input files reside at [NumUserInputs, NumInputs). For explicitly 2651 // loaded module files, ignore missing inputs. 2652 if (!DisableValidation && F.Kind != MK_ExplicitModule && 2653 F.Kind != MK_PrebuiltModule) { 2654 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 2655 2656 // If we are reading a module, we will create a verification timestamp, 2657 // so we verify all input files. Otherwise, verify only user input 2658 // files. 2659 2660 unsigned N = NumUserInputs; 2661 if (ValidateSystemInputs || 2662 (HSOpts.ModulesValidateOncePerBuildSession && 2663 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp && 2664 F.Kind == MK_ImplicitModule)) 2665 N = NumInputs; 2666 2667 for (unsigned I = 0; I < N; ++I) { 2668 InputFile IF = getInputFile(F, I+1, Complain); 2669 if (!IF.getFile() || IF.isOutOfDate()) 2670 return OutOfDate; 2671 } 2672 } 2673 2674 if (Listener) 2675 Listener->visitModuleFile(F.FileName, F.Kind); 2676 2677 if (Listener && Listener->needsInputFileVisitation()) { 2678 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs 2679 : NumUserInputs; 2680 for (unsigned I = 0; I < N; ++I) { 2681 bool IsSystem = I >= NumUserInputs; 2682 InputFileInfo FI = readInputFileInfo(F, I+1); 2683 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden, 2684 F.Kind == MK_ExplicitModule || 2685 F.Kind == MK_PrebuiltModule); 2686 } 2687 } 2688 2689 return Success; 2690 } 2691 2692 case llvm::BitstreamEntry::SubBlock: 2693 switch (Entry.ID) { 2694 case INPUT_FILES_BLOCK_ID: 2695 F.InputFilesCursor = Stream; 2696 if (llvm::Error Err = Stream.SkipBlock()) { 2697 Error(std::move(Err)); 2698 return Failure; 2699 } 2700 if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) { 2701 Error("malformed block record in AST file"); 2702 return Failure; 2703 } 2704 continue; 2705 2706 case OPTIONS_BLOCK_ID: 2707 // If we're reading the first module for this group, check its options 2708 // are compatible with ours. For modules it imports, no further checking 2709 // is required, because we checked them when we built it. 2710 if (Listener && !ImportedBy) { 2711 // Should we allow the configuration of the module file to differ from 2712 // the configuration of the current translation unit in a compatible 2713 // way? 2714 // 2715 // FIXME: Allow this for files explicitly specified with -include-pch. 2716 bool AllowCompatibleConfigurationMismatch = 2717 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 2718 2719 ASTReadResult Result = 2720 ReadOptionsBlock(Stream, ClientLoadCapabilities, 2721 AllowCompatibleConfigurationMismatch, *Listener, 2722 SuggestedPredefines); 2723 if (Result == Failure) { 2724 Error("malformed block record in AST file"); 2725 return Result; 2726 } 2727 2728 if (DisableValidation || 2729 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 2730 Result = Success; 2731 2732 // If we can't load the module, exit early since we likely 2733 // will rebuild the module anyway. The stream may be in the 2734 // middle of a block. 2735 if (Result != Success) 2736 return Result; 2737 } else if (llvm::Error Err = Stream.SkipBlock()) { 2738 Error(std::move(Err)); 2739 return Failure; 2740 } 2741 continue; 2742 2743 default: 2744 if (llvm::Error Err = Stream.SkipBlock()) { 2745 Error(std::move(Err)); 2746 return Failure; 2747 } 2748 continue; 2749 } 2750 2751 case llvm::BitstreamEntry::Record: 2752 // The interesting case. 2753 break; 2754 } 2755 2756 // Read and process a record. 2757 Record.clear(); 2758 StringRef Blob; 2759 Expected<unsigned> MaybeRecordType = 2760 Stream.readRecord(Entry.ID, Record, &Blob); 2761 if (!MaybeRecordType) { 2762 Error(MaybeRecordType.takeError()); 2763 return Failure; 2764 } 2765 switch ((ControlRecordTypes)MaybeRecordType.get()) { 2766 case METADATA: { 2767 if (Record[0] != VERSION_MAJOR && !DisableValidation) { 2768 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2769 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old 2770 : diag::err_pch_version_too_new); 2771 return VersionMismatch; 2772 } 2773 2774 bool hasErrors = Record[6]; 2775 if (hasErrors && !DisableValidation) { 2776 // If requested by the caller and the module hasn't already been read 2777 // or compiled, mark modules on error as out-of-date. 2778 if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) && 2779 canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 2780 return OutOfDate; 2781 2782 if (!AllowASTWithCompilerErrors) { 2783 Diag(diag::err_pch_with_compiler_errors); 2784 return HadErrors; 2785 } 2786 } 2787 if (hasErrors) { 2788 Diags.ErrorOccurred = true; 2789 Diags.UncompilableErrorOccurred = true; 2790 Diags.UnrecoverableErrorOccurred = true; 2791 } 2792 2793 F.RelocatablePCH = Record[4]; 2794 // Relative paths in a relocatable PCH are relative to our sysroot. 2795 if (F.RelocatablePCH) 2796 F.BaseDirectory = isysroot.empty() ? "/" : isysroot; 2797 2798 F.HasTimestamps = Record[5]; 2799 2800 const std::string &CurBranch = getClangFullRepositoryVersion(); 2801 StringRef ASTBranch = Blob; 2802 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) { 2803 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2804 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch; 2805 return VersionMismatch; 2806 } 2807 break; 2808 } 2809 2810 case IMPORTS: { 2811 // Validate the AST before processing any imports (otherwise, untangling 2812 // them can be error-prone and expensive). A module will have a name and 2813 // will already have been validated, but this catches the PCH case. 2814 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2815 return Result; 2816 2817 // Load each of the imported PCH files. 2818 unsigned Idx = 0, N = Record.size(); 2819 while (Idx < N) { 2820 // Read information about the AST file. 2821 ModuleKind ImportedKind = (ModuleKind)Record[Idx++]; 2822 // The import location will be the local one for now; we will adjust 2823 // all import locations of module imports after the global source 2824 // location info are setup, in ReadAST. 2825 SourceLocation ImportLoc = 2826 ReadUntranslatedSourceLocation(Record[Idx++]); 2827 off_t StoredSize = (off_t)Record[Idx++]; 2828 time_t StoredModTime = (time_t)Record[Idx++]; 2829 auto FirstSignatureByte = Record.begin() + Idx; 2830 ASTFileSignature StoredSignature = ASTFileSignature::create( 2831 FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size); 2832 Idx += ASTFileSignature::size; 2833 2834 std::string ImportedName = ReadString(Record, Idx); 2835 std::string ImportedFile; 2836 2837 // For prebuilt and explicit modules first consult the file map for 2838 // an override. Note that here we don't search prebuilt module 2839 // directories, only the explicit name to file mappings. Also, we will 2840 // still verify the size/signature making sure it is essentially the 2841 // same file but perhaps in a different location. 2842 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule) 2843 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName( 2844 ImportedName, /*FileMapOnly*/ true); 2845 2846 if (ImportedFile.empty()) 2847 // Use BaseDirectoryAsWritten to ensure we use the same path in the 2848 // ModuleCache as when writing. 2849 ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx); 2850 else 2851 SkipPath(Record, Idx); 2852 2853 // If our client can't cope with us being out of date, we can't cope with 2854 // our dependency being missing. 2855 unsigned Capabilities = ClientLoadCapabilities; 2856 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 2857 Capabilities &= ~ARR_Missing; 2858 2859 // Load the AST file. 2860 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, 2861 Loaded, StoredSize, StoredModTime, 2862 StoredSignature, Capabilities); 2863 2864 // If we diagnosed a problem, produce a backtrace. 2865 bool recompilingFinalized = 2866 Result == OutOfDate && (Capabilities & ARR_OutOfDate) && 2867 getModuleManager().getModuleCache().isPCMFinal(F.FileName); 2868 if (isDiagnosedResult(Result, Capabilities) || recompilingFinalized) 2869 Diag(diag::note_module_file_imported_by) 2870 << F.FileName << !F.ModuleName.empty() << F.ModuleName; 2871 if (recompilingFinalized) 2872 Diag(diag::note_module_file_conflict); 2873 2874 switch (Result) { 2875 case Failure: return Failure; 2876 // If we have to ignore the dependency, we'll have to ignore this too. 2877 case Missing: 2878 case OutOfDate: return OutOfDate; 2879 case VersionMismatch: return VersionMismatch; 2880 case ConfigurationMismatch: return ConfigurationMismatch; 2881 case HadErrors: return HadErrors; 2882 case Success: break; 2883 } 2884 } 2885 break; 2886 } 2887 2888 case ORIGINAL_FILE: 2889 F.OriginalSourceFileID = FileID::get(Record[0]); 2890 F.ActualOriginalSourceFileName = std::string(Blob); 2891 F.OriginalSourceFileName = F.ActualOriginalSourceFileName; 2892 ResolveImportedPath(F, F.OriginalSourceFileName); 2893 break; 2894 2895 case ORIGINAL_FILE_ID: 2896 F.OriginalSourceFileID = FileID::get(Record[0]); 2897 break; 2898 2899 case ORIGINAL_PCH_DIR: 2900 F.OriginalDir = std::string(Blob); 2901 break; 2902 2903 case MODULE_NAME: 2904 F.ModuleName = std::string(Blob); 2905 Diag(diag::remark_module_import) 2906 << F.ModuleName << F.FileName << (ImportedBy ? true : false) 2907 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 2908 if (Listener) 2909 Listener->ReadModuleName(F.ModuleName); 2910 2911 // Validate the AST as soon as we have a name so we can exit early on 2912 // failure. 2913 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2914 return Result; 2915 2916 break; 2917 2918 case MODULE_DIRECTORY: { 2919 // Save the BaseDirectory as written in the PCM for computing the module 2920 // filename for the ModuleCache. 2921 BaseDirectoryAsWritten = Blob; 2922 assert(!F.ModuleName.empty() && 2923 "MODULE_DIRECTORY found before MODULE_NAME"); 2924 // If we've already loaded a module map file covering this module, we may 2925 // have a better path for it (relative to the current build). 2926 Module *M = PP.getHeaderSearchInfo().lookupModule( 2927 F.ModuleName, SourceLocation(), /*AllowSearch*/ true, 2928 /*AllowExtraModuleMapSearch*/ true); 2929 if (M && M->Directory) { 2930 // If we're implicitly loading a module, the base directory can't 2931 // change between the build and use. 2932 // Don't emit module relocation error if we have -fno-validate-pch 2933 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation & 2934 DisableValidationForModuleKind::Module) && 2935 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) { 2936 auto BuildDir = PP.getFileManager().getDirectory(Blob); 2937 if (!BuildDir || *BuildDir != M->Directory) { 2938 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 2939 Diag(diag::err_imported_module_relocated) 2940 << F.ModuleName << Blob << M->Directory->getName(); 2941 return OutOfDate; 2942 } 2943 } 2944 F.BaseDirectory = std::string(M->Directory->getName()); 2945 } else { 2946 F.BaseDirectory = std::string(Blob); 2947 } 2948 break; 2949 } 2950 2951 case MODULE_MAP_FILE: 2952 if (ASTReadResult Result = 2953 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities)) 2954 return Result; 2955 break; 2956 2957 case INPUT_FILE_OFFSETS: 2958 NumInputs = Record[0]; 2959 NumUserInputs = Record[1]; 2960 F.InputFileOffsets = 2961 (const llvm::support::unaligned_uint64_t *)Blob.data(); 2962 F.InputFilesLoaded.resize(NumInputs); 2963 F.NumUserInputFiles = NumUserInputs; 2964 break; 2965 } 2966 } 2967 } 2968 2969 void ASTReader::readIncludedFiles(ModuleFile &F, StringRef Blob, 2970 Preprocessor &PP) { 2971 using namespace llvm::support; 2972 2973 const unsigned char *D = (const unsigned char *)Blob.data(); 2974 unsigned FileCount = endian::readNext<uint32_t, little, unaligned>(D); 2975 2976 for (unsigned I = 0; I < FileCount; ++I) { 2977 size_t ID = endian::readNext<uint32_t, little, unaligned>(D); 2978 InputFileInfo IFI = readInputFileInfo(F, ID); 2979 if (llvm::ErrorOr<const FileEntry *> File = 2980 PP.getFileManager().getFile(IFI.Filename)) 2981 PP.getIncludedFiles().insert(*File); 2982 } 2983 } 2984 2985 llvm::Error ASTReader::ReadASTBlock(ModuleFile &F, 2986 unsigned ClientLoadCapabilities) { 2987 BitstreamCursor &Stream = F.Stream; 2988 2989 if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) 2990 return Err; 2991 F.ASTBlockStartOffset = Stream.GetCurrentBitNo(); 2992 2993 // Read all of the records and blocks for the AST file. 2994 RecordData Record; 2995 while (true) { 2996 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2997 if (!MaybeEntry) 2998 return MaybeEntry.takeError(); 2999 llvm::BitstreamEntry Entry = MaybeEntry.get(); 3000 3001 switch (Entry.Kind) { 3002 case llvm::BitstreamEntry::Error: 3003 return llvm::createStringError( 3004 std::errc::illegal_byte_sequence, 3005 "error at end of module block in AST file"); 3006 case llvm::BitstreamEntry::EndBlock: 3007 // Outside of C++, we do not store a lookup map for the translation unit. 3008 // Instead, mark it as needing a lookup map to be built if this module 3009 // contains any declarations lexically within it (which it always does!). 3010 // This usually has no cost, since we very rarely need the lookup map for 3011 // the translation unit outside C++. 3012 if (ASTContext *Ctx = ContextObj) { 3013 DeclContext *DC = Ctx->getTranslationUnitDecl(); 3014 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus) 3015 DC->setMustBuildLookupTable(); 3016 } 3017 3018 return llvm::Error::success(); 3019 case llvm::BitstreamEntry::SubBlock: 3020 switch (Entry.ID) { 3021 case DECLTYPES_BLOCK_ID: 3022 // We lazily load the decls block, but we want to set up the 3023 // DeclsCursor cursor to point into it. Clone our current bitcode 3024 // cursor to it, enter the block and read the abbrevs in that block. 3025 // With the main cursor, we just skip over it. 3026 F.DeclsCursor = Stream; 3027 if (llvm::Error Err = Stream.SkipBlock()) 3028 return Err; 3029 if (llvm::Error Err = ReadBlockAbbrevs( 3030 F.DeclsCursor, DECLTYPES_BLOCK_ID, &F.DeclsBlockStartOffset)) 3031 return Err; 3032 break; 3033 3034 case PREPROCESSOR_BLOCK_ID: 3035 F.MacroCursor = Stream; 3036 if (!PP.getExternalSource()) 3037 PP.setExternalSource(this); 3038 3039 if (llvm::Error Err = Stream.SkipBlock()) 3040 return Err; 3041 if (llvm::Error Err = 3042 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) 3043 return Err; 3044 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo(); 3045 break; 3046 3047 case PREPROCESSOR_DETAIL_BLOCK_ID: 3048 F.PreprocessorDetailCursor = Stream; 3049 3050 if (llvm::Error Err = Stream.SkipBlock()) { 3051 return Err; 3052 } 3053 if (llvm::Error Err = ReadBlockAbbrevs(F.PreprocessorDetailCursor, 3054 PREPROCESSOR_DETAIL_BLOCK_ID)) 3055 return Err; 3056 F.PreprocessorDetailStartOffset 3057 = F.PreprocessorDetailCursor.GetCurrentBitNo(); 3058 3059 if (!PP.getPreprocessingRecord()) 3060 PP.createPreprocessingRecord(); 3061 if (!PP.getPreprocessingRecord()->getExternalSource()) 3062 PP.getPreprocessingRecord()->SetExternalSource(*this); 3063 break; 3064 3065 case SOURCE_MANAGER_BLOCK_ID: 3066 if (llvm::Error Err = ReadSourceManagerBlock(F)) 3067 return Err; 3068 break; 3069 3070 case SUBMODULE_BLOCK_ID: 3071 if (llvm::Error Err = ReadSubmoduleBlock(F, ClientLoadCapabilities)) 3072 return Err; 3073 break; 3074 3075 case COMMENTS_BLOCK_ID: { 3076 BitstreamCursor C = Stream; 3077 3078 if (llvm::Error Err = Stream.SkipBlock()) 3079 return Err; 3080 if (llvm::Error Err = ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) 3081 return Err; 3082 CommentsCursors.push_back(std::make_pair(C, &F)); 3083 break; 3084 } 3085 3086 default: 3087 if (llvm::Error Err = Stream.SkipBlock()) 3088 return Err; 3089 break; 3090 } 3091 continue; 3092 3093 case llvm::BitstreamEntry::Record: 3094 // The interesting case. 3095 break; 3096 } 3097 3098 // Read and process a record. 3099 Record.clear(); 3100 StringRef Blob; 3101 Expected<unsigned> MaybeRecordType = 3102 Stream.readRecord(Entry.ID, Record, &Blob); 3103 if (!MaybeRecordType) 3104 return MaybeRecordType.takeError(); 3105 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get(); 3106 3107 // If we're not loading an AST context, we don't care about most records. 3108 if (!ContextObj) { 3109 switch (RecordType) { 3110 case IDENTIFIER_TABLE: 3111 case IDENTIFIER_OFFSET: 3112 case INTERESTING_IDENTIFIERS: 3113 case STATISTICS: 3114 case PP_ASSUME_NONNULL_LOC: 3115 case PP_CONDITIONAL_STACK: 3116 case PP_COUNTER_VALUE: 3117 case SOURCE_LOCATION_OFFSETS: 3118 case MODULE_OFFSET_MAP: 3119 case SOURCE_MANAGER_LINE_TABLE: 3120 case SOURCE_LOCATION_PRELOADS: 3121 case PPD_ENTITIES_OFFSETS: 3122 case HEADER_SEARCH_TABLE: 3123 case IMPORTED_MODULES: 3124 case MACRO_OFFSET: 3125 break; 3126 default: 3127 continue; 3128 } 3129 } 3130 3131 switch (RecordType) { 3132 default: // Default behavior: ignore. 3133 break; 3134 3135 case TYPE_OFFSET: { 3136 if (F.LocalNumTypes != 0) 3137 return llvm::createStringError( 3138 std::errc::illegal_byte_sequence, 3139 "duplicate TYPE_OFFSET record in AST file"); 3140 F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data()); 3141 F.LocalNumTypes = Record[0]; 3142 unsigned LocalBaseTypeIndex = Record[1]; 3143 F.BaseTypeIndex = getTotalNumTypes(); 3144 3145 if (F.LocalNumTypes > 0) { 3146 // Introduce the global -> local mapping for types within this module. 3147 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F)); 3148 3149 // Introduce the local -> global mapping for types within this module. 3150 F.TypeRemap.insertOrReplace( 3151 std::make_pair(LocalBaseTypeIndex, 3152 F.BaseTypeIndex - LocalBaseTypeIndex)); 3153 3154 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes); 3155 } 3156 break; 3157 } 3158 3159 case DECL_OFFSET: { 3160 if (F.LocalNumDecls != 0) 3161 return llvm::createStringError( 3162 std::errc::illegal_byte_sequence, 3163 "duplicate DECL_OFFSET record in AST file"); 3164 F.DeclOffsets = (const DeclOffset *)Blob.data(); 3165 F.LocalNumDecls = Record[0]; 3166 unsigned LocalBaseDeclID = Record[1]; 3167 F.BaseDeclID = getTotalNumDecls(); 3168 3169 if (F.LocalNumDecls > 0) { 3170 // Introduce the global -> local mapping for declarations within this 3171 // module. 3172 GlobalDeclMap.insert( 3173 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F)); 3174 3175 // Introduce the local -> global mapping for declarations within this 3176 // module. 3177 F.DeclRemap.insertOrReplace( 3178 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID)); 3179 3180 // Introduce the global -> local mapping for declarations within this 3181 // module. 3182 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID; 3183 3184 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls); 3185 } 3186 break; 3187 } 3188 3189 case TU_UPDATE_LEXICAL: { 3190 DeclContext *TU = ContextObj->getTranslationUnitDecl(); 3191 LexicalContents Contents( 3192 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 3193 Blob.data()), 3194 static_cast<unsigned int>(Blob.size() / 4)); 3195 TULexicalDecls.push_back(std::make_pair(&F, Contents)); 3196 TU->setHasExternalLexicalStorage(true); 3197 break; 3198 } 3199 3200 case UPDATE_VISIBLE: { 3201 unsigned Idx = 0; 3202 serialization::DeclID ID = ReadDeclID(F, Record, Idx); 3203 auto *Data = (const unsigned char*)Blob.data(); 3204 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data}); 3205 // If we've already loaded the decl, perform the updates when we finish 3206 // loading this block. 3207 if (Decl *D = GetExistingDecl(ID)) 3208 PendingUpdateRecords.push_back( 3209 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3210 break; 3211 } 3212 3213 case IDENTIFIER_TABLE: 3214 F.IdentifierTableData = 3215 reinterpret_cast<const unsigned char *>(Blob.data()); 3216 if (Record[0]) { 3217 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create( 3218 F.IdentifierTableData + Record[0], 3219 F.IdentifierTableData + sizeof(uint32_t), 3220 F.IdentifierTableData, 3221 ASTIdentifierLookupTrait(*this, F)); 3222 3223 PP.getIdentifierTable().setExternalIdentifierLookup(this); 3224 } 3225 break; 3226 3227 case IDENTIFIER_OFFSET: { 3228 if (F.LocalNumIdentifiers != 0) 3229 return llvm::createStringError( 3230 std::errc::illegal_byte_sequence, 3231 "duplicate IDENTIFIER_OFFSET record in AST file"); 3232 F.IdentifierOffsets = (const uint32_t *)Blob.data(); 3233 F.LocalNumIdentifiers = Record[0]; 3234 unsigned LocalBaseIdentifierID = Record[1]; 3235 F.BaseIdentifierID = getTotalNumIdentifiers(); 3236 3237 if (F.LocalNumIdentifiers > 0) { 3238 // Introduce the global -> local mapping for identifiers within this 3239 // module. 3240 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1, 3241 &F)); 3242 3243 // Introduce the local -> global mapping for identifiers within this 3244 // module. 3245 F.IdentifierRemap.insertOrReplace( 3246 std::make_pair(LocalBaseIdentifierID, 3247 F.BaseIdentifierID - LocalBaseIdentifierID)); 3248 3249 IdentifiersLoaded.resize(IdentifiersLoaded.size() 3250 + F.LocalNumIdentifiers); 3251 } 3252 break; 3253 } 3254 3255 case INTERESTING_IDENTIFIERS: 3256 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end()); 3257 break; 3258 3259 case EAGERLY_DESERIALIZED_DECLS: 3260 // FIXME: Skip reading this record if our ASTConsumer doesn't care 3261 // about "interesting" decls (for instance, if we're building a module). 3262 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3263 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3264 break; 3265 3266 case MODULAR_CODEGEN_DECLS: 3267 // FIXME: Skip reading this record if our ASTConsumer doesn't care about 3268 // them (ie: if we're not codegenerating this module). 3269 if (F.Kind == MK_MainFile || 3270 getContext().getLangOpts().BuildingPCHWithObjectFile) 3271 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3272 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3273 break; 3274 3275 case SPECIAL_TYPES: 3276 if (SpecialTypes.empty()) { 3277 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3278 SpecialTypes.push_back(getGlobalTypeID(F, Record[I])); 3279 break; 3280 } 3281 3282 if (SpecialTypes.size() != Record.size()) 3283 return llvm::createStringError(std::errc::illegal_byte_sequence, 3284 "invalid special-types record"); 3285 3286 for (unsigned I = 0, N = Record.size(); I != N; ++I) { 3287 serialization::TypeID ID = getGlobalTypeID(F, Record[I]); 3288 if (!SpecialTypes[I]) 3289 SpecialTypes[I] = ID; 3290 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate 3291 // merge step? 3292 } 3293 break; 3294 3295 case STATISTICS: 3296 TotalNumStatements += Record[0]; 3297 TotalNumMacros += Record[1]; 3298 TotalLexicalDeclContexts += Record[2]; 3299 TotalVisibleDeclContexts += Record[3]; 3300 break; 3301 3302 case UNUSED_FILESCOPED_DECLS: 3303 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3304 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I])); 3305 break; 3306 3307 case DELEGATING_CTORS: 3308 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3309 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I])); 3310 break; 3311 3312 case WEAK_UNDECLARED_IDENTIFIERS: 3313 if (Record.size() % 3 != 0) 3314 return llvm::createStringError(std::errc::illegal_byte_sequence, 3315 "invalid weak identifiers record"); 3316 3317 // FIXME: Ignore weak undeclared identifiers from non-original PCH 3318 // files. This isn't the way to do it :) 3319 WeakUndeclaredIdentifiers.clear(); 3320 3321 // Translate the weak, undeclared identifiers into global IDs. 3322 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) { 3323 WeakUndeclaredIdentifiers.push_back( 3324 getGlobalIdentifierID(F, Record[I++])); 3325 WeakUndeclaredIdentifiers.push_back( 3326 getGlobalIdentifierID(F, Record[I++])); 3327 WeakUndeclaredIdentifiers.push_back( 3328 ReadSourceLocation(F, Record, I).getRawEncoding()); 3329 } 3330 break; 3331 3332 case SELECTOR_OFFSETS: { 3333 F.SelectorOffsets = (const uint32_t *)Blob.data(); 3334 F.LocalNumSelectors = Record[0]; 3335 unsigned LocalBaseSelectorID = Record[1]; 3336 F.BaseSelectorID = getTotalNumSelectors(); 3337 3338 if (F.LocalNumSelectors > 0) { 3339 // Introduce the global -> local mapping for selectors within this 3340 // module. 3341 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F)); 3342 3343 // Introduce the local -> global mapping for selectors within this 3344 // module. 3345 F.SelectorRemap.insertOrReplace( 3346 std::make_pair(LocalBaseSelectorID, 3347 F.BaseSelectorID - LocalBaseSelectorID)); 3348 3349 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors); 3350 } 3351 break; 3352 } 3353 3354 case METHOD_POOL: 3355 F.SelectorLookupTableData = (const unsigned char *)Blob.data(); 3356 if (Record[0]) 3357 F.SelectorLookupTable 3358 = ASTSelectorLookupTable::Create( 3359 F.SelectorLookupTableData + Record[0], 3360 F.SelectorLookupTableData, 3361 ASTSelectorLookupTrait(*this, F)); 3362 TotalNumMethodPoolEntries += Record[1]; 3363 break; 3364 3365 case REFERENCED_SELECTOR_POOL: 3366 if (!Record.empty()) { 3367 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) { 3368 ReferencedSelectorsData.push_back(getGlobalSelectorID(F, 3369 Record[Idx++])); 3370 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx). 3371 getRawEncoding()); 3372 } 3373 } 3374 break; 3375 3376 case PP_ASSUME_NONNULL_LOC: { 3377 unsigned Idx = 0; 3378 if (!Record.empty()) 3379 PP.setPreambleRecordedPragmaAssumeNonNullLoc( 3380 ReadSourceLocation(F, Record, Idx)); 3381 break; 3382 } 3383 3384 case PP_CONDITIONAL_STACK: 3385 if (!Record.empty()) { 3386 unsigned Idx = 0, End = Record.size() - 1; 3387 bool ReachedEOFWhileSkipping = Record[Idx++]; 3388 llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo; 3389 if (ReachedEOFWhileSkipping) { 3390 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx); 3391 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx); 3392 bool FoundNonSkipPortion = Record[Idx++]; 3393 bool FoundElse = Record[Idx++]; 3394 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx); 3395 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion, 3396 FoundElse, ElseLoc); 3397 } 3398 SmallVector<PPConditionalInfo, 4> ConditionalStack; 3399 while (Idx < End) { 3400 auto Loc = ReadSourceLocation(F, Record, Idx); 3401 bool WasSkipping = Record[Idx++]; 3402 bool FoundNonSkip = Record[Idx++]; 3403 bool FoundElse = Record[Idx++]; 3404 ConditionalStack.push_back( 3405 {Loc, WasSkipping, FoundNonSkip, FoundElse}); 3406 } 3407 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo); 3408 } 3409 break; 3410 3411 case PP_COUNTER_VALUE: 3412 if (!Record.empty() && Listener) 3413 Listener->ReadCounter(F, Record[0]); 3414 break; 3415 3416 case FILE_SORTED_DECLS: 3417 F.FileSortedDecls = (const DeclID *)Blob.data(); 3418 F.NumFileSortedDecls = Record[0]; 3419 break; 3420 3421 case SOURCE_LOCATION_OFFSETS: { 3422 F.SLocEntryOffsets = (const uint32_t *)Blob.data(); 3423 F.LocalNumSLocEntries = Record[0]; 3424 SourceLocation::UIntTy SLocSpaceSize = Record[1]; 3425 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset; 3426 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) = 3427 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, 3428 SLocSpaceSize); 3429 if (!F.SLocEntryBaseID) 3430 return llvm::createStringError(std::errc::invalid_argument, 3431 "ran out of source locations"); 3432 // Make our entry in the range map. BaseID is negative and growing, so 3433 // we invert it. Because we invert it, though, we need the other end of 3434 // the range. 3435 unsigned RangeStart = 3436 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1; 3437 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F)); 3438 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset); 3439 3440 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing. 3441 assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0); 3442 GlobalSLocOffsetMap.insert( 3443 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset 3444 - SLocSpaceSize,&F)); 3445 3446 // Initialize the remapping table. 3447 // Invalid stays invalid. 3448 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0)); 3449 // This module. Base was 2 when being compiled. 3450 F.SLocRemap.insertOrReplace(std::make_pair( 3451 2U, static_cast<SourceLocation::IntTy>(F.SLocEntryBaseOffset - 2))); 3452 3453 TotalNumSLocEntries += F.LocalNumSLocEntries; 3454 break; 3455 } 3456 3457 case MODULE_OFFSET_MAP: 3458 F.ModuleOffsetMap = Blob; 3459 break; 3460 3461 case SOURCE_MANAGER_LINE_TABLE: 3462 ParseLineTable(F, Record); 3463 break; 3464 3465 case SOURCE_LOCATION_PRELOADS: { 3466 // Need to transform from the local view (1-based IDs) to the global view, 3467 // which is based off F.SLocEntryBaseID. 3468 if (!F.PreloadSLocEntries.empty()) 3469 return llvm::createStringError( 3470 std::errc::illegal_byte_sequence, 3471 "Multiple SOURCE_LOCATION_PRELOADS records in AST file"); 3472 3473 F.PreloadSLocEntries.swap(Record); 3474 break; 3475 } 3476 3477 case EXT_VECTOR_DECLS: 3478 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3479 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I])); 3480 break; 3481 3482 case VTABLE_USES: 3483 if (Record.size() % 3 != 0) 3484 return llvm::createStringError(std::errc::illegal_byte_sequence, 3485 "Invalid VTABLE_USES record"); 3486 3487 // Later tables overwrite earlier ones. 3488 // FIXME: Modules will have some trouble with this. This is clearly not 3489 // the right way to do this. 3490 VTableUses.clear(); 3491 3492 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) { 3493 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++])); 3494 VTableUses.push_back( 3495 ReadSourceLocation(F, Record, Idx).getRawEncoding()); 3496 VTableUses.push_back(Record[Idx++]); 3497 } 3498 break; 3499 3500 case PENDING_IMPLICIT_INSTANTIATIONS: 3501 if (PendingInstantiations.size() % 2 != 0) 3502 return llvm::createStringError( 3503 std::errc::illegal_byte_sequence, 3504 "Invalid existing PendingInstantiations"); 3505 3506 if (Record.size() % 2 != 0) 3507 return llvm::createStringError( 3508 std::errc::illegal_byte_sequence, 3509 "Invalid PENDING_IMPLICIT_INSTANTIATIONS block"); 3510 3511 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3512 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++])); 3513 PendingInstantiations.push_back( 3514 ReadSourceLocation(F, Record, I).getRawEncoding()); 3515 } 3516 break; 3517 3518 case SEMA_DECL_REFS: 3519 if (Record.size() != 3) 3520 return llvm::createStringError(std::errc::illegal_byte_sequence, 3521 "Invalid SEMA_DECL_REFS block"); 3522 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3523 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3524 break; 3525 3526 case PPD_ENTITIES_OFFSETS: { 3527 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data(); 3528 assert(Blob.size() % sizeof(PPEntityOffset) == 0); 3529 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset); 3530 3531 unsigned LocalBasePreprocessedEntityID = Record[0]; 3532 3533 unsigned StartingID; 3534 if (!PP.getPreprocessingRecord()) 3535 PP.createPreprocessingRecord(); 3536 if (!PP.getPreprocessingRecord()->getExternalSource()) 3537 PP.getPreprocessingRecord()->SetExternalSource(*this); 3538 StartingID 3539 = PP.getPreprocessingRecord() 3540 ->allocateLoadedEntities(F.NumPreprocessedEntities); 3541 F.BasePreprocessedEntityID = StartingID; 3542 3543 if (F.NumPreprocessedEntities > 0) { 3544 // Introduce the global -> local mapping for preprocessed entities in 3545 // this module. 3546 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F)); 3547 3548 // Introduce the local -> global mapping for preprocessed entities in 3549 // this module. 3550 F.PreprocessedEntityRemap.insertOrReplace( 3551 std::make_pair(LocalBasePreprocessedEntityID, 3552 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID)); 3553 } 3554 3555 break; 3556 } 3557 3558 case PPD_SKIPPED_RANGES: { 3559 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data(); 3560 assert(Blob.size() % sizeof(PPSkippedRange) == 0); 3561 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange); 3562 3563 if (!PP.getPreprocessingRecord()) 3564 PP.createPreprocessingRecord(); 3565 if (!PP.getPreprocessingRecord()->getExternalSource()) 3566 PP.getPreprocessingRecord()->SetExternalSource(*this); 3567 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord() 3568 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges); 3569 3570 if (F.NumPreprocessedSkippedRanges > 0) 3571 GlobalSkippedRangeMap.insert( 3572 std::make_pair(F.BasePreprocessedSkippedRangeID, &F)); 3573 break; 3574 } 3575 3576 case DECL_UPDATE_OFFSETS: 3577 if (Record.size() % 2 != 0) 3578 return llvm::createStringError( 3579 std::errc::illegal_byte_sequence, 3580 "invalid DECL_UPDATE_OFFSETS block in AST file"); 3581 for (unsigned I = 0, N = Record.size(); I != N; I += 2) { 3582 GlobalDeclID ID = getGlobalDeclID(F, Record[I]); 3583 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1])); 3584 3585 // If we've already loaded the decl, perform the updates when we finish 3586 // loading this block. 3587 if (Decl *D = GetExistingDecl(ID)) 3588 PendingUpdateRecords.push_back( 3589 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3590 } 3591 break; 3592 3593 case OBJC_CATEGORIES_MAP: 3594 if (F.LocalNumObjCCategoriesInMap != 0) 3595 return llvm::createStringError( 3596 std::errc::illegal_byte_sequence, 3597 "duplicate OBJC_CATEGORIES_MAP record in AST file"); 3598 3599 F.LocalNumObjCCategoriesInMap = Record[0]; 3600 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data(); 3601 break; 3602 3603 case OBJC_CATEGORIES: 3604 F.ObjCCategories.swap(Record); 3605 break; 3606 3607 case CUDA_SPECIAL_DECL_REFS: 3608 // Later tables overwrite earlier ones. 3609 // FIXME: Modules will have trouble with this. 3610 CUDASpecialDeclRefs.clear(); 3611 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3612 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3613 break; 3614 3615 case HEADER_SEARCH_TABLE: 3616 F.HeaderFileInfoTableData = Blob.data(); 3617 F.LocalNumHeaderFileInfos = Record[1]; 3618 if (Record[0]) { 3619 F.HeaderFileInfoTable 3620 = HeaderFileInfoLookupTable::Create( 3621 (const unsigned char *)F.HeaderFileInfoTableData + Record[0], 3622 (const unsigned char *)F.HeaderFileInfoTableData, 3623 HeaderFileInfoTrait(*this, F, 3624 &PP.getHeaderSearchInfo(), 3625 Blob.data() + Record[2])); 3626 3627 PP.getHeaderSearchInfo().SetExternalSource(this); 3628 if (!PP.getHeaderSearchInfo().getExternalLookup()) 3629 PP.getHeaderSearchInfo().SetExternalLookup(this); 3630 } 3631 break; 3632 3633 case FP_PRAGMA_OPTIONS: 3634 // Later tables overwrite earlier ones. 3635 FPPragmaOptions.swap(Record); 3636 break; 3637 3638 case OPENCL_EXTENSIONS: 3639 for (unsigned I = 0, E = Record.size(); I != E; ) { 3640 auto Name = ReadString(Record, I); 3641 auto &OptInfo = OpenCLExtensions.OptMap[Name]; 3642 OptInfo.Supported = Record[I++] != 0; 3643 OptInfo.Enabled = Record[I++] != 0; 3644 OptInfo.WithPragma = Record[I++] != 0; 3645 OptInfo.Avail = Record[I++]; 3646 OptInfo.Core = Record[I++]; 3647 OptInfo.Opt = Record[I++]; 3648 } 3649 break; 3650 3651 case TENTATIVE_DEFINITIONS: 3652 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3653 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I])); 3654 break; 3655 3656 case KNOWN_NAMESPACES: 3657 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3658 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I])); 3659 break; 3660 3661 case UNDEFINED_BUT_USED: 3662 if (UndefinedButUsed.size() % 2 != 0) 3663 return llvm::createStringError(std::errc::illegal_byte_sequence, 3664 "Invalid existing UndefinedButUsed"); 3665 3666 if (Record.size() % 2 != 0) 3667 return llvm::createStringError(std::errc::illegal_byte_sequence, 3668 "invalid undefined-but-used record"); 3669 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3670 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++])); 3671 UndefinedButUsed.push_back( 3672 ReadSourceLocation(F, Record, I).getRawEncoding()); 3673 } 3674 break; 3675 3676 case DELETE_EXPRS_TO_ANALYZE: 3677 for (unsigned I = 0, N = Record.size(); I != N;) { 3678 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++])); 3679 const uint64_t Count = Record[I++]; 3680 DelayedDeleteExprs.push_back(Count); 3681 for (uint64_t C = 0; C < Count; ++C) { 3682 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding()); 3683 bool IsArrayForm = Record[I++] == 1; 3684 DelayedDeleteExprs.push_back(IsArrayForm); 3685 } 3686 } 3687 break; 3688 3689 case IMPORTED_MODULES: 3690 if (!F.isModule()) { 3691 // If we aren't loading a module (which has its own exports), make 3692 // all of the imported modules visible. 3693 // FIXME: Deal with macros-only imports. 3694 for (unsigned I = 0, N = Record.size(); I != N; /**/) { 3695 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]); 3696 SourceLocation Loc = ReadSourceLocation(F, Record, I); 3697 if (GlobalID) { 3698 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc)); 3699 if (DeserializationListener) 3700 DeserializationListener->ModuleImportRead(GlobalID, Loc); 3701 } 3702 } 3703 } 3704 break; 3705 3706 case MACRO_OFFSET: { 3707 if (F.LocalNumMacros != 0) 3708 return llvm::createStringError( 3709 std::errc::illegal_byte_sequence, 3710 "duplicate MACRO_OFFSET record in AST file"); 3711 F.MacroOffsets = (const uint32_t *)Blob.data(); 3712 F.LocalNumMacros = Record[0]; 3713 unsigned LocalBaseMacroID = Record[1]; 3714 F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset; 3715 F.BaseMacroID = getTotalNumMacros(); 3716 3717 if (F.LocalNumMacros > 0) { 3718 // Introduce the global -> local mapping for macros within this module. 3719 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F)); 3720 3721 // Introduce the local -> global mapping for macros within this module. 3722 F.MacroRemap.insertOrReplace( 3723 std::make_pair(LocalBaseMacroID, 3724 F.BaseMacroID - LocalBaseMacroID)); 3725 3726 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros); 3727 } 3728 break; 3729 } 3730 3731 case PP_INCLUDED_FILES: 3732 readIncludedFiles(F, Blob, PP); 3733 break; 3734 3735 case LATE_PARSED_TEMPLATE: 3736 LateParsedTemplates.emplace_back( 3737 std::piecewise_construct, std::forward_as_tuple(&F), 3738 std::forward_as_tuple(Record.begin(), Record.end())); 3739 break; 3740 3741 case OPTIMIZE_PRAGMA_OPTIONS: 3742 if (Record.size() != 1) 3743 return llvm::createStringError(std::errc::illegal_byte_sequence, 3744 "invalid pragma optimize record"); 3745 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]); 3746 break; 3747 3748 case MSSTRUCT_PRAGMA_OPTIONS: 3749 if (Record.size() != 1) 3750 return llvm::createStringError(std::errc::illegal_byte_sequence, 3751 "invalid pragma ms_struct record"); 3752 PragmaMSStructState = Record[0]; 3753 break; 3754 3755 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS: 3756 if (Record.size() != 2) 3757 return llvm::createStringError( 3758 std::errc::illegal_byte_sequence, 3759 "invalid pragma pointers to members record"); 3760 PragmaMSPointersToMembersState = Record[0]; 3761 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]); 3762 break; 3763 3764 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES: 3765 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3766 UnusedLocalTypedefNameCandidates.push_back( 3767 getGlobalDeclID(F, Record[I])); 3768 break; 3769 3770 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH: 3771 if (Record.size() != 1) 3772 return llvm::createStringError(std::errc::illegal_byte_sequence, 3773 "invalid cuda pragma options record"); 3774 ForceCUDAHostDeviceDepth = Record[0]; 3775 break; 3776 3777 case ALIGN_PACK_PRAGMA_OPTIONS: { 3778 if (Record.size() < 3) 3779 return llvm::createStringError(std::errc::illegal_byte_sequence, 3780 "invalid pragma pack record"); 3781 PragmaAlignPackCurrentValue = ReadAlignPackInfo(Record[0]); 3782 PragmaAlignPackCurrentLocation = ReadSourceLocation(F, Record[1]); 3783 unsigned NumStackEntries = Record[2]; 3784 unsigned Idx = 3; 3785 // Reset the stack when importing a new module. 3786 PragmaAlignPackStack.clear(); 3787 for (unsigned I = 0; I < NumStackEntries; ++I) { 3788 PragmaAlignPackStackEntry Entry; 3789 Entry.Value = ReadAlignPackInfo(Record[Idx++]); 3790 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3791 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3792 PragmaAlignPackStrings.push_back(ReadString(Record, Idx)); 3793 Entry.SlotLabel = PragmaAlignPackStrings.back(); 3794 PragmaAlignPackStack.push_back(Entry); 3795 } 3796 break; 3797 } 3798 3799 case FLOAT_CONTROL_PRAGMA_OPTIONS: { 3800 if (Record.size() < 3) 3801 return llvm::createStringError(std::errc::illegal_byte_sequence, 3802 "invalid pragma float control record"); 3803 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]); 3804 FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]); 3805 unsigned NumStackEntries = Record[2]; 3806 unsigned Idx = 3; 3807 // Reset the stack when importing a new module. 3808 FpPragmaStack.clear(); 3809 for (unsigned I = 0; I < NumStackEntries; ++I) { 3810 FpPragmaStackEntry Entry; 3811 Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]); 3812 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3813 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3814 FpPragmaStrings.push_back(ReadString(Record, Idx)); 3815 Entry.SlotLabel = FpPragmaStrings.back(); 3816 FpPragmaStack.push_back(Entry); 3817 } 3818 break; 3819 } 3820 3821 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS: 3822 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3823 DeclsToCheckForDeferredDiags.insert(getGlobalDeclID(F, Record[I])); 3824 break; 3825 } 3826 } 3827 } 3828 3829 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const { 3830 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read"); 3831 3832 // Additional remapping information. 3833 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data(); 3834 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size(); 3835 F.ModuleOffsetMap = StringRef(); 3836 3837 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders. 3838 if (F.SLocRemap.find(0) == F.SLocRemap.end()) { 3839 F.SLocRemap.insert(std::make_pair(0U, 0)); 3840 F.SLocRemap.insert(std::make_pair(2U, 1)); 3841 } 3842 3843 // Continuous range maps we may be updating in our module. 3844 using SLocRemapBuilder = 3845 ContinuousRangeMap<SourceLocation::UIntTy, SourceLocation::IntTy, 3846 2>::Builder; 3847 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder; 3848 SLocRemapBuilder SLocRemap(F.SLocRemap); 3849 RemapBuilder IdentifierRemap(F.IdentifierRemap); 3850 RemapBuilder MacroRemap(F.MacroRemap); 3851 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap); 3852 RemapBuilder SubmoduleRemap(F.SubmoduleRemap); 3853 RemapBuilder SelectorRemap(F.SelectorRemap); 3854 RemapBuilder DeclRemap(F.DeclRemap); 3855 RemapBuilder TypeRemap(F.TypeRemap); 3856 3857 while (Data < DataEnd) { 3858 // FIXME: Looking up dependency modules by filename is horrible. Let's 3859 // start fixing this with prebuilt, explicit and implicit modules and see 3860 // how it goes... 3861 using namespace llvm::support; 3862 ModuleKind Kind = static_cast<ModuleKind>( 3863 endian::readNext<uint8_t, little, unaligned>(Data)); 3864 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data); 3865 StringRef Name = StringRef((const char*)Data, Len); 3866 Data += Len; 3867 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule || 3868 Kind == MK_ImplicitModule 3869 ? ModuleMgr.lookupByModuleName(Name) 3870 : ModuleMgr.lookupByFileName(Name)); 3871 if (!OM) { 3872 std::string Msg = 3873 "SourceLocation remap refers to unknown module, cannot find "; 3874 Msg.append(std::string(Name)); 3875 Error(Msg); 3876 return; 3877 } 3878 3879 SourceLocation::UIntTy SLocOffset = 3880 endian::readNext<uint32_t, little, unaligned>(Data); 3881 uint32_t IdentifierIDOffset = 3882 endian::readNext<uint32_t, little, unaligned>(Data); 3883 uint32_t MacroIDOffset = 3884 endian::readNext<uint32_t, little, unaligned>(Data); 3885 uint32_t PreprocessedEntityIDOffset = 3886 endian::readNext<uint32_t, little, unaligned>(Data); 3887 uint32_t SubmoduleIDOffset = 3888 endian::readNext<uint32_t, little, unaligned>(Data); 3889 uint32_t SelectorIDOffset = 3890 endian::readNext<uint32_t, little, unaligned>(Data); 3891 uint32_t DeclIDOffset = 3892 endian::readNext<uint32_t, little, unaligned>(Data); 3893 uint32_t TypeIndexOffset = 3894 endian::readNext<uint32_t, little, unaligned>(Data); 3895 3896 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset, 3897 RemapBuilder &Remap) { 3898 constexpr uint32_t None = std::numeric_limits<uint32_t>::max(); 3899 if (Offset != None) 3900 Remap.insert(std::make_pair(Offset, 3901 static_cast<int>(BaseOffset - Offset))); 3902 }; 3903 3904 constexpr SourceLocation::UIntTy SLocNone = 3905 std::numeric_limits<SourceLocation::UIntTy>::max(); 3906 if (SLocOffset != SLocNone) 3907 SLocRemap.insert(std::make_pair( 3908 SLocOffset, static_cast<SourceLocation::IntTy>( 3909 OM->SLocEntryBaseOffset - SLocOffset))); 3910 3911 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap); 3912 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap); 3913 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID, 3914 PreprocessedEntityRemap); 3915 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap); 3916 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap); 3917 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap); 3918 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap); 3919 3920 // Global -> local mappings. 3921 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset; 3922 } 3923 } 3924 3925 ASTReader::ASTReadResult 3926 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F, 3927 const ModuleFile *ImportedBy, 3928 unsigned ClientLoadCapabilities) { 3929 unsigned Idx = 0; 3930 F.ModuleMapPath = ReadPath(F, Record, Idx); 3931 3932 // Try to resolve ModuleName in the current header search context and 3933 // verify that it is found in the same module map file as we saved. If the 3934 // top-level AST file is a main file, skip this check because there is no 3935 // usable header search context. 3936 assert(!F.ModuleName.empty() && 3937 "MODULE_NAME should come before MODULE_MAP_FILE"); 3938 if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) { 3939 // An implicitly-loaded module file should have its module listed in some 3940 // module map file that we've already loaded. 3941 Module *M = 3942 PP.getHeaderSearchInfo().lookupModule(F.ModuleName, F.ImportLoc); 3943 auto &Map = PP.getHeaderSearchInfo().getModuleMap(); 3944 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr; 3945 // Don't emit module relocation error if we have -fno-validate-pch 3946 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation & 3947 DisableValidationForModuleKind::Module) && 3948 !ModMap) { 3949 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) { 3950 if (auto ASTFE = M ? M->getASTFile() : None) { 3951 // This module was defined by an imported (explicit) module. 3952 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName 3953 << ASTFE->getName(); 3954 } else { 3955 // This module was built with a different module map. 3956 Diag(diag::err_imported_module_not_found) 3957 << F.ModuleName << F.FileName 3958 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath 3959 << !ImportedBy; 3960 // In case it was imported by a PCH, there's a chance the user is 3961 // just missing to include the search path to the directory containing 3962 // the modulemap. 3963 if (ImportedBy && ImportedBy->Kind == MK_PCH) 3964 Diag(diag::note_imported_by_pch_module_not_found) 3965 << llvm::sys::path::parent_path(F.ModuleMapPath); 3966 } 3967 } 3968 return OutOfDate; 3969 } 3970 3971 assert(M && M->Name == F.ModuleName && "found module with different name"); 3972 3973 // Check the primary module map file. 3974 auto StoredModMap = FileMgr.getFile(F.ModuleMapPath); 3975 if (!StoredModMap || *StoredModMap != ModMap) { 3976 assert(ModMap && "found module is missing module map file"); 3977 assert((ImportedBy || F.Kind == MK_ImplicitModule) && 3978 "top-level import should be verified"); 3979 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy; 3980 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 3981 Diag(diag::err_imported_module_modmap_changed) 3982 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName) 3983 << ModMap->getName() << F.ModuleMapPath << NotImported; 3984 return OutOfDate; 3985 } 3986 3987 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps; 3988 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) { 3989 // FIXME: we should use input files rather than storing names. 3990 std::string Filename = ReadPath(F, Record, Idx); 3991 auto SF = FileMgr.getFile(Filename, false, false); 3992 if (!SF) { 3993 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 3994 Error("could not find file '" + Filename +"' referenced by AST file"); 3995 return OutOfDate; 3996 } 3997 AdditionalStoredMaps.insert(*SF); 3998 } 3999 4000 // Check any additional module map files (e.g. module.private.modulemap) 4001 // that are not in the pcm. 4002 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) { 4003 for (const FileEntry *ModMap : *AdditionalModuleMaps) { 4004 // Remove files that match 4005 // Note: SmallPtrSet::erase is really remove 4006 if (!AdditionalStoredMaps.erase(ModMap)) { 4007 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 4008 Diag(diag::err_module_different_modmap) 4009 << F.ModuleName << /*new*/0 << ModMap->getName(); 4010 return OutOfDate; 4011 } 4012 } 4013 } 4014 4015 // Check any additional module map files that are in the pcm, but not 4016 // found in header search. Cases that match are already removed. 4017 for (const FileEntry *ModMap : AdditionalStoredMaps) { 4018 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 4019 Diag(diag::err_module_different_modmap) 4020 << F.ModuleName << /*not new*/1 << ModMap->getName(); 4021 return OutOfDate; 4022 } 4023 } 4024 4025 if (Listener) 4026 Listener->ReadModuleMapFile(F.ModuleMapPath); 4027 return Success; 4028 } 4029 4030 /// Move the given method to the back of the global list of methods. 4031 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) { 4032 // Find the entry for this selector in the method pool. 4033 Sema::GlobalMethodPool::iterator Known 4034 = S.MethodPool.find(Method->getSelector()); 4035 if (Known == S.MethodPool.end()) 4036 return; 4037 4038 // Retrieve the appropriate method list. 4039 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first 4040 : Known->second.second; 4041 bool Found = false; 4042 for (ObjCMethodList *List = &Start; List; List = List->getNext()) { 4043 if (!Found) { 4044 if (List->getMethod() == Method) { 4045 Found = true; 4046 } else { 4047 // Keep searching. 4048 continue; 4049 } 4050 } 4051 4052 if (List->getNext()) 4053 List->setMethod(List->getNext()->getMethod()); 4054 else 4055 List->setMethod(Method); 4056 } 4057 } 4058 4059 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) { 4060 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?"); 4061 for (Decl *D : Names) { 4062 bool wasHidden = !D->isUnconditionallyVisible(); 4063 D->setVisibleDespiteOwningModule(); 4064 4065 if (wasHidden && SemaObj) { 4066 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) { 4067 moveMethodToBackOfGlobalList(*SemaObj, Method); 4068 } 4069 } 4070 } 4071 } 4072 4073 void ASTReader::makeModuleVisible(Module *Mod, 4074 Module::NameVisibilityKind NameVisibility, 4075 SourceLocation ImportLoc) { 4076 llvm::SmallPtrSet<Module *, 4> Visited; 4077 SmallVector<Module *, 4> Stack; 4078 Stack.push_back(Mod); 4079 while (!Stack.empty()) { 4080 Mod = Stack.pop_back_val(); 4081 4082 if (NameVisibility <= Mod->NameVisibility) { 4083 // This module already has this level of visibility (or greater), so 4084 // there is nothing more to do. 4085 continue; 4086 } 4087 4088 if (Mod->isUnimportable()) { 4089 // Modules that aren't importable cannot be made visible. 4090 continue; 4091 } 4092 4093 // Update the module's name visibility. 4094 Mod->NameVisibility = NameVisibility; 4095 4096 // If we've already deserialized any names from this module, 4097 // mark them as visible. 4098 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod); 4099 if (Hidden != HiddenNamesMap.end()) { 4100 auto HiddenNames = std::move(*Hidden); 4101 HiddenNamesMap.erase(Hidden); 4102 makeNamesVisible(HiddenNames.second, HiddenNames.first); 4103 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() && 4104 "making names visible added hidden names"); 4105 } 4106 4107 // Push any exported modules onto the stack to be marked as visible. 4108 SmallVector<Module *, 16> Exports; 4109 Mod->getExportedModules(Exports); 4110 for (SmallVectorImpl<Module *>::iterator 4111 I = Exports.begin(), E = Exports.end(); I != E; ++I) { 4112 Module *Exported = *I; 4113 if (Visited.insert(Exported).second) 4114 Stack.push_back(Exported); 4115 } 4116 } 4117 } 4118 4119 /// We've merged the definition \p MergedDef into the existing definition 4120 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made 4121 /// visible. 4122 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def, 4123 NamedDecl *MergedDef) { 4124 if (!Def->isUnconditionallyVisible()) { 4125 // If MergedDef is visible or becomes visible, make the definition visible. 4126 if (MergedDef->isUnconditionallyVisible()) 4127 Def->setVisibleDespiteOwningModule(); 4128 else { 4129 getContext().mergeDefinitionIntoModule( 4130 Def, MergedDef->getImportedOwningModule(), 4131 /*NotifyListeners*/ false); 4132 PendingMergedDefinitionsToDeduplicate.insert(Def); 4133 } 4134 } 4135 } 4136 4137 bool ASTReader::loadGlobalIndex() { 4138 if (GlobalIndex) 4139 return false; 4140 4141 if (TriedLoadingGlobalIndex || !UseGlobalIndex || 4142 !PP.getLangOpts().Modules) 4143 return true; 4144 4145 // Try to load the global index. 4146 TriedLoadingGlobalIndex = true; 4147 StringRef ModuleCachePath 4148 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath(); 4149 std::pair<GlobalModuleIndex *, llvm::Error> Result = 4150 GlobalModuleIndex::readIndex(ModuleCachePath); 4151 if (llvm::Error Err = std::move(Result.second)) { 4152 assert(!Result.first); 4153 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 4154 return true; 4155 } 4156 4157 GlobalIndex.reset(Result.first); 4158 ModuleMgr.setGlobalIndex(GlobalIndex.get()); 4159 return false; 4160 } 4161 4162 bool ASTReader::isGlobalIndexUnavailable() const { 4163 return PP.getLangOpts().Modules && UseGlobalIndex && 4164 !hasGlobalIndex() && TriedLoadingGlobalIndex; 4165 } 4166 4167 static void updateModuleTimestamp(ModuleFile &MF) { 4168 // Overwrite the timestamp file contents so that file's mtime changes. 4169 std::string TimestampFilename = MF.getTimestampFilename(); 4170 std::error_code EC; 4171 llvm::raw_fd_ostream OS(TimestampFilename, EC, 4172 llvm::sys::fs::OF_TextWithCRLF); 4173 if (EC) 4174 return; 4175 OS << "Timestamp file\n"; 4176 OS.close(); 4177 OS.clear_error(); // Avoid triggering a fatal error. 4178 } 4179 4180 /// Given a cursor at the start of an AST file, scan ahead and drop the 4181 /// cursor into the start of the given block ID, returning false on success and 4182 /// true on failure. 4183 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) { 4184 while (true) { 4185 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 4186 if (!MaybeEntry) { 4187 // FIXME this drops errors on the floor. 4188 consumeError(MaybeEntry.takeError()); 4189 return true; 4190 } 4191 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4192 4193 switch (Entry.Kind) { 4194 case llvm::BitstreamEntry::Error: 4195 case llvm::BitstreamEntry::EndBlock: 4196 return true; 4197 4198 case llvm::BitstreamEntry::Record: 4199 // Ignore top-level records. 4200 if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID)) 4201 break; 4202 else { 4203 // FIXME this drops errors on the floor. 4204 consumeError(Skipped.takeError()); 4205 return true; 4206 } 4207 4208 case llvm::BitstreamEntry::SubBlock: 4209 if (Entry.ID == BlockID) { 4210 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) { 4211 // FIXME this drops the error on the floor. 4212 consumeError(std::move(Err)); 4213 return true; 4214 } 4215 // Found it! 4216 return false; 4217 } 4218 4219 if (llvm::Error Err = Cursor.SkipBlock()) { 4220 // FIXME this drops the error on the floor. 4221 consumeError(std::move(Err)); 4222 return true; 4223 } 4224 } 4225 } 4226 } 4227 4228 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, 4229 ModuleKind Type, 4230 SourceLocation ImportLoc, 4231 unsigned ClientLoadCapabilities, 4232 SmallVectorImpl<ImportedSubmodule> *Imported) { 4233 llvm::SaveAndRestore<SourceLocation> 4234 SetCurImportLocRAII(CurrentImportLoc, ImportLoc); 4235 llvm::SaveAndRestore<Optional<ModuleKind>> SetCurModuleKindRAII( 4236 CurrentDeserializingModuleKind, Type); 4237 4238 // Defer any pending actions until we get to the end of reading the AST file. 4239 Deserializing AnASTFile(this); 4240 4241 // Bump the generation number. 4242 unsigned PreviousGeneration = 0; 4243 if (ContextObj) 4244 PreviousGeneration = incrementGeneration(*ContextObj); 4245 4246 unsigned NumModules = ModuleMgr.size(); 4247 SmallVector<ImportedModule, 4> Loaded; 4248 if (ASTReadResult ReadResult = 4249 ReadASTCore(FileName, Type, ImportLoc, 4250 /*ImportedBy=*/nullptr, Loaded, 0, 0, ASTFileSignature(), 4251 ClientLoadCapabilities)) { 4252 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, 4253 PP.getLangOpts().Modules 4254 ? &PP.getHeaderSearchInfo().getModuleMap() 4255 : nullptr); 4256 4257 // If we find that any modules are unusable, the global index is going 4258 // to be out-of-date. Just remove it. 4259 GlobalIndex.reset(); 4260 ModuleMgr.setGlobalIndex(nullptr); 4261 return ReadResult; 4262 } 4263 4264 // Here comes stuff that we only do once the entire chain is loaded. Do *not* 4265 // remove modules from this point. Various fields are updated during reading 4266 // the AST block and removing the modules would result in dangling pointers. 4267 // They are generally only incidentally dereferenced, ie. a binary search 4268 // runs over `GlobalSLocEntryMap`, which could cause an invalid module to 4269 // be dereferenced but it wouldn't actually be used. 4270 4271 // Load the AST blocks of all of the modules that we loaded. We can still 4272 // hit errors parsing the ASTs at this point. 4273 for (ImportedModule &M : Loaded) { 4274 ModuleFile &F = *M.Mod; 4275 4276 // Read the AST block. 4277 if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) { 4278 Error(std::move(Err)); 4279 return Failure; 4280 } 4281 4282 // The AST block should always have a definition for the main module. 4283 if (F.isModule() && !F.DidReadTopLevelSubmodule) { 4284 Error(diag::err_module_file_missing_top_level_submodule, F.FileName); 4285 return Failure; 4286 } 4287 4288 // Read the extension blocks. 4289 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) { 4290 if (llvm::Error Err = ReadExtensionBlock(F)) { 4291 Error(std::move(Err)); 4292 return Failure; 4293 } 4294 } 4295 4296 // Once read, set the ModuleFile bit base offset and update the size in 4297 // bits of all files we've seen. 4298 F.GlobalBitOffset = TotalModulesSizeInBits; 4299 TotalModulesSizeInBits += F.SizeInBits; 4300 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F)); 4301 } 4302 4303 // Preload source locations and interesting indentifiers. 4304 for (ImportedModule &M : Loaded) { 4305 ModuleFile &F = *M.Mod; 4306 4307 // Preload SLocEntries. 4308 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) { 4309 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID; 4310 // Load it through the SourceManager and don't call ReadSLocEntry() 4311 // directly because the entry may have already been loaded in which case 4312 // calling ReadSLocEntry() directly would trigger an assertion in 4313 // SourceManager. 4314 SourceMgr.getLoadedSLocEntryByID(Index); 4315 } 4316 4317 // Map the original source file ID into the ID space of the current 4318 // compilation. 4319 if (F.OriginalSourceFileID.isValid()) { 4320 F.OriginalSourceFileID = FileID::get( 4321 F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1); 4322 } 4323 4324 // Preload all the pending interesting identifiers by marking them out of 4325 // date. 4326 for (auto Offset : F.PreloadIdentifierOffsets) { 4327 const unsigned char *Data = F.IdentifierTableData + Offset; 4328 4329 ASTIdentifierLookupTrait Trait(*this, F); 4330 auto KeyDataLen = Trait.ReadKeyDataLength(Data); 4331 auto Key = Trait.ReadKey(Data, KeyDataLen.first); 4332 auto &II = PP.getIdentifierTable().getOwn(Key); 4333 II.setOutOfDate(true); 4334 4335 // Mark this identifier as being from an AST file so that we can track 4336 // whether we need to serialize it. 4337 markIdentifierFromAST(*this, II); 4338 4339 // Associate the ID with the identifier so that the writer can reuse it. 4340 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first); 4341 SetIdentifierInfo(ID, &II); 4342 } 4343 } 4344 4345 // Setup the import locations and notify the module manager that we've 4346 // committed to these module files. 4347 for (ImportedModule &M : Loaded) { 4348 ModuleFile &F = *M.Mod; 4349 4350 ModuleMgr.moduleFileAccepted(&F); 4351 4352 // Set the import location. 4353 F.DirectImportLoc = ImportLoc; 4354 // FIXME: We assume that locations from PCH / preamble do not need 4355 // any translation. 4356 if (!M.ImportedBy) 4357 F.ImportLoc = M.ImportLoc; 4358 else 4359 F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc); 4360 } 4361 4362 if (!PP.getLangOpts().CPlusPlus || 4363 (Type != MK_ImplicitModule && Type != MK_ExplicitModule && 4364 Type != MK_PrebuiltModule)) { 4365 // Mark all of the identifiers in the identifier table as being out of date, 4366 // so that various accessors know to check the loaded modules when the 4367 // identifier is used. 4368 // 4369 // For C++ modules, we don't need information on many identifiers (just 4370 // those that provide macros or are poisoned), so we mark all of 4371 // the interesting ones via PreloadIdentifierOffsets. 4372 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(), 4373 IdEnd = PP.getIdentifierTable().end(); 4374 Id != IdEnd; ++Id) 4375 Id->second->setOutOfDate(true); 4376 } 4377 // Mark selectors as out of date. 4378 for (auto Sel : SelectorGeneration) 4379 SelectorOutOfDate[Sel.first] = true; 4380 4381 // Resolve any unresolved module exports. 4382 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) { 4383 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I]; 4384 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID); 4385 Module *ResolvedMod = getSubmodule(GlobalID); 4386 4387 switch (Unresolved.Kind) { 4388 case UnresolvedModuleRef::Conflict: 4389 if (ResolvedMod) { 4390 Module::Conflict Conflict; 4391 Conflict.Other = ResolvedMod; 4392 Conflict.Message = Unresolved.String.str(); 4393 Unresolved.Mod->Conflicts.push_back(Conflict); 4394 } 4395 continue; 4396 4397 case UnresolvedModuleRef::Import: 4398 if (ResolvedMod) 4399 Unresolved.Mod->Imports.insert(ResolvedMod); 4400 continue; 4401 4402 case UnresolvedModuleRef::Export: 4403 if (ResolvedMod || Unresolved.IsWildcard) 4404 Unresolved.Mod->Exports.push_back( 4405 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard)); 4406 continue; 4407 } 4408 } 4409 UnresolvedModuleRefs.clear(); 4410 4411 if (Imported) 4412 Imported->append(ImportedModules.begin(), 4413 ImportedModules.end()); 4414 4415 // FIXME: How do we load the 'use'd modules? They may not be submodules. 4416 // Might be unnecessary as use declarations are only used to build the 4417 // module itself. 4418 4419 if (ContextObj) 4420 InitializeContext(); 4421 4422 if (SemaObj) 4423 UpdateSema(); 4424 4425 if (DeserializationListener) 4426 DeserializationListener->ReaderInitialized(this); 4427 4428 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule(); 4429 if (PrimaryModule.OriginalSourceFileID.isValid()) { 4430 // If this AST file is a precompiled preamble, then set the 4431 // preamble file ID of the source manager to the file source file 4432 // from which the preamble was built. 4433 if (Type == MK_Preamble) { 4434 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID); 4435 } else if (Type == MK_MainFile) { 4436 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID); 4437 } 4438 } 4439 4440 // For any Objective-C class definitions we have already loaded, make sure 4441 // that we load any additional categories. 4442 if (ContextObj) { 4443 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) { 4444 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(), 4445 ObjCClassesLoaded[I], 4446 PreviousGeneration); 4447 } 4448 } 4449 4450 if (PP.getHeaderSearchInfo() 4451 .getHeaderSearchOpts() 4452 .ModulesValidateOncePerBuildSession) { 4453 // Now we are certain that the module and all modules it depends on are 4454 // up to date. Create or update timestamp files for modules that are 4455 // located in the module cache (not for PCH files that could be anywhere 4456 // in the filesystem). 4457 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) { 4458 ImportedModule &M = Loaded[I]; 4459 if (M.Mod->Kind == MK_ImplicitModule) { 4460 updateModuleTimestamp(*M.Mod); 4461 } 4462 } 4463 } 4464 4465 return Success; 4466 } 4467 4468 static ASTFileSignature readASTFileSignature(StringRef PCH); 4469 4470 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'. 4471 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) { 4472 // FIXME checking magic headers is done in other places such as 4473 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't 4474 // always done the same. Unify it all with a helper. 4475 if (!Stream.canSkipToPos(4)) 4476 return llvm::createStringError(std::errc::illegal_byte_sequence, 4477 "file too small to contain AST file magic"); 4478 for (unsigned C : {'C', 'P', 'C', 'H'}) 4479 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) { 4480 if (Res.get() != C) 4481 return llvm::createStringError( 4482 std::errc::illegal_byte_sequence, 4483 "file doesn't start with AST file magic"); 4484 } else 4485 return Res.takeError(); 4486 return llvm::Error::success(); 4487 } 4488 4489 static unsigned moduleKindForDiagnostic(ModuleKind Kind) { 4490 switch (Kind) { 4491 case MK_PCH: 4492 return 0; // PCH 4493 case MK_ImplicitModule: 4494 case MK_ExplicitModule: 4495 case MK_PrebuiltModule: 4496 return 1; // module 4497 case MK_MainFile: 4498 case MK_Preamble: 4499 return 2; // main source file 4500 } 4501 llvm_unreachable("unknown module kind"); 4502 } 4503 4504 ASTReader::ASTReadResult 4505 ASTReader::ReadASTCore(StringRef FileName, 4506 ModuleKind Type, 4507 SourceLocation ImportLoc, 4508 ModuleFile *ImportedBy, 4509 SmallVectorImpl<ImportedModule> &Loaded, 4510 off_t ExpectedSize, time_t ExpectedModTime, 4511 ASTFileSignature ExpectedSignature, 4512 unsigned ClientLoadCapabilities) { 4513 ModuleFile *M; 4514 std::string ErrorStr; 4515 ModuleManager::AddModuleResult AddResult 4516 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy, 4517 getGeneration(), ExpectedSize, ExpectedModTime, 4518 ExpectedSignature, readASTFileSignature, 4519 M, ErrorStr); 4520 4521 switch (AddResult) { 4522 case ModuleManager::AlreadyLoaded: 4523 Diag(diag::remark_module_import) 4524 << M->ModuleName << M->FileName << (ImportedBy ? true : false) 4525 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 4526 return Success; 4527 4528 case ModuleManager::NewlyLoaded: 4529 // Load module file below. 4530 break; 4531 4532 case ModuleManager::Missing: 4533 // The module file was missing; if the client can handle that, return 4534 // it. 4535 if (ClientLoadCapabilities & ARR_Missing) 4536 return Missing; 4537 4538 // Otherwise, return an error. 4539 Diag(diag::err_ast_file_not_found) 4540 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty() 4541 << ErrorStr; 4542 return Failure; 4543 4544 case ModuleManager::OutOfDate: 4545 // We couldn't load the module file because it is out-of-date. If the 4546 // client can handle out-of-date, return it. 4547 if (ClientLoadCapabilities & ARR_OutOfDate) 4548 return OutOfDate; 4549 4550 // Otherwise, return an error. 4551 Diag(diag::err_ast_file_out_of_date) 4552 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty() 4553 << ErrorStr; 4554 return Failure; 4555 } 4556 4557 assert(M && "Missing module file"); 4558 4559 bool ShouldFinalizePCM = false; 4560 auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() { 4561 auto &MC = getModuleManager().getModuleCache(); 4562 if (ShouldFinalizePCM) 4563 MC.finalizePCM(FileName); 4564 else 4565 MC.tryToDropPCM(FileName); 4566 }); 4567 ModuleFile &F = *M; 4568 BitstreamCursor &Stream = F.Stream; 4569 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer)); 4570 F.SizeInBits = F.Buffer->getBufferSize() * 8; 4571 4572 // Sniff for the signature. 4573 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4574 Diag(diag::err_ast_file_invalid) 4575 << moduleKindForDiagnostic(Type) << FileName << std::move(Err); 4576 return Failure; 4577 } 4578 4579 // This is used for compatibility with older PCH formats. 4580 bool HaveReadControlBlock = false; 4581 while (true) { 4582 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4583 if (!MaybeEntry) { 4584 Error(MaybeEntry.takeError()); 4585 return Failure; 4586 } 4587 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4588 4589 switch (Entry.Kind) { 4590 case llvm::BitstreamEntry::Error: 4591 case llvm::BitstreamEntry::Record: 4592 case llvm::BitstreamEntry::EndBlock: 4593 Error("invalid record at top-level of AST file"); 4594 return Failure; 4595 4596 case llvm::BitstreamEntry::SubBlock: 4597 break; 4598 } 4599 4600 switch (Entry.ID) { 4601 case CONTROL_BLOCK_ID: 4602 HaveReadControlBlock = true; 4603 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) { 4604 case Success: 4605 // Check that we didn't try to load a non-module AST file as a module. 4606 // 4607 // FIXME: Should we also perform the converse check? Loading a module as 4608 // a PCH file sort of works, but it's a bit wonky. 4609 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule || 4610 Type == MK_PrebuiltModule) && 4611 F.ModuleName.empty()) { 4612 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure; 4613 if (Result != OutOfDate || 4614 (ClientLoadCapabilities & ARR_OutOfDate) == 0) 4615 Diag(diag::err_module_file_not_module) << FileName; 4616 return Result; 4617 } 4618 break; 4619 4620 case Failure: return Failure; 4621 case Missing: return Missing; 4622 case OutOfDate: return OutOfDate; 4623 case VersionMismatch: return VersionMismatch; 4624 case ConfigurationMismatch: return ConfigurationMismatch; 4625 case HadErrors: return HadErrors; 4626 } 4627 break; 4628 4629 case AST_BLOCK_ID: 4630 if (!HaveReadControlBlock) { 4631 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 4632 Diag(diag::err_pch_version_too_old); 4633 return VersionMismatch; 4634 } 4635 4636 // Record that we've loaded this module. 4637 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc)); 4638 ShouldFinalizePCM = true; 4639 return Success; 4640 4641 case UNHASHED_CONTROL_BLOCK_ID: 4642 // This block is handled using look-ahead during ReadControlBlock. We 4643 // shouldn't get here! 4644 Error("malformed block record in AST file"); 4645 return Failure; 4646 4647 default: 4648 if (llvm::Error Err = Stream.SkipBlock()) { 4649 Error(std::move(Err)); 4650 return Failure; 4651 } 4652 break; 4653 } 4654 } 4655 4656 llvm_unreachable("unexpected break; expected return"); 4657 } 4658 4659 ASTReader::ASTReadResult 4660 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy, 4661 unsigned ClientLoadCapabilities) { 4662 const HeaderSearchOptions &HSOpts = 4663 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 4664 bool AllowCompatibleConfigurationMismatch = 4665 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 4666 bool DisableValidation = shouldDisableValidationForFile(F); 4667 4668 ASTReadResult Result = readUnhashedControlBlockImpl( 4669 &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch, 4670 Listener.get(), 4671 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions); 4672 4673 // If F was directly imported by another module, it's implicitly validated by 4674 // the importing module. 4675 if (DisableValidation || WasImportedBy || 4676 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 4677 return Success; 4678 4679 if (Result == Failure) { 4680 Error("malformed block record in AST file"); 4681 return Failure; 4682 } 4683 4684 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) { 4685 // If this module has already been finalized in the ModuleCache, we're stuck 4686 // with it; we can only load a single version of each module. 4687 // 4688 // This can happen when a module is imported in two contexts: in one, as a 4689 // user module; in another, as a system module (due to an import from 4690 // another module marked with the [system] flag). It usually indicates a 4691 // bug in the module map: this module should also be marked with [system]. 4692 // 4693 // If -Wno-system-headers (the default), and the first import is as a 4694 // system module, then validation will fail during the as-user import, 4695 // since -Werror flags won't have been validated. However, it's reasonable 4696 // to treat this consistently as a system module. 4697 // 4698 // If -Wsystem-headers, the PCM on disk was built with 4699 // -Wno-system-headers, and the first import is as a user module, then 4700 // validation will fail during the as-system import since the PCM on disk 4701 // doesn't guarantee that -Werror was respected. However, the -Werror 4702 // flags were checked during the initial as-user import. 4703 if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) { 4704 Diag(diag::warn_module_system_bit_conflict) << F.FileName; 4705 return Success; 4706 } 4707 } 4708 4709 return Result; 4710 } 4711 4712 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl( 4713 ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities, 4714 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener, 4715 bool ValidateDiagnosticOptions) { 4716 // Initialize a stream. 4717 BitstreamCursor Stream(StreamData); 4718 4719 // Sniff for the signature. 4720 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4721 // FIXME this drops the error on the floor. 4722 consumeError(std::move(Err)); 4723 return Failure; 4724 } 4725 4726 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 4727 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 4728 return Failure; 4729 4730 // Read all of the records in the options block. 4731 RecordData Record; 4732 ASTReadResult Result = Success; 4733 while (true) { 4734 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4735 if (!MaybeEntry) { 4736 // FIXME this drops the error on the floor. 4737 consumeError(MaybeEntry.takeError()); 4738 return Failure; 4739 } 4740 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4741 4742 switch (Entry.Kind) { 4743 case llvm::BitstreamEntry::Error: 4744 case llvm::BitstreamEntry::SubBlock: 4745 return Failure; 4746 4747 case llvm::BitstreamEntry::EndBlock: 4748 return Result; 4749 4750 case llvm::BitstreamEntry::Record: 4751 // The interesting case. 4752 break; 4753 } 4754 4755 // Read and process a record. 4756 Record.clear(); 4757 StringRef Blob; 4758 Expected<unsigned> MaybeRecordType = 4759 Stream.readRecord(Entry.ID, Record, &Blob); 4760 if (!MaybeRecordType) { 4761 // FIXME this drops the error. 4762 return Failure; 4763 } 4764 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) { 4765 case SIGNATURE: 4766 if (F) 4767 F->Signature = ASTFileSignature::create(Record.begin(), Record.end()); 4768 break; 4769 case AST_BLOCK_HASH: 4770 if (F) 4771 F->ASTBlockHash = 4772 ASTFileSignature::create(Record.begin(), Record.end()); 4773 break; 4774 case DIAGNOSTIC_OPTIONS: { 4775 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 4776 if (Listener && ValidateDiagnosticOptions && 4777 !AllowCompatibleConfigurationMismatch && 4778 ParseDiagnosticOptions(Record, Complain, *Listener)) 4779 Result = OutOfDate; // Don't return early. Read the signature. 4780 break; 4781 } 4782 case DIAG_PRAGMA_MAPPINGS: 4783 if (!F) 4784 break; 4785 if (F->PragmaDiagMappings.empty()) 4786 F->PragmaDiagMappings.swap(Record); 4787 else 4788 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(), 4789 Record.begin(), Record.end()); 4790 break; 4791 case HEADER_SEARCH_ENTRY_USAGE: 4792 if (!F) 4793 break; 4794 unsigned Count = Record[0]; 4795 const char *Byte = Blob.data(); 4796 F->SearchPathUsage = llvm::BitVector(Count, false); 4797 for (unsigned I = 0; I < Count; ++Byte) 4798 for (unsigned Bit = 0; Bit < 8 && I < Count; ++Bit, ++I) 4799 if (*Byte & (1 << Bit)) 4800 F->SearchPathUsage[I] = true; 4801 break; 4802 } 4803 } 4804 } 4805 4806 /// Parse a record and blob containing module file extension metadata. 4807 static bool parseModuleFileExtensionMetadata( 4808 const SmallVectorImpl<uint64_t> &Record, 4809 StringRef Blob, 4810 ModuleFileExtensionMetadata &Metadata) { 4811 if (Record.size() < 4) return true; 4812 4813 Metadata.MajorVersion = Record[0]; 4814 Metadata.MinorVersion = Record[1]; 4815 4816 unsigned BlockNameLen = Record[2]; 4817 unsigned UserInfoLen = Record[3]; 4818 4819 if (BlockNameLen + UserInfoLen > Blob.size()) return true; 4820 4821 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen); 4822 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen, 4823 Blob.data() + BlockNameLen + UserInfoLen); 4824 return false; 4825 } 4826 4827 llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) { 4828 BitstreamCursor &Stream = F.Stream; 4829 4830 RecordData Record; 4831 while (true) { 4832 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4833 if (!MaybeEntry) 4834 return MaybeEntry.takeError(); 4835 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4836 4837 switch (Entry.Kind) { 4838 case llvm::BitstreamEntry::SubBlock: 4839 if (llvm::Error Err = Stream.SkipBlock()) 4840 return Err; 4841 continue; 4842 case llvm::BitstreamEntry::EndBlock: 4843 return llvm::Error::success(); 4844 case llvm::BitstreamEntry::Error: 4845 return llvm::createStringError(std::errc::illegal_byte_sequence, 4846 "malformed block record in AST file"); 4847 case llvm::BitstreamEntry::Record: 4848 break; 4849 } 4850 4851 Record.clear(); 4852 StringRef Blob; 4853 Expected<unsigned> MaybeRecCode = 4854 Stream.readRecord(Entry.ID, Record, &Blob); 4855 if (!MaybeRecCode) 4856 return MaybeRecCode.takeError(); 4857 switch (MaybeRecCode.get()) { 4858 case EXTENSION_METADATA: { 4859 ModuleFileExtensionMetadata Metadata; 4860 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) 4861 return llvm::createStringError( 4862 std::errc::illegal_byte_sequence, 4863 "malformed EXTENSION_METADATA in AST file"); 4864 4865 // Find a module file extension with this block name. 4866 auto Known = ModuleFileExtensions.find(Metadata.BlockName); 4867 if (Known == ModuleFileExtensions.end()) break; 4868 4869 // Form a reader. 4870 if (auto Reader = Known->second->createExtensionReader(Metadata, *this, 4871 F, Stream)) { 4872 F.ExtensionReaders.push_back(std::move(Reader)); 4873 } 4874 4875 break; 4876 } 4877 } 4878 } 4879 4880 return llvm::Error::success(); 4881 } 4882 4883 void ASTReader::InitializeContext() { 4884 assert(ContextObj && "no context to initialize"); 4885 ASTContext &Context = *ContextObj; 4886 4887 // If there's a listener, notify them that we "read" the translation unit. 4888 if (DeserializationListener) 4889 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, 4890 Context.getTranslationUnitDecl()); 4891 4892 // FIXME: Find a better way to deal with collisions between these 4893 // built-in types. Right now, we just ignore the problem. 4894 4895 // Load the special types. 4896 if (SpecialTypes.size() >= NumSpecialTypeIDs) { 4897 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) { 4898 if (!Context.CFConstantStringTypeDecl) 4899 Context.setCFConstantStringType(GetType(String)); 4900 } 4901 4902 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) { 4903 QualType FileType = GetType(File); 4904 if (FileType.isNull()) { 4905 Error("FILE type is NULL"); 4906 return; 4907 } 4908 4909 if (!Context.FILEDecl) { 4910 if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) 4911 Context.setFILEDecl(Typedef->getDecl()); 4912 else { 4913 const TagType *Tag = FileType->getAs<TagType>(); 4914 if (!Tag) { 4915 Error("Invalid FILE type in AST file"); 4916 return; 4917 } 4918 Context.setFILEDecl(Tag->getDecl()); 4919 } 4920 } 4921 } 4922 4923 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) { 4924 QualType Jmp_bufType = GetType(Jmp_buf); 4925 if (Jmp_bufType.isNull()) { 4926 Error("jmp_buf type is NULL"); 4927 return; 4928 } 4929 4930 if (!Context.jmp_bufDecl) { 4931 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) 4932 Context.setjmp_bufDecl(Typedef->getDecl()); 4933 else { 4934 const TagType *Tag = Jmp_bufType->getAs<TagType>(); 4935 if (!Tag) { 4936 Error("Invalid jmp_buf type in AST file"); 4937 return; 4938 } 4939 Context.setjmp_bufDecl(Tag->getDecl()); 4940 } 4941 } 4942 } 4943 4944 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) { 4945 QualType Sigjmp_bufType = GetType(Sigjmp_buf); 4946 if (Sigjmp_bufType.isNull()) { 4947 Error("sigjmp_buf type is NULL"); 4948 return; 4949 } 4950 4951 if (!Context.sigjmp_bufDecl) { 4952 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) 4953 Context.setsigjmp_bufDecl(Typedef->getDecl()); 4954 else { 4955 const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); 4956 assert(Tag && "Invalid sigjmp_buf type in AST file"); 4957 Context.setsigjmp_bufDecl(Tag->getDecl()); 4958 } 4959 } 4960 } 4961 4962 if (unsigned ObjCIdRedef 4963 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) { 4964 if (Context.ObjCIdRedefinitionType.isNull()) 4965 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef); 4966 } 4967 4968 if (unsigned ObjCClassRedef 4969 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) { 4970 if (Context.ObjCClassRedefinitionType.isNull()) 4971 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef); 4972 } 4973 4974 if (unsigned ObjCSelRedef 4975 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) { 4976 if (Context.ObjCSelRedefinitionType.isNull()) 4977 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef); 4978 } 4979 4980 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) { 4981 QualType Ucontext_tType = GetType(Ucontext_t); 4982 if (Ucontext_tType.isNull()) { 4983 Error("ucontext_t type is NULL"); 4984 return; 4985 } 4986 4987 if (!Context.ucontext_tDecl) { 4988 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>()) 4989 Context.setucontext_tDecl(Typedef->getDecl()); 4990 else { 4991 const TagType *Tag = Ucontext_tType->getAs<TagType>(); 4992 assert(Tag && "Invalid ucontext_t type in AST file"); 4993 Context.setucontext_tDecl(Tag->getDecl()); 4994 } 4995 } 4996 } 4997 } 4998 4999 ReadPragmaDiagnosticMappings(Context.getDiagnostics()); 5000 5001 // If there were any CUDA special declarations, deserialize them. 5002 if (!CUDASpecialDeclRefs.empty()) { 5003 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!"); 5004 Context.setcudaConfigureCallDecl( 5005 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0]))); 5006 } 5007 5008 // Re-export any modules that were imported by a non-module AST file. 5009 // FIXME: This does not make macro-only imports visible again. 5010 for (auto &Import : ImportedModules) { 5011 if (Module *Imported = getSubmodule(Import.ID)) { 5012 makeModuleVisible(Imported, Module::AllVisible, 5013 /*ImportLoc=*/Import.ImportLoc); 5014 if (Import.ImportLoc.isValid()) 5015 PP.makeModuleVisible(Imported, Import.ImportLoc); 5016 // This updates visibility for Preprocessor only. For Sema, which can be 5017 // nullptr here, we do the same later, in UpdateSema(). 5018 } 5019 } 5020 } 5021 5022 void ASTReader::finalizeForWriting() { 5023 // Nothing to do for now. 5024 } 5025 5026 /// Reads and return the signature record from \p PCH's control block, or 5027 /// else returns 0. 5028 static ASTFileSignature readASTFileSignature(StringRef PCH) { 5029 BitstreamCursor Stream(PCH); 5030 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5031 // FIXME this drops the error on the floor. 5032 consumeError(std::move(Err)); 5033 return ASTFileSignature(); 5034 } 5035 5036 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 5037 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 5038 return ASTFileSignature(); 5039 5040 // Scan for SIGNATURE inside the diagnostic options block. 5041 ASTReader::RecordData Record; 5042 while (true) { 5043 Expected<llvm::BitstreamEntry> MaybeEntry = 5044 Stream.advanceSkippingSubblocks(); 5045 if (!MaybeEntry) { 5046 // FIXME this drops the error on the floor. 5047 consumeError(MaybeEntry.takeError()); 5048 return ASTFileSignature(); 5049 } 5050 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5051 5052 if (Entry.Kind != llvm::BitstreamEntry::Record) 5053 return ASTFileSignature(); 5054 5055 Record.clear(); 5056 StringRef Blob; 5057 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 5058 if (!MaybeRecord) { 5059 // FIXME this drops the error on the floor. 5060 consumeError(MaybeRecord.takeError()); 5061 return ASTFileSignature(); 5062 } 5063 if (SIGNATURE == MaybeRecord.get()) 5064 return ASTFileSignature::create(Record.begin(), 5065 Record.begin() + ASTFileSignature::size); 5066 } 5067 } 5068 5069 /// Retrieve the name of the original source file name 5070 /// directly from the AST file, without actually loading the AST 5071 /// file. 5072 std::string ASTReader::getOriginalSourceFile( 5073 const std::string &ASTFileName, FileManager &FileMgr, 5074 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) { 5075 // Open the AST file. 5076 auto Buffer = FileMgr.getBufferForFile(ASTFileName, /*IsVolatile=*/false, 5077 /*RequiresNullTerminator=*/false); 5078 if (!Buffer) { 5079 Diags.Report(diag::err_fe_unable_to_read_pch_file) 5080 << ASTFileName << Buffer.getError().message(); 5081 return std::string(); 5082 } 5083 5084 // Initialize the stream 5085 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer)); 5086 5087 // Sniff for the signature. 5088 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5089 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err); 5090 return std::string(); 5091 } 5092 5093 // Scan for the CONTROL_BLOCK_ID block. 5094 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) { 5095 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 5096 return std::string(); 5097 } 5098 5099 // Scan for ORIGINAL_FILE inside the control block. 5100 RecordData Record; 5101 while (true) { 5102 Expected<llvm::BitstreamEntry> MaybeEntry = 5103 Stream.advanceSkippingSubblocks(); 5104 if (!MaybeEntry) { 5105 // FIXME this drops errors on the floor. 5106 consumeError(MaybeEntry.takeError()); 5107 return std::string(); 5108 } 5109 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5110 5111 if (Entry.Kind == llvm::BitstreamEntry::EndBlock) 5112 return std::string(); 5113 5114 if (Entry.Kind != llvm::BitstreamEntry::Record) { 5115 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 5116 return std::string(); 5117 } 5118 5119 Record.clear(); 5120 StringRef Blob; 5121 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 5122 if (!MaybeRecord) { 5123 // FIXME this drops the errors on the floor. 5124 consumeError(MaybeRecord.takeError()); 5125 return std::string(); 5126 } 5127 if (ORIGINAL_FILE == MaybeRecord.get()) 5128 return Blob.str(); 5129 } 5130 } 5131 5132 namespace { 5133 5134 class SimplePCHValidator : public ASTReaderListener { 5135 const LangOptions &ExistingLangOpts; 5136 const TargetOptions &ExistingTargetOpts; 5137 const PreprocessorOptions &ExistingPPOpts; 5138 std::string ExistingModuleCachePath; 5139 FileManager &FileMgr; 5140 5141 public: 5142 SimplePCHValidator(const LangOptions &ExistingLangOpts, 5143 const TargetOptions &ExistingTargetOpts, 5144 const PreprocessorOptions &ExistingPPOpts, 5145 StringRef ExistingModuleCachePath, FileManager &FileMgr) 5146 : ExistingLangOpts(ExistingLangOpts), 5147 ExistingTargetOpts(ExistingTargetOpts), 5148 ExistingPPOpts(ExistingPPOpts), 5149 ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {} 5150 5151 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain, 5152 bool AllowCompatibleDifferences) override { 5153 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr, 5154 AllowCompatibleDifferences); 5155 } 5156 5157 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain, 5158 bool AllowCompatibleDifferences) override { 5159 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr, 5160 AllowCompatibleDifferences); 5161 } 5162 5163 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 5164 StringRef SpecificModuleCachePath, 5165 bool Complain) override { 5166 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5167 ExistingModuleCachePath, nullptr, 5168 ExistingLangOpts, ExistingPPOpts); 5169 } 5170 5171 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 5172 bool Complain, 5173 std::string &SuggestedPredefines) override { 5174 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr, 5175 SuggestedPredefines, ExistingLangOpts); 5176 } 5177 }; 5178 5179 } // namespace 5180 5181 bool ASTReader::readASTFileControlBlock( 5182 StringRef Filename, FileManager &FileMgr, 5183 const PCHContainerReader &PCHContainerRdr, 5184 bool FindModuleFileExtensions, 5185 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) { 5186 // Open the AST file. 5187 // FIXME: This allows use of the VFS; we do not allow use of the 5188 // VFS when actually loading a module. 5189 auto Buffer = FileMgr.getBufferForFile(Filename); 5190 if (!Buffer) { 5191 return true; 5192 } 5193 5194 // Initialize the stream 5195 StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer); 5196 BitstreamCursor Stream(Bytes); 5197 5198 // Sniff for the signature. 5199 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5200 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 5201 return true; 5202 } 5203 5204 // Scan for the CONTROL_BLOCK_ID block. 5205 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) 5206 return true; 5207 5208 bool NeedsInputFiles = Listener.needsInputFileVisitation(); 5209 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation(); 5210 bool NeedsImports = Listener.needsImportVisitation(); 5211 BitstreamCursor InputFilesCursor; 5212 5213 RecordData Record; 5214 std::string ModuleDir; 5215 bool DoneWithControlBlock = false; 5216 while (!DoneWithControlBlock) { 5217 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5218 if (!MaybeEntry) { 5219 // FIXME this drops the error on the floor. 5220 consumeError(MaybeEntry.takeError()); 5221 return true; 5222 } 5223 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5224 5225 switch (Entry.Kind) { 5226 case llvm::BitstreamEntry::SubBlock: { 5227 switch (Entry.ID) { 5228 case OPTIONS_BLOCK_ID: { 5229 std::string IgnoredSuggestedPredefines; 5230 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate, 5231 /*AllowCompatibleConfigurationMismatch*/ false, 5232 Listener, IgnoredSuggestedPredefines) != Success) 5233 return true; 5234 break; 5235 } 5236 5237 case INPUT_FILES_BLOCK_ID: 5238 InputFilesCursor = Stream; 5239 if (llvm::Error Err = Stream.SkipBlock()) { 5240 // FIXME this drops the error on the floor. 5241 consumeError(std::move(Err)); 5242 return true; 5243 } 5244 if (NeedsInputFiles && 5245 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID)) 5246 return true; 5247 break; 5248 5249 default: 5250 if (llvm::Error Err = Stream.SkipBlock()) { 5251 // FIXME this drops the error on the floor. 5252 consumeError(std::move(Err)); 5253 return true; 5254 } 5255 break; 5256 } 5257 5258 continue; 5259 } 5260 5261 case llvm::BitstreamEntry::EndBlock: 5262 DoneWithControlBlock = true; 5263 break; 5264 5265 case llvm::BitstreamEntry::Error: 5266 return true; 5267 5268 case llvm::BitstreamEntry::Record: 5269 break; 5270 } 5271 5272 if (DoneWithControlBlock) break; 5273 5274 Record.clear(); 5275 StringRef Blob; 5276 Expected<unsigned> MaybeRecCode = 5277 Stream.readRecord(Entry.ID, Record, &Blob); 5278 if (!MaybeRecCode) { 5279 // FIXME this drops the error. 5280 return Failure; 5281 } 5282 switch ((ControlRecordTypes)MaybeRecCode.get()) { 5283 case METADATA: 5284 if (Record[0] != VERSION_MAJOR) 5285 return true; 5286 if (Listener.ReadFullVersionInformation(Blob)) 5287 return true; 5288 break; 5289 case MODULE_NAME: 5290 Listener.ReadModuleName(Blob); 5291 break; 5292 case MODULE_DIRECTORY: 5293 ModuleDir = std::string(Blob); 5294 break; 5295 case MODULE_MAP_FILE: { 5296 unsigned Idx = 0; 5297 auto Path = ReadString(Record, Idx); 5298 ResolveImportedPath(Path, ModuleDir); 5299 Listener.ReadModuleMapFile(Path); 5300 break; 5301 } 5302 case INPUT_FILE_OFFSETS: { 5303 if (!NeedsInputFiles) 5304 break; 5305 5306 unsigned NumInputFiles = Record[0]; 5307 unsigned NumUserFiles = Record[1]; 5308 const llvm::support::unaligned_uint64_t *InputFileOffs = 5309 (const llvm::support::unaligned_uint64_t *)Blob.data(); 5310 for (unsigned I = 0; I != NumInputFiles; ++I) { 5311 // Go find this input file. 5312 bool isSystemFile = I >= NumUserFiles; 5313 5314 if (isSystemFile && !NeedsSystemInputFiles) 5315 break; // the rest are system input files 5316 5317 BitstreamCursor &Cursor = InputFilesCursor; 5318 SavedStreamPosition SavedPosition(Cursor); 5319 if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) { 5320 // FIXME this drops errors on the floor. 5321 consumeError(std::move(Err)); 5322 } 5323 5324 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 5325 if (!MaybeCode) { 5326 // FIXME this drops errors on the floor. 5327 consumeError(MaybeCode.takeError()); 5328 } 5329 unsigned Code = MaybeCode.get(); 5330 5331 RecordData Record; 5332 StringRef Blob; 5333 bool shouldContinue = false; 5334 Expected<unsigned> MaybeRecordType = 5335 Cursor.readRecord(Code, Record, &Blob); 5336 if (!MaybeRecordType) { 5337 // FIXME this drops errors on the floor. 5338 consumeError(MaybeRecordType.takeError()); 5339 } 5340 switch ((InputFileRecordTypes)MaybeRecordType.get()) { 5341 case INPUT_FILE_HASH: 5342 break; 5343 case INPUT_FILE: 5344 bool Overridden = static_cast<bool>(Record[3]); 5345 std::string Filename = std::string(Blob); 5346 ResolveImportedPath(Filename, ModuleDir); 5347 shouldContinue = Listener.visitInputFile( 5348 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false); 5349 break; 5350 } 5351 if (!shouldContinue) 5352 break; 5353 } 5354 break; 5355 } 5356 5357 case IMPORTS: { 5358 if (!NeedsImports) 5359 break; 5360 5361 unsigned Idx = 0, N = Record.size(); 5362 while (Idx < N) { 5363 // Read information about the AST file. 5364 Idx += 5365 1 + 1 + 1 + 1 + 5366 ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature 5367 std::string ModuleName = ReadString(Record, Idx); 5368 std::string Filename = ReadString(Record, Idx); 5369 ResolveImportedPath(Filename, ModuleDir); 5370 Listener.visitImport(ModuleName, Filename); 5371 } 5372 break; 5373 } 5374 5375 default: 5376 // No other validation to perform. 5377 break; 5378 } 5379 } 5380 5381 // Look for module file extension blocks, if requested. 5382 if (FindModuleFileExtensions) { 5383 BitstreamCursor SavedStream = Stream; 5384 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) { 5385 bool DoneWithExtensionBlock = false; 5386 while (!DoneWithExtensionBlock) { 5387 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5388 if (!MaybeEntry) { 5389 // FIXME this drops the error. 5390 return true; 5391 } 5392 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5393 5394 switch (Entry.Kind) { 5395 case llvm::BitstreamEntry::SubBlock: 5396 if (llvm::Error Err = Stream.SkipBlock()) { 5397 // FIXME this drops the error on the floor. 5398 consumeError(std::move(Err)); 5399 return true; 5400 } 5401 continue; 5402 5403 case llvm::BitstreamEntry::EndBlock: 5404 DoneWithExtensionBlock = true; 5405 continue; 5406 5407 case llvm::BitstreamEntry::Error: 5408 return true; 5409 5410 case llvm::BitstreamEntry::Record: 5411 break; 5412 } 5413 5414 Record.clear(); 5415 StringRef Blob; 5416 Expected<unsigned> MaybeRecCode = 5417 Stream.readRecord(Entry.ID, Record, &Blob); 5418 if (!MaybeRecCode) { 5419 // FIXME this drops the error. 5420 return true; 5421 } 5422 switch (MaybeRecCode.get()) { 5423 case EXTENSION_METADATA: { 5424 ModuleFileExtensionMetadata Metadata; 5425 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) 5426 return true; 5427 5428 Listener.readModuleFileExtension(Metadata); 5429 break; 5430 } 5431 } 5432 } 5433 } 5434 Stream = SavedStream; 5435 } 5436 5437 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 5438 if (readUnhashedControlBlockImpl( 5439 nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate, 5440 /*AllowCompatibleConfigurationMismatch*/ false, &Listener, 5441 ValidateDiagnosticOptions) != Success) 5442 return true; 5443 5444 return false; 5445 } 5446 5447 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr, 5448 const PCHContainerReader &PCHContainerRdr, 5449 const LangOptions &LangOpts, 5450 const TargetOptions &TargetOpts, 5451 const PreprocessorOptions &PPOpts, 5452 StringRef ExistingModuleCachePath) { 5453 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, 5454 ExistingModuleCachePath, FileMgr); 5455 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr, 5456 /*FindModuleFileExtensions=*/false, 5457 validator, 5458 /*ValidateDiagnosticOptions=*/true); 5459 } 5460 5461 llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F, 5462 unsigned ClientLoadCapabilities) { 5463 // Enter the submodule block. 5464 if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) 5465 return Err; 5466 5467 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap(); 5468 bool First = true; 5469 Module *CurrentModule = nullptr; 5470 RecordData Record; 5471 while (true) { 5472 Expected<llvm::BitstreamEntry> MaybeEntry = 5473 F.Stream.advanceSkippingSubblocks(); 5474 if (!MaybeEntry) 5475 return MaybeEntry.takeError(); 5476 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5477 5478 switch (Entry.Kind) { 5479 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 5480 case llvm::BitstreamEntry::Error: 5481 return llvm::createStringError(std::errc::illegal_byte_sequence, 5482 "malformed block record in AST file"); 5483 case llvm::BitstreamEntry::EndBlock: 5484 return llvm::Error::success(); 5485 case llvm::BitstreamEntry::Record: 5486 // The interesting case. 5487 break; 5488 } 5489 5490 // Read a record. 5491 StringRef Blob; 5492 Record.clear(); 5493 Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob); 5494 if (!MaybeKind) 5495 return MaybeKind.takeError(); 5496 unsigned Kind = MaybeKind.get(); 5497 5498 if ((Kind == SUBMODULE_METADATA) != First) 5499 return llvm::createStringError( 5500 std::errc::illegal_byte_sequence, 5501 "submodule metadata record should be at beginning of block"); 5502 First = false; 5503 5504 // Submodule information is only valid if we have a current module. 5505 // FIXME: Should we error on these cases? 5506 if (!CurrentModule && Kind != SUBMODULE_METADATA && 5507 Kind != SUBMODULE_DEFINITION) 5508 continue; 5509 5510 switch (Kind) { 5511 default: // Default behavior: ignore. 5512 break; 5513 5514 case SUBMODULE_DEFINITION: { 5515 if (Record.size() < 12) 5516 return llvm::createStringError(std::errc::illegal_byte_sequence, 5517 "malformed module definition"); 5518 5519 StringRef Name = Blob; 5520 unsigned Idx = 0; 5521 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]); 5522 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]); 5523 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++]; 5524 bool IsFramework = Record[Idx++]; 5525 bool IsExplicit = Record[Idx++]; 5526 bool IsSystem = Record[Idx++]; 5527 bool IsExternC = Record[Idx++]; 5528 bool InferSubmodules = Record[Idx++]; 5529 bool InferExplicitSubmodules = Record[Idx++]; 5530 bool InferExportWildcard = Record[Idx++]; 5531 bool ConfigMacrosExhaustive = Record[Idx++]; 5532 bool ModuleMapIsPrivate = Record[Idx++]; 5533 5534 Module *ParentModule = nullptr; 5535 if (Parent) 5536 ParentModule = getSubmodule(Parent); 5537 5538 // Retrieve this (sub)module from the module map, creating it if 5539 // necessary. 5540 CurrentModule = 5541 ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit) 5542 .first; 5543 5544 // FIXME: set the definition loc for CurrentModule, or call 5545 // ModMap.setInferredModuleAllowedBy() 5546 5547 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS; 5548 if (GlobalIndex >= SubmodulesLoaded.size() || 5549 SubmodulesLoaded[GlobalIndex]) 5550 return llvm::createStringError(std::errc::invalid_argument, 5551 "too many submodules"); 5552 5553 if (!ParentModule) { 5554 if (const FileEntry *CurFile = CurrentModule->getASTFile()) { 5555 // Don't emit module relocation error if we have -fno-validate-pch 5556 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation & 5557 DisableValidationForModuleKind::Module) && 5558 CurFile != F.File) { 5559 auto ConflictError = 5560 PartialDiagnostic(diag::err_module_file_conflict, 5561 ContextObj->DiagAllocator) 5562 << CurrentModule->getTopLevelModuleName() << CurFile->getName() 5563 << F.File->getName(); 5564 return DiagnosticError::create(CurrentImportLoc, ConflictError); 5565 } 5566 } 5567 5568 F.DidReadTopLevelSubmodule = true; 5569 CurrentModule->setASTFile(F.File); 5570 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath; 5571 } 5572 5573 CurrentModule->Kind = Kind; 5574 CurrentModule->Signature = F.Signature; 5575 CurrentModule->IsFromModuleFile = true; 5576 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem; 5577 CurrentModule->IsExternC = IsExternC; 5578 CurrentModule->InferSubmodules = InferSubmodules; 5579 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules; 5580 CurrentModule->InferExportWildcard = InferExportWildcard; 5581 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive; 5582 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate; 5583 if (DeserializationListener) 5584 DeserializationListener->ModuleRead(GlobalID, CurrentModule); 5585 5586 SubmodulesLoaded[GlobalIndex] = CurrentModule; 5587 5588 // Clear out data that will be replaced by what is in the module file. 5589 CurrentModule->LinkLibraries.clear(); 5590 CurrentModule->ConfigMacros.clear(); 5591 CurrentModule->UnresolvedConflicts.clear(); 5592 CurrentModule->Conflicts.clear(); 5593 5594 // The module is available unless it's missing a requirement; relevant 5595 // requirements will be (re-)added by SUBMODULE_REQUIRES records. 5596 // Missing headers that were present when the module was built do not 5597 // make it unavailable -- if we got this far, this must be an explicitly 5598 // imported module file. 5599 CurrentModule->Requirements.clear(); 5600 CurrentModule->MissingHeaders.clear(); 5601 CurrentModule->IsUnimportable = 5602 ParentModule && ParentModule->IsUnimportable; 5603 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable; 5604 break; 5605 } 5606 5607 case SUBMODULE_UMBRELLA_HEADER: { 5608 // FIXME: This doesn't work for framework modules as `Filename` is the 5609 // name as written in the module file and does not include 5610 // `Headers/`, so this path will never exist. 5611 std::string Filename = std::string(Blob); 5612 ResolveImportedPath(F, Filename); 5613 if (auto Umbrella = PP.getFileManager().getFile(Filename)) { 5614 if (!CurrentModule->getUmbrellaHeader()) { 5615 // FIXME: NameAsWritten 5616 ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob, ""); 5617 } 5618 // Note that it's too late at this point to return out of date if the 5619 // name from the PCM doesn't match up with the one in the module map, 5620 // but also quite unlikely since we will have already checked the 5621 // modification time and size of the module map file itself. 5622 } 5623 break; 5624 } 5625 5626 case SUBMODULE_HEADER: 5627 case SUBMODULE_EXCLUDED_HEADER: 5628 case SUBMODULE_PRIVATE_HEADER: 5629 // We lazily associate headers with their modules via the HeaderInfo table. 5630 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead 5631 // of complete filenames or remove it entirely. 5632 break; 5633 5634 case SUBMODULE_TEXTUAL_HEADER: 5635 case SUBMODULE_PRIVATE_TEXTUAL_HEADER: 5636 // FIXME: Textual headers are not marked in the HeaderInfo table. Load 5637 // them here. 5638 break; 5639 5640 case SUBMODULE_TOPHEADER: 5641 CurrentModule->addTopHeaderFilename(Blob); 5642 break; 5643 5644 case SUBMODULE_UMBRELLA_DIR: { 5645 // See comments in SUBMODULE_UMBRELLA_HEADER 5646 std::string Dirname = std::string(Blob); 5647 ResolveImportedPath(F, Dirname); 5648 if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) { 5649 if (!CurrentModule->getUmbrellaDir()) { 5650 // FIXME: NameAsWritten 5651 ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob, ""); 5652 } 5653 } 5654 break; 5655 } 5656 5657 case SUBMODULE_METADATA: { 5658 F.BaseSubmoduleID = getTotalNumSubmodules(); 5659 F.LocalNumSubmodules = Record[0]; 5660 unsigned LocalBaseSubmoduleID = Record[1]; 5661 if (F.LocalNumSubmodules > 0) { 5662 // Introduce the global -> local mapping for submodules within this 5663 // module. 5664 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F)); 5665 5666 // Introduce the local -> global mapping for submodules within this 5667 // module. 5668 F.SubmoduleRemap.insertOrReplace( 5669 std::make_pair(LocalBaseSubmoduleID, 5670 F.BaseSubmoduleID - LocalBaseSubmoduleID)); 5671 5672 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules); 5673 } 5674 break; 5675 } 5676 5677 case SUBMODULE_IMPORTS: 5678 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) { 5679 UnresolvedModuleRef Unresolved; 5680 Unresolved.File = &F; 5681 Unresolved.Mod = CurrentModule; 5682 Unresolved.ID = Record[Idx]; 5683 Unresolved.Kind = UnresolvedModuleRef::Import; 5684 Unresolved.IsWildcard = false; 5685 UnresolvedModuleRefs.push_back(Unresolved); 5686 } 5687 break; 5688 5689 case SUBMODULE_EXPORTS: 5690 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) { 5691 UnresolvedModuleRef Unresolved; 5692 Unresolved.File = &F; 5693 Unresolved.Mod = CurrentModule; 5694 Unresolved.ID = Record[Idx]; 5695 Unresolved.Kind = UnresolvedModuleRef::Export; 5696 Unresolved.IsWildcard = Record[Idx + 1]; 5697 UnresolvedModuleRefs.push_back(Unresolved); 5698 } 5699 5700 // Once we've loaded the set of exports, there's no reason to keep 5701 // the parsed, unresolved exports around. 5702 CurrentModule->UnresolvedExports.clear(); 5703 break; 5704 5705 case SUBMODULE_REQUIRES: 5706 CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(), 5707 PP.getTargetInfo()); 5708 break; 5709 5710 case SUBMODULE_LINK_LIBRARY: 5711 ModMap.resolveLinkAsDependencies(CurrentModule); 5712 CurrentModule->LinkLibraries.push_back( 5713 Module::LinkLibrary(std::string(Blob), Record[0])); 5714 break; 5715 5716 case SUBMODULE_CONFIG_MACRO: 5717 CurrentModule->ConfigMacros.push_back(Blob.str()); 5718 break; 5719 5720 case SUBMODULE_CONFLICT: { 5721 UnresolvedModuleRef Unresolved; 5722 Unresolved.File = &F; 5723 Unresolved.Mod = CurrentModule; 5724 Unresolved.ID = Record[0]; 5725 Unresolved.Kind = UnresolvedModuleRef::Conflict; 5726 Unresolved.IsWildcard = false; 5727 Unresolved.String = Blob; 5728 UnresolvedModuleRefs.push_back(Unresolved); 5729 break; 5730 } 5731 5732 case SUBMODULE_INITIALIZERS: { 5733 if (!ContextObj) 5734 break; 5735 SmallVector<uint32_t, 16> Inits; 5736 for (auto &ID : Record) 5737 Inits.push_back(getGlobalDeclID(F, ID)); 5738 ContextObj->addLazyModuleInitializers(CurrentModule, Inits); 5739 break; 5740 } 5741 5742 case SUBMODULE_EXPORT_AS: 5743 CurrentModule->ExportAsModule = Blob.str(); 5744 ModMap.addLinkAsDependency(CurrentModule); 5745 break; 5746 } 5747 } 5748 } 5749 5750 /// Parse the record that corresponds to a LangOptions data 5751 /// structure. 5752 /// 5753 /// This routine parses the language options from the AST file and then gives 5754 /// them to the AST listener if one is set. 5755 /// 5756 /// \returns true if the listener deems the file unacceptable, false otherwise. 5757 bool ASTReader::ParseLanguageOptions(const RecordData &Record, 5758 bool Complain, 5759 ASTReaderListener &Listener, 5760 bool AllowCompatibleDifferences) { 5761 LangOptions LangOpts; 5762 unsigned Idx = 0; 5763 #define LANGOPT(Name, Bits, Default, Description) \ 5764 LangOpts.Name = Record[Idx++]; 5765 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 5766 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++])); 5767 #include "clang/Basic/LangOptions.def" 5768 #define SANITIZER(NAME, ID) \ 5769 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]); 5770 #include "clang/Basic/Sanitizers.def" 5771 5772 for (unsigned N = Record[Idx++]; N; --N) 5773 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx)); 5774 5775 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++]; 5776 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx); 5777 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion); 5778 5779 LangOpts.CurrentModule = ReadString(Record, Idx); 5780 5781 // Comment options. 5782 for (unsigned N = Record[Idx++]; N; --N) { 5783 LangOpts.CommentOpts.BlockCommandNames.push_back( 5784 ReadString(Record, Idx)); 5785 } 5786 LangOpts.CommentOpts.ParseAllComments = Record[Idx++]; 5787 5788 // OpenMP offloading options. 5789 for (unsigned N = Record[Idx++]; N; --N) { 5790 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx))); 5791 } 5792 5793 LangOpts.OMPHostIRFile = ReadString(Record, Idx); 5794 5795 return Listener.ReadLanguageOptions(LangOpts, Complain, 5796 AllowCompatibleDifferences); 5797 } 5798 5799 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain, 5800 ASTReaderListener &Listener, 5801 bool AllowCompatibleDifferences) { 5802 unsigned Idx = 0; 5803 TargetOptions TargetOpts; 5804 TargetOpts.Triple = ReadString(Record, Idx); 5805 TargetOpts.CPU = ReadString(Record, Idx); 5806 TargetOpts.TuneCPU = ReadString(Record, Idx); 5807 TargetOpts.ABI = ReadString(Record, Idx); 5808 for (unsigned N = Record[Idx++]; N; --N) { 5809 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx)); 5810 } 5811 for (unsigned N = Record[Idx++]; N; --N) { 5812 TargetOpts.Features.push_back(ReadString(Record, Idx)); 5813 } 5814 5815 return Listener.ReadTargetOptions(TargetOpts, Complain, 5816 AllowCompatibleDifferences); 5817 } 5818 5819 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain, 5820 ASTReaderListener &Listener) { 5821 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions); 5822 unsigned Idx = 0; 5823 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++]; 5824 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \ 5825 DiagOpts->set##Name(static_cast<Type>(Record[Idx++])); 5826 #include "clang/Basic/DiagnosticOptions.def" 5827 5828 for (unsigned N = Record[Idx++]; N; --N) 5829 DiagOpts->Warnings.push_back(ReadString(Record, Idx)); 5830 for (unsigned N = Record[Idx++]; N; --N) 5831 DiagOpts->Remarks.push_back(ReadString(Record, Idx)); 5832 5833 return Listener.ReadDiagnosticOptions(DiagOpts, Complain); 5834 } 5835 5836 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain, 5837 ASTReaderListener &Listener) { 5838 FileSystemOptions FSOpts; 5839 unsigned Idx = 0; 5840 FSOpts.WorkingDir = ReadString(Record, Idx); 5841 return Listener.ReadFileSystemOptions(FSOpts, Complain); 5842 } 5843 5844 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record, 5845 bool Complain, 5846 ASTReaderListener &Listener) { 5847 HeaderSearchOptions HSOpts; 5848 unsigned Idx = 0; 5849 HSOpts.Sysroot = ReadString(Record, Idx); 5850 5851 // Include entries. 5852 for (unsigned N = Record[Idx++]; N; --N) { 5853 std::string Path = ReadString(Record, Idx); 5854 frontend::IncludeDirGroup Group 5855 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]); 5856 bool IsFramework = Record[Idx++]; 5857 bool IgnoreSysRoot = Record[Idx++]; 5858 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework, 5859 IgnoreSysRoot); 5860 } 5861 5862 // System header prefixes. 5863 for (unsigned N = Record[Idx++]; N; --N) { 5864 std::string Prefix = ReadString(Record, Idx); 5865 bool IsSystemHeader = Record[Idx++]; 5866 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader); 5867 } 5868 5869 HSOpts.ResourceDir = ReadString(Record, Idx); 5870 HSOpts.ModuleCachePath = ReadString(Record, Idx); 5871 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx); 5872 HSOpts.DisableModuleHash = Record[Idx++]; 5873 HSOpts.ImplicitModuleMaps = Record[Idx++]; 5874 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++]; 5875 HSOpts.EnablePrebuiltImplicitModules = Record[Idx++]; 5876 HSOpts.UseBuiltinIncludes = Record[Idx++]; 5877 HSOpts.UseStandardSystemIncludes = Record[Idx++]; 5878 HSOpts.UseStandardCXXIncludes = Record[Idx++]; 5879 HSOpts.UseLibcxx = Record[Idx++]; 5880 std::string SpecificModuleCachePath = ReadString(Record, Idx); 5881 5882 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5883 Complain); 5884 } 5885 5886 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record, 5887 bool Complain, 5888 ASTReaderListener &Listener, 5889 std::string &SuggestedPredefines) { 5890 PreprocessorOptions PPOpts; 5891 unsigned Idx = 0; 5892 5893 // Macro definitions/undefs 5894 for (unsigned N = Record[Idx++]; N; --N) { 5895 std::string Macro = ReadString(Record, Idx); 5896 bool IsUndef = Record[Idx++]; 5897 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef)); 5898 } 5899 5900 // Includes 5901 for (unsigned N = Record[Idx++]; N; --N) { 5902 PPOpts.Includes.push_back(ReadString(Record, Idx)); 5903 } 5904 5905 // Macro Includes 5906 for (unsigned N = Record[Idx++]; N; --N) { 5907 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx)); 5908 } 5909 5910 PPOpts.UsePredefines = Record[Idx++]; 5911 PPOpts.DetailedRecord = Record[Idx++]; 5912 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx); 5913 PPOpts.ObjCXXARCStandardLibrary = 5914 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]); 5915 SuggestedPredefines.clear(); 5916 return Listener.ReadPreprocessorOptions(PPOpts, Complain, 5917 SuggestedPredefines); 5918 } 5919 5920 std::pair<ModuleFile *, unsigned> 5921 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) { 5922 GlobalPreprocessedEntityMapType::iterator 5923 I = GlobalPreprocessedEntityMap.find(GlobalIndex); 5924 assert(I != GlobalPreprocessedEntityMap.end() && 5925 "Corrupted global preprocessed entity map"); 5926 ModuleFile *M = I->second; 5927 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID; 5928 return std::make_pair(M, LocalIndex); 5929 } 5930 5931 llvm::iterator_range<PreprocessingRecord::iterator> 5932 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const { 5933 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord()) 5934 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID, 5935 Mod.NumPreprocessedEntities); 5936 5937 return llvm::make_range(PreprocessingRecord::iterator(), 5938 PreprocessingRecord::iterator()); 5939 } 5940 5941 bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName, 5942 unsigned int ClientLoadCapabilities) { 5943 return ClientLoadCapabilities & ARR_OutOfDate && 5944 !getModuleManager().getModuleCache().isPCMFinal(ModuleFileName); 5945 } 5946 5947 llvm::iterator_range<ASTReader::ModuleDeclIterator> 5948 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) { 5949 return llvm::make_range( 5950 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls), 5951 ModuleDeclIterator(this, &Mod, 5952 Mod.FileSortedDecls + Mod.NumFileSortedDecls)); 5953 } 5954 5955 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) { 5956 auto I = GlobalSkippedRangeMap.find(GlobalIndex); 5957 assert(I != GlobalSkippedRangeMap.end() && 5958 "Corrupted global skipped range map"); 5959 ModuleFile *M = I->second; 5960 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID; 5961 assert(LocalIndex < M->NumPreprocessedSkippedRanges); 5962 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex]; 5963 SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()), 5964 TranslateSourceLocation(*M, RawRange.getEnd())); 5965 assert(Range.isValid()); 5966 return Range; 5967 } 5968 5969 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) { 5970 PreprocessedEntityID PPID = Index+1; 5971 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 5972 ModuleFile &M = *PPInfo.first; 5973 unsigned LocalIndex = PPInfo.second; 5974 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 5975 5976 if (!PP.getPreprocessingRecord()) { 5977 Error("no preprocessing record"); 5978 return nullptr; 5979 } 5980 5981 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor); 5982 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit( 5983 M.MacroOffsetsBase + PPOffs.BitOffset)) { 5984 Error(std::move(Err)); 5985 return nullptr; 5986 } 5987 5988 Expected<llvm::BitstreamEntry> MaybeEntry = 5989 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 5990 if (!MaybeEntry) { 5991 Error(MaybeEntry.takeError()); 5992 return nullptr; 5993 } 5994 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5995 5996 if (Entry.Kind != llvm::BitstreamEntry::Record) 5997 return nullptr; 5998 5999 // Read the record. 6000 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()), 6001 TranslateSourceLocation(M, PPOffs.getEnd())); 6002 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 6003 StringRef Blob; 6004 RecordData Record; 6005 Expected<unsigned> MaybeRecType = 6006 M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob); 6007 if (!MaybeRecType) { 6008 Error(MaybeRecType.takeError()); 6009 return nullptr; 6010 } 6011 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) { 6012 case PPD_MACRO_EXPANSION: { 6013 bool isBuiltin = Record[0]; 6014 IdentifierInfo *Name = nullptr; 6015 MacroDefinitionRecord *Def = nullptr; 6016 if (isBuiltin) 6017 Name = getLocalIdentifier(M, Record[1]); 6018 else { 6019 PreprocessedEntityID GlobalID = 6020 getGlobalPreprocessedEntityID(M, Record[1]); 6021 Def = cast<MacroDefinitionRecord>( 6022 PPRec.getLoadedPreprocessedEntity(GlobalID - 1)); 6023 } 6024 6025 MacroExpansion *ME; 6026 if (isBuiltin) 6027 ME = new (PPRec) MacroExpansion(Name, Range); 6028 else 6029 ME = new (PPRec) MacroExpansion(Def, Range); 6030 6031 return ME; 6032 } 6033 6034 case PPD_MACRO_DEFINITION: { 6035 // Decode the identifier info and then check again; if the macro is 6036 // still defined and associated with the identifier, 6037 IdentifierInfo *II = getLocalIdentifier(M, Record[0]); 6038 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range); 6039 6040 if (DeserializationListener) 6041 DeserializationListener->MacroDefinitionRead(PPID, MD); 6042 6043 return MD; 6044 } 6045 6046 case PPD_INCLUSION_DIRECTIVE: { 6047 const char *FullFileNameStart = Blob.data() + Record[0]; 6048 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]); 6049 Optional<FileEntryRef> File; 6050 if (!FullFileName.empty()) 6051 File = PP.getFileManager().getOptionalFileRef(FullFileName); 6052 6053 // FIXME: Stable encoding 6054 InclusionDirective::InclusionKind Kind 6055 = static_cast<InclusionDirective::InclusionKind>(Record[2]); 6056 InclusionDirective *ID 6057 = new (PPRec) InclusionDirective(PPRec, Kind, 6058 StringRef(Blob.data(), Record[0]), 6059 Record[1], Record[3], 6060 File, 6061 Range); 6062 return ID; 6063 } 6064 } 6065 6066 llvm_unreachable("Invalid PreprocessorDetailRecordTypes"); 6067 } 6068 6069 /// Find the next module that contains entities and return the ID 6070 /// of the first entry. 6071 /// 6072 /// \param SLocMapI points at a chunk of a module that contains no 6073 /// preprocessed entities or the entities it contains are not the ones we are 6074 /// looking for. 6075 PreprocessedEntityID ASTReader::findNextPreprocessedEntity( 6076 GlobalSLocOffsetMapType::const_iterator SLocMapI) const { 6077 ++SLocMapI; 6078 for (GlobalSLocOffsetMapType::const_iterator 6079 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) { 6080 ModuleFile &M = *SLocMapI->second; 6081 if (M.NumPreprocessedEntities) 6082 return M.BasePreprocessedEntityID; 6083 } 6084 6085 return getTotalNumPreprocessedEntities(); 6086 } 6087 6088 namespace { 6089 6090 struct PPEntityComp { 6091 const ASTReader &Reader; 6092 ModuleFile &M; 6093 6094 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {} 6095 6096 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const { 6097 SourceLocation LHS = getLoc(L); 6098 SourceLocation RHS = getLoc(R); 6099 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6100 } 6101 6102 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const { 6103 SourceLocation LHS = getLoc(L); 6104 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6105 } 6106 6107 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const { 6108 SourceLocation RHS = getLoc(R); 6109 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6110 } 6111 6112 SourceLocation getLoc(const PPEntityOffset &PPE) const { 6113 return Reader.TranslateSourceLocation(M, PPE.getBegin()); 6114 } 6115 }; 6116 6117 } // namespace 6118 6119 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc, 6120 bool EndsAfter) const { 6121 if (SourceMgr.isLocalSourceLocation(Loc)) 6122 return getTotalNumPreprocessedEntities(); 6123 6124 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find( 6125 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1); 6126 assert(SLocMapI != GlobalSLocOffsetMap.end() && 6127 "Corrupted global sloc offset map"); 6128 6129 if (SLocMapI->second->NumPreprocessedEntities == 0) 6130 return findNextPreprocessedEntity(SLocMapI); 6131 6132 ModuleFile &M = *SLocMapI->second; 6133 6134 using pp_iterator = const PPEntityOffset *; 6135 6136 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 6137 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 6138 6139 size_t Count = M.NumPreprocessedEntities; 6140 size_t Half; 6141 pp_iterator First = pp_begin; 6142 pp_iterator PPI; 6143 6144 if (EndsAfter) { 6145 PPI = std::upper_bound(pp_begin, pp_end, Loc, 6146 PPEntityComp(*this, M)); 6147 } else { 6148 // Do a binary search manually instead of using std::lower_bound because 6149 // The end locations of entities may be unordered (when a macro expansion 6150 // is inside another macro argument), but for this case it is not important 6151 // whether we get the first macro expansion or its containing macro. 6152 while (Count > 0) { 6153 Half = Count / 2; 6154 PPI = First; 6155 std::advance(PPI, Half); 6156 if (SourceMgr.isBeforeInTranslationUnit( 6157 TranslateSourceLocation(M, PPI->getEnd()), Loc)) { 6158 First = PPI; 6159 ++First; 6160 Count = Count - Half - 1; 6161 } else 6162 Count = Half; 6163 } 6164 } 6165 6166 if (PPI == pp_end) 6167 return findNextPreprocessedEntity(SLocMapI); 6168 6169 return M.BasePreprocessedEntityID + (PPI - pp_begin); 6170 } 6171 6172 /// Returns a pair of [Begin, End) indices of preallocated 6173 /// preprocessed entities that \arg Range encompasses. 6174 std::pair<unsigned, unsigned> 6175 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) { 6176 if (Range.isInvalid()) 6177 return std::make_pair(0,0); 6178 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); 6179 6180 PreprocessedEntityID BeginID = 6181 findPreprocessedEntity(Range.getBegin(), false); 6182 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true); 6183 return std::make_pair(BeginID, EndID); 6184 } 6185 6186 /// Optionally returns true or false if the preallocated preprocessed 6187 /// entity with index \arg Index came from file \arg FID. 6188 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index, 6189 FileID FID) { 6190 if (FID.isInvalid()) 6191 return false; 6192 6193 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 6194 ModuleFile &M = *PPInfo.first; 6195 unsigned LocalIndex = PPInfo.second; 6196 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 6197 6198 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin()); 6199 if (Loc.isInvalid()) 6200 return false; 6201 6202 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID)) 6203 return true; 6204 else 6205 return false; 6206 } 6207 6208 namespace { 6209 6210 /// Visitor used to search for information about a header file. 6211 class HeaderFileInfoVisitor { 6212 const FileEntry *FE; 6213 Optional<HeaderFileInfo> HFI; 6214 6215 public: 6216 explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {} 6217 6218 bool operator()(ModuleFile &M) { 6219 HeaderFileInfoLookupTable *Table 6220 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable); 6221 if (!Table) 6222 return false; 6223 6224 // Look in the on-disk hash table for an entry for this file name. 6225 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE); 6226 if (Pos == Table->end()) 6227 return false; 6228 6229 HFI = *Pos; 6230 return true; 6231 } 6232 6233 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; } 6234 }; 6235 6236 } // namespace 6237 6238 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) { 6239 HeaderFileInfoVisitor Visitor(FE); 6240 ModuleMgr.visit(Visitor); 6241 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) 6242 return *HFI; 6243 6244 return HeaderFileInfo(); 6245 } 6246 6247 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) { 6248 using DiagState = DiagnosticsEngine::DiagState; 6249 SmallVector<DiagState *, 32> DiagStates; 6250 6251 for (ModuleFile &F : ModuleMgr) { 6252 unsigned Idx = 0; 6253 auto &Record = F.PragmaDiagMappings; 6254 if (Record.empty()) 6255 continue; 6256 6257 DiagStates.clear(); 6258 6259 auto ReadDiagState = 6260 [&](const DiagState &BasedOn, SourceLocation Loc, 6261 bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * { 6262 unsigned BackrefID = Record[Idx++]; 6263 if (BackrefID != 0) 6264 return DiagStates[BackrefID - 1]; 6265 6266 // A new DiagState was created here. 6267 Diag.DiagStates.push_back(BasedOn); 6268 DiagState *NewState = &Diag.DiagStates.back(); 6269 DiagStates.push_back(NewState); 6270 unsigned Size = Record[Idx++]; 6271 assert(Idx + Size * 2 <= Record.size() && 6272 "Invalid data, not enough diag/map pairs"); 6273 while (Size--) { 6274 unsigned DiagID = Record[Idx++]; 6275 DiagnosticMapping NewMapping = 6276 DiagnosticMapping::deserialize(Record[Idx++]); 6277 if (!NewMapping.isPragma() && !IncludeNonPragmaStates) 6278 continue; 6279 6280 DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID); 6281 6282 // If this mapping was specified as a warning but the severity was 6283 // upgraded due to diagnostic settings, simulate the current diagnostic 6284 // settings (and use a warning). 6285 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) { 6286 NewMapping.setSeverity(diag::Severity::Warning); 6287 NewMapping.setUpgradedFromWarning(false); 6288 } 6289 6290 Mapping = NewMapping; 6291 } 6292 return NewState; 6293 }; 6294 6295 // Read the first state. 6296 DiagState *FirstState; 6297 if (F.Kind == MK_ImplicitModule) { 6298 // Implicitly-built modules are reused with different diagnostic 6299 // settings. Use the initial diagnostic state from Diag to simulate this 6300 // compilation's diagnostic settings. 6301 FirstState = Diag.DiagStatesByLoc.FirstDiagState; 6302 DiagStates.push_back(FirstState); 6303 6304 // Skip the initial diagnostic state from the serialized module. 6305 assert(Record[1] == 0 && 6306 "Invalid data, unexpected backref in initial state"); 6307 Idx = 3 + Record[2] * 2; 6308 assert(Idx < Record.size() && 6309 "Invalid data, not enough state change pairs in initial state"); 6310 } else if (F.isModule()) { 6311 // For an explicit module, preserve the flags from the module build 6312 // command line (-w, -Weverything, -Werror, ...) along with any explicit 6313 // -Wblah flags. 6314 unsigned Flags = Record[Idx++]; 6315 DiagState Initial; 6316 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1; 6317 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1; 6318 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1; 6319 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1; 6320 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1; 6321 Initial.ExtBehavior = (diag::Severity)Flags; 6322 FirstState = ReadDiagState(Initial, SourceLocation(), true); 6323 6324 assert(F.OriginalSourceFileID.isValid()); 6325 6326 // Set up the root buffer of the module to start with the initial 6327 // diagnostic state of the module itself, to cover files that contain no 6328 // explicit transitions (for which we did not serialize anything). 6329 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID] 6330 .StateTransitions.push_back({FirstState, 0}); 6331 } else { 6332 // For prefix ASTs, start with whatever the user configured on the 6333 // command line. 6334 Idx++; // Skip flags. 6335 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, 6336 SourceLocation(), false); 6337 } 6338 6339 // Read the state transitions. 6340 unsigned NumLocations = Record[Idx++]; 6341 while (NumLocations--) { 6342 assert(Idx < Record.size() && 6343 "Invalid data, missing pragma diagnostic states"); 6344 SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]); 6345 auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc); 6346 assert(IDAndOffset.first.isValid() && "invalid FileID for transition"); 6347 assert(IDAndOffset.second == 0 && "not a start location for a FileID"); 6348 unsigned Transitions = Record[Idx++]; 6349 6350 // Note that we don't need to set up Parent/ParentOffset here, because 6351 // we won't be changing the diagnostic state within imported FileIDs 6352 // (other than perhaps appending to the main source file, which has no 6353 // parent). 6354 auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first]; 6355 F.StateTransitions.reserve(F.StateTransitions.size() + Transitions); 6356 for (unsigned I = 0; I != Transitions; ++I) { 6357 unsigned Offset = Record[Idx++]; 6358 auto *State = 6359 ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false); 6360 F.StateTransitions.push_back({State, Offset}); 6361 } 6362 } 6363 6364 // Read the final state. 6365 assert(Idx < Record.size() && 6366 "Invalid data, missing final pragma diagnostic state"); 6367 SourceLocation CurStateLoc = 6368 ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]); 6369 auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false); 6370 6371 if (!F.isModule()) { 6372 Diag.DiagStatesByLoc.CurDiagState = CurState; 6373 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc; 6374 6375 // Preserve the property that the imaginary root file describes the 6376 // current state. 6377 FileID NullFile; 6378 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions; 6379 if (T.empty()) 6380 T.push_back({CurState, 0}); 6381 else 6382 T[0].State = CurState; 6383 } 6384 6385 // Don't try to read these mappings again. 6386 Record.clear(); 6387 } 6388 } 6389 6390 /// Get the correct cursor and offset for loading a type. 6391 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) { 6392 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index); 6393 assert(I != GlobalTypeMap.end() && "Corrupted global type map"); 6394 ModuleFile *M = I->second; 6395 return RecordLocation( 6396 M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() + 6397 M->DeclsBlockStartOffset); 6398 } 6399 6400 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) { 6401 switch (code) { 6402 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \ 6403 case TYPE_##CODE_ID: return Type::CLASS_ID; 6404 #include "clang/Serialization/TypeBitCodes.def" 6405 default: return llvm::None; 6406 } 6407 } 6408 6409 /// Read and return the type with the given index.. 6410 /// 6411 /// The index is the type ID, shifted and minus the number of predefs. This 6412 /// routine actually reads the record corresponding to the type at the given 6413 /// location. It is a helper routine for GetType, which deals with reading type 6414 /// IDs. 6415 QualType ASTReader::readTypeRecord(unsigned Index) { 6416 assert(ContextObj && "reading type with no AST context"); 6417 ASTContext &Context = *ContextObj; 6418 RecordLocation Loc = TypeCursorForIndex(Index); 6419 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 6420 6421 // Keep track of where we are in the stream, then jump back there 6422 // after reading this type. 6423 SavedStreamPosition SavedPosition(DeclsCursor); 6424 6425 ReadingKindTracker ReadingKind(Read_Type, *this); 6426 6427 // Note that we are loading a type record. 6428 Deserializing AType(this); 6429 6430 if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) { 6431 Error(std::move(Err)); 6432 return QualType(); 6433 } 6434 Expected<unsigned> RawCode = DeclsCursor.ReadCode(); 6435 if (!RawCode) { 6436 Error(RawCode.takeError()); 6437 return QualType(); 6438 } 6439 6440 ASTRecordReader Record(*this, *Loc.F); 6441 Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get()); 6442 if (!Code) { 6443 Error(Code.takeError()); 6444 return QualType(); 6445 } 6446 if (Code.get() == TYPE_EXT_QUAL) { 6447 QualType baseType = Record.readQualType(); 6448 Qualifiers quals = Record.readQualifiers(); 6449 return Context.getQualifiedType(baseType, quals); 6450 } 6451 6452 auto maybeClass = getTypeClassForCode((TypeCode) Code.get()); 6453 if (!maybeClass) { 6454 Error("Unexpected code for type"); 6455 return QualType(); 6456 } 6457 6458 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record); 6459 return TypeReader.read(*maybeClass); 6460 } 6461 6462 namespace clang { 6463 6464 class TypeLocReader : public TypeLocVisitor<TypeLocReader> { 6465 ASTRecordReader &Reader; 6466 6467 SourceLocation readSourceLocation() { 6468 return Reader.readSourceLocation(); 6469 } 6470 6471 TypeSourceInfo *GetTypeSourceInfo() { 6472 return Reader.readTypeSourceInfo(); 6473 } 6474 6475 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() { 6476 return Reader.readNestedNameSpecifierLoc(); 6477 } 6478 6479 Attr *ReadAttr() { 6480 return Reader.readAttr(); 6481 } 6482 6483 public: 6484 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {} 6485 6486 // We want compile-time assurance that we've enumerated all of 6487 // these, so unfortunately we have to declare them first, then 6488 // define them out-of-line. 6489 #define ABSTRACT_TYPELOC(CLASS, PARENT) 6490 #define TYPELOC(CLASS, PARENT) \ 6491 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 6492 #include "clang/AST/TypeLocNodes.def" 6493 6494 void VisitFunctionTypeLoc(FunctionTypeLoc); 6495 void VisitArrayTypeLoc(ArrayTypeLoc); 6496 }; 6497 6498 } // namespace clang 6499 6500 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 6501 // nothing to do 6502 } 6503 6504 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 6505 TL.setBuiltinLoc(readSourceLocation()); 6506 if (TL.needsExtraLocalData()) { 6507 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt())); 6508 TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt())); 6509 TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt())); 6510 TL.setModeAttr(Reader.readInt()); 6511 } 6512 } 6513 6514 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { 6515 TL.setNameLoc(readSourceLocation()); 6516 } 6517 6518 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { 6519 TL.setStarLoc(readSourceLocation()); 6520 } 6521 6522 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) { 6523 // nothing to do 6524 } 6525 6526 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { 6527 // nothing to do 6528 } 6529 6530 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) { 6531 TL.setExpansionLoc(readSourceLocation()); 6532 } 6533 6534 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 6535 TL.setCaretLoc(readSourceLocation()); 6536 } 6537 6538 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 6539 TL.setAmpLoc(readSourceLocation()); 6540 } 6541 6542 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 6543 TL.setAmpAmpLoc(readSourceLocation()); 6544 } 6545 6546 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 6547 TL.setStarLoc(readSourceLocation()); 6548 TL.setClassTInfo(GetTypeSourceInfo()); 6549 } 6550 6551 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { 6552 TL.setLBracketLoc(readSourceLocation()); 6553 TL.setRBracketLoc(readSourceLocation()); 6554 if (Reader.readBool()) 6555 TL.setSizeExpr(Reader.readExpr()); 6556 else 6557 TL.setSizeExpr(nullptr); 6558 } 6559 6560 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 6561 VisitArrayTypeLoc(TL); 6562 } 6563 6564 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 6565 VisitArrayTypeLoc(TL); 6566 } 6567 6568 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 6569 VisitArrayTypeLoc(TL); 6570 } 6571 6572 void TypeLocReader::VisitDependentSizedArrayTypeLoc( 6573 DependentSizedArrayTypeLoc TL) { 6574 VisitArrayTypeLoc(TL); 6575 } 6576 6577 void TypeLocReader::VisitDependentAddressSpaceTypeLoc( 6578 DependentAddressSpaceTypeLoc TL) { 6579 6580 TL.setAttrNameLoc(readSourceLocation()); 6581 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6582 TL.setAttrExprOperand(Reader.readExpr()); 6583 } 6584 6585 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( 6586 DependentSizedExtVectorTypeLoc TL) { 6587 TL.setNameLoc(readSourceLocation()); 6588 } 6589 6590 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { 6591 TL.setNameLoc(readSourceLocation()); 6592 } 6593 6594 void TypeLocReader::VisitDependentVectorTypeLoc( 6595 DependentVectorTypeLoc TL) { 6596 TL.setNameLoc(readSourceLocation()); 6597 } 6598 6599 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 6600 TL.setNameLoc(readSourceLocation()); 6601 } 6602 6603 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) { 6604 TL.setAttrNameLoc(readSourceLocation()); 6605 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6606 TL.setAttrRowOperand(Reader.readExpr()); 6607 TL.setAttrColumnOperand(Reader.readExpr()); 6608 } 6609 6610 void TypeLocReader::VisitDependentSizedMatrixTypeLoc( 6611 DependentSizedMatrixTypeLoc TL) { 6612 TL.setAttrNameLoc(readSourceLocation()); 6613 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6614 TL.setAttrRowOperand(Reader.readExpr()); 6615 TL.setAttrColumnOperand(Reader.readExpr()); 6616 } 6617 6618 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 6619 TL.setLocalRangeBegin(readSourceLocation()); 6620 TL.setLParenLoc(readSourceLocation()); 6621 TL.setRParenLoc(readSourceLocation()); 6622 TL.setExceptionSpecRange(Reader.readSourceRange()); 6623 TL.setLocalRangeEnd(readSourceLocation()); 6624 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) { 6625 TL.setParam(i, Reader.readDeclAs<ParmVarDecl>()); 6626 } 6627 } 6628 6629 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 6630 VisitFunctionTypeLoc(TL); 6631 } 6632 6633 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 6634 VisitFunctionTypeLoc(TL); 6635 } 6636 6637 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 6638 TL.setNameLoc(readSourceLocation()); 6639 } 6640 6641 void TypeLocReader::VisitUsingTypeLoc(UsingTypeLoc TL) { 6642 TL.setNameLoc(readSourceLocation()); 6643 } 6644 6645 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 6646 TL.setNameLoc(readSourceLocation()); 6647 } 6648 6649 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 6650 TL.setTypeofLoc(readSourceLocation()); 6651 TL.setLParenLoc(readSourceLocation()); 6652 TL.setRParenLoc(readSourceLocation()); 6653 } 6654 6655 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 6656 TL.setTypeofLoc(readSourceLocation()); 6657 TL.setLParenLoc(readSourceLocation()); 6658 TL.setRParenLoc(readSourceLocation()); 6659 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6660 } 6661 6662 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 6663 TL.setDecltypeLoc(readSourceLocation()); 6664 TL.setRParenLoc(readSourceLocation()); 6665 } 6666 6667 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 6668 TL.setKWLoc(readSourceLocation()); 6669 TL.setLParenLoc(readSourceLocation()); 6670 TL.setRParenLoc(readSourceLocation()); 6671 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6672 } 6673 6674 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) { 6675 TL.setNameLoc(readSourceLocation()); 6676 if (Reader.readBool()) { 6677 TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc()); 6678 TL.setTemplateKWLoc(readSourceLocation()); 6679 TL.setConceptNameLoc(readSourceLocation()); 6680 TL.setFoundDecl(Reader.readDeclAs<NamedDecl>()); 6681 TL.setLAngleLoc(readSourceLocation()); 6682 TL.setRAngleLoc(readSourceLocation()); 6683 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6684 TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo( 6685 TL.getTypePtr()->getArg(i).getKind())); 6686 } 6687 if (Reader.readBool()) 6688 TL.setRParenLoc(readSourceLocation()); 6689 } 6690 6691 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc( 6692 DeducedTemplateSpecializationTypeLoc TL) { 6693 TL.setTemplateNameLoc(readSourceLocation()); 6694 } 6695 6696 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { 6697 TL.setNameLoc(readSourceLocation()); 6698 } 6699 6700 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { 6701 TL.setNameLoc(readSourceLocation()); 6702 } 6703 6704 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 6705 TL.setAttr(ReadAttr()); 6706 } 6707 6708 void TypeLocReader::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) { 6709 // Nothing to do. 6710 } 6711 6712 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 6713 TL.setNameLoc(readSourceLocation()); 6714 } 6715 6716 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( 6717 SubstTemplateTypeParmTypeLoc TL) { 6718 TL.setNameLoc(readSourceLocation()); 6719 } 6720 6721 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc( 6722 SubstTemplateTypeParmPackTypeLoc TL) { 6723 TL.setNameLoc(readSourceLocation()); 6724 } 6725 6726 void TypeLocReader::VisitTemplateSpecializationTypeLoc( 6727 TemplateSpecializationTypeLoc TL) { 6728 TL.setTemplateKeywordLoc(readSourceLocation()); 6729 TL.setTemplateNameLoc(readSourceLocation()); 6730 TL.setLAngleLoc(readSourceLocation()); 6731 TL.setRAngleLoc(readSourceLocation()); 6732 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6733 TL.setArgLocInfo( 6734 i, 6735 Reader.readTemplateArgumentLocInfo( 6736 TL.getTypePtr()->getArg(i).getKind())); 6737 } 6738 6739 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) { 6740 TL.setLParenLoc(readSourceLocation()); 6741 TL.setRParenLoc(readSourceLocation()); 6742 } 6743 6744 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 6745 TL.setElaboratedKeywordLoc(readSourceLocation()); 6746 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6747 } 6748 6749 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 6750 TL.setNameLoc(readSourceLocation()); 6751 } 6752 6753 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 6754 TL.setElaboratedKeywordLoc(readSourceLocation()); 6755 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6756 TL.setNameLoc(readSourceLocation()); 6757 } 6758 6759 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( 6760 DependentTemplateSpecializationTypeLoc TL) { 6761 TL.setElaboratedKeywordLoc(readSourceLocation()); 6762 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6763 TL.setTemplateKeywordLoc(readSourceLocation()); 6764 TL.setTemplateNameLoc(readSourceLocation()); 6765 TL.setLAngleLoc(readSourceLocation()); 6766 TL.setRAngleLoc(readSourceLocation()); 6767 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 6768 TL.setArgLocInfo( 6769 I, 6770 Reader.readTemplateArgumentLocInfo( 6771 TL.getTypePtr()->getArg(I).getKind())); 6772 } 6773 6774 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 6775 TL.setEllipsisLoc(readSourceLocation()); 6776 } 6777 6778 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 6779 TL.setNameLoc(readSourceLocation()); 6780 } 6781 6782 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) { 6783 if (TL.getNumProtocols()) { 6784 TL.setProtocolLAngleLoc(readSourceLocation()); 6785 TL.setProtocolRAngleLoc(readSourceLocation()); 6786 } 6787 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6788 TL.setProtocolLoc(i, readSourceLocation()); 6789 } 6790 6791 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 6792 TL.setHasBaseTypeAsWritten(Reader.readBool()); 6793 TL.setTypeArgsLAngleLoc(readSourceLocation()); 6794 TL.setTypeArgsRAngleLoc(readSourceLocation()); 6795 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i) 6796 TL.setTypeArgTInfo(i, GetTypeSourceInfo()); 6797 TL.setProtocolLAngleLoc(readSourceLocation()); 6798 TL.setProtocolRAngleLoc(readSourceLocation()); 6799 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6800 TL.setProtocolLoc(i, readSourceLocation()); 6801 } 6802 6803 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 6804 TL.setStarLoc(readSourceLocation()); 6805 } 6806 6807 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) { 6808 TL.setKWLoc(readSourceLocation()); 6809 TL.setLParenLoc(readSourceLocation()); 6810 TL.setRParenLoc(readSourceLocation()); 6811 } 6812 6813 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) { 6814 TL.setKWLoc(readSourceLocation()); 6815 } 6816 6817 void TypeLocReader::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) { 6818 TL.setNameLoc(readSourceLocation()); 6819 } 6820 void TypeLocReader::VisitDependentBitIntTypeLoc( 6821 clang::DependentBitIntTypeLoc TL) { 6822 TL.setNameLoc(readSourceLocation()); 6823 } 6824 6825 6826 void ASTRecordReader::readTypeLoc(TypeLoc TL) { 6827 TypeLocReader TLR(*this); 6828 for (; !TL.isNull(); TL = TL.getNextTypeLoc()) 6829 TLR.Visit(TL); 6830 } 6831 6832 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() { 6833 QualType InfoTy = readType(); 6834 if (InfoTy.isNull()) 6835 return nullptr; 6836 6837 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy); 6838 readTypeLoc(TInfo->getTypeLoc()); 6839 return TInfo; 6840 } 6841 6842 QualType ASTReader::GetType(TypeID ID) { 6843 assert(ContextObj && "reading type with no AST context"); 6844 ASTContext &Context = *ContextObj; 6845 6846 unsigned FastQuals = ID & Qualifiers::FastMask; 6847 unsigned Index = ID >> Qualifiers::FastWidth; 6848 6849 if (Index < NUM_PREDEF_TYPE_IDS) { 6850 QualType T; 6851 switch ((PredefinedTypeIDs)Index) { 6852 case PREDEF_TYPE_NULL_ID: 6853 return QualType(); 6854 case PREDEF_TYPE_VOID_ID: 6855 T = Context.VoidTy; 6856 break; 6857 case PREDEF_TYPE_BOOL_ID: 6858 T = Context.BoolTy; 6859 break; 6860 case PREDEF_TYPE_CHAR_U_ID: 6861 case PREDEF_TYPE_CHAR_S_ID: 6862 // FIXME: Check that the signedness of CharTy is correct! 6863 T = Context.CharTy; 6864 break; 6865 case PREDEF_TYPE_UCHAR_ID: 6866 T = Context.UnsignedCharTy; 6867 break; 6868 case PREDEF_TYPE_USHORT_ID: 6869 T = Context.UnsignedShortTy; 6870 break; 6871 case PREDEF_TYPE_UINT_ID: 6872 T = Context.UnsignedIntTy; 6873 break; 6874 case PREDEF_TYPE_ULONG_ID: 6875 T = Context.UnsignedLongTy; 6876 break; 6877 case PREDEF_TYPE_ULONGLONG_ID: 6878 T = Context.UnsignedLongLongTy; 6879 break; 6880 case PREDEF_TYPE_UINT128_ID: 6881 T = Context.UnsignedInt128Ty; 6882 break; 6883 case PREDEF_TYPE_SCHAR_ID: 6884 T = Context.SignedCharTy; 6885 break; 6886 case PREDEF_TYPE_WCHAR_ID: 6887 T = Context.WCharTy; 6888 break; 6889 case PREDEF_TYPE_SHORT_ID: 6890 T = Context.ShortTy; 6891 break; 6892 case PREDEF_TYPE_INT_ID: 6893 T = Context.IntTy; 6894 break; 6895 case PREDEF_TYPE_LONG_ID: 6896 T = Context.LongTy; 6897 break; 6898 case PREDEF_TYPE_LONGLONG_ID: 6899 T = Context.LongLongTy; 6900 break; 6901 case PREDEF_TYPE_INT128_ID: 6902 T = Context.Int128Ty; 6903 break; 6904 case PREDEF_TYPE_BFLOAT16_ID: 6905 T = Context.BFloat16Ty; 6906 break; 6907 case PREDEF_TYPE_HALF_ID: 6908 T = Context.HalfTy; 6909 break; 6910 case PREDEF_TYPE_FLOAT_ID: 6911 T = Context.FloatTy; 6912 break; 6913 case PREDEF_TYPE_DOUBLE_ID: 6914 T = Context.DoubleTy; 6915 break; 6916 case PREDEF_TYPE_LONGDOUBLE_ID: 6917 T = Context.LongDoubleTy; 6918 break; 6919 case PREDEF_TYPE_SHORT_ACCUM_ID: 6920 T = Context.ShortAccumTy; 6921 break; 6922 case PREDEF_TYPE_ACCUM_ID: 6923 T = Context.AccumTy; 6924 break; 6925 case PREDEF_TYPE_LONG_ACCUM_ID: 6926 T = Context.LongAccumTy; 6927 break; 6928 case PREDEF_TYPE_USHORT_ACCUM_ID: 6929 T = Context.UnsignedShortAccumTy; 6930 break; 6931 case PREDEF_TYPE_UACCUM_ID: 6932 T = Context.UnsignedAccumTy; 6933 break; 6934 case PREDEF_TYPE_ULONG_ACCUM_ID: 6935 T = Context.UnsignedLongAccumTy; 6936 break; 6937 case PREDEF_TYPE_SHORT_FRACT_ID: 6938 T = Context.ShortFractTy; 6939 break; 6940 case PREDEF_TYPE_FRACT_ID: 6941 T = Context.FractTy; 6942 break; 6943 case PREDEF_TYPE_LONG_FRACT_ID: 6944 T = Context.LongFractTy; 6945 break; 6946 case PREDEF_TYPE_USHORT_FRACT_ID: 6947 T = Context.UnsignedShortFractTy; 6948 break; 6949 case PREDEF_TYPE_UFRACT_ID: 6950 T = Context.UnsignedFractTy; 6951 break; 6952 case PREDEF_TYPE_ULONG_FRACT_ID: 6953 T = Context.UnsignedLongFractTy; 6954 break; 6955 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID: 6956 T = Context.SatShortAccumTy; 6957 break; 6958 case PREDEF_TYPE_SAT_ACCUM_ID: 6959 T = Context.SatAccumTy; 6960 break; 6961 case PREDEF_TYPE_SAT_LONG_ACCUM_ID: 6962 T = Context.SatLongAccumTy; 6963 break; 6964 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID: 6965 T = Context.SatUnsignedShortAccumTy; 6966 break; 6967 case PREDEF_TYPE_SAT_UACCUM_ID: 6968 T = Context.SatUnsignedAccumTy; 6969 break; 6970 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID: 6971 T = Context.SatUnsignedLongAccumTy; 6972 break; 6973 case PREDEF_TYPE_SAT_SHORT_FRACT_ID: 6974 T = Context.SatShortFractTy; 6975 break; 6976 case PREDEF_TYPE_SAT_FRACT_ID: 6977 T = Context.SatFractTy; 6978 break; 6979 case PREDEF_TYPE_SAT_LONG_FRACT_ID: 6980 T = Context.SatLongFractTy; 6981 break; 6982 case PREDEF_TYPE_SAT_USHORT_FRACT_ID: 6983 T = Context.SatUnsignedShortFractTy; 6984 break; 6985 case PREDEF_TYPE_SAT_UFRACT_ID: 6986 T = Context.SatUnsignedFractTy; 6987 break; 6988 case PREDEF_TYPE_SAT_ULONG_FRACT_ID: 6989 T = Context.SatUnsignedLongFractTy; 6990 break; 6991 case PREDEF_TYPE_FLOAT16_ID: 6992 T = Context.Float16Ty; 6993 break; 6994 case PREDEF_TYPE_FLOAT128_ID: 6995 T = Context.Float128Ty; 6996 break; 6997 case PREDEF_TYPE_IBM128_ID: 6998 T = Context.Ibm128Ty; 6999 break; 7000 case PREDEF_TYPE_OVERLOAD_ID: 7001 T = Context.OverloadTy; 7002 break; 7003 case PREDEF_TYPE_BOUND_MEMBER: 7004 T = Context.BoundMemberTy; 7005 break; 7006 case PREDEF_TYPE_PSEUDO_OBJECT: 7007 T = Context.PseudoObjectTy; 7008 break; 7009 case PREDEF_TYPE_DEPENDENT_ID: 7010 T = Context.DependentTy; 7011 break; 7012 case PREDEF_TYPE_UNKNOWN_ANY: 7013 T = Context.UnknownAnyTy; 7014 break; 7015 case PREDEF_TYPE_NULLPTR_ID: 7016 T = Context.NullPtrTy; 7017 break; 7018 case PREDEF_TYPE_CHAR8_ID: 7019 T = Context.Char8Ty; 7020 break; 7021 case PREDEF_TYPE_CHAR16_ID: 7022 T = Context.Char16Ty; 7023 break; 7024 case PREDEF_TYPE_CHAR32_ID: 7025 T = Context.Char32Ty; 7026 break; 7027 case PREDEF_TYPE_OBJC_ID: 7028 T = Context.ObjCBuiltinIdTy; 7029 break; 7030 case PREDEF_TYPE_OBJC_CLASS: 7031 T = Context.ObjCBuiltinClassTy; 7032 break; 7033 case PREDEF_TYPE_OBJC_SEL: 7034 T = Context.ObjCBuiltinSelTy; 7035 break; 7036 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 7037 case PREDEF_TYPE_##Id##_ID: \ 7038 T = Context.SingletonId; \ 7039 break; 7040 #include "clang/Basic/OpenCLImageTypes.def" 7041 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ 7042 case PREDEF_TYPE_##Id##_ID: \ 7043 T = Context.Id##Ty; \ 7044 break; 7045 #include "clang/Basic/OpenCLExtensionTypes.def" 7046 case PREDEF_TYPE_SAMPLER_ID: 7047 T = Context.OCLSamplerTy; 7048 break; 7049 case PREDEF_TYPE_EVENT_ID: 7050 T = Context.OCLEventTy; 7051 break; 7052 case PREDEF_TYPE_CLK_EVENT_ID: 7053 T = Context.OCLClkEventTy; 7054 break; 7055 case PREDEF_TYPE_QUEUE_ID: 7056 T = Context.OCLQueueTy; 7057 break; 7058 case PREDEF_TYPE_RESERVE_ID_ID: 7059 T = Context.OCLReserveIDTy; 7060 break; 7061 case PREDEF_TYPE_AUTO_DEDUCT: 7062 T = Context.getAutoDeductType(); 7063 break; 7064 case PREDEF_TYPE_AUTO_RREF_DEDUCT: 7065 T = Context.getAutoRRefDeductType(); 7066 break; 7067 case PREDEF_TYPE_ARC_UNBRIDGED_CAST: 7068 T = Context.ARCUnbridgedCastTy; 7069 break; 7070 case PREDEF_TYPE_BUILTIN_FN: 7071 T = Context.BuiltinFnTy; 7072 break; 7073 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX: 7074 T = Context.IncompleteMatrixIdxTy; 7075 break; 7076 case PREDEF_TYPE_OMP_ARRAY_SECTION: 7077 T = Context.OMPArraySectionTy; 7078 break; 7079 case PREDEF_TYPE_OMP_ARRAY_SHAPING: 7080 T = Context.OMPArraySectionTy; 7081 break; 7082 case PREDEF_TYPE_OMP_ITERATOR: 7083 T = Context.OMPIteratorTy; 7084 break; 7085 #define SVE_TYPE(Name, Id, SingletonId) \ 7086 case PREDEF_TYPE_##Id##_ID: \ 7087 T = Context.SingletonId; \ 7088 break; 7089 #include "clang/Basic/AArch64SVEACLETypes.def" 7090 #define PPC_VECTOR_TYPE(Name, Id, Size) \ 7091 case PREDEF_TYPE_##Id##_ID: \ 7092 T = Context.Id##Ty; \ 7093 break; 7094 #include "clang/Basic/PPCTypes.def" 7095 #define RVV_TYPE(Name, Id, SingletonId) \ 7096 case PREDEF_TYPE_##Id##_ID: \ 7097 T = Context.SingletonId; \ 7098 break; 7099 #include "clang/Basic/RISCVVTypes.def" 7100 } 7101 7102 assert(!T.isNull() && "Unknown predefined type"); 7103 return T.withFastQualifiers(FastQuals); 7104 } 7105 7106 Index -= NUM_PREDEF_TYPE_IDS; 7107 assert(Index < TypesLoaded.size() && "Type index out-of-range"); 7108 if (TypesLoaded[Index].isNull()) { 7109 TypesLoaded[Index] = readTypeRecord(Index); 7110 if (TypesLoaded[Index].isNull()) 7111 return QualType(); 7112 7113 TypesLoaded[Index]->setFromAST(); 7114 if (DeserializationListener) 7115 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID), 7116 TypesLoaded[Index]); 7117 } 7118 7119 return TypesLoaded[Index].withFastQualifiers(FastQuals); 7120 } 7121 7122 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) { 7123 return GetType(getGlobalTypeID(F, LocalID)); 7124 } 7125 7126 serialization::TypeID 7127 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const { 7128 unsigned FastQuals = LocalID & Qualifiers::FastMask; 7129 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth; 7130 7131 if (LocalIndex < NUM_PREDEF_TYPE_IDS) 7132 return LocalID; 7133 7134 if (!F.ModuleOffsetMap.empty()) 7135 ReadModuleOffsetMap(F); 7136 7137 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7138 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS); 7139 assert(I != F.TypeRemap.end() && "Invalid index into type index remap"); 7140 7141 unsigned GlobalIndex = LocalIndex + I->second; 7142 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals; 7143 } 7144 7145 TemplateArgumentLocInfo 7146 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) { 7147 switch (Kind) { 7148 case TemplateArgument::Expression: 7149 return readExpr(); 7150 case TemplateArgument::Type: 7151 return readTypeSourceInfo(); 7152 case TemplateArgument::Template: { 7153 NestedNameSpecifierLoc QualifierLoc = 7154 readNestedNameSpecifierLoc(); 7155 SourceLocation TemplateNameLoc = readSourceLocation(); 7156 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc, 7157 TemplateNameLoc, SourceLocation()); 7158 } 7159 case TemplateArgument::TemplateExpansion: { 7160 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc(); 7161 SourceLocation TemplateNameLoc = readSourceLocation(); 7162 SourceLocation EllipsisLoc = readSourceLocation(); 7163 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc, 7164 TemplateNameLoc, EllipsisLoc); 7165 } 7166 case TemplateArgument::Null: 7167 case TemplateArgument::Integral: 7168 case TemplateArgument::Declaration: 7169 case TemplateArgument::NullPtr: 7170 case TemplateArgument::Pack: 7171 // FIXME: Is this right? 7172 return TemplateArgumentLocInfo(); 7173 } 7174 llvm_unreachable("unexpected template argument loc"); 7175 } 7176 7177 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() { 7178 TemplateArgument Arg = readTemplateArgument(); 7179 7180 if (Arg.getKind() == TemplateArgument::Expression) { 7181 if (readBool()) // bool InfoHasSameExpr. 7182 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); 7183 } 7184 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind())); 7185 } 7186 7187 const ASTTemplateArgumentListInfo * 7188 ASTRecordReader::readASTTemplateArgumentListInfo() { 7189 SourceLocation LAngleLoc = readSourceLocation(); 7190 SourceLocation RAngleLoc = readSourceLocation(); 7191 unsigned NumArgsAsWritten = readInt(); 7192 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc); 7193 for (unsigned i = 0; i != NumArgsAsWritten; ++i) 7194 TemplArgsInfo.addArgument(readTemplateArgumentLoc()); 7195 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo); 7196 } 7197 7198 Decl *ASTReader::GetExternalDecl(uint32_t ID) { 7199 return GetDecl(ID); 7200 } 7201 7202 void ASTReader::CompleteRedeclChain(const Decl *D) { 7203 if (NumCurrentElementsDeserializing) { 7204 // We arrange to not care about the complete redeclaration chain while we're 7205 // deserializing. Just remember that the AST has marked this one as complete 7206 // but that it's not actually complete yet, so we know we still need to 7207 // complete it later. 7208 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D)); 7209 return; 7210 } 7211 7212 if (!D->getDeclContext()) { 7213 assert(isa<TranslationUnitDecl>(D) && "Not a TU?"); 7214 return; 7215 } 7216 7217 const DeclContext *DC = D->getDeclContext()->getRedeclContext(); 7218 7219 // If this is a named declaration, complete it by looking it up 7220 // within its context. 7221 // 7222 // FIXME: Merging a function definition should merge 7223 // all mergeable entities within it. 7224 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) || 7225 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) { 7226 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) { 7227 if (!getContext().getLangOpts().CPlusPlus && 7228 isa<TranslationUnitDecl>(DC)) { 7229 // Outside of C++, we don't have a lookup table for the TU, so update 7230 // the identifier instead. (For C++ modules, we don't store decls 7231 // in the serialized identifier table, so we do the lookup in the TU.) 7232 auto *II = Name.getAsIdentifierInfo(); 7233 assert(II && "non-identifier name in C?"); 7234 if (II->isOutOfDate()) 7235 updateOutOfDateIdentifier(*II); 7236 } else 7237 DC->lookup(Name); 7238 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) { 7239 // Find all declarations of this kind from the relevant context. 7240 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) { 7241 auto *DC = cast<DeclContext>(DCDecl); 7242 SmallVector<Decl*, 8> Decls; 7243 FindExternalLexicalDecls( 7244 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls); 7245 } 7246 } 7247 } 7248 7249 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) 7250 CTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7251 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) 7252 VTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7253 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 7254 if (auto *Template = FD->getPrimaryTemplate()) 7255 Template->LoadLazySpecializations(); 7256 } 7257 } 7258 7259 CXXCtorInitializer ** 7260 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) { 7261 RecordLocation Loc = getLocalBitOffset(Offset); 7262 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7263 SavedStreamPosition SavedPosition(Cursor); 7264 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7265 Error(std::move(Err)); 7266 return nullptr; 7267 } 7268 ReadingKindTracker ReadingKind(Read_Decl, *this); 7269 7270 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7271 if (!MaybeCode) { 7272 Error(MaybeCode.takeError()); 7273 return nullptr; 7274 } 7275 unsigned Code = MaybeCode.get(); 7276 7277 ASTRecordReader Record(*this, *Loc.F); 7278 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7279 if (!MaybeRecCode) { 7280 Error(MaybeRecCode.takeError()); 7281 return nullptr; 7282 } 7283 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) { 7284 Error("malformed AST file: missing C++ ctor initializers"); 7285 return nullptr; 7286 } 7287 7288 return Record.readCXXCtorInitializers(); 7289 } 7290 7291 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) { 7292 assert(ContextObj && "reading base specifiers with no AST context"); 7293 ASTContext &Context = *ContextObj; 7294 7295 RecordLocation Loc = getLocalBitOffset(Offset); 7296 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7297 SavedStreamPosition SavedPosition(Cursor); 7298 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7299 Error(std::move(Err)); 7300 return nullptr; 7301 } 7302 ReadingKindTracker ReadingKind(Read_Decl, *this); 7303 7304 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7305 if (!MaybeCode) { 7306 Error(MaybeCode.takeError()); 7307 return nullptr; 7308 } 7309 unsigned Code = MaybeCode.get(); 7310 7311 ASTRecordReader Record(*this, *Loc.F); 7312 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7313 if (!MaybeRecCode) { 7314 Error(MaybeCode.takeError()); 7315 return nullptr; 7316 } 7317 unsigned RecCode = MaybeRecCode.get(); 7318 7319 if (RecCode != DECL_CXX_BASE_SPECIFIERS) { 7320 Error("malformed AST file: missing C++ base specifiers"); 7321 return nullptr; 7322 } 7323 7324 unsigned NumBases = Record.readInt(); 7325 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases); 7326 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases]; 7327 for (unsigned I = 0; I != NumBases; ++I) 7328 Bases[I] = Record.readCXXBaseSpecifier(); 7329 return Bases; 7330 } 7331 7332 serialization::DeclID 7333 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const { 7334 if (LocalID < NUM_PREDEF_DECL_IDS) 7335 return LocalID; 7336 7337 if (!F.ModuleOffsetMap.empty()) 7338 ReadModuleOffsetMap(F); 7339 7340 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7341 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS); 7342 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap"); 7343 7344 return LocalID + I->second; 7345 } 7346 7347 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID, 7348 ModuleFile &M) const { 7349 // Predefined decls aren't from any module. 7350 if (ID < NUM_PREDEF_DECL_IDS) 7351 return false; 7352 7353 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID && 7354 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls; 7355 } 7356 7357 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) { 7358 if (!D->isFromASTFile()) 7359 return nullptr; 7360 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID()); 7361 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7362 return I->second; 7363 } 7364 7365 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) { 7366 if (ID < NUM_PREDEF_DECL_IDS) 7367 return SourceLocation(); 7368 7369 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7370 7371 if (Index > DeclsLoaded.size()) { 7372 Error("declaration ID out-of-range for AST file"); 7373 return SourceLocation(); 7374 } 7375 7376 if (Decl *D = DeclsLoaded[Index]) 7377 return D->getLocation(); 7378 7379 SourceLocation Loc; 7380 DeclCursorForID(ID, Loc); 7381 return Loc; 7382 } 7383 7384 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) { 7385 switch (ID) { 7386 case PREDEF_DECL_NULL_ID: 7387 return nullptr; 7388 7389 case PREDEF_DECL_TRANSLATION_UNIT_ID: 7390 return Context.getTranslationUnitDecl(); 7391 7392 case PREDEF_DECL_OBJC_ID_ID: 7393 return Context.getObjCIdDecl(); 7394 7395 case PREDEF_DECL_OBJC_SEL_ID: 7396 return Context.getObjCSelDecl(); 7397 7398 case PREDEF_DECL_OBJC_CLASS_ID: 7399 return Context.getObjCClassDecl(); 7400 7401 case PREDEF_DECL_OBJC_PROTOCOL_ID: 7402 return Context.getObjCProtocolDecl(); 7403 7404 case PREDEF_DECL_INT_128_ID: 7405 return Context.getInt128Decl(); 7406 7407 case PREDEF_DECL_UNSIGNED_INT_128_ID: 7408 return Context.getUInt128Decl(); 7409 7410 case PREDEF_DECL_OBJC_INSTANCETYPE_ID: 7411 return Context.getObjCInstanceTypeDecl(); 7412 7413 case PREDEF_DECL_BUILTIN_VA_LIST_ID: 7414 return Context.getBuiltinVaListDecl(); 7415 7416 case PREDEF_DECL_VA_LIST_TAG: 7417 return Context.getVaListTagDecl(); 7418 7419 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID: 7420 return Context.getBuiltinMSVaListDecl(); 7421 7422 case PREDEF_DECL_BUILTIN_MS_GUID_ID: 7423 return Context.getMSGuidTagDecl(); 7424 7425 case PREDEF_DECL_EXTERN_C_CONTEXT_ID: 7426 return Context.getExternCContextDecl(); 7427 7428 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID: 7429 return Context.getMakeIntegerSeqDecl(); 7430 7431 case PREDEF_DECL_CF_CONSTANT_STRING_ID: 7432 return Context.getCFConstantStringDecl(); 7433 7434 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID: 7435 return Context.getCFConstantStringTagDecl(); 7436 7437 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID: 7438 return Context.getTypePackElementDecl(); 7439 } 7440 llvm_unreachable("PredefinedDeclIDs unknown enum value"); 7441 } 7442 7443 Decl *ASTReader::GetExistingDecl(DeclID ID) { 7444 assert(ContextObj && "reading decl with no AST context"); 7445 if (ID < NUM_PREDEF_DECL_IDS) { 7446 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID); 7447 if (D) { 7448 // Track that we have merged the declaration with ID \p ID into the 7449 // pre-existing predefined declaration \p D. 7450 auto &Merged = KeyDecls[D->getCanonicalDecl()]; 7451 if (Merged.empty()) 7452 Merged.push_back(ID); 7453 } 7454 return D; 7455 } 7456 7457 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7458 7459 if (Index >= DeclsLoaded.size()) { 7460 assert(0 && "declaration ID out-of-range for AST file"); 7461 Error("declaration ID out-of-range for AST file"); 7462 return nullptr; 7463 } 7464 7465 return DeclsLoaded[Index]; 7466 } 7467 7468 Decl *ASTReader::GetDecl(DeclID ID) { 7469 if (ID < NUM_PREDEF_DECL_IDS) 7470 return GetExistingDecl(ID); 7471 7472 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7473 7474 if (Index >= DeclsLoaded.size()) { 7475 assert(0 && "declaration ID out-of-range for AST file"); 7476 Error("declaration ID out-of-range for AST file"); 7477 return nullptr; 7478 } 7479 7480 if (!DeclsLoaded[Index]) { 7481 ReadDeclRecord(ID); 7482 if (DeserializationListener) 7483 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); 7484 } 7485 7486 return DeclsLoaded[Index]; 7487 } 7488 7489 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M, 7490 DeclID GlobalID) { 7491 if (GlobalID < NUM_PREDEF_DECL_IDS) 7492 return GlobalID; 7493 7494 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID); 7495 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7496 ModuleFile *Owner = I->second; 7497 7498 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos 7499 = M.GlobalToLocalDeclIDs.find(Owner); 7500 if (Pos == M.GlobalToLocalDeclIDs.end()) 7501 return 0; 7502 7503 return GlobalID - Owner->BaseDeclID + Pos->second; 7504 } 7505 7506 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F, 7507 const RecordData &Record, 7508 unsigned &Idx) { 7509 if (Idx >= Record.size()) { 7510 Error("Corrupted AST file"); 7511 return 0; 7512 } 7513 7514 return getGlobalDeclID(F, Record[Idx++]); 7515 } 7516 7517 /// Resolve the offset of a statement into a statement. 7518 /// 7519 /// This operation will read a new statement from the external 7520 /// source each time it is called, and is meant to be used via a 7521 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 7522 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) { 7523 // Switch case IDs are per Decl. 7524 ClearSwitchCaseIDs(); 7525 7526 // Offset here is a global offset across the entire chain. 7527 RecordLocation Loc = getLocalBitOffset(Offset); 7528 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) { 7529 Error(std::move(Err)); 7530 return nullptr; 7531 } 7532 assert(NumCurrentElementsDeserializing == 0 && 7533 "should not be called while already deserializing"); 7534 Deserializing D(this); 7535 return ReadStmtFromStream(*Loc.F); 7536 } 7537 7538 void ASTReader::FindExternalLexicalDecls( 7539 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant, 7540 SmallVectorImpl<Decl *> &Decls) { 7541 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {}; 7542 7543 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) { 7544 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries"); 7545 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) { 7546 auto K = (Decl::Kind)+LexicalDecls[I]; 7547 if (!IsKindWeWant(K)) 7548 continue; 7549 7550 auto ID = (serialization::DeclID)+LexicalDecls[I + 1]; 7551 7552 // Don't add predefined declarations to the lexical context more 7553 // than once. 7554 if (ID < NUM_PREDEF_DECL_IDS) { 7555 if (PredefsVisited[ID]) 7556 continue; 7557 7558 PredefsVisited[ID] = true; 7559 } 7560 7561 if (Decl *D = GetLocalDecl(*M, ID)) { 7562 assert(D->getKind() == K && "wrong kind for lexical decl"); 7563 if (!DC->isDeclInLexicalTraversal(D)) 7564 Decls.push_back(D); 7565 } 7566 } 7567 }; 7568 7569 if (isa<TranslationUnitDecl>(DC)) { 7570 for (auto Lexical : TULexicalDecls) 7571 Visit(Lexical.first, Lexical.second); 7572 } else { 7573 auto I = LexicalDecls.find(DC); 7574 if (I != LexicalDecls.end()) 7575 Visit(I->second.first, I->second.second); 7576 } 7577 7578 ++NumLexicalDeclContextsRead; 7579 } 7580 7581 namespace { 7582 7583 class DeclIDComp { 7584 ASTReader &Reader; 7585 ModuleFile &Mod; 7586 7587 public: 7588 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {} 7589 7590 bool operator()(LocalDeclID L, LocalDeclID R) const { 7591 SourceLocation LHS = getLocation(L); 7592 SourceLocation RHS = getLocation(R); 7593 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7594 } 7595 7596 bool operator()(SourceLocation LHS, LocalDeclID R) const { 7597 SourceLocation RHS = getLocation(R); 7598 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7599 } 7600 7601 bool operator()(LocalDeclID L, SourceLocation RHS) const { 7602 SourceLocation LHS = getLocation(L); 7603 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7604 } 7605 7606 SourceLocation getLocation(LocalDeclID ID) const { 7607 return Reader.getSourceManager().getFileLoc( 7608 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID))); 7609 } 7610 }; 7611 7612 } // namespace 7613 7614 void ASTReader::FindFileRegionDecls(FileID File, 7615 unsigned Offset, unsigned Length, 7616 SmallVectorImpl<Decl *> &Decls) { 7617 SourceManager &SM = getSourceManager(); 7618 7619 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File); 7620 if (I == FileDeclIDs.end()) 7621 return; 7622 7623 FileDeclsInfo &DInfo = I->second; 7624 if (DInfo.Decls.empty()) 7625 return; 7626 7627 SourceLocation 7628 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset); 7629 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length); 7630 7631 DeclIDComp DIDComp(*this, *DInfo.Mod); 7632 ArrayRef<serialization::LocalDeclID>::iterator BeginIt = 7633 llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp); 7634 if (BeginIt != DInfo.Decls.begin()) 7635 --BeginIt; 7636 7637 // If we are pointing at a top-level decl inside an objc container, we need 7638 // to backtrack until we find it otherwise we will fail to report that the 7639 // region overlaps with an objc container. 7640 while (BeginIt != DInfo.Decls.begin() && 7641 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt)) 7642 ->isTopLevelDeclInObjCContainer()) 7643 --BeginIt; 7644 7645 ArrayRef<serialization::LocalDeclID>::iterator EndIt = 7646 llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp); 7647 if (EndIt != DInfo.Decls.end()) 7648 ++EndIt; 7649 7650 for (ArrayRef<serialization::LocalDeclID>::iterator 7651 DIt = BeginIt; DIt != EndIt; ++DIt) 7652 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt))); 7653 } 7654 7655 bool 7656 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, 7657 DeclarationName Name) { 7658 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() && 7659 "DeclContext has no visible decls in storage"); 7660 if (!Name) 7661 return false; 7662 7663 auto It = Lookups.find(DC); 7664 if (It == Lookups.end()) 7665 return false; 7666 7667 Deserializing LookupResults(this); 7668 7669 // Load the list of declarations. 7670 SmallVector<NamedDecl *, 64> Decls; 7671 llvm::SmallPtrSet<NamedDecl *, 8> Found; 7672 for (DeclID ID : It->second.Table.find(Name)) { 7673 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7674 if (ND->getDeclName() == Name && Found.insert(ND).second) 7675 Decls.push_back(ND); 7676 } 7677 7678 ++NumVisibleDeclContextsRead; 7679 SetExternalVisibleDeclsForName(DC, Name, Decls); 7680 return !Decls.empty(); 7681 } 7682 7683 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) { 7684 if (!DC->hasExternalVisibleStorage()) 7685 return; 7686 7687 auto It = Lookups.find(DC); 7688 assert(It != Lookups.end() && 7689 "have external visible storage but no lookup tables"); 7690 7691 DeclsMap Decls; 7692 7693 for (DeclID ID : It->second.Table.findAll()) { 7694 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7695 Decls[ND->getDeclName()].push_back(ND); 7696 } 7697 7698 ++NumVisibleDeclContextsRead; 7699 7700 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) { 7701 SetExternalVisibleDeclsForName(DC, I->first, I->second); 7702 } 7703 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false); 7704 } 7705 7706 const serialization::reader::DeclContextLookupTable * 7707 ASTReader::getLoadedLookupTables(DeclContext *Primary) const { 7708 auto I = Lookups.find(Primary); 7709 return I == Lookups.end() ? nullptr : &I->second; 7710 } 7711 7712 /// Under non-PCH compilation the consumer receives the objc methods 7713 /// before receiving the implementation, and codegen depends on this. 7714 /// We simulate this by deserializing and passing to consumer the methods of the 7715 /// implementation before passing the deserialized implementation decl. 7716 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD, 7717 ASTConsumer *Consumer) { 7718 assert(ImplD && Consumer); 7719 7720 for (auto *I : ImplD->methods()) 7721 Consumer->HandleInterestingDecl(DeclGroupRef(I)); 7722 7723 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD)); 7724 } 7725 7726 void ASTReader::PassInterestingDeclToConsumer(Decl *D) { 7727 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) 7728 PassObjCImplDeclToConsumer(ImplD, Consumer); 7729 else 7730 Consumer->HandleInterestingDecl(DeclGroupRef(D)); 7731 } 7732 7733 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) { 7734 this->Consumer = Consumer; 7735 7736 if (Consumer) 7737 PassInterestingDeclsToConsumer(); 7738 7739 if (DeserializationListener) 7740 DeserializationListener->ReaderInitialized(this); 7741 } 7742 7743 void ASTReader::PrintStats() { 7744 std::fprintf(stderr, "*** AST File Statistics:\n"); 7745 7746 unsigned NumTypesLoaded = 7747 TypesLoaded.size() - llvm::count(TypesLoaded, QualType()); 7748 unsigned NumDeclsLoaded = 7749 DeclsLoaded.size() - llvm::count(DeclsLoaded, (Decl *)nullptr); 7750 unsigned NumIdentifiersLoaded = 7751 IdentifiersLoaded.size() - 7752 llvm::count(IdentifiersLoaded, (IdentifierInfo *)nullptr); 7753 unsigned NumMacrosLoaded = 7754 MacrosLoaded.size() - llvm::count(MacrosLoaded, (MacroInfo *)nullptr); 7755 unsigned NumSelectorsLoaded = 7756 SelectorsLoaded.size() - llvm::count(SelectorsLoaded, Selector()); 7757 7758 if (unsigned TotalNumSLocEntries = getTotalNumSLocs()) 7759 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", 7760 NumSLocEntriesRead, TotalNumSLocEntries, 7761 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); 7762 if (!TypesLoaded.empty()) 7763 std::fprintf(stderr, " %u/%u types read (%f%%)\n", 7764 NumTypesLoaded, (unsigned)TypesLoaded.size(), 7765 ((float)NumTypesLoaded/TypesLoaded.size() * 100)); 7766 if (!DeclsLoaded.empty()) 7767 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", 7768 NumDeclsLoaded, (unsigned)DeclsLoaded.size(), 7769 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); 7770 if (!IdentifiersLoaded.empty()) 7771 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", 7772 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), 7773 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); 7774 if (!MacrosLoaded.empty()) 7775 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7776 NumMacrosLoaded, (unsigned)MacrosLoaded.size(), 7777 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100)); 7778 if (!SelectorsLoaded.empty()) 7779 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", 7780 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), 7781 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); 7782 if (TotalNumStatements) 7783 std::fprintf(stderr, " %u/%u statements read (%f%%)\n", 7784 NumStatementsRead, TotalNumStatements, 7785 ((float)NumStatementsRead/TotalNumStatements * 100)); 7786 if (TotalNumMacros) 7787 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7788 NumMacrosRead, TotalNumMacros, 7789 ((float)NumMacrosRead/TotalNumMacros * 100)); 7790 if (TotalLexicalDeclContexts) 7791 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", 7792 NumLexicalDeclContextsRead, TotalLexicalDeclContexts, 7793 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts 7794 * 100)); 7795 if (TotalVisibleDeclContexts) 7796 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", 7797 NumVisibleDeclContextsRead, TotalVisibleDeclContexts, 7798 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts 7799 * 100)); 7800 if (TotalNumMethodPoolEntries) 7801 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", 7802 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, 7803 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries 7804 * 100)); 7805 if (NumMethodPoolLookups) 7806 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n", 7807 NumMethodPoolHits, NumMethodPoolLookups, 7808 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0)); 7809 if (NumMethodPoolTableLookups) 7810 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n", 7811 NumMethodPoolTableHits, NumMethodPoolTableLookups, 7812 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups 7813 * 100.0)); 7814 if (NumIdentifierLookupHits) 7815 std::fprintf(stderr, 7816 " %u / %u identifier table lookups succeeded (%f%%)\n", 7817 NumIdentifierLookupHits, NumIdentifierLookups, 7818 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups); 7819 7820 if (GlobalIndex) { 7821 std::fprintf(stderr, "\n"); 7822 GlobalIndex->printStats(); 7823 } 7824 7825 std::fprintf(stderr, "\n"); 7826 dump(); 7827 std::fprintf(stderr, "\n"); 7828 } 7829 7830 template<typename Key, typename ModuleFile, unsigned InitialCapacity> 7831 LLVM_DUMP_METHOD static void 7832 dumpModuleIDMap(StringRef Name, 7833 const ContinuousRangeMap<Key, ModuleFile *, 7834 InitialCapacity> &Map) { 7835 if (Map.begin() == Map.end()) 7836 return; 7837 7838 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>; 7839 7840 llvm::errs() << Name << ":\n"; 7841 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end(); 7842 I != IEnd; ++I) { 7843 llvm::errs() << " " << I->first << " -> " << I->second->FileName 7844 << "\n"; 7845 } 7846 } 7847 7848 LLVM_DUMP_METHOD void ASTReader::dump() { 7849 llvm::errs() << "*** PCH/ModuleFile Remappings:\n"; 7850 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap); 7851 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap); 7852 dumpModuleIDMap("Global type map", GlobalTypeMap); 7853 dumpModuleIDMap("Global declaration map", GlobalDeclMap); 7854 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap); 7855 dumpModuleIDMap("Global macro map", GlobalMacroMap); 7856 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap); 7857 dumpModuleIDMap("Global selector map", GlobalSelectorMap); 7858 dumpModuleIDMap("Global preprocessed entity map", 7859 GlobalPreprocessedEntityMap); 7860 7861 llvm::errs() << "\n*** PCH/Modules Loaded:"; 7862 for (ModuleFile &M : ModuleMgr) 7863 M.dump(); 7864 } 7865 7866 /// Return the amount of memory used by memory buffers, breaking down 7867 /// by heap-backed versus mmap'ed memory. 7868 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const { 7869 for (ModuleFile &I : ModuleMgr) { 7870 if (llvm::MemoryBuffer *buf = I.Buffer) { 7871 size_t bytes = buf->getBufferSize(); 7872 switch (buf->getBufferKind()) { 7873 case llvm::MemoryBuffer::MemoryBuffer_Malloc: 7874 sizes.malloc_bytes += bytes; 7875 break; 7876 case llvm::MemoryBuffer::MemoryBuffer_MMap: 7877 sizes.mmap_bytes += bytes; 7878 break; 7879 } 7880 } 7881 } 7882 } 7883 7884 void ASTReader::InitializeSema(Sema &S) { 7885 SemaObj = &S; 7886 S.addExternalSource(this); 7887 7888 // Makes sure any declarations that were deserialized "too early" 7889 // still get added to the identifier's declaration chains. 7890 for (uint64_t ID : PreloadedDeclIDs) { 7891 NamedDecl *D = cast<NamedDecl>(GetDecl(ID)); 7892 pushExternalDeclIntoScope(D, D->getDeclName()); 7893 } 7894 PreloadedDeclIDs.clear(); 7895 7896 // FIXME: What happens if these are changed by a module import? 7897 if (!FPPragmaOptions.empty()) { 7898 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS"); 7899 FPOptionsOverride NewOverrides = 7900 FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]); 7901 SemaObj->CurFPFeatures = 7902 NewOverrides.applyOverrides(SemaObj->getLangOpts()); 7903 } 7904 7905 SemaObj->OpenCLFeatures = OpenCLExtensions; 7906 7907 UpdateSema(); 7908 } 7909 7910 void ASTReader::UpdateSema() { 7911 assert(SemaObj && "no Sema to update"); 7912 7913 // Load the offsets of the declarations that Sema references. 7914 // They will be lazily deserialized when needed. 7915 if (!SemaDeclRefs.empty()) { 7916 assert(SemaDeclRefs.size() % 3 == 0); 7917 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) { 7918 if (!SemaObj->StdNamespace) 7919 SemaObj->StdNamespace = SemaDeclRefs[I]; 7920 if (!SemaObj->StdBadAlloc) 7921 SemaObj->StdBadAlloc = SemaDeclRefs[I+1]; 7922 if (!SemaObj->StdAlignValT) 7923 SemaObj->StdAlignValT = SemaDeclRefs[I+2]; 7924 } 7925 SemaDeclRefs.clear(); 7926 } 7927 7928 // Update the state of pragmas. Use the same API as if we had encountered the 7929 // pragma in the source. 7930 if(OptimizeOffPragmaLocation.isValid()) 7931 SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation); 7932 if (PragmaMSStructState != -1) 7933 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState); 7934 if (PointersToMembersPragmaLocation.isValid()) { 7935 SemaObj->ActOnPragmaMSPointersToMembers( 7936 (LangOptions::PragmaMSPointersToMembersKind) 7937 PragmaMSPointersToMembersState, 7938 PointersToMembersPragmaLocation); 7939 } 7940 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth; 7941 7942 if (PragmaAlignPackCurrentValue) { 7943 // The bottom of the stack might have a default value. It must be adjusted 7944 // to the current value to ensure that the packing state is preserved after 7945 // popping entries that were included/imported from a PCH/module. 7946 bool DropFirst = false; 7947 if (!PragmaAlignPackStack.empty() && 7948 PragmaAlignPackStack.front().Location.isInvalid()) { 7949 assert(PragmaAlignPackStack.front().Value == 7950 SemaObj->AlignPackStack.DefaultValue && 7951 "Expected a default alignment value"); 7952 SemaObj->AlignPackStack.Stack.emplace_back( 7953 PragmaAlignPackStack.front().SlotLabel, 7954 SemaObj->AlignPackStack.CurrentValue, 7955 SemaObj->AlignPackStack.CurrentPragmaLocation, 7956 PragmaAlignPackStack.front().PushLocation); 7957 DropFirst = true; 7958 } 7959 for (const auto &Entry : llvm::makeArrayRef(PragmaAlignPackStack) 7960 .drop_front(DropFirst ? 1 : 0)) { 7961 SemaObj->AlignPackStack.Stack.emplace_back( 7962 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation); 7963 } 7964 if (PragmaAlignPackCurrentLocation.isInvalid()) { 7965 assert(*PragmaAlignPackCurrentValue == 7966 SemaObj->AlignPackStack.DefaultValue && 7967 "Expected a default align and pack value"); 7968 // Keep the current values. 7969 } else { 7970 SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue; 7971 SemaObj->AlignPackStack.CurrentPragmaLocation = 7972 PragmaAlignPackCurrentLocation; 7973 } 7974 } 7975 if (FpPragmaCurrentValue) { 7976 // The bottom of the stack might have a default value. It must be adjusted 7977 // to the current value to ensure that fp-pragma state is preserved after 7978 // popping entries that were included/imported from a PCH/module. 7979 bool DropFirst = false; 7980 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) { 7981 assert(FpPragmaStack.front().Value == 7982 SemaObj->FpPragmaStack.DefaultValue && 7983 "Expected a default pragma float_control value"); 7984 SemaObj->FpPragmaStack.Stack.emplace_back( 7985 FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue, 7986 SemaObj->FpPragmaStack.CurrentPragmaLocation, 7987 FpPragmaStack.front().PushLocation); 7988 DropFirst = true; 7989 } 7990 for (const auto &Entry : 7991 llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0)) 7992 SemaObj->FpPragmaStack.Stack.emplace_back( 7993 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation); 7994 if (FpPragmaCurrentLocation.isInvalid()) { 7995 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue && 7996 "Expected a default pragma float_control value"); 7997 // Keep the current values. 7998 } else { 7999 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue; 8000 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation; 8001 } 8002 } 8003 8004 // For non-modular AST files, restore visiblity of modules. 8005 for (auto &Import : ImportedModules) { 8006 if (Import.ImportLoc.isInvalid()) 8007 continue; 8008 if (Module *Imported = getSubmodule(Import.ID)) { 8009 SemaObj->makeModuleVisible(Imported, Import.ImportLoc); 8010 } 8011 } 8012 } 8013 8014 IdentifierInfo *ASTReader::get(StringRef Name) { 8015 // Note that we are loading an identifier. 8016 Deserializing AnIdentifier(this); 8017 8018 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0, 8019 NumIdentifierLookups, 8020 NumIdentifierLookupHits); 8021 8022 // We don't need to do identifier table lookups in C++ modules (we preload 8023 // all interesting declarations, and don't need to use the scope for name 8024 // lookups). Perform the lookup in PCH files, though, since we don't build 8025 // a complete initial identifier table if we're carrying on from a PCH. 8026 if (PP.getLangOpts().CPlusPlus) { 8027 for (auto F : ModuleMgr.pch_modules()) 8028 if (Visitor(*F)) 8029 break; 8030 } else { 8031 // If there is a global index, look there first to determine which modules 8032 // provably do not have any results for this identifier. 8033 GlobalModuleIndex::HitSet Hits; 8034 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 8035 if (!loadGlobalIndex()) { 8036 if (GlobalIndex->lookupIdentifier(Name, Hits)) { 8037 HitsPtr = &Hits; 8038 } 8039 } 8040 8041 ModuleMgr.visit(Visitor, HitsPtr); 8042 } 8043 8044 IdentifierInfo *II = Visitor.getIdentifierInfo(); 8045 markIdentifierUpToDate(II); 8046 return II; 8047 } 8048 8049 namespace clang { 8050 8051 /// An identifier-lookup iterator that enumerates all of the 8052 /// identifiers stored within a set of AST files. 8053 class ASTIdentifierIterator : public IdentifierIterator { 8054 /// The AST reader whose identifiers are being enumerated. 8055 const ASTReader &Reader; 8056 8057 /// The current index into the chain of AST files stored in 8058 /// the AST reader. 8059 unsigned Index; 8060 8061 /// The current position within the identifier lookup table 8062 /// of the current AST file. 8063 ASTIdentifierLookupTable::key_iterator Current; 8064 8065 /// The end position within the identifier lookup table of 8066 /// the current AST file. 8067 ASTIdentifierLookupTable::key_iterator End; 8068 8069 /// Whether to skip any modules in the ASTReader. 8070 bool SkipModules; 8071 8072 public: 8073 explicit ASTIdentifierIterator(const ASTReader &Reader, 8074 bool SkipModules = false); 8075 8076 StringRef Next() override; 8077 }; 8078 8079 } // namespace clang 8080 8081 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader, 8082 bool SkipModules) 8083 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) { 8084 } 8085 8086 StringRef ASTIdentifierIterator::Next() { 8087 while (Current == End) { 8088 // If we have exhausted all of our AST files, we're done. 8089 if (Index == 0) 8090 return StringRef(); 8091 8092 --Index; 8093 ModuleFile &F = Reader.ModuleMgr[Index]; 8094 if (SkipModules && F.isModule()) 8095 continue; 8096 8097 ASTIdentifierLookupTable *IdTable = 8098 (ASTIdentifierLookupTable *)F.IdentifierLookupTable; 8099 Current = IdTable->key_begin(); 8100 End = IdTable->key_end(); 8101 } 8102 8103 // We have any identifiers remaining in the current AST file; return 8104 // the next one. 8105 StringRef Result = *Current; 8106 ++Current; 8107 return Result; 8108 } 8109 8110 namespace { 8111 8112 /// A utility for appending two IdentifierIterators. 8113 class ChainedIdentifierIterator : public IdentifierIterator { 8114 std::unique_ptr<IdentifierIterator> Current; 8115 std::unique_ptr<IdentifierIterator> Queued; 8116 8117 public: 8118 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First, 8119 std::unique_ptr<IdentifierIterator> Second) 8120 : Current(std::move(First)), Queued(std::move(Second)) {} 8121 8122 StringRef Next() override { 8123 if (!Current) 8124 return StringRef(); 8125 8126 StringRef result = Current->Next(); 8127 if (!result.empty()) 8128 return result; 8129 8130 // Try the queued iterator, which may itself be empty. 8131 Current.reset(); 8132 std::swap(Current, Queued); 8133 return Next(); 8134 } 8135 }; 8136 8137 } // namespace 8138 8139 IdentifierIterator *ASTReader::getIdentifiers() { 8140 if (!loadGlobalIndex()) { 8141 std::unique_ptr<IdentifierIterator> ReaderIter( 8142 new ASTIdentifierIterator(*this, /*SkipModules=*/true)); 8143 std::unique_ptr<IdentifierIterator> ModulesIter( 8144 GlobalIndex->createIdentifierIterator()); 8145 return new ChainedIdentifierIterator(std::move(ReaderIter), 8146 std::move(ModulesIter)); 8147 } 8148 8149 return new ASTIdentifierIterator(*this); 8150 } 8151 8152 namespace clang { 8153 namespace serialization { 8154 8155 class ReadMethodPoolVisitor { 8156 ASTReader &Reader; 8157 Selector Sel; 8158 unsigned PriorGeneration; 8159 unsigned InstanceBits = 0; 8160 unsigned FactoryBits = 0; 8161 bool InstanceHasMoreThanOneDecl = false; 8162 bool FactoryHasMoreThanOneDecl = false; 8163 SmallVector<ObjCMethodDecl *, 4> InstanceMethods; 8164 SmallVector<ObjCMethodDecl *, 4> FactoryMethods; 8165 8166 public: 8167 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel, 8168 unsigned PriorGeneration) 8169 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {} 8170 8171 bool operator()(ModuleFile &M) { 8172 if (!M.SelectorLookupTable) 8173 return false; 8174 8175 // If we've already searched this module file, skip it now. 8176 if (M.Generation <= PriorGeneration) 8177 return true; 8178 8179 ++Reader.NumMethodPoolTableLookups; 8180 ASTSelectorLookupTable *PoolTable 8181 = (ASTSelectorLookupTable*)M.SelectorLookupTable; 8182 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel); 8183 if (Pos == PoolTable->end()) 8184 return false; 8185 8186 ++Reader.NumMethodPoolTableHits; 8187 ++Reader.NumSelectorsRead; 8188 // FIXME: Not quite happy with the statistics here. We probably should 8189 // disable this tracking when called via LoadSelector. 8190 // Also, should entries without methods count as misses? 8191 ++Reader.NumMethodPoolEntriesRead; 8192 ASTSelectorLookupTrait::data_type Data = *Pos; 8193 if (Reader.DeserializationListener) 8194 Reader.DeserializationListener->SelectorRead(Data.ID, Sel); 8195 8196 // Append methods in the reverse order, so that later we can process them 8197 // in the order they appear in the source code by iterating through 8198 // the vector in the reverse order. 8199 InstanceMethods.append(Data.Instance.rbegin(), Data.Instance.rend()); 8200 FactoryMethods.append(Data.Factory.rbegin(), Data.Factory.rend()); 8201 InstanceBits = Data.InstanceBits; 8202 FactoryBits = Data.FactoryBits; 8203 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl; 8204 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl; 8205 return false; 8206 } 8207 8208 /// Retrieve the instance methods found by this visitor. 8209 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const { 8210 return InstanceMethods; 8211 } 8212 8213 /// Retrieve the instance methods found by this visitor. 8214 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const { 8215 return FactoryMethods; 8216 } 8217 8218 unsigned getInstanceBits() const { return InstanceBits; } 8219 unsigned getFactoryBits() const { return FactoryBits; } 8220 8221 bool instanceHasMoreThanOneDecl() const { 8222 return InstanceHasMoreThanOneDecl; 8223 } 8224 8225 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; } 8226 }; 8227 8228 } // namespace serialization 8229 } // namespace clang 8230 8231 /// Add the given set of methods to the method list. 8232 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods, 8233 ObjCMethodList &List) { 8234 for (auto I = Methods.rbegin(), E = Methods.rend(); I != E; ++I) 8235 S.addMethodToGlobalList(&List, *I); 8236 } 8237 8238 void ASTReader::ReadMethodPool(Selector Sel) { 8239 // Get the selector generation and update it to the current generation. 8240 unsigned &Generation = SelectorGeneration[Sel]; 8241 unsigned PriorGeneration = Generation; 8242 Generation = getGeneration(); 8243 SelectorOutOfDate[Sel] = false; 8244 8245 // Search for methods defined with this selector. 8246 ++NumMethodPoolLookups; 8247 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration); 8248 ModuleMgr.visit(Visitor); 8249 8250 if (Visitor.getInstanceMethods().empty() && 8251 Visitor.getFactoryMethods().empty()) 8252 return; 8253 8254 ++NumMethodPoolHits; 8255 8256 if (!getSema()) 8257 return; 8258 8259 Sema &S = *getSema(); 8260 Sema::GlobalMethodPool::iterator Pos = 8261 S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethodPool::Lists())) 8262 .first; 8263 8264 Pos->second.first.setBits(Visitor.getInstanceBits()); 8265 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl()); 8266 Pos->second.second.setBits(Visitor.getFactoryBits()); 8267 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl()); 8268 8269 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since 8270 // when building a module we keep every method individually and may need to 8271 // update hasMoreThanOneDecl as we add the methods. 8272 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first); 8273 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second); 8274 } 8275 8276 void ASTReader::updateOutOfDateSelector(Selector Sel) { 8277 if (SelectorOutOfDate[Sel]) 8278 ReadMethodPool(Sel); 8279 } 8280 8281 void ASTReader::ReadKnownNamespaces( 8282 SmallVectorImpl<NamespaceDecl *> &Namespaces) { 8283 Namespaces.clear(); 8284 8285 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) { 8286 if (NamespaceDecl *Namespace 8287 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I]))) 8288 Namespaces.push_back(Namespace); 8289 } 8290 } 8291 8292 void ASTReader::ReadUndefinedButUsed( 8293 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) { 8294 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) { 8295 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++])); 8296 SourceLocation Loc = 8297 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]); 8298 Undefined.insert(std::make_pair(D, Loc)); 8299 } 8300 } 8301 8302 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector< 8303 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> & 8304 Exprs) { 8305 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) { 8306 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++])); 8307 uint64_t Count = DelayedDeleteExprs[Idx++]; 8308 for (uint64_t C = 0; C < Count; ++C) { 8309 SourceLocation DeleteLoc = 8310 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]); 8311 const bool IsArrayForm = DelayedDeleteExprs[Idx++]; 8312 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm)); 8313 } 8314 } 8315 } 8316 8317 void ASTReader::ReadTentativeDefinitions( 8318 SmallVectorImpl<VarDecl *> &TentativeDefs) { 8319 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { 8320 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I])); 8321 if (Var) 8322 TentativeDefs.push_back(Var); 8323 } 8324 TentativeDefinitions.clear(); 8325 } 8326 8327 void ASTReader::ReadUnusedFileScopedDecls( 8328 SmallVectorImpl<const DeclaratorDecl *> &Decls) { 8329 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) { 8330 DeclaratorDecl *D 8331 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I])); 8332 if (D) 8333 Decls.push_back(D); 8334 } 8335 UnusedFileScopedDecls.clear(); 8336 } 8337 8338 void ASTReader::ReadDelegatingConstructors( 8339 SmallVectorImpl<CXXConstructorDecl *> &Decls) { 8340 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) { 8341 CXXConstructorDecl *D 8342 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I])); 8343 if (D) 8344 Decls.push_back(D); 8345 } 8346 DelegatingCtorDecls.clear(); 8347 } 8348 8349 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) { 8350 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) { 8351 TypedefNameDecl *D 8352 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])); 8353 if (D) 8354 Decls.push_back(D); 8355 } 8356 ExtVectorDecls.clear(); 8357 } 8358 8359 void ASTReader::ReadUnusedLocalTypedefNameCandidates( 8360 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) { 8361 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N; 8362 ++I) { 8363 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>( 8364 GetDecl(UnusedLocalTypedefNameCandidates[I])); 8365 if (D) 8366 Decls.insert(D); 8367 } 8368 UnusedLocalTypedefNameCandidates.clear(); 8369 } 8370 8371 void ASTReader::ReadDeclsToCheckForDeferredDiags( 8372 llvm::SmallSetVector<Decl *, 4> &Decls) { 8373 for (auto I : DeclsToCheckForDeferredDiags) { 8374 auto *D = dyn_cast_or_null<Decl>(GetDecl(I)); 8375 if (D) 8376 Decls.insert(D); 8377 } 8378 DeclsToCheckForDeferredDiags.clear(); 8379 } 8380 8381 void ASTReader::ReadReferencedSelectors( 8382 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) { 8383 if (ReferencedSelectorsData.empty()) 8384 return; 8385 8386 // If there are @selector references added them to its pool. This is for 8387 // implementation of -Wselector. 8388 unsigned int DataSize = ReferencedSelectorsData.size()-1; 8389 unsigned I = 0; 8390 while (I < DataSize) { 8391 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]); 8392 SourceLocation SelLoc 8393 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]); 8394 Sels.push_back(std::make_pair(Sel, SelLoc)); 8395 } 8396 ReferencedSelectorsData.clear(); 8397 } 8398 8399 void ASTReader::ReadWeakUndeclaredIdentifiers( 8400 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) { 8401 if (WeakUndeclaredIdentifiers.empty()) 8402 return; 8403 8404 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) { 8405 IdentifierInfo *WeakId 8406 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8407 IdentifierInfo *AliasId 8408 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8409 SourceLocation Loc = 8410 SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]); 8411 WeakInfo WI(AliasId, Loc); 8412 WeakIDs.push_back(std::make_pair(WeakId, WI)); 8413 } 8414 WeakUndeclaredIdentifiers.clear(); 8415 } 8416 8417 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) { 8418 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) { 8419 ExternalVTableUse VT; 8420 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); 8421 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]); 8422 VT.DefinitionRequired = VTableUses[Idx++]; 8423 VTables.push_back(VT); 8424 } 8425 8426 VTableUses.clear(); 8427 } 8428 8429 void ASTReader::ReadPendingInstantiations( 8430 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) { 8431 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) { 8432 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++])); 8433 SourceLocation Loc 8434 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]); 8435 8436 Pending.push_back(std::make_pair(D, Loc)); 8437 } 8438 PendingInstantiations.clear(); 8439 } 8440 8441 void ASTReader::ReadLateParsedTemplates( 8442 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>> 8443 &LPTMap) { 8444 for (auto &LPT : LateParsedTemplates) { 8445 ModuleFile *FMod = LPT.first; 8446 RecordDataImpl &LateParsed = LPT.second; 8447 for (unsigned Idx = 0, N = LateParsed.size(); Idx < N; 8448 /* In loop */) { 8449 FunctionDecl *FD = 8450 cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++])); 8451 8452 auto LT = std::make_unique<LateParsedTemplate>(); 8453 LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]); 8454 8455 ModuleFile *F = getOwningModuleFile(LT->D); 8456 assert(F && "No module"); 8457 8458 unsigned TokN = LateParsed[Idx++]; 8459 LT->Toks.reserve(TokN); 8460 for (unsigned T = 0; T < TokN; ++T) 8461 LT->Toks.push_back(ReadToken(*F, LateParsed, Idx)); 8462 8463 LPTMap.insert(std::make_pair(FD, std::move(LT))); 8464 } 8465 } 8466 8467 LateParsedTemplates.clear(); 8468 } 8469 8470 void ASTReader::LoadSelector(Selector Sel) { 8471 // It would be complicated to avoid reading the methods anyway. So don't. 8472 ReadMethodPool(Sel); 8473 } 8474 8475 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) { 8476 assert(ID && "Non-zero identifier ID required"); 8477 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); 8478 IdentifiersLoaded[ID - 1] = II; 8479 if (DeserializationListener) 8480 DeserializationListener->IdentifierRead(ID, II); 8481 } 8482 8483 /// Set the globally-visible declarations associated with the given 8484 /// identifier. 8485 /// 8486 /// If the AST reader is currently in a state where the given declaration IDs 8487 /// cannot safely be resolved, they are queued until it is safe to resolve 8488 /// them. 8489 /// 8490 /// \param II an IdentifierInfo that refers to one or more globally-visible 8491 /// declarations. 8492 /// 8493 /// \param DeclIDs the set of declaration IDs with the name @p II that are 8494 /// visible at global scope. 8495 /// 8496 /// \param Decls if non-null, this vector will be populated with the set of 8497 /// deserialized declarations. These declarations will not be pushed into 8498 /// scope. 8499 void 8500 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, 8501 const SmallVectorImpl<uint32_t> &DeclIDs, 8502 SmallVectorImpl<Decl *> *Decls) { 8503 if (NumCurrentElementsDeserializing && !Decls) { 8504 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end()); 8505 return; 8506 } 8507 8508 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { 8509 if (!SemaObj) { 8510 // Queue this declaration so that it will be added to the 8511 // translation unit scope and identifier's declaration chain 8512 // once a Sema object is known. 8513 PreloadedDeclIDs.push_back(DeclIDs[I]); 8514 continue; 8515 } 8516 8517 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); 8518 8519 // If we're simply supposed to record the declarations, do so now. 8520 if (Decls) { 8521 Decls->push_back(D); 8522 continue; 8523 } 8524 8525 // Introduce this declaration into the translation-unit scope 8526 // and add it to the declaration chain for this identifier, so 8527 // that (unqualified) name lookup will find it. 8528 pushExternalDeclIntoScope(D, II); 8529 } 8530 } 8531 8532 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) { 8533 if (ID == 0) 8534 return nullptr; 8535 8536 if (IdentifiersLoaded.empty()) { 8537 Error("no identifier table in AST file"); 8538 return nullptr; 8539 } 8540 8541 ID -= 1; 8542 if (!IdentifiersLoaded[ID]) { 8543 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1); 8544 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map"); 8545 ModuleFile *M = I->second; 8546 unsigned Index = ID - M->BaseIdentifierID; 8547 const unsigned char *Data = 8548 M->IdentifierTableData + M->IdentifierOffsets[Index]; 8549 8550 ASTIdentifierLookupTrait Trait(*this, *M); 8551 auto KeyDataLen = Trait.ReadKeyDataLength(Data); 8552 auto Key = Trait.ReadKey(Data, KeyDataLen.first); 8553 auto &II = PP.getIdentifierTable().get(Key); 8554 IdentifiersLoaded[ID] = &II; 8555 markIdentifierFromAST(*this, II); 8556 if (DeserializationListener) 8557 DeserializationListener->IdentifierRead(ID + 1, &II); 8558 } 8559 8560 return IdentifiersLoaded[ID]; 8561 } 8562 8563 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) { 8564 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID)); 8565 } 8566 8567 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) { 8568 if (LocalID < NUM_PREDEF_IDENT_IDS) 8569 return LocalID; 8570 8571 if (!M.ModuleOffsetMap.empty()) 8572 ReadModuleOffsetMap(M); 8573 8574 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8575 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS); 8576 assert(I != M.IdentifierRemap.end() 8577 && "Invalid index into identifier index remap"); 8578 8579 return LocalID + I->second; 8580 } 8581 8582 MacroInfo *ASTReader::getMacro(MacroID ID) { 8583 if (ID == 0) 8584 return nullptr; 8585 8586 if (MacrosLoaded.empty()) { 8587 Error("no macro table in AST file"); 8588 return nullptr; 8589 } 8590 8591 ID -= NUM_PREDEF_MACRO_IDS; 8592 if (!MacrosLoaded[ID]) { 8593 GlobalMacroMapType::iterator I 8594 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS); 8595 assert(I != GlobalMacroMap.end() && "Corrupted global macro map"); 8596 ModuleFile *M = I->second; 8597 unsigned Index = ID - M->BaseMacroID; 8598 MacrosLoaded[ID] = 8599 ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]); 8600 8601 if (DeserializationListener) 8602 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS, 8603 MacrosLoaded[ID]); 8604 } 8605 8606 return MacrosLoaded[ID]; 8607 } 8608 8609 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) { 8610 if (LocalID < NUM_PREDEF_MACRO_IDS) 8611 return LocalID; 8612 8613 if (!M.ModuleOffsetMap.empty()) 8614 ReadModuleOffsetMap(M); 8615 8616 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8617 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS); 8618 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap"); 8619 8620 return LocalID + I->second; 8621 } 8622 8623 serialization::SubmoduleID 8624 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) { 8625 if (LocalID < NUM_PREDEF_SUBMODULE_IDS) 8626 return LocalID; 8627 8628 if (!M.ModuleOffsetMap.empty()) 8629 ReadModuleOffsetMap(M); 8630 8631 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8632 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS); 8633 assert(I != M.SubmoduleRemap.end() 8634 && "Invalid index into submodule index remap"); 8635 8636 return LocalID + I->second; 8637 } 8638 8639 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) { 8640 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) { 8641 assert(GlobalID == 0 && "Unhandled global submodule ID"); 8642 return nullptr; 8643 } 8644 8645 if (GlobalID > SubmodulesLoaded.size()) { 8646 Error("submodule ID out of range in AST file"); 8647 return nullptr; 8648 } 8649 8650 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS]; 8651 } 8652 8653 Module *ASTReader::getModule(unsigned ID) { 8654 return getSubmodule(ID); 8655 } 8656 8657 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) { 8658 if (ID & 1) { 8659 // It's a module, look it up by submodule ID. 8660 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1)); 8661 return I == GlobalSubmoduleMap.end() ? nullptr : I->second; 8662 } else { 8663 // It's a prefix (preamble, PCH, ...). Look it up by index. 8664 unsigned IndexFromEnd = ID >> 1; 8665 assert(IndexFromEnd && "got reference to unknown module file"); 8666 return getModuleManager().pch_modules().end()[-IndexFromEnd]; 8667 } 8668 } 8669 8670 unsigned ASTReader::getModuleFileID(ModuleFile *F) { 8671 if (!F) 8672 return 1; 8673 8674 // For a file representing a module, use the submodule ID of the top-level 8675 // module as the file ID. For any other kind of file, the number of such 8676 // files loaded beforehand will be the same on reload. 8677 // FIXME: Is this true even if we have an explicit module file and a PCH? 8678 if (F->isModule()) 8679 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1; 8680 8681 auto PCHModules = getModuleManager().pch_modules(); 8682 auto I = llvm::find(PCHModules, F); 8683 assert(I != PCHModules.end() && "emitting reference to unknown file"); 8684 return (I - PCHModules.end()) << 1; 8685 } 8686 8687 llvm::Optional<ASTSourceDescriptor> 8688 ASTReader::getSourceDescriptor(unsigned ID) { 8689 if (Module *M = getSubmodule(ID)) 8690 return ASTSourceDescriptor(*M); 8691 8692 // If there is only a single PCH, return it instead. 8693 // Chained PCH are not supported. 8694 const auto &PCHChain = ModuleMgr.pch_modules(); 8695 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) { 8696 ModuleFile &MF = ModuleMgr.getPrimaryModule(); 8697 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName); 8698 StringRef FileName = llvm::sys::path::filename(MF.FileName); 8699 return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName, 8700 MF.Signature); 8701 } 8702 return None; 8703 } 8704 8705 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) { 8706 auto I = DefinitionSource.find(FD); 8707 if (I == DefinitionSource.end()) 8708 return EK_ReplyHazy; 8709 return I->second ? EK_Never : EK_Always; 8710 } 8711 8712 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) { 8713 return DecodeSelector(getGlobalSelectorID(M, LocalID)); 8714 } 8715 8716 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) { 8717 if (ID == 0) 8718 return Selector(); 8719 8720 if (ID > SelectorsLoaded.size()) { 8721 Error("selector ID out of range in AST file"); 8722 return Selector(); 8723 } 8724 8725 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) { 8726 // Load this selector from the selector table. 8727 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID); 8728 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map"); 8729 ModuleFile &M = *I->second; 8730 ASTSelectorLookupTrait Trait(*this, M); 8731 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS; 8732 SelectorsLoaded[ID - 1] = 8733 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0); 8734 if (DeserializationListener) 8735 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 8736 } 8737 8738 return SelectorsLoaded[ID - 1]; 8739 } 8740 8741 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) { 8742 return DecodeSelector(ID); 8743 } 8744 8745 uint32_t ASTReader::GetNumExternalSelectors() { 8746 // ID 0 (the null selector) is considered an external selector. 8747 return getTotalNumSelectors() + 1; 8748 } 8749 8750 serialization::SelectorID 8751 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const { 8752 if (LocalID < NUM_PREDEF_SELECTOR_IDS) 8753 return LocalID; 8754 8755 if (!M.ModuleOffsetMap.empty()) 8756 ReadModuleOffsetMap(M); 8757 8758 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8759 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS); 8760 assert(I != M.SelectorRemap.end() 8761 && "Invalid index into selector index remap"); 8762 8763 return LocalID + I->second; 8764 } 8765 8766 DeclarationNameLoc 8767 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) { 8768 switch (Name.getNameKind()) { 8769 case DeclarationName::CXXConstructorName: 8770 case DeclarationName::CXXDestructorName: 8771 case DeclarationName::CXXConversionFunctionName: 8772 return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo()); 8773 8774 case DeclarationName::CXXOperatorName: 8775 return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange()); 8776 8777 case DeclarationName::CXXLiteralOperatorName: 8778 return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc( 8779 readSourceLocation()); 8780 8781 case DeclarationName::Identifier: 8782 case DeclarationName::ObjCZeroArgSelector: 8783 case DeclarationName::ObjCOneArgSelector: 8784 case DeclarationName::ObjCMultiArgSelector: 8785 case DeclarationName::CXXUsingDirective: 8786 case DeclarationName::CXXDeductionGuideName: 8787 break; 8788 } 8789 return DeclarationNameLoc(); 8790 } 8791 8792 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() { 8793 DeclarationNameInfo NameInfo; 8794 NameInfo.setName(readDeclarationName()); 8795 NameInfo.setLoc(readSourceLocation()); 8796 NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName())); 8797 return NameInfo; 8798 } 8799 8800 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) { 8801 Info.QualifierLoc = readNestedNameSpecifierLoc(); 8802 unsigned NumTPLists = readInt(); 8803 Info.NumTemplParamLists = NumTPLists; 8804 if (NumTPLists) { 8805 Info.TemplParamLists = 8806 new (getContext()) TemplateParameterList *[NumTPLists]; 8807 for (unsigned i = 0; i != NumTPLists; ++i) 8808 Info.TemplParamLists[i] = readTemplateParameterList(); 8809 } 8810 } 8811 8812 TemplateParameterList * 8813 ASTRecordReader::readTemplateParameterList() { 8814 SourceLocation TemplateLoc = readSourceLocation(); 8815 SourceLocation LAngleLoc = readSourceLocation(); 8816 SourceLocation RAngleLoc = readSourceLocation(); 8817 8818 unsigned NumParams = readInt(); 8819 SmallVector<NamedDecl *, 16> Params; 8820 Params.reserve(NumParams); 8821 while (NumParams--) 8822 Params.push_back(readDeclAs<NamedDecl>()); 8823 8824 bool HasRequiresClause = readBool(); 8825 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr; 8826 8827 TemplateParameterList *TemplateParams = TemplateParameterList::Create( 8828 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause); 8829 return TemplateParams; 8830 } 8831 8832 void ASTRecordReader::readTemplateArgumentList( 8833 SmallVectorImpl<TemplateArgument> &TemplArgs, 8834 bool Canonicalize) { 8835 unsigned NumTemplateArgs = readInt(); 8836 TemplArgs.reserve(NumTemplateArgs); 8837 while (NumTemplateArgs--) 8838 TemplArgs.push_back(readTemplateArgument(Canonicalize)); 8839 } 8840 8841 /// Read a UnresolvedSet structure. 8842 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) { 8843 unsigned NumDecls = readInt(); 8844 Set.reserve(getContext(), NumDecls); 8845 while (NumDecls--) { 8846 DeclID ID = readDeclID(); 8847 AccessSpecifier AS = (AccessSpecifier) readInt(); 8848 Set.addLazyDecl(getContext(), ID, AS); 8849 } 8850 } 8851 8852 CXXBaseSpecifier 8853 ASTRecordReader::readCXXBaseSpecifier() { 8854 bool isVirtual = readBool(); 8855 bool isBaseOfClass = readBool(); 8856 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt()); 8857 bool inheritConstructors = readBool(); 8858 TypeSourceInfo *TInfo = readTypeSourceInfo(); 8859 SourceRange Range = readSourceRange(); 8860 SourceLocation EllipsisLoc = readSourceLocation(); 8861 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo, 8862 EllipsisLoc); 8863 Result.setInheritConstructors(inheritConstructors); 8864 return Result; 8865 } 8866 8867 CXXCtorInitializer ** 8868 ASTRecordReader::readCXXCtorInitializers() { 8869 ASTContext &Context = getContext(); 8870 unsigned NumInitializers = readInt(); 8871 assert(NumInitializers && "wrote ctor initializers but have no inits"); 8872 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers]; 8873 for (unsigned i = 0; i != NumInitializers; ++i) { 8874 TypeSourceInfo *TInfo = nullptr; 8875 bool IsBaseVirtual = false; 8876 FieldDecl *Member = nullptr; 8877 IndirectFieldDecl *IndirectMember = nullptr; 8878 8879 CtorInitializerType Type = (CtorInitializerType) readInt(); 8880 switch (Type) { 8881 case CTOR_INITIALIZER_BASE: 8882 TInfo = readTypeSourceInfo(); 8883 IsBaseVirtual = readBool(); 8884 break; 8885 8886 case CTOR_INITIALIZER_DELEGATING: 8887 TInfo = readTypeSourceInfo(); 8888 break; 8889 8890 case CTOR_INITIALIZER_MEMBER: 8891 Member = readDeclAs<FieldDecl>(); 8892 break; 8893 8894 case CTOR_INITIALIZER_INDIRECT_MEMBER: 8895 IndirectMember = readDeclAs<IndirectFieldDecl>(); 8896 break; 8897 } 8898 8899 SourceLocation MemberOrEllipsisLoc = readSourceLocation(); 8900 Expr *Init = readExpr(); 8901 SourceLocation LParenLoc = readSourceLocation(); 8902 SourceLocation RParenLoc = readSourceLocation(); 8903 8904 CXXCtorInitializer *BOMInit; 8905 if (Type == CTOR_INITIALIZER_BASE) 8906 BOMInit = new (Context) 8907 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init, 8908 RParenLoc, MemberOrEllipsisLoc); 8909 else if (Type == CTOR_INITIALIZER_DELEGATING) 8910 BOMInit = new (Context) 8911 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc); 8912 else if (Member) 8913 BOMInit = new (Context) 8914 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc, 8915 Init, RParenLoc); 8916 else 8917 BOMInit = new (Context) 8918 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc, 8919 LParenLoc, Init, RParenLoc); 8920 8921 if (/*IsWritten*/readBool()) { 8922 unsigned SourceOrder = readInt(); 8923 BOMInit->setSourceOrder(SourceOrder); 8924 } 8925 8926 CtorInitializers[i] = BOMInit; 8927 } 8928 8929 return CtorInitializers; 8930 } 8931 8932 NestedNameSpecifierLoc 8933 ASTRecordReader::readNestedNameSpecifierLoc() { 8934 ASTContext &Context = getContext(); 8935 unsigned N = readInt(); 8936 NestedNameSpecifierLocBuilder Builder; 8937 for (unsigned I = 0; I != N; ++I) { 8938 auto Kind = readNestedNameSpecifierKind(); 8939 switch (Kind) { 8940 case NestedNameSpecifier::Identifier: { 8941 IdentifierInfo *II = readIdentifier(); 8942 SourceRange Range = readSourceRange(); 8943 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd()); 8944 break; 8945 } 8946 8947 case NestedNameSpecifier::Namespace: { 8948 NamespaceDecl *NS = readDeclAs<NamespaceDecl>(); 8949 SourceRange Range = readSourceRange(); 8950 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd()); 8951 break; 8952 } 8953 8954 case NestedNameSpecifier::NamespaceAlias: { 8955 NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>(); 8956 SourceRange Range = readSourceRange(); 8957 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd()); 8958 break; 8959 } 8960 8961 case NestedNameSpecifier::TypeSpec: 8962 case NestedNameSpecifier::TypeSpecWithTemplate: { 8963 bool Template = readBool(); 8964 TypeSourceInfo *T = readTypeSourceInfo(); 8965 if (!T) 8966 return NestedNameSpecifierLoc(); 8967 SourceLocation ColonColonLoc = readSourceLocation(); 8968 8969 // FIXME: 'template' keyword location not saved anywhere, so we fake it. 8970 Builder.Extend(Context, 8971 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(), 8972 T->getTypeLoc(), ColonColonLoc); 8973 break; 8974 } 8975 8976 case NestedNameSpecifier::Global: { 8977 SourceLocation ColonColonLoc = readSourceLocation(); 8978 Builder.MakeGlobal(Context, ColonColonLoc); 8979 break; 8980 } 8981 8982 case NestedNameSpecifier::Super: { 8983 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>(); 8984 SourceRange Range = readSourceRange(); 8985 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd()); 8986 break; 8987 } 8988 } 8989 } 8990 8991 return Builder.getWithLocInContext(Context); 8992 } 8993 8994 SourceRange 8995 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record, 8996 unsigned &Idx) { 8997 SourceLocation beg = ReadSourceLocation(F, Record, Idx); 8998 SourceLocation end = ReadSourceLocation(F, Record, Idx); 8999 return SourceRange(beg, end); 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 !PendingObjCExtensionIvarRedeclarations.empty()) { 9200 // If any identifiers with corresponding top-level declarations have 9201 // been loaded, load those declarations now. 9202 using TopLevelDeclsMap = 9203 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>; 9204 TopLevelDeclsMap TopLevelDecls; 9205 9206 while (!PendingIdentifierInfos.empty()) { 9207 IdentifierInfo *II = PendingIdentifierInfos.back().first; 9208 SmallVector<uint32_t, 4> DeclIDs = 9209 std::move(PendingIdentifierInfos.back().second); 9210 PendingIdentifierInfos.pop_back(); 9211 9212 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]); 9213 } 9214 9215 // Load each function type that we deferred loading because it was a 9216 // deduced type that might refer to a local type declared within itself. 9217 for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) { 9218 auto *FD = PendingFunctionTypes[I].first; 9219 FD->setType(GetType(PendingFunctionTypes[I].second)); 9220 9221 // If we gave a function a deduced return type, remember that we need to 9222 // propagate that along the redeclaration chain. 9223 auto *DT = FD->getReturnType()->getContainedDeducedType(); 9224 if (DT && DT->isDeduced()) 9225 PendingDeducedTypeUpdates.insert( 9226 {FD->getCanonicalDecl(), FD->getReturnType()}); 9227 } 9228 PendingFunctionTypes.clear(); 9229 9230 // For each decl chain that we wanted to complete while deserializing, mark 9231 // it as "still needs to be completed". 9232 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) { 9233 markIncompleteDeclChain(PendingIncompleteDeclChains[I]); 9234 } 9235 PendingIncompleteDeclChains.clear(); 9236 9237 // Load pending declaration chains. 9238 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) 9239 loadPendingDeclChain(PendingDeclChains[I].first, 9240 PendingDeclChains[I].second); 9241 PendingDeclChains.clear(); 9242 9243 // Make the most recent of the top-level declarations visible. 9244 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(), 9245 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) { 9246 IdentifierInfo *II = TLD->first; 9247 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) { 9248 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II); 9249 } 9250 } 9251 9252 // Load any pending macro definitions. 9253 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) { 9254 IdentifierInfo *II = PendingMacroIDs.begin()[I].first; 9255 SmallVector<PendingMacroInfo, 2> GlobalIDs; 9256 GlobalIDs.swap(PendingMacroIDs.begin()[I].second); 9257 // Initialize the macro history from chained-PCHs ahead of module imports. 9258 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9259 ++IDIdx) { 9260 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9261 if (!Info.M->isModule()) 9262 resolvePendingMacro(II, Info); 9263 } 9264 // Handle module imports. 9265 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9266 ++IDIdx) { 9267 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9268 if (Info.M->isModule()) 9269 resolvePendingMacro(II, Info); 9270 } 9271 } 9272 PendingMacroIDs.clear(); 9273 9274 // Wire up the DeclContexts for Decls that we delayed setting until 9275 // recursive loading is completed. 9276 while (!PendingDeclContextInfos.empty()) { 9277 PendingDeclContextInfo Info = PendingDeclContextInfos.front(); 9278 PendingDeclContextInfos.pop_front(); 9279 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC)); 9280 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC)); 9281 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext()); 9282 } 9283 9284 // Perform any pending declaration updates. 9285 while (!PendingUpdateRecords.empty()) { 9286 auto Update = PendingUpdateRecords.pop_back_val(); 9287 ReadingKindTracker ReadingKind(Read_Decl, *this); 9288 loadDeclUpdateRecords(Update); 9289 } 9290 9291 while (!PendingObjCExtensionIvarRedeclarations.empty()) { 9292 auto ExtensionsPair = PendingObjCExtensionIvarRedeclarations.back().first; 9293 auto DuplicateIvars = 9294 PendingObjCExtensionIvarRedeclarations.back().second; 9295 llvm::DenseSet<std::pair<Decl *, Decl *>> NonEquivalentDecls; 9296 StructuralEquivalenceContext Ctx( 9297 ExtensionsPair.first->getASTContext(), 9298 ExtensionsPair.second->getASTContext(), NonEquivalentDecls, 9299 StructuralEquivalenceKind::Default, /*StrictTypeSpelling =*/false, 9300 /*Complain =*/false, 9301 /*ErrorOnTagTypeMismatch =*/true); 9302 if (Ctx.IsEquivalent(ExtensionsPair.first, ExtensionsPair.second)) { 9303 // Merge redeclared ivars with their predecessors. 9304 for (auto IvarPair : DuplicateIvars) { 9305 ObjCIvarDecl *Ivar = IvarPair.first, *PrevIvar = IvarPair.second; 9306 // Change semantic DeclContext but keep the lexical one. 9307 Ivar->setDeclContextsImpl(PrevIvar->getDeclContext(), 9308 Ivar->getLexicalDeclContext(), 9309 getContext()); 9310 getContext().setPrimaryMergedDecl(Ivar, PrevIvar->getCanonicalDecl()); 9311 } 9312 // Invalidate duplicate extension and the cached ivar list. 9313 ExtensionsPair.first->setInvalidDecl(); 9314 ExtensionsPair.second->getClassInterface() 9315 ->getDefinition() 9316 ->setIvarList(nullptr); 9317 } else { 9318 for (auto IvarPair : DuplicateIvars) { 9319 Diag(IvarPair.first->getLocation(), 9320 diag::err_duplicate_ivar_declaration) 9321 << IvarPair.first->getIdentifier(); 9322 Diag(IvarPair.second->getLocation(), diag::note_previous_definition); 9323 } 9324 } 9325 PendingObjCExtensionIvarRedeclarations.pop_back(); 9326 } 9327 } 9328 9329 // At this point, all update records for loaded decls are in place, so any 9330 // fake class definitions should have become real. 9331 assert(PendingFakeDefinitionData.empty() && 9332 "faked up a class definition but never saw the real one"); 9333 9334 // If we deserialized any C++ or Objective-C class definitions, any 9335 // Objective-C protocol definitions, or any redeclarable templates, make sure 9336 // that all redeclarations point to the definitions. Note that this can only 9337 // happen now, after the redeclaration chains have been fully wired. 9338 for (Decl *D : PendingDefinitions) { 9339 if (TagDecl *TD = dyn_cast<TagDecl>(D)) { 9340 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) { 9341 // Make sure that the TagType points at the definition. 9342 const_cast<TagType*>(TagT)->decl = TD; 9343 } 9344 9345 if (auto RD = dyn_cast<CXXRecordDecl>(D)) { 9346 for (auto *R = getMostRecentExistingDecl(RD); R; 9347 R = R->getPreviousDecl()) { 9348 assert((R == D) == 9349 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() && 9350 "declaration thinks it's the definition but it isn't"); 9351 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData; 9352 } 9353 } 9354 9355 continue; 9356 } 9357 9358 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) { 9359 // Make sure that the ObjCInterfaceType points at the definition. 9360 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl)) 9361 ->Decl = ID; 9362 9363 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl()) 9364 cast<ObjCInterfaceDecl>(R)->Data = ID->Data; 9365 9366 continue; 9367 } 9368 9369 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) { 9370 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl()) 9371 cast<ObjCProtocolDecl>(R)->Data = PD->Data; 9372 9373 continue; 9374 } 9375 9376 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl(); 9377 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl()) 9378 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common; 9379 } 9380 PendingDefinitions.clear(); 9381 9382 // Load the bodies of any functions or methods we've encountered. We do 9383 // this now (delayed) so that we can be sure that the declaration chains 9384 // have been fully wired up (hasBody relies on this). 9385 // FIXME: We shouldn't require complete redeclaration chains here. 9386 for (PendingBodiesMap::iterator PB = PendingBodies.begin(), 9387 PBEnd = PendingBodies.end(); 9388 PB != PBEnd; ++PB) { 9389 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) { 9390 // For a function defined inline within a class template, force the 9391 // canonical definition to be the one inside the canonical definition of 9392 // the template. This ensures that we instantiate from a correct view 9393 // of the template. 9394 // 9395 // Sadly we can't do this more generally: we can't be sure that all 9396 // copies of an arbitrary class definition will have the same members 9397 // defined (eg, some member functions may not be instantiated, and some 9398 // special members may or may not have been implicitly defined). 9399 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent())) 9400 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition()) 9401 continue; 9402 9403 // FIXME: Check for =delete/=default? 9404 // FIXME: Complain about ODR violations here? 9405 const FunctionDecl *Defn = nullptr; 9406 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) { 9407 FD->setLazyBody(PB->second); 9408 } else { 9409 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn); 9410 mergeDefinitionVisibility(NonConstDefn, FD); 9411 9412 if (!FD->isLateTemplateParsed() && 9413 !NonConstDefn->isLateTemplateParsed() && 9414 FD->getODRHash() != NonConstDefn->getODRHash()) { 9415 if (!isa<CXXMethodDecl>(FD)) { 9416 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9417 } else if (FD->getLexicalParent()->isFileContext() && 9418 NonConstDefn->getLexicalParent()->isFileContext()) { 9419 // Only diagnose out-of-line method definitions. If they are 9420 // in class definitions, then an error will be generated when 9421 // processing the class bodies. 9422 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9423 } 9424 } 9425 } 9426 continue; 9427 } 9428 9429 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first); 9430 if (!getContext().getLangOpts().Modules || !MD->hasBody()) 9431 MD->setLazyBody(PB->second); 9432 } 9433 PendingBodies.clear(); 9434 9435 // Do some cleanup. 9436 for (auto *ND : PendingMergedDefinitionsToDeduplicate) 9437 getContext().deduplicateMergedDefinitonsFor(ND); 9438 PendingMergedDefinitionsToDeduplicate.clear(); 9439 } 9440 9441 void ASTReader::diagnoseOdrViolations() { 9442 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() && 9443 PendingFunctionOdrMergeFailures.empty() && 9444 PendingEnumOdrMergeFailures.empty()) 9445 return; 9446 9447 // Trigger the import of the full definition of each class that had any 9448 // odr-merging problems, so we can produce better diagnostics for them. 9449 // These updates may in turn find and diagnose some ODR failures, so take 9450 // ownership of the set first. 9451 auto OdrMergeFailures = std::move(PendingOdrMergeFailures); 9452 PendingOdrMergeFailures.clear(); 9453 for (auto &Merge : OdrMergeFailures) { 9454 Merge.first->buildLookup(); 9455 Merge.first->decls_begin(); 9456 Merge.first->bases_begin(); 9457 Merge.first->vbases_begin(); 9458 for (auto &RecordPair : Merge.second) { 9459 auto *RD = RecordPair.first; 9460 RD->decls_begin(); 9461 RD->bases_begin(); 9462 RD->vbases_begin(); 9463 } 9464 } 9465 9466 // Trigger the import of functions. 9467 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures); 9468 PendingFunctionOdrMergeFailures.clear(); 9469 for (auto &Merge : FunctionOdrMergeFailures) { 9470 Merge.first->buildLookup(); 9471 Merge.first->decls_begin(); 9472 Merge.first->getBody(); 9473 for (auto &FD : Merge.second) { 9474 FD->buildLookup(); 9475 FD->decls_begin(); 9476 FD->getBody(); 9477 } 9478 } 9479 9480 // Trigger the import of enums. 9481 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures); 9482 PendingEnumOdrMergeFailures.clear(); 9483 for (auto &Merge : EnumOdrMergeFailures) { 9484 Merge.first->decls_begin(); 9485 for (auto &Enum : Merge.second) { 9486 Enum->decls_begin(); 9487 } 9488 } 9489 9490 // For each declaration from a merged context, check that the canonical 9491 // definition of that context also contains a declaration of the same 9492 // entity. 9493 // 9494 // Caution: this loop does things that might invalidate iterators into 9495 // PendingOdrMergeChecks. Don't turn this into a range-based for loop! 9496 while (!PendingOdrMergeChecks.empty()) { 9497 NamedDecl *D = PendingOdrMergeChecks.pop_back_val(); 9498 9499 // FIXME: Skip over implicit declarations for now. This matters for things 9500 // like implicitly-declared special member functions. This isn't entirely 9501 // correct; we can end up with multiple unmerged declarations of the same 9502 // implicit entity. 9503 if (D->isImplicit()) 9504 continue; 9505 9506 DeclContext *CanonDef = D->getDeclContext(); 9507 9508 bool Found = false; 9509 const Decl *DCanon = D->getCanonicalDecl(); 9510 9511 for (auto RI : D->redecls()) { 9512 if (RI->getLexicalDeclContext() == CanonDef) { 9513 Found = true; 9514 break; 9515 } 9516 } 9517 if (Found) 9518 continue; 9519 9520 // Quick check failed, time to do the slow thing. Note, we can't just 9521 // look up the name of D in CanonDef here, because the member that is 9522 // in CanonDef might not be found by name lookup (it might have been 9523 // replaced by a more recent declaration in the lookup table), and we 9524 // can't necessarily find it in the redeclaration chain because it might 9525 // be merely mergeable, not redeclarable. 9526 llvm::SmallVector<const NamedDecl*, 4> Candidates; 9527 for (auto *CanonMember : CanonDef->decls()) { 9528 if (CanonMember->getCanonicalDecl() == DCanon) { 9529 // This can happen if the declaration is merely mergeable and not 9530 // actually redeclarable (we looked for redeclarations earlier). 9531 // 9532 // FIXME: We should be able to detect this more efficiently, without 9533 // pulling in all of the members of CanonDef. 9534 Found = true; 9535 break; 9536 } 9537 if (auto *ND = dyn_cast<NamedDecl>(CanonMember)) 9538 if (ND->getDeclName() == D->getDeclName()) 9539 Candidates.push_back(ND); 9540 } 9541 9542 if (!Found) { 9543 // The AST doesn't like TagDecls becoming invalid after they've been 9544 // completed. We only really need to mark FieldDecls as invalid here. 9545 if (!isa<TagDecl>(D)) 9546 D->setInvalidDecl(); 9547 9548 // Ensure we don't accidentally recursively enter deserialization while 9549 // we're producing our diagnostic. 9550 Deserializing RecursionGuard(this); 9551 9552 std::string CanonDefModule = 9553 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef)); 9554 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl) 9555 << D << getOwningModuleNameForDiagnostic(D) 9556 << CanonDef << CanonDefModule.empty() << CanonDefModule; 9557 9558 if (Candidates.empty()) 9559 Diag(cast<Decl>(CanonDef)->getLocation(), 9560 diag::note_module_odr_violation_no_possible_decls) << D; 9561 else { 9562 for (unsigned I = 0, N = Candidates.size(); I != N; ++I) 9563 Diag(Candidates[I]->getLocation(), 9564 diag::note_module_odr_violation_possible_decl) 9565 << Candidates[I]; 9566 } 9567 9568 DiagnosedOdrMergeFailures.insert(CanonDef); 9569 } 9570 } 9571 9572 if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() && 9573 EnumOdrMergeFailures.empty()) 9574 return; 9575 9576 // Ensure we don't accidentally recursively enter deserialization while 9577 // we're producing our diagnostics. 9578 Deserializing RecursionGuard(this); 9579 9580 // Common code for hashing helpers. 9581 ODRHash Hash; 9582 auto ComputeQualTypeODRHash = [&Hash](QualType Ty) { 9583 Hash.clear(); 9584 Hash.AddQualType(Ty); 9585 return Hash.CalculateHash(); 9586 }; 9587 9588 auto ComputeODRHash = [&Hash](const Stmt *S) { 9589 assert(S); 9590 Hash.clear(); 9591 Hash.AddStmt(S); 9592 return Hash.CalculateHash(); 9593 }; 9594 9595 auto ComputeSubDeclODRHash = [&Hash](const Decl *D) { 9596 assert(D); 9597 Hash.clear(); 9598 Hash.AddSubDecl(D); 9599 return Hash.CalculateHash(); 9600 }; 9601 9602 auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) { 9603 Hash.clear(); 9604 Hash.AddTemplateArgument(TA); 9605 return Hash.CalculateHash(); 9606 }; 9607 9608 auto ComputeTemplateParameterListODRHash = 9609 [&Hash](const TemplateParameterList *TPL) { 9610 assert(TPL); 9611 Hash.clear(); 9612 Hash.AddTemplateParameterList(TPL); 9613 return Hash.CalculateHash(); 9614 }; 9615 9616 // Used with err_module_odr_violation_mismatch_decl and 9617 // note_module_odr_violation_mismatch_decl 9618 // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed 9619 enum ODRMismatchDecl { 9620 EndOfClass, 9621 PublicSpecifer, 9622 PrivateSpecifer, 9623 ProtectedSpecifer, 9624 StaticAssert, 9625 Field, 9626 CXXMethod, 9627 TypeAlias, 9628 TypeDef, 9629 Var, 9630 Friend, 9631 FunctionTemplate, 9632 Other 9633 }; 9634 9635 // Used with err_module_odr_violation_mismatch_decl_diff and 9636 // note_module_odr_violation_mismatch_decl_diff 9637 enum ODRMismatchDeclDifference { 9638 StaticAssertCondition, 9639 StaticAssertMessage, 9640 StaticAssertOnlyMessage, 9641 FieldName, 9642 FieldTypeName, 9643 FieldSingleBitField, 9644 FieldDifferentWidthBitField, 9645 FieldSingleMutable, 9646 FieldSingleInitializer, 9647 FieldDifferentInitializers, 9648 MethodName, 9649 MethodDeleted, 9650 MethodDefaulted, 9651 MethodVirtual, 9652 MethodStatic, 9653 MethodVolatile, 9654 MethodConst, 9655 MethodInline, 9656 MethodNumberParameters, 9657 MethodParameterType, 9658 MethodParameterName, 9659 MethodParameterSingleDefaultArgument, 9660 MethodParameterDifferentDefaultArgument, 9661 MethodNoTemplateArguments, 9662 MethodDifferentNumberTemplateArguments, 9663 MethodDifferentTemplateArgument, 9664 MethodSingleBody, 9665 MethodDifferentBody, 9666 TypedefName, 9667 TypedefType, 9668 VarName, 9669 VarType, 9670 VarSingleInitializer, 9671 VarDifferentInitializer, 9672 VarConstexpr, 9673 FriendTypeFunction, 9674 FriendType, 9675 FriendFunction, 9676 FunctionTemplateDifferentNumberParameters, 9677 FunctionTemplateParameterDifferentKind, 9678 FunctionTemplateParameterName, 9679 FunctionTemplateParameterSingleDefaultArgument, 9680 FunctionTemplateParameterDifferentDefaultArgument, 9681 FunctionTemplateParameterDifferentType, 9682 FunctionTemplatePackParameter, 9683 }; 9684 9685 // These lambdas have the common portions of the ODR diagnostics. This 9686 // has the same return as Diag(), so addition parameters can be passed 9687 // in with operator<< 9688 auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule, 9689 SourceLocation Loc, SourceRange Range, 9690 ODRMismatchDeclDifference DiffType) { 9691 return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff) 9692 << FirstRecord << FirstModule.empty() << FirstModule << Range 9693 << DiffType; 9694 }; 9695 auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc, 9696 SourceRange Range, ODRMismatchDeclDifference DiffType) { 9697 return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff) 9698 << SecondModule << Range << DiffType; 9699 }; 9700 9701 auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote, 9702 &ComputeQualTypeODRHash, &ComputeODRHash]( 9703 NamedDecl *FirstRecord, StringRef FirstModule, 9704 StringRef SecondModule, FieldDecl *FirstField, 9705 FieldDecl *SecondField) { 9706 IdentifierInfo *FirstII = FirstField->getIdentifier(); 9707 IdentifierInfo *SecondII = SecondField->getIdentifier(); 9708 if (FirstII->getName() != SecondII->getName()) { 9709 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9710 FirstField->getSourceRange(), FieldName) 9711 << FirstII; 9712 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9713 SecondField->getSourceRange(), FieldName) 9714 << SecondII; 9715 9716 return true; 9717 } 9718 9719 assert(getContext().hasSameType(FirstField->getType(), 9720 SecondField->getType())); 9721 9722 QualType FirstType = FirstField->getType(); 9723 QualType SecondType = SecondField->getType(); 9724 if (ComputeQualTypeODRHash(FirstType) != 9725 ComputeQualTypeODRHash(SecondType)) { 9726 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9727 FirstField->getSourceRange(), FieldTypeName) 9728 << FirstII << FirstType; 9729 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9730 SecondField->getSourceRange(), FieldTypeName) 9731 << SecondII << SecondType; 9732 9733 return true; 9734 } 9735 9736 const bool IsFirstBitField = FirstField->isBitField(); 9737 const bool IsSecondBitField = SecondField->isBitField(); 9738 if (IsFirstBitField != IsSecondBitField) { 9739 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9740 FirstField->getSourceRange(), FieldSingleBitField) 9741 << FirstII << IsFirstBitField; 9742 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9743 SecondField->getSourceRange(), FieldSingleBitField) 9744 << SecondII << IsSecondBitField; 9745 return true; 9746 } 9747 9748 if (IsFirstBitField && IsSecondBitField) { 9749 unsigned FirstBitWidthHash = 9750 ComputeODRHash(FirstField->getBitWidth()); 9751 unsigned SecondBitWidthHash = 9752 ComputeODRHash(SecondField->getBitWidth()); 9753 if (FirstBitWidthHash != SecondBitWidthHash) { 9754 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9755 FirstField->getSourceRange(), 9756 FieldDifferentWidthBitField) 9757 << FirstII << FirstField->getBitWidth()->getSourceRange(); 9758 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9759 SecondField->getSourceRange(), 9760 FieldDifferentWidthBitField) 9761 << SecondII << SecondField->getBitWidth()->getSourceRange(); 9762 return true; 9763 } 9764 } 9765 9766 if (!PP.getLangOpts().CPlusPlus) 9767 return false; 9768 9769 const bool IsFirstMutable = FirstField->isMutable(); 9770 const bool IsSecondMutable = SecondField->isMutable(); 9771 if (IsFirstMutable != IsSecondMutable) { 9772 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9773 FirstField->getSourceRange(), FieldSingleMutable) 9774 << FirstII << IsFirstMutable; 9775 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9776 SecondField->getSourceRange(), FieldSingleMutable) 9777 << SecondII << IsSecondMutable; 9778 return true; 9779 } 9780 9781 const Expr *FirstInitializer = FirstField->getInClassInitializer(); 9782 const Expr *SecondInitializer = SecondField->getInClassInitializer(); 9783 if ((!FirstInitializer && SecondInitializer) || 9784 (FirstInitializer && !SecondInitializer)) { 9785 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9786 FirstField->getSourceRange(), FieldSingleInitializer) 9787 << FirstII << (FirstInitializer != nullptr); 9788 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9789 SecondField->getSourceRange(), FieldSingleInitializer) 9790 << SecondII << (SecondInitializer != nullptr); 9791 return true; 9792 } 9793 9794 if (FirstInitializer && SecondInitializer) { 9795 unsigned FirstInitHash = ComputeODRHash(FirstInitializer); 9796 unsigned SecondInitHash = ComputeODRHash(SecondInitializer); 9797 if (FirstInitHash != SecondInitHash) { 9798 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9799 FirstField->getSourceRange(), 9800 FieldDifferentInitializers) 9801 << FirstII << FirstInitializer->getSourceRange(); 9802 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9803 SecondField->getSourceRange(), 9804 FieldDifferentInitializers) 9805 << SecondII << SecondInitializer->getSourceRange(); 9806 return true; 9807 } 9808 } 9809 9810 return false; 9811 }; 9812 9813 auto ODRDiagTypeDefOrAlias = 9814 [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash]( 9815 NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule, 9816 TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD, 9817 bool IsTypeAlias) { 9818 auto FirstName = FirstTD->getDeclName(); 9819 auto SecondName = SecondTD->getDeclName(); 9820 if (FirstName != SecondName) { 9821 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(), 9822 FirstTD->getSourceRange(), TypedefName) 9823 << IsTypeAlias << FirstName; 9824 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(), 9825 SecondTD->getSourceRange(), TypedefName) 9826 << IsTypeAlias << SecondName; 9827 return true; 9828 } 9829 9830 QualType FirstType = FirstTD->getUnderlyingType(); 9831 QualType SecondType = SecondTD->getUnderlyingType(); 9832 if (ComputeQualTypeODRHash(FirstType) != 9833 ComputeQualTypeODRHash(SecondType)) { 9834 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(), 9835 FirstTD->getSourceRange(), TypedefType) 9836 << IsTypeAlias << FirstName << FirstType; 9837 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(), 9838 SecondTD->getSourceRange(), TypedefType) 9839 << IsTypeAlias << SecondName << SecondType; 9840 return true; 9841 } 9842 9843 return false; 9844 }; 9845 9846 auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote, 9847 &ComputeQualTypeODRHash, &ComputeODRHash, 9848 this](NamedDecl *FirstRecord, StringRef FirstModule, 9849 StringRef SecondModule, VarDecl *FirstVD, 9850 VarDecl *SecondVD) { 9851 auto FirstName = FirstVD->getDeclName(); 9852 auto SecondName = SecondVD->getDeclName(); 9853 if (FirstName != SecondName) { 9854 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9855 FirstVD->getSourceRange(), VarName) 9856 << FirstName; 9857 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9858 SecondVD->getSourceRange(), VarName) 9859 << SecondName; 9860 return true; 9861 } 9862 9863 QualType FirstType = FirstVD->getType(); 9864 QualType SecondType = SecondVD->getType(); 9865 if (ComputeQualTypeODRHash(FirstType) != 9866 ComputeQualTypeODRHash(SecondType)) { 9867 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9868 FirstVD->getSourceRange(), VarType) 9869 << FirstName << FirstType; 9870 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9871 SecondVD->getSourceRange(), VarType) 9872 << SecondName << SecondType; 9873 return true; 9874 } 9875 9876 if (!PP.getLangOpts().CPlusPlus) 9877 return false; 9878 9879 const Expr *FirstInit = FirstVD->getInit(); 9880 const Expr *SecondInit = SecondVD->getInit(); 9881 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 9882 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9883 FirstVD->getSourceRange(), VarSingleInitializer) 9884 << FirstName << (FirstInit == nullptr) 9885 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 9886 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9887 SecondVD->getSourceRange(), VarSingleInitializer) 9888 << SecondName << (SecondInit == nullptr) 9889 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 9890 return true; 9891 } 9892 9893 if (FirstInit && SecondInit && 9894 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 9895 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9896 FirstVD->getSourceRange(), VarDifferentInitializer) 9897 << FirstName << FirstInit->getSourceRange(); 9898 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9899 SecondVD->getSourceRange(), VarDifferentInitializer) 9900 << SecondName << SecondInit->getSourceRange(); 9901 return true; 9902 } 9903 9904 const bool FirstIsConstexpr = FirstVD->isConstexpr(); 9905 const bool SecondIsConstexpr = SecondVD->isConstexpr(); 9906 if (FirstIsConstexpr != SecondIsConstexpr) { 9907 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9908 FirstVD->getSourceRange(), VarConstexpr) 9909 << FirstName << FirstIsConstexpr; 9910 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9911 SecondVD->getSourceRange(), VarConstexpr) 9912 << SecondName << SecondIsConstexpr; 9913 return true; 9914 } 9915 return false; 9916 }; 9917 9918 auto DifferenceSelector = [](Decl *D) { 9919 assert(D && "valid Decl required"); 9920 switch (D->getKind()) { 9921 default: 9922 return Other; 9923 case Decl::AccessSpec: 9924 switch (D->getAccess()) { 9925 case AS_public: 9926 return PublicSpecifer; 9927 case AS_private: 9928 return PrivateSpecifer; 9929 case AS_protected: 9930 return ProtectedSpecifer; 9931 case AS_none: 9932 break; 9933 } 9934 llvm_unreachable("Invalid access specifier"); 9935 case Decl::StaticAssert: 9936 return StaticAssert; 9937 case Decl::Field: 9938 return Field; 9939 case Decl::CXXMethod: 9940 case Decl::CXXConstructor: 9941 case Decl::CXXDestructor: 9942 return CXXMethod; 9943 case Decl::TypeAlias: 9944 return TypeAlias; 9945 case Decl::Typedef: 9946 return TypeDef; 9947 case Decl::Var: 9948 return Var; 9949 case Decl::Friend: 9950 return Friend; 9951 case Decl::FunctionTemplate: 9952 return FunctionTemplate; 9953 } 9954 }; 9955 9956 using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>; 9957 auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes, 9958 RecordDecl *Record, 9959 const DeclContext *DC) { 9960 for (auto *D : Record->decls()) { 9961 if (!ODRHash::isDeclToBeProcessed(D, DC)) 9962 continue; 9963 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 9964 } 9965 }; 9966 9967 struct DiffResult { 9968 Decl *FirstDecl = nullptr, *SecondDecl = nullptr; 9969 ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other; 9970 }; 9971 9972 // If there is a diagnoseable difference, FirstDiffType and 9973 // SecondDiffType will not be Other and FirstDecl and SecondDecl will be 9974 // filled in if not EndOfClass. 9975 auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes, 9976 DeclHashes &SecondHashes) { 9977 DiffResult DR; 9978 auto FirstIt = FirstHashes.begin(); 9979 auto SecondIt = SecondHashes.begin(); 9980 while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) { 9981 if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() && 9982 FirstIt->second == SecondIt->second) { 9983 ++FirstIt; 9984 ++SecondIt; 9985 continue; 9986 } 9987 9988 DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first; 9989 DR.SecondDecl = 9990 SecondIt == SecondHashes.end() ? nullptr : SecondIt->first; 9991 9992 DR.FirstDiffType = 9993 DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass; 9994 DR.SecondDiffType = 9995 DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass; 9996 return DR; 9997 } 9998 return DR; 9999 }; 10000 10001 // Use this to diagnose that an unexpected Decl was encountered 10002 // or no difference was detected. This causes a generic error 10003 // message to be emitted. 10004 auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord, 10005 StringRef FirstModule, 10006 NamedDecl *SecondRecord, 10007 StringRef SecondModule) { 10008 Diag(FirstRecord->getLocation(), 10009 diag::err_module_odr_violation_different_definitions) 10010 << FirstRecord << FirstModule.empty() << FirstModule; 10011 10012 if (DR.FirstDecl) { 10013 Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference) 10014 << FirstRecord << DR.FirstDecl->getSourceRange(); 10015 } 10016 10017 Diag(SecondRecord->getLocation(), 10018 diag::note_module_odr_violation_different_definitions) 10019 << SecondModule; 10020 10021 if (DR.SecondDecl) { 10022 Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference) 10023 << DR.SecondDecl->getSourceRange(); 10024 } 10025 }; 10026 10027 auto DiagnoseODRMismatch = 10028 [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule, 10029 NamedDecl *SecondRecord, StringRef SecondModule) { 10030 SourceLocation FirstLoc; 10031 SourceRange FirstRange; 10032 auto *FirstTag = dyn_cast<TagDecl>(FirstRecord); 10033 if (DR.FirstDiffType == EndOfClass && FirstTag) { 10034 FirstLoc = FirstTag->getBraceRange().getEnd(); 10035 } else { 10036 FirstLoc = DR.FirstDecl->getLocation(); 10037 FirstRange = DR.FirstDecl->getSourceRange(); 10038 } 10039 Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl) 10040 << FirstRecord << FirstModule.empty() << FirstModule << FirstRange 10041 << DR.FirstDiffType; 10042 10043 SourceLocation SecondLoc; 10044 SourceRange SecondRange; 10045 auto *SecondTag = dyn_cast<TagDecl>(SecondRecord); 10046 if (DR.SecondDiffType == EndOfClass && SecondTag) { 10047 SecondLoc = SecondTag->getBraceRange().getEnd(); 10048 } else { 10049 SecondLoc = DR.SecondDecl->getLocation(); 10050 SecondRange = DR.SecondDecl->getSourceRange(); 10051 } 10052 Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl) 10053 << SecondModule << SecondRange << DR.SecondDiffType; 10054 }; 10055 10056 // Issue any pending ODR-failure diagnostics. 10057 for (auto &Merge : OdrMergeFailures) { 10058 // If we've already pointed out a specific problem with this class, don't 10059 // bother issuing a general "something's different" diagnostic. 10060 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 10061 continue; 10062 10063 bool Diagnosed = false; 10064 CXXRecordDecl *FirstRecord = Merge.first; 10065 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord); 10066 for (auto &RecordPair : Merge.second) { 10067 CXXRecordDecl *SecondRecord = RecordPair.first; 10068 // Multiple different declarations got merged together; tell the user 10069 // where they came from. 10070 if (FirstRecord == SecondRecord) 10071 continue; 10072 10073 std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord); 10074 10075 auto *FirstDD = FirstRecord->DefinitionData; 10076 auto *SecondDD = RecordPair.second; 10077 10078 assert(FirstDD && SecondDD && "Definitions without DefinitionData"); 10079 10080 // Diagnostics from DefinitionData are emitted here. 10081 if (FirstDD != SecondDD) { 10082 enum ODRDefinitionDataDifference { 10083 NumBases, 10084 NumVBases, 10085 BaseType, 10086 BaseVirtual, 10087 BaseAccess, 10088 }; 10089 auto ODRDiagBaseError = [FirstRecord, &FirstModule, 10090 this](SourceLocation Loc, SourceRange Range, 10091 ODRDefinitionDataDifference DiffType) { 10092 return Diag(Loc, diag::err_module_odr_violation_definition_data) 10093 << FirstRecord << FirstModule.empty() << FirstModule << Range 10094 << DiffType; 10095 }; 10096 auto ODRDiagBaseNote = [&SecondModule, 10097 this](SourceLocation Loc, SourceRange Range, 10098 ODRDefinitionDataDifference DiffType) { 10099 return Diag(Loc, diag::note_module_odr_violation_definition_data) 10100 << SecondModule << Range << DiffType; 10101 }; 10102 10103 unsigned FirstNumBases = FirstDD->NumBases; 10104 unsigned FirstNumVBases = FirstDD->NumVBases; 10105 unsigned SecondNumBases = SecondDD->NumBases; 10106 unsigned SecondNumVBases = SecondDD->NumVBases; 10107 10108 auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) { 10109 unsigned NumBases = DD->NumBases; 10110 if (NumBases == 0) return SourceRange(); 10111 auto bases = DD->bases(); 10112 return SourceRange(bases[0].getBeginLoc(), 10113 bases[NumBases - 1].getEndLoc()); 10114 }; 10115 10116 if (FirstNumBases != SecondNumBases) { 10117 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10118 NumBases) 10119 << FirstNumBases; 10120 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10121 NumBases) 10122 << SecondNumBases; 10123 Diagnosed = true; 10124 break; 10125 } 10126 10127 if (FirstNumVBases != SecondNumVBases) { 10128 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10129 NumVBases) 10130 << FirstNumVBases; 10131 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10132 NumVBases) 10133 << SecondNumVBases; 10134 Diagnosed = true; 10135 break; 10136 } 10137 10138 auto FirstBases = FirstDD->bases(); 10139 auto SecondBases = SecondDD->bases(); 10140 unsigned i = 0; 10141 for (i = 0; i < FirstNumBases; ++i) { 10142 auto FirstBase = FirstBases[i]; 10143 auto SecondBase = SecondBases[i]; 10144 if (ComputeQualTypeODRHash(FirstBase.getType()) != 10145 ComputeQualTypeODRHash(SecondBase.getType())) { 10146 ODRDiagBaseError(FirstRecord->getLocation(), 10147 FirstBase.getSourceRange(), BaseType) 10148 << (i + 1) << FirstBase.getType(); 10149 ODRDiagBaseNote(SecondRecord->getLocation(), 10150 SecondBase.getSourceRange(), BaseType) 10151 << (i + 1) << SecondBase.getType(); 10152 break; 10153 } 10154 10155 if (FirstBase.isVirtual() != SecondBase.isVirtual()) { 10156 ODRDiagBaseError(FirstRecord->getLocation(), 10157 FirstBase.getSourceRange(), BaseVirtual) 10158 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType(); 10159 ODRDiagBaseNote(SecondRecord->getLocation(), 10160 SecondBase.getSourceRange(), BaseVirtual) 10161 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType(); 10162 break; 10163 } 10164 10165 if (FirstBase.getAccessSpecifierAsWritten() != 10166 SecondBase.getAccessSpecifierAsWritten()) { 10167 ODRDiagBaseError(FirstRecord->getLocation(), 10168 FirstBase.getSourceRange(), BaseAccess) 10169 << (i + 1) << FirstBase.getType() 10170 << (int)FirstBase.getAccessSpecifierAsWritten(); 10171 ODRDiagBaseNote(SecondRecord->getLocation(), 10172 SecondBase.getSourceRange(), BaseAccess) 10173 << (i + 1) << SecondBase.getType() 10174 << (int)SecondBase.getAccessSpecifierAsWritten(); 10175 break; 10176 } 10177 } 10178 10179 if (i != FirstNumBases) { 10180 Diagnosed = true; 10181 break; 10182 } 10183 } 10184 10185 const ClassTemplateDecl *FirstTemplate = 10186 FirstRecord->getDescribedClassTemplate(); 10187 const ClassTemplateDecl *SecondTemplate = 10188 SecondRecord->getDescribedClassTemplate(); 10189 10190 assert(!FirstTemplate == !SecondTemplate && 10191 "Both pointers should be null or non-null"); 10192 10193 if (FirstTemplate && SecondTemplate) { 10194 DeclHashes FirstTemplateHashes; 10195 DeclHashes SecondTemplateHashes; 10196 10197 auto PopulateTemplateParameterHashs = 10198 [&ComputeSubDeclODRHash](DeclHashes &Hashes, 10199 const ClassTemplateDecl *TD) { 10200 for (auto *D : TD->getTemplateParameters()->asArray()) { 10201 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 10202 } 10203 }; 10204 10205 PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate); 10206 PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate); 10207 10208 assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() && 10209 "Number of template parameters should be equal."); 10210 10211 auto FirstIt = FirstTemplateHashes.begin(); 10212 auto FirstEnd = FirstTemplateHashes.end(); 10213 auto SecondIt = SecondTemplateHashes.begin(); 10214 for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) { 10215 if (FirstIt->second == SecondIt->second) 10216 continue; 10217 10218 const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first); 10219 const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first); 10220 10221 assert(FirstDecl->getKind() == SecondDecl->getKind() && 10222 "Parameter Decl's should be the same kind."); 10223 10224 enum ODRTemplateDifference { 10225 ParamEmptyName, 10226 ParamName, 10227 ParamSingleDefaultArgument, 10228 ParamDifferentDefaultArgument, 10229 }; 10230 10231 auto hasDefaultArg = [](const NamedDecl *D) { 10232 if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(D)) 10233 return TTP->hasDefaultArgument() && 10234 !TTP->defaultArgumentWasInherited(); 10235 if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) 10236 return NTTP->hasDefaultArgument() && 10237 !NTTP->defaultArgumentWasInherited(); 10238 auto *TTP = cast<TemplateTemplateParmDecl>(D); 10239 return TTP->hasDefaultArgument() && 10240 !TTP->defaultArgumentWasInherited(); 10241 }; 10242 bool hasFirstArg = hasDefaultArg(FirstDecl); 10243 bool hasSecondArg = hasDefaultArg(SecondDecl); 10244 10245 ODRTemplateDifference ErrDiffType; 10246 ODRTemplateDifference NoteDiffType; 10247 10248 DeclarationName FirstName = FirstDecl->getDeclName(); 10249 DeclarationName SecondName = SecondDecl->getDeclName(); 10250 10251 if (FirstName != SecondName) { 10252 bool FirstNameEmpty = 10253 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo(); 10254 bool SecondNameEmpty = SecondName.isIdentifier() && 10255 !SecondName.getAsIdentifierInfo(); 10256 ErrDiffType = FirstNameEmpty ? ParamEmptyName : ParamName; 10257 NoteDiffType = SecondNameEmpty ? ParamEmptyName : ParamName; 10258 } else if (hasFirstArg == hasSecondArg) 10259 ErrDiffType = NoteDiffType = ParamDifferentDefaultArgument; 10260 else 10261 ErrDiffType = NoteDiffType = ParamSingleDefaultArgument; 10262 10263 Diag(FirstDecl->getLocation(), 10264 diag::err_module_odr_violation_template_parameter) 10265 << FirstRecord << FirstModule.empty() << FirstModule 10266 << FirstDecl->getSourceRange() << ErrDiffType << hasFirstArg 10267 << FirstName; 10268 Diag(SecondDecl->getLocation(), 10269 diag::note_module_odr_violation_template_parameter) 10270 << SecondModule << SecondDecl->getSourceRange() << NoteDiffType 10271 << hasSecondArg << SecondName; 10272 break; 10273 } 10274 10275 if (FirstIt != FirstEnd) { 10276 Diagnosed = true; 10277 break; 10278 } 10279 } 10280 10281 DeclHashes FirstHashes; 10282 DeclHashes SecondHashes; 10283 const DeclContext *DC = FirstRecord; 10284 PopulateHashes(FirstHashes, FirstRecord, DC); 10285 PopulateHashes(SecondHashes, SecondRecord, DC); 10286 10287 auto DR = FindTypeDiffs(FirstHashes, SecondHashes); 10288 ODRMismatchDecl FirstDiffType = DR.FirstDiffType; 10289 ODRMismatchDecl SecondDiffType = DR.SecondDiffType; 10290 Decl *FirstDecl = DR.FirstDecl; 10291 Decl *SecondDecl = DR.SecondDecl; 10292 10293 if (FirstDiffType == Other || SecondDiffType == Other) { 10294 DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord, 10295 SecondModule); 10296 Diagnosed = true; 10297 break; 10298 } 10299 10300 if (FirstDiffType != SecondDiffType) { 10301 DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord, 10302 SecondModule); 10303 Diagnosed = true; 10304 break; 10305 } 10306 10307 assert(FirstDiffType == SecondDiffType); 10308 10309 switch (FirstDiffType) { 10310 case Other: 10311 case EndOfClass: 10312 case PublicSpecifer: 10313 case PrivateSpecifer: 10314 case ProtectedSpecifer: 10315 llvm_unreachable("Invalid diff type"); 10316 10317 case StaticAssert: { 10318 StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl); 10319 StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl); 10320 10321 Expr *FirstExpr = FirstSA->getAssertExpr(); 10322 Expr *SecondExpr = SecondSA->getAssertExpr(); 10323 unsigned FirstODRHash = ComputeODRHash(FirstExpr); 10324 unsigned SecondODRHash = ComputeODRHash(SecondExpr); 10325 if (FirstODRHash != SecondODRHash) { 10326 ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(), 10327 FirstExpr->getSourceRange(), StaticAssertCondition); 10328 ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(), 10329 SecondExpr->getSourceRange(), StaticAssertCondition); 10330 Diagnosed = true; 10331 break; 10332 } 10333 10334 StringLiteral *FirstStr = FirstSA->getMessage(); 10335 StringLiteral *SecondStr = SecondSA->getMessage(); 10336 assert((FirstStr || SecondStr) && "Both messages cannot be empty"); 10337 if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) { 10338 SourceLocation FirstLoc, SecondLoc; 10339 SourceRange FirstRange, SecondRange; 10340 if (FirstStr) { 10341 FirstLoc = FirstStr->getBeginLoc(); 10342 FirstRange = FirstStr->getSourceRange(); 10343 } else { 10344 FirstLoc = FirstSA->getBeginLoc(); 10345 FirstRange = FirstSA->getSourceRange(); 10346 } 10347 if (SecondStr) { 10348 SecondLoc = SecondStr->getBeginLoc(); 10349 SecondRange = SecondStr->getSourceRange(); 10350 } else { 10351 SecondLoc = SecondSA->getBeginLoc(); 10352 SecondRange = SecondSA->getSourceRange(); 10353 } 10354 ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange, 10355 StaticAssertOnlyMessage) 10356 << (FirstStr == nullptr); 10357 ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange, 10358 StaticAssertOnlyMessage) 10359 << (SecondStr == nullptr); 10360 Diagnosed = true; 10361 break; 10362 } 10363 10364 if (FirstStr && SecondStr && 10365 FirstStr->getString() != SecondStr->getString()) { 10366 ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(), 10367 FirstStr->getSourceRange(), StaticAssertMessage); 10368 ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(), 10369 SecondStr->getSourceRange(), StaticAssertMessage); 10370 Diagnosed = true; 10371 break; 10372 } 10373 break; 10374 } 10375 case Field: { 10376 Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule, 10377 cast<FieldDecl>(FirstDecl), 10378 cast<FieldDecl>(SecondDecl)); 10379 break; 10380 } 10381 case CXXMethod: { 10382 enum { 10383 DiagMethod, 10384 DiagConstructor, 10385 DiagDestructor, 10386 } FirstMethodType, 10387 SecondMethodType; 10388 auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) { 10389 if (isa<CXXConstructorDecl>(D)) return DiagConstructor; 10390 if (isa<CXXDestructorDecl>(D)) return DiagDestructor; 10391 return DiagMethod; 10392 }; 10393 const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl); 10394 const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl); 10395 FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod); 10396 SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod); 10397 auto FirstName = FirstMethod->getDeclName(); 10398 auto SecondName = SecondMethod->getDeclName(); 10399 if (FirstMethodType != SecondMethodType || FirstName != SecondName) { 10400 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10401 FirstMethod->getSourceRange(), MethodName) 10402 << FirstMethodType << FirstName; 10403 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10404 SecondMethod->getSourceRange(), MethodName) 10405 << SecondMethodType << SecondName; 10406 10407 Diagnosed = true; 10408 break; 10409 } 10410 10411 const bool FirstDeleted = FirstMethod->isDeletedAsWritten(); 10412 const bool SecondDeleted = SecondMethod->isDeletedAsWritten(); 10413 if (FirstDeleted != SecondDeleted) { 10414 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10415 FirstMethod->getSourceRange(), MethodDeleted) 10416 << FirstMethodType << FirstName << FirstDeleted; 10417 10418 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10419 SecondMethod->getSourceRange(), MethodDeleted) 10420 << SecondMethodType << SecondName << SecondDeleted; 10421 Diagnosed = true; 10422 break; 10423 } 10424 10425 const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted(); 10426 const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted(); 10427 if (FirstDefaulted != SecondDefaulted) { 10428 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10429 FirstMethod->getSourceRange(), MethodDefaulted) 10430 << FirstMethodType << FirstName << FirstDefaulted; 10431 10432 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10433 SecondMethod->getSourceRange(), MethodDefaulted) 10434 << SecondMethodType << SecondName << SecondDefaulted; 10435 Diagnosed = true; 10436 break; 10437 } 10438 10439 const bool FirstVirtual = FirstMethod->isVirtualAsWritten(); 10440 const bool SecondVirtual = SecondMethod->isVirtualAsWritten(); 10441 const bool FirstPure = FirstMethod->isPure(); 10442 const bool SecondPure = SecondMethod->isPure(); 10443 if ((FirstVirtual || SecondVirtual) && 10444 (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) { 10445 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10446 FirstMethod->getSourceRange(), MethodVirtual) 10447 << FirstMethodType << FirstName << FirstPure << FirstVirtual; 10448 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10449 SecondMethod->getSourceRange(), MethodVirtual) 10450 << SecondMethodType << SecondName << SecondPure << SecondVirtual; 10451 Diagnosed = true; 10452 break; 10453 } 10454 10455 // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging, 10456 // FirstDecl is the canonical Decl of SecondDecl, so the storage 10457 // class needs to be checked instead. 10458 const auto FirstStorage = FirstMethod->getStorageClass(); 10459 const auto SecondStorage = SecondMethod->getStorageClass(); 10460 const bool FirstStatic = FirstStorage == SC_Static; 10461 const bool SecondStatic = SecondStorage == SC_Static; 10462 if (FirstStatic != SecondStatic) { 10463 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10464 FirstMethod->getSourceRange(), MethodStatic) 10465 << FirstMethodType << FirstName << FirstStatic; 10466 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10467 SecondMethod->getSourceRange(), MethodStatic) 10468 << SecondMethodType << SecondName << SecondStatic; 10469 Diagnosed = true; 10470 break; 10471 } 10472 10473 const bool FirstVolatile = FirstMethod->isVolatile(); 10474 const bool SecondVolatile = SecondMethod->isVolatile(); 10475 if (FirstVolatile != SecondVolatile) { 10476 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10477 FirstMethod->getSourceRange(), MethodVolatile) 10478 << FirstMethodType << FirstName << FirstVolatile; 10479 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10480 SecondMethod->getSourceRange(), MethodVolatile) 10481 << SecondMethodType << SecondName << SecondVolatile; 10482 Diagnosed = true; 10483 break; 10484 } 10485 10486 const bool FirstConst = FirstMethod->isConst(); 10487 const bool SecondConst = SecondMethod->isConst(); 10488 if (FirstConst != SecondConst) { 10489 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10490 FirstMethod->getSourceRange(), MethodConst) 10491 << FirstMethodType << FirstName << FirstConst; 10492 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10493 SecondMethod->getSourceRange(), MethodConst) 10494 << SecondMethodType << SecondName << SecondConst; 10495 Diagnosed = true; 10496 break; 10497 } 10498 10499 const bool FirstInline = FirstMethod->isInlineSpecified(); 10500 const bool SecondInline = SecondMethod->isInlineSpecified(); 10501 if (FirstInline != SecondInline) { 10502 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10503 FirstMethod->getSourceRange(), MethodInline) 10504 << FirstMethodType << FirstName << FirstInline; 10505 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10506 SecondMethod->getSourceRange(), MethodInline) 10507 << SecondMethodType << SecondName << SecondInline; 10508 Diagnosed = true; 10509 break; 10510 } 10511 10512 const unsigned FirstNumParameters = FirstMethod->param_size(); 10513 const unsigned SecondNumParameters = SecondMethod->param_size(); 10514 if (FirstNumParameters != SecondNumParameters) { 10515 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10516 FirstMethod->getSourceRange(), 10517 MethodNumberParameters) 10518 << FirstMethodType << FirstName << FirstNumParameters; 10519 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10520 SecondMethod->getSourceRange(), 10521 MethodNumberParameters) 10522 << SecondMethodType << SecondName << SecondNumParameters; 10523 Diagnosed = true; 10524 break; 10525 } 10526 10527 // Need this status boolean to know when break out of the switch. 10528 bool ParameterMismatch = false; 10529 for (unsigned I = 0; I < FirstNumParameters; ++I) { 10530 const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I); 10531 const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I); 10532 10533 QualType FirstParamType = FirstParam->getType(); 10534 QualType SecondParamType = SecondParam->getType(); 10535 if (FirstParamType != SecondParamType && 10536 ComputeQualTypeODRHash(FirstParamType) != 10537 ComputeQualTypeODRHash(SecondParamType)) { 10538 if (const DecayedType *ParamDecayedType = 10539 FirstParamType->getAs<DecayedType>()) { 10540 ODRDiagDeclError( 10541 FirstRecord, FirstModule, FirstMethod->getLocation(), 10542 FirstMethod->getSourceRange(), MethodParameterType) 10543 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10544 << true << ParamDecayedType->getOriginalType(); 10545 } else { 10546 ODRDiagDeclError( 10547 FirstRecord, FirstModule, FirstMethod->getLocation(), 10548 FirstMethod->getSourceRange(), MethodParameterType) 10549 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10550 << false; 10551 } 10552 10553 if (const DecayedType *ParamDecayedType = 10554 SecondParamType->getAs<DecayedType>()) { 10555 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10556 SecondMethod->getSourceRange(), 10557 MethodParameterType) 10558 << SecondMethodType << SecondName << (I + 1) 10559 << SecondParamType << true 10560 << ParamDecayedType->getOriginalType(); 10561 } else { 10562 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10563 SecondMethod->getSourceRange(), 10564 MethodParameterType) 10565 << SecondMethodType << SecondName << (I + 1) 10566 << SecondParamType << false; 10567 } 10568 ParameterMismatch = true; 10569 break; 10570 } 10571 10572 DeclarationName FirstParamName = FirstParam->getDeclName(); 10573 DeclarationName SecondParamName = SecondParam->getDeclName(); 10574 if (FirstParamName != SecondParamName) { 10575 ODRDiagDeclError(FirstRecord, FirstModule, 10576 FirstMethod->getLocation(), 10577 FirstMethod->getSourceRange(), MethodParameterName) 10578 << FirstMethodType << FirstName << (I + 1) << FirstParamName; 10579 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10580 SecondMethod->getSourceRange(), MethodParameterName) 10581 << SecondMethodType << SecondName << (I + 1) << SecondParamName; 10582 ParameterMismatch = true; 10583 break; 10584 } 10585 10586 const Expr *FirstInit = FirstParam->getInit(); 10587 const Expr *SecondInit = SecondParam->getInit(); 10588 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 10589 ODRDiagDeclError(FirstRecord, FirstModule, 10590 FirstMethod->getLocation(), 10591 FirstMethod->getSourceRange(), 10592 MethodParameterSingleDefaultArgument) 10593 << FirstMethodType << FirstName << (I + 1) 10594 << (FirstInit == nullptr) 10595 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 10596 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10597 SecondMethod->getSourceRange(), 10598 MethodParameterSingleDefaultArgument) 10599 << SecondMethodType << SecondName << (I + 1) 10600 << (SecondInit == nullptr) 10601 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 10602 ParameterMismatch = true; 10603 break; 10604 } 10605 10606 if (FirstInit && SecondInit && 10607 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 10608 ODRDiagDeclError(FirstRecord, FirstModule, 10609 FirstMethod->getLocation(), 10610 FirstMethod->getSourceRange(), 10611 MethodParameterDifferentDefaultArgument) 10612 << FirstMethodType << FirstName << (I + 1) 10613 << FirstInit->getSourceRange(); 10614 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10615 SecondMethod->getSourceRange(), 10616 MethodParameterDifferentDefaultArgument) 10617 << SecondMethodType << SecondName << (I + 1) 10618 << SecondInit->getSourceRange(); 10619 ParameterMismatch = true; 10620 break; 10621 10622 } 10623 } 10624 10625 if (ParameterMismatch) { 10626 Diagnosed = true; 10627 break; 10628 } 10629 10630 const auto *FirstTemplateArgs = 10631 FirstMethod->getTemplateSpecializationArgs(); 10632 const auto *SecondTemplateArgs = 10633 SecondMethod->getTemplateSpecializationArgs(); 10634 10635 if ((FirstTemplateArgs && !SecondTemplateArgs) || 10636 (!FirstTemplateArgs && SecondTemplateArgs)) { 10637 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10638 FirstMethod->getSourceRange(), 10639 MethodNoTemplateArguments) 10640 << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr); 10641 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10642 SecondMethod->getSourceRange(), 10643 MethodNoTemplateArguments) 10644 << SecondMethodType << SecondName 10645 << (SecondTemplateArgs != nullptr); 10646 10647 Diagnosed = true; 10648 break; 10649 } 10650 10651 if (FirstTemplateArgs && SecondTemplateArgs) { 10652 // Remove pack expansions from argument list. 10653 auto ExpandTemplateArgumentList = 10654 [](const TemplateArgumentList *TAL) { 10655 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList; 10656 for (const TemplateArgument &TA : TAL->asArray()) { 10657 if (TA.getKind() != TemplateArgument::Pack) { 10658 ExpandedList.push_back(&TA); 10659 continue; 10660 } 10661 llvm::append_range(ExpandedList, llvm::make_pointer_range( 10662 TA.getPackAsArray())); 10663 } 10664 return ExpandedList; 10665 }; 10666 llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList = 10667 ExpandTemplateArgumentList(FirstTemplateArgs); 10668 llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList = 10669 ExpandTemplateArgumentList(SecondTemplateArgs); 10670 10671 if (FirstExpandedList.size() != SecondExpandedList.size()) { 10672 ODRDiagDeclError(FirstRecord, FirstModule, 10673 FirstMethod->getLocation(), 10674 FirstMethod->getSourceRange(), 10675 MethodDifferentNumberTemplateArguments) 10676 << FirstMethodType << FirstName 10677 << (unsigned)FirstExpandedList.size(); 10678 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10679 SecondMethod->getSourceRange(), 10680 MethodDifferentNumberTemplateArguments) 10681 << SecondMethodType << SecondName 10682 << (unsigned)SecondExpandedList.size(); 10683 10684 Diagnosed = true; 10685 break; 10686 } 10687 10688 bool TemplateArgumentMismatch = false; 10689 for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) { 10690 const TemplateArgument &FirstTA = *FirstExpandedList[i], 10691 &SecondTA = *SecondExpandedList[i]; 10692 if (ComputeTemplateArgumentODRHash(FirstTA) == 10693 ComputeTemplateArgumentODRHash(SecondTA)) { 10694 continue; 10695 } 10696 10697 ODRDiagDeclError( 10698 FirstRecord, FirstModule, FirstMethod->getLocation(), 10699 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument) 10700 << FirstMethodType << FirstName << FirstTA << i + 1; 10701 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10702 SecondMethod->getSourceRange(), 10703 MethodDifferentTemplateArgument) 10704 << SecondMethodType << SecondName << SecondTA << i + 1; 10705 10706 TemplateArgumentMismatch = true; 10707 break; 10708 } 10709 10710 if (TemplateArgumentMismatch) { 10711 Diagnosed = true; 10712 break; 10713 } 10714 } 10715 10716 // Compute the hash of the method as if it has no body. 10717 auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) { 10718 Hash.clear(); 10719 Hash.AddFunctionDecl(D, true /*SkipBody*/); 10720 return Hash.CalculateHash(); 10721 }; 10722 10723 // Compare the hash generated to the hash stored. A difference means 10724 // that a body was present in the original source. Due to merging, 10725 // the stardard way of detecting a body will not work. 10726 const bool HasFirstBody = 10727 ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash(); 10728 const bool HasSecondBody = 10729 ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash(); 10730 10731 if (HasFirstBody != HasSecondBody) { 10732 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10733 FirstMethod->getSourceRange(), MethodSingleBody) 10734 << FirstMethodType << FirstName << HasFirstBody; 10735 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10736 SecondMethod->getSourceRange(), MethodSingleBody) 10737 << SecondMethodType << SecondName << HasSecondBody; 10738 Diagnosed = true; 10739 break; 10740 } 10741 10742 if (HasFirstBody && HasSecondBody) { 10743 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10744 FirstMethod->getSourceRange(), MethodDifferentBody) 10745 << FirstMethodType << FirstName; 10746 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10747 SecondMethod->getSourceRange(), MethodDifferentBody) 10748 << SecondMethodType << SecondName; 10749 Diagnosed = true; 10750 break; 10751 } 10752 10753 break; 10754 } 10755 case TypeAlias: 10756 case TypeDef: { 10757 Diagnosed = ODRDiagTypeDefOrAlias( 10758 FirstRecord, FirstModule, SecondModule, 10759 cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl), 10760 FirstDiffType == TypeAlias); 10761 break; 10762 } 10763 case Var: { 10764 Diagnosed = 10765 ODRDiagVar(FirstRecord, FirstModule, SecondModule, 10766 cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl)); 10767 break; 10768 } 10769 case Friend: { 10770 FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl); 10771 FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl); 10772 10773 NamedDecl *FirstND = FirstFriend->getFriendDecl(); 10774 NamedDecl *SecondND = SecondFriend->getFriendDecl(); 10775 10776 TypeSourceInfo *FirstTSI = FirstFriend->getFriendType(); 10777 TypeSourceInfo *SecondTSI = SecondFriend->getFriendType(); 10778 10779 if (FirstND && SecondND) { 10780 ODRDiagDeclError(FirstRecord, FirstModule, 10781 FirstFriend->getFriendLoc(), 10782 FirstFriend->getSourceRange(), FriendFunction) 10783 << FirstND; 10784 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10785 SecondFriend->getSourceRange(), FriendFunction) 10786 << SecondND; 10787 10788 Diagnosed = true; 10789 break; 10790 } 10791 10792 if (FirstTSI && SecondTSI) { 10793 QualType FirstFriendType = FirstTSI->getType(); 10794 QualType SecondFriendType = SecondTSI->getType(); 10795 assert(ComputeQualTypeODRHash(FirstFriendType) != 10796 ComputeQualTypeODRHash(SecondFriendType)); 10797 ODRDiagDeclError(FirstRecord, FirstModule, 10798 FirstFriend->getFriendLoc(), 10799 FirstFriend->getSourceRange(), FriendType) 10800 << FirstFriendType; 10801 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10802 SecondFriend->getSourceRange(), FriendType) 10803 << SecondFriendType; 10804 Diagnosed = true; 10805 break; 10806 } 10807 10808 ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(), 10809 FirstFriend->getSourceRange(), FriendTypeFunction) 10810 << (FirstTSI == nullptr); 10811 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10812 SecondFriend->getSourceRange(), FriendTypeFunction) 10813 << (SecondTSI == nullptr); 10814 10815 Diagnosed = true; 10816 break; 10817 } 10818 case FunctionTemplate: { 10819 FunctionTemplateDecl *FirstTemplate = 10820 cast<FunctionTemplateDecl>(FirstDecl); 10821 FunctionTemplateDecl *SecondTemplate = 10822 cast<FunctionTemplateDecl>(SecondDecl); 10823 10824 TemplateParameterList *FirstTPL = 10825 FirstTemplate->getTemplateParameters(); 10826 TemplateParameterList *SecondTPL = 10827 SecondTemplate->getTemplateParameters(); 10828 10829 if (FirstTPL->size() != SecondTPL->size()) { 10830 ODRDiagDeclError(FirstRecord, FirstModule, 10831 FirstTemplate->getLocation(), 10832 FirstTemplate->getSourceRange(), 10833 FunctionTemplateDifferentNumberParameters) 10834 << FirstTemplate << FirstTPL->size(); 10835 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10836 SecondTemplate->getSourceRange(), 10837 FunctionTemplateDifferentNumberParameters) 10838 << SecondTemplate << SecondTPL->size(); 10839 10840 Diagnosed = true; 10841 break; 10842 } 10843 10844 bool ParameterMismatch = false; 10845 for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) { 10846 NamedDecl *FirstParam = FirstTPL->getParam(i); 10847 NamedDecl *SecondParam = SecondTPL->getParam(i); 10848 10849 if (FirstParam->getKind() != SecondParam->getKind()) { 10850 enum { 10851 TemplateTypeParameter, 10852 NonTypeTemplateParameter, 10853 TemplateTemplateParameter, 10854 }; 10855 auto GetParamType = [](NamedDecl *D) { 10856 switch (D->getKind()) { 10857 default: 10858 llvm_unreachable("Unexpected template parameter type"); 10859 case Decl::TemplateTypeParm: 10860 return TemplateTypeParameter; 10861 case Decl::NonTypeTemplateParm: 10862 return NonTypeTemplateParameter; 10863 case Decl::TemplateTemplateParm: 10864 return TemplateTemplateParameter; 10865 } 10866 }; 10867 10868 ODRDiagDeclError(FirstRecord, FirstModule, 10869 FirstTemplate->getLocation(), 10870 FirstTemplate->getSourceRange(), 10871 FunctionTemplateParameterDifferentKind) 10872 << FirstTemplate << (i + 1) << GetParamType(FirstParam); 10873 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10874 SecondTemplate->getSourceRange(), 10875 FunctionTemplateParameterDifferentKind) 10876 << SecondTemplate << (i + 1) << GetParamType(SecondParam); 10877 10878 ParameterMismatch = true; 10879 break; 10880 } 10881 10882 if (FirstParam->getName() != SecondParam->getName()) { 10883 ODRDiagDeclError( 10884 FirstRecord, FirstModule, FirstTemplate->getLocation(), 10885 FirstTemplate->getSourceRange(), FunctionTemplateParameterName) 10886 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier() 10887 << FirstParam; 10888 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10889 SecondTemplate->getSourceRange(), 10890 FunctionTemplateParameterName) 10891 << SecondTemplate << (i + 1) 10892 << (bool)SecondParam->getIdentifier() << SecondParam; 10893 ParameterMismatch = true; 10894 break; 10895 } 10896 10897 if (isa<TemplateTypeParmDecl>(FirstParam) && 10898 isa<TemplateTypeParmDecl>(SecondParam)) { 10899 TemplateTypeParmDecl *FirstTTPD = 10900 cast<TemplateTypeParmDecl>(FirstParam); 10901 TemplateTypeParmDecl *SecondTTPD = 10902 cast<TemplateTypeParmDecl>(SecondParam); 10903 bool HasFirstDefaultArgument = 10904 FirstTTPD->hasDefaultArgument() && 10905 !FirstTTPD->defaultArgumentWasInherited(); 10906 bool HasSecondDefaultArgument = 10907 SecondTTPD->hasDefaultArgument() && 10908 !SecondTTPD->defaultArgumentWasInherited(); 10909 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10910 ODRDiagDeclError(FirstRecord, FirstModule, 10911 FirstTemplate->getLocation(), 10912 FirstTemplate->getSourceRange(), 10913 FunctionTemplateParameterSingleDefaultArgument) 10914 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 10915 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10916 SecondTemplate->getSourceRange(), 10917 FunctionTemplateParameterSingleDefaultArgument) 10918 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 10919 ParameterMismatch = true; 10920 break; 10921 } 10922 10923 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 10924 QualType FirstType = FirstTTPD->getDefaultArgument(); 10925 QualType SecondType = SecondTTPD->getDefaultArgument(); 10926 if (ComputeQualTypeODRHash(FirstType) != 10927 ComputeQualTypeODRHash(SecondType)) { 10928 ODRDiagDeclError( 10929 FirstRecord, FirstModule, FirstTemplate->getLocation(), 10930 FirstTemplate->getSourceRange(), 10931 FunctionTemplateParameterDifferentDefaultArgument) 10932 << FirstTemplate << (i + 1) << FirstType; 10933 ODRDiagDeclNote( 10934 SecondModule, SecondTemplate->getLocation(), 10935 SecondTemplate->getSourceRange(), 10936 FunctionTemplateParameterDifferentDefaultArgument) 10937 << SecondTemplate << (i + 1) << SecondType; 10938 ParameterMismatch = true; 10939 break; 10940 } 10941 } 10942 10943 if (FirstTTPD->isParameterPack() != 10944 SecondTTPD->isParameterPack()) { 10945 ODRDiagDeclError(FirstRecord, FirstModule, 10946 FirstTemplate->getLocation(), 10947 FirstTemplate->getSourceRange(), 10948 FunctionTemplatePackParameter) 10949 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 10950 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10951 SecondTemplate->getSourceRange(), 10952 FunctionTemplatePackParameter) 10953 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 10954 ParameterMismatch = true; 10955 break; 10956 } 10957 } 10958 10959 if (isa<TemplateTemplateParmDecl>(FirstParam) && 10960 isa<TemplateTemplateParmDecl>(SecondParam)) { 10961 TemplateTemplateParmDecl *FirstTTPD = 10962 cast<TemplateTemplateParmDecl>(FirstParam); 10963 TemplateTemplateParmDecl *SecondTTPD = 10964 cast<TemplateTemplateParmDecl>(SecondParam); 10965 10966 TemplateParameterList *FirstTPL = 10967 FirstTTPD->getTemplateParameters(); 10968 TemplateParameterList *SecondTPL = 10969 SecondTTPD->getTemplateParameters(); 10970 10971 if (ComputeTemplateParameterListODRHash(FirstTPL) != 10972 ComputeTemplateParameterListODRHash(SecondTPL)) { 10973 ODRDiagDeclError(FirstRecord, FirstModule, 10974 FirstTemplate->getLocation(), 10975 FirstTemplate->getSourceRange(), 10976 FunctionTemplateParameterDifferentType) 10977 << FirstTemplate << (i + 1); 10978 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10979 SecondTemplate->getSourceRange(), 10980 FunctionTemplateParameterDifferentType) 10981 << SecondTemplate << (i + 1); 10982 ParameterMismatch = true; 10983 break; 10984 } 10985 10986 bool HasFirstDefaultArgument = 10987 FirstTTPD->hasDefaultArgument() && 10988 !FirstTTPD->defaultArgumentWasInherited(); 10989 bool HasSecondDefaultArgument = 10990 SecondTTPD->hasDefaultArgument() && 10991 !SecondTTPD->defaultArgumentWasInherited(); 10992 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10993 ODRDiagDeclError(FirstRecord, FirstModule, 10994 FirstTemplate->getLocation(), 10995 FirstTemplate->getSourceRange(), 10996 FunctionTemplateParameterSingleDefaultArgument) 10997 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 10998 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10999 SecondTemplate->getSourceRange(), 11000 FunctionTemplateParameterSingleDefaultArgument) 11001 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11002 ParameterMismatch = true; 11003 break; 11004 } 11005 11006 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11007 TemplateArgument FirstTA = 11008 FirstTTPD->getDefaultArgument().getArgument(); 11009 TemplateArgument SecondTA = 11010 SecondTTPD->getDefaultArgument().getArgument(); 11011 if (ComputeTemplateArgumentODRHash(FirstTA) != 11012 ComputeTemplateArgumentODRHash(SecondTA)) { 11013 ODRDiagDeclError( 11014 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11015 FirstTemplate->getSourceRange(), 11016 FunctionTemplateParameterDifferentDefaultArgument) 11017 << FirstTemplate << (i + 1) << FirstTA; 11018 ODRDiagDeclNote( 11019 SecondModule, SecondTemplate->getLocation(), 11020 SecondTemplate->getSourceRange(), 11021 FunctionTemplateParameterDifferentDefaultArgument) 11022 << SecondTemplate << (i + 1) << SecondTA; 11023 ParameterMismatch = true; 11024 break; 11025 } 11026 } 11027 11028 if (FirstTTPD->isParameterPack() != 11029 SecondTTPD->isParameterPack()) { 11030 ODRDiagDeclError(FirstRecord, FirstModule, 11031 FirstTemplate->getLocation(), 11032 FirstTemplate->getSourceRange(), 11033 FunctionTemplatePackParameter) 11034 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 11035 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11036 SecondTemplate->getSourceRange(), 11037 FunctionTemplatePackParameter) 11038 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 11039 ParameterMismatch = true; 11040 break; 11041 } 11042 } 11043 11044 if (isa<NonTypeTemplateParmDecl>(FirstParam) && 11045 isa<NonTypeTemplateParmDecl>(SecondParam)) { 11046 NonTypeTemplateParmDecl *FirstNTTPD = 11047 cast<NonTypeTemplateParmDecl>(FirstParam); 11048 NonTypeTemplateParmDecl *SecondNTTPD = 11049 cast<NonTypeTemplateParmDecl>(SecondParam); 11050 11051 QualType FirstType = FirstNTTPD->getType(); 11052 QualType SecondType = SecondNTTPD->getType(); 11053 if (ComputeQualTypeODRHash(FirstType) != 11054 ComputeQualTypeODRHash(SecondType)) { 11055 ODRDiagDeclError(FirstRecord, FirstModule, 11056 FirstTemplate->getLocation(), 11057 FirstTemplate->getSourceRange(), 11058 FunctionTemplateParameterDifferentType) 11059 << FirstTemplate << (i + 1); 11060 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11061 SecondTemplate->getSourceRange(), 11062 FunctionTemplateParameterDifferentType) 11063 << SecondTemplate << (i + 1); 11064 ParameterMismatch = true; 11065 break; 11066 } 11067 11068 bool HasFirstDefaultArgument = 11069 FirstNTTPD->hasDefaultArgument() && 11070 !FirstNTTPD->defaultArgumentWasInherited(); 11071 bool HasSecondDefaultArgument = 11072 SecondNTTPD->hasDefaultArgument() && 11073 !SecondNTTPD->defaultArgumentWasInherited(); 11074 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11075 ODRDiagDeclError(FirstRecord, FirstModule, 11076 FirstTemplate->getLocation(), 11077 FirstTemplate->getSourceRange(), 11078 FunctionTemplateParameterSingleDefaultArgument) 11079 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11080 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11081 SecondTemplate->getSourceRange(), 11082 FunctionTemplateParameterSingleDefaultArgument) 11083 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11084 ParameterMismatch = true; 11085 break; 11086 } 11087 11088 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11089 Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument(); 11090 Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument(); 11091 if (ComputeODRHash(FirstDefaultArgument) != 11092 ComputeODRHash(SecondDefaultArgument)) { 11093 ODRDiagDeclError( 11094 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11095 FirstTemplate->getSourceRange(), 11096 FunctionTemplateParameterDifferentDefaultArgument) 11097 << FirstTemplate << (i + 1) << FirstDefaultArgument; 11098 ODRDiagDeclNote( 11099 SecondModule, SecondTemplate->getLocation(), 11100 SecondTemplate->getSourceRange(), 11101 FunctionTemplateParameterDifferentDefaultArgument) 11102 << SecondTemplate << (i + 1) << SecondDefaultArgument; 11103 ParameterMismatch = true; 11104 break; 11105 } 11106 } 11107 11108 if (FirstNTTPD->isParameterPack() != 11109 SecondNTTPD->isParameterPack()) { 11110 ODRDiagDeclError(FirstRecord, FirstModule, 11111 FirstTemplate->getLocation(), 11112 FirstTemplate->getSourceRange(), 11113 FunctionTemplatePackParameter) 11114 << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack(); 11115 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11116 SecondTemplate->getSourceRange(), 11117 FunctionTemplatePackParameter) 11118 << SecondTemplate << (i + 1) 11119 << SecondNTTPD->isParameterPack(); 11120 ParameterMismatch = true; 11121 break; 11122 } 11123 } 11124 } 11125 11126 if (ParameterMismatch) { 11127 Diagnosed = true; 11128 break; 11129 } 11130 11131 break; 11132 } 11133 } 11134 11135 if (Diagnosed) 11136 continue; 11137 11138 Diag(FirstDecl->getLocation(), 11139 diag::err_module_odr_violation_mismatch_decl_unknown) 11140 << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType 11141 << FirstDecl->getSourceRange(); 11142 Diag(SecondDecl->getLocation(), 11143 diag::note_module_odr_violation_mismatch_decl_unknown) 11144 << SecondModule << FirstDiffType << SecondDecl->getSourceRange(); 11145 Diagnosed = true; 11146 } 11147 11148 if (!Diagnosed) { 11149 // All definitions are updates to the same declaration. This happens if a 11150 // module instantiates the declaration of a class template specialization 11151 // and two or more other modules instantiate its definition. 11152 // 11153 // FIXME: Indicate which modules had instantiations of this definition. 11154 // FIXME: How can this even happen? 11155 Diag(Merge.first->getLocation(), 11156 diag::err_module_odr_violation_different_instantiations) 11157 << Merge.first; 11158 } 11159 } 11160 11161 // Issue ODR failures diagnostics for functions. 11162 for (auto &Merge : FunctionOdrMergeFailures) { 11163 enum ODRFunctionDifference { 11164 ReturnType, 11165 ParameterName, 11166 ParameterType, 11167 ParameterSingleDefaultArgument, 11168 ParameterDifferentDefaultArgument, 11169 FunctionBody, 11170 }; 11171 11172 FunctionDecl *FirstFunction = Merge.first; 11173 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction); 11174 11175 bool Diagnosed = false; 11176 for (auto &SecondFunction : Merge.second) { 11177 11178 if (FirstFunction == SecondFunction) 11179 continue; 11180 11181 std::string SecondModule = 11182 getOwningModuleNameForDiagnostic(SecondFunction); 11183 11184 auto ODRDiagError = [FirstFunction, &FirstModule, 11185 this](SourceLocation Loc, SourceRange Range, 11186 ODRFunctionDifference DiffType) { 11187 return Diag(Loc, diag::err_module_odr_violation_function) 11188 << FirstFunction << FirstModule.empty() << FirstModule << Range 11189 << DiffType; 11190 }; 11191 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11192 SourceRange Range, 11193 ODRFunctionDifference DiffType) { 11194 return Diag(Loc, diag::note_module_odr_violation_function) 11195 << SecondModule << Range << DiffType; 11196 }; 11197 11198 if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) != 11199 ComputeQualTypeODRHash(SecondFunction->getReturnType())) { 11200 ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(), 11201 FirstFunction->getReturnTypeSourceRange(), ReturnType) 11202 << FirstFunction->getReturnType(); 11203 ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(), 11204 SecondFunction->getReturnTypeSourceRange(), ReturnType) 11205 << SecondFunction->getReturnType(); 11206 Diagnosed = true; 11207 break; 11208 } 11209 11210 assert(FirstFunction->param_size() == SecondFunction->param_size() && 11211 "Merged functions with different number of parameters"); 11212 11213 auto ParamSize = FirstFunction->param_size(); 11214 bool ParameterMismatch = false; 11215 for (unsigned I = 0; I < ParamSize; ++I) { 11216 auto *FirstParam = FirstFunction->getParamDecl(I); 11217 auto *SecondParam = SecondFunction->getParamDecl(I); 11218 11219 assert(getContext().hasSameType(FirstParam->getType(), 11220 SecondParam->getType()) && 11221 "Merged function has different parameter types."); 11222 11223 if (FirstParam->getDeclName() != SecondParam->getDeclName()) { 11224 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11225 ParameterName) 11226 << I + 1 << FirstParam->getDeclName(); 11227 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11228 ParameterName) 11229 << I + 1 << SecondParam->getDeclName(); 11230 ParameterMismatch = true; 11231 break; 11232 }; 11233 11234 QualType FirstParamType = FirstParam->getType(); 11235 QualType SecondParamType = SecondParam->getType(); 11236 if (FirstParamType != SecondParamType && 11237 ComputeQualTypeODRHash(FirstParamType) != 11238 ComputeQualTypeODRHash(SecondParamType)) { 11239 if (const DecayedType *ParamDecayedType = 11240 FirstParamType->getAs<DecayedType>()) { 11241 ODRDiagError(FirstParam->getLocation(), 11242 FirstParam->getSourceRange(), ParameterType) 11243 << (I + 1) << FirstParamType << true 11244 << ParamDecayedType->getOriginalType(); 11245 } else { 11246 ODRDiagError(FirstParam->getLocation(), 11247 FirstParam->getSourceRange(), ParameterType) 11248 << (I + 1) << FirstParamType << false; 11249 } 11250 11251 if (const DecayedType *ParamDecayedType = 11252 SecondParamType->getAs<DecayedType>()) { 11253 ODRDiagNote(SecondParam->getLocation(), 11254 SecondParam->getSourceRange(), ParameterType) 11255 << (I + 1) << SecondParamType << true 11256 << ParamDecayedType->getOriginalType(); 11257 } else { 11258 ODRDiagNote(SecondParam->getLocation(), 11259 SecondParam->getSourceRange(), ParameterType) 11260 << (I + 1) << SecondParamType << false; 11261 } 11262 ParameterMismatch = true; 11263 break; 11264 } 11265 11266 const Expr *FirstInit = FirstParam->getInit(); 11267 const Expr *SecondInit = SecondParam->getInit(); 11268 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 11269 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11270 ParameterSingleDefaultArgument) 11271 << (I + 1) << (FirstInit == nullptr) 11272 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 11273 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11274 ParameterSingleDefaultArgument) 11275 << (I + 1) << (SecondInit == nullptr) 11276 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 11277 ParameterMismatch = true; 11278 break; 11279 } 11280 11281 if (FirstInit && SecondInit && 11282 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11283 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11284 ParameterDifferentDefaultArgument) 11285 << (I + 1) << FirstInit->getSourceRange(); 11286 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11287 ParameterDifferentDefaultArgument) 11288 << (I + 1) << SecondInit->getSourceRange(); 11289 ParameterMismatch = true; 11290 break; 11291 } 11292 11293 assert(ComputeSubDeclODRHash(FirstParam) == 11294 ComputeSubDeclODRHash(SecondParam) && 11295 "Undiagnosed parameter difference."); 11296 } 11297 11298 if (ParameterMismatch) { 11299 Diagnosed = true; 11300 break; 11301 } 11302 11303 // If no error has been generated before now, assume the problem is in 11304 // the body and generate a message. 11305 ODRDiagError(FirstFunction->getLocation(), 11306 FirstFunction->getSourceRange(), FunctionBody); 11307 ODRDiagNote(SecondFunction->getLocation(), 11308 SecondFunction->getSourceRange(), FunctionBody); 11309 Diagnosed = true; 11310 break; 11311 } 11312 (void)Diagnosed; 11313 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11314 } 11315 11316 // Issue ODR failures diagnostics for enums. 11317 for (auto &Merge : EnumOdrMergeFailures) { 11318 enum ODREnumDifference { 11319 SingleScopedEnum, 11320 EnumTagKeywordMismatch, 11321 SingleSpecifiedType, 11322 DifferentSpecifiedTypes, 11323 DifferentNumberEnumConstants, 11324 EnumConstantName, 11325 EnumConstantSingleInitilizer, 11326 EnumConstantDifferentInitilizer, 11327 }; 11328 11329 // If we've already pointed out a specific problem with this enum, don't 11330 // bother issuing a general "something's different" diagnostic. 11331 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 11332 continue; 11333 11334 EnumDecl *FirstEnum = Merge.first; 11335 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum); 11336 11337 using DeclHashes = 11338 llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>; 11339 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum]( 11340 DeclHashes &Hashes, EnumDecl *Enum) { 11341 for (auto *D : Enum->decls()) { 11342 // Due to decl merging, the first EnumDecl is the parent of 11343 // Decls in both records. 11344 if (!ODRHash::isDeclToBeProcessed(D, FirstEnum)) 11345 continue; 11346 assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind"); 11347 Hashes.emplace_back(cast<EnumConstantDecl>(D), 11348 ComputeSubDeclODRHash(D)); 11349 } 11350 }; 11351 DeclHashes FirstHashes; 11352 PopulateHashes(FirstHashes, FirstEnum); 11353 bool Diagnosed = false; 11354 for (auto &SecondEnum : Merge.second) { 11355 11356 if (FirstEnum == SecondEnum) 11357 continue; 11358 11359 std::string SecondModule = 11360 getOwningModuleNameForDiagnostic(SecondEnum); 11361 11362 auto ODRDiagError = [FirstEnum, &FirstModule, 11363 this](SourceLocation Loc, SourceRange Range, 11364 ODREnumDifference DiffType) { 11365 return Diag(Loc, diag::err_module_odr_violation_enum) 11366 << FirstEnum << FirstModule.empty() << FirstModule << Range 11367 << DiffType; 11368 }; 11369 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11370 SourceRange Range, 11371 ODREnumDifference DiffType) { 11372 return Diag(Loc, diag::note_module_odr_violation_enum) 11373 << SecondModule << Range << DiffType; 11374 }; 11375 11376 if (FirstEnum->isScoped() != SecondEnum->isScoped()) { 11377 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11378 SingleScopedEnum) 11379 << FirstEnum->isScoped(); 11380 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11381 SingleScopedEnum) 11382 << SecondEnum->isScoped(); 11383 Diagnosed = true; 11384 continue; 11385 } 11386 11387 if (FirstEnum->isScoped() && SecondEnum->isScoped()) { 11388 if (FirstEnum->isScopedUsingClassTag() != 11389 SecondEnum->isScopedUsingClassTag()) { 11390 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11391 EnumTagKeywordMismatch) 11392 << FirstEnum->isScopedUsingClassTag(); 11393 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11394 EnumTagKeywordMismatch) 11395 << SecondEnum->isScopedUsingClassTag(); 11396 Diagnosed = true; 11397 continue; 11398 } 11399 } 11400 11401 QualType FirstUnderlyingType = 11402 FirstEnum->getIntegerTypeSourceInfo() 11403 ? FirstEnum->getIntegerTypeSourceInfo()->getType() 11404 : QualType(); 11405 QualType SecondUnderlyingType = 11406 SecondEnum->getIntegerTypeSourceInfo() 11407 ? SecondEnum->getIntegerTypeSourceInfo()->getType() 11408 : QualType(); 11409 if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) { 11410 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11411 SingleSpecifiedType) 11412 << !FirstUnderlyingType.isNull(); 11413 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11414 SingleSpecifiedType) 11415 << !SecondUnderlyingType.isNull(); 11416 Diagnosed = true; 11417 continue; 11418 } 11419 11420 if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) { 11421 if (ComputeQualTypeODRHash(FirstUnderlyingType) != 11422 ComputeQualTypeODRHash(SecondUnderlyingType)) { 11423 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11424 DifferentSpecifiedTypes) 11425 << FirstUnderlyingType; 11426 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11427 DifferentSpecifiedTypes) 11428 << SecondUnderlyingType; 11429 Diagnosed = true; 11430 continue; 11431 } 11432 } 11433 11434 DeclHashes SecondHashes; 11435 PopulateHashes(SecondHashes, SecondEnum); 11436 11437 if (FirstHashes.size() != SecondHashes.size()) { 11438 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11439 DifferentNumberEnumConstants) 11440 << (int)FirstHashes.size(); 11441 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11442 DifferentNumberEnumConstants) 11443 << (int)SecondHashes.size(); 11444 Diagnosed = true; 11445 continue; 11446 } 11447 11448 for (unsigned I = 0; I < FirstHashes.size(); ++I) { 11449 if (FirstHashes[I].second == SecondHashes[I].second) 11450 continue; 11451 const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first; 11452 const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first; 11453 11454 if (FirstEnumConstant->getDeclName() != 11455 SecondEnumConstant->getDeclName()) { 11456 11457 ODRDiagError(FirstEnumConstant->getLocation(), 11458 FirstEnumConstant->getSourceRange(), EnumConstantName) 11459 << I + 1 << FirstEnumConstant; 11460 ODRDiagNote(SecondEnumConstant->getLocation(), 11461 SecondEnumConstant->getSourceRange(), EnumConstantName) 11462 << I + 1 << SecondEnumConstant; 11463 Diagnosed = true; 11464 break; 11465 } 11466 11467 const Expr *FirstInit = FirstEnumConstant->getInitExpr(); 11468 const Expr *SecondInit = SecondEnumConstant->getInitExpr(); 11469 if (!FirstInit && !SecondInit) 11470 continue; 11471 11472 if (!FirstInit || !SecondInit) { 11473 ODRDiagError(FirstEnumConstant->getLocation(), 11474 FirstEnumConstant->getSourceRange(), 11475 EnumConstantSingleInitilizer) 11476 << I + 1 << FirstEnumConstant << (FirstInit != nullptr); 11477 ODRDiagNote(SecondEnumConstant->getLocation(), 11478 SecondEnumConstant->getSourceRange(), 11479 EnumConstantSingleInitilizer) 11480 << I + 1 << SecondEnumConstant << (SecondInit != nullptr); 11481 Diagnosed = true; 11482 break; 11483 } 11484 11485 if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11486 ODRDiagError(FirstEnumConstant->getLocation(), 11487 FirstEnumConstant->getSourceRange(), 11488 EnumConstantDifferentInitilizer) 11489 << I + 1 << FirstEnumConstant; 11490 ODRDiagNote(SecondEnumConstant->getLocation(), 11491 SecondEnumConstant->getSourceRange(), 11492 EnumConstantDifferentInitilizer) 11493 << I + 1 << SecondEnumConstant; 11494 Diagnosed = true; 11495 break; 11496 } 11497 } 11498 } 11499 11500 (void)Diagnosed; 11501 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11502 } 11503 } 11504 11505 void ASTReader::StartedDeserializing() { 11506 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get()) 11507 ReadTimer->startTimer(); 11508 } 11509 11510 void ASTReader::FinishedDeserializing() { 11511 assert(NumCurrentElementsDeserializing && 11512 "FinishedDeserializing not paired with StartedDeserializing"); 11513 if (NumCurrentElementsDeserializing == 1) { 11514 // We decrease NumCurrentElementsDeserializing only after pending actions 11515 // are finished, to avoid recursively re-calling finishPendingActions(). 11516 finishPendingActions(); 11517 } 11518 --NumCurrentElementsDeserializing; 11519 11520 if (NumCurrentElementsDeserializing == 0) { 11521 // Propagate exception specification and deduced type updates along 11522 // redeclaration chains. 11523 // 11524 // We do this now rather than in finishPendingActions because we want to 11525 // be able to walk the complete redeclaration chains of the updated decls. 11526 while (!PendingExceptionSpecUpdates.empty() || 11527 !PendingDeducedTypeUpdates.empty()) { 11528 auto ESUpdates = std::move(PendingExceptionSpecUpdates); 11529 PendingExceptionSpecUpdates.clear(); 11530 for (auto Update : ESUpdates) { 11531 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11532 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>(); 11533 auto ESI = FPT->getExtProtoInfo().ExceptionSpec; 11534 if (auto *Listener = getContext().getASTMutationListener()) 11535 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second)); 11536 for (auto *Redecl : Update.second->redecls()) 11537 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI); 11538 } 11539 11540 auto DTUpdates = std::move(PendingDeducedTypeUpdates); 11541 PendingDeducedTypeUpdates.clear(); 11542 for (auto Update : DTUpdates) { 11543 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11544 // FIXME: If the return type is already deduced, check that it matches. 11545 getContext().adjustDeducedFunctionResultType(Update.first, 11546 Update.second); 11547 } 11548 } 11549 11550 if (ReadTimer) 11551 ReadTimer->stopTimer(); 11552 11553 diagnoseOdrViolations(); 11554 11555 // We are not in recursive loading, so it's safe to pass the "interesting" 11556 // decls to the consumer. 11557 if (Consumer) 11558 PassInterestingDeclsToConsumer(); 11559 } 11560 } 11561 11562 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) { 11563 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) { 11564 // Remove any fake results before adding any real ones. 11565 auto It = PendingFakeLookupResults.find(II); 11566 if (It != PendingFakeLookupResults.end()) { 11567 for (auto *ND : It->second) 11568 SemaObj->IdResolver.RemoveDecl(ND); 11569 // FIXME: this works around module+PCH performance issue. 11570 // Rather than erase the result from the map, which is O(n), just clear 11571 // the vector of NamedDecls. 11572 It->second.clear(); 11573 } 11574 } 11575 11576 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) { 11577 SemaObj->TUScope->AddDecl(D); 11578 } else if (SemaObj->TUScope) { 11579 // Adding the decl to IdResolver may have failed because it was already in 11580 // (even though it was not added in scope). If it is already in, make sure 11581 // it gets in the scope as well. 11582 if (std::find(SemaObj->IdResolver.begin(Name), 11583 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end()) 11584 SemaObj->TUScope->AddDecl(D); 11585 } 11586 } 11587 11588 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache, 11589 ASTContext *Context, 11590 const PCHContainerReader &PCHContainerRdr, 11591 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, 11592 StringRef isysroot, 11593 DisableValidationForModuleKind DisableValidationKind, 11594 bool AllowASTWithCompilerErrors, 11595 bool AllowConfigurationMismatch, bool ValidateSystemInputs, 11596 bool ValidateASTInputFilesContent, bool UseGlobalIndex, 11597 std::unique_ptr<llvm::Timer> ReadTimer) 11598 : Listener(bool(DisableValidationKind &DisableValidationForModuleKind::PCH) 11599 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP)) 11600 : cast<ASTReaderListener>(new PCHValidator(PP, *this))), 11601 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), 11602 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP), 11603 ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache, 11604 PCHContainerRdr, PP.getHeaderSearchInfo()), 11605 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot), 11606 DisableValidationKind(DisableValidationKind), 11607 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors), 11608 AllowConfigurationMismatch(AllowConfigurationMismatch), 11609 ValidateSystemInputs(ValidateSystemInputs), 11610 ValidateASTInputFilesContent(ValidateASTInputFilesContent), 11611 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) { 11612 SourceMgr.setExternalSLocEntrySource(this); 11613 11614 for (const auto &Ext : Extensions) { 11615 auto BlockName = Ext->getExtensionMetadata().BlockName; 11616 auto Known = ModuleFileExtensions.find(BlockName); 11617 if (Known != ModuleFileExtensions.end()) { 11618 Diags.Report(diag::warn_duplicate_module_file_extension) 11619 << BlockName; 11620 continue; 11621 } 11622 11623 ModuleFileExtensions.insert({BlockName, Ext}); 11624 } 11625 } 11626 11627 ASTReader::~ASTReader() { 11628 if (OwnsDeserializationListener) 11629 delete DeserializationListener; 11630 } 11631 11632 IdentifierResolver &ASTReader::getIdResolver() { 11633 return SemaObj ? SemaObj->IdResolver : DummyIdResolver; 11634 } 11635 11636 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor, 11637 unsigned AbbrevID) { 11638 Idx = 0; 11639 Record.clear(); 11640 return Cursor.readRecord(AbbrevID, Record); 11641 } 11642 //===----------------------------------------------------------------------===// 11643 //// OMPClauseReader implementation 11644 ////===----------------------------------------------------------------------===// 11645 11646 // This has to be in namespace clang because it's friended by all 11647 // of the OMP clauses. 11648 namespace clang { 11649 11650 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> { 11651 ASTRecordReader &Record; 11652 ASTContext &Context; 11653 11654 public: 11655 OMPClauseReader(ASTRecordReader &Record) 11656 : Record(Record), Context(Record.getContext()) {} 11657 #define GEN_CLANG_CLAUSE_CLASS 11658 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C); 11659 #include "llvm/Frontend/OpenMP/OMP.inc" 11660 OMPClause *readClause(); 11661 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C); 11662 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C); 11663 }; 11664 11665 } // end namespace clang 11666 11667 OMPClause *ASTRecordReader::readOMPClause() { 11668 return OMPClauseReader(*this).readClause(); 11669 } 11670 11671 OMPClause *OMPClauseReader::readClause() { 11672 OMPClause *C = nullptr; 11673 switch (llvm::omp::Clause(Record.readInt())) { 11674 case llvm::omp::OMPC_if: 11675 C = new (Context) OMPIfClause(); 11676 break; 11677 case llvm::omp::OMPC_final: 11678 C = new (Context) OMPFinalClause(); 11679 break; 11680 case llvm::omp::OMPC_num_threads: 11681 C = new (Context) OMPNumThreadsClause(); 11682 break; 11683 case llvm::omp::OMPC_safelen: 11684 C = new (Context) OMPSafelenClause(); 11685 break; 11686 case llvm::omp::OMPC_simdlen: 11687 C = new (Context) OMPSimdlenClause(); 11688 break; 11689 case llvm::omp::OMPC_sizes: { 11690 unsigned NumSizes = Record.readInt(); 11691 C = OMPSizesClause::CreateEmpty(Context, NumSizes); 11692 break; 11693 } 11694 case llvm::omp::OMPC_full: 11695 C = OMPFullClause::CreateEmpty(Context); 11696 break; 11697 case llvm::omp::OMPC_partial: 11698 C = OMPPartialClause::CreateEmpty(Context); 11699 break; 11700 case llvm::omp::OMPC_allocator: 11701 C = new (Context) OMPAllocatorClause(); 11702 break; 11703 case llvm::omp::OMPC_collapse: 11704 C = new (Context) OMPCollapseClause(); 11705 break; 11706 case llvm::omp::OMPC_default: 11707 C = new (Context) OMPDefaultClause(); 11708 break; 11709 case llvm::omp::OMPC_proc_bind: 11710 C = new (Context) OMPProcBindClause(); 11711 break; 11712 case llvm::omp::OMPC_schedule: 11713 C = new (Context) OMPScheduleClause(); 11714 break; 11715 case llvm::omp::OMPC_ordered: 11716 C = OMPOrderedClause::CreateEmpty(Context, Record.readInt()); 11717 break; 11718 case llvm::omp::OMPC_nowait: 11719 C = new (Context) OMPNowaitClause(); 11720 break; 11721 case llvm::omp::OMPC_untied: 11722 C = new (Context) OMPUntiedClause(); 11723 break; 11724 case llvm::omp::OMPC_mergeable: 11725 C = new (Context) OMPMergeableClause(); 11726 break; 11727 case llvm::omp::OMPC_read: 11728 C = new (Context) OMPReadClause(); 11729 break; 11730 case llvm::omp::OMPC_write: 11731 C = new (Context) OMPWriteClause(); 11732 break; 11733 case llvm::omp::OMPC_update: 11734 C = OMPUpdateClause::CreateEmpty(Context, Record.readInt()); 11735 break; 11736 case llvm::omp::OMPC_capture: 11737 C = new (Context) OMPCaptureClause(); 11738 break; 11739 case llvm::omp::OMPC_compare: 11740 C = new (Context) OMPCompareClause(); 11741 break; 11742 case llvm::omp::OMPC_seq_cst: 11743 C = new (Context) OMPSeqCstClause(); 11744 break; 11745 case llvm::omp::OMPC_acq_rel: 11746 C = new (Context) OMPAcqRelClause(); 11747 break; 11748 case llvm::omp::OMPC_acquire: 11749 C = new (Context) OMPAcquireClause(); 11750 break; 11751 case llvm::omp::OMPC_release: 11752 C = new (Context) OMPReleaseClause(); 11753 break; 11754 case llvm::omp::OMPC_relaxed: 11755 C = new (Context) OMPRelaxedClause(); 11756 break; 11757 case llvm::omp::OMPC_threads: 11758 C = new (Context) OMPThreadsClause(); 11759 break; 11760 case llvm::omp::OMPC_simd: 11761 C = new (Context) OMPSIMDClause(); 11762 break; 11763 case llvm::omp::OMPC_nogroup: 11764 C = new (Context) OMPNogroupClause(); 11765 break; 11766 case llvm::omp::OMPC_unified_address: 11767 C = new (Context) OMPUnifiedAddressClause(); 11768 break; 11769 case llvm::omp::OMPC_unified_shared_memory: 11770 C = new (Context) OMPUnifiedSharedMemoryClause(); 11771 break; 11772 case llvm::omp::OMPC_reverse_offload: 11773 C = new (Context) OMPReverseOffloadClause(); 11774 break; 11775 case llvm::omp::OMPC_dynamic_allocators: 11776 C = new (Context) OMPDynamicAllocatorsClause(); 11777 break; 11778 case llvm::omp::OMPC_atomic_default_mem_order: 11779 C = new (Context) OMPAtomicDefaultMemOrderClause(); 11780 break; 11781 case llvm::omp::OMPC_private: 11782 C = OMPPrivateClause::CreateEmpty(Context, Record.readInt()); 11783 break; 11784 case llvm::omp::OMPC_firstprivate: 11785 C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt()); 11786 break; 11787 case llvm::omp::OMPC_lastprivate: 11788 C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt()); 11789 break; 11790 case llvm::omp::OMPC_shared: 11791 C = OMPSharedClause::CreateEmpty(Context, Record.readInt()); 11792 break; 11793 case llvm::omp::OMPC_reduction: { 11794 unsigned N = Record.readInt(); 11795 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>(); 11796 C = OMPReductionClause::CreateEmpty(Context, N, Modifier); 11797 break; 11798 } 11799 case llvm::omp::OMPC_task_reduction: 11800 C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt()); 11801 break; 11802 case llvm::omp::OMPC_in_reduction: 11803 C = OMPInReductionClause::CreateEmpty(Context, Record.readInt()); 11804 break; 11805 case llvm::omp::OMPC_linear: 11806 C = OMPLinearClause::CreateEmpty(Context, Record.readInt()); 11807 break; 11808 case llvm::omp::OMPC_aligned: 11809 C = OMPAlignedClause::CreateEmpty(Context, Record.readInt()); 11810 break; 11811 case llvm::omp::OMPC_copyin: 11812 C = OMPCopyinClause::CreateEmpty(Context, Record.readInt()); 11813 break; 11814 case llvm::omp::OMPC_copyprivate: 11815 C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt()); 11816 break; 11817 case llvm::omp::OMPC_flush: 11818 C = OMPFlushClause::CreateEmpty(Context, Record.readInt()); 11819 break; 11820 case llvm::omp::OMPC_depobj: 11821 C = OMPDepobjClause::CreateEmpty(Context); 11822 break; 11823 case llvm::omp::OMPC_depend: { 11824 unsigned NumVars = Record.readInt(); 11825 unsigned NumLoops = Record.readInt(); 11826 C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops); 11827 break; 11828 } 11829 case llvm::omp::OMPC_device: 11830 C = new (Context) OMPDeviceClause(); 11831 break; 11832 case llvm::omp::OMPC_map: { 11833 OMPMappableExprListSizeTy Sizes; 11834 Sizes.NumVars = Record.readInt(); 11835 Sizes.NumUniqueDeclarations = Record.readInt(); 11836 Sizes.NumComponentLists = Record.readInt(); 11837 Sizes.NumComponents = Record.readInt(); 11838 C = OMPMapClause::CreateEmpty(Context, Sizes); 11839 break; 11840 } 11841 case llvm::omp::OMPC_num_teams: 11842 C = new (Context) OMPNumTeamsClause(); 11843 break; 11844 case llvm::omp::OMPC_thread_limit: 11845 C = new (Context) OMPThreadLimitClause(); 11846 break; 11847 case llvm::omp::OMPC_priority: 11848 C = new (Context) OMPPriorityClause(); 11849 break; 11850 case llvm::omp::OMPC_grainsize: 11851 C = new (Context) OMPGrainsizeClause(); 11852 break; 11853 case llvm::omp::OMPC_num_tasks: 11854 C = new (Context) OMPNumTasksClause(); 11855 break; 11856 case llvm::omp::OMPC_hint: 11857 C = new (Context) OMPHintClause(); 11858 break; 11859 case llvm::omp::OMPC_dist_schedule: 11860 C = new (Context) OMPDistScheduleClause(); 11861 break; 11862 case llvm::omp::OMPC_defaultmap: 11863 C = new (Context) OMPDefaultmapClause(); 11864 break; 11865 case llvm::omp::OMPC_to: { 11866 OMPMappableExprListSizeTy Sizes; 11867 Sizes.NumVars = Record.readInt(); 11868 Sizes.NumUniqueDeclarations = Record.readInt(); 11869 Sizes.NumComponentLists = Record.readInt(); 11870 Sizes.NumComponents = Record.readInt(); 11871 C = OMPToClause::CreateEmpty(Context, Sizes); 11872 break; 11873 } 11874 case llvm::omp::OMPC_from: { 11875 OMPMappableExprListSizeTy Sizes; 11876 Sizes.NumVars = Record.readInt(); 11877 Sizes.NumUniqueDeclarations = Record.readInt(); 11878 Sizes.NumComponentLists = Record.readInt(); 11879 Sizes.NumComponents = Record.readInt(); 11880 C = OMPFromClause::CreateEmpty(Context, Sizes); 11881 break; 11882 } 11883 case llvm::omp::OMPC_use_device_ptr: { 11884 OMPMappableExprListSizeTy Sizes; 11885 Sizes.NumVars = Record.readInt(); 11886 Sizes.NumUniqueDeclarations = Record.readInt(); 11887 Sizes.NumComponentLists = Record.readInt(); 11888 Sizes.NumComponents = Record.readInt(); 11889 C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes); 11890 break; 11891 } 11892 case llvm::omp::OMPC_use_device_addr: { 11893 OMPMappableExprListSizeTy Sizes; 11894 Sizes.NumVars = Record.readInt(); 11895 Sizes.NumUniqueDeclarations = Record.readInt(); 11896 Sizes.NumComponentLists = Record.readInt(); 11897 Sizes.NumComponents = Record.readInt(); 11898 C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes); 11899 break; 11900 } 11901 case llvm::omp::OMPC_is_device_ptr: { 11902 OMPMappableExprListSizeTy Sizes; 11903 Sizes.NumVars = Record.readInt(); 11904 Sizes.NumUniqueDeclarations = Record.readInt(); 11905 Sizes.NumComponentLists = Record.readInt(); 11906 Sizes.NumComponents = Record.readInt(); 11907 C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes); 11908 break; 11909 } 11910 case llvm::omp::OMPC_has_device_addr: { 11911 OMPMappableExprListSizeTy Sizes; 11912 Sizes.NumVars = Record.readInt(); 11913 Sizes.NumUniqueDeclarations = Record.readInt(); 11914 Sizes.NumComponentLists = Record.readInt(); 11915 Sizes.NumComponents = Record.readInt(); 11916 C = OMPHasDeviceAddrClause::CreateEmpty(Context, Sizes); 11917 break; 11918 } 11919 case llvm::omp::OMPC_allocate: 11920 C = OMPAllocateClause::CreateEmpty(Context, Record.readInt()); 11921 break; 11922 case llvm::omp::OMPC_nontemporal: 11923 C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt()); 11924 break; 11925 case llvm::omp::OMPC_inclusive: 11926 C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt()); 11927 break; 11928 case llvm::omp::OMPC_exclusive: 11929 C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt()); 11930 break; 11931 case llvm::omp::OMPC_order: 11932 C = new (Context) OMPOrderClause(); 11933 break; 11934 case llvm::omp::OMPC_init: 11935 C = OMPInitClause::CreateEmpty(Context, Record.readInt()); 11936 break; 11937 case llvm::omp::OMPC_use: 11938 C = new (Context) OMPUseClause(); 11939 break; 11940 case llvm::omp::OMPC_destroy: 11941 C = new (Context) OMPDestroyClause(); 11942 break; 11943 case llvm::omp::OMPC_novariants: 11944 C = new (Context) OMPNovariantsClause(); 11945 break; 11946 case llvm::omp::OMPC_nocontext: 11947 C = new (Context) OMPNocontextClause(); 11948 break; 11949 case llvm::omp::OMPC_detach: 11950 C = new (Context) OMPDetachClause(); 11951 break; 11952 case llvm::omp::OMPC_uses_allocators: 11953 C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt()); 11954 break; 11955 case llvm::omp::OMPC_affinity: 11956 C = OMPAffinityClause::CreateEmpty(Context, Record.readInt()); 11957 break; 11958 case llvm::omp::OMPC_filter: 11959 C = new (Context) OMPFilterClause(); 11960 break; 11961 case llvm::omp::OMPC_bind: 11962 C = OMPBindClause::CreateEmpty(Context); 11963 break; 11964 case llvm::omp::OMPC_align: 11965 C = new (Context) OMPAlignClause(); 11966 break; 11967 #define OMP_CLAUSE_NO_CLASS(Enum, Str) \ 11968 case llvm::omp::Enum: \ 11969 break; 11970 #include "llvm/Frontend/OpenMP/OMPKinds.def" 11971 default: 11972 break; 11973 } 11974 assert(C && "Unknown OMPClause type"); 11975 11976 Visit(C); 11977 C->setLocStart(Record.readSourceLocation()); 11978 C->setLocEnd(Record.readSourceLocation()); 11979 11980 return C; 11981 } 11982 11983 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) { 11984 C->setPreInitStmt(Record.readSubStmt(), 11985 static_cast<OpenMPDirectiveKind>(Record.readInt())); 11986 } 11987 11988 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) { 11989 VisitOMPClauseWithPreInit(C); 11990 C->setPostUpdateExpr(Record.readSubExpr()); 11991 } 11992 11993 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) { 11994 VisitOMPClauseWithPreInit(C); 11995 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt())); 11996 C->setNameModifierLoc(Record.readSourceLocation()); 11997 C->setColonLoc(Record.readSourceLocation()); 11998 C->setCondition(Record.readSubExpr()); 11999 C->setLParenLoc(Record.readSourceLocation()); 12000 } 12001 12002 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) { 12003 VisitOMPClauseWithPreInit(C); 12004 C->setCondition(Record.readSubExpr()); 12005 C->setLParenLoc(Record.readSourceLocation()); 12006 } 12007 12008 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) { 12009 VisitOMPClauseWithPreInit(C); 12010 C->setNumThreads(Record.readSubExpr()); 12011 C->setLParenLoc(Record.readSourceLocation()); 12012 } 12013 12014 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) { 12015 C->setSafelen(Record.readSubExpr()); 12016 C->setLParenLoc(Record.readSourceLocation()); 12017 } 12018 12019 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) { 12020 C->setSimdlen(Record.readSubExpr()); 12021 C->setLParenLoc(Record.readSourceLocation()); 12022 } 12023 12024 void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) { 12025 for (Expr *&E : C->getSizesRefs()) 12026 E = Record.readSubExpr(); 12027 C->setLParenLoc(Record.readSourceLocation()); 12028 } 12029 12030 void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {} 12031 12032 void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) { 12033 C->setFactor(Record.readSubExpr()); 12034 C->setLParenLoc(Record.readSourceLocation()); 12035 } 12036 12037 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) { 12038 C->setAllocator(Record.readExpr()); 12039 C->setLParenLoc(Record.readSourceLocation()); 12040 } 12041 12042 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) { 12043 C->setNumForLoops(Record.readSubExpr()); 12044 C->setLParenLoc(Record.readSourceLocation()); 12045 } 12046 12047 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) { 12048 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt())); 12049 C->setLParenLoc(Record.readSourceLocation()); 12050 C->setDefaultKindKwLoc(Record.readSourceLocation()); 12051 } 12052 12053 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) { 12054 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt())); 12055 C->setLParenLoc(Record.readSourceLocation()); 12056 C->setProcBindKindKwLoc(Record.readSourceLocation()); 12057 } 12058 12059 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) { 12060 VisitOMPClauseWithPreInit(C); 12061 C->setScheduleKind( 12062 static_cast<OpenMPScheduleClauseKind>(Record.readInt())); 12063 C->setFirstScheduleModifier( 12064 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 12065 C->setSecondScheduleModifier( 12066 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 12067 C->setChunkSize(Record.readSubExpr()); 12068 C->setLParenLoc(Record.readSourceLocation()); 12069 C->setFirstScheduleModifierLoc(Record.readSourceLocation()); 12070 C->setSecondScheduleModifierLoc(Record.readSourceLocation()); 12071 C->setScheduleKindLoc(Record.readSourceLocation()); 12072 C->setCommaLoc(Record.readSourceLocation()); 12073 } 12074 12075 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) { 12076 C->setNumForLoops(Record.readSubExpr()); 12077 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 12078 C->setLoopNumIterations(I, Record.readSubExpr()); 12079 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 12080 C->setLoopCounter(I, Record.readSubExpr()); 12081 C->setLParenLoc(Record.readSourceLocation()); 12082 } 12083 12084 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) { 12085 C->setEventHandler(Record.readSubExpr()); 12086 C->setLParenLoc(Record.readSourceLocation()); 12087 } 12088 12089 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {} 12090 12091 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {} 12092 12093 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {} 12094 12095 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {} 12096 12097 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {} 12098 12099 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) { 12100 if (C->isExtended()) { 12101 C->setLParenLoc(Record.readSourceLocation()); 12102 C->setArgumentLoc(Record.readSourceLocation()); 12103 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>()); 12104 } 12105 } 12106 12107 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {} 12108 12109 void OMPClauseReader::VisitOMPCompareClause(OMPCompareClause *) {} 12110 12111 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {} 12112 12113 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {} 12114 12115 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {} 12116 12117 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {} 12118 12119 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {} 12120 12121 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {} 12122 12123 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {} 12124 12125 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {} 12126 12127 void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) { 12128 unsigned NumVars = C->varlist_size(); 12129 SmallVector<Expr *, 16> Vars; 12130 Vars.reserve(NumVars); 12131 for (unsigned I = 0; I != NumVars; ++I) 12132 Vars.push_back(Record.readSubExpr()); 12133 C->setVarRefs(Vars); 12134 C->setIsTarget(Record.readBool()); 12135 C->setIsTargetSync(Record.readBool()); 12136 C->setLParenLoc(Record.readSourceLocation()); 12137 C->setVarLoc(Record.readSourceLocation()); 12138 } 12139 12140 void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) { 12141 C->setInteropVar(Record.readSubExpr()); 12142 C->setLParenLoc(Record.readSourceLocation()); 12143 C->setVarLoc(Record.readSourceLocation()); 12144 } 12145 12146 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) { 12147 C->setInteropVar(Record.readSubExpr()); 12148 C->setLParenLoc(Record.readSourceLocation()); 12149 C->setVarLoc(Record.readSourceLocation()); 12150 } 12151 12152 void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) { 12153 VisitOMPClauseWithPreInit(C); 12154 C->setCondition(Record.readSubExpr()); 12155 C->setLParenLoc(Record.readSourceLocation()); 12156 } 12157 12158 void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) { 12159 VisitOMPClauseWithPreInit(C); 12160 C->setCondition(Record.readSubExpr()); 12161 C->setLParenLoc(Record.readSourceLocation()); 12162 } 12163 12164 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {} 12165 12166 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause( 12167 OMPUnifiedSharedMemoryClause *) {} 12168 12169 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {} 12170 12171 void 12172 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) { 12173 } 12174 12175 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause( 12176 OMPAtomicDefaultMemOrderClause *C) { 12177 C->setAtomicDefaultMemOrderKind( 12178 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt())); 12179 C->setLParenLoc(Record.readSourceLocation()); 12180 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation()); 12181 } 12182 12183 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) { 12184 C->setLParenLoc(Record.readSourceLocation()); 12185 unsigned NumVars = C->varlist_size(); 12186 SmallVector<Expr *, 16> Vars; 12187 Vars.reserve(NumVars); 12188 for (unsigned i = 0; i != NumVars; ++i) 12189 Vars.push_back(Record.readSubExpr()); 12190 C->setVarRefs(Vars); 12191 Vars.clear(); 12192 for (unsigned i = 0; i != NumVars; ++i) 12193 Vars.push_back(Record.readSubExpr()); 12194 C->setPrivateCopies(Vars); 12195 } 12196 12197 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) { 12198 VisitOMPClauseWithPreInit(C); 12199 C->setLParenLoc(Record.readSourceLocation()); 12200 unsigned NumVars = C->varlist_size(); 12201 SmallVector<Expr *, 16> Vars; 12202 Vars.reserve(NumVars); 12203 for (unsigned i = 0; i != NumVars; ++i) 12204 Vars.push_back(Record.readSubExpr()); 12205 C->setVarRefs(Vars); 12206 Vars.clear(); 12207 for (unsigned i = 0; i != NumVars; ++i) 12208 Vars.push_back(Record.readSubExpr()); 12209 C->setPrivateCopies(Vars); 12210 Vars.clear(); 12211 for (unsigned i = 0; i != NumVars; ++i) 12212 Vars.push_back(Record.readSubExpr()); 12213 C->setInits(Vars); 12214 } 12215 12216 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) { 12217 VisitOMPClauseWithPostUpdate(C); 12218 C->setLParenLoc(Record.readSourceLocation()); 12219 C->setKind(Record.readEnum<OpenMPLastprivateModifier>()); 12220 C->setKindLoc(Record.readSourceLocation()); 12221 C->setColonLoc(Record.readSourceLocation()); 12222 unsigned NumVars = C->varlist_size(); 12223 SmallVector<Expr *, 16> Vars; 12224 Vars.reserve(NumVars); 12225 for (unsigned i = 0; i != NumVars; ++i) 12226 Vars.push_back(Record.readSubExpr()); 12227 C->setVarRefs(Vars); 12228 Vars.clear(); 12229 for (unsigned i = 0; i != NumVars; ++i) 12230 Vars.push_back(Record.readSubExpr()); 12231 C->setPrivateCopies(Vars); 12232 Vars.clear(); 12233 for (unsigned i = 0; i != NumVars; ++i) 12234 Vars.push_back(Record.readSubExpr()); 12235 C->setSourceExprs(Vars); 12236 Vars.clear(); 12237 for (unsigned i = 0; i != NumVars; ++i) 12238 Vars.push_back(Record.readSubExpr()); 12239 C->setDestinationExprs(Vars); 12240 Vars.clear(); 12241 for (unsigned i = 0; i != NumVars; ++i) 12242 Vars.push_back(Record.readSubExpr()); 12243 C->setAssignmentOps(Vars); 12244 } 12245 12246 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) { 12247 C->setLParenLoc(Record.readSourceLocation()); 12248 unsigned NumVars = C->varlist_size(); 12249 SmallVector<Expr *, 16> Vars; 12250 Vars.reserve(NumVars); 12251 for (unsigned i = 0; i != NumVars; ++i) 12252 Vars.push_back(Record.readSubExpr()); 12253 C->setVarRefs(Vars); 12254 } 12255 12256 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) { 12257 VisitOMPClauseWithPostUpdate(C); 12258 C->setLParenLoc(Record.readSourceLocation()); 12259 C->setModifierLoc(Record.readSourceLocation()); 12260 C->setColonLoc(Record.readSourceLocation()); 12261 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12262 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12263 C->setQualifierLoc(NNSL); 12264 C->setNameInfo(DNI); 12265 12266 unsigned NumVars = C->varlist_size(); 12267 SmallVector<Expr *, 16> Vars; 12268 Vars.reserve(NumVars); 12269 for (unsigned i = 0; i != NumVars; ++i) 12270 Vars.push_back(Record.readSubExpr()); 12271 C->setVarRefs(Vars); 12272 Vars.clear(); 12273 for (unsigned i = 0; i != NumVars; ++i) 12274 Vars.push_back(Record.readSubExpr()); 12275 C->setPrivates(Vars); 12276 Vars.clear(); 12277 for (unsigned i = 0; i != NumVars; ++i) 12278 Vars.push_back(Record.readSubExpr()); 12279 C->setLHSExprs(Vars); 12280 Vars.clear(); 12281 for (unsigned i = 0; i != NumVars; ++i) 12282 Vars.push_back(Record.readSubExpr()); 12283 C->setRHSExprs(Vars); 12284 Vars.clear(); 12285 for (unsigned i = 0; i != NumVars; ++i) 12286 Vars.push_back(Record.readSubExpr()); 12287 C->setReductionOps(Vars); 12288 if (C->getModifier() == OMPC_REDUCTION_inscan) { 12289 Vars.clear(); 12290 for (unsigned i = 0; i != NumVars; ++i) 12291 Vars.push_back(Record.readSubExpr()); 12292 C->setInscanCopyOps(Vars); 12293 Vars.clear(); 12294 for (unsigned i = 0; i != NumVars; ++i) 12295 Vars.push_back(Record.readSubExpr()); 12296 C->setInscanCopyArrayTemps(Vars); 12297 Vars.clear(); 12298 for (unsigned i = 0; i != NumVars; ++i) 12299 Vars.push_back(Record.readSubExpr()); 12300 C->setInscanCopyArrayElems(Vars); 12301 } 12302 } 12303 12304 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) { 12305 VisitOMPClauseWithPostUpdate(C); 12306 C->setLParenLoc(Record.readSourceLocation()); 12307 C->setColonLoc(Record.readSourceLocation()); 12308 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12309 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12310 C->setQualifierLoc(NNSL); 12311 C->setNameInfo(DNI); 12312 12313 unsigned NumVars = C->varlist_size(); 12314 SmallVector<Expr *, 16> Vars; 12315 Vars.reserve(NumVars); 12316 for (unsigned I = 0; I != NumVars; ++I) 12317 Vars.push_back(Record.readSubExpr()); 12318 C->setVarRefs(Vars); 12319 Vars.clear(); 12320 for (unsigned I = 0; I != NumVars; ++I) 12321 Vars.push_back(Record.readSubExpr()); 12322 C->setPrivates(Vars); 12323 Vars.clear(); 12324 for (unsigned I = 0; I != NumVars; ++I) 12325 Vars.push_back(Record.readSubExpr()); 12326 C->setLHSExprs(Vars); 12327 Vars.clear(); 12328 for (unsigned I = 0; I != NumVars; ++I) 12329 Vars.push_back(Record.readSubExpr()); 12330 C->setRHSExprs(Vars); 12331 Vars.clear(); 12332 for (unsigned I = 0; I != NumVars; ++I) 12333 Vars.push_back(Record.readSubExpr()); 12334 C->setReductionOps(Vars); 12335 } 12336 12337 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) { 12338 VisitOMPClauseWithPostUpdate(C); 12339 C->setLParenLoc(Record.readSourceLocation()); 12340 C->setColonLoc(Record.readSourceLocation()); 12341 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12342 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12343 C->setQualifierLoc(NNSL); 12344 C->setNameInfo(DNI); 12345 12346 unsigned NumVars = C->varlist_size(); 12347 SmallVector<Expr *, 16> Vars; 12348 Vars.reserve(NumVars); 12349 for (unsigned I = 0; I != NumVars; ++I) 12350 Vars.push_back(Record.readSubExpr()); 12351 C->setVarRefs(Vars); 12352 Vars.clear(); 12353 for (unsigned I = 0; I != NumVars; ++I) 12354 Vars.push_back(Record.readSubExpr()); 12355 C->setPrivates(Vars); 12356 Vars.clear(); 12357 for (unsigned I = 0; I != NumVars; ++I) 12358 Vars.push_back(Record.readSubExpr()); 12359 C->setLHSExprs(Vars); 12360 Vars.clear(); 12361 for (unsigned I = 0; I != NumVars; ++I) 12362 Vars.push_back(Record.readSubExpr()); 12363 C->setRHSExprs(Vars); 12364 Vars.clear(); 12365 for (unsigned I = 0; I != NumVars; ++I) 12366 Vars.push_back(Record.readSubExpr()); 12367 C->setReductionOps(Vars); 12368 Vars.clear(); 12369 for (unsigned I = 0; I != NumVars; ++I) 12370 Vars.push_back(Record.readSubExpr()); 12371 C->setTaskgroupDescriptors(Vars); 12372 } 12373 12374 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) { 12375 VisitOMPClauseWithPostUpdate(C); 12376 C->setLParenLoc(Record.readSourceLocation()); 12377 C->setColonLoc(Record.readSourceLocation()); 12378 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt())); 12379 C->setModifierLoc(Record.readSourceLocation()); 12380 unsigned NumVars = C->varlist_size(); 12381 SmallVector<Expr *, 16> Vars; 12382 Vars.reserve(NumVars); 12383 for (unsigned i = 0; i != NumVars; ++i) 12384 Vars.push_back(Record.readSubExpr()); 12385 C->setVarRefs(Vars); 12386 Vars.clear(); 12387 for (unsigned i = 0; i != NumVars; ++i) 12388 Vars.push_back(Record.readSubExpr()); 12389 C->setPrivates(Vars); 12390 Vars.clear(); 12391 for (unsigned i = 0; i != NumVars; ++i) 12392 Vars.push_back(Record.readSubExpr()); 12393 C->setInits(Vars); 12394 Vars.clear(); 12395 for (unsigned i = 0; i != NumVars; ++i) 12396 Vars.push_back(Record.readSubExpr()); 12397 C->setUpdates(Vars); 12398 Vars.clear(); 12399 for (unsigned i = 0; i != NumVars; ++i) 12400 Vars.push_back(Record.readSubExpr()); 12401 C->setFinals(Vars); 12402 C->setStep(Record.readSubExpr()); 12403 C->setCalcStep(Record.readSubExpr()); 12404 Vars.clear(); 12405 for (unsigned I = 0; I != NumVars + 1; ++I) 12406 Vars.push_back(Record.readSubExpr()); 12407 C->setUsedExprs(Vars); 12408 } 12409 12410 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) { 12411 C->setLParenLoc(Record.readSourceLocation()); 12412 C->setColonLoc(Record.readSourceLocation()); 12413 unsigned NumVars = C->varlist_size(); 12414 SmallVector<Expr *, 16> Vars; 12415 Vars.reserve(NumVars); 12416 for (unsigned i = 0; i != NumVars; ++i) 12417 Vars.push_back(Record.readSubExpr()); 12418 C->setVarRefs(Vars); 12419 C->setAlignment(Record.readSubExpr()); 12420 } 12421 12422 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) { 12423 C->setLParenLoc(Record.readSourceLocation()); 12424 unsigned NumVars = C->varlist_size(); 12425 SmallVector<Expr *, 16> Exprs; 12426 Exprs.reserve(NumVars); 12427 for (unsigned i = 0; i != NumVars; ++i) 12428 Exprs.push_back(Record.readSubExpr()); 12429 C->setVarRefs(Exprs); 12430 Exprs.clear(); 12431 for (unsigned i = 0; i != NumVars; ++i) 12432 Exprs.push_back(Record.readSubExpr()); 12433 C->setSourceExprs(Exprs); 12434 Exprs.clear(); 12435 for (unsigned i = 0; i != NumVars; ++i) 12436 Exprs.push_back(Record.readSubExpr()); 12437 C->setDestinationExprs(Exprs); 12438 Exprs.clear(); 12439 for (unsigned i = 0; i != NumVars; ++i) 12440 Exprs.push_back(Record.readSubExpr()); 12441 C->setAssignmentOps(Exprs); 12442 } 12443 12444 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) { 12445 C->setLParenLoc(Record.readSourceLocation()); 12446 unsigned NumVars = C->varlist_size(); 12447 SmallVector<Expr *, 16> Exprs; 12448 Exprs.reserve(NumVars); 12449 for (unsigned i = 0; i != NumVars; ++i) 12450 Exprs.push_back(Record.readSubExpr()); 12451 C->setVarRefs(Exprs); 12452 Exprs.clear(); 12453 for (unsigned i = 0; i != NumVars; ++i) 12454 Exprs.push_back(Record.readSubExpr()); 12455 C->setSourceExprs(Exprs); 12456 Exprs.clear(); 12457 for (unsigned i = 0; i != NumVars; ++i) 12458 Exprs.push_back(Record.readSubExpr()); 12459 C->setDestinationExprs(Exprs); 12460 Exprs.clear(); 12461 for (unsigned i = 0; i != NumVars; ++i) 12462 Exprs.push_back(Record.readSubExpr()); 12463 C->setAssignmentOps(Exprs); 12464 } 12465 12466 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) { 12467 C->setLParenLoc(Record.readSourceLocation()); 12468 unsigned NumVars = C->varlist_size(); 12469 SmallVector<Expr *, 16> Vars; 12470 Vars.reserve(NumVars); 12471 for (unsigned i = 0; i != NumVars; ++i) 12472 Vars.push_back(Record.readSubExpr()); 12473 C->setVarRefs(Vars); 12474 } 12475 12476 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) { 12477 C->setDepobj(Record.readSubExpr()); 12478 C->setLParenLoc(Record.readSourceLocation()); 12479 } 12480 12481 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) { 12482 C->setLParenLoc(Record.readSourceLocation()); 12483 C->setModifier(Record.readSubExpr()); 12484 C->setDependencyKind( 12485 static_cast<OpenMPDependClauseKind>(Record.readInt())); 12486 C->setDependencyLoc(Record.readSourceLocation()); 12487 C->setColonLoc(Record.readSourceLocation()); 12488 unsigned NumVars = C->varlist_size(); 12489 SmallVector<Expr *, 16> Vars; 12490 Vars.reserve(NumVars); 12491 for (unsigned I = 0; I != NumVars; ++I) 12492 Vars.push_back(Record.readSubExpr()); 12493 C->setVarRefs(Vars); 12494 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I) 12495 C->setLoopData(I, Record.readSubExpr()); 12496 } 12497 12498 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) { 12499 VisitOMPClauseWithPreInit(C); 12500 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>()); 12501 C->setDevice(Record.readSubExpr()); 12502 C->setModifierLoc(Record.readSourceLocation()); 12503 C->setLParenLoc(Record.readSourceLocation()); 12504 } 12505 12506 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) { 12507 C->setLParenLoc(Record.readSourceLocation()); 12508 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) { 12509 C->setMapTypeModifier( 12510 I, static_cast<OpenMPMapModifierKind>(Record.readInt())); 12511 C->setMapTypeModifierLoc(I, Record.readSourceLocation()); 12512 } 12513 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12514 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12515 C->setMapType( 12516 static_cast<OpenMPMapClauseKind>(Record.readInt())); 12517 C->setMapLoc(Record.readSourceLocation()); 12518 C->setColonLoc(Record.readSourceLocation()); 12519 auto NumVars = C->varlist_size(); 12520 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12521 auto TotalLists = C->getTotalComponentListNum(); 12522 auto TotalComponents = C->getTotalComponentsNum(); 12523 12524 SmallVector<Expr *, 16> Vars; 12525 Vars.reserve(NumVars); 12526 for (unsigned i = 0; i != NumVars; ++i) 12527 Vars.push_back(Record.readExpr()); 12528 C->setVarRefs(Vars); 12529 12530 SmallVector<Expr *, 16> UDMappers; 12531 UDMappers.reserve(NumVars); 12532 for (unsigned I = 0; I < NumVars; ++I) 12533 UDMappers.push_back(Record.readExpr()); 12534 C->setUDMapperRefs(UDMappers); 12535 12536 SmallVector<ValueDecl *, 16> Decls; 12537 Decls.reserve(UniqueDecls); 12538 for (unsigned i = 0; i < UniqueDecls; ++i) 12539 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12540 C->setUniqueDecls(Decls); 12541 12542 SmallVector<unsigned, 16> ListsPerDecl; 12543 ListsPerDecl.reserve(UniqueDecls); 12544 for (unsigned i = 0; i < UniqueDecls; ++i) 12545 ListsPerDecl.push_back(Record.readInt()); 12546 C->setDeclNumLists(ListsPerDecl); 12547 12548 SmallVector<unsigned, 32> ListSizes; 12549 ListSizes.reserve(TotalLists); 12550 for (unsigned i = 0; i < TotalLists; ++i) 12551 ListSizes.push_back(Record.readInt()); 12552 C->setComponentListSizes(ListSizes); 12553 12554 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12555 Components.reserve(TotalComponents); 12556 for (unsigned i = 0; i < TotalComponents; ++i) { 12557 Expr *AssociatedExprPr = Record.readExpr(); 12558 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12559 Components.emplace_back(AssociatedExprPr, AssociatedDecl, 12560 /*IsNonContiguous=*/false); 12561 } 12562 C->setComponents(Components, ListSizes); 12563 } 12564 12565 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) { 12566 C->setLParenLoc(Record.readSourceLocation()); 12567 C->setColonLoc(Record.readSourceLocation()); 12568 C->setAllocator(Record.readSubExpr()); 12569 unsigned NumVars = C->varlist_size(); 12570 SmallVector<Expr *, 16> Vars; 12571 Vars.reserve(NumVars); 12572 for (unsigned i = 0; i != NumVars; ++i) 12573 Vars.push_back(Record.readSubExpr()); 12574 C->setVarRefs(Vars); 12575 } 12576 12577 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) { 12578 VisitOMPClauseWithPreInit(C); 12579 C->setNumTeams(Record.readSubExpr()); 12580 C->setLParenLoc(Record.readSourceLocation()); 12581 } 12582 12583 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) { 12584 VisitOMPClauseWithPreInit(C); 12585 C->setThreadLimit(Record.readSubExpr()); 12586 C->setLParenLoc(Record.readSourceLocation()); 12587 } 12588 12589 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) { 12590 VisitOMPClauseWithPreInit(C); 12591 C->setPriority(Record.readSubExpr()); 12592 C->setLParenLoc(Record.readSourceLocation()); 12593 } 12594 12595 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) { 12596 VisitOMPClauseWithPreInit(C); 12597 C->setGrainsize(Record.readSubExpr()); 12598 C->setLParenLoc(Record.readSourceLocation()); 12599 } 12600 12601 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) { 12602 VisitOMPClauseWithPreInit(C); 12603 C->setNumTasks(Record.readSubExpr()); 12604 C->setLParenLoc(Record.readSourceLocation()); 12605 } 12606 12607 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) { 12608 C->setHint(Record.readSubExpr()); 12609 C->setLParenLoc(Record.readSourceLocation()); 12610 } 12611 12612 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) { 12613 VisitOMPClauseWithPreInit(C); 12614 C->setDistScheduleKind( 12615 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt())); 12616 C->setChunkSize(Record.readSubExpr()); 12617 C->setLParenLoc(Record.readSourceLocation()); 12618 C->setDistScheduleKindLoc(Record.readSourceLocation()); 12619 C->setCommaLoc(Record.readSourceLocation()); 12620 } 12621 12622 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) { 12623 C->setDefaultmapKind( 12624 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt())); 12625 C->setDefaultmapModifier( 12626 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt())); 12627 C->setLParenLoc(Record.readSourceLocation()); 12628 C->setDefaultmapModifierLoc(Record.readSourceLocation()); 12629 C->setDefaultmapKindLoc(Record.readSourceLocation()); 12630 } 12631 12632 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) { 12633 C->setLParenLoc(Record.readSourceLocation()); 12634 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) { 12635 C->setMotionModifier( 12636 I, static_cast<OpenMPMotionModifierKind>(Record.readInt())); 12637 C->setMotionModifierLoc(I, Record.readSourceLocation()); 12638 } 12639 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12640 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12641 C->setColonLoc(Record.readSourceLocation()); 12642 auto NumVars = C->varlist_size(); 12643 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12644 auto TotalLists = C->getTotalComponentListNum(); 12645 auto TotalComponents = C->getTotalComponentsNum(); 12646 12647 SmallVector<Expr *, 16> Vars; 12648 Vars.reserve(NumVars); 12649 for (unsigned i = 0; i != NumVars; ++i) 12650 Vars.push_back(Record.readSubExpr()); 12651 C->setVarRefs(Vars); 12652 12653 SmallVector<Expr *, 16> UDMappers; 12654 UDMappers.reserve(NumVars); 12655 for (unsigned I = 0; I < NumVars; ++I) 12656 UDMappers.push_back(Record.readSubExpr()); 12657 C->setUDMapperRefs(UDMappers); 12658 12659 SmallVector<ValueDecl *, 16> Decls; 12660 Decls.reserve(UniqueDecls); 12661 for (unsigned i = 0; i < UniqueDecls; ++i) 12662 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12663 C->setUniqueDecls(Decls); 12664 12665 SmallVector<unsigned, 16> ListsPerDecl; 12666 ListsPerDecl.reserve(UniqueDecls); 12667 for (unsigned i = 0; i < UniqueDecls; ++i) 12668 ListsPerDecl.push_back(Record.readInt()); 12669 C->setDeclNumLists(ListsPerDecl); 12670 12671 SmallVector<unsigned, 32> ListSizes; 12672 ListSizes.reserve(TotalLists); 12673 for (unsigned i = 0; i < TotalLists; ++i) 12674 ListSizes.push_back(Record.readInt()); 12675 C->setComponentListSizes(ListSizes); 12676 12677 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12678 Components.reserve(TotalComponents); 12679 for (unsigned i = 0; i < TotalComponents; ++i) { 12680 Expr *AssociatedExprPr = Record.readSubExpr(); 12681 bool IsNonContiguous = Record.readBool(); 12682 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12683 Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous); 12684 } 12685 C->setComponents(Components, ListSizes); 12686 } 12687 12688 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) { 12689 C->setLParenLoc(Record.readSourceLocation()); 12690 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) { 12691 C->setMotionModifier( 12692 I, static_cast<OpenMPMotionModifierKind>(Record.readInt())); 12693 C->setMotionModifierLoc(I, Record.readSourceLocation()); 12694 } 12695 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12696 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12697 C->setColonLoc(Record.readSourceLocation()); 12698 auto NumVars = C->varlist_size(); 12699 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12700 auto TotalLists = C->getTotalComponentListNum(); 12701 auto TotalComponents = C->getTotalComponentsNum(); 12702 12703 SmallVector<Expr *, 16> Vars; 12704 Vars.reserve(NumVars); 12705 for (unsigned i = 0; i != NumVars; ++i) 12706 Vars.push_back(Record.readSubExpr()); 12707 C->setVarRefs(Vars); 12708 12709 SmallVector<Expr *, 16> UDMappers; 12710 UDMappers.reserve(NumVars); 12711 for (unsigned I = 0; I < NumVars; ++I) 12712 UDMappers.push_back(Record.readSubExpr()); 12713 C->setUDMapperRefs(UDMappers); 12714 12715 SmallVector<ValueDecl *, 16> Decls; 12716 Decls.reserve(UniqueDecls); 12717 for (unsigned i = 0; i < UniqueDecls; ++i) 12718 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12719 C->setUniqueDecls(Decls); 12720 12721 SmallVector<unsigned, 16> ListsPerDecl; 12722 ListsPerDecl.reserve(UniqueDecls); 12723 for (unsigned i = 0; i < UniqueDecls; ++i) 12724 ListsPerDecl.push_back(Record.readInt()); 12725 C->setDeclNumLists(ListsPerDecl); 12726 12727 SmallVector<unsigned, 32> ListSizes; 12728 ListSizes.reserve(TotalLists); 12729 for (unsigned i = 0; i < TotalLists; ++i) 12730 ListSizes.push_back(Record.readInt()); 12731 C->setComponentListSizes(ListSizes); 12732 12733 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12734 Components.reserve(TotalComponents); 12735 for (unsigned i = 0; i < TotalComponents; ++i) { 12736 Expr *AssociatedExprPr = Record.readSubExpr(); 12737 bool IsNonContiguous = Record.readBool(); 12738 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12739 Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous); 12740 } 12741 C->setComponents(Components, ListSizes); 12742 } 12743 12744 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) { 12745 C->setLParenLoc(Record.readSourceLocation()); 12746 auto NumVars = C->varlist_size(); 12747 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12748 auto TotalLists = C->getTotalComponentListNum(); 12749 auto TotalComponents = C->getTotalComponentsNum(); 12750 12751 SmallVector<Expr *, 16> Vars; 12752 Vars.reserve(NumVars); 12753 for (unsigned i = 0; i != NumVars; ++i) 12754 Vars.push_back(Record.readSubExpr()); 12755 C->setVarRefs(Vars); 12756 Vars.clear(); 12757 for (unsigned i = 0; i != NumVars; ++i) 12758 Vars.push_back(Record.readSubExpr()); 12759 C->setPrivateCopies(Vars); 12760 Vars.clear(); 12761 for (unsigned i = 0; i != NumVars; ++i) 12762 Vars.push_back(Record.readSubExpr()); 12763 C->setInits(Vars); 12764 12765 SmallVector<ValueDecl *, 16> Decls; 12766 Decls.reserve(UniqueDecls); 12767 for (unsigned i = 0; i < UniqueDecls; ++i) 12768 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12769 C->setUniqueDecls(Decls); 12770 12771 SmallVector<unsigned, 16> ListsPerDecl; 12772 ListsPerDecl.reserve(UniqueDecls); 12773 for (unsigned i = 0; i < UniqueDecls; ++i) 12774 ListsPerDecl.push_back(Record.readInt()); 12775 C->setDeclNumLists(ListsPerDecl); 12776 12777 SmallVector<unsigned, 32> ListSizes; 12778 ListSizes.reserve(TotalLists); 12779 for (unsigned i = 0; i < TotalLists; ++i) 12780 ListSizes.push_back(Record.readInt()); 12781 C->setComponentListSizes(ListSizes); 12782 12783 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12784 Components.reserve(TotalComponents); 12785 for (unsigned i = 0; i < TotalComponents; ++i) { 12786 auto *AssociatedExprPr = Record.readSubExpr(); 12787 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12788 Components.emplace_back(AssociatedExprPr, AssociatedDecl, 12789 /*IsNonContiguous=*/false); 12790 } 12791 C->setComponents(Components, ListSizes); 12792 } 12793 12794 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) { 12795 C->setLParenLoc(Record.readSourceLocation()); 12796 auto NumVars = C->varlist_size(); 12797 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12798 auto TotalLists = C->getTotalComponentListNum(); 12799 auto TotalComponents = C->getTotalComponentsNum(); 12800 12801 SmallVector<Expr *, 16> Vars; 12802 Vars.reserve(NumVars); 12803 for (unsigned i = 0; i != NumVars; ++i) 12804 Vars.push_back(Record.readSubExpr()); 12805 C->setVarRefs(Vars); 12806 12807 SmallVector<ValueDecl *, 16> Decls; 12808 Decls.reserve(UniqueDecls); 12809 for (unsigned i = 0; i < UniqueDecls; ++i) 12810 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12811 C->setUniqueDecls(Decls); 12812 12813 SmallVector<unsigned, 16> ListsPerDecl; 12814 ListsPerDecl.reserve(UniqueDecls); 12815 for (unsigned i = 0; i < UniqueDecls; ++i) 12816 ListsPerDecl.push_back(Record.readInt()); 12817 C->setDeclNumLists(ListsPerDecl); 12818 12819 SmallVector<unsigned, 32> ListSizes; 12820 ListSizes.reserve(TotalLists); 12821 for (unsigned i = 0; i < TotalLists; ++i) 12822 ListSizes.push_back(Record.readInt()); 12823 C->setComponentListSizes(ListSizes); 12824 12825 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12826 Components.reserve(TotalComponents); 12827 for (unsigned i = 0; i < TotalComponents; ++i) { 12828 Expr *AssociatedExpr = Record.readSubExpr(); 12829 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12830 Components.emplace_back(AssociatedExpr, AssociatedDecl, 12831 /*IsNonContiguous*/ false); 12832 } 12833 C->setComponents(Components, ListSizes); 12834 } 12835 12836 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) { 12837 C->setLParenLoc(Record.readSourceLocation()); 12838 auto NumVars = C->varlist_size(); 12839 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12840 auto TotalLists = C->getTotalComponentListNum(); 12841 auto TotalComponents = C->getTotalComponentsNum(); 12842 12843 SmallVector<Expr *, 16> Vars; 12844 Vars.reserve(NumVars); 12845 for (unsigned i = 0; i != NumVars; ++i) 12846 Vars.push_back(Record.readSubExpr()); 12847 C->setVarRefs(Vars); 12848 Vars.clear(); 12849 12850 SmallVector<ValueDecl *, 16> Decls; 12851 Decls.reserve(UniqueDecls); 12852 for (unsigned i = 0; i < UniqueDecls; ++i) 12853 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12854 C->setUniqueDecls(Decls); 12855 12856 SmallVector<unsigned, 16> ListsPerDecl; 12857 ListsPerDecl.reserve(UniqueDecls); 12858 for (unsigned i = 0; i < UniqueDecls; ++i) 12859 ListsPerDecl.push_back(Record.readInt()); 12860 C->setDeclNumLists(ListsPerDecl); 12861 12862 SmallVector<unsigned, 32> ListSizes; 12863 ListSizes.reserve(TotalLists); 12864 for (unsigned i = 0; i < TotalLists; ++i) 12865 ListSizes.push_back(Record.readInt()); 12866 C->setComponentListSizes(ListSizes); 12867 12868 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12869 Components.reserve(TotalComponents); 12870 for (unsigned i = 0; i < TotalComponents; ++i) { 12871 Expr *AssociatedExpr = Record.readSubExpr(); 12872 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12873 Components.emplace_back(AssociatedExpr, AssociatedDecl, 12874 /*IsNonContiguous=*/false); 12875 } 12876 C->setComponents(Components, ListSizes); 12877 } 12878 12879 void OMPClauseReader::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *C) { 12880 C->setLParenLoc(Record.readSourceLocation()); 12881 auto NumVars = C->varlist_size(); 12882 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12883 auto TotalLists = C->getTotalComponentListNum(); 12884 auto TotalComponents = C->getTotalComponentsNum(); 12885 12886 SmallVector<Expr *, 16> Vars; 12887 Vars.reserve(NumVars); 12888 for (unsigned I = 0; I != NumVars; ++I) 12889 Vars.push_back(Record.readSubExpr()); 12890 C->setVarRefs(Vars); 12891 Vars.clear(); 12892 12893 SmallVector<ValueDecl *, 16> Decls; 12894 Decls.reserve(UniqueDecls); 12895 for (unsigned I = 0; I < UniqueDecls; ++I) 12896 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12897 C->setUniqueDecls(Decls); 12898 12899 SmallVector<unsigned, 16> ListsPerDecl; 12900 ListsPerDecl.reserve(UniqueDecls); 12901 for (unsigned I = 0; I < UniqueDecls; ++I) 12902 ListsPerDecl.push_back(Record.readInt()); 12903 C->setDeclNumLists(ListsPerDecl); 12904 12905 SmallVector<unsigned, 32> ListSizes; 12906 ListSizes.reserve(TotalLists); 12907 for (unsigned i = 0; i < TotalLists; ++i) 12908 ListSizes.push_back(Record.readInt()); 12909 C->setComponentListSizes(ListSizes); 12910 12911 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12912 Components.reserve(TotalComponents); 12913 for (unsigned I = 0; I < TotalComponents; ++I) { 12914 Expr *AssociatedExpr = Record.readSubExpr(); 12915 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12916 Components.emplace_back(AssociatedExpr, AssociatedDecl, 12917 /*IsNonContiguous=*/false); 12918 } 12919 C->setComponents(Components, ListSizes); 12920 } 12921 12922 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) { 12923 C->setLParenLoc(Record.readSourceLocation()); 12924 unsigned NumVars = C->varlist_size(); 12925 SmallVector<Expr *, 16> Vars; 12926 Vars.reserve(NumVars); 12927 for (unsigned i = 0; i != NumVars; ++i) 12928 Vars.push_back(Record.readSubExpr()); 12929 C->setVarRefs(Vars); 12930 Vars.clear(); 12931 Vars.reserve(NumVars); 12932 for (unsigned i = 0; i != NumVars; ++i) 12933 Vars.push_back(Record.readSubExpr()); 12934 C->setPrivateRefs(Vars); 12935 } 12936 12937 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) { 12938 C->setLParenLoc(Record.readSourceLocation()); 12939 unsigned NumVars = C->varlist_size(); 12940 SmallVector<Expr *, 16> Vars; 12941 Vars.reserve(NumVars); 12942 for (unsigned i = 0; i != NumVars; ++i) 12943 Vars.push_back(Record.readSubExpr()); 12944 C->setVarRefs(Vars); 12945 } 12946 12947 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) { 12948 C->setLParenLoc(Record.readSourceLocation()); 12949 unsigned NumVars = C->varlist_size(); 12950 SmallVector<Expr *, 16> Vars; 12951 Vars.reserve(NumVars); 12952 for (unsigned i = 0; i != NumVars; ++i) 12953 Vars.push_back(Record.readSubExpr()); 12954 C->setVarRefs(Vars); 12955 } 12956 12957 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) { 12958 C->setLParenLoc(Record.readSourceLocation()); 12959 unsigned NumOfAllocators = C->getNumberOfAllocators(); 12960 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data; 12961 Data.reserve(NumOfAllocators); 12962 for (unsigned I = 0; I != NumOfAllocators; ++I) { 12963 OMPUsesAllocatorsClause::Data &D = Data.emplace_back(); 12964 D.Allocator = Record.readSubExpr(); 12965 D.AllocatorTraits = Record.readSubExpr(); 12966 D.LParenLoc = Record.readSourceLocation(); 12967 D.RParenLoc = Record.readSourceLocation(); 12968 } 12969 C->setAllocatorsData(Data); 12970 } 12971 12972 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) { 12973 C->setLParenLoc(Record.readSourceLocation()); 12974 C->setModifier(Record.readSubExpr()); 12975 C->setColonLoc(Record.readSourceLocation()); 12976 unsigned NumOfLocators = C->varlist_size(); 12977 SmallVector<Expr *, 4> Locators; 12978 Locators.reserve(NumOfLocators); 12979 for (unsigned I = 0; I != NumOfLocators; ++I) 12980 Locators.push_back(Record.readSubExpr()); 12981 C->setVarRefs(Locators); 12982 } 12983 12984 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) { 12985 C->setKind(Record.readEnum<OpenMPOrderClauseKind>()); 12986 C->setLParenLoc(Record.readSourceLocation()); 12987 C->setKindKwLoc(Record.readSourceLocation()); 12988 } 12989 12990 void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) { 12991 VisitOMPClauseWithPreInit(C); 12992 C->setThreadID(Record.readSubExpr()); 12993 C->setLParenLoc(Record.readSourceLocation()); 12994 } 12995 12996 void OMPClauseReader::VisitOMPBindClause(OMPBindClause *C) { 12997 C->setBindKind(Record.readEnum<OpenMPBindClauseKind>()); 12998 C->setLParenLoc(Record.readSourceLocation()); 12999 C->setBindKindLoc(Record.readSourceLocation()); 13000 } 13001 13002 void OMPClauseReader::VisitOMPAlignClause(OMPAlignClause *C) { 13003 C->setAlignment(Record.readExpr()); 13004 C->setLParenLoc(Record.readSourceLocation()); 13005 } 13006 13007 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() { 13008 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo(); 13009 TI.Sets.resize(readUInt32()); 13010 for (auto &Set : TI.Sets) { 13011 Set.Kind = readEnum<llvm::omp::TraitSet>(); 13012 Set.Selectors.resize(readUInt32()); 13013 for (auto &Selector : Set.Selectors) { 13014 Selector.Kind = readEnum<llvm::omp::TraitSelector>(); 13015 Selector.ScoreOrCondition = nullptr; 13016 if (readBool()) 13017 Selector.ScoreOrCondition = readExprRef(); 13018 Selector.Properties.resize(readUInt32()); 13019 for (auto &Property : Selector.Properties) 13020 Property.Kind = readEnum<llvm::omp::TraitProperty>(); 13021 } 13022 } 13023 return &TI; 13024 } 13025 13026 void ASTRecordReader::readOMPChildren(OMPChildren *Data) { 13027 if (!Data) 13028 return; 13029 if (Reader->ReadingKind == ASTReader::Read_Stmt) { 13030 // Skip NumClauses, NumChildren and HasAssociatedStmt fields. 13031 skipInts(3); 13032 } 13033 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses()); 13034 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I) 13035 Clauses[I] = readOMPClause(); 13036 Data->setClauses(Clauses); 13037 if (Data->hasAssociatedStmt()) 13038 Data->setAssociatedStmt(readStmt()); 13039 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I) 13040 Data->getChildren()[I] = readStmt(); 13041 } 13042