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