1 //===- ASTReader.cpp - AST File Reader ------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the ASTReader class, which reads AST files. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "clang/Basic/OpenMPKinds.h" 14 #include "clang/Serialization/ASTRecordReader.h" 15 #include "ASTCommon.h" 16 #include "ASTReaderInternals.h" 17 #include "clang/AST/AbstractTypeReader.h" 18 #include "clang/AST/ASTConsumer.h" 19 #include "clang/AST/ASTContext.h" 20 #include "clang/AST/ASTMutationListener.h" 21 #include "clang/AST/ASTUnresolvedSet.h" 22 #include "clang/AST/Decl.h" 23 #include "clang/AST/DeclBase.h" 24 #include "clang/AST/DeclCXX.h" 25 #include "clang/AST/DeclFriend.h" 26 #include "clang/AST/DeclGroup.h" 27 #include "clang/AST/DeclObjC.h" 28 #include "clang/AST/DeclTemplate.h" 29 #include "clang/AST/DeclarationName.h" 30 #include "clang/AST/Expr.h" 31 #include "clang/AST/ExprCXX.h" 32 #include "clang/AST/ExternalASTSource.h" 33 #include "clang/AST/NestedNameSpecifier.h" 34 #include "clang/AST/OpenMPClause.h" 35 #include "clang/AST/ODRHash.h" 36 #include "clang/AST/RawCommentList.h" 37 #include "clang/AST/TemplateBase.h" 38 #include "clang/AST/TemplateName.h" 39 #include "clang/AST/Type.h" 40 #include "clang/AST/TypeLoc.h" 41 #include "clang/AST/TypeLocVisitor.h" 42 #include "clang/AST/UnresolvedSet.h" 43 #include "clang/Basic/CommentOptions.h" 44 #include "clang/Basic/Diagnostic.h" 45 #include "clang/Basic/DiagnosticOptions.h" 46 #include "clang/Basic/ExceptionSpecificationType.h" 47 #include "clang/Basic/FileManager.h" 48 #include "clang/Basic/FileSystemOptions.h" 49 #include "clang/Basic/IdentifierTable.h" 50 #include "clang/Basic/LLVM.h" 51 #include "clang/Basic/LangOptions.h" 52 #include "clang/Basic/Module.h" 53 #include "clang/Basic/ObjCRuntime.h" 54 #include "clang/Basic/OperatorKinds.h" 55 #include "clang/Basic/PragmaKinds.h" 56 #include "clang/Basic/Sanitizers.h" 57 #include "clang/Basic/SourceLocation.h" 58 #include "clang/Basic/SourceManager.h" 59 #include "clang/Basic/SourceManagerInternals.h" 60 #include "clang/Basic/Specifiers.h" 61 #include "clang/Basic/TargetInfo.h" 62 #include "clang/Basic/TargetOptions.h" 63 #include "clang/Basic/TokenKinds.h" 64 #include "clang/Basic/Version.h" 65 #include "clang/Lex/HeaderSearch.h" 66 #include "clang/Lex/HeaderSearchOptions.h" 67 #include "clang/Lex/MacroInfo.h" 68 #include "clang/Lex/ModuleMap.h" 69 #include "clang/Lex/PreprocessingRecord.h" 70 #include "clang/Lex/Preprocessor.h" 71 #include "clang/Lex/PreprocessorOptions.h" 72 #include "clang/Lex/Token.h" 73 #include "clang/Sema/ObjCMethodList.h" 74 #include "clang/Sema/Scope.h" 75 #include "clang/Sema/Sema.h" 76 #include "clang/Sema/Weak.h" 77 #include "clang/Serialization/ASTBitCodes.h" 78 #include "clang/Serialization/ASTDeserializationListener.h" 79 #include "clang/Serialization/ContinuousRangeMap.h" 80 #include "clang/Serialization/GlobalModuleIndex.h" 81 #include "clang/Serialization/InMemoryModuleCache.h" 82 #include "clang/Serialization/ModuleFile.h" 83 #include "clang/Serialization/ModuleFileExtension.h" 84 #include "clang/Serialization/ModuleManager.h" 85 #include "clang/Serialization/PCHContainerOperations.h" 86 #include "clang/Serialization/SerializationDiagnostic.h" 87 #include "llvm/ADT/APFloat.h" 88 #include "llvm/ADT/APInt.h" 89 #include "llvm/ADT/APSInt.h" 90 #include "llvm/ADT/ArrayRef.h" 91 #include "llvm/ADT/DenseMap.h" 92 #include "llvm/ADT/FloatingPointMode.h" 93 #include "llvm/ADT/FoldingSet.h" 94 #include "llvm/ADT/Hashing.h" 95 #include "llvm/ADT/IntrusiveRefCntPtr.h" 96 #include "llvm/ADT/None.h" 97 #include "llvm/ADT/Optional.h" 98 #include "llvm/ADT/STLExtras.h" 99 #include "llvm/ADT/ScopeExit.h" 100 #include "llvm/ADT/SmallPtrSet.h" 101 #include "llvm/ADT/SmallString.h" 102 #include "llvm/ADT/SmallVector.h" 103 #include "llvm/ADT/StringExtras.h" 104 #include "llvm/ADT/StringMap.h" 105 #include "llvm/ADT/StringRef.h" 106 #include "llvm/ADT/Triple.h" 107 #include "llvm/ADT/iterator_range.h" 108 #include "llvm/Bitstream/BitstreamReader.h" 109 #include "llvm/Support/Casting.h" 110 #include "llvm/Support/Compiler.h" 111 #include "llvm/Support/Compression.h" 112 #include "llvm/Support/DJB.h" 113 #include "llvm/Support/Endian.h" 114 #include "llvm/Support/Error.h" 115 #include "llvm/Support/ErrorHandling.h" 116 #include "llvm/Support/FileSystem.h" 117 #include "llvm/Support/MemoryBuffer.h" 118 #include "llvm/Support/Path.h" 119 #include "llvm/Support/SaveAndRestore.h" 120 #include "llvm/Support/Timer.h" 121 #include "llvm/Support/VersionTuple.h" 122 #include "llvm/Support/raw_ostream.h" 123 #include <algorithm> 124 #include <cassert> 125 #include <cstddef> 126 #include <cstdint> 127 #include <cstdio> 128 #include <ctime> 129 #include <iterator> 130 #include <limits> 131 #include <map> 132 #include <memory> 133 #include <string> 134 #include <system_error> 135 #include <tuple> 136 #include <utility> 137 #include <vector> 138 139 using namespace clang; 140 using namespace clang::serialization; 141 using namespace clang::serialization::reader; 142 using llvm::BitstreamCursor; 143 using llvm::RoundingMode; 144 145 //===----------------------------------------------------------------------===// 146 // ChainedASTReaderListener implementation 147 //===----------------------------------------------------------------------===// 148 149 bool 150 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) { 151 return First->ReadFullVersionInformation(FullVersion) || 152 Second->ReadFullVersionInformation(FullVersion); 153 } 154 155 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) { 156 First->ReadModuleName(ModuleName); 157 Second->ReadModuleName(ModuleName); 158 } 159 160 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) { 161 First->ReadModuleMapFile(ModuleMapPath); 162 Second->ReadModuleMapFile(ModuleMapPath); 163 } 164 165 bool 166 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts, 167 bool Complain, 168 bool AllowCompatibleDifferences) { 169 return First->ReadLanguageOptions(LangOpts, Complain, 170 AllowCompatibleDifferences) || 171 Second->ReadLanguageOptions(LangOpts, Complain, 172 AllowCompatibleDifferences); 173 } 174 175 bool ChainedASTReaderListener::ReadTargetOptions( 176 const TargetOptions &TargetOpts, bool Complain, 177 bool AllowCompatibleDifferences) { 178 return First->ReadTargetOptions(TargetOpts, Complain, 179 AllowCompatibleDifferences) || 180 Second->ReadTargetOptions(TargetOpts, Complain, 181 AllowCompatibleDifferences); 182 } 183 184 bool ChainedASTReaderListener::ReadDiagnosticOptions( 185 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 186 return First->ReadDiagnosticOptions(DiagOpts, Complain) || 187 Second->ReadDiagnosticOptions(DiagOpts, Complain); 188 } 189 190 bool 191 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts, 192 bool Complain) { 193 return First->ReadFileSystemOptions(FSOpts, Complain) || 194 Second->ReadFileSystemOptions(FSOpts, Complain); 195 } 196 197 bool ChainedASTReaderListener::ReadHeaderSearchOptions( 198 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath, 199 bool Complain) { 200 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 201 Complain) || 202 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 203 Complain); 204 } 205 206 bool ChainedASTReaderListener::ReadPreprocessorOptions( 207 const PreprocessorOptions &PPOpts, bool Complain, 208 std::string &SuggestedPredefines) { 209 return First->ReadPreprocessorOptions(PPOpts, Complain, 210 SuggestedPredefines) || 211 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines); 212 } 213 214 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M, 215 unsigned Value) { 216 First->ReadCounter(M, Value); 217 Second->ReadCounter(M, Value); 218 } 219 220 bool ChainedASTReaderListener::needsInputFileVisitation() { 221 return First->needsInputFileVisitation() || 222 Second->needsInputFileVisitation(); 223 } 224 225 bool ChainedASTReaderListener::needsSystemInputFileVisitation() { 226 return First->needsSystemInputFileVisitation() || 227 Second->needsSystemInputFileVisitation(); 228 } 229 230 void ChainedASTReaderListener::visitModuleFile(StringRef Filename, 231 ModuleKind Kind) { 232 First->visitModuleFile(Filename, Kind); 233 Second->visitModuleFile(Filename, Kind); 234 } 235 236 bool ChainedASTReaderListener::visitInputFile(StringRef Filename, 237 bool isSystem, 238 bool isOverridden, 239 bool isExplicitModule) { 240 bool Continue = false; 241 if (First->needsInputFileVisitation() && 242 (!isSystem || First->needsSystemInputFileVisitation())) 243 Continue |= First->visitInputFile(Filename, isSystem, isOverridden, 244 isExplicitModule); 245 if (Second->needsInputFileVisitation() && 246 (!isSystem || Second->needsSystemInputFileVisitation())) 247 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden, 248 isExplicitModule); 249 return Continue; 250 } 251 252 void ChainedASTReaderListener::readModuleFileExtension( 253 const ModuleFileExtensionMetadata &Metadata) { 254 First->readModuleFileExtension(Metadata); 255 Second->readModuleFileExtension(Metadata); 256 } 257 258 //===----------------------------------------------------------------------===// 259 // PCH validator implementation 260 //===----------------------------------------------------------------------===// 261 262 ASTReaderListener::~ASTReaderListener() = default; 263 264 /// Compare the given set of language options against an existing set of 265 /// language options. 266 /// 267 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 268 /// \param AllowCompatibleDifferences If true, differences between compatible 269 /// language options will be permitted. 270 /// 271 /// \returns true if the languagae options mis-match, false otherwise. 272 static bool checkLanguageOptions(const LangOptions &LangOpts, 273 const LangOptions &ExistingLangOpts, 274 DiagnosticsEngine *Diags, 275 bool AllowCompatibleDifferences = true) { 276 #define LANGOPT(Name, Bits, Default, Description) \ 277 if (ExistingLangOpts.Name != LangOpts.Name) { \ 278 if (Diags) \ 279 Diags->Report(diag::err_pch_langopt_mismatch) \ 280 << Description << LangOpts.Name << ExistingLangOpts.Name; \ 281 return true; \ 282 } 283 284 #define VALUE_LANGOPT(Name, Bits, Default, Description) \ 285 if (ExistingLangOpts.Name != LangOpts.Name) { \ 286 if (Diags) \ 287 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 288 << Description; \ 289 return true; \ 290 } 291 292 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 293 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \ 294 if (Diags) \ 295 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 296 << Description; \ 297 return true; \ 298 } 299 300 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \ 301 if (!AllowCompatibleDifferences) \ 302 LANGOPT(Name, Bits, Default, Description) 303 304 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \ 305 if (!AllowCompatibleDifferences) \ 306 ENUM_LANGOPT(Name, Bits, Default, Description) 307 308 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \ 309 if (!AllowCompatibleDifferences) \ 310 VALUE_LANGOPT(Name, Bits, Default, Description) 311 312 #define BENIGN_LANGOPT(Name, Bits, Default, Description) 313 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) 314 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description) 315 #include "clang/Basic/LangOptions.def" 316 317 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) { 318 if (Diags) 319 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features"; 320 return true; 321 } 322 323 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) { 324 if (Diags) 325 Diags->Report(diag::err_pch_langopt_value_mismatch) 326 << "target Objective-C runtime"; 327 return true; 328 } 329 330 if (ExistingLangOpts.CommentOpts.BlockCommandNames != 331 LangOpts.CommentOpts.BlockCommandNames) { 332 if (Diags) 333 Diags->Report(diag::err_pch_langopt_value_mismatch) 334 << "block command names"; 335 return true; 336 } 337 338 // Sanitizer feature mismatches are treated as compatible differences. If 339 // compatible differences aren't allowed, we still only want to check for 340 // mismatches of non-modular sanitizers (the only ones which can affect AST 341 // generation). 342 if (!AllowCompatibleDifferences) { 343 SanitizerMask ModularSanitizers = getPPTransparentSanitizers(); 344 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize; 345 SanitizerSet ImportedSanitizers = LangOpts.Sanitize; 346 ExistingSanitizers.clear(ModularSanitizers); 347 ImportedSanitizers.clear(ModularSanitizers); 348 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) { 349 const std::string Flag = "-fsanitize="; 350 if (Diags) { 351 #define SANITIZER(NAME, ID) \ 352 { \ 353 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \ 354 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \ 355 if (InExistingModule != InImportedModule) \ 356 Diags->Report(diag::err_pch_targetopt_feature_mismatch) \ 357 << InExistingModule << (Flag + NAME); \ 358 } 359 #include "clang/Basic/Sanitizers.def" 360 } 361 return true; 362 } 363 } 364 365 return false; 366 } 367 368 /// Compare the given set of target options against an existing set of 369 /// target options. 370 /// 371 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 372 /// 373 /// \returns true if the target options mis-match, false otherwise. 374 static bool checkTargetOptions(const TargetOptions &TargetOpts, 375 const TargetOptions &ExistingTargetOpts, 376 DiagnosticsEngine *Diags, 377 bool AllowCompatibleDifferences = true) { 378 #define CHECK_TARGET_OPT(Field, Name) \ 379 if (TargetOpts.Field != ExistingTargetOpts.Field) { \ 380 if (Diags) \ 381 Diags->Report(diag::err_pch_targetopt_mismatch) \ 382 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \ 383 return true; \ 384 } 385 386 // The triple and ABI must match exactly. 387 CHECK_TARGET_OPT(Triple, "target"); 388 CHECK_TARGET_OPT(ABI, "target ABI"); 389 390 // We can tolerate different CPUs in many cases, notably when one CPU 391 // supports a strict superset of another. When allowing compatible 392 // differences skip this check. 393 if (!AllowCompatibleDifferences) { 394 CHECK_TARGET_OPT(CPU, "target CPU"); 395 CHECK_TARGET_OPT(TuneCPU, "tune CPU"); 396 } 397 398 #undef CHECK_TARGET_OPT 399 400 // Compare feature sets. 401 SmallVector<StringRef, 4> ExistingFeatures( 402 ExistingTargetOpts.FeaturesAsWritten.begin(), 403 ExistingTargetOpts.FeaturesAsWritten.end()); 404 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(), 405 TargetOpts.FeaturesAsWritten.end()); 406 llvm::sort(ExistingFeatures); 407 llvm::sort(ReadFeatures); 408 409 // We compute the set difference in both directions explicitly so that we can 410 // diagnose the differences differently. 411 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures; 412 std::set_difference( 413 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(), 414 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures)); 415 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(), 416 ExistingFeatures.begin(), ExistingFeatures.end(), 417 std::back_inserter(UnmatchedReadFeatures)); 418 419 // If we are allowing compatible differences and the read feature set is 420 // a strict subset of the existing feature set, there is nothing to diagnose. 421 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty()) 422 return false; 423 424 if (Diags) { 425 for (StringRef Feature : UnmatchedReadFeatures) 426 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 427 << /* is-existing-feature */ false << Feature; 428 for (StringRef Feature : UnmatchedExistingFeatures) 429 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 430 << /* is-existing-feature */ true << Feature; 431 } 432 433 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty(); 434 } 435 436 bool 437 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts, 438 bool Complain, 439 bool AllowCompatibleDifferences) { 440 const LangOptions &ExistingLangOpts = PP.getLangOpts(); 441 return checkLanguageOptions(LangOpts, ExistingLangOpts, 442 Complain ? &Reader.Diags : nullptr, 443 AllowCompatibleDifferences); 444 } 445 446 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts, 447 bool Complain, 448 bool AllowCompatibleDifferences) { 449 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts(); 450 return checkTargetOptions(TargetOpts, ExistingTargetOpts, 451 Complain ? &Reader.Diags : nullptr, 452 AllowCompatibleDifferences); 453 } 454 455 namespace { 456 457 using MacroDefinitionsMap = 458 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>; 459 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>; 460 461 } // namespace 462 463 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags, 464 DiagnosticsEngine &Diags, 465 bool Complain) { 466 using Level = DiagnosticsEngine::Level; 467 468 // Check current mappings for new -Werror mappings, and the stored mappings 469 // for cases that were explicitly mapped to *not* be errors that are now 470 // errors because of options like -Werror. 471 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags }; 472 473 for (DiagnosticsEngine *MappingSource : MappingSources) { 474 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) { 475 diag::kind DiagID = DiagIDMappingPair.first; 476 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation()); 477 if (CurLevel < DiagnosticsEngine::Error) 478 continue; // not significant 479 Level StoredLevel = 480 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation()); 481 if (StoredLevel < DiagnosticsEngine::Error) { 482 if (Complain) 483 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" + 484 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str(); 485 return true; 486 } 487 } 488 } 489 490 return false; 491 } 492 493 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) { 494 diag::Severity Ext = Diags.getExtensionHandlingBehavior(); 495 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors()) 496 return true; 497 return Ext >= diag::Severity::Error; 498 } 499 500 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags, 501 DiagnosticsEngine &Diags, 502 bool IsSystem, bool Complain) { 503 // Top-level options 504 if (IsSystem) { 505 if (Diags.getSuppressSystemWarnings()) 506 return false; 507 // If -Wsystem-headers was not enabled before, be conservative 508 if (StoredDiags.getSuppressSystemWarnings()) { 509 if (Complain) 510 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers"; 511 return true; 512 } 513 } 514 515 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) { 516 if (Complain) 517 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror"; 518 return true; 519 } 520 521 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() && 522 !StoredDiags.getEnableAllWarnings()) { 523 if (Complain) 524 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror"; 525 return true; 526 } 527 528 if (isExtHandlingFromDiagsError(Diags) && 529 !isExtHandlingFromDiagsError(StoredDiags)) { 530 if (Complain) 531 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors"; 532 return true; 533 } 534 535 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain); 536 } 537 538 /// Return the top import module if it is implicit, nullptr otherwise. 539 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr, 540 Preprocessor &PP) { 541 // If the original import came from a file explicitly generated by the user, 542 // don't check the diagnostic mappings. 543 // FIXME: currently this is approximated by checking whether this is not a 544 // module import of an implicitly-loaded module file. 545 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in 546 // the transitive closure of its imports, since unrelated modules cannot be 547 // imported until after this module finishes validation. 548 ModuleFile *TopImport = &*ModuleMgr.rbegin(); 549 while (!TopImport->ImportedBy.empty()) 550 TopImport = TopImport->ImportedBy[0]; 551 if (TopImport->Kind != MK_ImplicitModule) 552 return nullptr; 553 554 StringRef ModuleName = TopImport->ModuleName; 555 assert(!ModuleName.empty() && "diagnostic options read before module name"); 556 557 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName); 558 assert(M && "missing module"); 559 return M; 560 } 561 562 bool PCHValidator::ReadDiagnosticOptions( 563 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 564 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics(); 565 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs()); 566 IntrusiveRefCntPtr<DiagnosticsEngine> Diags( 567 new DiagnosticsEngine(DiagIDs, DiagOpts.get())); 568 // This should never fail, because we would have processed these options 569 // before writing them to an ASTFile. 570 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false); 571 572 ModuleManager &ModuleMgr = Reader.getModuleManager(); 573 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then"); 574 575 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP); 576 if (!TopM) 577 return false; 578 579 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that 580 // contains the union of their flags. 581 return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem, 582 Complain); 583 } 584 585 /// Collect the macro definitions provided by the given preprocessor 586 /// options. 587 static void 588 collectMacroDefinitions(const PreprocessorOptions &PPOpts, 589 MacroDefinitionsMap &Macros, 590 SmallVectorImpl<StringRef> *MacroNames = nullptr) { 591 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) { 592 StringRef Macro = PPOpts.Macros[I].first; 593 bool IsUndef = PPOpts.Macros[I].second; 594 595 std::pair<StringRef, StringRef> MacroPair = Macro.split('='); 596 StringRef MacroName = MacroPair.first; 597 StringRef MacroBody = MacroPair.second; 598 599 // For an #undef'd macro, we only care about the name. 600 if (IsUndef) { 601 if (MacroNames && !Macros.count(MacroName)) 602 MacroNames->push_back(MacroName); 603 604 Macros[MacroName] = std::make_pair("", true); 605 continue; 606 } 607 608 // For a #define'd macro, figure out the actual definition. 609 if (MacroName.size() == Macro.size()) 610 MacroBody = "1"; 611 else { 612 // Note: GCC drops anything following an end-of-line character. 613 StringRef::size_type End = MacroBody.find_first_of("\n\r"); 614 MacroBody = MacroBody.substr(0, End); 615 } 616 617 if (MacroNames && !Macros.count(MacroName)) 618 MacroNames->push_back(MacroName); 619 Macros[MacroName] = std::make_pair(MacroBody, false); 620 } 621 } 622 623 /// Check the preprocessor options deserialized from the control block 624 /// against the preprocessor options in an existing preprocessor. 625 /// 626 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 627 /// \param Validate If true, validate preprocessor options. If false, allow 628 /// macros defined by \p ExistingPPOpts to override those defined by 629 /// \p PPOpts in SuggestedPredefines. 630 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts, 631 const PreprocessorOptions &ExistingPPOpts, 632 DiagnosticsEngine *Diags, 633 FileManager &FileMgr, 634 std::string &SuggestedPredefines, 635 const LangOptions &LangOpts, 636 bool Validate = true) { 637 // Check macro definitions. 638 MacroDefinitionsMap ASTFileMacros; 639 collectMacroDefinitions(PPOpts, ASTFileMacros); 640 MacroDefinitionsMap ExistingMacros; 641 SmallVector<StringRef, 4> ExistingMacroNames; 642 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames); 643 644 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) { 645 // Dig out the macro definition in the existing preprocessor options. 646 StringRef MacroName = ExistingMacroNames[I]; 647 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName]; 648 649 // Check whether we know anything about this macro name or not. 650 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known = 651 ASTFileMacros.find(MacroName); 652 if (!Validate || Known == ASTFileMacros.end()) { 653 // FIXME: Check whether this identifier was referenced anywhere in the 654 // AST file. If so, we should reject the AST file. Unfortunately, this 655 // information isn't in the control block. What shall we do about it? 656 657 if (Existing.second) { 658 SuggestedPredefines += "#undef "; 659 SuggestedPredefines += MacroName.str(); 660 SuggestedPredefines += '\n'; 661 } else { 662 SuggestedPredefines += "#define "; 663 SuggestedPredefines += MacroName.str(); 664 SuggestedPredefines += ' '; 665 SuggestedPredefines += Existing.first.str(); 666 SuggestedPredefines += '\n'; 667 } 668 continue; 669 } 670 671 // If the macro was defined in one but undef'd in the other, we have a 672 // conflict. 673 if (Existing.second != Known->second.second) { 674 if (Diags) { 675 Diags->Report(diag::err_pch_macro_def_undef) 676 << MacroName << Known->second.second; 677 } 678 return true; 679 } 680 681 // If the macro was #undef'd in both, or if the macro bodies are identical, 682 // it's fine. 683 if (Existing.second || Existing.first == Known->second.first) 684 continue; 685 686 // The macro bodies differ; complain. 687 if (Diags) { 688 Diags->Report(diag::err_pch_macro_def_conflict) 689 << MacroName << Known->second.first << Existing.first; 690 } 691 return true; 692 } 693 694 // Check whether we're using predefines. 695 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) { 696 if (Diags) { 697 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines; 698 } 699 return true; 700 } 701 702 // Detailed record is important since it is used for the module cache hash. 703 if (LangOpts.Modules && 704 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) { 705 if (Diags) { 706 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord; 707 } 708 return true; 709 } 710 711 // Compute the #include and #include_macros lines we need. 712 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) { 713 StringRef File = ExistingPPOpts.Includes[I]; 714 715 if (!ExistingPPOpts.ImplicitPCHInclude.empty() && 716 !ExistingPPOpts.PCHThroughHeader.empty()) { 717 // In case the through header is an include, we must add all the includes 718 // to the predefines so the start point can be determined. 719 SuggestedPredefines += "#include \""; 720 SuggestedPredefines += File; 721 SuggestedPredefines += "\"\n"; 722 continue; 723 } 724 725 if (File == ExistingPPOpts.ImplicitPCHInclude) 726 continue; 727 728 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File) 729 != PPOpts.Includes.end()) 730 continue; 731 732 SuggestedPredefines += "#include \""; 733 SuggestedPredefines += File; 734 SuggestedPredefines += "\"\n"; 735 } 736 737 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) { 738 StringRef File = ExistingPPOpts.MacroIncludes[I]; 739 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(), 740 File) 741 != PPOpts.MacroIncludes.end()) 742 continue; 743 744 SuggestedPredefines += "#__include_macros \""; 745 SuggestedPredefines += File; 746 SuggestedPredefines += "\"\n##\n"; 747 } 748 749 return false; 750 } 751 752 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 753 bool Complain, 754 std::string &SuggestedPredefines) { 755 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts(); 756 757 return checkPreprocessorOptions(PPOpts, ExistingPPOpts, 758 Complain? &Reader.Diags : nullptr, 759 PP.getFileManager(), 760 SuggestedPredefines, 761 PP.getLangOpts()); 762 } 763 764 bool SimpleASTReaderListener::ReadPreprocessorOptions( 765 const PreprocessorOptions &PPOpts, 766 bool Complain, 767 std::string &SuggestedPredefines) { 768 return checkPreprocessorOptions(PPOpts, 769 PP.getPreprocessorOpts(), 770 nullptr, 771 PP.getFileManager(), 772 SuggestedPredefines, 773 PP.getLangOpts(), 774 false); 775 } 776 777 /// Check the header search options deserialized from the control block 778 /// against the header search options in an existing preprocessor. 779 /// 780 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 781 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 782 StringRef SpecificModuleCachePath, 783 StringRef ExistingModuleCachePath, 784 DiagnosticsEngine *Diags, 785 const LangOptions &LangOpts) { 786 if (LangOpts.Modules) { 787 if (SpecificModuleCachePath != ExistingModuleCachePath) { 788 if (Diags) 789 Diags->Report(diag::err_pch_modulecache_mismatch) 790 << SpecificModuleCachePath << ExistingModuleCachePath; 791 return true; 792 } 793 } 794 795 return false; 796 } 797 798 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 799 StringRef SpecificModuleCachePath, 800 bool Complain) { 801 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 802 PP.getHeaderSearchInfo().getModuleCachePath(), 803 Complain ? &Reader.Diags : nullptr, 804 PP.getLangOpts()); 805 } 806 807 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) { 808 PP.setCounterValue(Value); 809 } 810 811 //===----------------------------------------------------------------------===// 812 // AST reader implementation 813 //===----------------------------------------------------------------------===// 814 815 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener, 816 bool TakeOwnership) { 817 DeserializationListener = Listener; 818 OwnsDeserializationListener = TakeOwnership; 819 } 820 821 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) { 822 return serialization::ComputeHash(Sel); 823 } 824 825 std::pair<unsigned, unsigned> 826 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 827 using namespace llvm::support; 828 829 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 830 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 831 return std::make_pair(KeyLen, DataLen); 832 } 833 834 ASTSelectorLookupTrait::internal_key_type 835 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) { 836 using namespace llvm::support; 837 838 SelectorTable &SelTable = Reader.getContext().Selectors; 839 unsigned N = endian::readNext<uint16_t, little, unaligned>(d); 840 IdentifierInfo *FirstII = Reader.getLocalIdentifier( 841 F, endian::readNext<uint32_t, little, unaligned>(d)); 842 if (N == 0) 843 return SelTable.getNullarySelector(FirstII); 844 else if (N == 1) 845 return SelTable.getUnarySelector(FirstII); 846 847 SmallVector<IdentifierInfo *, 16> Args; 848 Args.push_back(FirstII); 849 for (unsigned I = 1; I != N; ++I) 850 Args.push_back(Reader.getLocalIdentifier( 851 F, endian::readNext<uint32_t, little, unaligned>(d))); 852 853 return SelTable.getSelector(N, Args.data()); 854 } 855 856 ASTSelectorLookupTrait::data_type 857 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d, 858 unsigned DataLen) { 859 using namespace llvm::support; 860 861 data_type Result; 862 863 Result.ID = Reader.getGlobalSelectorID( 864 F, endian::readNext<uint32_t, little, unaligned>(d)); 865 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d); 866 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d); 867 Result.InstanceBits = FullInstanceBits & 0x3; 868 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1; 869 Result.FactoryBits = FullFactoryBits & 0x3; 870 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1; 871 unsigned NumInstanceMethods = FullInstanceBits >> 3; 872 unsigned NumFactoryMethods = FullFactoryBits >> 3; 873 874 // Load instance methods 875 for (unsigned I = 0; I != NumInstanceMethods; ++I) { 876 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 877 F, endian::readNext<uint32_t, little, unaligned>(d))) 878 Result.Instance.push_back(Method); 879 } 880 881 // Load factory methods 882 for (unsigned I = 0; I != NumFactoryMethods; ++I) { 883 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 884 F, endian::readNext<uint32_t, little, unaligned>(d))) 885 Result.Factory.push_back(Method); 886 } 887 888 return Result; 889 } 890 891 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) { 892 return llvm::djbHash(a); 893 } 894 895 std::pair<unsigned, unsigned> 896 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) { 897 using namespace llvm::support; 898 899 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 900 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 901 return std::make_pair(KeyLen, DataLen); 902 } 903 904 ASTIdentifierLookupTraitBase::internal_key_type 905 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) { 906 assert(n >= 2 && d[n-1] == '\0'); 907 return StringRef((const char*) d, n-1); 908 } 909 910 /// Whether the given identifier is "interesting". 911 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II, 912 bool IsModule) { 913 return II.hadMacroDefinition() || II.isPoisoned() || 914 (!IsModule && II.getObjCOrBuiltinID()) || 915 II.hasRevertedTokenIDToIdentifier() || 916 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) && 917 II.getFETokenInfo()); 918 } 919 920 static bool readBit(unsigned &Bits) { 921 bool Value = Bits & 0x1; 922 Bits >>= 1; 923 return Value; 924 } 925 926 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) { 927 using namespace llvm::support; 928 929 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 930 return Reader.getGlobalIdentifierID(F, RawID >> 1); 931 } 932 933 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) { 934 if (!II.isFromAST()) { 935 II.setIsFromAST(); 936 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr; 937 if (isInterestingIdentifier(Reader, II, IsModule)) 938 II.setChangedSinceDeserialization(); 939 } 940 } 941 942 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k, 943 const unsigned char* d, 944 unsigned DataLen) { 945 using namespace llvm::support; 946 947 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 948 bool IsInteresting = RawID & 0x01; 949 950 // Wipe out the "is interesting" bit. 951 RawID = RawID >> 1; 952 953 // Build the IdentifierInfo and link the identifier ID with it. 954 IdentifierInfo *II = KnownII; 955 if (!II) { 956 II = &Reader.getIdentifierTable().getOwn(k); 957 KnownII = II; 958 } 959 markIdentifierFromAST(Reader, *II); 960 Reader.markIdentifierUpToDate(II); 961 962 IdentID ID = Reader.getGlobalIdentifierID(F, RawID); 963 if (!IsInteresting) { 964 // For uninteresting identifiers, there's nothing else to do. Just notify 965 // the reader that we've finished loading this identifier. 966 Reader.SetIdentifierInfo(ID, II); 967 return II; 968 } 969 970 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d); 971 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d); 972 bool CPlusPlusOperatorKeyword = readBit(Bits); 973 bool HasRevertedTokenIDToIdentifier = readBit(Bits); 974 bool Poisoned = readBit(Bits); 975 bool ExtensionToken = readBit(Bits); 976 bool HadMacroDefinition = readBit(Bits); 977 978 assert(Bits == 0 && "Extra bits in the identifier?"); 979 DataLen -= 8; 980 981 // Set or check the various bits in the IdentifierInfo structure. 982 // Token IDs are read-only. 983 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier) 984 II->revertTokenIDToIdentifier(); 985 if (!F.isModule()) 986 II->setObjCOrBuiltinID(ObjCOrBuiltinID); 987 assert(II->isExtensionToken() == ExtensionToken && 988 "Incorrect extension token flag"); 989 (void)ExtensionToken; 990 if (Poisoned) 991 II->setIsPoisoned(true); 992 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword && 993 "Incorrect C++ operator keyword flag"); 994 (void)CPlusPlusOperatorKeyword; 995 996 // If this identifier is a macro, deserialize the macro 997 // definition. 998 if (HadMacroDefinition) { 999 uint32_t MacroDirectivesOffset = 1000 endian::readNext<uint32_t, little, unaligned>(d); 1001 DataLen -= 4; 1002 1003 Reader.addPendingMacro(II, &F, MacroDirectivesOffset); 1004 } 1005 1006 Reader.SetIdentifierInfo(ID, II); 1007 1008 // Read all of the declarations visible at global scope with this 1009 // name. 1010 if (DataLen > 0) { 1011 SmallVector<uint32_t, 4> DeclIDs; 1012 for (; DataLen > 0; DataLen -= 4) 1013 DeclIDs.push_back(Reader.getGlobalDeclID( 1014 F, endian::readNext<uint32_t, little, unaligned>(d))); 1015 Reader.SetGloballyVisibleDecls(II, DeclIDs); 1016 } 1017 1018 return II; 1019 } 1020 1021 DeclarationNameKey::DeclarationNameKey(DeclarationName Name) 1022 : Kind(Name.getNameKind()) { 1023 switch (Kind) { 1024 case DeclarationName::Identifier: 1025 Data = (uint64_t)Name.getAsIdentifierInfo(); 1026 break; 1027 case DeclarationName::ObjCZeroArgSelector: 1028 case DeclarationName::ObjCOneArgSelector: 1029 case DeclarationName::ObjCMultiArgSelector: 1030 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr(); 1031 break; 1032 case DeclarationName::CXXOperatorName: 1033 Data = Name.getCXXOverloadedOperator(); 1034 break; 1035 case DeclarationName::CXXLiteralOperatorName: 1036 Data = (uint64_t)Name.getCXXLiteralIdentifier(); 1037 break; 1038 case DeclarationName::CXXDeductionGuideName: 1039 Data = (uint64_t)Name.getCXXDeductionGuideTemplate() 1040 ->getDeclName().getAsIdentifierInfo(); 1041 break; 1042 case DeclarationName::CXXConstructorName: 1043 case DeclarationName::CXXDestructorName: 1044 case DeclarationName::CXXConversionFunctionName: 1045 case DeclarationName::CXXUsingDirective: 1046 Data = 0; 1047 break; 1048 } 1049 } 1050 1051 unsigned DeclarationNameKey::getHash() const { 1052 llvm::FoldingSetNodeID ID; 1053 ID.AddInteger(Kind); 1054 1055 switch (Kind) { 1056 case DeclarationName::Identifier: 1057 case DeclarationName::CXXLiteralOperatorName: 1058 case DeclarationName::CXXDeductionGuideName: 1059 ID.AddString(((IdentifierInfo*)Data)->getName()); 1060 break; 1061 case DeclarationName::ObjCZeroArgSelector: 1062 case DeclarationName::ObjCOneArgSelector: 1063 case DeclarationName::ObjCMultiArgSelector: 1064 ID.AddInteger(serialization::ComputeHash(Selector(Data))); 1065 break; 1066 case DeclarationName::CXXOperatorName: 1067 ID.AddInteger((OverloadedOperatorKind)Data); 1068 break; 1069 case DeclarationName::CXXConstructorName: 1070 case DeclarationName::CXXDestructorName: 1071 case DeclarationName::CXXConversionFunctionName: 1072 case DeclarationName::CXXUsingDirective: 1073 break; 1074 } 1075 1076 return ID.ComputeHash(); 1077 } 1078 1079 ModuleFile * 1080 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) { 1081 using namespace llvm::support; 1082 1083 uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d); 1084 return Reader.getLocalModuleFile(F, ModuleFileID); 1085 } 1086 1087 std::pair<unsigned, unsigned> 1088 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) { 1089 using namespace llvm::support; 1090 1091 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 1092 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 1093 return std::make_pair(KeyLen, DataLen); 1094 } 1095 1096 ASTDeclContextNameLookupTrait::internal_key_type 1097 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) { 1098 using namespace llvm::support; 1099 1100 auto Kind = (DeclarationName::NameKind)*d++; 1101 uint64_t Data; 1102 switch (Kind) { 1103 case DeclarationName::Identifier: 1104 case DeclarationName::CXXLiteralOperatorName: 1105 case DeclarationName::CXXDeductionGuideName: 1106 Data = (uint64_t)Reader.getLocalIdentifier( 1107 F, endian::readNext<uint32_t, little, unaligned>(d)); 1108 break; 1109 case DeclarationName::ObjCZeroArgSelector: 1110 case DeclarationName::ObjCOneArgSelector: 1111 case DeclarationName::ObjCMultiArgSelector: 1112 Data = 1113 (uint64_t)Reader.getLocalSelector( 1114 F, endian::readNext<uint32_t, little, unaligned>( 1115 d)).getAsOpaquePtr(); 1116 break; 1117 case DeclarationName::CXXOperatorName: 1118 Data = *d++; // OverloadedOperatorKind 1119 break; 1120 case DeclarationName::CXXConstructorName: 1121 case DeclarationName::CXXDestructorName: 1122 case DeclarationName::CXXConversionFunctionName: 1123 case DeclarationName::CXXUsingDirective: 1124 Data = 0; 1125 break; 1126 } 1127 1128 return DeclarationNameKey(Kind, Data); 1129 } 1130 1131 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type, 1132 const unsigned char *d, 1133 unsigned DataLen, 1134 data_type_builder &Val) { 1135 using namespace llvm::support; 1136 1137 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) { 1138 uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d); 1139 Val.insert(Reader.getGlobalDeclID(F, LocalID)); 1140 } 1141 } 1142 1143 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M, 1144 BitstreamCursor &Cursor, 1145 uint64_t Offset, 1146 DeclContext *DC) { 1147 assert(Offset != 0); 1148 1149 SavedStreamPosition SavedPosition(Cursor); 1150 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1151 Error(std::move(Err)); 1152 return true; 1153 } 1154 1155 RecordData Record; 1156 StringRef Blob; 1157 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1158 if (!MaybeCode) { 1159 Error(MaybeCode.takeError()); 1160 return true; 1161 } 1162 unsigned Code = MaybeCode.get(); 1163 1164 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1165 if (!MaybeRecCode) { 1166 Error(MaybeRecCode.takeError()); 1167 return true; 1168 } 1169 unsigned RecCode = MaybeRecCode.get(); 1170 if (RecCode != DECL_CONTEXT_LEXICAL) { 1171 Error("Expected lexical block"); 1172 return true; 1173 } 1174 1175 assert(!isa<TranslationUnitDecl>(DC) && 1176 "expected a TU_UPDATE_LEXICAL record for TU"); 1177 // If we are handling a C++ class template instantiation, we can see multiple 1178 // lexical updates for the same record. It's important that we select only one 1179 // of them, so that field numbering works properly. Just pick the first one we 1180 // see. 1181 auto &Lex = LexicalDecls[DC]; 1182 if (!Lex.first) { 1183 Lex = std::make_pair( 1184 &M, llvm::makeArrayRef( 1185 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 1186 Blob.data()), 1187 Blob.size() / 4)); 1188 } 1189 DC->setHasExternalLexicalStorage(true); 1190 return false; 1191 } 1192 1193 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M, 1194 BitstreamCursor &Cursor, 1195 uint64_t Offset, 1196 DeclID ID) { 1197 assert(Offset != 0); 1198 1199 SavedStreamPosition SavedPosition(Cursor); 1200 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1201 Error(std::move(Err)); 1202 return true; 1203 } 1204 1205 RecordData Record; 1206 StringRef Blob; 1207 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1208 if (!MaybeCode) { 1209 Error(MaybeCode.takeError()); 1210 return true; 1211 } 1212 unsigned Code = MaybeCode.get(); 1213 1214 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1215 if (!MaybeRecCode) { 1216 Error(MaybeRecCode.takeError()); 1217 return true; 1218 } 1219 unsigned RecCode = MaybeRecCode.get(); 1220 if (RecCode != DECL_CONTEXT_VISIBLE) { 1221 Error("Expected visible lookup table block"); 1222 return true; 1223 } 1224 1225 // We can't safely determine the primary context yet, so delay attaching the 1226 // lookup table until we're done with recursive deserialization. 1227 auto *Data = (const unsigned char*)Blob.data(); 1228 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data}); 1229 return false; 1230 } 1231 1232 void ASTReader::Error(StringRef Msg) const { 1233 Error(diag::err_fe_pch_malformed, Msg); 1234 if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() && 1235 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) { 1236 Diag(diag::note_module_cache_path) 1237 << PP.getHeaderSearchInfo().getModuleCachePath(); 1238 } 1239 } 1240 1241 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2, 1242 StringRef Arg3) const { 1243 if (Diags.isDiagnosticInFlight()) 1244 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3); 1245 else 1246 Diag(DiagID) << Arg1 << Arg2 << Arg3; 1247 } 1248 1249 void ASTReader::Error(llvm::Error &&Err) const { 1250 Error(toString(std::move(Err))); 1251 } 1252 1253 //===----------------------------------------------------------------------===// 1254 // Source Manager Deserialization 1255 //===----------------------------------------------------------------------===// 1256 1257 /// Read the line table in the source manager block. 1258 /// \returns true if there was an error. 1259 bool ASTReader::ParseLineTable(ModuleFile &F, 1260 const RecordData &Record) { 1261 unsigned Idx = 0; 1262 LineTableInfo &LineTable = SourceMgr.getLineTable(); 1263 1264 // Parse the file names 1265 std::map<int, int> FileIDs; 1266 FileIDs[-1] = -1; // For unspecified filenames. 1267 for (unsigned I = 0; Record[Idx]; ++I) { 1268 // Extract the file name 1269 auto Filename = ReadPath(F, Record, Idx); 1270 FileIDs[I] = LineTable.getLineTableFilenameID(Filename); 1271 } 1272 ++Idx; 1273 1274 // Parse the line entries 1275 std::vector<LineEntry> Entries; 1276 while (Idx < Record.size()) { 1277 int FID = Record[Idx++]; 1278 assert(FID >= 0 && "Serialized line entries for non-local file."); 1279 // Remap FileID from 1-based old view. 1280 FID += F.SLocEntryBaseID - 1; 1281 1282 // Extract the line entries 1283 unsigned NumEntries = Record[Idx++]; 1284 assert(NumEntries && "no line entries for file ID"); 1285 Entries.clear(); 1286 Entries.reserve(NumEntries); 1287 for (unsigned I = 0; I != NumEntries; ++I) { 1288 unsigned FileOffset = Record[Idx++]; 1289 unsigned LineNo = Record[Idx++]; 1290 int FilenameID = FileIDs[Record[Idx++]]; 1291 SrcMgr::CharacteristicKind FileKind 1292 = (SrcMgr::CharacteristicKind)Record[Idx++]; 1293 unsigned IncludeOffset = Record[Idx++]; 1294 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, 1295 FileKind, IncludeOffset)); 1296 } 1297 LineTable.AddEntry(FileID::get(FID), Entries); 1298 } 1299 1300 return false; 1301 } 1302 1303 /// Read a source manager block 1304 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) { 1305 using namespace SrcMgr; 1306 1307 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor; 1308 1309 // Set the source-location entry cursor to the current position in 1310 // the stream. This cursor will be used to read the contents of the 1311 // source manager block initially, and then lazily read 1312 // source-location entries as needed. 1313 SLocEntryCursor = F.Stream; 1314 1315 // The stream itself is going to skip over the source manager block. 1316 if (llvm::Error Err = F.Stream.SkipBlock()) { 1317 Error(std::move(Err)); 1318 return true; 1319 } 1320 1321 // Enter the source manager block. 1322 if (llvm::Error Err = 1323 SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) { 1324 Error(std::move(Err)); 1325 return true; 1326 } 1327 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo(); 1328 1329 RecordData Record; 1330 while (true) { 1331 Expected<llvm::BitstreamEntry> MaybeE = 1332 SLocEntryCursor.advanceSkippingSubblocks(); 1333 if (!MaybeE) { 1334 Error(MaybeE.takeError()); 1335 return true; 1336 } 1337 llvm::BitstreamEntry E = MaybeE.get(); 1338 1339 switch (E.Kind) { 1340 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1341 case llvm::BitstreamEntry::Error: 1342 Error("malformed block record in AST file"); 1343 return true; 1344 case llvm::BitstreamEntry::EndBlock: 1345 return false; 1346 case llvm::BitstreamEntry::Record: 1347 // The interesting case. 1348 break; 1349 } 1350 1351 // Read a record. 1352 Record.clear(); 1353 StringRef Blob; 1354 Expected<unsigned> MaybeRecord = 1355 SLocEntryCursor.readRecord(E.ID, Record, &Blob); 1356 if (!MaybeRecord) { 1357 Error(MaybeRecord.takeError()); 1358 return true; 1359 } 1360 switch (MaybeRecord.get()) { 1361 default: // Default behavior: ignore. 1362 break; 1363 1364 case SM_SLOC_FILE_ENTRY: 1365 case SM_SLOC_BUFFER_ENTRY: 1366 case SM_SLOC_EXPANSION_ENTRY: 1367 // Once we hit one of the source location entries, we're done. 1368 return false; 1369 } 1370 } 1371 } 1372 1373 /// If a header file is not found at the path that we expect it to be 1374 /// and the PCH file was moved from its original location, try to resolve the 1375 /// file by assuming that header+PCH were moved together and the header is in 1376 /// the same place relative to the PCH. 1377 static std::string 1378 resolveFileRelativeToOriginalDir(const std::string &Filename, 1379 const std::string &OriginalDir, 1380 const std::string &CurrDir) { 1381 assert(OriginalDir != CurrDir && 1382 "No point trying to resolve the file if the PCH dir didn't change"); 1383 1384 using namespace llvm::sys; 1385 1386 SmallString<128> filePath(Filename); 1387 fs::make_absolute(filePath); 1388 assert(path::is_absolute(OriginalDir)); 1389 SmallString<128> currPCHPath(CurrDir); 1390 1391 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)), 1392 fileDirE = path::end(path::parent_path(filePath)); 1393 path::const_iterator origDirI = path::begin(OriginalDir), 1394 origDirE = path::end(OriginalDir); 1395 // Skip the common path components from filePath and OriginalDir. 1396 while (fileDirI != fileDirE && origDirI != origDirE && 1397 *fileDirI == *origDirI) { 1398 ++fileDirI; 1399 ++origDirI; 1400 } 1401 for (; origDirI != origDirE; ++origDirI) 1402 path::append(currPCHPath, ".."); 1403 path::append(currPCHPath, fileDirI, fileDirE); 1404 path::append(currPCHPath, path::filename(Filename)); 1405 return std::string(currPCHPath.str()); 1406 } 1407 1408 bool ASTReader::ReadSLocEntry(int ID) { 1409 if (ID == 0) 1410 return false; 1411 1412 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1413 Error("source location entry ID out-of-range for AST file"); 1414 return true; 1415 } 1416 1417 // Local helper to read the (possibly-compressed) buffer data following the 1418 // entry record. 1419 auto ReadBuffer = [this]( 1420 BitstreamCursor &SLocEntryCursor, 1421 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> { 1422 RecordData Record; 1423 StringRef Blob; 1424 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode(); 1425 if (!MaybeCode) { 1426 Error(MaybeCode.takeError()); 1427 return nullptr; 1428 } 1429 unsigned Code = MaybeCode.get(); 1430 1431 Expected<unsigned> MaybeRecCode = 1432 SLocEntryCursor.readRecord(Code, Record, &Blob); 1433 if (!MaybeRecCode) { 1434 Error(MaybeRecCode.takeError()); 1435 return nullptr; 1436 } 1437 unsigned RecCode = MaybeRecCode.get(); 1438 1439 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) { 1440 if (!llvm::zlib::isAvailable()) { 1441 Error("zlib is not available"); 1442 return nullptr; 1443 } 1444 SmallString<0> Uncompressed; 1445 if (llvm::Error E = 1446 llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) { 1447 Error("could not decompress embedded file contents: " + 1448 llvm::toString(std::move(E))); 1449 return nullptr; 1450 } 1451 return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name); 1452 } else if (RecCode == SM_SLOC_BUFFER_BLOB) { 1453 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true); 1454 } else { 1455 Error("AST record has invalid code"); 1456 return nullptr; 1457 } 1458 }; 1459 1460 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second; 1461 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit( 1462 F->SLocEntryOffsetsBase + 1463 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) { 1464 Error(std::move(Err)); 1465 return true; 1466 } 1467 1468 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor; 1469 unsigned BaseOffset = F->SLocEntryBaseOffset; 1470 1471 ++NumSLocEntriesRead; 1472 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance(); 1473 if (!MaybeEntry) { 1474 Error(MaybeEntry.takeError()); 1475 return true; 1476 } 1477 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1478 1479 if (Entry.Kind != llvm::BitstreamEntry::Record) { 1480 Error("incorrectly-formatted source location entry in AST file"); 1481 return true; 1482 } 1483 1484 RecordData Record; 1485 StringRef Blob; 1486 Expected<unsigned> MaybeSLOC = 1487 SLocEntryCursor.readRecord(Entry.ID, Record, &Blob); 1488 if (!MaybeSLOC) { 1489 Error(MaybeSLOC.takeError()); 1490 return true; 1491 } 1492 switch (MaybeSLOC.get()) { 1493 default: 1494 Error("incorrectly-formatted source location entry in AST file"); 1495 return true; 1496 1497 case SM_SLOC_FILE_ENTRY: { 1498 // We will detect whether a file changed and return 'Failure' for it, but 1499 // we will also try to fail gracefully by setting up the SLocEntry. 1500 unsigned InputID = Record[4]; 1501 InputFile IF = getInputFile(*F, InputID); 1502 Optional<FileEntryRef> File = IF.getFile(); 1503 bool OverriddenBuffer = IF.isOverridden(); 1504 1505 // Note that we only check if a File was returned. If it was out-of-date 1506 // we have complained but we will continue creating a FileID to recover 1507 // gracefully. 1508 if (!File) 1509 return true; 1510 1511 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1512 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) { 1513 // This is the module's main file. 1514 IncludeLoc = getImportLocation(F); 1515 } 1516 SrcMgr::CharacteristicKind 1517 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1518 FileID FID = SourceMgr.createFileID(*File, IncludeLoc, FileCharacter, ID, 1519 BaseOffset + Record[0]); 1520 SrcMgr::FileInfo &FileInfo = 1521 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()); 1522 FileInfo.NumCreatedFIDs = Record[5]; 1523 if (Record[3]) 1524 FileInfo.setHasLineDirectives(); 1525 1526 unsigned NumFileDecls = Record[7]; 1527 if (NumFileDecls && ContextObj) { 1528 const DeclID *FirstDecl = F->FileSortedDecls + Record[6]; 1529 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?"); 1530 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl, 1531 NumFileDecls)); 1532 } 1533 1534 const SrcMgr::ContentCache &ContentCache = 1535 SourceMgr.getOrCreateContentCache(*File, isSystem(FileCharacter)); 1536 if (OverriddenBuffer && !ContentCache.BufferOverridden && 1537 ContentCache.ContentsEntry == ContentCache.OrigEntry && 1538 !ContentCache.getBufferIfLoaded()) { 1539 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName()); 1540 if (!Buffer) 1541 return true; 1542 SourceMgr.overrideFileContents(*File, std::move(Buffer)); 1543 } 1544 1545 break; 1546 } 1547 1548 case SM_SLOC_BUFFER_ENTRY: { 1549 const char *Name = Blob.data(); 1550 unsigned Offset = Record[0]; 1551 SrcMgr::CharacteristicKind 1552 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1553 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1554 if (IncludeLoc.isInvalid() && F->isModule()) { 1555 IncludeLoc = getImportLocation(F); 1556 } 1557 1558 auto Buffer = ReadBuffer(SLocEntryCursor, Name); 1559 if (!Buffer) 1560 return true; 1561 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID, 1562 BaseOffset + Offset, IncludeLoc); 1563 break; 1564 } 1565 1566 case SM_SLOC_EXPANSION_ENTRY: { 1567 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]); 1568 SourceMgr.createExpansionLoc(SpellingLoc, 1569 ReadSourceLocation(*F, Record[2]), 1570 ReadSourceLocation(*F, Record[3]), 1571 Record[5], 1572 Record[4], 1573 ID, 1574 BaseOffset + Record[0]); 1575 break; 1576 } 1577 } 1578 1579 return false; 1580 } 1581 1582 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) { 1583 if (ID == 0) 1584 return std::make_pair(SourceLocation(), ""); 1585 1586 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1587 Error("source location entry ID out-of-range for AST file"); 1588 return std::make_pair(SourceLocation(), ""); 1589 } 1590 1591 // Find which module file this entry lands in. 1592 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second; 1593 if (!M->isModule()) 1594 return std::make_pair(SourceLocation(), ""); 1595 1596 // FIXME: Can we map this down to a particular submodule? That would be 1597 // ideal. 1598 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName)); 1599 } 1600 1601 /// Find the location where the module F is imported. 1602 SourceLocation ASTReader::getImportLocation(ModuleFile *F) { 1603 if (F->ImportLoc.isValid()) 1604 return F->ImportLoc; 1605 1606 // Otherwise we have a PCH. It's considered to be "imported" at the first 1607 // location of its includer. 1608 if (F->ImportedBy.empty() || !F->ImportedBy[0]) { 1609 // Main file is the importer. 1610 assert(SourceMgr.getMainFileID().isValid() && "missing main file"); 1611 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); 1612 } 1613 return F->ImportedBy[0]->FirstLoc; 1614 } 1615 1616 /// Enter a subblock of the specified BlockID with the specified cursor. Read 1617 /// the abbreviations that are at the top of the block and then leave the cursor 1618 /// pointing into the block. 1619 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID, 1620 uint64_t *StartOfBlockOffset) { 1621 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) { 1622 // FIXME this drops errors on the floor. 1623 consumeError(std::move(Err)); 1624 return true; 1625 } 1626 1627 if (StartOfBlockOffset) 1628 *StartOfBlockOffset = Cursor.GetCurrentBitNo(); 1629 1630 while (true) { 1631 uint64_t Offset = Cursor.GetCurrentBitNo(); 1632 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1633 if (!MaybeCode) { 1634 // FIXME this drops errors on the floor. 1635 consumeError(MaybeCode.takeError()); 1636 return true; 1637 } 1638 unsigned Code = MaybeCode.get(); 1639 1640 // We expect all abbrevs to be at the start of the block. 1641 if (Code != llvm::bitc::DEFINE_ABBREV) { 1642 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1643 // FIXME this drops errors on the floor. 1644 consumeError(std::move(Err)); 1645 return true; 1646 } 1647 return false; 1648 } 1649 if (llvm::Error Err = Cursor.ReadAbbrevRecord()) { 1650 // FIXME this drops errors on the floor. 1651 consumeError(std::move(Err)); 1652 return true; 1653 } 1654 } 1655 } 1656 1657 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record, 1658 unsigned &Idx) { 1659 Token Tok; 1660 Tok.startToken(); 1661 Tok.setLocation(ReadSourceLocation(F, Record, Idx)); 1662 Tok.setLength(Record[Idx++]); 1663 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++])) 1664 Tok.setIdentifierInfo(II); 1665 Tok.setKind((tok::TokenKind)Record[Idx++]); 1666 Tok.setFlag((Token::TokenFlags)Record[Idx++]); 1667 return Tok; 1668 } 1669 1670 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) { 1671 BitstreamCursor &Stream = F.MacroCursor; 1672 1673 // Keep track of where we are in the stream, then jump back there 1674 // after reading this macro. 1675 SavedStreamPosition SavedPosition(Stream); 1676 1677 if (llvm::Error Err = Stream.JumpToBit(Offset)) { 1678 // FIXME this drops errors on the floor. 1679 consumeError(std::move(Err)); 1680 return nullptr; 1681 } 1682 RecordData Record; 1683 SmallVector<IdentifierInfo*, 16> MacroParams; 1684 MacroInfo *Macro = nullptr; 1685 1686 while (true) { 1687 // Advance to the next record, but if we get to the end of the block, don't 1688 // pop it (removing all the abbreviations from the cursor) since we want to 1689 // be able to reseek within the block and read entries. 1690 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd; 1691 Expected<llvm::BitstreamEntry> MaybeEntry = 1692 Stream.advanceSkippingSubblocks(Flags); 1693 if (!MaybeEntry) { 1694 Error(MaybeEntry.takeError()); 1695 return Macro; 1696 } 1697 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1698 1699 switch (Entry.Kind) { 1700 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1701 case llvm::BitstreamEntry::Error: 1702 Error("malformed block record in AST file"); 1703 return Macro; 1704 case llvm::BitstreamEntry::EndBlock: 1705 return Macro; 1706 case llvm::BitstreamEntry::Record: 1707 // The interesting case. 1708 break; 1709 } 1710 1711 // Read a record. 1712 Record.clear(); 1713 PreprocessorRecordTypes RecType; 1714 if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record)) 1715 RecType = (PreprocessorRecordTypes)MaybeRecType.get(); 1716 else { 1717 Error(MaybeRecType.takeError()); 1718 return Macro; 1719 } 1720 switch (RecType) { 1721 case PP_MODULE_MACRO: 1722 case PP_MACRO_DIRECTIVE_HISTORY: 1723 return Macro; 1724 1725 case PP_MACRO_OBJECT_LIKE: 1726 case PP_MACRO_FUNCTION_LIKE: { 1727 // If we already have a macro, that means that we've hit the end 1728 // of the definition of the macro we were looking for. We're 1729 // done. 1730 if (Macro) 1731 return Macro; 1732 1733 unsigned NextIndex = 1; // Skip identifier ID. 1734 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex); 1735 MacroInfo *MI = PP.AllocateMacroInfo(Loc); 1736 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex)); 1737 MI->setIsUsed(Record[NextIndex++]); 1738 MI->setUsedForHeaderGuard(Record[NextIndex++]); 1739 1740 if (RecType == PP_MACRO_FUNCTION_LIKE) { 1741 // Decode function-like macro info. 1742 bool isC99VarArgs = Record[NextIndex++]; 1743 bool isGNUVarArgs = Record[NextIndex++]; 1744 bool hasCommaPasting = Record[NextIndex++]; 1745 MacroParams.clear(); 1746 unsigned NumArgs = Record[NextIndex++]; 1747 for (unsigned i = 0; i != NumArgs; ++i) 1748 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++])); 1749 1750 // Install function-like macro info. 1751 MI->setIsFunctionLike(); 1752 if (isC99VarArgs) MI->setIsC99Varargs(); 1753 if (isGNUVarArgs) MI->setIsGNUVarargs(); 1754 if (hasCommaPasting) MI->setHasCommaPasting(); 1755 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator()); 1756 } 1757 1758 // Remember that we saw this macro last so that we add the tokens that 1759 // form its body to it. 1760 Macro = MI; 1761 1762 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() && 1763 Record[NextIndex]) { 1764 // We have a macro definition. Register the association 1765 PreprocessedEntityID 1766 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]); 1767 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 1768 PreprocessingRecord::PPEntityID PPID = 1769 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true); 1770 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>( 1771 PPRec.getPreprocessedEntity(PPID)); 1772 if (PPDef) 1773 PPRec.RegisterMacroDefinition(Macro, PPDef); 1774 } 1775 1776 ++NumMacrosRead; 1777 break; 1778 } 1779 1780 case PP_TOKEN: { 1781 // If we see a TOKEN before a PP_MACRO_*, then the file is 1782 // erroneous, just pretend we didn't see this. 1783 if (!Macro) break; 1784 1785 unsigned Idx = 0; 1786 Token Tok = ReadToken(F, Record, Idx); 1787 Macro->AddTokenToBody(Tok); 1788 break; 1789 } 1790 } 1791 } 1792 } 1793 1794 PreprocessedEntityID 1795 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, 1796 unsigned LocalID) const { 1797 if (!M.ModuleOffsetMap.empty()) 1798 ReadModuleOffsetMap(M); 1799 1800 ContinuousRangeMap<uint32_t, int, 2>::const_iterator 1801 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS); 1802 assert(I != M.PreprocessedEntityRemap.end() 1803 && "Invalid index into preprocessed entity index remap"); 1804 1805 return LocalID + I->second; 1806 } 1807 1808 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) { 1809 return llvm::hash_combine(ikey.Size, ikey.ModTime); 1810 } 1811 1812 HeaderFileInfoTrait::internal_key_type 1813 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) { 1814 internal_key_type ikey = {FE->getSize(), 1815 M.HasTimestamps ? FE->getModificationTime() : 0, 1816 FE->getName(), /*Imported*/ false}; 1817 return ikey; 1818 } 1819 1820 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) { 1821 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime)) 1822 return false; 1823 1824 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename) 1825 return true; 1826 1827 // Determine whether the actual files are equivalent. 1828 FileManager &FileMgr = Reader.getFileManager(); 1829 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* { 1830 if (!Key.Imported) { 1831 if (auto File = FileMgr.getFile(Key.Filename)) 1832 return *File; 1833 return nullptr; 1834 } 1835 1836 std::string Resolved = std::string(Key.Filename); 1837 Reader.ResolveImportedPath(M, Resolved); 1838 if (auto File = FileMgr.getFile(Resolved)) 1839 return *File; 1840 return nullptr; 1841 }; 1842 1843 const FileEntry *FEA = GetFile(a); 1844 const FileEntry *FEB = GetFile(b); 1845 return FEA && FEA == FEB; 1846 } 1847 1848 std::pair<unsigned, unsigned> 1849 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) { 1850 using namespace llvm::support; 1851 1852 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d); 1853 unsigned DataLen = (unsigned) *d++; 1854 return std::make_pair(KeyLen, DataLen); 1855 } 1856 1857 HeaderFileInfoTrait::internal_key_type 1858 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) { 1859 using namespace llvm::support; 1860 1861 internal_key_type ikey; 1862 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d)); 1863 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d)); 1864 ikey.Filename = (const char *)d; 1865 ikey.Imported = true; 1866 return ikey; 1867 } 1868 1869 HeaderFileInfoTrait::data_type 1870 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d, 1871 unsigned DataLen) { 1872 using namespace llvm::support; 1873 1874 const unsigned char *End = d + DataLen; 1875 HeaderFileInfo HFI; 1876 unsigned Flags = *d++; 1877 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp. 1878 HFI.isImport |= (Flags >> 5) & 0x01; 1879 HFI.isPragmaOnce |= (Flags >> 4) & 0x01; 1880 HFI.DirInfo = (Flags >> 1) & 0x07; 1881 HFI.IndexHeaderMapHeader = Flags & 0x01; 1882 // FIXME: Find a better way to handle this. Maybe just store a 1883 // "has been included" flag? 1884 HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d), 1885 HFI.NumIncludes); 1886 HFI.ControllingMacroID = Reader.getGlobalIdentifierID( 1887 M, endian::readNext<uint32_t, little, unaligned>(d)); 1888 if (unsigned FrameworkOffset = 1889 endian::readNext<uint32_t, little, unaligned>(d)) { 1890 // The framework offset is 1 greater than the actual offset, 1891 // since 0 is used as an indicator for "no framework name". 1892 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1); 1893 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName); 1894 } 1895 1896 assert((End - d) % 4 == 0 && 1897 "Wrong data length in HeaderFileInfo deserialization"); 1898 while (d != End) { 1899 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d); 1900 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3); 1901 LocalSMID >>= 2; 1902 1903 // This header is part of a module. Associate it with the module to enable 1904 // implicit module import. 1905 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID); 1906 Module *Mod = Reader.getSubmodule(GlobalSMID); 1907 FileManager &FileMgr = Reader.getFileManager(); 1908 ModuleMap &ModMap = 1909 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap(); 1910 1911 std::string Filename = std::string(key.Filename); 1912 if (key.Imported) 1913 Reader.ResolveImportedPath(M, Filename); 1914 // FIXME: This is not always the right filename-as-written, but we're not 1915 // going to use this information to rebuild the module, so it doesn't make 1916 // a lot of difference. 1917 Module::Header H = {std::string(key.Filename), 1918 *FileMgr.getOptionalFileRef(Filename)}; 1919 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true); 1920 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader); 1921 } 1922 1923 // This HeaderFileInfo was externally loaded. 1924 HFI.External = true; 1925 HFI.IsValid = true; 1926 return HFI; 1927 } 1928 1929 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M, 1930 uint32_t MacroDirectivesOffset) { 1931 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard"); 1932 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset)); 1933 } 1934 1935 void ASTReader::ReadDefinedMacros() { 1936 // Note that we are loading defined macros. 1937 Deserializing Macros(this); 1938 1939 for (ModuleFile &I : llvm::reverse(ModuleMgr)) { 1940 BitstreamCursor &MacroCursor = I.MacroCursor; 1941 1942 // If there was no preprocessor block, skip this file. 1943 if (MacroCursor.getBitcodeBytes().empty()) 1944 continue; 1945 1946 BitstreamCursor Cursor = MacroCursor; 1947 if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) { 1948 Error(std::move(Err)); 1949 return; 1950 } 1951 1952 RecordData Record; 1953 while (true) { 1954 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks(); 1955 if (!MaybeE) { 1956 Error(MaybeE.takeError()); 1957 return; 1958 } 1959 llvm::BitstreamEntry E = MaybeE.get(); 1960 1961 switch (E.Kind) { 1962 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1963 case llvm::BitstreamEntry::Error: 1964 Error("malformed block record in AST file"); 1965 return; 1966 case llvm::BitstreamEntry::EndBlock: 1967 goto NextCursor; 1968 1969 case llvm::BitstreamEntry::Record: { 1970 Record.clear(); 1971 Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record); 1972 if (!MaybeRecord) { 1973 Error(MaybeRecord.takeError()); 1974 return; 1975 } 1976 switch (MaybeRecord.get()) { 1977 default: // Default behavior: ignore. 1978 break; 1979 1980 case PP_MACRO_OBJECT_LIKE: 1981 case PP_MACRO_FUNCTION_LIKE: { 1982 IdentifierInfo *II = getLocalIdentifier(I, Record[0]); 1983 if (II->isOutOfDate()) 1984 updateOutOfDateIdentifier(*II); 1985 break; 1986 } 1987 1988 case PP_TOKEN: 1989 // Ignore tokens. 1990 break; 1991 } 1992 break; 1993 } 1994 } 1995 } 1996 NextCursor: ; 1997 } 1998 } 1999 2000 namespace { 2001 2002 /// Visitor class used to look up identifirs in an AST file. 2003 class IdentifierLookupVisitor { 2004 StringRef Name; 2005 unsigned NameHash; 2006 unsigned PriorGeneration; 2007 unsigned &NumIdentifierLookups; 2008 unsigned &NumIdentifierLookupHits; 2009 IdentifierInfo *Found = nullptr; 2010 2011 public: 2012 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration, 2013 unsigned &NumIdentifierLookups, 2014 unsigned &NumIdentifierLookupHits) 2015 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)), 2016 PriorGeneration(PriorGeneration), 2017 NumIdentifierLookups(NumIdentifierLookups), 2018 NumIdentifierLookupHits(NumIdentifierLookupHits) {} 2019 2020 bool operator()(ModuleFile &M) { 2021 // If we've already searched this module file, skip it now. 2022 if (M.Generation <= PriorGeneration) 2023 return true; 2024 2025 ASTIdentifierLookupTable *IdTable 2026 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable; 2027 if (!IdTable) 2028 return false; 2029 2030 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M, 2031 Found); 2032 ++NumIdentifierLookups; 2033 ASTIdentifierLookupTable::iterator Pos = 2034 IdTable->find_hashed(Name, NameHash, &Trait); 2035 if (Pos == IdTable->end()) 2036 return false; 2037 2038 // Dereferencing the iterator has the effect of building the 2039 // IdentifierInfo node and populating it with the various 2040 // declarations it needs. 2041 ++NumIdentifierLookupHits; 2042 Found = *Pos; 2043 return true; 2044 } 2045 2046 // Retrieve the identifier info found within the module 2047 // files. 2048 IdentifierInfo *getIdentifierInfo() const { return Found; } 2049 }; 2050 2051 } // namespace 2052 2053 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) { 2054 // Note that we are loading an identifier. 2055 Deserializing AnIdentifier(this); 2056 2057 unsigned PriorGeneration = 0; 2058 if (getContext().getLangOpts().Modules) 2059 PriorGeneration = IdentifierGeneration[&II]; 2060 2061 // If there is a global index, look there first to determine which modules 2062 // provably do not have any results for this identifier. 2063 GlobalModuleIndex::HitSet Hits; 2064 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 2065 if (!loadGlobalIndex()) { 2066 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) { 2067 HitsPtr = &Hits; 2068 } 2069 } 2070 2071 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration, 2072 NumIdentifierLookups, 2073 NumIdentifierLookupHits); 2074 ModuleMgr.visit(Visitor, HitsPtr); 2075 markIdentifierUpToDate(&II); 2076 } 2077 2078 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) { 2079 if (!II) 2080 return; 2081 2082 II->setOutOfDate(false); 2083 2084 // Update the generation for this identifier. 2085 if (getContext().getLangOpts().Modules) 2086 IdentifierGeneration[II] = getGeneration(); 2087 } 2088 2089 void ASTReader::resolvePendingMacro(IdentifierInfo *II, 2090 const PendingMacroInfo &PMInfo) { 2091 ModuleFile &M = *PMInfo.M; 2092 2093 BitstreamCursor &Cursor = M.MacroCursor; 2094 SavedStreamPosition SavedPosition(Cursor); 2095 if (llvm::Error Err = 2096 Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) { 2097 Error(std::move(Err)); 2098 return; 2099 } 2100 2101 struct ModuleMacroRecord { 2102 SubmoduleID SubModID; 2103 MacroInfo *MI; 2104 SmallVector<SubmoduleID, 8> Overrides; 2105 }; 2106 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros; 2107 2108 // We expect to see a sequence of PP_MODULE_MACRO records listing exported 2109 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete 2110 // macro histroy. 2111 RecordData Record; 2112 while (true) { 2113 Expected<llvm::BitstreamEntry> MaybeEntry = 2114 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 2115 if (!MaybeEntry) { 2116 Error(MaybeEntry.takeError()); 2117 return; 2118 } 2119 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2120 2121 if (Entry.Kind != llvm::BitstreamEntry::Record) { 2122 Error("malformed block record in AST file"); 2123 return; 2124 } 2125 2126 Record.clear(); 2127 Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record); 2128 if (!MaybePP) { 2129 Error(MaybePP.takeError()); 2130 return; 2131 } 2132 switch ((PreprocessorRecordTypes)MaybePP.get()) { 2133 case PP_MACRO_DIRECTIVE_HISTORY: 2134 break; 2135 2136 case PP_MODULE_MACRO: { 2137 ModuleMacros.push_back(ModuleMacroRecord()); 2138 auto &Info = ModuleMacros.back(); 2139 Info.SubModID = getGlobalSubmoduleID(M, Record[0]); 2140 Info.MI = getMacro(getGlobalMacroID(M, Record[1])); 2141 for (int I = 2, N = Record.size(); I != N; ++I) 2142 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I])); 2143 continue; 2144 } 2145 2146 default: 2147 Error("malformed block record in AST file"); 2148 return; 2149 } 2150 2151 // We found the macro directive history; that's the last record 2152 // for this macro. 2153 break; 2154 } 2155 2156 // Module macros are listed in reverse dependency order. 2157 { 2158 std::reverse(ModuleMacros.begin(), ModuleMacros.end()); 2159 llvm::SmallVector<ModuleMacro*, 8> Overrides; 2160 for (auto &MMR : ModuleMacros) { 2161 Overrides.clear(); 2162 for (unsigned ModID : MMR.Overrides) { 2163 Module *Mod = getSubmodule(ModID); 2164 auto *Macro = PP.getModuleMacro(Mod, II); 2165 assert(Macro && "missing definition for overridden macro"); 2166 Overrides.push_back(Macro); 2167 } 2168 2169 bool Inserted = false; 2170 Module *Owner = getSubmodule(MMR.SubModID); 2171 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted); 2172 } 2173 } 2174 2175 // Don't read the directive history for a module; we don't have anywhere 2176 // to put it. 2177 if (M.isModule()) 2178 return; 2179 2180 // Deserialize the macro directives history in reverse source-order. 2181 MacroDirective *Latest = nullptr, *Earliest = nullptr; 2182 unsigned Idx = 0, N = Record.size(); 2183 while (Idx < N) { 2184 MacroDirective *MD = nullptr; 2185 SourceLocation Loc = ReadSourceLocation(M, Record, Idx); 2186 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++]; 2187 switch (K) { 2188 case MacroDirective::MD_Define: { 2189 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++])); 2190 MD = PP.AllocateDefMacroDirective(MI, Loc); 2191 break; 2192 } 2193 case MacroDirective::MD_Undefine: 2194 MD = PP.AllocateUndefMacroDirective(Loc); 2195 break; 2196 case MacroDirective::MD_Visibility: 2197 bool isPublic = Record[Idx++]; 2198 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic); 2199 break; 2200 } 2201 2202 if (!Latest) 2203 Latest = MD; 2204 if (Earliest) 2205 Earliest->setPrevious(MD); 2206 Earliest = MD; 2207 } 2208 2209 if (Latest) 2210 PP.setLoadedMacroDirective(II, Earliest, Latest); 2211 } 2212 2213 bool ASTReader::shouldDisableValidationForFile( 2214 const serialization::ModuleFile &M) const { 2215 if (DisableValidationKind == DisableValidationForModuleKind::None) 2216 return false; 2217 2218 // If a PCH is loaded and validation is disabled for PCH then disable 2219 // validation for the PCH and the modules it loads. 2220 ModuleKind K = CurrentDeserializingModuleKind.getValueOr(M.Kind); 2221 2222 switch (K) { 2223 case MK_MainFile: 2224 case MK_Preamble: 2225 case MK_PCH: 2226 return bool(DisableValidationKind & DisableValidationForModuleKind::PCH); 2227 case MK_ImplicitModule: 2228 case MK_ExplicitModule: 2229 case MK_PrebuiltModule: 2230 return bool(DisableValidationKind & DisableValidationForModuleKind::Module); 2231 } 2232 2233 return false; 2234 } 2235 2236 ASTReader::InputFileInfo 2237 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) { 2238 // Go find this input file. 2239 BitstreamCursor &Cursor = F.InputFilesCursor; 2240 SavedStreamPosition SavedPosition(Cursor); 2241 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2242 // FIXME this drops errors on the floor. 2243 consumeError(std::move(Err)); 2244 } 2245 2246 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 2247 if (!MaybeCode) { 2248 // FIXME this drops errors on the floor. 2249 consumeError(MaybeCode.takeError()); 2250 } 2251 unsigned Code = MaybeCode.get(); 2252 RecordData Record; 2253 StringRef Blob; 2254 2255 if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob)) 2256 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE && 2257 "invalid record type for input file"); 2258 else { 2259 // FIXME this drops errors on the floor. 2260 consumeError(Maybe.takeError()); 2261 } 2262 2263 assert(Record[0] == ID && "Bogus stored ID or offset"); 2264 InputFileInfo R; 2265 R.StoredSize = static_cast<off_t>(Record[1]); 2266 R.StoredTime = static_cast<time_t>(Record[2]); 2267 R.Overridden = static_cast<bool>(Record[3]); 2268 R.Transient = static_cast<bool>(Record[4]); 2269 R.TopLevelModuleMap = static_cast<bool>(Record[5]); 2270 R.Filename = std::string(Blob); 2271 ResolveImportedPath(F, R.Filename); 2272 2273 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 2274 if (!MaybeEntry) // FIXME this drops errors on the floor. 2275 consumeError(MaybeEntry.takeError()); 2276 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2277 assert(Entry.Kind == llvm::BitstreamEntry::Record && 2278 "expected record type for input file hash"); 2279 2280 Record.clear(); 2281 if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record)) 2282 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH && 2283 "invalid record type for input file hash"); 2284 else { 2285 // FIXME this drops errors on the floor. 2286 consumeError(Maybe.takeError()); 2287 } 2288 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) | 2289 static_cast<uint64_t>(Record[0]); 2290 return R; 2291 } 2292 2293 static unsigned moduleKindForDiagnostic(ModuleKind Kind); 2294 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) { 2295 // If this ID is bogus, just return an empty input file. 2296 if (ID == 0 || ID > F.InputFilesLoaded.size()) 2297 return InputFile(); 2298 2299 // If we've already loaded this input file, return it. 2300 if (F.InputFilesLoaded[ID-1].getFile()) 2301 return F.InputFilesLoaded[ID-1]; 2302 2303 if (F.InputFilesLoaded[ID-1].isNotFound()) 2304 return InputFile(); 2305 2306 // Go find this input file. 2307 BitstreamCursor &Cursor = F.InputFilesCursor; 2308 SavedStreamPosition SavedPosition(Cursor); 2309 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2310 // FIXME this drops errors on the floor. 2311 consumeError(std::move(Err)); 2312 } 2313 2314 InputFileInfo FI = readInputFileInfo(F, ID); 2315 off_t StoredSize = FI.StoredSize; 2316 time_t StoredTime = FI.StoredTime; 2317 bool Overridden = FI.Overridden; 2318 bool Transient = FI.Transient; 2319 StringRef Filename = FI.Filename; 2320 uint64_t StoredContentHash = FI.ContentHash; 2321 2322 OptionalFileEntryRefDegradesToFileEntryPtr File = 2323 expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false)); 2324 2325 // If we didn't find the file, resolve it relative to the 2326 // original directory from which this AST file was created. 2327 if (!File && !F.OriginalDir.empty() && !F.BaseDirectory.empty() && 2328 F.OriginalDir != F.BaseDirectory) { 2329 std::string Resolved = resolveFileRelativeToOriginalDir( 2330 std::string(Filename), F.OriginalDir, F.BaseDirectory); 2331 if (!Resolved.empty()) 2332 File = expectedToOptional(FileMgr.getFileRef(Resolved)); 2333 } 2334 2335 // For an overridden file, create a virtual file with the stored 2336 // size/timestamp. 2337 if ((Overridden || Transient) && !File) 2338 File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime); 2339 2340 if (!File) { 2341 if (Complain) { 2342 std::string ErrorStr = "could not find file '"; 2343 ErrorStr += Filename; 2344 ErrorStr += "' referenced by AST file '"; 2345 ErrorStr += F.FileName; 2346 ErrorStr += "'"; 2347 Error(ErrorStr); 2348 } 2349 // Record that we didn't find the file. 2350 F.InputFilesLoaded[ID-1] = InputFile::getNotFound(); 2351 return InputFile(); 2352 } 2353 2354 // Check if there was a request to override the contents of the file 2355 // that was part of the precompiled header. Overriding such a file 2356 // can lead to problems when lexing using the source locations from the 2357 // PCH. 2358 SourceManager &SM = getSourceManager(); 2359 // FIXME: Reject if the overrides are different. 2360 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) { 2361 if (Complain) 2362 Error(diag::err_fe_pch_file_overridden, Filename); 2363 2364 // After emitting the diagnostic, bypass the overriding file to recover 2365 // (this creates a separate FileEntry). 2366 File = SM.bypassFileContentsOverride(*File); 2367 if (!File) { 2368 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound(); 2369 return InputFile(); 2370 } 2371 } 2372 2373 enum ModificationType { 2374 Size, 2375 ModTime, 2376 Content, 2377 None, 2378 }; 2379 auto HasInputFileChanged = [&]() { 2380 if (StoredSize != File->getSize()) 2381 return ModificationType::Size; 2382 if (!shouldDisableValidationForFile(F) && StoredTime && 2383 StoredTime != File->getModificationTime()) { 2384 // In case the modification time changes but not the content, 2385 // accept the cached file as legit. 2386 if (ValidateASTInputFilesContent && 2387 StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) { 2388 auto MemBuffOrError = FileMgr.getBufferForFile(File); 2389 if (!MemBuffOrError) { 2390 if (!Complain) 2391 return ModificationType::ModTime; 2392 std::string ErrorStr = "could not get buffer for file '"; 2393 ErrorStr += File->getName(); 2394 ErrorStr += "'"; 2395 Error(ErrorStr); 2396 return ModificationType::ModTime; 2397 } 2398 2399 auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer()); 2400 if (StoredContentHash == static_cast<uint64_t>(ContentHash)) 2401 return ModificationType::None; 2402 return ModificationType::Content; 2403 } 2404 return ModificationType::ModTime; 2405 } 2406 return ModificationType::None; 2407 }; 2408 2409 bool IsOutOfDate = false; 2410 auto FileChange = HasInputFileChanged(); 2411 // For an overridden file, there is nothing to validate. 2412 if (!Overridden && FileChange != ModificationType::None) { 2413 if (Complain && !Diags.isDiagnosticInFlight()) { 2414 // Build a list of the PCH imports that got us here (in reverse). 2415 SmallVector<ModuleFile *, 4> ImportStack(1, &F); 2416 while (!ImportStack.back()->ImportedBy.empty()) 2417 ImportStack.push_back(ImportStack.back()->ImportedBy[0]); 2418 2419 // The top-level PCH is stale. 2420 StringRef TopLevelPCHName(ImportStack.back()->FileName); 2421 Diag(diag::err_fe_ast_file_modified) 2422 << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind) 2423 << TopLevelPCHName << FileChange; 2424 2425 // Print the import stack. 2426 if (ImportStack.size() > 1) { 2427 Diag(diag::note_pch_required_by) 2428 << Filename << ImportStack[0]->FileName; 2429 for (unsigned I = 1; I < ImportStack.size(); ++I) 2430 Diag(diag::note_pch_required_by) 2431 << ImportStack[I-1]->FileName << ImportStack[I]->FileName; 2432 } 2433 2434 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName; 2435 } 2436 2437 IsOutOfDate = true; 2438 } 2439 // FIXME: If the file is overridden and we've already opened it, 2440 // issue an error (or split it into a separate FileEntry). 2441 2442 InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate); 2443 2444 // Note that we've loaded this input file. 2445 F.InputFilesLoaded[ID-1] = IF; 2446 return IF; 2447 } 2448 2449 /// If we are loading a relocatable PCH or module file, and the filename 2450 /// is not an absolute path, add the system or module root to the beginning of 2451 /// the file name. 2452 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) { 2453 // Resolve relative to the base directory, if we have one. 2454 if (!M.BaseDirectory.empty()) 2455 return ResolveImportedPath(Filename, M.BaseDirectory); 2456 } 2457 2458 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) { 2459 if (Filename.empty() || llvm::sys::path::is_absolute(Filename)) 2460 return; 2461 2462 SmallString<128> Buffer; 2463 llvm::sys::path::append(Buffer, Prefix, Filename); 2464 Filename.assign(Buffer.begin(), Buffer.end()); 2465 } 2466 2467 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) { 2468 switch (ARR) { 2469 case ASTReader::Failure: return true; 2470 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing); 2471 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate); 2472 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch); 2473 case ASTReader::ConfigurationMismatch: 2474 return !(Caps & ASTReader::ARR_ConfigurationMismatch); 2475 case ASTReader::HadErrors: return true; 2476 case ASTReader::Success: return false; 2477 } 2478 2479 llvm_unreachable("unknown ASTReadResult"); 2480 } 2481 2482 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock( 2483 BitstreamCursor &Stream, unsigned ClientLoadCapabilities, 2484 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener, 2485 std::string &SuggestedPredefines) { 2486 if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) { 2487 // FIXME this drops errors on the floor. 2488 consumeError(std::move(Err)); 2489 return Failure; 2490 } 2491 2492 // Read all of the records in the options block. 2493 RecordData Record; 2494 ASTReadResult Result = Success; 2495 while (true) { 2496 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2497 if (!MaybeEntry) { 2498 // FIXME this drops errors on the floor. 2499 consumeError(MaybeEntry.takeError()); 2500 return Failure; 2501 } 2502 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2503 2504 switch (Entry.Kind) { 2505 case llvm::BitstreamEntry::Error: 2506 case llvm::BitstreamEntry::SubBlock: 2507 return Failure; 2508 2509 case llvm::BitstreamEntry::EndBlock: 2510 return Result; 2511 2512 case llvm::BitstreamEntry::Record: 2513 // The interesting case. 2514 break; 2515 } 2516 2517 // Read and process a record. 2518 Record.clear(); 2519 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record); 2520 if (!MaybeRecordType) { 2521 // FIXME this drops errors on the floor. 2522 consumeError(MaybeRecordType.takeError()); 2523 return Failure; 2524 } 2525 switch ((OptionsRecordTypes)MaybeRecordType.get()) { 2526 case LANGUAGE_OPTIONS: { 2527 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2528 if (ParseLanguageOptions(Record, Complain, Listener, 2529 AllowCompatibleConfigurationMismatch)) 2530 Result = ConfigurationMismatch; 2531 break; 2532 } 2533 2534 case TARGET_OPTIONS: { 2535 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2536 if (ParseTargetOptions(Record, Complain, Listener, 2537 AllowCompatibleConfigurationMismatch)) 2538 Result = ConfigurationMismatch; 2539 break; 2540 } 2541 2542 case FILE_SYSTEM_OPTIONS: { 2543 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2544 if (!AllowCompatibleConfigurationMismatch && 2545 ParseFileSystemOptions(Record, Complain, Listener)) 2546 Result = ConfigurationMismatch; 2547 break; 2548 } 2549 2550 case HEADER_SEARCH_OPTIONS: { 2551 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2552 if (!AllowCompatibleConfigurationMismatch && 2553 ParseHeaderSearchOptions(Record, Complain, Listener)) 2554 Result = ConfigurationMismatch; 2555 break; 2556 } 2557 2558 case PREPROCESSOR_OPTIONS: 2559 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2560 if (!AllowCompatibleConfigurationMismatch && 2561 ParsePreprocessorOptions(Record, Complain, Listener, 2562 SuggestedPredefines)) 2563 Result = ConfigurationMismatch; 2564 break; 2565 } 2566 } 2567 } 2568 2569 ASTReader::ASTReadResult 2570 ASTReader::ReadControlBlock(ModuleFile &F, 2571 SmallVectorImpl<ImportedModule> &Loaded, 2572 const ModuleFile *ImportedBy, 2573 unsigned ClientLoadCapabilities) { 2574 BitstreamCursor &Stream = F.Stream; 2575 2576 if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) { 2577 Error(std::move(Err)); 2578 return Failure; 2579 } 2580 2581 // Lambda to read the unhashed control block the first time it's called. 2582 // 2583 // For PCM files, the unhashed control block cannot be read until after the 2584 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still 2585 // need to look ahead before reading the IMPORTS record. For consistency, 2586 // this block is always read somehow (see BitstreamEntry::EndBlock). 2587 bool HasReadUnhashedControlBlock = false; 2588 auto readUnhashedControlBlockOnce = [&]() { 2589 if (!HasReadUnhashedControlBlock) { 2590 HasReadUnhashedControlBlock = true; 2591 if (ASTReadResult Result = 2592 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities)) 2593 return Result; 2594 } 2595 return Success; 2596 }; 2597 2598 bool DisableValidation = shouldDisableValidationForFile(F); 2599 2600 // Read all of the records and blocks in the control block. 2601 RecordData Record; 2602 unsigned NumInputs = 0; 2603 unsigned NumUserInputs = 0; 2604 StringRef BaseDirectoryAsWritten; 2605 while (true) { 2606 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2607 if (!MaybeEntry) { 2608 Error(MaybeEntry.takeError()); 2609 return Failure; 2610 } 2611 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2612 2613 switch (Entry.Kind) { 2614 case llvm::BitstreamEntry::Error: 2615 Error("malformed block record in AST file"); 2616 return Failure; 2617 case llvm::BitstreamEntry::EndBlock: { 2618 // Validate the module before returning. This call catches an AST with 2619 // no module name and no imports. 2620 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2621 return Result; 2622 2623 // Validate input files. 2624 const HeaderSearchOptions &HSOpts = 2625 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 2626 2627 // All user input files reside at the index range [0, NumUserInputs), and 2628 // system input files reside at [NumUserInputs, NumInputs). For explicitly 2629 // loaded module files, ignore missing inputs. 2630 if (!DisableValidation && F.Kind != MK_ExplicitModule && 2631 F.Kind != MK_PrebuiltModule) { 2632 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 2633 2634 // If we are reading a module, we will create a verification timestamp, 2635 // so we verify all input files. Otherwise, verify only user input 2636 // files. 2637 2638 unsigned N = NumUserInputs; 2639 if (ValidateSystemInputs || 2640 (HSOpts.ModulesValidateOncePerBuildSession && 2641 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp && 2642 F.Kind == MK_ImplicitModule)) 2643 N = NumInputs; 2644 2645 for (unsigned I = 0; I < N; ++I) { 2646 InputFile IF = getInputFile(F, I+1, Complain); 2647 if (!IF.getFile() || IF.isOutOfDate()) 2648 return OutOfDate; 2649 } 2650 } 2651 2652 if (Listener) 2653 Listener->visitModuleFile(F.FileName, F.Kind); 2654 2655 if (Listener && Listener->needsInputFileVisitation()) { 2656 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs 2657 : NumUserInputs; 2658 for (unsigned I = 0; I < N; ++I) { 2659 bool IsSystem = I >= NumUserInputs; 2660 InputFileInfo FI = readInputFileInfo(F, I+1); 2661 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden, 2662 F.Kind == MK_ExplicitModule || 2663 F.Kind == MK_PrebuiltModule); 2664 } 2665 } 2666 2667 return Success; 2668 } 2669 2670 case llvm::BitstreamEntry::SubBlock: 2671 switch (Entry.ID) { 2672 case INPUT_FILES_BLOCK_ID: 2673 F.InputFilesCursor = Stream; 2674 if (llvm::Error Err = Stream.SkipBlock()) { 2675 Error(std::move(Err)); 2676 return Failure; 2677 } 2678 if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) { 2679 Error("malformed block record in AST file"); 2680 return Failure; 2681 } 2682 continue; 2683 2684 case OPTIONS_BLOCK_ID: 2685 // If we're reading the first module for this group, check its options 2686 // are compatible with ours. For modules it imports, no further checking 2687 // is required, because we checked them when we built it. 2688 if (Listener && !ImportedBy) { 2689 // Should we allow the configuration of the module file to differ from 2690 // the configuration of the current translation unit in a compatible 2691 // way? 2692 // 2693 // FIXME: Allow this for files explicitly specified with -include-pch. 2694 bool AllowCompatibleConfigurationMismatch = 2695 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 2696 2697 ASTReadResult Result = 2698 ReadOptionsBlock(Stream, ClientLoadCapabilities, 2699 AllowCompatibleConfigurationMismatch, *Listener, 2700 SuggestedPredefines); 2701 if (Result == Failure) { 2702 Error("malformed block record in AST file"); 2703 return Result; 2704 } 2705 2706 if (DisableValidation || 2707 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 2708 Result = Success; 2709 2710 // If we can't load the module, exit early since we likely 2711 // will rebuild the module anyway. The stream may be in the 2712 // middle of a block. 2713 if (Result != Success) 2714 return Result; 2715 } else if (llvm::Error Err = Stream.SkipBlock()) { 2716 Error(std::move(Err)); 2717 return Failure; 2718 } 2719 continue; 2720 2721 default: 2722 if (llvm::Error Err = Stream.SkipBlock()) { 2723 Error(std::move(Err)); 2724 return Failure; 2725 } 2726 continue; 2727 } 2728 2729 case llvm::BitstreamEntry::Record: 2730 // The interesting case. 2731 break; 2732 } 2733 2734 // Read and process a record. 2735 Record.clear(); 2736 StringRef Blob; 2737 Expected<unsigned> MaybeRecordType = 2738 Stream.readRecord(Entry.ID, Record, &Blob); 2739 if (!MaybeRecordType) { 2740 Error(MaybeRecordType.takeError()); 2741 return Failure; 2742 } 2743 switch ((ControlRecordTypes)MaybeRecordType.get()) { 2744 case METADATA: { 2745 if (Record[0] != VERSION_MAJOR && !DisableValidation) { 2746 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2747 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old 2748 : diag::err_pch_version_too_new); 2749 return VersionMismatch; 2750 } 2751 2752 bool hasErrors = Record[6]; 2753 if (hasErrors && !DisableValidation) { 2754 // If requested by the caller, mark modules on error as out-of-date. 2755 if (F.Kind == MK_ImplicitModule && 2756 (ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate)) 2757 return OutOfDate; 2758 2759 if (!AllowASTWithCompilerErrors) { 2760 Diag(diag::err_pch_with_compiler_errors); 2761 return HadErrors; 2762 } 2763 } 2764 if (hasErrors) { 2765 Diags.ErrorOccurred = true; 2766 Diags.UncompilableErrorOccurred = true; 2767 Diags.UnrecoverableErrorOccurred = true; 2768 } 2769 2770 F.RelocatablePCH = Record[4]; 2771 // Relative paths in a relocatable PCH are relative to our sysroot. 2772 if (F.RelocatablePCH) 2773 F.BaseDirectory = isysroot.empty() ? "/" : isysroot; 2774 2775 F.HasTimestamps = Record[5]; 2776 2777 const std::string &CurBranch = getClangFullRepositoryVersion(); 2778 StringRef ASTBranch = Blob; 2779 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) { 2780 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2781 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch; 2782 return VersionMismatch; 2783 } 2784 break; 2785 } 2786 2787 case IMPORTS: { 2788 // Validate the AST before processing any imports (otherwise, untangling 2789 // them can be error-prone and expensive). A module will have a name and 2790 // will already have been validated, but this catches the PCH case. 2791 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2792 return Result; 2793 2794 // Load each of the imported PCH files. 2795 unsigned Idx = 0, N = Record.size(); 2796 while (Idx < N) { 2797 // Read information about the AST file. 2798 ModuleKind ImportedKind = (ModuleKind)Record[Idx++]; 2799 // The import location will be the local one for now; we will adjust 2800 // all import locations of module imports after the global source 2801 // location info are setup, in ReadAST. 2802 SourceLocation ImportLoc = 2803 ReadUntranslatedSourceLocation(Record[Idx++]); 2804 off_t StoredSize = (off_t)Record[Idx++]; 2805 time_t StoredModTime = (time_t)Record[Idx++]; 2806 auto FirstSignatureByte = Record.begin() + Idx; 2807 ASTFileSignature StoredSignature = ASTFileSignature::create( 2808 FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size); 2809 Idx += ASTFileSignature::size; 2810 2811 std::string ImportedName = ReadString(Record, Idx); 2812 std::string ImportedFile; 2813 2814 // For prebuilt and explicit modules first consult the file map for 2815 // an override. Note that here we don't search prebuilt module 2816 // directories, only the explicit name to file mappings. Also, we will 2817 // still verify the size/signature making sure it is essentially the 2818 // same file but perhaps in a different location. 2819 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule) 2820 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName( 2821 ImportedName, /*FileMapOnly*/ true); 2822 2823 if (ImportedFile.empty()) 2824 // Use BaseDirectoryAsWritten to ensure we use the same path in the 2825 // ModuleCache as when writing. 2826 ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx); 2827 else 2828 SkipPath(Record, Idx); 2829 2830 // If our client can't cope with us being out of date, we can't cope with 2831 // our dependency being missing. 2832 unsigned Capabilities = ClientLoadCapabilities; 2833 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 2834 Capabilities &= ~ARR_Missing; 2835 2836 // Load the AST file. 2837 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, 2838 Loaded, StoredSize, StoredModTime, 2839 StoredSignature, Capabilities); 2840 2841 // If we diagnosed a problem, produce a backtrace. 2842 if (isDiagnosedResult(Result, Capabilities)) 2843 Diag(diag::note_module_file_imported_by) 2844 << F.FileName << !F.ModuleName.empty() << F.ModuleName; 2845 2846 switch (Result) { 2847 case Failure: return Failure; 2848 // If we have to ignore the dependency, we'll have to ignore this too. 2849 case Missing: 2850 case OutOfDate: return OutOfDate; 2851 case VersionMismatch: return VersionMismatch; 2852 case ConfigurationMismatch: return ConfigurationMismatch; 2853 case HadErrors: return HadErrors; 2854 case Success: break; 2855 } 2856 } 2857 break; 2858 } 2859 2860 case ORIGINAL_FILE: 2861 F.OriginalSourceFileID = FileID::get(Record[0]); 2862 F.ActualOriginalSourceFileName = std::string(Blob); 2863 F.OriginalSourceFileName = F.ActualOriginalSourceFileName; 2864 ResolveImportedPath(F, F.OriginalSourceFileName); 2865 break; 2866 2867 case ORIGINAL_FILE_ID: 2868 F.OriginalSourceFileID = FileID::get(Record[0]); 2869 break; 2870 2871 case ORIGINAL_PCH_DIR: 2872 F.OriginalDir = std::string(Blob); 2873 break; 2874 2875 case MODULE_NAME: 2876 F.ModuleName = std::string(Blob); 2877 Diag(diag::remark_module_import) 2878 << F.ModuleName << F.FileName << (ImportedBy ? true : false) 2879 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 2880 if (Listener) 2881 Listener->ReadModuleName(F.ModuleName); 2882 2883 // Validate the AST as soon as we have a name so we can exit early on 2884 // failure. 2885 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2886 return Result; 2887 2888 break; 2889 2890 case MODULE_DIRECTORY: { 2891 // Save the BaseDirectory as written in the PCM for computing the module 2892 // filename for the ModuleCache. 2893 BaseDirectoryAsWritten = Blob; 2894 assert(!F.ModuleName.empty() && 2895 "MODULE_DIRECTORY found before MODULE_NAME"); 2896 // If we've already loaded a module map file covering this module, we may 2897 // have a better path for it (relative to the current build). 2898 Module *M = PP.getHeaderSearchInfo().lookupModule( 2899 F.ModuleName, /*AllowSearch*/ true, 2900 /*AllowExtraModuleMapSearch*/ true); 2901 if (M && M->Directory) { 2902 // If we're implicitly loading a module, the base directory can't 2903 // change between the build and use. 2904 // Don't emit module relocation error if we have -fno-validate-pch 2905 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation & 2906 DisableValidationForModuleKind::Module) && 2907 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) { 2908 auto BuildDir = PP.getFileManager().getDirectory(Blob); 2909 if (!BuildDir || *BuildDir != M->Directory) { 2910 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 2911 Diag(diag::err_imported_module_relocated) 2912 << F.ModuleName << Blob << M->Directory->getName(); 2913 return OutOfDate; 2914 } 2915 } 2916 F.BaseDirectory = std::string(M->Directory->getName()); 2917 } else { 2918 F.BaseDirectory = std::string(Blob); 2919 } 2920 break; 2921 } 2922 2923 case MODULE_MAP_FILE: 2924 if (ASTReadResult Result = 2925 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities)) 2926 return Result; 2927 break; 2928 2929 case INPUT_FILE_OFFSETS: 2930 NumInputs = Record[0]; 2931 NumUserInputs = Record[1]; 2932 F.InputFileOffsets = 2933 (const llvm::support::unaligned_uint64_t *)Blob.data(); 2934 F.InputFilesLoaded.resize(NumInputs); 2935 F.NumUserInputFiles = NumUserInputs; 2936 break; 2937 } 2938 } 2939 } 2940 2941 ASTReader::ASTReadResult 2942 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) { 2943 BitstreamCursor &Stream = F.Stream; 2944 2945 if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) { 2946 Error(std::move(Err)); 2947 return Failure; 2948 } 2949 F.ASTBlockStartOffset = Stream.GetCurrentBitNo(); 2950 2951 // Read all of the records and blocks for the AST file. 2952 RecordData Record; 2953 while (true) { 2954 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2955 if (!MaybeEntry) { 2956 Error(MaybeEntry.takeError()); 2957 return Failure; 2958 } 2959 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2960 2961 switch (Entry.Kind) { 2962 case llvm::BitstreamEntry::Error: 2963 Error("error at end of module block in AST file"); 2964 return Failure; 2965 case llvm::BitstreamEntry::EndBlock: 2966 // Outside of C++, we do not store a lookup map for the translation unit. 2967 // Instead, mark it as needing a lookup map to be built if this module 2968 // contains any declarations lexically within it (which it always does!). 2969 // This usually has no cost, since we very rarely need the lookup map for 2970 // the translation unit outside C++. 2971 if (ASTContext *Ctx = ContextObj) { 2972 DeclContext *DC = Ctx->getTranslationUnitDecl(); 2973 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus) 2974 DC->setMustBuildLookupTable(); 2975 } 2976 2977 return Success; 2978 case llvm::BitstreamEntry::SubBlock: 2979 switch (Entry.ID) { 2980 case DECLTYPES_BLOCK_ID: 2981 // We lazily load the decls block, but we want to set up the 2982 // DeclsCursor cursor to point into it. Clone our current bitcode 2983 // cursor to it, enter the block and read the abbrevs in that block. 2984 // With the main cursor, we just skip over it. 2985 F.DeclsCursor = Stream; 2986 if (llvm::Error Err = Stream.SkipBlock()) { 2987 Error(std::move(Err)); 2988 return Failure; 2989 } 2990 if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID, 2991 &F.DeclsBlockStartOffset)) { 2992 Error("malformed block record in AST file"); 2993 return Failure; 2994 } 2995 break; 2996 2997 case PREPROCESSOR_BLOCK_ID: 2998 F.MacroCursor = Stream; 2999 if (!PP.getExternalSource()) 3000 PP.setExternalSource(this); 3001 3002 if (llvm::Error Err = Stream.SkipBlock()) { 3003 Error(std::move(Err)); 3004 return Failure; 3005 } 3006 if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) { 3007 Error("malformed block record in AST file"); 3008 return Failure; 3009 } 3010 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo(); 3011 break; 3012 3013 case PREPROCESSOR_DETAIL_BLOCK_ID: 3014 F.PreprocessorDetailCursor = Stream; 3015 3016 if (llvm::Error Err = Stream.SkipBlock()) { 3017 Error(std::move(Err)); 3018 return Failure; 3019 } 3020 if (ReadBlockAbbrevs(F.PreprocessorDetailCursor, 3021 PREPROCESSOR_DETAIL_BLOCK_ID)) { 3022 Error("malformed preprocessor detail record in AST file"); 3023 return Failure; 3024 } 3025 F.PreprocessorDetailStartOffset 3026 = F.PreprocessorDetailCursor.GetCurrentBitNo(); 3027 3028 if (!PP.getPreprocessingRecord()) 3029 PP.createPreprocessingRecord(); 3030 if (!PP.getPreprocessingRecord()->getExternalSource()) 3031 PP.getPreprocessingRecord()->SetExternalSource(*this); 3032 break; 3033 3034 case SOURCE_MANAGER_BLOCK_ID: 3035 if (ReadSourceManagerBlock(F)) 3036 return Failure; 3037 break; 3038 3039 case SUBMODULE_BLOCK_ID: 3040 if (ASTReadResult Result = 3041 ReadSubmoduleBlock(F, ClientLoadCapabilities)) 3042 return Result; 3043 break; 3044 3045 case COMMENTS_BLOCK_ID: { 3046 BitstreamCursor C = Stream; 3047 3048 if (llvm::Error Err = Stream.SkipBlock()) { 3049 Error(std::move(Err)); 3050 return Failure; 3051 } 3052 if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) { 3053 Error("malformed comments block in AST file"); 3054 return Failure; 3055 } 3056 CommentsCursors.push_back(std::make_pair(C, &F)); 3057 break; 3058 } 3059 3060 default: 3061 if (llvm::Error Err = Stream.SkipBlock()) { 3062 Error(std::move(Err)); 3063 return Failure; 3064 } 3065 break; 3066 } 3067 continue; 3068 3069 case llvm::BitstreamEntry::Record: 3070 // The interesting case. 3071 break; 3072 } 3073 3074 // Read and process a record. 3075 Record.clear(); 3076 StringRef Blob; 3077 Expected<unsigned> MaybeRecordType = 3078 Stream.readRecord(Entry.ID, Record, &Blob); 3079 if (!MaybeRecordType) { 3080 Error(MaybeRecordType.takeError()); 3081 return Failure; 3082 } 3083 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get(); 3084 3085 // If we're not loading an AST context, we don't care about most records. 3086 if (!ContextObj) { 3087 switch (RecordType) { 3088 case IDENTIFIER_TABLE: 3089 case IDENTIFIER_OFFSET: 3090 case INTERESTING_IDENTIFIERS: 3091 case STATISTICS: 3092 case PP_CONDITIONAL_STACK: 3093 case PP_COUNTER_VALUE: 3094 case SOURCE_LOCATION_OFFSETS: 3095 case MODULE_OFFSET_MAP: 3096 case SOURCE_MANAGER_LINE_TABLE: 3097 case SOURCE_LOCATION_PRELOADS: 3098 case PPD_ENTITIES_OFFSETS: 3099 case HEADER_SEARCH_TABLE: 3100 case IMPORTED_MODULES: 3101 case MACRO_OFFSET: 3102 break; 3103 default: 3104 continue; 3105 } 3106 } 3107 3108 switch (RecordType) { 3109 default: // Default behavior: ignore. 3110 break; 3111 3112 case TYPE_OFFSET: { 3113 if (F.LocalNumTypes != 0) { 3114 Error("duplicate TYPE_OFFSET record in AST file"); 3115 return Failure; 3116 } 3117 F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data()); 3118 F.LocalNumTypes = Record[0]; 3119 unsigned LocalBaseTypeIndex = Record[1]; 3120 F.BaseTypeIndex = getTotalNumTypes(); 3121 3122 if (F.LocalNumTypes > 0) { 3123 // Introduce the global -> local mapping for types within this module. 3124 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F)); 3125 3126 // Introduce the local -> global mapping for types within this module. 3127 F.TypeRemap.insertOrReplace( 3128 std::make_pair(LocalBaseTypeIndex, 3129 F.BaseTypeIndex - LocalBaseTypeIndex)); 3130 3131 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes); 3132 } 3133 break; 3134 } 3135 3136 case DECL_OFFSET: { 3137 if (F.LocalNumDecls != 0) { 3138 Error("duplicate DECL_OFFSET record in AST file"); 3139 return Failure; 3140 } 3141 F.DeclOffsets = (const DeclOffset *)Blob.data(); 3142 F.LocalNumDecls = Record[0]; 3143 unsigned LocalBaseDeclID = Record[1]; 3144 F.BaseDeclID = getTotalNumDecls(); 3145 3146 if (F.LocalNumDecls > 0) { 3147 // Introduce the global -> local mapping for declarations within this 3148 // module. 3149 GlobalDeclMap.insert( 3150 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F)); 3151 3152 // Introduce the local -> global mapping for declarations within this 3153 // module. 3154 F.DeclRemap.insertOrReplace( 3155 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID)); 3156 3157 // Introduce the global -> local mapping for declarations within this 3158 // module. 3159 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID; 3160 3161 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls); 3162 } 3163 break; 3164 } 3165 3166 case TU_UPDATE_LEXICAL: { 3167 DeclContext *TU = ContextObj->getTranslationUnitDecl(); 3168 LexicalContents Contents( 3169 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 3170 Blob.data()), 3171 static_cast<unsigned int>(Blob.size() / 4)); 3172 TULexicalDecls.push_back(std::make_pair(&F, Contents)); 3173 TU->setHasExternalLexicalStorage(true); 3174 break; 3175 } 3176 3177 case UPDATE_VISIBLE: { 3178 unsigned Idx = 0; 3179 serialization::DeclID ID = ReadDeclID(F, Record, Idx); 3180 auto *Data = (const unsigned char*)Blob.data(); 3181 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data}); 3182 // If we've already loaded the decl, perform the updates when we finish 3183 // loading this block. 3184 if (Decl *D = GetExistingDecl(ID)) 3185 PendingUpdateRecords.push_back( 3186 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3187 break; 3188 } 3189 3190 case IDENTIFIER_TABLE: 3191 F.IdentifierTableData = Blob.data(); 3192 if (Record[0]) { 3193 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create( 3194 (const unsigned char *)F.IdentifierTableData + Record[0], 3195 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t), 3196 (const unsigned char *)F.IdentifierTableData, 3197 ASTIdentifierLookupTrait(*this, F)); 3198 3199 PP.getIdentifierTable().setExternalIdentifierLookup(this); 3200 } 3201 break; 3202 3203 case IDENTIFIER_OFFSET: { 3204 if (F.LocalNumIdentifiers != 0) { 3205 Error("duplicate IDENTIFIER_OFFSET record in AST file"); 3206 return Failure; 3207 } 3208 F.IdentifierOffsets = (const uint32_t *)Blob.data(); 3209 F.LocalNumIdentifiers = Record[0]; 3210 unsigned LocalBaseIdentifierID = Record[1]; 3211 F.BaseIdentifierID = getTotalNumIdentifiers(); 3212 3213 if (F.LocalNumIdentifiers > 0) { 3214 // Introduce the global -> local mapping for identifiers within this 3215 // module. 3216 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1, 3217 &F)); 3218 3219 // Introduce the local -> global mapping for identifiers within this 3220 // module. 3221 F.IdentifierRemap.insertOrReplace( 3222 std::make_pair(LocalBaseIdentifierID, 3223 F.BaseIdentifierID - LocalBaseIdentifierID)); 3224 3225 IdentifiersLoaded.resize(IdentifiersLoaded.size() 3226 + F.LocalNumIdentifiers); 3227 } 3228 break; 3229 } 3230 3231 case INTERESTING_IDENTIFIERS: 3232 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end()); 3233 break; 3234 3235 case EAGERLY_DESERIALIZED_DECLS: 3236 // FIXME: Skip reading this record if our ASTConsumer doesn't care 3237 // about "interesting" decls (for instance, if we're building a module). 3238 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3239 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3240 break; 3241 3242 case MODULAR_CODEGEN_DECLS: 3243 // FIXME: Skip reading this record if our ASTConsumer doesn't care about 3244 // them (ie: if we're not codegenerating this module). 3245 if (F.Kind == MK_MainFile || 3246 getContext().getLangOpts().BuildingPCHWithObjectFile) 3247 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3248 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3249 break; 3250 3251 case SPECIAL_TYPES: 3252 if (SpecialTypes.empty()) { 3253 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3254 SpecialTypes.push_back(getGlobalTypeID(F, Record[I])); 3255 break; 3256 } 3257 3258 if (SpecialTypes.size() != Record.size()) { 3259 Error("invalid special-types record"); 3260 return Failure; 3261 } 3262 3263 for (unsigned I = 0, N = Record.size(); I != N; ++I) { 3264 serialization::TypeID ID = getGlobalTypeID(F, Record[I]); 3265 if (!SpecialTypes[I]) 3266 SpecialTypes[I] = ID; 3267 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate 3268 // merge step? 3269 } 3270 break; 3271 3272 case STATISTICS: 3273 TotalNumStatements += Record[0]; 3274 TotalNumMacros += Record[1]; 3275 TotalLexicalDeclContexts += Record[2]; 3276 TotalVisibleDeclContexts += Record[3]; 3277 break; 3278 3279 case UNUSED_FILESCOPED_DECLS: 3280 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3281 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I])); 3282 break; 3283 3284 case DELEGATING_CTORS: 3285 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3286 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I])); 3287 break; 3288 3289 case WEAK_UNDECLARED_IDENTIFIERS: 3290 if (Record.size() % 4 != 0) { 3291 Error("invalid weak identifiers record"); 3292 return Failure; 3293 } 3294 3295 // FIXME: Ignore weak undeclared identifiers from non-original PCH 3296 // files. This isn't the way to do it :) 3297 WeakUndeclaredIdentifiers.clear(); 3298 3299 // Translate the weak, undeclared identifiers into global IDs. 3300 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) { 3301 WeakUndeclaredIdentifiers.push_back( 3302 getGlobalIdentifierID(F, Record[I++])); 3303 WeakUndeclaredIdentifiers.push_back( 3304 getGlobalIdentifierID(F, Record[I++])); 3305 WeakUndeclaredIdentifiers.push_back( 3306 ReadSourceLocation(F, Record, I).getRawEncoding()); 3307 WeakUndeclaredIdentifiers.push_back(Record[I++]); 3308 } 3309 break; 3310 3311 case SELECTOR_OFFSETS: { 3312 F.SelectorOffsets = (const uint32_t *)Blob.data(); 3313 F.LocalNumSelectors = Record[0]; 3314 unsigned LocalBaseSelectorID = Record[1]; 3315 F.BaseSelectorID = getTotalNumSelectors(); 3316 3317 if (F.LocalNumSelectors > 0) { 3318 // Introduce the global -> local mapping for selectors within this 3319 // module. 3320 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F)); 3321 3322 // Introduce the local -> global mapping for selectors within this 3323 // module. 3324 F.SelectorRemap.insertOrReplace( 3325 std::make_pair(LocalBaseSelectorID, 3326 F.BaseSelectorID - LocalBaseSelectorID)); 3327 3328 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors); 3329 } 3330 break; 3331 } 3332 3333 case METHOD_POOL: 3334 F.SelectorLookupTableData = (const unsigned char *)Blob.data(); 3335 if (Record[0]) 3336 F.SelectorLookupTable 3337 = ASTSelectorLookupTable::Create( 3338 F.SelectorLookupTableData + Record[0], 3339 F.SelectorLookupTableData, 3340 ASTSelectorLookupTrait(*this, F)); 3341 TotalNumMethodPoolEntries += Record[1]; 3342 break; 3343 3344 case REFERENCED_SELECTOR_POOL: 3345 if (!Record.empty()) { 3346 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) { 3347 ReferencedSelectorsData.push_back(getGlobalSelectorID(F, 3348 Record[Idx++])); 3349 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx). 3350 getRawEncoding()); 3351 } 3352 } 3353 break; 3354 3355 case PP_CONDITIONAL_STACK: 3356 if (!Record.empty()) { 3357 unsigned Idx = 0, End = Record.size() - 1; 3358 bool ReachedEOFWhileSkipping = Record[Idx++]; 3359 llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo; 3360 if (ReachedEOFWhileSkipping) { 3361 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx); 3362 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx); 3363 bool FoundNonSkipPortion = Record[Idx++]; 3364 bool FoundElse = Record[Idx++]; 3365 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx); 3366 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion, 3367 FoundElse, ElseLoc); 3368 } 3369 SmallVector<PPConditionalInfo, 4> ConditionalStack; 3370 while (Idx < End) { 3371 auto Loc = ReadSourceLocation(F, Record, Idx); 3372 bool WasSkipping = Record[Idx++]; 3373 bool FoundNonSkip = Record[Idx++]; 3374 bool FoundElse = Record[Idx++]; 3375 ConditionalStack.push_back( 3376 {Loc, WasSkipping, FoundNonSkip, FoundElse}); 3377 } 3378 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo); 3379 } 3380 break; 3381 3382 case PP_COUNTER_VALUE: 3383 if (!Record.empty() && Listener) 3384 Listener->ReadCounter(F, Record[0]); 3385 break; 3386 3387 case FILE_SORTED_DECLS: 3388 F.FileSortedDecls = (const DeclID *)Blob.data(); 3389 F.NumFileSortedDecls = Record[0]; 3390 break; 3391 3392 case SOURCE_LOCATION_OFFSETS: { 3393 F.SLocEntryOffsets = (const uint32_t *)Blob.data(); 3394 F.LocalNumSLocEntries = Record[0]; 3395 unsigned SLocSpaceSize = Record[1]; 3396 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset; 3397 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) = 3398 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, 3399 SLocSpaceSize); 3400 if (!F.SLocEntryBaseID) { 3401 Error("ran out of source locations"); 3402 break; 3403 } 3404 // Make our entry in the range map. BaseID is negative and growing, so 3405 // we invert it. Because we invert it, though, we need the other end of 3406 // the range. 3407 unsigned RangeStart = 3408 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1; 3409 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F)); 3410 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset); 3411 3412 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing. 3413 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0); 3414 GlobalSLocOffsetMap.insert( 3415 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset 3416 - SLocSpaceSize,&F)); 3417 3418 // Initialize the remapping table. 3419 // Invalid stays invalid. 3420 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0)); 3421 // This module. Base was 2 when being compiled. 3422 F.SLocRemap.insertOrReplace(std::make_pair(2U, 3423 static_cast<int>(F.SLocEntryBaseOffset - 2))); 3424 3425 TotalNumSLocEntries += F.LocalNumSLocEntries; 3426 break; 3427 } 3428 3429 case MODULE_OFFSET_MAP: 3430 F.ModuleOffsetMap = Blob; 3431 break; 3432 3433 case SOURCE_MANAGER_LINE_TABLE: 3434 if (ParseLineTable(F, Record)) { 3435 Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file"); 3436 return Failure; 3437 } 3438 break; 3439 3440 case SOURCE_LOCATION_PRELOADS: { 3441 // Need to transform from the local view (1-based IDs) to the global view, 3442 // which is based off F.SLocEntryBaseID. 3443 if (!F.PreloadSLocEntries.empty()) { 3444 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file"); 3445 return Failure; 3446 } 3447 3448 F.PreloadSLocEntries.swap(Record); 3449 break; 3450 } 3451 3452 case EXT_VECTOR_DECLS: 3453 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3454 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I])); 3455 break; 3456 3457 case VTABLE_USES: 3458 if (Record.size() % 3 != 0) { 3459 Error("Invalid VTABLE_USES record"); 3460 return Failure; 3461 } 3462 3463 // Later tables overwrite earlier ones. 3464 // FIXME: Modules will have some trouble with this. This is clearly not 3465 // the right way to do this. 3466 VTableUses.clear(); 3467 3468 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) { 3469 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++])); 3470 VTableUses.push_back( 3471 ReadSourceLocation(F, Record, Idx).getRawEncoding()); 3472 VTableUses.push_back(Record[Idx++]); 3473 } 3474 break; 3475 3476 case PENDING_IMPLICIT_INSTANTIATIONS: 3477 if (PendingInstantiations.size() % 2 != 0) { 3478 Error("Invalid existing PendingInstantiations"); 3479 return Failure; 3480 } 3481 3482 if (Record.size() % 2 != 0) { 3483 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block"); 3484 return Failure; 3485 } 3486 3487 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3488 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++])); 3489 PendingInstantiations.push_back( 3490 ReadSourceLocation(F, Record, I).getRawEncoding()); 3491 } 3492 break; 3493 3494 case SEMA_DECL_REFS: 3495 if (Record.size() != 3) { 3496 Error("Invalid SEMA_DECL_REFS block"); 3497 return Failure; 3498 } 3499 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3500 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3501 break; 3502 3503 case PPD_ENTITIES_OFFSETS: { 3504 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data(); 3505 assert(Blob.size() % sizeof(PPEntityOffset) == 0); 3506 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset); 3507 3508 unsigned LocalBasePreprocessedEntityID = Record[0]; 3509 3510 unsigned StartingID; 3511 if (!PP.getPreprocessingRecord()) 3512 PP.createPreprocessingRecord(); 3513 if (!PP.getPreprocessingRecord()->getExternalSource()) 3514 PP.getPreprocessingRecord()->SetExternalSource(*this); 3515 StartingID 3516 = PP.getPreprocessingRecord() 3517 ->allocateLoadedEntities(F.NumPreprocessedEntities); 3518 F.BasePreprocessedEntityID = StartingID; 3519 3520 if (F.NumPreprocessedEntities > 0) { 3521 // Introduce the global -> local mapping for preprocessed entities in 3522 // this module. 3523 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F)); 3524 3525 // Introduce the local -> global mapping for preprocessed entities in 3526 // this module. 3527 F.PreprocessedEntityRemap.insertOrReplace( 3528 std::make_pair(LocalBasePreprocessedEntityID, 3529 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID)); 3530 } 3531 3532 break; 3533 } 3534 3535 case PPD_SKIPPED_RANGES: { 3536 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data(); 3537 assert(Blob.size() % sizeof(PPSkippedRange) == 0); 3538 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange); 3539 3540 if (!PP.getPreprocessingRecord()) 3541 PP.createPreprocessingRecord(); 3542 if (!PP.getPreprocessingRecord()->getExternalSource()) 3543 PP.getPreprocessingRecord()->SetExternalSource(*this); 3544 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord() 3545 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges); 3546 3547 if (F.NumPreprocessedSkippedRanges > 0) 3548 GlobalSkippedRangeMap.insert( 3549 std::make_pair(F.BasePreprocessedSkippedRangeID, &F)); 3550 break; 3551 } 3552 3553 case DECL_UPDATE_OFFSETS: 3554 if (Record.size() % 2 != 0) { 3555 Error("invalid DECL_UPDATE_OFFSETS block in AST file"); 3556 return Failure; 3557 } 3558 for (unsigned I = 0, N = Record.size(); I != N; I += 2) { 3559 GlobalDeclID ID = getGlobalDeclID(F, Record[I]); 3560 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1])); 3561 3562 // If we've already loaded the decl, perform the updates when we finish 3563 // loading this block. 3564 if (Decl *D = GetExistingDecl(ID)) 3565 PendingUpdateRecords.push_back( 3566 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3567 } 3568 break; 3569 3570 case OBJC_CATEGORIES_MAP: 3571 if (F.LocalNumObjCCategoriesInMap != 0) { 3572 Error("duplicate OBJC_CATEGORIES_MAP record in AST file"); 3573 return Failure; 3574 } 3575 3576 F.LocalNumObjCCategoriesInMap = Record[0]; 3577 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data(); 3578 break; 3579 3580 case OBJC_CATEGORIES: 3581 F.ObjCCategories.swap(Record); 3582 break; 3583 3584 case CUDA_SPECIAL_DECL_REFS: 3585 // Later tables overwrite earlier ones. 3586 // FIXME: Modules will have trouble with this. 3587 CUDASpecialDeclRefs.clear(); 3588 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3589 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3590 break; 3591 3592 case HEADER_SEARCH_TABLE: 3593 F.HeaderFileInfoTableData = Blob.data(); 3594 F.LocalNumHeaderFileInfos = Record[1]; 3595 if (Record[0]) { 3596 F.HeaderFileInfoTable 3597 = HeaderFileInfoLookupTable::Create( 3598 (const unsigned char *)F.HeaderFileInfoTableData + Record[0], 3599 (const unsigned char *)F.HeaderFileInfoTableData, 3600 HeaderFileInfoTrait(*this, F, 3601 &PP.getHeaderSearchInfo(), 3602 Blob.data() + Record[2])); 3603 3604 PP.getHeaderSearchInfo().SetExternalSource(this); 3605 if (!PP.getHeaderSearchInfo().getExternalLookup()) 3606 PP.getHeaderSearchInfo().SetExternalLookup(this); 3607 } 3608 break; 3609 3610 case FP_PRAGMA_OPTIONS: 3611 // Later tables overwrite earlier ones. 3612 FPPragmaOptions.swap(Record); 3613 break; 3614 3615 case OPENCL_EXTENSIONS: 3616 for (unsigned I = 0, E = Record.size(); I != E; ) { 3617 auto Name = ReadString(Record, I); 3618 auto &OptInfo = OpenCLExtensions.OptMap[Name]; 3619 OptInfo.Supported = Record[I++] != 0; 3620 OptInfo.Enabled = Record[I++] != 0; 3621 OptInfo.Avail = Record[I++]; 3622 OptInfo.Core = Record[I++]; 3623 OptInfo.Opt = Record[I++]; 3624 } 3625 break; 3626 3627 case OPENCL_EXTENSION_TYPES: 3628 for (unsigned I = 0, E = Record.size(); I != E;) { 3629 auto TypeID = static_cast<::TypeID>(Record[I++]); 3630 auto *Type = GetType(TypeID).getTypePtr(); 3631 auto NumExt = static_cast<unsigned>(Record[I++]); 3632 for (unsigned II = 0; II != NumExt; ++II) { 3633 auto Ext = ReadString(Record, I); 3634 OpenCLTypeExtMap[Type].insert(Ext); 3635 } 3636 } 3637 break; 3638 3639 case OPENCL_EXTENSION_DECLS: 3640 for (unsigned I = 0, E = Record.size(); I != E;) { 3641 auto DeclID = static_cast<::DeclID>(Record[I++]); 3642 auto *Decl = GetDecl(DeclID); 3643 auto NumExt = static_cast<unsigned>(Record[I++]); 3644 for (unsigned II = 0; II != NumExt; ++II) { 3645 auto Ext = ReadString(Record, I); 3646 OpenCLDeclExtMap[Decl].insert(Ext); 3647 } 3648 } 3649 break; 3650 3651 case TENTATIVE_DEFINITIONS: 3652 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3653 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I])); 3654 break; 3655 3656 case KNOWN_NAMESPACES: 3657 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3658 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I])); 3659 break; 3660 3661 case UNDEFINED_BUT_USED: 3662 if (UndefinedButUsed.size() % 2 != 0) { 3663 Error("Invalid existing UndefinedButUsed"); 3664 return Failure; 3665 } 3666 3667 if (Record.size() % 2 != 0) { 3668 Error("invalid undefined-but-used record"); 3669 return Failure; 3670 } 3671 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3672 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++])); 3673 UndefinedButUsed.push_back( 3674 ReadSourceLocation(F, Record, I).getRawEncoding()); 3675 } 3676 break; 3677 3678 case DELETE_EXPRS_TO_ANALYZE: 3679 for (unsigned I = 0, N = Record.size(); I != N;) { 3680 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++])); 3681 const uint64_t Count = Record[I++]; 3682 DelayedDeleteExprs.push_back(Count); 3683 for (uint64_t C = 0; C < Count; ++C) { 3684 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding()); 3685 bool IsArrayForm = Record[I++] == 1; 3686 DelayedDeleteExprs.push_back(IsArrayForm); 3687 } 3688 } 3689 break; 3690 3691 case IMPORTED_MODULES: 3692 if (!F.isModule()) { 3693 // If we aren't loading a module (which has its own exports), make 3694 // all of the imported modules visible. 3695 // FIXME: Deal with macros-only imports. 3696 for (unsigned I = 0, N = Record.size(); I != N; /**/) { 3697 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]); 3698 SourceLocation Loc = ReadSourceLocation(F, Record, I); 3699 if (GlobalID) { 3700 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc)); 3701 if (DeserializationListener) 3702 DeserializationListener->ModuleImportRead(GlobalID, Loc); 3703 } 3704 } 3705 } 3706 break; 3707 3708 case MACRO_OFFSET: { 3709 if (F.LocalNumMacros != 0) { 3710 Error("duplicate MACRO_OFFSET record in AST file"); 3711 return Failure; 3712 } 3713 F.MacroOffsets = (const uint32_t *)Blob.data(); 3714 F.LocalNumMacros = Record[0]; 3715 unsigned LocalBaseMacroID = Record[1]; 3716 F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset; 3717 F.BaseMacroID = getTotalNumMacros(); 3718 3719 if (F.LocalNumMacros > 0) { 3720 // Introduce the global -> local mapping for macros within this module. 3721 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F)); 3722 3723 // Introduce the local -> global mapping for macros within this module. 3724 F.MacroRemap.insertOrReplace( 3725 std::make_pair(LocalBaseMacroID, 3726 F.BaseMacroID - LocalBaseMacroID)); 3727 3728 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros); 3729 } 3730 break; 3731 } 3732 3733 case LATE_PARSED_TEMPLATE: 3734 LateParsedTemplates.emplace_back( 3735 std::piecewise_construct, std::forward_as_tuple(&F), 3736 std::forward_as_tuple(Record.begin(), Record.end())); 3737 break; 3738 3739 case OPTIMIZE_PRAGMA_OPTIONS: 3740 if (Record.size() != 1) { 3741 Error("invalid pragma optimize record"); 3742 return Failure; 3743 } 3744 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]); 3745 break; 3746 3747 case MSSTRUCT_PRAGMA_OPTIONS: 3748 if (Record.size() != 1) { 3749 Error("invalid pragma ms_struct record"); 3750 return Failure; 3751 } 3752 PragmaMSStructState = Record[0]; 3753 break; 3754 3755 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS: 3756 if (Record.size() != 2) { 3757 Error("invalid pragma ms_struct record"); 3758 return Failure; 3759 } 3760 PragmaMSPointersToMembersState = Record[0]; 3761 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]); 3762 break; 3763 3764 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES: 3765 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3766 UnusedLocalTypedefNameCandidates.push_back( 3767 getGlobalDeclID(F, Record[I])); 3768 break; 3769 3770 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH: 3771 if (Record.size() != 1) { 3772 Error("invalid cuda pragma options record"); 3773 return Failure; 3774 } 3775 ForceCUDAHostDeviceDepth = Record[0]; 3776 break; 3777 3778 case ALIGN_PACK_PRAGMA_OPTIONS: { 3779 if (Record.size() < 3) { 3780 Error("invalid pragma pack record"); 3781 return Failure; 3782 } 3783 PragmaAlignPackCurrentValue = ReadAlignPackInfo(Record[0]); 3784 PragmaAlignPackCurrentLocation = ReadSourceLocation(F, Record[1]); 3785 unsigned NumStackEntries = Record[2]; 3786 unsigned Idx = 3; 3787 // Reset the stack when importing a new module. 3788 PragmaAlignPackStack.clear(); 3789 for (unsigned I = 0; I < NumStackEntries; ++I) { 3790 PragmaAlignPackStackEntry Entry; 3791 Entry.Value = ReadAlignPackInfo(Record[Idx++]); 3792 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3793 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3794 PragmaAlignPackStrings.push_back(ReadString(Record, Idx)); 3795 Entry.SlotLabel = PragmaAlignPackStrings.back(); 3796 PragmaAlignPackStack.push_back(Entry); 3797 } 3798 break; 3799 } 3800 3801 case FLOAT_CONTROL_PRAGMA_OPTIONS: { 3802 if (Record.size() < 3) { 3803 Error("invalid pragma pack record"); 3804 return Failure; 3805 } 3806 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]); 3807 FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]); 3808 unsigned NumStackEntries = Record[2]; 3809 unsigned Idx = 3; 3810 // Reset the stack when importing a new module. 3811 FpPragmaStack.clear(); 3812 for (unsigned I = 0; I < NumStackEntries; ++I) { 3813 FpPragmaStackEntry Entry; 3814 Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]); 3815 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3816 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3817 FpPragmaStrings.push_back(ReadString(Record, Idx)); 3818 Entry.SlotLabel = FpPragmaStrings.back(); 3819 FpPragmaStack.push_back(Entry); 3820 } 3821 break; 3822 } 3823 3824 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS: 3825 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3826 DeclsToCheckForDeferredDiags.push_back(getGlobalDeclID(F, Record[I])); 3827 break; 3828 } 3829 } 3830 } 3831 3832 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const { 3833 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read"); 3834 3835 // Additional remapping information. 3836 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data(); 3837 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size(); 3838 F.ModuleOffsetMap = StringRef(); 3839 3840 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders. 3841 if (F.SLocRemap.find(0) == F.SLocRemap.end()) { 3842 F.SLocRemap.insert(std::make_pair(0U, 0)); 3843 F.SLocRemap.insert(std::make_pair(2U, 1)); 3844 } 3845 3846 // Continuous range maps we may be updating in our module. 3847 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder; 3848 RemapBuilder SLocRemap(F.SLocRemap); 3849 RemapBuilder IdentifierRemap(F.IdentifierRemap); 3850 RemapBuilder MacroRemap(F.MacroRemap); 3851 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap); 3852 RemapBuilder SubmoduleRemap(F.SubmoduleRemap); 3853 RemapBuilder SelectorRemap(F.SelectorRemap); 3854 RemapBuilder DeclRemap(F.DeclRemap); 3855 RemapBuilder TypeRemap(F.TypeRemap); 3856 3857 while (Data < DataEnd) { 3858 // FIXME: Looking up dependency modules by filename is horrible. Let's 3859 // start fixing this with prebuilt, explicit and implicit modules and see 3860 // how it goes... 3861 using namespace llvm::support; 3862 ModuleKind Kind = static_cast<ModuleKind>( 3863 endian::readNext<uint8_t, little, unaligned>(Data)); 3864 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data); 3865 StringRef Name = StringRef((const char*)Data, Len); 3866 Data += Len; 3867 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule || 3868 Kind == MK_ImplicitModule 3869 ? ModuleMgr.lookupByModuleName(Name) 3870 : ModuleMgr.lookupByFileName(Name)); 3871 if (!OM) { 3872 std::string Msg = 3873 "SourceLocation remap refers to unknown module, cannot find "; 3874 Msg.append(std::string(Name)); 3875 Error(Msg); 3876 return; 3877 } 3878 3879 uint32_t SLocOffset = 3880 endian::readNext<uint32_t, little, unaligned>(Data); 3881 uint32_t IdentifierIDOffset = 3882 endian::readNext<uint32_t, little, unaligned>(Data); 3883 uint32_t MacroIDOffset = 3884 endian::readNext<uint32_t, little, unaligned>(Data); 3885 uint32_t PreprocessedEntityIDOffset = 3886 endian::readNext<uint32_t, little, unaligned>(Data); 3887 uint32_t SubmoduleIDOffset = 3888 endian::readNext<uint32_t, little, unaligned>(Data); 3889 uint32_t SelectorIDOffset = 3890 endian::readNext<uint32_t, little, unaligned>(Data); 3891 uint32_t DeclIDOffset = 3892 endian::readNext<uint32_t, little, unaligned>(Data); 3893 uint32_t TypeIndexOffset = 3894 endian::readNext<uint32_t, little, unaligned>(Data); 3895 3896 uint32_t None = std::numeric_limits<uint32_t>::max(); 3897 3898 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset, 3899 RemapBuilder &Remap) { 3900 if (Offset != None) 3901 Remap.insert(std::make_pair(Offset, 3902 static_cast<int>(BaseOffset - Offset))); 3903 }; 3904 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap); 3905 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap); 3906 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap); 3907 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID, 3908 PreprocessedEntityRemap); 3909 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap); 3910 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap); 3911 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap); 3912 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap); 3913 3914 // Global -> local mappings. 3915 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset; 3916 } 3917 } 3918 3919 ASTReader::ASTReadResult 3920 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F, 3921 const ModuleFile *ImportedBy, 3922 unsigned ClientLoadCapabilities) { 3923 unsigned Idx = 0; 3924 F.ModuleMapPath = ReadPath(F, Record, Idx); 3925 3926 // Try to resolve ModuleName in the current header search context and 3927 // verify that it is found in the same module map file as we saved. If the 3928 // top-level AST file is a main file, skip this check because there is no 3929 // usable header search context. 3930 assert(!F.ModuleName.empty() && 3931 "MODULE_NAME should come before MODULE_MAP_FILE"); 3932 if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) { 3933 // An implicitly-loaded module file should have its module listed in some 3934 // module map file that we've already loaded. 3935 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName); 3936 auto &Map = PP.getHeaderSearchInfo().getModuleMap(); 3937 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr; 3938 // Don't emit module relocation error if we have -fno-validate-pch 3939 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation & 3940 DisableValidationForModuleKind::Module) && 3941 !ModMap) { 3942 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) { 3943 if (auto ASTFE = M ? M->getASTFile() : None) { 3944 // This module was defined by an imported (explicit) module. 3945 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName 3946 << ASTFE->getName(); 3947 } else { 3948 // This module was built with a different module map. 3949 Diag(diag::err_imported_module_not_found) 3950 << F.ModuleName << F.FileName 3951 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath 3952 << !ImportedBy; 3953 // In case it was imported by a PCH, there's a chance the user is 3954 // just missing to include the search path to the directory containing 3955 // the modulemap. 3956 if (ImportedBy && ImportedBy->Kind == MK_PCH) 3957 Diag(diag::note_imported_by_pch_module_not_found) 3958 << llvm::sys::path::parent_path(F.ModuleMapPath); 3959 } 3960 } 3961 return OutOfDate; 3962 } 3963 3964 assert(M && M->Name == F.ModuleName && "found module with different name"); 3965 3966 // Check the primary module map file. 3967 auto StoredModMap = FileMgr.getFile(F.ModuleMapPath); 3968 if (!StoredModMap || *StoredModMap != ModMap) { 3969 assert(ModMap && "found module is missing module map file"); 3970 assert((ImportedBy || F.Kind == MK_ImplicitModule) && 3971 "top-level import should be verified"); 3972 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy; 3973 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3974 Diag(diag::err_imported_module_modmap_changed) 3975 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName) 3976 << ModMap->getName() << F.ModuleMapPath << NotImported; 3977 return OutOfDate; 3978 } 3979 3980 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps; 3981 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) { 3982 // FIXME: we should use input files rather than storing names. 3983 std::string Filename = ReadPath(F, Record, Idx); 3984 auto F = FileMgr.getFile(Filename, false, false); 3985 if (!F) { 3986 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3987 Error("could not find file '" + Filename +"' referenced by AST file"); 3988 return OutOfDate; 3989 } 3990 AdditionalStoredMaps.insert(*F); 3991 } 3992 3993 // Check any additional module map files (e.g. module.private.modulemap) 3994 // that are not in the pcm. 3995 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) { 3996 for (const FileEntry *ModMap : *AdditionalModuleMaps) { 3997 // Remove files that match 3998 // Note: SmallPtrSet::erase is really remove 3999 if (!AdditionalStoredMaps.erase(ModMap)) { 4000 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 4001 Diag(diag::err_module_different_modmap) 4002 << F.ModuleName << /*new*/0 << ModMap->getName(); 4003 return OutOfDate; 4004 } 4005 } 4006 } 4007 4008 // Check any additional module map files that are in the pcm, but not 4009 // found in header search. Cases that match are already removed. 4010 for (const FileEntry *ModMap : AdditionalStoredMaps) { 4011 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 4012 Diag(diag::err_module_different_modmap) 4013 << F.ModuleName << /*not new*/1 << ModMap->getName(); 4014 return OutOfDate; 4015 } 4016 } 4017 4018 if (Listener) 4019 Listener->ReadModuleMapFile(F.ModuleMapPath); 4020 return Success; 4021 } 4022 4023 /// Move the given method to the back of the global list of methods. 4024 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) { 4025 // Find the entry for this selector in the method pool. 4026 Sema::GlobalMethodPool::iterator Known 4027 = S.MethodPool.find(Method->getSelector()); 4028 if (Known == S.MethodPool.end()) 4029 return; 4030 4031 // Retrieve the appropriate method list. 4032 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first 4033 : Known->second.second; 4034 bool Found = false; 4035 for (ObjCMethodList *List = &Start; List; List = List->getNext()) { 4036 if (!Found) { 4037 if (List->getMethod() == Method) { 4038 Found = true; 4039 } else { 4040 // Keep searching. 4041 continue; 4042 } 4043 } 4044 4045 if (List->getNext()) 4046 List->setMethod(List->getNext()->getMethod()); 4047 else 4048 List->setMethod(Method); 4049 } 4050 } 4051 4052 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) { 4053 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?"); 4054 for (Decl *D : Names) { 4055 bool wasHidden = !D->isUnconditionallyVisible(); 4056 D->setVisibleDespiteOwningModule(); 4057 4058 if (wasHidden && SemaObj) { 4059 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) { 4060 moveMethodToBackOfGlobalList(*SemaObj, Method); 4061 } 4062 } 4063 } 4064 } 4065 4066 void ASTReader::makeModuleVisible(Module *Mod, 4067 Module::NameVisibilityKind NameVisibility, 4068 SourceLocation ImportLoc) { 4069 llvm::SmallPtrSet<Module *, 4> Visited; 4070 SmallVector<Module *, 4> Stack; 4071 Stack.push_back(Mod); 4072 while (!Stack.empty()) { 4073 Mod = Stack.pop_back_val(); 4074 4075 if (NameVisibility <= Mod->NameVisibility) { 4076 // This module already has this level of visibility (or greater), so 4077 // there is nothing more to do. 4078 continue; 4079 } 4080 4081 if (Mod->isUnimportable()) { 4082 // Modules that aren't importable cannot be made visible. 4083 continue; 4084 } 4085 4086 // Update the module's name visibility. 4087 Mod->NameVisibility = NameVisibility; 4088 4089 // If we've already deserialized any names from this module, 4090 // mark them as visible. 4091 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod); 4092 if (Hidden != HiddenNamesMap.end()) { 4093 auto HiddenNames = std::move(*Hidden); 4094 HiddenNamesMap.erase(Hidden); 4095 makeNamesVisible(HiddenNames.second, HiddenNames.first); 4096 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() && 4097 "making names visible added hidden names"); 4098 } 4099 4100 // Push any exported modules onto the stack to be marked as visible. 4101 SmallVector<Module *, 16> Exports; 4102 Mod->getExportedModules(Exports); 4103 for (SmallVectorImpl<Module *>::iterator 4104 I = Exports.begin(), E = Exports.end(); I != E; ++I) { 4105 Module *Exported = *I; 4106 if (Visited.insert(Exported).second) 4107 Stack.push_back(Exported); 4108 } 4109 } 4110 } 4111 4112 /// We've merged the definition \p MergedDef into the existing definition 4113 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made 4114 /// visible. 4115 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def, 4116 NamedDecl *MergedDef) { 4117 if (!Def->isUnconditionallyVisible()) { 4118 // If MergedDef is visible or becomes visible, make the definition visible. 4119 if (MergedDef->isUnconditionallyVisible()) 4120 Def->setVisibleDespiteOwningModule(); 4121 else { 4122 getContext().mergeDefinitionIntoModule( 4123 Def, MergedDef->getImportedOwningModule(), 4124 /*NotifyListeners*/ false); 4125 PendingMergedDefinitionsToDeduplicate.insert(Def); 4126 } 4127 } 4128 } 4129 4130 bool ASTReader::loadGlobalIndex() { 4131 if (GlobalIndex) 4132 return false; 4133 4134 if (TriedLoadingGlobalIndex || !UseGlobalIndex || 4135 !PP.getLangOpts().Modules) 4136 return true; 4137 4138 // Try to load the global index. 4139 TriedLoadingGlobalIndex = true; 4140 StringRef ModuleCachePath 4141 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath(); 4142 std::pair<GlobalModuleIndex *, llvm::Error> Result = 4143 GlobalModuleIndex::readIndex(ModuleCachePath); 4144 if (llvm::Error Err = std::move(Result.second)) { 4145 assert(!Result.first); 4146 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 4147 return true; 4148 } 4149 4150 GlobalIndex.reset(Result.first); 4151 ModuleMgr.setGlobalIndex(GlobalIndex.get()); 4152 return false; 4153 } 4154 4155 bool ASTReader::isGlobalIndexUnavailable() const { 4156 return PP.getLangOpts().Modules && UseGlobalIndex && 4157 !hasGlobalIndex() && TriedLoadingGlobalIndex; 4158 } 4159 4160 static void updateModuleTimestamp(ModuleFile &MF) { 4161 // Overwrite the timestamp file contents so that file's mtime changes. 4162 std::string TimestampFilename = MF.getTimestampFilename(); 4163 std::error_code EC; 4164 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text); 4165 if (EC) 4166 return; 4167 OS << "Timestamp file\n"; 4168 OS.close(); 4169 OS.clear_error(); // Avoid triggering a fatal error. 4170 } 4171 4172 /// Given a cursor at the start of an AST file, scan ahead and drop the 4173 /// cursor into the start of the given block ID, returning false on success and 4174 /// true on failure. 4175 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) { 4176 while (true) { 4177 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 4178 if (!MaybeEntry) { 4179 // FIXME this drops errors on the floor. 4180 consumeError(MaybeEntry.takeError()); 4181 return true; 4182 } 4183 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4184 4185 switch (Entry.Kind) { 4186 case llvm::BitstreamEntry::Error: 4187 case llvm::BitstreamEntry::EndBlock: 4188 return true; 4189 4190 case llvm::BitstreamEntry::Record: 4191 // Ignore top-level records. 4192 if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID)) 4193 break; 4194 else { 4195 // FIXME this drops errors on the floor. 4196 consumeError(Skipped.takeError()); 4197 return true; 4198 } 4199 4200 case llvm::BitstreamEntry::SubBlock: 4201 if (Entry.ID == BlockID) { 4202 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) { 4203 // FIXME this drops the error on the floor. 4204 consumeError(std::move(Err)); 4205 return true; 4206 } 4207 // Found it! 4208 return false; 4209 } 4210 4211 if (llvm::Error Err = Cursor.SkipBlock()) { 4212 // FIXME this drops the error on the floor. 4213 consumeError(std::move(Err)); 4214 return true; 4215 } 4216 } 4217 } 4218 } 4219 4220 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, 4221 ModuleKind Type, 4222 SourceLocation ImportLoc, 4223 unsigned ClientLoadCapabilities, 4224 SmallVectorImpl<ImportedSubmodule> *Imported) { 4225 llvm::SaveAndRestore<SourceLocation> 4226 SetCurImportLocRAII(CurrentImportLoc, ImportLoc); 4227 llvm::SaveAndRestore<Optional<ModuleKind>> SetCurModuleKindRAII( 4228 CurrentDeserializingModuleKind, Type); 4229 4230 // Defer any pending actions until we get to the end of reading the AST file. 4231 Deserializing AnASTFile(this); 4232 4233 // Bump the generation number. 4234 unsigned PreviousGeneration = 0; 4235 if (ContextObj) 4236 PreviousGeneration = incrementGeneration(*ContextObj); 4237 4238 unsigned NumModules = ModuleMgr.size(); 4239 auto removeModulesAndReturn = [&](ASTReadResult ReadResult) { 4240 assert(ReadResult && "expected to return error"); 4241 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, 4242 PP.getLangOpts().Modules 4243 ? &PP.getHeaderSearchInfo().getModuleMap() 4244 : nullptr); 4245 4246 // If we find that any modules are unusable, the global index is going 4247 // to be out-of-date. Just remove it. 4248 GlobalIndex.reset(); 4249 ModuleMgr.setGlobalIndex(nullptr); 4250 return ReadResult; 4251 }; 4252 4253 SmallVector<ImportedModule, 4> Loaded; 4254 switch (ASTReadResult ReadResult = 4255 ReadASTCore(FileName, Type, ImportLoc, 4256 /*ImportedBy=*/nullptr, Loaded, 0, 0, 4257 ASTFileSignature(), ClientLoadCapabilities)) { 4258 case Failure: 4259 case Missing: 4260 case OutOfDate: 4261 case VersionMismatch: 4262 case ConfigurationMismatch: 4263 case HadErrors: 4264 return removeModulesAndReturn(ReadResult); 4265 case Success: 4266 break; 4267 } 4268 4269 // Here comes stuff that we only do once the entire chain is loaded. 4270 4271 // Load the AST blocks of all of the modules that we loaded. We can still 4272 // hit errors parsing the ASTs at this point. 4273 for (ImportedModule &M : Loaded) { 4274 ModuleFile &F = *M.Mod; 4275 4276 // Read the AST block. 4277 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities)) 4278 return removeModulesAndReturn(Result); 4279 4280 // The AST block should always have a definition for the main module. 4281 if (F.isModule() && !F.DidReadTopLevelSubmodule) { 4282 Error(diag::err_module_file_missing_top_level_submodule, F.FileName); 4283 return removeModulesAndReturn(Failure); 4284 } 4285 4286 // Read the extension blocks. 4287 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) { 4288 if (ASTReadResult Result = ReadExtensionBlock(F)) 4289 return removeModulesAndReturn(Result); 4290 } 4291 4292 // Once read, set the ModuleFile bit base offset and update the size in 4293 // bits of all files we've seen. 4294 F.GlobalBitOffset = TotalModulesSizeInBits; 4295 TotalModulesSizeInBits += F.SizeInBits; 4296 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F)); 4297 } 4298 4299 // Preload source locations and interesting indentifiers. 4300 for (ImportedModule &M : Loaded) { 4301 ModuleFile &F = *M.Mod; 4302 4303 // Preload SLocEntries. 4304 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) { 4305 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID; 4306 // Load it through the SourceManager and don't call ReadSLocEntry() 4307 // directly because the entry may have already been loaded in which case 4308 // calling ReadSLocEntry() directly would trigger an assertion in 4309 // SourceManager. 4310 SourceMgr.getLoadedSLocEntryByID(Index); 4311 } 4312 4313 // Map the original source file ID into the ID space of the current 4314 // compilation. 4315 if (F.OriginalSourceFileID.isValid()) { 4316 F.OriginalSourceFileID = FileID::get( 4317 F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1); 4318 } 4319 4320 // Preload all the pending interesting identifiers by marking them out of 4321 // date. 4322 for (auto Offset : F.PreloadIdentifierOffsets) { 4323 const unsigned char *Data = reinterpret_cast<const unsigned char *>( 4324 F.IdentifierTableData + Offset); 4325 4326 ASTIdentifierLookupTrait Trait(*this, F); 4327 auto KeyDataLen = Trait.ReadKeyDataLength(Data); 4328 auto Key = Trait.ReadKey(Data, KeyDataLen.first); 4329 auto &II = PP.getIdentifierTable().getOwn(Key); 4330 II.setOutOfDate(true); 4331 4332 // Mark this identifier as being from an AST file so that we can track 4333 // whether we need to serialize it. 4334 markIdentifierFromAST(*this, II); 4335 4336 // Associate the ID with the identifier so that the writer can reuse it. 4337 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first); 4338 SetIdentifierInfo(ID, &II); 4339 } 4340 } 4341 4342 // Setup the import locations and notify the module manager that we've 4343 // committed to these module files. 4344 for (ImportedModule &M : Loaded) { 4345 ModuleFile &F = *M.Mod; 4346 4347 ModuleMgr.moduleFileAccepted(&F); 4348 4349 // Set the import location. 4350 F.DirectImportLoc = ImportLoc; 4351 // FIXME: We assume that locations from PCH / preamble do not need 4352 // any translation. 4353 if (!M.ImportedBy) 4354 F.ImportLoc = M.ImportLoc; 4355 else 4356 F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc); 4357 } 4358 4359 if (!PP.getLangOpts().CPlusPlus || 4360 (Type != MK_ImplicitModule && Type != MK_ExplicitModule && 4361 Type != MK_PrebuiltModule)) { 4362 // Mark all of the identifiers in the identifier table as being out of date, 4363 // so that various accessors know to check the loaded modules when the 4364 // identifier is used. 4365 // 4366 // For C++ modules, we don't need information on many identifiers (just 4367 // those that provide macros or are poisoned), so we mark all of 4368 // the interesting ones via PreloadIdentifierOffsets. 4369 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(), 4370 IdEnd = PP.getIdentifierTable().end(); 4371 Id != IdEnd; ++Id) 4372 Id->second->setOutOfDate(true); 4373 } 4374 // Mark selectors as out of date. 4375 for (auto Sel : SelectorGeneration) 4376 SelectorOutOfDate[Sel.first] = true; 4377 4378 // Resolve any unresolved module exports. 4379 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) { 4380 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I]; 4381 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID); 4382 Module *ResolvedMod = getSubmodule(GlobalID); 4383 4384 switch (Unresolved.Kind) { 4385 case UnresolvedModuleRef::Conflict: 4386 if (ResolvedMod) { 4387 Module::Conflict Conflict; 4388 Conflict.Other = ResolvedMod; 4389 Conflict.Message = Unresolved.String.str(); 4390 Unresolved.Mod->Conflicts.push_back(Conflict); 4391 } 4392 continue; 4393 4394 case UnresolvedModuleRef::Import: 4395 if (ResolvedMod) 4396 Unresolved.Mod->Imports.insert(ResolvedMod); 4397 continue; 4398 4399 case UnresolvedModuleRef::Export: 4400 if (ResolvedMod || Unresolved.IsWildcard) 4401 Unresolved.Mod->Exports.push_back( 4402 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard)); 4403 continue; 4404 } 4405 } 4406 UnresolvedModuleRefs.clear(); 4407 4408 if (Imported) 4409 Imported->append(ImportedModules.begin(), 4410 ImportedModules.end()); 4411 4412 // FIXME: How do we load the 'use'd modules? They may not be submodules. 4413 // Might be unnecessary as use declarations are only used to build the 4414 // module itself. 4415 4416 if (ContextObj) 4417 InitializeContext(); 4418 4419 if (SemaObj) 4420 UpdateSema(); 4421 4422 if (DeserializationListener) 4423 DeserializationListener->ReaderInitialized(this); 4424 4425 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule(); 4426 if (PrimaryModule.OriginalSourceFileID.isValid()) { 4427 // If this AST file is a precompiled preamble, then set the 4428 // preamble file ID of the source manager to the file source file 4429 // from which the preamble was built. 4430 if (Type == MK_Preamble) { 4431 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID); 4432 } else if (Type == MK_MainFile) { 4433 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID); 4434 } 4435 } 4436 4437 // For any Objective-C class definitions we have already loaded, make sure 4438 // that we load any additional categories. 4439 if (ContextObj) { 4440 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) { 4441 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(), 4442 ObjCClassesLoaded[I], 4443 PreviousGeneration); 4444 } 4445 } 4446 4447 if (PP.getHeaderSearchInfo() 4448 .getHeaderSearchOpts() 4449 .ModulesValidateOncePerBuildSession) { 4450 // Now we are certain that the module and all modules it depends on are 4451 // up to date. Create or update timestamp files for modules that are 4452 // located in the module cache (not for PCH files that could be anywhere 4453 // in the filesystem). 4454 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) { 4455 ImportedModule &M = Loaded[I]; 4456 if (M.Mod->Kind == MK_ImplicitModule) { 4457 updateModuleTimestamp(*M.Mod); 4458 } 4459 } 4460 } 4461 4462 return Success; 4463 } 4464 4465 static ASTFileSignature readASTFileSignature(StringRef PCH); 4466 4467 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'. 4468 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) { 4469 // FIXME checking magic headers is done in other places such as 4470 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't 4471 // always done the same. Unify it all with a helper. 4472 if (!Stream.canSkipToPos(4)) 4473 return llvm::createStringError(std::errc::illegal_byte_sequence, 4474 "file too small to contain AST file magic"); 4475 for (unsigned C : {'C', 'P', 'C', 'H'}) 4476 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) { 4477 if (Res.get() != C) 4478 return llvm::createStringError( 4479 std::errc::illegal_byte_sequence, 4480 "file doesn't start with AST file magic"); 4481 } else 4482 return Res.takeError(); 4483 return llvm::Error::success(); 4484 } 4485 4486 static unsigned moduleKindForDiagnostic(ModuleKind Kind) { 4487 switch (Kind) { 4488 case MK_PCH: 4489 return 0; // PCH 4490 case MK_ImplicitModule: 4491 case MK_ExplicitModule: 4492 case MK_PrebuiltModule: 4493 return 1; // module 4494 case MK_MainFile: 4495 case MK_Preamble: 4496 return 2; // main source file 4497 } 4498 llvm_unreachable("unknown module kind"); 4499 } 4500 4501 ASTReader::ASTReadResult 4502 ASTReader::ReadASTCore(StringRef FileName, 4503 ModuleKind Type, 4504 SourceLocation ImportLoc, 4505 ModuleFile *ImportedBy, 4506 SmallVectorImpl<ImportedModule> &Loaded, 4507 off_t ExpectedSize, time_t ExpectedModTime, 4508 ASTFileSignature ExpectedSignature, 4509 unsigned ClientLoadCapabilities) { 4510 ModuleFile *M; 4511 std::string ErrorStr; 4512 ModuleManager::AddModuleResult AddResult 4513 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy, 4514 getGeneration(), ExpectedSize, ExpectedModTime, 4515 ExpectedSignature, readASTFileSignature, 4516 M, ErrorStr); 4517 4518 switch (AddResult) { 4519 case ModuleManager::AlreadyLoaded: 4520 Diag(diag::remark_module_import) 4521 << M->ModuleName << M->FileName << (ImportedBy ? true : false) 4522 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 4523 return Success; 4524 4525 case ModuleManager::NewlyLoaded: 4526 // Load module file below. 4527 break; 4528 4529 case ModuleManager::Missing: 4530 // The module file was missing; if the client can handle that, return 4531 // it. 4532 if (ClientLoadCapabilities & ARR_Missing) 4533 return Missing; 4534 4535 // Otherwise, return an error. 4536 Diag(diag::err_ast_file_not_found) 4537 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty() 4538 << ErrorStr; 4539 return Failure; 4540 4541 case ModuleManager::OutOfDate: 4542 // We couldn't load the module file because it is out-of-date. If the 4543 // client can handle out-of-date, return it. 4544 if (ClientLoadCapabilities & ARR_OutOfDate) 4545 return OutOfDate; 4546 4547 // Otherwise, return an error. 4548 Diag(diag::err_ast_file_out_of_date) 4549 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty() 4550 << ErrorStr; 4551 return Failure; 4552 } 4553 4554 assert(M && "Missing module file"); 4555 4556 bool ShouldFinalizePCM = false; 4557 auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() { 4558 auto &MC = getModuleManager().getModuleCache(); 4559 if (ShouldFinalizePCM) 4560 MC.finalizePCM(FileName); 4561 else 4562 MC.tryToDropPCM(FileName); 4563 }); 4564 ModuleFile &F = *M; 4565 BitstreamCursor &Stream = F.Stream; 4566 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer)); 4567 F.SizeInBits = F.Buffer->getBufferSize() * 8; 4568 4569 // Sniff for the signature. 4570 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4571 Diag(diag::err_ast_file_invalid) 4572 << moduleKindForDiagnostic(Type) << FileName << std::move(Err); 4573 return Failure; 4574 } 4575 4576 // This is used for compatibility with older PCH formats. 4577 bool HaveReadControlBlock = false; 4578 while (true) { 4579 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4580 if (!MaybeEntry) { 4581 Error(MaybeEntry.takeError()); 4582 return Failure; 4583 } 4584 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4585 4586 switch (Entry.Kind) { 4587 case llvm::BitstreamEntry::Error: 4588 case llvm::BitstreamEntry::Record: 4589 case llvm::BitstreamEntry::EndBlock: 4590 Error("invalid record at top-level of AST file"); 4591 return Failure; 4592 4593 case llvm::BitstreamEntry::SubBlock: 4594 break; 4595 } 4596 4597 switch (Entry.ID) { 4598 case CONTROL_BLOCK_ID: 4599 HaveReadControlBlock = true; 4600 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) { 4601 case Success: 4602 // Check that we didn't try to load a non-module AST file as a module. 4603 // 4604 // FIXME: Should we also perform the converse check? Loading a module as 4605 // a PCH file sort of works, but it's a bit wonky. 4606 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule || 4607 Type == MK_PrebuiltModule) && 4608 F.ModuleName.empty()) { 4609 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure; 4610 if (Result != OutOfDate || 4611 (ClientLoadCapabilities & ARR_OutOfDate) == 0) 4612 Diag(diag::err_module_file_not_module) << FileName; 4613 return Result; 4614 } 4615 break; 4616 4617 case Failure: return Failure; 4618 case Missing: return Missing; 4619 case OutOfDate: return OutOfDate; 4620 case VersionMismatch: return VersionMismatch; 4621 case ConfigurationMismatch: return ConfigurationMismatch; 4622 case HadErrors: return HadErrors; 4623 } 4624 break; 4625 4626 case AST_BLOCK_ID: 4627 if (!HaveReadControlBlock) { 4628 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 4629 Diag(diag::err_pch_version_too_old); 4630 return VersionMismatch; 4631 } 4632 4633 // Record that we've loaded this module. 4634 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc)); 4635 ShouldFinalizePCM = true; 4636 return Success; 4637 4638 case UNHASHED_CONTROL_BLOCK_ID: 4639 // This block is handled using look-ahead during ReadControlBlock. We 4640 // shouldn't get here! 4641 Error("malformed block record in AST file"); 4642 return Failure; 4643 4644 default: 4645 if (llvm::Error Err = Stream.SkipBlock()) { 4646 Error(std::move(Err)); 4647 return Failure; 4648 } 4649 break; 4650 } 4651 } 4652 4653 llvm_unreachable("unexpected break; expected return"); 4654 } 4655 4656 ASTReader::ASTReadResult 4657 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy, 4658 unsigned ClientLoadCapabilities) { 4659 const HeaderSearchOptions &HSOpts = 4660 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 4661 bool AllowCompatibleConfigurationMismatch = 4662 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 4663 bool DisableValidation = shouldDisableValidationForFile(F); 4664 4665 ASTReadResult Result = readUnhashedControlBlockImpl( 4666 &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch, 4667 Listener.get(), 4668 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions); 4669 4670 // If F was directly imported by another module, it's implicitly validated by 4671 // the importing module. 4672 if (DisableValidation || WasImportedBy || 4673 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 4674 return Success; 4675 4676 if (Result == Failure) { 4677 Error("malformed block record in AST file"); 4678 return Failure; 4679 } 4680 4681 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) { 4682 // If this module has already been finalized in the ModuleCache, we're stuck 4683 // with it; we can only load a single version of each module. 4684 // 4685 // This can happen when a module is imported in two contexts: in one, as a 4686 // user module; in another, as a system module (due to an import from 4687 // another module marked with the [system] flag). It usually indicates a 4688 // bug in the module map: this module should also be marked with [system]. 4689 // 4690 // If -Wno-system-headers (the default), and the first import is as a 4691 // system module, then validation will fail during the as-user import, 4692 // since -Werror flags won't have been validated. However, it's reasonable 4693 // to treat this consistently as a system module. 4694 // 4695 // If -Wsystem-headers, the PCM on disk was built with 4696 // -Wno-system-headers, and the first import is as a user module, then 4697 // validation will fail during the as-system import since the PCM on disk 4698 // doesn't guarantee that -Werror was respected. However, the -Werror 4699 // flags were checked during the initial as-user import. 4700 if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) { 4701 Diag(diag::warn_module_system_bit_conflict) << F.FileName; 4702 return Success; 4703 } 4704 } 4705 4706 return Result; 4707 } 4708 4709 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl( 4710 ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities, 4711 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener, 4712 bool ValidateDiagnosticOptions) { 4713 // Initialize a stream. 4714 BitstreamCursor Stream(StreamData); 4715 4716 // Sniff for the signature. 4717 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4718 // FIXME this drops the error on the floor. 4719 consumeError(std::move(Err)); 4720 return Failure; 4721 } 4722 4723 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 4724 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 4725 return Failure; 4726 4727 // Read all of the records in the options block. 4728 RecordData Record; 4729 ASTReadResult Result = Success; 4730 while (true) { 4731 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4732 if (!MaybeEntry) { 4733 // FIXME this drops the error on the floor. 4734 consumeError(MaybeEntry.takeError()); 4735 return Failure; 4736 } 4737 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4738 4739 switch (Entry.Kind) { 4740 case llvm::BitstreamEntry::Error: 4741 case llvm::BitstreamEntry::SubBlock: 4742 return Failure; 4743 4744 case llvm::BitstreamEntry::EndBlock: 4745 return Result; 4746 4747 case llvm::BitstreamEntry::Record: 4748 // The interesting case. 4749 break; 4750 } 4751 4752 // Read and process a record. 4753 Record.clear(); 4754 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record); 4755 if (!MaybeRecordType) { 4756 // FIXME this drops the error. 4757 return Failure; 4758 } 4759 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) { 4760 case SIGNATURE: 4761 if (F) 4762 F->Signature = ASTFileSignature::create(Record.begin(), Record.end()); 4763 break; 4764 case AST_BLOCK_HASH: 4765 if (F) 4766 F->ASTBlockHash = 4767 ASTFileSignature::create(Record.begin(), Record.end()); 4768 break; 4769 case DIAGNOSTIC_OPTIONS: { 4770 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 4771 if (Listener && ValidateDiagnosticOptions && 4772 !AllowCompatibleConfigurationMismatch && 4773 ParseDiagnosticOptions(Record, Complain, *Listener)) 4774 Result = OutOfDate; // Don't return early. Read the signature. 4775 break; 4776 } 4777 case DIAG_PRAGMA_MAPPINGS: 4778 if (!F) 4779 break; 4780 if (F->PragmaDiagMappings.empty()) 4781 F->PragmaDiagMappings.swap(Record); 4782 else 4783 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(), 4784 Record.begin(), Record.end()); 4785 break; 4786 } 4787 } 4788 } 4789 4790 /// Parse a record and blob containing module file extension metadata. 4791 static bool parseModuleFileExtensionMetadata( 4792 const SmallVectorImpl<uint64_t> &Record, 4793 StringRef Blob, 4794 ModuleFileExtensionMetadata &Metadata) { 4795 if (Record.size() < 4) return true; 4796 4797 Metadata.MajorVersion = Record[0]; 4798 Metadata.MinorVersion = Record[1]; 4799 4800 unsigned BlockNameLen = Record[2]; 4801 unsigned UserInfoLen = Record[3]; 4802 4803 if (BlockNameLen + UserInfoLen > Blob.size()) return true; 4804 4805 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen); 4806 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen, 4807 Blob.data() + BlockNameLen + UserInfoLen); 4808 return false; 4809 } 4810 4811 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) { 4812 BitstreamCursor &Stream = F.Stream; 4813 4814 RecordData Record; 4815 while (true) { 4816 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4817 if (!MaybeEntry) { 4818 Error(MaybeEntry.takeError()); 4819 return Failure; 4820 } 4821 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4822 4823 switch (Entry.Kind) { 4824 case llvm::BitstreamEntry::SubBlock: 4825 if (llvm::Error Err = Stream.SkipBlock()) { 4826 Error(std::move(Err)); 4827 return Failure; 4828 } 4829 continue; 4830 4831 case llvm::BitstreamEntry::EndBlock: 4832 return Success; 4833 4834 case llvm::BitstreamEntry::Error: 4835 return HadErrors; 4836 4837 case llvm::BitstreamEntry::Record: 4838 break; 4839 } 4840 4841 Record.clear(); 4842 StringRef Blob; 4843 Expected<unsigned> MaybeRecCode = 4844 Stream.readRecord(Entry.ID, Record, &Blob); 4845 if (!MaybeRecCode) { 4846 Error(MaybeRecCode.takeError()); 4847 return Failure; 4848 } 4849 switch (MaybeRecCode.get()) { 4850 case EXTENSION_METADATA: { 4851 ModuleFileExtensionMetadata Metadata; 4852 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) { 4853 Error("malformed EXTENSION_METADATA in AST file"); 4854 return Failure; 4855 } 4856 4857 // Find a module file extension with this block name. 4858 auto Known = ModuleFileExtensions.find(Metadata.BlockName); 4859 if (Known == ModuleFileExtensions.end()) break; 4860 4861 // Form a reader. 4862 if (auto Reader = Known->second->createExtensionReader(Metadata, *this, 4863 F, Stream)) { 4864 F.ExtensionReaders.push_back(std::move(Reader)); 4865 } 4866 4867 break; 4868 } 4869 } 4870 } 4871 4872 return Success; 4873 } 4874 4875 void ASTReader::InitializeContext() { 4876 assert(ContextObj && "no context to initialize"); 4877 ASTContext &Context = *ContextObj; 4878 4879 // If there's a listener, notify them that we "read" the translation unit. 4880 if (DeserializationListener) 4881 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, 4882 Context.getTranslationUnitDecl()); 4883 4884 // FIXME: Find a better way to deal with collisions between these 4885 // built-in types. Right now, we just ignore the problem. 4886 4887 // Load the special types. 4888 if (SpecialTypes.size() >= NumSpecialTypeIDs) { 4889 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) { 4890 if (!Context.CFConstantStringTypeDecl) 4891 Context.setCFConstantStringType(GetType(String)); 4892 } 4893 4894 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) { 4895 QualType FileType = GetType(File); 4896 if (FileType.isNull()) { 4897 Error("FILE type is NULL"); 4898 return; 4899 } 4900 4901 if (!Context.FILEDecl) { 4902 if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) 4903 Context.setFILEDecl(Typedef->getDecl()); 4904 else { 4905 const TagType *Tag = FileType->getAs<TagType>(); 4906 if (!Tag) { 4907 Error("Invalid FILE type in AST file"); 4908 return; 4909 } 4910 Context.setFILEDecl(Tag->getDecl()); 4911 } 4912 } 4913 } 4914 4915 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) { 4916 QualType Jmp_bufType = GetType(Jmp_buf); 4917 if (Jmp_bufType.isNull()) { 4918 Error("jmp_buf type is NULL"); 4919 return; 4920 } 4921 4922 if (!Context.jmp_bufDecl) { 4923 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) 4924 Context.setjmp_bufDecl(Typedef->getDecl()); 4925 else { 4926 const TagType *Tag = Jmp_bufType->getAs<TagType>(); 4927 if (!Tag) { 4928 Error("Invalid jmp_buf type in AST file"); 4929 return; 4930 } 4931 Context.setjmp_bufDecl(Tag->getDecl()); 4932 } 4933 } 4934 } 4935 4936 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) { 4937 QualType Sigjmp_bufType = GetType(Sigjmp_buf); 4938 if (Sigjmp_bufType.isNull()) { 4939 Error("sigjmp_buf type is NULL"); 4940 return; 4941 } 4942 4943 if (!Context.sigjmp_bufDecl) { 4944 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) 4945 Context.setsigjmp_bufDecl(Typedef->getDecl()); 4946 else { 4947 const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); 4948 assert(Tag && "Invalid sigjmp_buf type in AST file"); 4949 Context.setsigjmp_bufDecl(Tag->getDecl()); 4950 } 4951 } 4952 } 4953 4954 if (unsigned ObjCIdRedef 4955 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) { 4956 if (Context.ObjCIdRedefinitionType.isNull()) 4957 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef); 4958 } 4959 4960 if (unsigned ObjCClassRedef 4961 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) { 4962 if (Context.ObjCClassRedefinitionType.isNull()) 4963 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef); 4964 } 4965 4966 if (unsigned ObjCSelRedef 4967 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) { 4968 if (Context.ObjCSelRedefinitionType.isNull()) 4969 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef); 4970 } 4971 4972 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) { 4973 QualType Ucontext_tType = GetType(Ucontext_t); 4974 if (Ucontext_tType.isNull()) { 4975 Error("ucontext_t type is NULL"); 4976 return; 4977 } 4978 4979 if (!Context.ucontext_tDecl) { 4980 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>()) 4981 Context.setucontext_tDecl(Typedef->getDecl()); 4982 else { 4983 const TagType *Tag = Ucontext_tType->getAs<TagType>(); 4984 assert(Tag && "Invalid ucontext_t type in AST file"); 4985 Context.setucontext_tDecl(Tag->getDecl()); 4986 } 4987 } 4988 } 4989 } 4990 4991 ReadPragmaDiagnosticMappings(Context.getDiagnostics()); 4992 4993 // If there were any CUDA special declarations, deserialize them. 4994 if (!CUDASpecialDeclRefs.empty()) { 4995 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!"); 4996 Context.setcudaConfigureCallDecl( 4997 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0]))); 4998 } 4999 5000 // Re-export any modules that were imported by a non-module AST file. 5001 // FIXME: This does not make macro-only imports visible again. 5002 for (auto &Import : ImportedModules) { 5003 if (Module *Imported = getSubmodule(Import.ID)) { 5004 makeModuleVisible(Imported, Module::AllVisible, 5005 /*ImportLoc=*/Import.ImportLoc); 5006 if (Import.ImportLoc.isValid()) 5007 PP.makeModuleVisible(Imported, Import.ImportLoc); 5008 // This updates visibility for Preprocessor only. For Sema, which can be 5009 // nullptr here, we do the same later, in UpdateSema(). 5010 } 5011 } 5012 } 5013 5014 void ASTReader::finalizeForWriting() { 5015 // Nothing to do for now. 5016 } 5017 5018 /// Reads and return the signature record from \p PCH's control block, or 5019 /// else returns 0. 5020 static ASTFileSignature readASTFileSignature(StringRef PCH) { 5021 BitstreamCursor Stream(PCH); 5022 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5023 // FIXME this drops the error on the floor. 5024 consumeError(std::move(Err)); 5025 return ASTFileSignature(); 5026 } 5027 5028 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 5029 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 5030 return ASTFileSignature(); 5031 5032 // Scan for SIGNATURE inside the diagnostic options block. 5033 ASTReader::RecordData Record; 5034 while (true) { 5035 Expected<llvm::BitstreamEntry> MaybeEntry = 5036 Stream.advanceSkippingSubblocks(); 5037 if (!MaybeEntry) { 5038 // FIXME this drops the error on the floor. 5039 consumeError(MaybeEntry.takeError()); 5040 return ASTFileSignature(); 5041 } 5042 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5043 5044 if (Entry.Kind != llvm::BitstreamEntry::Record) 5045 return ASTFileSignature(); 5046 5047 Record.clear(); 5048 StringRef Blob; 5049 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 5050 if (!MaybeRecord) { 5051 // FIXME this drops the error on the floor. 5052 consumeError(MaybeRecord.takeError()); 5053 return ASTFileSignature(); 5054 } 5055 if (SIGNATURE == MaybeRecord.get()) 5056 return ASTFileSignature::create(Record.begin(), 5057 Record.begin() + ASTFileSignature::size); 5058 } 5059 } 5060 5061 /// Retrieve the name of the original source file name 5062 /// directly from the AST file, without actually loading the AST 5063 /// file. 5064 std::string ASTReader::getOriginalSourceFile( 5065 const std::string &ASTFileName, FileManager &FileMgr, 5066 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) { 5067 // Open the AST file. 5068 auto Buffer = FileMgr.getBufferForFile(ASTFileName); 5069 if (!Buffer) { 5070 Diags.Report(diag::err_fe_unable_to_read_pch_file) 5071 << ASTFileName << Buffer.getError().message(); 5072 return std::string(); 5073 } 5074 5075 // Initialize the stream 5076 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer)); 5077 5078 // Sniff for the signature. 5079 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5080 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err); 5081 return std::string(); 5082 } 5083 5084 // Scan for the CONTROL_BLOCK_ID block. 5085 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) { 5086 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 5087 return std::string(); 5088 } 5089 5090 // Scan for ORIGINAL_FILE inside the control block. 5091 RecordData Record; 5092 while (true) { 5093 Expected<llvm::BitstreamEntry> MaybeEntry = 5094 Stream.advanceSkippingSubblocks(); 5095 if (!MaybeEntry) { 5096 // FIXME this drops errors on the floor. 5097 consumeError(MaybeEntry.takeError()); 5098 return std::string(); 5099 } 5100 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5101 5102 if (Entry.Kind == llvm::BitstreamEntry::EndBlock) 5103 return std::string(); 5104 5105 if (Entry.Kind != llvm::BitstreamEntry::Record) { 5106 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 5107 return std::string(); 5108 } 5109 5110 Record.clear(); 5111 StringRef Blob; 5112 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 5113 if (!MaybeRecord) { 5114 // FIXME this drops the errors on the floor. 5115 consumeError(MaybeRecord.takeError()); 5116 return std::string(); 5117 } 5118 if (ORIGINAL_FILE == MaybeRecord.get()) 5119 return Blob.str(); 5120 } 5121 } 5122 5123 namespace { 5124 5125 class SimplePCHValidator : public ASTReaderListener { 5126 const LangOptions &ExistingLangOpts; 5127 const TargetOptions &ExistingTargetOpts; 5128 const PreprocessorOptions &ExistingPPOpts; 5129 std::string ExistingModuleCachePath; 5130 FileManager &FileMgr; 5131 5132 public: 5133 SimplePCHValidator(const LangOptions &ExistingLangOpts, 5134 const TargetOptions &ExistingTargetOpts, 5135 const PreprocessorOptions &ExistingPPOpts, 5136 StringRef ExistingModuleCachePath, FileManager &FileMgr) 5137 : ExistingLangOpts(ExistingLangOpts), 5138 ExistingTargetOpts(ExistingTargetOpts), 5139 ExistingPPOpts(ExistingPPOpts), 5140 ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {} 5141 5142 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain, 5143 bool AllowCompatibleDifferences) override { 5144 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr, 5145 AllowCompatibleDifferences); 5146 } 5147 5148 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain, 5149 bool AllowCompatibleDifferences) override { 5150 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr, 5151 AllowCompatibleDifferences); 5152 } 5153 5154 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 5155 StringRef SpecificModuleCachePath, 5156 bool Complain) override { 5157 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5158 ExistingModuleCachePath, 5159 nullptr, ExistingLangOpts); 5160 } 5161 5162 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 5163 bool Complain, 5164 std::string &SuggestedPredefines) override { 5165 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr, 5166 SuggestedPredefines, ExistingLangOpts); 5167 } 5168 }; 5169 5170 } // namespace 5171 5172 bool ASTReader::readASTFileControlBlock( 5173 StringRef Filename, FileManager &FileMgr, 5174 const PCHContainerReader &PCHContainerRdr, 5175 bool FindModuleFileExtensions, 5176 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) { 5177 // Open the AST file. 5178 // FIXME: This allows use of the VFS; we do not allow use of the 5179 // VFS when actually loading a module. 5180 auto Buffer = FileMgr.getBufferForFile(Filename); 5181 if (!Buffer) { 5182 return true; 5183 } 5184 5185 // Initialize the stream 5186 StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer); 5187 BitstreamCursor Stream(Bytes); 5188 5189 // Sniff for the signature. 5190 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5191 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 5192 return true; 5193 } 5194 5195 // Scan for the CONTROL_BLOCK_ID block. 5196 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) 5197 return true; 5198 5199 bool NeedsInputFiles = Listener.needsInputFileVisitation(); 5200 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation(); 5201 bool NeedsImports = Listener.needsImportVisitation(); 5202 BitstreamCursor InputFilesCursor; 5203 5204 RecordData Record; 5205 std::string ModuleDir; 5206 bool DoneWithControlBlock = false; 5207 while (!DoneWithControlBlock) { 5208 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5209 if (!MaybeEntry) { 5210 // FIXME this drops the error on the floor. 5211 consumeError(MaybeEntry.takeError()); 5212 return true; 5213 } 5214 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5215 5216 switch (Entry.Kind) { 5217 case llvm::BitstreamEntry::SubBlock: { 5218 switch (Entry.ID) { 5219 case OPTIONS_BLOCK_ID: { 5220 std::string IgnoredSuggestedPredefines; 5221 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate, 5222 /*AllowCompatibleConfigurationMismatch*/ false, 5223 Listener, IgnoredSuggestedPredefines) != Success) 5224 return true; 5225 break; 5226 } 5227 5228 case INPUT_FILES_BLOCK_ID: 5229 InputFilesCursor = Stream; 5230 if (llvm::Error Err = Stream.SkipBlock()) { 5231 // FIXME this drops the error on the floor. 5232 consumeError(std::move(Err)); 5233 return true; 5234 } 5235 if (NeedsInputFiles && 5236 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID)) 5237 return true; 5238 break; 5239 5240 default: 5241 if (llvm::Error Err = Stream.SkipBlock()) { 5242 // FIXME this drops the error on the floor. 5243 consumeError(std::move(Err)); 5244 return true; 5245 } 5246 break; 5247 } 5248 5249 continue; 5250 } 5251 5252 case llvm::BitstreamEntry::EndBlock: 5253 DoneWithControlBlock = true; 5254 break; 5255 5256 case llvm::BitstreamEntry::Error: 5257 return true; 5258 5259 case llvm::BitstreamEntry::Record: 5260 break; 5261 } 5262 5263 if (DoneWithControlBlock) break; 5264 5265 Record.clear(); 5266 StringRef Blob; 5267 Expected<unsigned> MaybeRecCode = 5268 Stream.readRecord(Entry.ID, Record, &Blob); 5269 if (!MaybeRecCode) { 5270 // FIXME this drops the error. 5271 return Failure; 5272 } 5273 switch ((ControlRecordTypes)MaybeRecCode.get()) { 5274 case METADATA: 5275 if (Record[0] != VERSION_MAJOR) 5276 return true; 5277 if (Listener.ReadFullVersionInformation(Blob)) 5278 return true; 5279 break; 5280 case MODULE_NAME: 5281 Listener.ReadModuleName(Blob); 5282 break; 5283 case MODULE_DIRECTORY: 5284 ModuleDir = std::string(Blob); 5285 break; 5286 case MODULE_MAP_FILE: { 5287 unsigned Idx = 0; 5288 auto Path = ReadString(Record, Idx); 5289 ResolveImportedPath(Path, ModuleDir); 5290 Listener.ReadModuleMapFile(Path); 5291 break; 5292 } 5293 case INPUT_FILE_OFFSETS: { 5294 if (!NeedsInputFiles) 5295 break; 5296 5297 unsigned NumInputFiles = Record[0]; 5298 unsigned NumUserFiles = Record[1]; 5299 const llvm::support::unaligned_uint64_t *InputFileOffs = 5300 (const llvm::support::unaligned_uint64_t *)Blob.data(); 5301 for (unsigned I = 0; I != NumInputFiles; ++I) { 5302 // Go find this input file. 5303 bool isSystemFile = I >= NumUserFiles; 5304 5305 if (isSystemFile && !NeedsSystemInputFiles) 5306 break; // the rest are system input files 5307 5308 BitstreamCursor &Cursor = InputFilesCursor; 5309 SavedStreamPosition SavedPosition(Cursor); 5310 if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) { 5311 // FIXME this drops errors on the floor. 5312 consumeError(std::move(Err)); 5313 } 5314 5315 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 5316 if (!MaybeCode) { 5317 // FIXME this drops errors on the floor. 5318 consumeError(MaybeCode.takeError()); 5319 } 5320 unsigned Code = MaybeCode.get(); 5321 5322 RecordData Record; 5323 StringRef Blob; 5324 bool shouldContinue = false; 5325 Expected<unsigned> MaybeRecordType = 5326 Cursor.readRecord(Code, Record, &Blob); 5327 if (!MaybeRecordType) { 5328 // FIXME this drops errors on the floor. 5329 consumeError(MaybeRecordType.takeError()); 5330 } 5331 switch ((InputFileRecordTypes)MaybeRecordType.get()) { 5332 case INPUT_FILE_HASH: 5333 break; 5334 case INPUT_FILE: 5335 bool Overridden = static_cast<bool>(Record[3]); 5336 std::string Filename = std::string(Blob); 5337 ResolveImportedPath(Filename, ModuleDir); 5338 shouldContinue = Listener.visitInputFile( 5339 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false); 5340 break; 5341 } 5342 if (!shouldContinue) 5343 break; 5344 } 5345 break; 5346 } 5347 5348 case IMPORTS: { 5349 if (!NeedsImports) 5350 break; 5351 5352 unsigned Idx = 0, N = Record.size(); 5353 while (Idx < N) { 5354 // Read information about the AST file. 5355 Idx += 5356 1 + 1 + 1 + 1 + 5357 ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature 5358 std::string ModuleName = ReadString(Record, Idx); 5359 std::string Filename = ReadString(Record, Idx); 5360 ResolveImportedPath(Filename, ModuleDir); 5361 Listener.visitImport(ModuleName, Filename); 5362 } 5363 break; 5364 } 5365 5366 default: 5367 // No other validation to perform. 5368 break; 5369 } 5370 } 5371 5372 // Look for module file extension blocks, if requested. 5373 if (FindModuleFileExtensions) { 5374 BitstreamCursor SavedStream = Stream; 5375 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) { 5376 bool DoneWithExtensionBlock = false; 5377 while (!DoneWithExtensionBlock) { 5378 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5379 if (!MaybeEntry) { 5380 // FIXME this drops the error. 5381 return true; 5382 } 5383 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5384 5385 switch (Entry.Kind) { 5386 case llvm::BitstreamEntry::SubBlock: 5387 if (llvm::Error Err = Stream.SkipBlock()) { 5388 // FIXME this drops the error on the floor. 5389 consumeError(std::move(Err)); 5390 return true; 5391 } 5392 continue; 5393 5394 case llvm::BitstreamEntry::EndBlock: 5395 DoneWithExtensionBlock = true; 5396 continue; 5397 5398 case llvm::BitstreamEntry::Error: 5399 return true; 5400 5401 case llvm::BitstreamEntry::Record: 5402 break; 5403 } 5404 5405 Record.clear(); 5406 StringRef Blob; 5407 Expected<unsigned> MaybeRecCode = 5408 Stream.readRecord(Entry.ID, Record, &Blob); 5409 if (!MaybeRecCode) { 5410 // FIXME this drops the error. 5411 return true; 5412 } 5413 switch (MaybeRecCode.get()) { 5414 case EXTENSION_METADATA: { 5415 ModuleFileExtensionMetadata Metadata; 5416 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) 5417 return true; 5418 5419 Listener.readModuleFileExtension(Metadata); 5420 break; 5421 } 5422 } 5423 } 5424 } 5425 Stream = SavedStream; 5426 } 5427 5428 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 5429 if (readUnhashedControlBlockImpl( 5430 nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate, 5431 /*AllowCompatibleConfigurationMismatch*/ false, &Listener, 5432 ValidateDiagnosticOptions) != Success) 5433 return true; 5434 5435 return false; 5436 } 5437 5438 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr, 5439 const PCHContainerReader &PCHContainerRdr, 5440 const LangOptions &LangOpts, 5441 const TargetOptions &TargetOpts, 5442 const PreprocessorOptions &PPOpts, 5443 StringRef ExistingModuleCachePath) { 5444 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, 5445 ExistingModuleCachePath, FileMgr); 5446 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr, 5447 /*FindModuleFileExtensions=*/false, 5448 validator, 5449 /*ValidateDiagnosticOptions=*/true); 5450 } 5451 5452 ASTReader::ASTReadResult 5453 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) { 5454 // Enter the submodule block. 5455 if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) { 5456 Error(std::move(Err)); 5457 return Failure; 5458 } 5459 5460 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap(); 5461 bool First = true; 5462 Module *CurrentModule = nullptr; 5463 RecordData Record; 5464 while (true) { 5465 Expected<llvm::BitstreamEntry> MaybeEntry = 5466 F.Stream.advanceSkippingSubblocks(); 5467 if (!MaybeEntry) { 5468 Error(MaybeEntry.takeError()); 5469 return Failure; 5470 } 5471 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5472 5473 switch (Entry.Kind) { 5474 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 5475 case llvm::BitstreamEntry::Error: 5476 Error("malformed block record in AST file"); 5477 return Failure; 5478 case llvm::BitstreamEntry::EndBlock: 5479 return Success; 5480 case llvm::BitstreamEntry::Record: 5481 // The interesting case. 5482 break; 5483 } 5484 5485 // Read a record. 5486 StringRef Blob; 5487 Record.clear(); 5488 Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob); 5489 if (!MaybeKind) { 5490 Error(MaybeKind.takeError()); 5491 return Failure; 5492 } 5493 unsigned Kind = MaybeKind.get(); 5494 5495 if ((Kind == SUBMODULE_METADATA) != First) { 5496 Error("submodule metadata record should be at beginning of block"); 5497 return Failure; 5498 } 5499 First = false; 5500 5501 // Submodule information is only valid if we have a current module. 5502 // FIXME: Should we error on these cases? 5503 if (!CurrentModule && Kind != SUBMODULE_METADATA && 5504 Kind != SUBMODULE_DEFINITION) 5505 continue; 5506 5507 switch (Kind) { 5508 default: // Default behavior: ignore. 5509 break; 5510 5511 case SUBMODULE_DEFINITION: { 5512 if (Record.size() < 12) { 5513 Error("malformed module definition"); 5514 return Failure; 5515 } 5516 5517 StringRef Name = Blob; 5518 unsigned Idx = 0; 5519 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]); 5520 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]); 5521 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++]; 5522 bool IsFramework = Record[Idx++]; 5523 bool IsExplicit = Record[Idx++]; 5524 bool IsSystem = Record[Idx++]; 5525 bool IsExternC = Record[Idx++]; 5526 bool InferSubmodules = Record[Idx++]; 5527 bool InferExplicitSubmodules = Record[Idx++]; 5528 bool InferExportWildcard = Record[Idx++]; 5529 bool ConfigMacrosExhaustive = Record[Idx++]; 5530 bool ModuleMapIsPrivate = Record[Idx++]; 5531 5532 Module *ParentModule = nullptr; 5533 if (Parent) 5534 ParentModule = getSubmodule(Parent); 5535 5536 // Retrieve this (sub)module from the module map, creating it if 5537 // necessary. 5538 CurrentModule = 5539 ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit) 5540 .first; 5541 5542 // FIXME: set the definition loc for CurrentModule, or call 5543 // ModMap.setInferredModuleAllowedBy() 5544 5545 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS; 5546 if (GlobalIndex >= SubmodulesLoaded.size() || 5547 SubmodulesLoaded[GlobalIndex]) { 5548 Error("too many submodules"); 5549 return Failure; 5550 } 5551 5552 if (!ParentModule) { 5553 if (const FileEntry *CurFile = CurrentModule->getASTFile()) { 5554 // Don't emit module relocation error if we have -fno-validate-pch 5555 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation & 5556 DisableValidationForModuleKind::Module) && 5557 CurFile != F.File) { 5558 Error(diag::err_module_file_conflict, 5559 CurrentModule->getTopLevelModuleName(), CurFile->getName(), 5560 F.File->getName()); 5561 return Failure; 5562 } 5563 } 5564 5565 F.DidReadTopLevelSubmodule = true; 5566 CurrentModule->setASTFile(F.File); 5567 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath; 5568 } 5569 5570 CurrentModule->Kind = Kind; 5571 CurrentModule->Signature = F.Signature; 5572 CurrentModule->IsFromModuleFile = true; 5573 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem; 5574 CurrentModule->IsExternC = IsExternC; 5575 CurrentModule->InferSubmodules = InferSubmodules; 5576 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules; 5577 CurrentModule->InferExportWildcard = InferExportWildcard; 5578 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive; 5579 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate; 5580 if (DeserializationListener) 5581 DeserializationListener->ModuleRead(GlobalID, CurrentModule); 5582 5583 SubmodulesLoaded[GlobalIndex] = CurrentModule; 5584 5585 // Clear out data that will be replaced by what is in the module file. 5586 CurrentModule->LinkLibraries.clear(); 5587 CurrentModule->ConfigMacros.clear(); 5588 CurrentModule->UnresolvedConflicts.clear(); 5589 CurrentModule->Conflicts.clear(); 5590 5591 // The module is available unless it's missing a requirement; relevant 5592 // requirements will be (re-)added by SUBMODULE_REQUIRES records. 5593 // Missing headers that were present when the module was built do not 5594 // make it unavailable -- if we got this far, this must be an explicitly 5595 // imported module file. 5596 CurrentModule->Requirements.clear(); 5597 CurrentModule->MissingHeaders.clear(); 5598 CurrentModule->IsUnimportable = 5599 ParentModule && ParentModule->IsUnimportable; 5600 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable; 5601 break; 5602 } 5603 5604 case SUBMODULE_UMBRELLA_HEADER: { 5605 std::string Filename = std::string(Blob); 5606 ResolveImportedPath(F, Filename); 5607 if (auto Umbrella = PP.getFileManager().getOptionalFileRef(Filename)) { 5608 if (!CurrentModule->getUmbrellaHeader()) 5609 ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob); 5610 else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) { 5611 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 5612 Error("mismatched umbrella headers in submodule"); 5613 return OutOfDate; 5614 } 5615 } 5616 break; 5617 } 5618 5619 case SUBMODULE_HEADER: 5620 case SUBMODULE_EXCLUDED_HEADER: 5621 case SUBMODULE_PRIVATE_HEADER: 5622 // We lazily associate headers with their modules via the HeaderInfo table. 5623 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead 5624 // of complete filenames or remove it entirely. 5625 break; 5626 5627 case SUBMODULE_TEXTUAL_HEADER: 5628 case SUBMODULE_PRIVATE_TEXTUAL_HEADER: 5629 // FIXME: Textual headers are not marked in the HeaderInfo table. Load 5630 // them here. 5631 break; 5632 5633 case SUBMODULE_TOPHEADER: 5634 CurrentModule->addTopHeaderFilename(Blob); 5635 break; 5636 5637 case SUBMODULE_UMBRELLA_DIR: { 5638 std::string Dirname = std::string(Blob); 5639 ResolveImportedPath(F, Dirname); 5640 if (auto Umbrella = 5641 PP.getFileManager().getOptionalDirectoryRef(Dirname)) { 5642 if (!CurrentModule->getUmbrellaDir()) 5643 ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob); 5644 else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) { 5645 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 5646 Error("mismatched umbrella directories in submodule"); 5647 return OutOfDate; 5648 } 5649 } 5650 break; 5651 } 5652 5653 case SUBMODULE_METADATA: { 5654 F.BaseSubmoduleID = getTotalNumSubmodules(); 5655 F.LocalNumSubmodules = Record[0]; 5656 unsigned LocalBaseSubmoduleID = Record[1]; 5657 if (F.LocalNumSubmodules > 0) { 5658 // Introduce the global -> local mapping for submodules within this 5659 // module. 5660 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F)); 5661 5662 // Introduce the local -> global mapping for submodules within this 5663 // module. 5664 F.SubmoduleRemap.insertOrReplace( 5665 std::make_pair(LocalBaseSubmoduleID, 5666 F.BaseSubmoduleID - LocalBaseSubmoduleID)); 5667 5668 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules); 5669 } 5670 break; 5671 } 5672 5673 case SUBMODULE_IMPORTS: 5674 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) { 5675 UnresolvedModuleRef Unresolved; 5676 Unresolved.File = &F; 5677 Unresolved.Mod = CurrentModule; 5678 Unresolved.ID = Record[Idx]; 5679 Unresolved.Kind = UnresolvedModuleRef::Import; 5680 Unresolved.IsWildcard = false; 5681 UnresolvedModuleRefs.push_back(Unresolved); 5682 } 5683 break; 5684 5685 case SUBMODULE_EXPORTS: 5686 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) { 5687 UnresolvedModuleRef Unresolved; 5688 Unresolved.File = &F; 5689 Unresolved.Mod = CurrentModule; 5690 Unresolved.ID = Record[Idx]; 5691 Unresolved.Kind = UnresolvedModuleRef::Export; 5692 Unresolved.IsWildcard = Record[Idx + 1]; 5693 UnresolvedModuleRefs.push_back(Unresolved); 5694 } 5695 5696 // Once we've loaded the set of exports, there's no reason to keep 5697 // the parsed, unresolved exports around. 5698 CurrentModule->UnresolvedExports.clear(); 5699 break; 5700 5701 case SUBMODULE_REQUIRES: 5702 CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(), 5703 PP.getTargetInfo()); 5704 break; 5705 5706 case SUBMODULE_LINK_LIBRARY: 5707 ModMap.resolveLinkAsDependencies(CurrentModule); 5708 CurrentModule->LinkLibraries.push_back( 5709 Module::LinkLibrary(std::string(Blob), Record[0])); 5710 break; 5711 5712 case SUBMODULE_CONFIG_MACRO: 5713 CurrentModule->ConfigMacros.push_back(Blob.str()); 5714 break; 5715 5716 case SUBMODULE_CONFLICT: { 5717 UnresolvedModuleRef Unresolved; 5718 Unresolved.File = &F; 5719 Unresolved.Mod = CurrentModule; 5720 Unresolved.ID = Record[0]; 5721 Unresolved.Kind = UnresolvedModuleRef::Conflict; 5722 Unresolved.IsWildcard = false; 5723 Unresolved.String = Blob; 5724 UnresolvedModuleRefs.push_back(Unresolved); 5725 break; 5726 } 5727 5728 case SUBMODULE_INITIALIZERS: { 5729 if (!ContextObj) 5730 break; 5731 SmallVector<uint32_t, 16> Inits; 5732 for (auto &ID : Record) 5733 Inits.push_back(getGlobalDeclID(F, ID)); 5734 ContextObj->addLazyModuleInitializers(CurrentModule, Inits); 5735 break; 5736 } 5737 5738 case SUBMODULE_EXPORT_AS: 5739 CurrentModule->ExportAsModule = Blob.str(); 5740 ModMap.addLinkAsDependency(CurrentModule); 5741 break; 5742 } 5743 } 5744 } 5745 5746 /// Parse the record that corresponds to a LangOptions data 5747 /// structure. 5748 /// 5749 /// This routine parses the language options from the AST file and then gives 5750 /// them to the AST listener if one is set. 5751 /// 5752 /// \returns true if the listener deems the file unacceptable, false otherwise. 5753 bool ASTReader::ParseLanguageOptions(const RecordData &Record, 5754 bool Complain, 5755 ASTReaderListener &Listener, 5756 bool AllowCompatibleDifferences) { 5757 LangOptions LangOpts; 5758 unsigned Idx = 0; 5759 #define LANGOPT(Name, Bits, Default, Description) \ 5760 LangOpts.Name = Record[Idx++]; 5761 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 5762 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++])); 5763 #include "clang/Basic/LangOptions.def" 5764 #define SANITIZER(NAME, ID) \ 5765 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]); 5766 #include "clang/Basic/Sanitizers.def" 5767 5768 for (unsigned N = Record[Idx++]; N; --N) 5769 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx)); 5770 5771 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++]; 5772 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx); 5773 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion); 5774 5775 LangOpts.CurrentModule = ReadString(Record, Idx); 5776 5777 // Comment options. 5778 for (unsigned N = Record[Idx++]; N; --N) { 5779 LangOpts.CommentOpts.BlockCommandNames.push_back( 5780 ReadString(Record, Idx)); 5781 } 5782 LangOpts.CommentOpts.ParseAllComments = Record[Idx++]; 5783 5784 // OpenMP offloading options. 5785 for (unsigned N = Record[Idx++]; N; --N) { 5786 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx))); 5787 } 5788 5789 LangOpts.OMPHostIRFile = ReadString(Record, Idx); 5790 5791 return Listener.ReadLanguageOptions(LangOpts, Complain, 5792 AllowCompatibleDifferences); 5793 } 5794 5795 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain, 5796 ASTReaderListener &Listener, 5797 bool AllowCompatibleDifferences) { 5798 unsigned Idx = 0; 5799 TargetOptions TargetOpts; 5800 TargetOpts.Triple = ReadString(Record, Idx); 5801 TargetOpts.CPU = ReadString(Record, Idx); 5802 TargetOpts.TuneCPU = ReadString(Record, Idx); 5803 TargetOpts.ABI = ReadString(Record, Idx); 5804 for (unsigned N = Record[Idx++]; N; --N) { 5805 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx)); 5806 } 5807 for (unsigned N = Record[Idx++]; N; --N) { 5808 TargetOpts.Features.push_back(ReadString(Record, Idx)); 5809 } 5810 5811 return Listener.ReadTargetOptions(TargetOpts, Complain, 5812 AllowCompatibleDifferences); 5813 } 5814 5815 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain, 5816 ASTReaderListener &Listener) { 5817 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions); 5818 unsigned Idx = 0; 5819 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++]; 5820 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \ 5821 DiagOpts->set##Name(static_cast<Type>(Record[Idx++])); 5822 #include "clang/Basic/DiagnosticOptions.def" 5823 5824 for (unsigned N = Record[Idx++]; N; --N) 5825 DiagOpts->Warnings.push_back(ReadString(Record, Idx)); 5826 for (unsigned N = Record[Idx++]; N; --N) 5827 DiagOpts->Remarks.push_back(ReadString(Record, Idx)); 5828 5829 return Listener.ReadDiagnosticOptions(DiagOpts, Complain); 5830 } 5831 5832 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain, 5833 ASTReaderListener &Listener) { 5834 FileSystemOptions FSOpts; 5835 unsigned Idx = 0; 5836 FSOpts.WorkingDir = ReadString(Record, Idx); 5837 return Listener.ReadFileSystemOptions(FSOpts, Complain); 5838 } 5839 5840 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record, 5841 bool Complain, 5842 ASTReaderListener &Listener) { 5843 HeaderSearchOptions HSOpts; 5844 unsigned Idx = 0; 5845 HSOpts.Sysroot = ReadString(Record, Idx); 5846 5847 // Include entries. 5848 for (unsigned N = Record[Idx++]; N; --N) { 5849 std::string Path = ReadString(Record, Idx); 5850 frontend::IncludeDirGroup Group 5851 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]); 5852 bool IsFramework = Record[Idx++]; 5853 bool IgnoreSysRoot = Record[Idx++]; 5854 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework, 5855 IgnoreSysRoot); 5856 } 5857 5858 // System header prefixes. 5859 for (unsigned N = Record[Idx++]; N; --N) { 5860 std::string Prefix = ReadString(Record, Idx); 5861 bool IsSystemHeader = Record[Idx++]; 5862 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader); 5863 } 5864 5865 HSOpts.ResourceDir = ReadString(Record, Idx); 5866 HSOpts.ModuleCachePath = ReadString(Record, Idx); 5867 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx); 5868 HSOpts.DisableModuleHash = Record[Idx++]; 5869 HSOpts.ImplicitModuleMaps = Record[Idx++]; 5870 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++]; 5871 HSOpts.EnablePrebuiltImplicitModules = Record[Idx++]; 5872 HSOpts.UseBuiltinIncludes = Record[Idx++]; 5873 HSOpts.UseStandardSystemIncludes = Record[Idx++]; 5874 HSOpts.UseStandardCXXIncludes = Record[Idx++]; 5875 HSOpts.UseLibcxx = Record[Idx++]; 5876 std::string SpecificModuleCachePath = ReadString(Record, Idx); 5877 5878 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5879 Complain); 5880 } 5881 5882 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record, 5883 bool Complain, 5884 ASTReaderListener &Listener, 5885 std::string &SuggestedPredefines) { 5886 PreprocessorOptions PPOpts; 5887 unsigned Idx = 0; 5888 5889 // Macro definitions/undefs 5890 for (unsigned N = Record[Idx++]; N; --N) { 5891 std::string Macro = ReadString(Record, Idx); 5892 bool IsUndef = Record[Idx++]; 5893 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef)); 5894 } 5895 5896 // Includes 5897 for (unsigned N = Record[Idx++]; N; --N) { 5898 PPOpts.Includes.push_back(ReadString(Record, Idx)); 5899 } 5900 5901 // Macro Includes 5902 for (unsigned N = Record[Idx++]; N; --N) { 5903 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx)); 5904 } 5905 5906 PPOpts.UsePredefines = Record[Idx++]; 5907 PPOpts.DetailedRecord = Record[Idx++]; 5908 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx); 5909 PPOpts.ObjCXXARCStandardLibrary = 5910 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]); 5911 SuggestedPredefines.clear(); 5912 return Listener.ReadPreprocessorOptions(PPOpts, Complain, 5913 SuggestedPredefines); 5914 } 5915 5916 std::pair<ModuleFile *, unsigned> 5917 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) { 5918 GlobalPreprocessedEntityMapType::iterator 5919 I = GlobalPreprocessedEntityMap.find(GlobalIndex); 5920 assert(I != GlobalPreprocessedEntityMap.end() && 5921 "Corrupted global preprocessed entity map"); 5922 ModuleFile *M = I->second; 5923 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID; 5924 return std::make_pair(M, LocalIndex); 5925 } 5926 5927 llvm::iterator_range<PreprocessingRecord::iterator> 5928 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const { 5929 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord()) 5930 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID, 5931 Mod.NumPreprocessedEntities); 5932 5933 return llvm::make_range(PreprocessingRecord::iterator(), 5934 PreprocessingRecord::iterator()); 5935 } 5936 5937 llvm::iterator_range<ASTReader::ModuleDeclIterator> 5938 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) { 5939 return llvm::make_range( 5940 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls), 5941 ModuleDeclIterator(this, &Mod, 5942 Mod.FileSortedDecls + Mod.NumFileSortedDecls)); 5943 } 5944 5945 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) { 5946 auto I = GlobalSkippedRangeMap.find(GlobalIndex); 5947 assert(I != GlobalSkippedRangeMap.end() && 5948 "Corrupted global skipped range map"); 5949 ModuleFile *M = I->second; 5950 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID; 5951 assert(LocalIndex < M->NumPreprocessedSkippedRanges); 5952 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex]; 5953 SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()), 5954 TranslateSourceLocation(*M, RawRange.getEnd())); 5955 assert(Range.isValid()); 5956 return Range; 5957 } 5958 5959 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) { 5960 PreprocessedEntityID PPID = Index+1; 5961 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 5962 ModuleFile &M = *PPInfo.first; 5963 unsigned LocalIndex = PPInfo.second; 5964 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 5965 5966 if (!PP.getPreprocessingRecord()) { 5967 Error("no preprocessing record"); 5968 return nullptr; 5969 } 5970 5971 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor); 5972 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit( 5973 M.MacroOffsetsBase + PPOffs.BitOffset)) { 5974 Error(std::move(Err)); 5975 return nullptr; 5976 } 5977 5978 Expected<llvm::BitstreamEntry> MaybeEntry = 5979 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 5980 if (!MaybeEntry) { 5981 Error(MaybeEntry.takeError()); 5982 return nullptr; 5983 } 5984 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5985 5986 if (Entry.Kind != llvm::BitstreamEntry::Record) 5987 return nullptr; 5988 5989 // Read the record. 5990 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()), 5991 TranslateSourceLocation(M, PPOffs.getEnd())); 5992 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 5993 StringRef Blob; 5994 RecordData Record; 5995 Expected<unsigned> MaybeRecType = 5996 M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob); 5997 if (!MaybeRecType) { 5998 Error(MaybeRecType.takeError()); 5999 return nullptr; 6000 } 6001 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) { 6002 case PPD_MACRO_EXPANSION: { 6003 bool isBuiltin = Record[0]; 6004 IdentifierInfo *Name = nullptr; 6005 MacroDefinitionRecord *Def = nullptr; 6006 if (isBuiltin) 6007 Name = getLocalIdentifier(M, Record[1]); 6008 else { 6009 PreprocessedEntityID GlobalID = 6010 getGlobalPreprocessedEntityID(M, Record[1]); 6011 Def = cast<MacroDefinitionRecord>( 6012 PPRec.getLoadedPreprocessedEntity(GlobalID - 1)); 6013 } 6014 6015 MacroExpansion *ME; 6016 if (isBuiltin) 6017 ME = new (PPRec) MacroExpansion(Name, Range); 6018 else 6019 ME = new (PPRec) MacroExpansion(Def, Range); 6020 6021 return ME; 6022 } 6023 6024 case PPD_MACRO_DEFINITION: { 6025 // Decode the identifier info and then check again; if the macro is 6026 // still defined and associated with the identifier, 6027 IdentifierInfo *II = getLocalIdentifier(M, Record[0]); 6028 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range); 6029 6030 if (DeserializationListener) 6031 DeserializationListener->MacroDefinitionRead(PPID, MD); 6032 6033 return MD; 6034 } 6035 6036 case PPD_INCLUSION_DIRECTIVE: { 6037 const char *FullFileNameStart = Blob.data() + Record[0]; 6038 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]); 6039 const FileEntry *File = nullptr; 6040 if (!FullFileName.empty()) 6041 if (auto FE = PP.getFileManager().getFile(FullFileName)) 6042 File = *FE; 6043 6044 // FIXME: Stable encoding 6045 InclusionDirective::InclusionKind Kind 6046 = static_cast<InclusionDirective::InclusionKind>(Record[2]); 6047 InclusionDirective *ID 6048 = new (PPRec) InclusionDirective(PPRec, Kind, 6049 StringRef(Blob.data(), Record[0]), 6050 Record[1], Record[3], 6051 File, 6052 Range); 6053 return ID; 6054 } 6055 } 6056 6057 llvm_unreachable("Invalid PreprocessorDetailRecordTypes"); 6058 } 6059 6060 /// Find the next module that contains entities and return the ID 6061 /// of the first entry. 6062 /// 6063 /// \param SLocMapI points at a chunk of a module that contains no 6064 /// preprocessed entities or the entities it contains are not the ones we are 6065 /// looking for. 6066 PreprocessedEntityID ASTReader::findNextPreprocessedEntity( 6067 GlobalSLocOffsetMapType::const_iterator SLocMapI) const { 6068 ++SLocMapI; 6069 for (GlobalSLocOffsetMapType::const_iterator 6070 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) { 6071 ModuleFile &M = *SLocMapI->second; 6072 if (M.NumPreprocessedEntities) 6073 return M.BasePreprocessedEntityID; 6074 } 6075 6076 return getTotalNumPreprocessedEntities(); 6077 } 6078 6079 namespace { 6080 6081 struct PPEntityComp { 6082 const ASTReader &Reader; 6083 ModuleFile &M; 6084 6085 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {} 6086 6087 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const { 6088 SourceLocation LHS = getLoc(L); 6089 SourceLocation RHS = getLoc(R); 6090 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6091 } 6092 6093 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const { 6094 SourceLocation LHS = getLoc(L); 6095 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6096 } 6097 6098 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const { 6099 SourceLocation RHS = getLoc(R); 6100 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6101 } 6102 6103 SourceLocation getLoc(const PPEntityOffset &PPE) const { 6104 return Reader.TranslateSourceLocation(M, PPE.getBegin()); 6105 } 6106 }; 6107 6108 } // namespace 6109 6110 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc, 6111 bool EndsAfter) const { 6112 if (SourceMgr.isLocalSourceLocation(Loc)) 6113 return getTotalNumPreprocessedEntities(); 6114 6115 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find( 6116 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1); 6117 assert(SLocMapI != GlobalSLocOffsetMap.end() && 6118 "Corrupted global sloc offset map"); 6119 6120 if (SLocMapI->second->NumPreprocessedEntities == 0) 6121 return findNextPreprocessedEntity(SLocMapI); 6122 6123 ModuleFile &M = *SLocMapI->second; 6124 6125 using pp_iterator = const PPEntityOffset *; 6126 6127 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 6128 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 6129 6130 size_t Count = M.NumPreprocessedEntities; 6131 size_t Half; 6132 pp_iterator First = pp_begin; 6133 pp_iterator PPI; 6134 6135 if (EndsAfter) { 6136 PPI = std::upper_bound(pp_begin, pp_end, Loc, 6137 PPEntityComp(*this, M)); 6138 } else { 6139 // Do a binary search manually instead of using std::lower_bound because 6140 // The end locations of entities may be unordered (when a macro expansion 6141 // is inside another macro argument), but for this case it is not important 6142 // whether we get the first macro expansion or its containing macro. 6143 while (Count > 0) { 6144 Half = Count / 2; 6145 PPI = First; 6146 std::advance(PPI, Half); 6147 if (SourceMgr.isBeforeInTranslationUnit( 6148 TranslateSourceLocation(M, PPI->getEnd()), Loc)) { 6149 First = PPI; 6150 ++First; 6151 Count = Count - Half - 1; 6152 } else 6153 Count = Half; 6154 } 6155 } 6156 6157 if (PPI == pp_end) 6158 return findNextPreprocessedEntity(SLocMapI); 6159 6160 return M.BasePreprocessedEntityID + (PPI - pp_begin); 6161 } 6162 6163 /// Returns a pair of [Begin, End) indices of preallocated 6164 /// preprocessed entities that \arg Range encompasses. 6165 std::pair<unsigned, unsigned> 6166 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) { 6167 if (Range.isInvalid()) 6168 return std::make_pair(0,0); 6169 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); 6170 6171 PreprocessedEntityID BeginID = 6172 findPreprocessedEntity(Range.getBegin(), false); 6173 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true); 6174 return std::make_pair(BeginID, EndID); 6175 } 6176 6177 /// Optionally returns true or false if the preallocated preprocessed 6178 /// entity with index \arg Index came from file \arg FID. 6179 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index, 6180 FileID FID) { 6181 if (FID.isInvalid()) 6182 return false; 6183 6184 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 6185 ModuleFile &M = *PPInfo.first; 6186 unsigned LocalIndex = PPInfo.second; 6187 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 6188 6189 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin()); 6190 if (Loc.isInvalid()) 6191 return false; 6192 6193 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID)) 6194 return true; 6195 else 6196 return false; 6197 } 6198 6199 namespace { 6200 6201 /// Visitor used to search for information about a header file. 6202 class HeaderFileInfoVisitor { 6203 const FileEntry *FE; 6204 Optional<HeaderFileInfo> HFI; 6205 6206 public: 6207 explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {} 6208 6209 bool operator()(ModuleFile &M) { 6210 HeaderFileInfoLookupTable *Table 6211 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable); 6212 if (!Table) 6213 return false; 6214 6215 // Look in the on-disk hash table for an entry for this file name. 6216 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE); 6217 if (Pos == Table->end()) 6218 return false; 6219 6220 HFI = *Pos; 6221 return true; 6222 } 6223 6224 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; } 6225 }; 6226 6227 } // namespace 6228 6229 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) { 6230 HeaderFileInfoVisitor Visitor(FE); 6231 ModuleMgr.visit(Visitor); 6232 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) 6233 return *HFI; 6234 6235 return HeaderFileInfo(); 6236 } 6237 6238 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) { 6239 using DiagState = DiagnosticsEngine::DiagState; 6240 SmallVector<DiagState *, 32> DiagStates; 6241 6242 for (ModuleFile &F : ModuleMgr) { 6243 unsigned Idx = 0; 6244 auto &Record = F.PragmaDiagMappings; 6245 if (Record.empty()) 6246 continue; 6247 6248 DiagStates.clear(); 6249 6250 auto ReadDiagState = 6251 [&](const DiagState &BasedOn, SourceLocation Loc, 6252 bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * { 6253 unsigned BackrefID = Record[Idx++]; 6254 if (BackrefID != 0) 6255 return DiagStates[BackrefID - 1]; 6256 6257 // A new DiagState was created here. 6258 Diag.DiagStates.push_back(BasedOn); 6259 DiagState *NewState = &Diag.DiagStates.back(); 6260 DiagStates.push_back(NewState); 6261 unsigned Size = Record[Idx++]; 6262 assert(Idx + Size * 2 <= Record.size() && 6263 "Invalid data, not enough diag/map pairs"); 6264 while (Size--) { 6265 unsigned DiagID = Record[Idx++]; 6266 DiagnosticMapping NewMapping = 6267 DiagnosticMapping::deserialize(Record[Idx++]); 6268 if (!NewMapping.isPragma() && !IncludeNonPragmaStates) 6269 continue; 6270 6271 DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID); 6272 6273 // If this mapping was specified as a warning but the severity was 6274 // upgraded due to diagnostic settings, simulate the current diagnostic 6275 // settings (and use a warning). 6276 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) { 6277 NewMapping.setSeverity(diag::Severity::Warning); 6278 NewMapping.setUpgradedFromWarning(false); 6279 } 6280 6281 Mapping = NewMapping; 6282 } 6283 return NewState; 6284 }; 6285 6286 // Read the first state. 6287 DiagState *FirstState; 6288 if (F.Kind == MK_ImplicitModule) { 6289 // Implicitly-built modules are reused with different diagnostic 6290 // settings. Use the initial diagnostic state from Diag to simulate this 6291 // compilation's diagnostic settings. 6292 FirstState = Diag.DiagStatesByLoc.FirstDiagState; 6293 DiagStates.push_back(FirstState); 6294 6295 // Skip the initial diagnostic state from the serialized module. 6296 assert(Record[1] == 0 && 6297 "Invalid data, unexpected backref in initial state"); 6298 Idx = 3 + Record[2] * 2; 6299 assert(Idx < Record.size() && 6300 "Invalid data, not enough state change pairs in initial state"); 6301 } else if (F.isModule()) { 6302 // For an explicit module, preserve the flags from the module build 6303 // command line (-w, -Weverything, -Werror, ...) along with any explicit 6304 // -Wblah flags. 6305 unsigned Flags = Record[Idx++]; 6306 DiagState Initial; 6307 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1; 6308 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1; 6309 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1; 6310 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1; 6311 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1; 6312 Initial.ExtBehavior = (diag::Severity)Flags; 6313 FirstState = ReadDiagState(Initial, SourceLocation(), true); 6314 6315 assert(F.OriginalSourceFileID.isValid()); 6316 6317 // Set up the root buffer of the module to start with the initial 6318 // diagnostic state of the module itself, to cover files that contain no 6319 // explicit transitions (for which we did not serialize anything). 6320 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID] 6321 .StateTransitions.push_back({FirstState, 0}); 6322 } else { 6323 // For prefix ASTs, start with whatever the user configured on the 6324 // command line. 6325 Idx++; // Skip flags. 6326 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, 6327 SourceLocation(), false); 6328 } 6329 6330 // Read the state transitions. 6331 unsigned NumLocations = Record[Idx++]; 6332 while (NumLocations--) { 6333 assert(Idx < Record.size() && 6334 "Invalid data, missing pragma diagnostic states"); 6335 SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]); 6336 auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc); 6337 assert(IDAndOffset.first.isValid() && "invalid FileID for transition"); 6338 assert(IDAndOffset.second == 0 && "not a start location for a FileID"); 6339 unsigned Transitions = Record[Idx++]; 6340 6341 // Note that we don't need to set up Parent/ParentOffset here, because 6342 // we won't be changing the diagnostic state within imported FileIDs 6343 // (other than perhaps appending to the main source file, which has no 6344 // parent). 6345 auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first]; 6346 F.StateTransitions.reserve(F.StateTransitions.size() + Transitions); 6347 for (unsigned I = 0; I != Transitions; ++I) { 6348 unsigned Offset = Record[Idx++]; 6349 auto *State = 6350 ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false); 6351 F.StateTransitions.push_back({State, Offset}); 6352 } 6353 } 6354 6355 // Read the final state. 6356 assert(Idx < Record.size() && 6357 "Invalid data, missing final pragma diagnostic state"); 6358 SourceLocation CurStateLoc = 6359 ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]); 6360 auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false); 6361 6362 if (!F.isModule()) { 6363 Diag.DiagStatesByLoc.CurDiagState = CurState; 6364 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc; 6365 6366 // Preserve the property that the imaginary root file describes the 6367 // current state. 6368 FileID NullFile; 6369 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions; 6370 if (T.empty()) 6371 T.push_back({CurState, 0}); 6372 else 6373 T[0].State = CurState; 6374 } 6375 6376 // Don't try to read these mappings again. 6377 Record.clear(); 6378 } 6379 } 6380 6381 /// Get the correct cursor and offset for loading a type. 6382 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) { 6383 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index); 6384 assert(I != GlobalTypeMap.end() && "Corrupted global type map"); 6385 ModuleFile *M = I->second; 6386 return RecordLocation( 6387 M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() + 6388 M->DeclsBlockStartOffset); 6389 } 6390 6391 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) { 6392 switch (code) { 6393 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \ 6394 case TYPE_##CODE_ID: return Type::CLASS_ID; 6395 #include "clang/Serialization/TypeBitCodes.def" 6396 default: return llvm::None; 6397 } 6398 } 6399 6400 /// Read and return the type with the given index.. 6401 /// 6402 /// The index is the type ID, shifted and minus the number of predefs. This 6403 /// routine actually reads the record corresponding to the type at the given 6404 /// location. It is a helper routine for GetType, which deals with reading type 6405 /// IDs. 6406 QualType ASTReader::readTypeRecord(unsigned Index) { 6407 assert(ContextObj && "reading type with no AST context"); 6408 ASTContext &Context = *ContextObj; 6409 RecordLocation Loc = TypeCursorForIndex(Index); 6410 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 6411 6412 // Keep track of where we are in the stream, then jump back there 6413 // after reading this type. 6414 SavedStreamPosition SavedPosition(DeclsCursor); 6415 6416 ReadingKindTracker ReadingKind(Read_Type, *this); 6417 6418 // Note that we are loading a type record. 6419 Deserializing AType(this); 6420 6421 if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) { 6422 Error(std::move(Err)); 6423 return QualType(); 6424 } 6425 Expected<unsigned> RawCode = DeclsCursor.ReadCode(); 6426 if (!RawCode) { 6427 Error(RawCode.takeError()); 6428 return QualType(); 6429 } 6430 6431 ASTRecordReader Record(*this, *Loc.F); 6432 Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get()); 6433 if (!Code) { 6434 Error(Code.takeError()); 6435 return QualType(); 6436 } 6437 if (Code.get() == TYPE_EXT_QUAL) { 6438 QualType baseType = Record.readQualType(); 6439 Qualifiers quals = Record.readQualifiers(); 6440 return Context.getQualifiedType(baseType, quals); 6441 } 6442 6443 auto maybeClass = getTypeClassForCode((TypeCode) Code.get()); 6444 if (!maybeClass) { 6445 Error("Unexpected code for type"); 6446 return QualType(); 6447 } 6448 6449 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record); 6450 return TypeReader.read(*maybeClass); 6451 } 6452 6453 namespace clang { 6454 6455 class TypeLocReader : public TypeLocVisitor<TypeLocReader> { 6456 ASTRecordReader &Reader; 6457 6458 SourceLocation readSourceLocation() { 6459 return Reader.readSourceLocation(); 6460 } 6461 6462 TypeSourceInfo *GetTypeSourceInfo() { 6463 return Reader.readTypeSourceInfo(); 6464 } 6465 6466 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() { 6467 return Reader.readNestedNameSpecifierLoc(); 6468 } 6469 6470 Attr *ReadAttr() { 6471 return Reader.readAttr(); 6472 } 6473 6474 public: 6475 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {} 6476 6477 // We want compile-time assurance that we've enumerated all of 6478 // these, so unfortunately we have to declare them first, then 6479 // define them out-of-line. 6480 #define ABSTRACT_TYPELOC(CLASS, PARENT) 6481 #define TYPELOC(CLASS, PARENT) \ 6482 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 6483 #include "clang/AST/TypeLocNodes.def" 6484 6485 void VisitFunctionTypeLoc(FunctionTypeLoc); 6486 void VisitArrayTypeLoc(ArrayTypeLoc); 6487 }; 6488 6489 } // namespace clang 6490 6491 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 6492 // nothing to do 6493 } 6494 6495 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 6496 TL.setBuiltinLoc(readSourceLocation()); 6497 if (TL.needsExtraLocalData()) { 6498 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt())); 6499 TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt())); 6500 TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt())); 6501 TL.setModeAttr(Reader.readInt()); 6502 } 6503 } 6504 6505 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { 6506 TL.setNameLoc(readSourceLocation()); 6507 } 6508 6509 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { 6510 TL.setStarLoc(readSourceLocation()); 6511 } 6512 6513 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) { 6514 // nothing to do 6515 } 6516 6517 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { 6518 // nothing to do 6519 } 6520 6521 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) { 6522 TL.setExpansionLoc(readSourceLocation()); 6523 } 6524 6525 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 6526 TL.setCaretLoc(readSourceLocation()); 6527 } 6528 6529 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 6530 TL.setAmpLoc(readSourceLocation()); 6531 } 6532 6533 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 6534 TL.setAmpAmpLoc(readSourceLocation()); 6535 } 6536 6537 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 6538 TL.setStarLoc(readSourceLocation()); 6539 TL.setClassTInfo(GetTypeSourceInfo()); 6540 } 6541 6542 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { 6543 TL.setLBracketLoc(readSourceLocation()); 6544 TL.setRBracketLoc(readSourceLocation()); 6545 if (Reader.readBool()) 6546 TL.setSizeExpr(Reader.readExpr()); 6547 else 6548 TL.setSizeExpr(nullptr); 6549 } 6550 6551 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 6552 VisitArrayTypeLoc(TL); 6553 } 6554 6555 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 6556 VisitArrayTypeLoc(TL); 6557 } 6558 6559 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 6560 VisitArrayTypeLoc(TL); 6561 } 6562 6563 void TypeLocReader::VisitDependentSizedArrayTypeLoc( 6564 DependentSizedArrayTypeLoc TL) { 6565 VisitArrayTypeLoc(TL); 6566 } 6567 6568 void TypeLocReader::VisitDependentAddressSpaceTypeLoc( 6569 DependentAddressSpaceTypeLoc TL) { 6570 6571 TL.setAttrNameLoc(readSourceLocation()); 6572 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6573 TL.setAttrExprOperand(Reader.readExpr()); 6574 } 6575 6576 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( 6577 DependentSizedExtVectorTypeLoc TL) { 6578 TL.setNameLoc(readSourceLocation()); 6579 } 6580 6581 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { 6582 TL.setNameLoc(readSourceLocation()); 6583 } 6584 6585 void TypeLocReader::VisitDependentVectorTypeLoc( 6586 DependentVectorTypeLoc TL) { 6587 TL.setNameLoc(readSourceLocation()); 6588 } 6589 6590 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 6591 TL.setNameLoc(readSourceLocation()); 6592 } 6593 6594 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) { 6595 TL.setAttrNameLoc(readSourceLocation()); 6596 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6597 TL.setAttrRowOperand(Reader.readExpr()); 6598 TL.setAttrColumnOperand(Reader.readExpr()); 6599 } 6600 6601 void TypeLocReader::VisitDependentSizedMatrixTypeLoc( 6602 DependentSizedMatrixTypeLoc TL) { 6603 TL.setAttrNameLoc(readSourceLocation()); 6604 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6605 TL.setAttrRowOperand(Reader.readExpr()); 6606 TL.setAttrColumnOperand(Reader.readExpr()); 6607 } 6608 6609 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 6610 TL.setLocalRangeBegin(readSourceLocation()); 6611 TL.setLParenLoc(readSourceLocation()); 6612 TL.setRParenLoc(readSourceLocation()); 6613 TL.setExceptionSpecRange(Reader.readSourceRange()); 6614 TL.setLocalRangeEnd(readSourceLocation()); 6615 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) { 6616 TL.setParam(i, Reader.readDeclAs<ParmVarDecl>()); 6617 } 6618 } 6619 6620 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 6621 VisitFunctionTypeLoc(TL); 6622 } 6623 6624 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 6625 VisitFunctionTypeLoc(TL); 6626 } 6627 6628 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 6629 TL.setNameLoc(readSourceLocation()); 6630 } 6631 6632 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 6633 TL.setNameLoc(readSourceLocation()); 6634 } 6635 6636 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 6637 TL.setTypeofLoc(readSourceLocation()); 6638 TL.setLParenLoc(readSourceLocation()); 6639 TL.setRParenLoc(readSourceLocation()); 6640 } 6641 6642 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 6643 TL.setTypeofLoc(readSourceLocation()); 6644 TL.setLParenLoc(readSourceLocation()); 6645 TL.setRParenLoc(readSourceLocation()); 6646 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6647 } 6648 6649 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 6650 TL.setNameLoc(readSourceLocation()); 6651 } 6652 6653 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 6654 TL.setKWLoc(readSourceLocation()); 6655 TL.setLParenLoc(readSourceLocation()); 6656 TL.setRParenLoc(readSourceLocation()); 6657 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6658 } 6659 6660 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) { 6661 TL.setNameLoc(readSourceLocation()); 6662 if (Reader.readBool()) { 6663 TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc()); 6664 TL.setTemplateKWLoc(readSourceLocation()); 6665 TL.setConceptNameLoc(readSourceLocation()); 6666 TL.setFoundDecl(Reader.readDeclAs<NamedDecl>()); 6667 TL.setLAngleLoc(readSourceLocation()); 6668 TL.setRAngleLoc(readSourceLocation()); 6669 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6670 TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo( 6671 TL.getTypePtr()->getArg(i).getKind())); 6672 } 6673 } 6674 6675 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc( 6676 DeducedTemplateSpecializationTypeLoc TL) { 6677 TL.setTemplateNameLoc(readSourceLocation()); 6678 } 6679 6680 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { 6681 TL.setNameLoc(readSourceLocation()); 6682 } 6683 6684 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { 6685 TL.setNameLoc(readSourceLocation()); 6686 } 6687 6688 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 6689 TL.setAttr(ReadAttr()); 6690 } 6691 6692 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 6693 TL.setNameLoc(readSourceLocation()); 6694 } 6695 6696 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( 6697 SubstTemplateTypeParmTypeLoc TL) { 6698 TL.setNameLoc(readSourceLocation()); 6699 } 6700 6701 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc( 6702 SubstTemplateTypeParmPackTypeLoc TL) { 6703 TL.setNameLoc(readSourceLocation()); 6704 } 6705 6706 void TypeLocReader::VisitTemplateSpecializationTypeLoc( 6707 TemplateSpecializationTypeLoc TL) { 6708 TL.setTemplateKeywordLoc(readSourceLocation()); 6709 TL.setTemplateNameLoc(readSourceLocation()); 6710 TL.setLAngleLoc(readSourceLocation()); 6711 TL.setRAngleLoc(readSourceLocation()); 6712 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6713 TL.setArgLocInfo( 6714 i, 6715 Reader.readTemplateArgumentLocInfo( 6716 TL.getTypePtr()->getArg(i).getKind())); 6717 } 6718 6719 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) { 6720 TL.setLParenLoc(readSourceLocation()); 6721 TL.setRParenLoc(readSourceLocation()); 6722 } 6723 6724 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 6725 TL.setElaboratedKeywordLoc(readSourceLocation()); 6726 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6727 } 6728 6729 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 6730 TL.setNameLoc(readSourceLocation()); 6731 } 6732 6733 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 6734 TL.setElaboratedKeywordLoc(readSourceLocation()); 6735 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6736 TL.setNameLoc(readSourceLocation()); 6737 } 6738 6739 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( 6740 DependentTemplateSpecializationTypeLoc TL) { 6741 TL.setElaboratedKeywordLoc(readSourceLocation()); 6742 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6743 TL.setTemplateKeywordLoc(readSourceLocation()); 6744 TL.setTemplateNameLoc(readSourceLocation()); 6745 TL.setLAngleLoc(readSourceLocation()); 6746 TL.setRAngleLoc(readSourceLocation()); 6747 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 6748 TL.setArgLocInfo( 6749 I, 6750 Reader.readTemplateArgumentLocInfo( 6751 TL.getTypePtr()->getArg(I).getKind())); 6752 } 6753 6754 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 6755 TL.setEllipsisLoc(readSourceLocation()); 6756 } 6757 6758 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 6759 TL.setNameLoc(readSourceLocation()); 6760 } 6761 6762 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) { 6763 if (TL.getNumProtocols()) { 6764 TL.setProtocolLAngleLoc(readSourceLocation()); 6765 TL.setProtocolRAngleLoc(readSourceLocation()); 6766 } 6767 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6768 TL.setProtocolLoc(i, readSourceLocation()); 6769 } 6770 6771 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 6772 TL.setHasBaseTypeAsWritten(Reader.readBool()); 6773 TL.setTypeArgsLAngleLoc(readSourceLocation()); 6774 TL.setTypeArgsRAngleLoc(readSourceLocation()); 6775 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i) 6776 TL.setTypeArgTInfo(i, GetTypeSourceInfo()); 6777 TL.setProtocolLAngleLoc(readSourceLocation()); 6778 TL.setProtocolRAngleLoc(readSourceLocation()); 6779 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6780 TL.setProtocolLoc(i, readSourceLocation()); 6781 } 6782 6783 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 6784 TL.setStarLoc(readSourceLocation()); 6785 } 6786 6787 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) { 6788 TL.setKWLoc(readSourceLocation()); 6789 TL.setLParenLoc(readSourceLocation()); 6790 TL.setRParenLoc(readSourceLocation()); 6791 } 6792 6793 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) { 6794 TL.setKWLoc(readSourceLocation()); 6795 } 6796 6797 void TypeLocReader::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) { 6798 TL.setNameLoc(readSourceLocation()); 6799 } 6800 void TypeLocReader::VisitDependentExtIntTypeLoc( 6801 clang::DependentExtIntTypeLoc TL) { 6802 TL.setNameLoc(readSourceLocation()); 6803 } 6804 6805 6806 void ASTRecordReader::readTypeLoc(TypeLoc TL) { 6807 TypeLocReader TLR(*this); 6808 for (; !TL.isNull(); TL = TL.getNextTypeLoc()) 6809 TLR.Visit(TL); 6810 } 6811 6812 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() { 6813 QualType InfoTy = readType(); 6814 if (InfoTy.isNull()) 6815 return nullptr; 6816 6817 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy); 6818 readTypeLoc(TInfo->getTypeLoc()); 6819 return TInfo; 6820 } 6821 6822 QualType ASTReader::GetType(TypeID ID) { 6823 assert(ContextObj && "reading type with no AST context"); 6824 ASTContext &Context = *ContextObj; 6825 6826 unsigned FastQuals = ID & Qualifiers::FastMask; 6827 unsigned Index = ID >> Qualifiers::FastWidth; 6828 6829 if (Index < NUM_PREDEF_TYPE_IDS) { 6830 QualType T; 6831 switch ((PredefinedTypeIDs)Index) { 6832 case PREDEF_TYPE_NULL_ID: 6833 return QualType(); 6834 case PREDEF_TYPE_VOID_ID: 6835 T = Context.VoidTy; 6836 break; 6837 case PREDEF_TYPE_BOOL_ID: 6838 T = Context.BoolTy; 6839 break; 6840 case PREDEF_TYPE_CHAR_U_ID: 6841 case PREDEF_TYPE_CHAR_S_ID: 6842 // FIXME: Check that the signedness of CharTy is correct! 6843 T = Context.CharTy; 6844 break; 6845 case PREDEF_TYPE_UCHAR_ID: 6846 T = Context.UnsignedCharTy; 6847 break; 6848 case PREDEF_TYPE_USHORT_ID: 6849 T = Context.UnsignedShortTy; 6850 break; 6851 case PREDEF_TYPE_UINT_ID: 6852 T = Context.UnsignedIntTy; 6853 break; 6854 case PREDEF_TYPE_ULONG_ID: 6855 T = Context.UnsignedLongTy; 6856 break; 6857 case PREDEF_TYPE_ULONGLONG_ID: 6858 T = Context.UnsignedLongLongTy; 6859 break; 6860 case PREDEF_TYPE_UINT128_ID: 6861 T = Context.UnsignedInt128Ty; 6862 break; 6863 case PREDEF_TYPE_SCHAR_ID: 6864 T = Context.SignedCharTy; 6865 break; 6866 case PREDEF_TYPE_WCHAR_ID: 6867 T = Context.WCharTy; 6868 break; 6869 case PREDEF_TYPE_SHORT_ID: 6870 T = Context.ShortTy; 6871 break; 6872 case PREDEF_TYPE_INT_ID: 6873 T = Context.IntTy; 6874 break; 6875 case PREDEF_TYPE_LONG_ID: 6876 T = Context.LongTy; 6877 break; 6878 case PREDEF_TYPE_LONGLONG_ID: 6879 T = Context.LongLongTy; 6880 break; 6881 case PREDEF_TYPE_INT128_ID: 6882 T = Context.Int128Ty; 6883 break; 6884 case PREDEF_TYPE_BFLOAT16_ID: 6885 T = Context.BFloat16Ty; 6886 break; 6887 case PREDEF_TYPE_HALF_ID: 6888 T = Context.HalfTy; 6889 break; 6890 case PREDEF_TYPE_FLOAT_ID: 6891 T = Context.FloatTy; 6892 break; 6893 case PREDEF_TYPE_DOUBLE_ID: 6894 T = Context.DoubleTy; 6895 break; 6896 case PREDEF_TYPE_LONGDOUBLE_ID: 6897 T = Context.LongDoubleTy; 6898 break; 6899 case PREDEF_TYPE_SHORT_ACCUM_ID: 6900 T = Context.ShortAccumTy; 6901 break; 6902 case PREDEF_TYPE_ACCUM_ID: 6903 T = Context.AccumTy; 6904 break; 6905 case PREDEF_TYPE_LONG_ACCUM_ID: 6906 T = Context.LongAccumTy; 6907 break; 6908 case PREDEF_TYPE_USHORT_ACCUM_ID: 6909 T = Context.UnsignedShortAccumTy; 6910 break; 6911 case PREDEF_TYPE_UACCUM_ID: 6912 T = Context.UnsignedAccumTy; 6913 break; 6914 case PREDEF_TYPE_ULONG_ACCUM_ID: 6915 T = Context.UnsignedLongAccumTy; 6916 break; 6917 case PREDEF_TYPE_SHORT_FRACT_ID: 6918 T = Context.ShortFractTy; 6919 break; 6920 case PREDEF_TYPE_FRACT_ID: 6921 T = Context.FractTy; 6922 break; 6923 case PREDEF_TYPE_LONG_FRACT_ID: 6924 T = Context.LongFractTy; 6925 break; 6926 case PREDEF_TYPE_USHORT_FRACT_ID: 6927 T = Context.UnsignedShortFractTy; 6928 break; 6929 case PREDEF_TYPE_UFRACT_ID: 6930 T = Context.UnsignedFractTy; 6931 break; 6932 case PREDEF_TYPE_ULONG_FRACT_ID: 6933 T = Context.UnsignedLongFractTy; 6934 break; 6935 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID: 6936 T = Context.SatShortAccumTy; 6937 break; 6938 case PREDEF_TYPE_SAT_ACCUM_ID: 6939 T = Context.SatAccumTy; 6940 break; 6941 case PREDEF_TYPE_SAT_LONG_ACCUM_ID: 6942 T = Context.SatLongAccumTy; 6943 break; 6944 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID: 6945 T = Context.SatUnsignedShortAccumTy; 6946 break; 6947 case PREDEF_TYPE_SAT_UACCUM_ID: 6948 T = Context.SatUnsignedAccumTy; 6949 break; 6950 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID: 6951 T = Context.SatUnsignedLongAccumTy; 6952 break; 6953 case PREDEF_TYPE_SAT_SHORT_FRACT_ID: 6954 T = Context.SatShortFractTy; 6955 break; 6956 case PREDEF_TYPE_SAT_FRACT_ID: 6957 T = Context.SatFractTy; 6958 break; 6959 case PREDEF_TYPE_SAT_LONG_FRACT_ID: 6960 T = Context.SatLongFractTy; 6961 break; 6962 case PREDEF_TYPE_SAT_USHORT_FRACT_ID: 6963 T = Context.SatUnsignedShortFractTy; 6964 break; 6965 case PREDEF_TYPE_SAT_UFRACT_ID: 6966 T = Context.SatUnsignedFractTy; 6967 break; 6968 case PREDEF_TYPE_SAT_ULONG_FRACT_ID: 6969 T = Context.SatUnsignedLongFractTy; 6970 break; 6971 case PREDEF_TYPE_FLOAT16_ID: 6972 T = Context.Float16Ty; 6973 break; 6974 case PREDEF_TYPE_FLOAT128_ID: 6975 T = Context.Float128Ty; 6976 break; 6977 case PREDEF_TYPE_OVERLOAD_ID: 6978 T = Context.OverloadTy; 6979 break; 6980 case PREDEF_TYPE_BOUND_MEMBER: 6981 T = Context.BoundMemberTy; 6982 break; 6983 case PREDEF_TYPE_PSEUDO_OBJECT: 6984 T = Context.PseudoObjectTy; 6985 break; 6986 case PREDEF_TYPE_DEPENDENT_ID: 6987 T = Context.DependentTy; 6988 break; 6989 case PREDEF_TYPE_UNKNOWN_ANY: 6990 T = Context.UnknownAnyTy; 6991 break; 6992 case PREDEF_TYPE_NULLPTR_ID: 6993 T = Context.NullPtrTy; 6994 break; 6995 case PREDEF_TYPE_CHAR8_ID: 6996 T = Context.Char8Ty; 6997 break; 6998 case PREDEF_TYPE_CHAR16_ID: 6999 T = Context.Char16Ty; 7000 break; 7001 case PREDEF_TYPE_CHAR32_ID: 7002 T = Context.Char32Ty; 7003 break; 7004 case PREDEF_TYPE_OBJC_ID: 7005 T = Context.ObjCBuiltinIdTy; 7006 break; 7007 case PREDEF_TYPE_OBJC_CLASS: 7008 T = Context.ObjCBuiltinClassTy; 7009 break; 7010 case PREDEF_TYPE_OBJC_SEL: 7011 T = Context.ObjCBuiltinSelTy; 7012 break; 7013 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 7014 case PREDEF_TYPE_##Id##_ID: \ 7015 T = Context.SingletonId; \ 7016 break; 7017 #include "clang/Basic/OpenCLImageTypes.def" 7018 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ 7019 case PREDEF_TYPE_##Id##_ID: \ 7020 T = Context.Id##Ty; \ 7021 break; 7022 #include "clang/Basic/OpenCLExtensionTypes.def" 7023 case PREDEF_TYPE_SAMPLER_ID: 7024 T = Context.OCLSamplerTy; 7025 break; 7026 case PREDEF_TYPE_EVENT_ID: 7027 T = Context.OCLEventTy; 7028 break; 7029 case PREDEF_TYPE_CLK_EVENT_ID: 7030 T = Context.OCLClkEventTy; 7031 break; 7032 case PREDEF_TYPE_QUEUE_ID: 7033 T = Context.OCLQueueTy; 7034 break; 7035 case PREDEF_TYPE_RESERVE_ID_ID: 7036 T = Context.OCLReserveIDTy; 7037 break; 7038 case PREDEF_TYPE_AUTO_DEDUCT: 7039 T = Context.getAutoDeductType(); 7040 break; 7041 case PREDEF_TYPE_AUTO_RREF_DEDUCT: 7042 T = Context.getAutoRRefDeductType(); 7043 break; 7044 case PREDEF_TYPE_ARC_UNBRIDGED_CAST: 7045 T = Context.ARCUnbridgedCastTy; 7046 break; 7047 case PREDEF_TYPE_BUILTIN_FN: 7048 T = Context.BuiltinFnTy; 7049 break; 7050 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX: 7051 T = Context.IncompleteMatrixIdxTy; 7052 break; 7053 case PREDEF_TYPE_OMP_ARRAY_SECTION: 7054 T = Context.OMPArraySectionTy; 7055 break; 7056 case PREDEF_TYPE_OMP_ARRAY_SHAPING: 7057 T = Context.OMPArraySectionTy; 7058 break; 7059 case PREDEF_TYPE_OMP_ITERATOR: 7060 T = Context.OMPIteratorTy; 7061 break; 7062 #define SVE_TYPE(Name, Id, SingletonId) \ 7063 case PREDEF_TYPE_##Id##_ID: \ 7064 T = Context.SingletonId; \ 7065 break; 7066 #include "clang/Basic/AArch64SVEACLETypes.def" 7067 #define PPC_VECTOR_TYPE(Name, Id, Size) \ 7068 case PREDEF_TYPE_##Id##_ID: \ 7069 T = Context.Id##Ty; \ 7070 break; 7071 #include "clang/Basic/PPCTypes.def" 7072 #define RVV_TYPE(Name, Id, SingletonId) \ 7073 case PREDEF_TYPE_##Id##_ID: \ 7074 T = Context.SingletonId; \ 7075 break; 7076 #include "clang/Basic/RISCVVTypes.def" 7077 } 7078 7079 assert(!T.isNull() && "Unknown predefined type"); 7080 return T.withFastQualifiers(FastQuals); 7081 } 7082 7083 Index -= NUM_PREDEF_TYPE_IDS; 7084 assert(Index < TypesLoaded.size() && "Type index out-of-range"); 7085 if (TypesLoaded[Index].isNull()) { 7086 TypesLoaded[Index] = readTypeRecord(Index); 7087 if (TypesLoaded[Index].isNull()) 7088 return QualType(); 7089 7090 TypesLoaded[Index]->setFromAST(); 7091 if (DeserializationListener) 7092 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID), 7093 TypesLoaded[Index]); 7094 } 7095 7096 return TypesLoaded[Index].withFastQualifiers(FastQuals); 7097 } 7098 7099 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) { 7100 return GetType(getGlobalTypeID(F, LocalID)); 7101 } 7102 7103 serialization::TypeID 7104 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const { 7105 unsigned FastQuals = LocalID & Qualifiers::FastMask; 7106 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth; 7107 7108 if (LocalIndex < NUM_PREDEF_TYPE_IDS) 7109 return LocalID; 7110 7111 if (!F.ModuleOffsetMap.empty()) 7112 ReadModuleOffsetMap(F); 7113 7114 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7115 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS); 7116 assert(I != F.TypeRemap.end() && "Invalid index into type index remap"); 7117 7118 unsigned GlobalIndex = LocalIndex + I->second; 7119 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals; 7120 } 7121 7122 TemplateArgumentLocInfo 7123 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) { 7124 switch (Kind) { 7125 case TemplateArgument::Expression: 7126 return readExpr(); 7127 case TemplateArgument::Type: 7128 return readTypeSourceInfo(); 7129 case TemplateArgument::Template: { 7130 NestedNameSpecifierLoc QualifierLoc = 7131 readNestedNameSpecifierLoc(); 7132 SourceLocation TemplateNameLoc = readSourceLocation(); 7133 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc, 7134 TemplateNameLoc, SourceLocation()); 7135 } 7136 case TemplateArgument::TemplateExpansion: { 7137 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc(); 7138 SourceLocation TemplateNameLoc = readSourceLocation(); 7139 SourceLocation EllipsisLoc = readSourceLocation(); 7140 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc, 7141 TemplateNameLoc, EllipsisLoc); 7142 } 7143 case TemplateArgument::Null: 7144 case TemplateArgument::Integral: 7145 case TemplateArgument::Declaration: 7146 case TemplateArgument::NullPtr: 7147 case TemplateArgument::Pack: 7148 // FIXME: Is this right? 7149 return TemplateArgumentLocInfo(); 7150 } 7151 llvm_unreachable("unexpected template argument loc"); 7152 } 7153 7154 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() { 7155 TemplateArgument Arg = readTemplateArgument(); 7156 7157 if (Arg.getKind() == TemplateArgument::Expression) { 7158 if (readBool()) // bool InfoHasSameExpr. 7159 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); 7160 } 7161 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind())); 7162 } 7163 7164 const ASTTemplateArgumentListInfo * 7165 ASTRecordReader::readASTTemplateArgumentListInfo() { 7166 SourceLocation LAngleLoc = readSourceLocation(); 7167 SourceLocation RAngleLoc = readSourceLocation(); 7168 unsigned NumArgsAsWritten = readInt(); 7169 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc); 7170 for (unsigned i = 0; i != NumArgsAsWritten; ++i) 7171 TemplArgsInfo.addArgument(readTemplateArgumentLoc()); 7172 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo); 7173 } 7174 7175 Decl *ASTReader::GetExternalDecl(uint32_t ID) { 7176 return GetDecl(ID); 7177 } 7178 7179 void ASTReader::CompleteRedeclChain(const Decl *D) { 7180 if (NumCurrentElementsDeserializing) { 7181 // We arrange to not care about the complete redeclaration chain while we're 7182 // deserializing. Just remember that the AST has marked this one as complete 7183 // but that it's not actually complete yet, so we know we still need to 7184 // complete it later. 7185 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D)); 7186 return; 7187 } 7188 7189 const DeclContext *DC = D->getDeclContext()->getRedeclContext(); 7190 7191 // If this is a named declaration, complete it by looking it up 7192 // within its context. 7193 // 7194 // FIXME: Merging a function definition should merge 7195 // all mergeable entities within it. 7196 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) || 7197 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) { 7198 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) { 7199 if (!getContext().getLangOpts().CPlusPlus && 7200 isa<TranslationUnitDecl>(DC)) { 7201 // Outside of C++, we don't have a lookup table for the TU, so update 7202 // the identifier instead. (For C++ modules, we don't store decls 7203 // in the serialized identifier table, so we do the lookup in the TU.) 7204 auto *II = Name.getAsIdentifierInfo(); 7205 assert(II && "non-identifier name in C?"); 7206 if (II->isOutOfDate()) 7207 updateOutOfDateIdentifier(*II); 7208 } else 7209 DC->lookup(Name); 7210 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) { 7211 // Find all declarations of this kind from the relevant context. 7212 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) { 7213 auto *DC = cast<DeclContext>(DCDecl); 7214 SmallVector<Decl*, 8> Decls; 7215 FindExternalLexicalDecls( 7216 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls); 7217 } 7218 } 7219 } 7220 7221 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) 7222 CTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7223 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) 7224 VTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7225 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 7226 if (auto *Template = FD->getPrimaryTemplate()) 7227 Template->LoadLazySpecializations(); 7228 } 7229 } 7230 7231 CXXCtorInitializer ** 7232 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) { 7233 RecordLocation Loc = getLocalBitOffset(Offset); 7234 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7235 SavedStreamPosition SavedPosition(Cursor); 7236 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7237 Error(std::move(Err)); 7238 return nullptr; 7239 } 7240 ReadingKindTracker ReadingKind(Read_Decl, *this); 7241 7242 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7243 if (!MaybeCode) { 7244 Error(MaybeCode.takeError()); 7245 return nullptr; 7246 } 7247 unsigned Code = MaybeCode.get(); 7248 7249 ASTRecordReader Record(*this, *Loc.F); 7250 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7251 if (!MaybeRecCode) { 7252 Error(MaybeRecCode.takeError()); 7253 return nullptr; 7254 } 7255 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) { 7256 Error("malformed AST file: missing C++ ctor initializers"); 7257 return nullptr; 7258 } 7259 7260 return Record.readCXXCtorInitializers(); 7261 } 7262 7263 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) { 7264 assert(ContextObj && "reading base specifiers with no AST context"); 7265 ASTContext &Context = *ContextObj; 7266 7267 RecordLocation Loc = getLocalBitOffset(Offset); 7268 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7269 SavedStreamPosition SavedPosition(Cursor); 7270 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7271 Error(std::move(Err)); 7272 return nullptr; 7273 } 7274 ReadingKindTracker ReadingKind(Read_Decl, *this); 7275 7276 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7277 if (!MaybeCode) { 7278 Error(MaybeCode.takeError()); 7279 return nullptr; 7280 } 7281 unsigned Code = MaybeCode.get(); 7282 7283 ASTRecordReader Record(*this, *Loc.F); 7284 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7285 if (!MaybeRecCode) { 7286 Error(MaybeCode.takeError()); 7287 return nullptr; 7288 } 7289 unsigned RecCode = MaybeRecCode.get(); 7290 7291 if (RecCode != DECL_CXX_BASE_SPECIFIERS) { 7292 Error("malformed AST file: missing C++ base specifiers"); 7293 return nullptr; 7294 } 7295 7296 unsigned NumBases = Record.readInt(); 7297 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases); 7298 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases]; 7299 for (unsigned I = 0; I != NumBases; ++I) 7300 Bases[I] = Record.readCXXBaseSpecifier(); 7301 return Bases; 7302 } 7303 7304 serialization::DeclID 7305 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const { 7306 if (LocalID < NUM_PREDEF_DECL_IDS) 7307 return LocalID; 7308 7309 if (!F.ModuleOffsetMap.empty()) 7310 ReadModuleOffsetMap(F); 7311 7312 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7313 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS); 7314 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap"); 7315 7316 return LocalID + I->second; 7317 } 7318 7319 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID, 7320 ModuleFile &M) const { 7321 // Predefined decls aren't from any module. 7322 if (ID < NUM_PREDEF_DECL_IDS) 7323 return false; 7324 7325 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID && 7326 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls; 7327 } 7328 7329 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) { 7330 if (!D->isFromASTFile()) 7331 return nullptr; 7332 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID()); 7333 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7334 return I->second; 7335 } 7336 7337 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) { 7338 if (ID < NUM_PREDEF_DECL_IDS) 7339 return SourceLocation(); 7340 7341 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7342 7343 if (Index > DeclsLoaded.size()) { 7344 Error("declaration ID out-of-range for AST file"); 7345 return SourceLocation(); 7346 } 7347 7348 if (Decl *D = DeclsLoaded[Index]) 7349 return D->getLocation(); 7350 7351 SourceLocation Loc; 7352 DeclCursorForID(ID, Loc); 7353 return Loc; 7354 } 7355 7356 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) { 7357 switch (ID) { 7358 case PREDEF_DECL_NULL_ID: 7359 return nullptr; 7360 7361 case PREDEF_DECL_TRANSLATION_UNIT_ID: 7362 return Context.getTranslationUnitDecl(); 7363 7364 case PREDEF_DECL_OBJC_ID_ID: 7365 return Context.getObjCIdDecl(); 7366 7367 case PREDEF_DECL_OBJC_SEL_ID: 7368 return Context.getObjCSelDecl(); 7369 7370 case PREDEF_DECL_OBJC_CLASS_ID: 7371 return Context.getObjCClassDecl(); 7372 7373 case PREDEF_DECL_OBJC_PROTOCOL_ID: 7374 return Context.getObjCProtocolDecl(); 7375 7376 case PREDEF_DECL_INT_128_ID: 7377 return Context.getInt128Decl(); 7378 7379 case PREDEF_DECL_UNSIGNED_INT_128_ID: 7380 return Context.getUInt128Decl(); 7381 7382 case PREDEF_DECL_OBJC_INSTANCETYPE_ID: 7383 return Context.getObjCInstanceTypeDecl(); 7384 7385 case PREDEF_DECL_BUILTIN_VA_LIST_ID: 7386 return Context.getBuiltinVaListDecl(); 7387 7388 case PREDEF_DECL_VA_LIST_TAG: 7389 return Context.getVaListTagDecl(); 7390 7391 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID: 7392 return Context.getBuiltinMSVaListDecl(); 7393 7394 case PREDEF_DECL_BUILTIN_MS_GUID_ID: 7395 return Context.getMSGuidTagDecl(); 7396 7397 case PREDEF_DECL_EXTERN_C_CONTEXT_ID: 7398 return Context.getExternCContextDecl(); 7399 7400 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID: 7401 return Context.getMakeIntegerSeqDecl(); 7402 7403 case PREDEF_DECL_CF_CONSTANT_STRING_ID: 7404 return Context.getCFConstantStringDecl(); 7405 7406 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID: 7407 return Context.getCFConstantStringTagDecl(); 7408 7409 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID: 7410 return Context.getTypePackElementDecl(); 7411 } 7412 llvm_unreachable("PredefinedDeclIDs unknown enum value"); 7413 } 7414 7415 Decl *ASTReader::GetExistingDecl(DeclID ID) { 7416 assert(ContextObj && "reading decl with no AST context"); 7417 if (ID < NUM_PREDEF_DECL_IDS) { 7418 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID); 7419 if (D) { 7420 // Track that we have merged the declaration with ID \p ID into the 7421 // pre-existing predefined declaration \p D. 7422 auto &Merged = KeyDecls[D->getCanonicalDecl()]; 7423 if (Merged.empty()) 7424 Merged.push_back(ID); 7425 } 7426 return D; 7427 } 7428 7429 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7430 7431 if (Index >= DeclsLoaded.size()) { 7432 assert(0 && "declaration ID out-of-range for AST file"); 7433 Error("declaration ID out-of-range for AST file"); 7434 return nullptr; 7435 } 7436 7437 return DeclsLoaded[Index]; 7438 } 7439 7440 Decl *ASTReader::GetDecl(DeclID ID) { 7441 if (ID < NUM_PREDEF_DECL_IDS) 7442 return GetExistingDecl(ID); 7443 7444 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7445 7446 if (Index >= DeclsLoaded.size()) { 7447 assert(0 && "declaration ID out-of-range for AST file"); 7448 Error("declaration ID out-of-range for AST file"); 7449 return nullptr; 7450 } 7451 7452 if (!DeclsLoaded[Index]) { 7453 ReadDeclRecord(ID); 7454 if (DeserializationListener) 7455 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); 7456 } 7457 7458 return DeclsLoaded[Index]; 7459 } 7460 7461 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M, 7462 DeclID GlobalID) { 7463 if (GlobalID < NUM_PREDEF_DECL_IDS) 7464 return GlobalID; 7465 7466 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID); 7467 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7468 ModuleFile *Owner = I->second; 7469 7470 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos 7471 = M.GlobalToLocalDeclIDs.find(Owner); 7472 if (Pos == M.GlobalToLocalDeclIDs.end()) 7473 return 0; 7474 7475 return GlobalID - Owner->BaseDeclID + Pos->second; 7476 } 7477 7478 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F, 7479 const RecordData &Record, 7480 unsigned &Idx) { 7481 if (Idx >= Record.size()) { 7482 Error("Corrupted AST file"); 7483 return 0; 7484 } 7485 7486 return getGlobalDeclID(F, Record[Idx++]); 7487 } 7488 7489 /// Resolve the offset of a statement into a statement. 7490 /// 7491 /// This operation will read a new statement from the external 7492 /// source each time it is called, and is meant to be used via a 7493 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 7494 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) { 7495 // Switch case IDs are per Decl. 7496 ClearSwitchCaseIDs(); 7497 7498 // Offset here is a global offset across the entire chain. 7499 RecordLocation Loc = getLocalBitOffset(Offset); 7500 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) { 7501 Error(std::move(Err)); 7502 return nullptr; 7503 } 7504 assert(NumCurrentElementsDeserializing == 0 && 7505 "should not be called while already deserializing"); 7506 Deserializing D(this); 7507 return ReadStmtFromStream(*Loc.F); 7508 } 7509 7510 void ASTReader::FindExternalLexicalDecls( 7511 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant, 7512 SmallVectorImpl<Decl *> &Decls) { 7513 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {}; 7514 7515 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) { 7516 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries"); 7517 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) { 7518 auto K = (Decl::Kind)+LexicalDecls[I]; 7519 if (!IsKindWeWant(K)) 7520 continue; 7521 7522 auto ID = (serialization::DeclID)+LexicalDecls[I + 1]; 7523 7524 // Don't add predefined declarations to the lexical context more 7525 // than once. 7526 if (ID < NUM_PREDEF_DECL_IDS) { 7527 if (PredefsVisited[ID]) 7528 continue; 7529 7530 PredefsVisited[ID] = true; 7531 } 7532 7533 if (Decl *D = GetLocalDecl(*M, ID)) { 7534 assert(D->getKind() == K && "wrong kind for lexical decl"); 7535 if (!DC->isDeclInLexicalTraversal(D)) 7536 Decls.push_back(D); 7537 } 7538 } 7539 }; 7540 7541 if (isa<TranslationUnitDecl>(DC)) { 7542 for (auto Lexical : TULexicalDecls) 7543 Visit(Lexical.first, Lexical.second); 7544 } else { 7545 auto I = LexicalDecls.find(DC); 7546 if (I != LexicalDecls.end()) 7547 Visit(I->second.first, I->second.second); 7548 } 7549 7550 ++NumLexicalDeclContextsRead; 7551 } 7552 7553 namespace { 7554 7555 class DeclIDComp { 7556 ASTReader &Reader; 7557 ModuleFile &Mod; 7558 7559 public: 7560 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {} 7561 7562 bool operator()(LocalDeclID L, LocalDeclID R) const { 7563 SourceLocation LHS = getLocation(L); 7564 SourceLocation RHS = getLocation(R); 7565 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7566 } 7567 7568 bool operator()(SourceLocation LHS, LocalDeclID R) const { 7569 SourceLocation RHS = getLocation(R); 7570 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7571 } 7572 7573 bool operator()(LocalDeclID L, SourceLocation RHS) const { 7574 SourceLocation LHS = getLocation(L); 7575 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7576 } 7577 7578 SourceLocation getLocation(LocalDeclID ID) const { 7579 return Reader.getSourceManager().getFileLoc( 7580 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID))); 7581 } 7582 }; 7583 7584 } // namespace 7585 7586 void ASTReader::FindFileRegionDecls(FileID File, 7587 unsigned Offset, unsigned Length, 7588 SmallVectorImpl<Decl *> &Decls) { 7589 SourceManager &SM = getSourceManager(); 7590 7591 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File); 7592 if (I == FileDeclIDs.end()) 7593 return; 7594 7595 FileDeclsInfo &DInfo = I->second; 7596 if (DInfo.Decls.empty()) 7597 return; 7598 7599 SourceLocation 7600 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset); 7601 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length); 7602 7603 DeclIDComp DIDComp(*this, *DInfo.Mod); 7604 ArrayRef<serialization::LocalDeclID>::iterator BeginIt = 7605 llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp); 7606 if (BeginIt != DInfo.Decls.begin()) 7607 --BeginIt; 7608 7609 // If we are pointing at a top-level decl inside an objc container, we need 7610 // to backtrack until we find it otherwise we will fail to report that the 7611 // region overlaps with an objc container. 7612 while (BeginIt != DInfo.Decls.begin() && 7613 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt)) 7614 ->isTopLevelDeclInObjCContainer()) 7615 --BeginIt; 7616 7617 ArrayRef<serialization::LocalDeclID>::iterator EndIt = 7618 llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp); 7619 if (EndIt != DInfo.Decls.end()) 7620 ++EndIt; 7621 7622 for (ArrayRef<serialization::LocalDeclID>::iterator 7623 DIt = BeginIt; DIt != EndIt; ++DIt) 7624 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt))); 7625 } 7626 7627 bool 7628 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, 7629 DeclarationName Name) { 7630 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() && 7631 "DeclContext has no visible decls in storage"); 7632 if (!Name) 7633 return false; 7634 7635 auto It = Lookups.find(DC); 7636 if (It == Lookups.end()) 7637 return false; 7638 7639 Deserializing LookupResults(this); 7640 7641 // Load the list of declarations. 7642 SmallVector<NamedDecl *, 64> Decls; 7643 for (DeclID ID : It->second.Table.find(Name)) { 7644 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7645 if (ND->getDeclName() == Name) 7646 Decls.push_back(ND); 7647 } 7648 7649 ++NumVisibleDeclContextsRead; 7650 SetExternalVisibleDeclsForName(DC, Name, Decls); 7651 return !Decls.empty(); 7652 } 7653 7654 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) { 7655 if (!DC->hasExternalVisibleStorage()) 7656 return; 7657 7658 auto It = Lookups.find(DC); 7659 assert(It != Lookups.end() && 7660 "have external visible storage but no lookup tables"); 7661 7662 DeclsMap Decls; 7663 7664 for (DeclID ID : It->second.Table.findAll()) { 7665 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7666 Decls[ND->getDeclName()].push_back(ND); 7667 } 7668 7669 ++NumVisibleDeclContextsRead; 7670 7671 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) { 7672 SetExternalVisibleDeclsForName(DC, I->first, I->second); 7673 } 7674 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false); 7675 } 7676 7677 const serialization::reader::DeclContextLookupTable * 7678 ASTReader::getLoadedLookupTables(DeclContext *Primary) const { 7679 auto I = Lookups.find(Primary); 7680 return I == Lookups.end() ? nullptr : &I->second; 7681 } 7682 7683 /// Under non-PCH compilation the consumer receives the objc methods 7684 /// before receiving the implementation, and codegen depends on this. 7685 /// We simulate this by deserializing and passing to consumer the methods of the 7686 /// implementation before passing the deserialized implementation decl. 7687 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD, 7688 ASTConsumer *Consumer) { 7689 assert(ImplD && Consumer); 7690 7691 for (auto *I : ImplD->methods()) 7692 Consumer->HandleInterestingDecl(DeclGroupRef(I)); 7693 7694 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD)); 7695 } 7696 7697 void ASTReader::PassInterestingDeclToConsumer(Decl *D) { 7698 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) 7699 PassObjCImplDeclToConsumer(ImplD, Consumer); 7700 else 7701 Consumer->HandleInterestingDecl(DeclGroupRef(D)); 7702 } 7703 7704 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) { 7705 this->Consumer = Consumer; 7706 7707 if (Consumer) 7708 PassInterestingDeclsToConsumer(); 7709 7710 if (DeserializationListener) 7711 DeserializationListener->ReaderInitialized(this); 7712 } 7713 7714 void ASTReader::PrintStats() { 7715 std::fprintf(stderr, "*** AST File Statistics:\n"); 7716 7717 unsigned NumTypesLoaded 7718 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), 7719 QualType()); 7720 unsigned NumDeclsLoaded 7721 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), 7722 (Decl *)nullptr); 7723 unsigned NumIdentifiersLoaded 7724 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), 7725 IdentifiersLoaded.end(), 7726 (IdentifierInfo *)nullptr); 7727 unsigned NumMacrosLoaded 7728 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(), 7729 MacrosLoaded.end(), 7730 (MacroInfo *)nullptr); 7731 unsigned NumSelectorsLoaded 7732 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), 7733 SelectorsLoaded.end(), 7734 Selector()); 7735 7736 if (unsigned TotalNumSLocEntries = getTotalNumSLocs()) 7737 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", 7738 NumSLocEntriesRead, TotalNumSLocEntries, 7739 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); 7740 if (!TypesLoaded.empty()) 7741 std::fprintf(stderr, " %u/%u types read (%f%%)\n", 7742 NumTypesLoaded, (unsigned)TypesLoaded.size(), 7743 ((float)NumTypesLoaded/TypesLoaded.size() * 100)); 7744 if (!DeclsLoaded.empty()) 7745 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", 7746 NumDeclsLoaded, (unsigned)DeclsLoaded.size(), 7747 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); 7748 if (!IdentifiersLoaded.empty()) 7749 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", 7750 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), 7751 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); 7752 if (!MacrosLoaded.empty()) 7753 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7754 NumMacrosLoaded, (unsigned)MacrosLoaded.size(), 7755 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100)); 7756 if (!SelectorsLoaded.empty()) 7757 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", 7758 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), 7759 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); 7760 if (TotalNumStatements) 7761 std::fprintf(stderr, " %u/%u statements read (%f%%)\n", 7762 NumStatementsRead, TotalNumStatements, 7763 ((float)NumStatementsRead/TotalNumStatements * 100)); 7764 if (TotalNumMacros) 7765 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7766 NumMacrosRead, TotalNumMacros, 7767 ((float)NumMacrosRead/TotalNumMacros * 100)); 7768 if (TotalLexicalDeclContexts) 7769 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", 7770 NumLexicalDeclContextsRead, TotalLexicalDeclContexts, 7771 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts 7772 * 100)); 7773 if (TotalVisibleDeclContexts) 7774 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", 7775 NumVisibleDeclContextsRead, TotalVisibleDeclContexts, 7776 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts 7777 * 100)); 7778 if (TotalNumMethodPoolEntries) 7779 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", 7780 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, 7781 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries 7782 * 100)); 7783 if (NumMethodPoolLookups) 7784 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n", 7785 NumMethodPoolHits, NumMethodPoolLookups, 7786 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0)); 7787 if (NumMethodPoolTableLookups) 7788 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n", 7789 NumMethodPoolTableHits, NumMethodPoolTableLookups, 7790 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups 7791 * 100.0)); 7792 if (NumIdentifierLookupHits) 7793 std::fprintf(stderr, 7794 " %u / %u identifier table lookups succeeded (%f%%)\n", 7795 NumIdentifierLookupHits, NumIdentifierLookups, 7796 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups); 7797 7798 if (GlobalIndex) { 7799 std::fprintf(stderr, "\n"); 7800 GlobalIndex->printStats(); 7801 } 7802 7803 std::fprintf(stderr, "\n"); 7804 dump(); 7805 std::fprintf(stderr, "\n"); 7806 } 7807 7808 template<typename Key, typename ModuleFile, unsigned InitialCapacity> 7809 LLVM_DUMP_METHOD static void 7810 dumpModuleIDMap(StringRef Name, 7811 const ContinuousRangeMap<Key, ModuleFile *, 7812 InitialCapacity> &Map) { 7813 if (Map.begin() == Map.end()) 7814 return; 7815 7816 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>; 7817 7818 llvm::errs() << Name << ":\n"; 7819 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end(); 7820 I != IEnd; ++I) { 7821 llvm::errs() << " " << I->first << " -> " << I->second->FileName 7822 << "\n"; 7823 } 7824 } 7825 7826 LLVM_DUMP_METHOD void ASTReader::dump() { 7827 llvm::errs() << "*** PCH/ModuleFile Remappings:\n"; 7828 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap); 7829 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap); 7830 dumpModuleIDMap("Global type map", GlobalTypeMap); 7831 dumpModuleIDMap("Global declaration map", GlobalDeclMap); 7832 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap); 7833 dumpModuleIDMap("Global macro map", GlobalMacroMap); 7834 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap); 7835 dumpModuleIDMap("Global selector map", GlobalSelectorMap); 7836 dumpModuleIDMap("Global preprocessed entity map", 7837 GlobalPreprocessedEntityMap); 7838 7839 llvm::errs() << "\n*** PCH/Modules Loaded:"; 7840 for (ModuleFile &M : ModuleMgr) 7841 M.dump(); 7842 } 7843 7844 /// Return the amount of memory used by memory buffers, breaking down 7845 /// by heap-backed versus mmap'ed memory. 7846 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const { 7847 for (ModuleFile &I : ModuleMgr) { 7848 if (llvm::MemoryBuffer *buf = I.Buffer) { 7849 size_t bytes = buf->getBufferSize(); 7850 switch (buf->getBufferKind()) { 7851 case llvm::MemoryBuffer::MemoryBuffer_Malloc: 7852 sizes.malloc_bytes += bytes; 7853 break; 7854 case llvm::MemoryBuffer::MemoryBuffer_MMap: 7855 sizes.mmap_bytes += bytes; 7856 break; 7857 } 7858 } 7859 } 7860 } 7861 7862 void ASTReader::InitializeSema(Sema &S) { 7863 SemaObj = &S; 7864 S.addExternalSource(this); 7865 7866 // Makes sure any declarations that were deserialized "too early" 7867 // still get added to the identifier's declaration chains. 7868 for (uint64_t ID : PreloadedDeclIDs) { 7869 NamedDecl *D = cast<NamedDecl>(GetDecl(ID)); 7870 pushExternalDeclIntoScope(D, D->getDeclName()); 7871 } 7872 PreloadedDeclIDs.clear(); 7873 7874 // FIXME: What happens if these are changed by a module import? 7875 if (!FPPragmaOptions.empty()) { 7876 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS"); 7877 FPOptionsOverride NewOverrides = 7878 FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]); 7879 SemaObj->CurFPFeatures = 7880 NewOverrides.applyOverrides(SemaObj->getLangOpts()); 7881 } 7882 7883 SemaObj->OpenCLFeatures = OpenCLExtensions; 7884 SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap; 7885 SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap; 7886 7887 UpdateSema(); 7888 } 7889 7890 void ASTReader::UpdateSema() { 7891 assert(SemaObj && "no Sema to update"); 7892 7893 // Load the offsets of the declarations that Sema references. 7894 // They will be lazily deserialized when needed. 7895 if (!SemaDeclRefs.empty()) { 7896 assert(SemaDeclRefs.size() % 3 == 0); 7897 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) { 7898 if (!SemaObj->StdNamespace) 7899 SemaObj->StdNamespace = SemaDeclRefs[I]; 7900 if (!SemaObj->StdBadAlloc) 7901 SemaObj->StdBadAlloc = SemaDeclRefs[I+1]; 7902 if (!SemaObj->StdAlignValT) 7903 SemaObj->StdAlignValT = SemaDeclRefs[I+2]; 7904 } 7905 SemaDeclRefs.clear(); 7906 } 7907 7908 // Update the state of pragmas. Use the same API as if we had encountered the 7909 // pragma in the source. 7910 if(OptimizeOffPragmaLocation.isValid()) 7911 SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation); 7912 if (PragmaMSStructState != -1) 7913 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState); 7914 if (PointersToMembersPragmaLocation.isValid()) { 7915 SemaObj->ActOnPragmaMSPointersToMembers( 7916 (LangOptions::PragmaMSPointersToMembersKind) 7917 PragmaMSPointersToMembersState, 7918 PointersToMembersPragmaLocation); 7919 } 7920 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth; 7921 7922 if (PragmaAlignPackCurrentValue) { 7923 // The bottom of the stack might have a default value. It must be adjusted 7924 // to the current value to ensure that the packing state is preserved after 7925 // popping entries that were included/imported from a PCH/module. 7926 bool DropFirst = false; 7927 if (!PragmaAlignPackStack.empty() && 7928 PragmaAlignPackStack.front().Location.isInvalid()) { 7929 assert(PragmaAlignPackStack.front().Value == 7930 SemaObj->AlignPackStack.DefaultValue && 7931 "Expected a default alignment value"); 7932 SemaObj->AlignPackStack.Stack.emplace_back( 7933 PragmaAlignPackStack.front().SlotLabel, 7934 SemaObj->AlignPackStack.CurrentValue, 7935 SemaObj->AlignPackStack.CurrentPragmaLocation, 7936 PragmaAlignPackStack.front().PushLocation); 7937 DropFirst = true; 7938 } 7939 for (const auto &Entry : llvm::makeArrayRef(PragmaAlignPackStack) 7940 .drop_front(DropFirst ? 1 : 0)) { 7941 SemaObj->AlignPackStack.Stack.emplace_back( 7942 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation); 7943 } 7944 if (PragmaAlignPackCurrentLocation.isInvalid()) { 7945 assert(*PragmaAlignPackCurrentValue == 7946 SemaObj->AlignPackStack.DefaultValue && 7947 "Expected a default align and pack value"); 7948 // Keep the current values. 7949 } else { 7950 SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue; 7951 SemaObj->AlignPackStack.CurrentPragmaLocation = 7952 PragmaAlignPackCurrentLocation; 7953 } 7954 } 7955 if (FpPragmaCurrentValue) { 7956 // The bottom of the stack might have a default value. It must be adjusted 7957 // to the current value to ensure that fp-pragma state is preserved after 7958 // popping entries that were included/imported from a PCH/module. 7959 bool DropFirst = false; 7960 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) { 7961 assert(FpPragmaStack.front().Value == 7962 SemaObj->FpPragmaStack.DefaultValue && 7963 "Expected a default pragma float_control value"); 7964 SemaObj->FpPragmaStack.Stack.emplace_back( 7965 FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue, 7966 SemaObj->FpPragmaStack.CurrentPragmaLocation, 7967 FpPragmaStack.front().PushLocation); 7968 DropFirst = true; 7969 } 7970 for (const auto &Entry : 7971 llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0)) 7972 SemaObj->FpPragmaStack.Stack.emplace_back( 7973 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation); 7974 if (FpPragmaCurrentLocation.isInvalid()) { 7975 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue && 7976 "Expected a default pragma float_control value"); 7977 // Keep the current values. 7978 } else { 7979 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue; 7980 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation; 7981 } 7982 } 7983 7984 // For non-modular AST files, restore visiblity of modules. 7985 for (auto &Import : ImportedModules) { 7986 if (Import.ImportLoc.isInvalid()) 7987 continue; 7988 if (Module *Imported = getSubmodule(Import.ID)) { 7989 SemaObj->makeModuleVisible(Imported, Import.ImportLoc); 7990 } 7991 } 7992 } 7993 7994 IdentifierInfo *ASTReader::get(StringRef Name) { 7995 // Note that we are loading an identifier. 7996 Deserializing AnIdentifier(this); 7997 7998 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0, 7999 NumIdentifierLookups, 8000 NumIdentifierLookupHits); 8001 8002 // We don't need to do identifier table lookups in C++ modules (we preload 8003 // all interesting declarations, and don't need to use the scope for name 8004 // lookups). Perform the lookup in PCH files, though, since we don't build 8005 // a complete initial identifier table if we're carrying on from a PCH. 8006 if (PP.getLangOpts().CPlusPlus) { 8007 for (auto F : ModuleMgr.pch_modules()) 8008 if (Visitor(*F)) 8009 break; 8010 } else { 8011 // If there is a global index, look there first to determine which modules 8012 // provably do not have any results for this identifier. 8013 GlobalModuleIndex::HitSet Hits; 8014 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 8015 if (!loadGlobalIndex()) { 8016 if (GlobalIndex->lookupIdentifier(Name, Hits)) { 8017 HitsPtr = &Hits; 8018 } 8019 } 8020 8021 ModuleMgr.visit(Visitor, HitsPtr); 8022 } 8023 8024 IdentifierInfo *II = Visitor.getIdentifierInfo(); 8025 markIdentifierUpToDate(II); 8026 return II; 8027 } 8028 8029 namespace clang { 8030 8031 /// An identifier-lookup iterator that enumerates all of the 8032 /// identifiers stored within a set of AST files. 8033 class ASTIdentifierIterator : public IdentifierIterator { 8034 /// The AST reader whose identifiers are being enumerated. 8035 const ASTReader &Reader; 8036 8037 /// The current index into the chain of AST files stored in 8038 /// the AST reader. 8039 unsigned Index; 8040 8041 /// The current position within the identifier lookup table 8042 /// of the current AST file. 8043 ASTIdentifierLookupTable::key_iterator Current; 8044 8045 /// The end position within the identifier lookup table of 8046 /// the current AST file. 8047 ASTIdentifierLookupTable::key_iterator End; 8048 8049 /// Whether to skip any modules in the ASTReader. 8050 bool SkipModules; 8051 8052 public: 8053 explicit ASTIdentifierIterator(const ASTReader &Reader, 8054 bool SkipModules = false); 8055 8056 StringRef Next() override; 8057 }; 8058 8059 } // namespace clang 8060 8061 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader, 8062 bool SkipModules) 8063 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) { 8064 } 8065 8066 StringRef ASTIdentifierIterator::Next() { 8067 while (Current == End) { 8068 // If we have exhausted all of our AST files, we're done. 8069 if (Index == 0) 8070 return StringRef(); 8071 8072 --Index; 8073 ModuleFile &F = Reader.ModuleMgr[Index]; 8074 if (SkipModules && F.isModule()) 8075 continue; 8076 8077 ASTIdentifierLookupTable *IdTable = 8078 (ASTIdentifierLookupTable *)F.IdentifierLookupTable; 8079 Current = IdTable->key_begin(); 8080 End = IdTable->key_end(); 8081 } 8082 8083 // We have any identifiers remaining in the current AST file; return 8084 // the next one. 8085 StringRef Result = *Current; 8086 ++Current; 8087 return Result; 8088 } 8089 8090 namespace { 8091 8092 /// A utility for appending two IdentifierIterators. 8093 class ChainedIdentifierIterator : public IdentifierIterator { 8094 std::unique_ptr<IdentifierIterator> Current; 8095 std::unique_ptr<IdentifierIterator> Queued; 8096 8097 public: 8098 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First, 8099 std::unique_ptr<IdentifierIterator> Second) 8100 : Current(std::move(First)), Queued(std::move(Second)) {} 8101 8102 StringRef Next() override { 8103 if (!Current) 8104 return StringRef(); 8105 8106 StringRef result = Current->Next(); 8107 if (!result.empty()) 8108 return result; 8109 8110 // Try the queued iterator, which may itself be empty. 8111 Current.reset(); 8112 std::swap(Current, Queued); 8113 return Next(); 8114 } 8115 }; 8116 8117 } // namespace 8118 8119 IdentifierIterator *ASTReader::getIdentifiers() { 8120 if (!loadGlobalIndex()) { 8121 std::unique_ptr<IdentifierIterator> ReaderIter( 8122 new ASTIdentifierIterator(*this, /*SkipModules=*/true)); 8123 std::unique_ptr<IdentifierIterator> ModulesIter( 8124 GlobalIndex->createIdentifierIterator()); 8125 return new ChainedIdentifierIterator(std::move(ReaderIter), 8126 std::move(ModulesIter)); 8127 } 8128 8129 return new ASTIdentifierIterator(*this); 8130 } 8131 8132 namespace clang { 8133 namespace serialization { 8134 8135 class ReadMethodPoolVisitor { 8136 ASTReader &Reader; 8137 Selector Sel; 8138 unsigned PriorGeneration; 8139 unsigned InstanceBits = 0; 8140 unsigned FactoryBits = 0; 8141 bool InstanceHasMoreThanOneDecl = false; 8142 bool FactoryHasMoreThanOneDecl = false; 8143 SmallVector<ObjCMethodDecl *, 4> InstanceMethods; 8144 SmallVector<ObjCMethodDecl *, 4> FactoryMethods; 8145 8146 public: 8147 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel, 8148 unsigned PriorGeneration) 8149 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {} 8150 8151 bool operator()(ModuleFile &M) { 8152 if (!M.SelectorLookupTable) 8153 return false; 8154 8155 // If we've already searched this module file, skip it now. 8156 if (M.Generation <= PriorGeneration) 8157 return true; 8158 8159 ++Reader.NumMethodPoolTableLookups; 8160 ASTSelectorLookupTable *PoolTable 8161 = (ASTSelectorLookupTable*)M.SelectorLookupTable; 8162 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel); 8163 if (Pos == PoolTable->end()) 8164 return false; 8165 8166 ++Reader.NumMethodPoolTableHits; 8167 ++Reader.NumSelectorsRead; 8168 // FIXME: Not quite happy with the statistics here. We probably should 8169 // disable this tracking when called via LoadSelector. 8170 // Also, should entries without methods count as misses? 8171 ++Reader.NumMethodPoolEntriesRead; 8172 ASTSelectorLookupTrait::data_type Data = *Pos; 8173 if (Reader.DeserializationListener) 8174 Reader.DeserializationListener->SelectorRead(Data.ID, Sel); 8175 8176 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end()); 8177 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end()); 8178 InstanceBits = Data.InstanceBits; 8179 FactoryBits = Data.FactoryBits; 8180 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl; 8181 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl; 8182 return true; 8183 } 8184 8185 /// Retrieve the instance methods found by this visitor. 8186 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const { 8187 return InstanceMethods; 8188 } 8189 8190 /// Retrieve the instance methods found by this visitor. 8191 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const { 8192 return FactoryMethods; 8193 } 8194 8195 unsigned getInstanceBits() const { return InstanceBits; } 8196 unsigned getFactoryBits() const { return FactoryBits; } 8197 8198 bool instanceHasMoreThanOneDecl() const { 8199 return InstanceHasMoreThanOneDecl; 8200 } 8201 8202 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; } 8203 }; 8204 8205 } // namespace serialization 8206 } // namespace clang 8207 8208 /// Add the given set of methods to the method list. 8209 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods, 8210 ObjCMethodList &List) { 8211 for (unsigned I = 0, N = Methods.size(); I != N; ++I) { 8212 S.addMethodToGlobalList(&List, Methods[I]); 8213 } 8214 } 8215 8216 void ASTReader::ReadMethodPool(Selector Sel) { 8217 // Get the selector generation and update it to the current generation. 8218 unsigned &Generation = SelectorGeneration[Sel]; 8219 unsigned PriorGeneration = Generation; 8220 Generation = getGeneration(); 8221 SelectorOutOfDate[Sel] = false; 8222 8223 // Search for methods defined with this selector. 8224 ++NumMethodPoolLookups; 8225 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration); 8226 ModuleMgr.visit(Visitor); 8227 8228 if (Visitor.getInstanceMethods().empty() && 8229 Visitor.getFactoryMethods().empty()) 8230 return; 8231 8232 ++NumMethodPoolHits; 8233 8234 if (!getSema()) 8235 return; 8236 8237 Sema &S = *getSema(); 8238 Sema::GlobalMethodPool::iterator Pos 8239 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first; 8240 8241 Pos->second.first.setBits(Visitor.getInstanceBits()); 8242 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl()); 8243 Pos->second.second.setBits(Visitor.getFactoryBits()); 8244 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl()); 8245 8246 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since 8247 // when building a module we keep every method individually and may need to 8248 // update hasMoreThanOneDecl as we add the methods. 8249 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first); 8250 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second); 8251 } 8252 8253 void ASTReader::updateOutOfDateSelector(Selector Sel) { 8254 if (SelectorOutOfDate[Sel]) 8255 ReadMethodPool(Sel); 8256 } 8257 8258 void ASTReader::ReadKnownNamespaces( 8259 SmallVectorImpl<NamespaceDecl *> &Namespaces) { 8260 Namespaces.clear(); 8261 8262 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) { 8263 if (NamespaceDecl *Namespace 8264 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I]))) 8265 Namespaces.push_back(Namespace); 8266 } 8267 } 8268 8269 void ASTReader::ReadUndefinedButUsed( 8270 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) { 8271 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) { 8272 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++])); 8273 SourceLocation Loc = 8274 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]); 8275 Undefined.insert(std::make_pair(D, Loc)); 8276 } 8277 } 8278 8279 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector< 8280 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> & 8281 Exprs) { 8282 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) { 8283 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++])); 8284 uint64_t Count = DelayedDeleteExprs[Idx++]; 8285 for (uint64_t C = 0; C < Count; ++C) { 8286 SourceLocation DeleteLoc = 8287 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]); 8288 const bool IsArrayForm = DelayedDeleteExprs[Idx++]; 8289 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm)); 8290 } 8291 } 8292 } 8293 8294 void ASTReader::ReadTentativeDefinitions( 8295 SmallVectorImpl<VarDecl *> &TentativeDefs) { 8296 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { 8297 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I])); 8298 if (Var) 8299 TentativeDefs.push_back(Var); 8300 } 8301 TentativeDefinitions.clear(); 8302 } 8303 8304 void ASTReader::ReadUnusedFileScopedDecls( 8305 SmallVectorImpl<const DeclaratorDecl *> &Decls) { 8306 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) { 8307 DeclaratorDecl *D 8308 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I])); 8309 if (D) 8310 Decls.push_back(D); 8311 } 8312 UnusedFileScopedDecls.clear(); 8313 } 8314 8315 void ASTReader::ReadDelegatingConstructors( 8316 SmallVectorImpl<CXXConstructorDecl *> &Decls) { 8317 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) { 8318 CXXConstructorDecl *D 8319 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I])); 8320 if (D) 8321 Decls.push_back(D); 8322 } 8323 DelegatingCtorDecls.clear(); 8324 } 8325 8326 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) { 8327 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) { 8328 TypedefNameDecl *D 8329 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])); 8330 if (D) 8331 Decls.push_back(D); 8332 } 8333 ExtVectorDecls.clear(); 8334 } 8335 8336 void ASTReader::ReadUnusedLocalTypedefNameCandidates( 8337 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) { 8338 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N; 8339 ++I) { 8340 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>( 8341 GetDecl(UnusedLocalTypedefNameCandidates[I])); 8342 if (D) 8343 Decls.insert(D); 8344 } 8345 UnusedLocalTypedefNameCandidates.clear(); 8346 } 8347 8348 void ASTReader::ReadDeclsToCheckForDeferredDiags( 8349 llvm::SmallVector<Decl *, 4> &Decls) { 8350 for (unsigned I = 0, N = DeclsToCheckForDeferredDiags.size(); I != N; 8351 ++I) { 8352 auto *D = dyn_cast_or_null<Decl>( 8353 GetDecl(DeclsToCheckForDeferredDiags[I])); 8354 if (D) 8355 Decls.push_back(D); 8356 } 8357 DeclsToCheckForDeferredDiags.clear(); 8358 } 8359 8360 8361 void ASTReader::ReadReferencedSelectors( 8362 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) { 8363 if (ReferencedSelectorsData.empty()) 8364 return; 8365 8366 // If there are @selector references added them to its pool. This is for 8367 // implementation of -Wselector. 8368 unsigned int DataSize = ReferencedSelectorsData.size()-1; 8369 unsigned I = 0; 8370 while (I < DataSize) { 8371 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]); 8372 SourceLocation SelLoc 8373 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]); 8374 Sels.push_back(std::make_pair(Sel, SelLoc)); 8375 } 8376 ReferencedSelectorsData.clear(); 8377 } 8378 8379 void ASTReader::ReadWeakUndeclaredIdentifiers( 8380 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) { 8381 if (WeakUndeclaredIdentifiers.empty()) 8382 return; 8383 8384 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) { 8385 IdentifierInfo *WeakId 8386 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8387 IdentifierInfo *AliasId 8388 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8389 SourceLocation Loc 8390 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]); 8391 bool Used = WeakUndeclaredIdentifiers[I++]; 8392 WeakInfo WI(AliasId, Loc); 8393 WI.setUsed(Used); 8394 WeakIDs.push_back(std::make_pair(WeakId, WI)); 8395 } 8396 WeakUndeclaredIdentifiers.clear(); 8397 } 8398 8399 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) { 8400 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) { 8401 ExternalVTableUse VT; 8402 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); 8403 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]); 8404 VT.DefinitionRequired = VTableUses[Idx++]; 8405 VTables.push_back(VT); 8406 } 8407 8408 VTableUses.clear(); 8409 } 8410 8411 void ASTReader::ReadPendingInstantiations( 8412 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) { 8413 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) { 8414 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++])); 8415 SourceLocation Loc 8416 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]); 8417 8418 Pending.push_back(std::make_pair(D, Loc)); 8419 } 8420 PendingInstantiations.clear(); 8421 } 8422 8423 void ASTReader::ReadLateParsedTemplates( 8424 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>> 8425 &LPTMap) { 8426 for (auto &LPT : LateParsedTemplates) { 8427 ModuleFile *FMod = LPT.first; 8428 RecordDataImpl &LateParsed = LPT.second; 8429 for (unsigned Idx = 0, N = LateParsed.size(); Idx < N; 8430 /* In loop */) { 8431 FunctionDecl *FD = 8432 cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++])); 8433 8434 auto LT = std::make_unique<LateParsedTemplate>(); 8435 LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]); 8436 8437 ModuleFile *F = getOwningModuleFile(LT->D); 8438 assert(F && "No module"); 8439 8440 unsigned TokN = LateParsed[Idx++]; 8441 LT->Toks.reserve(TokN); 8442 for (unsigned T = 0; T < TokN; ++T) 8443 LT->Toks.push_back(ReadToken(*F, LateParsed, Idx)); 8444 8445 LPTMap.insert(std::make_pair(FD, std::move(LT))); 8446 } 8447 } 8448 } 8449 8450 void ASTReader::LoadSelector(Selector Sel) { 8451 // It would be complicated to avoid reading the methods anyway. So don't. 8452 ReadMethodPool(Sel); 8453 } 8454 8455 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) { 8456 assert(ID && "Non-zero identifier ID required"); 8457 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); 8458 IdentifiersLoaded[ID - 1] = II; 8459 if (DeserializationListener) 8460 DeserializationListener->IdentifierRead(ID, II); 8461 } 8462 8463 /// Set the globally-visible declarations associated with the given 8464 /// identifier. 8465 /// 8466 /// If the AST reader is currently in a state where the given declaration IDs 8467 /// cannot safely be resolved, they are queued until it is safe to resolve 8468 /// them. 8469 /// 8470 /// \param II an IdentifierInfo that refers to one or more globally-visible 8471 /// declarations. 8472 /// 8473 /// \param DeclIDs the set of declaration IDs with the name @p II that are 8474 /// visible at global scope. 8475 /// 8476 /// \param Decls if non-null, this vector will be populated with the set of 8477 /// deserialized declarations. These declarations will not be pushed into 8478 /// scope. 8479 void 8480 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, 8481 const SmallVectorImpl<uint32_t> &DeclIDs, 8482 SmallVectorImpl<Decl *> *Decls) { 8483 if (NumCurrentElementsDeserializing && !Decls) { 8484 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end()); 8485 return; 8486 } 8487 8488 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { 8489 if (!SemaObj) { 8490 // Queue this declaration so that it will be added to the 8491 // translation unit scope and identifier's declaration chain 8492 // once a Sema object is known. 8493 PreloadedDeclIDs.push_back(DeclIDs[I]); 8494 continue; 8495 } 8496 8497 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); 8498 8499 // If we're simply supposed to record the declarations, do so now. 8500 if (Decls) { 8501 Decls->push_back(D); 8502 continue; 8503 } 8504 8505 // Introduce this declaration into the translation-unit scope 8506 // and add it to the declaration chain for this identifier, so 8507 // that (unqualified) name lookup will find it. 8508 pushExternalDeclIntoScope(D, II); 8509 } 8510 } 8511 8512 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) { 8513 if (ID == 0) 8514 return nullptr; 8515 8516 if (IdentifiersLoaded.empty()) { 8517 Error("no identifier table in AST file"); 8518 return nullptr; 8519 } 8520 8521 ID -= 1; 8522 if (!IdentifiersLoaded[ID]) { 8523 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1); 8524 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map"); 8525 ModuleFile *M = I->second; 8526 unsigned Index = ID - M->BaseIdentifierID; 8527 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index]; 8528 8529 // All of the strings in the AST file are preceded by a 16-bit length. 8530 // Extract that 16-bit length to avoid having to execute strlen(). 8531 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as 8532 // unsigned integers. This is important to avoid integer overflow when 8533 // we cast them to 'unsigned'. 8534 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2; 8535 unsigned StrLen = (((unsigned) StrLenPtr[0]) 8536 | (((unsigned) StrLenPtr[1]) << 8)) - 1; 8537 auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen)); 8538 IdentifiersLoaded[ID] = &II; 8539 markIdentifierFromAST(*this, II); 8540 if (DeserializationListener) 8541 DeserializationListener->IdentifierRead(ID + 1, &II); 8542 } 8543 8544 return IdentifiersLoaded[ID]; 8545 } 8546 8547 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) { 8548 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID)); 8549 } 8550 8551 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) { 8552 if (LocalID < NUM_PREDEF_IDENT_IDS) 8553 return LocalID; 8554 8555 if (!M.ModuleOffsetMap.empty()) 8556 ReadModuleOffsetMap(M); 8557 8558 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8559 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS); 8560 assert(I != M.IdentifierRemap.end() 8561 && "Invalid index into identifier index remap"); 8562 8563 return LocalID + I->second; 8564 } 8565 8566 MacroInfo *ASTReader::getMacro(MacroID ID) { 8567 if (ID == 0) 8568 return nullptr; 8569 8570 if (MacrosLoaded.empty()) { 8571 Error("no macro table in AST file"); 8572 return nullptr; 8573 } 8574 8575 ID -= NUM_PREDEF_MACRO_IDS; 8576 if (!MacrosLoaded[ID]) { 8577 GlobalMacroMapType::iterator I 8578 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS); 8579 assert(I != GlobalMacroMap.end() && "Corrupted global macro map"); 8580 ModuleFile *M = I->second; 8581 unsigned Index = ID - M->BaseMacroID; 8582 MacrosLoaded[ID] = 8583 ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]); 8584 8585 if (DeserializationListener) 8586 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS, 8587 MacrosLoaded[ID]); 8588 } 8589 8590 return MacrosLoaded[ID]; 8591 } 8592 8593 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) { 8594 if (LocalID < NUM_PREDEF_MACRO_IDS) 8595 return LocalID; 8596 8597 if (!M.ModuleOffsetMap.empty()) 8598 ReadModuleOffsetMap(M); 8599 8600 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8601 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS); 8602 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap"); 8603 8604 return LocalID + I->second; 8605 } 8606 8607 serialization::SubmoduleID 8608 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) { 8609 if (LocalID < NUM_PREDEF_SUBMODULE_IDS) 8610 return LocalID; 8611 8612 if (!M.ModuleOffsetMap.empty()) 8613 ReadModuleOffsetMap(M); 8614 8615 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8616 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS); 8617 assert(I != M.SubmoduleRemap.end() 8618 && "Invalid index into submodule index remap"); 8619 8620 return LocalID + I->second; 8621 } 8622 8623 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) { 8624 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) { 8625 assert(GlobalID == 0 && "Unhandled global submodule ID"); 8626 return nullptr; 8627 } 8628 8629 if (GlobalID > SubmodulesLoaded.size()) { 8630 Error("submodule ID out of range in AST file"); 8631 return nullptr; 8632 } 8633 8634 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS]; 8635 } 8636 8637 Module *ASTReader::getModule(unsigned ID) { 8638 return getSubmodule(ID); 8639 } 8640 8641 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) { 8642 if (ID & 1) { 8643 // It's a module, look it up by submodule ID. 8644 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1)); 8645 return I == GlobalSubmoduleMap.end() ? nullptr : I->second; 8646 } else { 8647 // It's a prefix (preamble, PCH, ...). Look it up by index. 8648 unsigned IndexFromEnd = ID >> 1; 8649 assert(IndexFromEnd && "got reference to unknown module file"); 8650 return getModuleManager().pch_modules().end()[-IndexFromEnd]; 8651 } 8652 } 8653 8654 unsigned ASTReader::getModuleFileID(ModuleFile *F) { 8655 if (!F) 8656 return 1; 8657 8658 // For a file representing a module, use the submodule ID of the top-level 8659 // module as the file ID. For any other kind of file, the number of such 8660 // files loaded beforehand will be the same on reload. 8661 // FIXME: Is this true even if we have an explicit module file and a PCH? 8662 if (F->isModule()) 8663 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1; 8664 8665 auto PCHModules = getModuleManager().pch_modules(); 8666 auto I = llvm::find(PCHModules, F); 8667 assert(I != PCHModules.end() && "emitting reference to unknown file"); 8668 return (I - PCHModules.end()) << 1; 8669 } 8670 8671 llvm::Optional<ASTSourceDescriptor> 8672 ASTReader::getSourceDescriptor(unsigned ID) { 8673 if (Module *M = getSubmodule(ID)) 8674 return ASTSourceDescriptor(*M); 8675 8676 // If there is only a single PCH, return it instead. 8677 // Chained PCH are not supported. 8678 const auto &PCHChain = ModuleMgr.pch_modules(); 8679 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) { 8680 ModuleFile &MF = ModuleMgr.getPrimaryModule(); 8681 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName); 8682 StringRef FileName = llvm::sys::path::filename(MF.FileName); 8683 return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName, 8684 MF.Signature); 8685 } 8686 return None; 8687 } 8688 8689 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) { 8690 auto I = DefinitionSource.find(FD); 8691 if (I == DefinitionSource.end()) 8692 return EK_ReplyHazy; 8693 return I->second ? EK_Never : EK_Always; 8694 } 8695 8696 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) { 8697 return DecodeSelector(getGlobalSelectorID(M, LocalID)); 8698 } 8699 8700 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) { 8701 if (ID == 0) 8702 return Selector(); 8703 8704 if (ID > SelectorsLoaded.size()) { 8705 Error("selector ID out of range in AST file"); 8706 return Selector(); 8707 } 8708 8709 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) { 8710 // Load this selector from the selector table. 8711 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID); 8712 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map"); 8713 ModuleFile &M = *I->second; 8714 ASTSelectorLookupTrait Trait(*this, M); 8715 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS; 8716 SelectorsLoaded[ID - 1] = 8717 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0); 8718 if (DeserializationListener) 8719 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 8720 } 8721 8722 return SelectorsLoaded[ID - 1]; 8723 } 8724 8725 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) { 8726 return DecodeSelector(ID); 8727 } 8728 8729 uint32_t ASTReader::GetNumExternalSelectors() { 8730 // ID 0 (the null selector) is considered an external selector. 8731 return getTotalNumSelectors() + 1; 8732 } 8733 8734 serialization::SelectorID 8735 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const { 8736 if (LocalID < NUM_PREDEF_SELECTOR_IDS) 8737 return LocalID; 8738 8739 if (!M.ModuleOffsetMap.empty()) 8740 ReadModuleOffsetMap(M); 8741 8742 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8743 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS); 8744 assert(I != M.SelectorRemap.end() 8745 && "Invalid index into selector index remap"); 8746 8747 return LocalID + I->second; 8748 } 8749 8750 DeclarationNameLoc 8751 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) { 8752 switch (Name.getNameKind()) { 8753 case DeclarationName::CXXConstructorName: 8754 case DeclarationName::CXXDestructorName: 8755 case DeclarationName::CXXConversionFunctionName: 8756 return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo()); 8757 8758 case DeclarationName::CXXOperatorName: 8759 return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange()); 8760 8761 case DeclarationName::CXXLiteralOperatorName: 8762 return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc( 8763 readSourceLocation()); 8764 8765 case DeclarationName::Identifier: 8766 case DeclarationName::ObjCZeroArgSelector: 8767 case DeclarationName::ObjCOneArgSelector: 8768 case DeclarationName::ObjCMultiArgSelector: 8769 case DeclarationName::CXXUsingDirective: 8770 case DeclarationName::CXXDeductionGuideName: 8771 break; 8772 } 8773 return DeclarationNameLoc(); 8774 } 8775 8776 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() { 8777 DeclarationNameInfo NameInfo; 8778 NameInfo.setName(readDeclarationName()); 8779 NameInfo.setLoc(readSourceLocation()); 8780 NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName())); 8781 return NameInfo; 8782 } 8783 8784 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) { 8785 Info.QualifierLoc = readNestedNameSpecifierLoc(); 8786 unsigned NumTPLists = readInt(); 8787 Info.NumTemplParamLists = NumTPLists; 8788 if (NumTPLists) { 8789 Info.TemplParamLists = 8790 new (getContext()) TemplateParameterList *[NumTPLists]; 8791 for (unsigned i = 0; i != NumTPLists; ++i) 8792 Info.TemplParamLists[i] = readTemplateParameterList(); 8793 } 8794 } 8795 8796 TemplateParameterList * 8797 ASTRecordReader::readTemplateParameterList() { 8798 SourceLocation TemplateLoc = readSourceLocation(); 8799 SourceLocation LAngleLoc = readSourceLocation(); 8800 SourceLocation RAngleLoc = readSourceLocation(); 8801 8802 unsigned NumParams = readInt(); 8803 SmallVector<NamedDecl *, 16> Params; 8804 Params.reserve(NumParams); 8805 while (NumParams--) 8806 Params.push_back(readDeclAs<NamedDecl>()); 8807 8808 bool HasRequiresClause = readBool(); 8809 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr; 8810 8811 TemplateParameterList *TemplateParams = TemplateParameterList::Create( 8812 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause); 8813 return TemplateParams; 8814 } 8815 8816 void ASTRecordReader::readTemplateArgumentList( 8817 SmallVectorImpl<TemplateArgument> &TemplArgs, 8818 bool Canonicalize) { 8819 unsigned NumTemplateArgs = readInt(); 8820 TemplArgs.reserve(NumTemplateArgs); 8821 while (NumTemplateArgs--) 8822 TemplArgs.push_back(readTemplateArgument(Canonicalize)); 8823 } 8824 8825 /// Read a UnresolvedSet structure. 8826 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) { 8827 unsigned NumDecls = readInt(); 8828 Set.reserve(getContext(), NumDecls); 8829 while (NumDecls--) { 8830 DeclID ID = readDeclID(); 8831 AccessSpecifier AS = (AccessSpecifier) readInt(); 8832 Set.addLazyDecl(getContext(), ID, AS); 8833 } 8834 } 8835 8836 CXXBaseSpecifier 8837 ASTRecordReader::readCXXBaseSpecifier() { 8838 bool isVirtual = readBool(); 8839 bool isBaseOfClass = readBool(); 8840 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt()); 8841 bool inheritConstructors = readBool(); 8842 TypeSourceInfo *TInfo = readTypeSourceInfo(); 8843 SourceRange Range = readSourceRange(); 8844 SourceLocation EllipsisLoc = readSourceLocation(); 8845 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo, 8846 EllipsisLoc); 8847 Result.setInheritConstructors(inheritConstructors); 8848 return Result; 8849 } 8850 8851 CXXCtorInitializer ** 8852 ASTRecordReader::readCXXCtorInitializers() { 8853 ASTContext &Context = getContext(); 8854 unsigned NumInitializers = readInt(); 8855 assert(NumInitializers && "wrote ctor initializers but have no inits"); 8856 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers]; 8857 for (unsigned i = 0; i != NumInitializers; ++i) { 8858 TypeSourceInfo *TInfo = nullptr; 8859 bool IsBaseVirtual = false; 8860 FieldDecl *Member = nullptr; 8861 IndirectFieldDecl *IndirectMember = nullptr; 8862 8863 CtorInitializerType Type = (CtorInitializerType) readInt(); 8864 switch (Type) { 8865 case CTOR_INITIALIZER_BASE: 8866 TInfo = readTypeSourceInfo(); 8867 IsBaseVirtual = readBool(); 8868 break; 8869 8870 case CTOR_INITIALIZER_DELEGATING: 8871 TInfo = readTypeSourceInfo(); 8872 break; 8873 8874 case CTOR_INITIALIZER_MEMBER: 8875 Member = readDeclAs<FieldDecl>(); 8876 break; 8877 8878 case CTOR_INITIALIZER_INDIRECT_MEMBER: 8879 IndirectMember = readDeclAs<IndirectFieldDecl>(); 8880 break; 8881 } 8882 8883 SourceLocation MemberOrEllipsisLoc = readSourceLocation(); 8884 Expr *Init = readExpr(); 8885 SourceLocation LParenLoc = readSourceLocation(); 8886 SourceLocation RParenLoc = readSourceLocation(); 8887 8888 CXXCtorInitializer *BOMInit; 8889 if (Type == CTOR_INITIALIZER_BASE) 8890 BOMInit = new (Context) 8891 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init, 8892 RParenLoc, MemberOrEllipsisLoc); 8893 else if (Type == CTOR_INITIALIZER_DELEGATING) 8894 BOMInit = new (Context) 8895 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc); 8896 else if (Member) 8897 BOMInit = new (Context) 8898 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc, 8899 Init, RParenLoc); 8900 else 8901 BOMInit = new (Context) 8902 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc, 8903 LParenLoc, Init, RParenLoc); 8904 8905 if (/*IsWritten*/readBool()) { 8906 unsigned SourceOrder = readInt(); 8907 BOMInit->setSourceOrder(SourceOrder); 8908 } 8909 8910 CtorInitializers[i] = BOMInit; 8911 } 8912 8913 return CtorInitializers; 8914 } 8915 8916 NestedNameSpecifierLoc 8917 ASTRecordReader::readNestedNameSpecifierLoc() { 8918 ASTContext &Context = getContext(); 8919 unsigned N = readInt(); 8920 NestedNameSpecifierLocBuilder Builder; 8921 for (unsigned I = 0; I != N; ++I) { 8922 auto Kind = readNestedNameSpecifierKind(); 8923 switch (Kind) { 8924 case NestedNameSpecifier::Identifier: { 8925 IdentifierInfo *II = readIdentifier(); 8926 SourceRange Range = readSourceRange(); 8927 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd()); 8928 break; 8929 } 8930 8931 case NestedNameSpecifier::Namespace: { 8932 NamespaceDecl *NS = readDeclAs<NamespaceDecl>(); 8933 SourceRange Range = readSourceRange(); 8934 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd()); 8935 break; 8936 } 8937 8938 case NestedNameSpecifier::NamespaceAlias: { 8939 NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>(); 8940 SourceRange Range = readSourceRange(); 8941 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd()); 8942 break; 8943 } 8944 8945 case NestedNameSpecifier::TypeSpec: 8946 case NestedNameSpecifier::TypeSpecWithTemplate: { 8947 bool Template = readBool(); 8948 TypeSourceInfo *T = readTypeSourceInfo(); 8949 if (!T) 8950 return NestedNameSpecifierLoc(); 8951 SourceLocation ColonColonLoc = readSourceLocation(); 8952 8953 // FIXME: 'template' keyword location not saved anywhere, so we fake it. 8954 Builder.Extend(Context, 8955 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(), 8956 T->getTypeLoc(), ColonColonLoc); 8957 break; 8958 } 8959 8960 case NestedNameSpecifier::Global: { 8961 SourceLocation ColonColonLoc = readSourceLocation(); 8962 Builder.MakeGlobal(Context, ColonColonLoc); 8963 break; 8964 } 8965 8966 case NestedNameSpecifier::Super: { 8967 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>(); 8968 SourceRange Range = readSourceRange(); 8969 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd()); 8970 break; 8971 } 8972 } 8973 } 8974 8975 return Builder.getWithLocInContext(Context); 8976 } 8977 8978 SourceRange 8979 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record, 8980 unsigned &Idx) { 8981 SourceLocation beg = ReadSourceLocation(F, Record, Idx); 8982 SourceLocation end = ReadSourceLocation(F, Record, Idx); 8983 return SourceRange(beg, end); 8984 } 8985 8986 /// Read a floating-point value 8987 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) { 8988 return llvm::APFloat(Sem, readAPInt()); 8989 } 8990 8991 // Read a string 8992 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) { 8993 unsigned Len = Record[Idx++]; 8994 std::string Result(Record.data() + Idx, Record.data() + Idx + Len); 8995 Idx += Len; 8996 return Result; 8997 } 8998 8999 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record, 9000 unsigned &Idx) { 9001 std::string Filename = ReadString(Record, Idx); 9002 ResolveImportedPath(F, Filename); 9003 return Filename; 9004 } 9005 9006 std::string ASTReader::ReadPath(StringRef BaseDirectory, 9007 const RecordData &Record, unsigned &Idx) { 9008 std::string Filename = ReadString(Record, Idx); 9009 if (!BaseDirectory.empty()) 9010 ResolveImportedPath(Filename, BaseDirectory); 9011 return Filename; 9012 } 9013 9014 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record, 9015 unsigned &Idx) { 9016 unsigned Major = Record[Idx++]; 9017 unsigned Minor = Record[Idx++]; 9018 unsigned Subminor = Record[Idx++]; 9019 if (Minor == 0) 9020 return VersionTuple(Major); 9021 if (Subminor == 0) 9022 return VersionTuple(Major, Minor - 1); 9023 return VersionTuple(Major, Minor - 1, Subminor - 1); 9024 } 9025 9026 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F, 9027 const RecordData &Record, 9028 unsigned &Idx) { 9029 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx); 9030 return CXXTemporary::Create(getContext(), Decl); 9031 } 9032 9033 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const { 9034 return Diag(CurrentImportLoc, DiagID); 9035 } 9036 9037 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const { 9038 return Diags.Report(Loc, DiagID); 9039 } 9040 9041 /// Retrieve the identifier table associated with the 9042 /// preprocessor. 9043 IdentifierTable &ASTReader::getIdentifierTable() { 9044 return PP.getIdentifierTable(); 9045 } 9046 9047 /// Record that the given ID maps to the given switch-case 9048 /// statement. 9049 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { 9050 assert((*CurrSwitchCaseStmts)[ID] == nullptr && 9051 "Already have a SwitchCase with this ID"); 9052 (*CurrSwitchCaseStmts)[ID] = SC; 9053 } 9054 9055 /// Retrieve the switch-case statement with the given ID. 9056 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) { 9057 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID"); 9058 return (*CurrSwitchCaseStmts)[ID]; 9059 } 9060 9061 void ASTReader::ClearSwitchCaseIDs() { 9062 CurrSwitchCaseStmts->clear(); 9063 } 9064 9065 void ASTReader::ReadComments() { 9066 ASTContext &Context = getContext(); 9067 std::vector<RawComment *> Comments; 9068 for (SmallVectorImpl<std::pair<BitstreamCursor, 9069 serialization::ModuleFile *>>::iterator 9070 I = CommentsCursors.begin(), 9071 E = CommentsCursors.end(); 9072 I != E; ++I) { 9073 Comments.clear(); 9074 BitstreamCursor &Cursor = I->first; 9075 serialization::ModuleFile &F = *I->second; 9076 SavedStreamPosition SavedPosition(Cursor); 9077 9078 RecordData Record; 9079 while (true) { 9080 Expected<llvm::BitstreamEntry> MaybeEntry = 9081 Cursor.advanceSkippingSubblocks( 9082 BitstreamCursor::AF_DontPopBlockAtEnd); 9083 if (!MaybeEntry) { 9084 Error(MaybeEntry.takeError()); 9085 return; 9086 } 9087 llvm::BitstreamEntry Entry = MaybeEntry.get(); 9088 9089 switch (Entry.Kind) { 9090 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 9091 case llvm::BitstreamEntry::Error: 9092 Error("malformed block record in AST file"); 9093 return; 9094 case llvm::BitstreamEntry::EndBlock: 9095 goto NextCursor; 9096 case llvm::BitstreamEntry::Record: 9097 // The interesting case. 9098 break; 9099 } 9100 9101 // Read a record. 9102 Record.clear(); 9103 Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record); 9104 if (!MaybeComment) { 9105 Error(MaybeComment.takeError()); 9106 return; 9107 } 9108 switch ((CommentRecordTypes)MaybeComment.get()) { 9109 case COMMENTS_RAW_COMMENT: { 9110 unsigned Idx = 0; 9111 SourceRange SR = ReadSourceRange(F, Record, Idx); 9112 RawComment::CommentKind Kind = 9113 (RawComment::CommentKind) Record[Idx++]; 9114 bool IsTrailingComment = Record[Idx++]; 9115 bool IsAlmostTrailingComment = Record[Idx++]; 9116 Comments.push_back(new (Context) RawComment( 9117 SR, Kind, IsTrailingComment, IsAlmostTrailingComment)); 9118 break; 9119 } 9120 } 9121 } 9122 NextCursor: 9123 llvm::DenseMap<FileID, std::map<unsigned, RawComment *>> 9124 FileToOffsetToComment; 9125 for (RawComment *C : Comments) { 9126 SourceLocation CommentLoc = C->getBeginLoc(); 9127 if (CommentLoc.isValid()) { 9128 std::pair<FileID, unsigned> Loc = 9129 SourceMgr.getDecomposedLoc(CommentLoc); 9130 if (Loc.first.isValid()) 9131 Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C); 9132 } 9133 } 9134 } 9135 } 9136 9137 void ASTReader::visitInputFiles(serialization::ModuleFile &MF, 9138 bool IncludeSystem, bool Complain, 9139 llvm::function_ref<void(const serialization::InputFile &IF, 9140 bool isSystem)> Visitor) { 9141 unsigned NumUserInputs = MF.NumUserInputFiles; 9142 unsigned NumInputs = MF.InputFilesLoaded.size(); 9143 assert(NumUserInputs <= NumInputs); 9144 unsigned N = IncludeSystem ? NumInputs : NumUserInputs; 9145 for (unsigned I = 0; I < N; ++I) { 9146 bool IsSystem = I >= NumUserInputs; 9147 InputFile IF = getInputFile(MF, I+1, Complain); 9148 Visitor(IF, IsSystem); 9149 } 9150 } 9151 9152 void ASTReader::visitTopLevelModuleMaps( 9153 serialization::ModuleFile &MF, 9154 llvm::function_ref<void(const FileEntry *FE)> Visitor) { 9155 unsigned NumInputs = MF.InputFilesLoaded.size(); 9156 for (unsigned I = 0; I < NumInputs; ++I) { 9157 InputFileInfo IFI = readInputFileInfo(MF, I + 1); 9158 if (IFI.TopLevelModuleMap) 9159 // FIXME: This unnecessarily re-reads the InputFileInfo. 9160 if (auto FE = getInputFile(MF, I + 1).getFile()) 9161 Visitor(FE); 9162 } 9163 } 9164 9165 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) { 9166 // If we know the owning module, use it. 9167 if (Module *M = D->getImportedOwningModule()) 9168 return M->getFullModuleName(); 9169 9170 // Otherwise, use the name of the top-level module the decl is within. 9171 if (ModuleFile *M = getOwningModuleFile(D)) 9172 return M->ModuleName; 9173 9174 // Not from a module. 9175 return {}; 9176 } 9177 9178 void ASTReader::finishPendingActions() { 9179 while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() || 9180 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() || 9181 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() || 9182 !PendingUpdateRecords.empty()) { 9183 // If any identifiers with corresponding top-level declarations have 9184 // been loaded, load those declarations now. 9185 using TopLevelDeclsMap = 9186 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>; 9187 TopLevelDeclsMap TopLevelDecls; 9188 9189 while (!PendingIdentifierInfos.empty()) { 9190 IdentifierInfo *II = PendingIdentifierInfos.back().first; 9191 SmallVector<uint32_t, 4> DeclIDs = 9192 std::move(PendingIdentifierInfos.back().second); 9193 PendingIdentifierInfos.pop_back(); 9194 9195 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]); 9196 } 9197 9198 // Load each function type that we deferred loading because it was a 9199 // deduced type that might refer to a local type declared within itself. 9200 for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) { 9201 auto *FD = PendingFunctionTypes[I].first; 9202 FD->setType(GetType(PendingFunctionTypes[I].second)); 9203 9204 // If we gave a function a deduced return type, remember that we need to 9205 // propagate that along the redeclaration chain. 9206 auto *DT = FD->getReturnType()->getContainedDeducedType(); 9207 if (DT && DT->isDeduced()) 9208 PendingDeducedTypeUpdates.insert( 9209 {FD->getCanonicalDecl(), FD->getReturnType()}); 9210 } 9211 PendingFunctionTypes.clear(); 9212 9213 // For each decl chain that we wanted to complete while deserializing, mark 9214 // it as "still needs to be completed". 9215 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) { 9216 markIncompleteDeclChain(PendingIncompleteDeclChains[I]); 9217 } 9218 PendingIncompleteDeclChains.clear(); 9219 9220 // Load pending declaration chains. 9221 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) 9222 loadPendingDeclChain(PendingDeclChains[I].first, 9223 PendingDeclChains[I].second); 9224 PendingDeclChains.clear(); 9225 9226 // Make the most recent of the top-level declarations visible. 9227 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(), 9228 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) { 9229 IdentifierInfo *II = TLD->first; 9230 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) { 9231 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II); 9232 } 9233 } 9234 9235 // Load any pending macro definitions. 9236 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) { 9237 IdentifierInfo *II = PendingMacroIDs.begin()[I].first; 9238 SmallVector<PendingMacroInfo, 2> GlobalIDs; 9239 GlobalIDs.swap(PendingMacroIDs.begin()[I].second); 9240 // Initialize the macro history from chained-PCHs ahead of module imports. 9241 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9242 ++IDIdx) { 9243 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9244 if (!Info.M->isModule()) 9245 resolvePendingMacro(II, Info); 9246 } 9247 // Handle module imports. 9248 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9249 ++IDIdx) { 9250 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9251 if (Info.M->isModule()) 9252 resolvePendingMacro(II, Info); 9253 } 9254 } 9255 PendingMacroIDs.clear(); 9256 9257 // Wire up the DeclContexts for Decls that we delayed setting until 9258 // recursive loading is completed. 9259 while (!PendingDeclContextInfos.empty()) { 9260 PendingDeclContextInfo Info = PendingDeclContextInfos.front(); 9261 PendingDeclContextInfos.pop_front(); 9262 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC)); 9263 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC)); 9264 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext()); 9265 } 9266 9267 // Perform any pending declaration updates. 9268 while (!PendingUpdateRecords.empty()) { 9269 auto Update = PendingUpdateRecords.pop_back_val(); 9270 ReadingKindTracker ReadingKind(Read_Decl, *this); 9271 loadDeclUpdateRecords(Update); 9272 } 9273 } 9274 9275 // At this point, all update records for loaded decls are in place, so any 9276 // fake class definitions should have become real. 9277 assert(PendingFakeDefinitionData.empty() && 9278 "faked up a class definition but never saw the real one"); 9279 9280 // If we deserialized any C++ or Objective-C class definitions, any 9281 // Objective-C protocol definitions, or any redeclarable templates, make sure 9282 // that all redeclarations point to the definitions. Note that this can only 9283 // happen now, after the redeclaration chains have been fully wired. 9284 for (Decl *D : PendingDefinitions) { 9285 if (TagDecl *TD = dyn_cast<TagDecl>(D)) { 9286 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) { 9287 // Make sure that the TagType points at the definition. 9288 const_cast<TagType*>(TagT)->decl = TD; 9289 } 9290 9291 if (auto RD = dyn_cast<CXXRecordDecl>(D)) { 9292 for (auto *R = getMostRecentExistingDecl(RD); R; 9293 R = R->getPreviousDecl()) { 9294 assert((R == D) == 9295 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() && 9296 "declaration thinks it's the definition but it isn't"); 9297 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData; 9298 } 9299 } 9300 9301 continue; 9302 } 9303 9304 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) { 9305 // Make sure that the ObjCInterfaceType points at the definition. 9306 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl)) 9307 ->Decl = ID; 9308 9309 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl()) 9310 cast<ObjCInterfaceDecl>(R)->Data = ID->Data; 9311 9312 continue; 9313 } 9314 9315 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) { 9316 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl()) 9317 cast<ObjCProtocolDecl>(R)->Data = PD->Data; 9318 9319 continue; 9320 } 9321 9322 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl(); 9323 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl()) 9324 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common; 9325 } 9326 PendingDefinitions.clear(); 9327 9328 // Load the bodies of any functions or methods we've encountered. We do 9329 // this now (delayed) so that we can be sure that the declaration chains 9330 // have been fully wired up (hasBody relies on this). 9331 // FIXME: We shouldn't require complete redeclaration chains here. 9332 for (PendingBodiesMap::iterator PB = PendingBodies.begin(), 9333 PBEnd = PendingBodies.end(); 9334 PB != PBEnd; ++PB) { 9335 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) { 9336 // For a function defined inline within a class template, force the 9337 // canonical definition to be the one inside the canonical definition of 9338 // the template. This ensures that we instantiate from a correct view 9339 // of the template. 9340 // 9341 // Sadly we can't do this more generally: we can't be sure that all 9342 // copies of an arbitrary class definition will have the same members 9343 // defined (eg, some member functions may not be instantiated, and some 9344 // special members may or may not have been implicitly defined). 9345 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent())) 9346 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition()) 9347 continue; 9348 9349 // FIXME: Check for =delete/=default? 9350 // FIXME: Complain about ODR violations here? 9351 const FunctionDecl *Defn = nullptr; 9352 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) { 9353 FD->setLazyBody(PB->second); 9354 } else { 9355 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn); 9356 mergeDefinitionVisibility(NonConstDefn, FD); 9357 9358 if (!FD->isLateTemplateParsed() && 9359 !NonConstDefn->isLateTemplateParsed() && 9360 FD->getODRHash() != NonConstDefn->getODRHash()) { 9361 if (!isa<CXXMethodDecl>(FD)) { 9362 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9363 } else if (FD->getLexicalParent()->isFileContext() && 9364 NonConstDefn->getLexicalParent()->isFileContext()) { 9365 // Only diagnose out-of-line method definitions. If they are 9366 // in class definitions, then an error will be generated when 9367 // processing the class bodies. 9368 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9369 } 9370 } 9371 } 9372 continue; 9373 } 9374 9375 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first); 9376 if (!getContext().getLangOpts().Modules || !MD->hasBody()) 9377 MD->setLazyBody(PB->second); 9378 } 9379 PendingBodies.clear(); 9380 9381 // Do some cleanup. 9382 for (auto *ND : PendingMergedDefinitionsToDeduplicate) 9383 getContext().deduplicateMergedDefinitonsFor(ND); 9384 PendingMergedDefinitionsToDeduplicate.clear(); 9385 } 9386 9387 void ASTReader::diagnoseOdrViolations() { 9388 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() && 9389 PendingFunctionOdrMergeFailures.empty() && 9390 PendingEnumOdrMergeFailures.empty()) 9391 return; 9392 9393 // Trigger the import of the full definition of each class that had any 9394 // odr-merging problems, so we can produce better diagnostics for them. 9395 // These updates may in turn find and diagnose some ODR failures, so take 9396 // ownership of the set first. 9397 auto OdrMergeFailures = std::move(PendingOdrMergeFailures); 9398 PendingOdrMergeFailures.clear(); 9399 for (auto &Merge : OdrMergeFailures) { 9400 Merge.first->buildLookup(); 9401 Merge.first->decls_begin(); 9402 Merge.first->bases_begin(); 9403 Merge.first->vbases_begin(); 9404 for (auto &RecordPair : Merge.second) { 9405 auto *RD = RecordPair.first; 9406 RD->decls_begin(); 9407 RD->bases_begin(); 9408 RD->vbases_begin(); 9409 } 9410 } 9411 9412 // Trigger the import of functions. 9413 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures); 9414 PendingFunctionOdrMergeFailures.clear(); 9415 for (auto &Merge : FunctionOdrMergeFailures) { 9416 Merge.first->buildLookup(); 9417 Merge.first->decls_begin(); 9418 Merge.first->getBody(); 9419 for (auto &FD : Merge.second) { 9420 FD->buildLookup(); 9421 FD->decls_begin(); 9422 FD->getBody(); 9423 } 9424 } 9425 9426 // Trigger the import of enums. 9427 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures); 9428 PendingEnumOdrMergeFailures.clear(); 9429 for (auto &Merge : EnumOdrMergeFailures) { 9430 Merge.first->decls_begin(); 9431 for (auto &Enum : Merge.second) { 9432 Enum->decls_begin(); 9433 } 9434 } 9435 9436 // For each declaration from a merged context, check that the canonical 9437 // definition of that context also contains a declaration of the same 9438 // entity. 9439 // 9440 // Caution: this loop does things that might invalidate iterators into 9441 // PendingOdrMergeChecks. Don't turn this into a range-based for loop! 9442 while (!PendingOdrMergeChecks.empty()) { 9443 NamedDecl *D = PendingOdrMergeChecks.pop_back_val(); 9444 9445 // FIXME: Skip over implicit declarations for now. This matters for things 9446 // like implicitly-declared special member functions. This isn't entirely 9447 // correct; we can end up with multiple unmerged declarations of the same 9448 // implicit entity. 9449 if (D->isImplicit()) 9450 continue; 9451 9452 DeclContext *CanonDef = D->getDeclContext(); 9453 9454 bool Found = false; 9455 const Decl *DCanon = D->getCanonicalDecl(); 9456 9457 for (auto RI : D->redecls()) { 9458 if (RI->getLexicalDeclContext() == CanonDef) { 9459 Found = true; 9460 break; 9461 } 9462 } 9463 if (Found) 9464 continue; 9465 9466 // Quick check failed, time to do the slow thing. Note, we can't just 9467 // look up the name of D in CanonDef here, because the member that is 9468 // in CanonDef might not be found by name lookup (it might have been 9469 // replaced by a more recent declaration in the lookup table), and we 9470 // can't necessarily find it in the redeclaration chain because it might 9471 // be merely mergeable, not redeclarable. 9472 llvm::SmallVector<const NamedDecl*, 4> Candidates; 9473 for (auto *CanonMember : CanonDef->decls()) { 9474 if (CanonMember->getCanonicalDecl() == DCanon) { 9475 // This can happen if the declaration is merely mergeable and not 9476 // actually redeclarable (we looked for redeclarations earlier). 9477 // 9478 // FIXME: We should be able to detect this more efficiently, without 9479 // pulling in all of the members of CanonDef. 9480 Found = true; 9481 break; 9482 } 9483 if (auto *ND = dyn_cast<NamedDecl>(CanonMember)) 9484 if (ND->getDeclName() == D->getDeclName()) 9485 Candidates.push_back(ND); 9486 } 9487 9488 if (!Found) { 9489 // The AST doesn't like TagDecls becoming invalid after they've been 9490 // completed. We only really need to mark FieldDecls as invalid here. 9491 if (!isa<TagDecl>(D)) 9492 D->setInvalidDecl(); 9493 9494 // Ensure we don't accidentally recursively enter deserialization while 9495 // we're producing our diagnostic. 9496 Deserializing RecursionGuard(this); 9497 9498 std::string CanonDefModule = 9499 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef)); 9500 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl) 9501 << D << getOwningModuleNameForDiagnostic(D) 9502 << CanonDef << CanonDefModule.empty() << CanonDefModule; 9503 9504 if (Candidates.empty()) 9505 Diag(cast<Decl>(CanonDef)->getLocation(), 9506 diag::note_module_odr_violation_no_possible_decls) << D; 9507 else { 9508 for (unsigned I = 0, N = Candidates.size(); I != N; ++I) 9509 Diag(Candidates[I]->getLocation(), 9510 diag::note_module_odr_violation_possible_decl) 9511 << Candidates[I]; 9512 } 9513 9514 DiagnosedOdrMergeFailures.insert(CanonDef); 9515 } 9516 } 9517 9518 if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() && 9519 EnumOdrMergeFailures.empty()) 9520 return; 9521 9522 // Ensure we don't accidentally recursively enter deserialization while 9523 // we're producing our diagnostics. 9524 Deserializing RecursionGuard(this); 9525 9526 // Common code for hashing helpers. 9527 ODRHash Hash; 9528 auto ComputeQualTypeODRHash = [&Hash](QualType Ty) { 9529 Hash.clear(); 9530 Hash.AddQualType(Ty); 9531 return Hash.CalculateHash(); 9532 }; 9533 9534 auto ComputeODRHash = [&Hash](const Stmt *S) { 9535 assert(S); 9536 Hash.clear(); 9537 Hash.AddStmt(S); 9538 return Hash.CalculateHash(); 9539 }; 9540 9541 auto ComputeSubDeclODRHash = [&Hash](const Decl *D) { 9542 assert(D); 9543 Hash.clear(); 9544 Hash.AddSubDecl(D); 9545 return Hash.CalculateHash(); 9546 }; 9547 9548 auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) { 9549 Hash.clear(); 9550 Hash.AddTemplateArgument(TA); 9551 return Hash.CalculateHash(); 9552 }; 9553 9554 auto ComputeTemplateParameterListODRHash = 9555 [&Hash](const TemplateParameterList *TPL) { 9556 assert(TPL); 9557 Hash.clear(); 9558 Hash.AddTemplateParameterList(TPL); 9559 return Hash.CalculateHash(); 9560 }; 9561 9562 // Used with err_module_odr_violation_mismatch_decl and 9563 // note_module_odr_violation_mismatch_decl 9564 // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed 9565 enum ODRMismatchDecl { 9566 EndOfClass, 9567 PublicSpecifer, 9568 PrivateSpecifer, 9569 ProtectedSpecifer, 9570 StaticAssert, 9571 Field, 9572 CXXMethod, 9573 TypeAlias, 9574 TypeDef, 9575 Var, 9576 Friend, 9577 FunctionTemplate, 9578 Other 9579 }; 9580 9581 // Used with err_module_odr_violation_mismatch_decl_diff and 9582 // note_module_odr_violation_mismatch_decl_diff 9583 enum ODRMismatchDeclDifference { 9584 StaticAssertCondition, 9585 StaticAssertMessage, 9586 StaticAssertOnlyMessage, 9587 FieldName, 9588 FieldTypeName, 9589 FieldSingleBitField, 9590 FieldDifferentWidthBitField, 9591 FieldSingleMutable, 9592 FieldSingleInitializer, 9593 FieldDifferentInitializers, 9594 MethodName, 9595 MethodDeleted, 9596 MethodDefaulted, 9597 MethodVirtual, 9598 MethodStatic, 9599 MethodVolatile, 9600 MethodConst, 9601 MethodInline, 9602 MethodNumberParameters, 9603 MethodParameterType, 9604 MethodParameterName, 9605 MethodParameterSingleDefaultArgument, 9606 MethodParameterDifferentDefaultArgument, 9607 MethodNoTemplateArguments, 9608 MethodDifferentNumberTemplateArguments, 9609 MethodDifferentTemplateArgument, 9610 MethodSingleBody, 9611 MethodDifferentBody, 9612 TypedefName, 9613 TypedefType, 9614 VarName, 9615 VarType, 9616 VarSingleInitializer, 9617 VarDifferentInitializer, 9618 VarConstexpr, 9619 FriendTypeFunction, 9620 FriendType, 9621 FriendFunction, 9622 FunctionTemplateDifferentNumberParameters, 9623 FunctionTemplateParameterDifferentKind, 9624 FunctionTemplateParameterName, 9625 FunctionTemplateParameterSingleDefaultArgument, 9626 FunctionTemplateParameterDifferentDefaultArgument, 9627 FunctionTemplateParameterDifferentType, 9628 FunctionTemplatePackParameter, 9629 }; 9630 9631 // These lambdas have the common portions of the ODR diagnostics. This 9632 // has the same return as Diag(), so addition parameters can be passed 9633 // in with operator<< 9634 auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule, 9635 SourceLocation Loc, SourceRange Range, 9636 ODRMismatchDeclDifference DiffType) { 9637 return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff) 9638 << FirstRecord << FirstModule.empty() << FirstModule << Range 9639 << DiffType; 9640 }; 9641 auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc, 9642 SourceRange Range, ODRMismatchDeclDifference DiffType) { 9643 return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff) 9644 << SecondModule << Range << DiffType; 9645 }; 9646 9647 auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote, 9648 &ComputeQualTypeODRHash, &ComputeODRHash]( 9649 NamedDecl *FirstRecord, StringRef FirstModule, 9650 StringRef SecondModule, FieldDecl *FirstField, 9651 FieldDecl *SecondField) { 9652 IdentifierInfo *FirstII = FirstField->getIdentifier(); 9653 IdentifierInfo *SecondII = SecondField->getIdentifier(); 9654 if (FirstII->getName() != SecondII->getName()) { 9655 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9656 FirstField->getSourceRange(), FieldName) 9657 << FirstII; 9658 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9659 SecondField->getSourceRange(), FieldName) 9660 << SecondII; 9661 9662 return true; 9663 } 9664 9665 assert(getContext().hasSameType(FirstField->getType(), 9666 SecondField->getType())); 9667 9668 QualType FirstType = FirstField->getType(); 9669 QualType SecondType = SecondField->getType(); 9670 if (ComputeQualTypeODRHash(FirstType) != 9671 ComputeQualTypeODRHash(SecondType)) { 9672 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9673 FirstField->getSourceRange(), FieldTypeName) 9674 << FirstII << FirstType; 9675 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9676 SecondField->getSourceRange(), FieldTypeName) 9677 << SecondII << SecondType; 9678 9679 return true; 9680 } 9681 9682 const bool IsFirstBitField = FirstField->isBitField(); 9683 const bool IsSecondBitField = SecondField->isBitField(); 9684 if (IsFirstBitField != IsSecondBitField) { 9685 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9686 FirstField->getSourceRange(), FieldSingleBitField) 9687 << FirstII << IsFirstBitField; 9688 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9689 SecondField->getSourceRange(), FieldSingleBitField) 9690 << SecondII << IsSecondBitField; 9691 return true; 9692 } 9693 9694 if (IsFirstBitField && IsSecondBitField) { 9695 unsigned FirstBitWidthHash = 9696 ComputeODRHash(FirstField->getBitWidth()); 9697 unsigned SecondBitWidthHash = 9698 ComputeODRHash(SecondField->getBitWidth()); 9699 if (FirstBitWidthHash != SecondBitWidthHash) { 9700 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9701 FirstField->getSourceRange(), 9702 FieldDifferentWidthBitField) 9703 << FirstII << FirstField->getBitWidth()->getSourceRange(); 9704 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9705 SecondField->getSourceRange(), 9706 FieldDifferentWidthBitField) 9707 << SecondII << SecondField->getBitWidth()->getSourceRange(); 9708 return true; 9709 } 9710 } 9711 9712 if (!PP.getLangOpts().CPlusPlus) 9713 return false; 9714 9715 const bool IsFirstMutable = FirstField->isMutable(); 9716 const bool IsSecondMutable = SecondField->isMutable(); 9717 if (IsFirstMutable != IsSecondMutable) { 9718 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9719 FirstField->getSourceRange(), FieldSingleMutable) 9720 << FirstII << IsFirstMutable; 9721 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9722 SecondField->getSourceRange(), FieldSingleMutable) 9723 << SecondII << IsSecondMutable; 9724 return true; 9725 } 9726 9727 const Expr *FirstInitializer = FirstField->getInClassInitializer(); 9728 const Expr *SecondInitializer = SecondField->getInClassInitializer(); 9729 if ((!FirstInitializer && SecondInitializer) || 9730 (FirstInitializer && !SecondInitializer)) { 9731 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9732 FirstField->getSourceRange(), FieldSingleInitializer) 9733 << FirstII << (FirstInitializer != nullptr); 9734 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9735 SecondField->getSourceRange(), FieldSingleInitializer) 9736 << SecondII << (SecondInitializer != nullptr); 9737 return true; 9738 } 9739 9740 if (FirstInitializer && SecondInitializer) { 9741 unsigned FirstInitHash = ComputeODRHash(FirstInitializer); 9742 unsigned SecondInitHash = ComputeODRHash(SecondInitializer); 9743 if (FirstInitHash != SecondInitHash) { 9744 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9745 FirstField->getSourceRange(), 9746 FieldDifferentInitializers) 9747 << FirstII << FirstInitializer->getSourceRange(); 9748 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9749 SecondField->getSourceRange(), 9750 FieldDifferentInitializers) 9751 << SecondII << SecondInitializer->getSourceRange(); 9752 return true; 9753 } 9754 } 9755 9756 return false; 9757 }; 9758 9759 auto ODRDiagTypeDefOrAlias = 9760 [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash]( 9761 NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule, 9762 TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD, 9763 bool IsTypeAlias) { 9764 auto FirstName = FirstTD->getDeclName(); 9765 auto SecondName = SecondTD->getDeclName(); 9766 if (FirstName != SecondName) { 9767 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(), 9768 FirstTD->getSourceRange(), TypedefName) 9769 << IsTypeAlias << FirstName; 9770 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(), 9771 SecondTD->getSourceRange(), TypedefName) 9772 << IsTypeAlias << SecondName; 9773 return true; 9774 } 9775 9776 QualType FirstType = FirstTD->getUnderlyingType(); 9777 QualType SecondType = SecondTD->getUnderlyingType(); 9778 if (ComputeQualTypeODRHash(FirstType) != 9779 ComputeQualTypeODRHash(SecondType)) { 9780 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(), 9781 FirstTD->getSourceRange(), TypedefType) 9782 << IsTypeAlias << FirstName << FirstType; 9783 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(), 9784 SecondTD->getSourceRange(), TypedefType) 9785 << IsTypeAlias << SecondName << SecondType; 9786 return true; 9787 } 9788 9789 return false; 9790 }; 9791 9792 auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote, 9793 &ComputeQualTypeODRHash, &ComputeODRHash, 9794 this](NamedDecl *FirstRecord, StringRef FirstModule, 9795 StringRef SecondModule, VarDecl *FirstVD, 9796 VarDecl *SecondVD) { 9797 auto FirstName = FirstVD->getDeclName(); 9798 auto SecondName = SecondVD->getDeclName(); 9799 if (FirstName != SecondName) { 9800 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9801 FirstVD->getSourceRange(), VarName) 9802 << FirstName; 9803 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9804 SecondVD->getSourceRange(), VarName) 9805 << SecondName; 9806 return true; 9807 } 9808 9809 QualType FirstType = FirstVD->getType(); 9810 QualType SecondType = SecondVD->getType(); 9811 if (ComputeQualTypeODRHash(FirstType) != 9812 ComputeQualTypeODRHash(SecondType)) { 9813 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9814 FirstVD->getSourceRange(), VarType) 9815 << FirstName << FirstType; 9816 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9817 SecondVD->getSourceRange(), VarType) 9818 << SecondName << SecondType; 9819 return true; 9820 } 9821 9822 if (!PP.getLangOpts().CPlusPlus) 9823 return false; 9824 9825 const Expr *FirstInit = FirstVD->getInit(); 9826 const Expr *SecondInit = SecondVD->getInit(); 9827 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 9828 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9829 FirstVD->getSourceRange(), VarSingleInitializer) 9830 << FirstName << (FirstInit == nullptr) 9831 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 9832 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9833 SecondVD->getSourceRange(), VarSingleInitializer) 9834 << SecondName << (SecondInit == nullptr) 9835 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 9836 return true; 9837 } 9838 9839 if (FirstInit && SecondInit && 9840 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 9841 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9842 FirstVD->getSourceRange(), VarDifferentInitializer) 9843 << FirstName << FirstInit->getSourceRange(); 9844 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9845 SecondVD->getSourceRange(), VarDifferentInitializer) 9846 << SecondName << SecondInit->getSourceRange(); 9847 return true; 9848 } 9849 9850 const bool FirstIsConstexpr = FirstVD->isConstexpr(); 9851 const bool SecondIsConstexpr = SecondVD->isConstexpr(); 9852 if (FirstIsConstexpr != SecondIsConstexpr) { 9853 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9854 FirstVD->getSourceRange(), VarConstexpr) 9855 << FirstName << FirstIsConstexpr; 9856 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9857 SecondVD->getSourceRange(), VarConstexpr) 9858 << SecondName << SecondIsConstexpr; 9859 return true; 9860 } 9861 return false; 9862 }; 9863 9864 auto DifferenceSelector = [](Decl *D) { 9865 assert(D && "valid Decl required"); 9866 switch (D->getKind()) { 9867 default: 9868 return Other; 9869 case Decl::AccessSpec: 9870 switch (D->getAccess()) { 9871 case AS_public: 9872 return PublicSpecifer; 9873 case AS_private: 9874 return PrivateSpecifer; 9875 case AS_protected: 9876 return ProtectedSpecifer; 9877 case AS_none: 9878 break; 9879 } 9880 llvm_unreachable("Invalid access specifier"); 9881 case Decl::StaticAssert: 9882 return StaticAssert; 9883 case Decl::Field: 9884 return Field; 9885 case Decl::CXXMethod: 9886 case Decl::CXXConstructor: 9887 case Decl::CXXDestructor: 9888 return CXXMethod; 9889 case Decl::TypeAlias: 9890 return TypeAlias; 9891 case Decl::Typedef: 9892 return TypeDef; 9893 case Decl::Var: 9894 return Var; 9895 case Decl::Friend: 9896 return Friend; 9897 case Decl::FunctionTemplate: 9898 return FunctionTemplate; 9899 } 9900 }; 9901 9902 using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>; 9903 auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes, 9904 RecordDecl *Record, 9905 const DeclContext *DC) { 9906 for (auto *D : Record->decls()) { 9907 if (!ODRHash::isDeclToBeProcessed(D, DC)) 9908 continue; 9909 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 9910 } 9911 }; 9912 9913 struct DiffResult { 9914 Decl *FirstDecl = nullptr, *SecondDecl = nullptr; 9915 ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other; 9916 }; 9917 9918 // If there is a diagnoseable difference, FirstDiffType and 9919 // SecondDiffType will not be Other and FirstDecl and SecondDecl will be 9920 // filled in if not EndOfClass. 9921 auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes, 9922 DeclHashes &SecondHashes) { 9923 DiffResult DR; 9924 auto FirstIt = FirstHashes.begin(); 9925 auto SecondIt = SecondHashes.begin(); 9926 while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) { 9927 if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() && 9928 FirstIt->second == SecondIt->second) { 9929 ++FirstIt; 9930 ++SecondIt; 9931 continue; 9932 } 9933 9934 DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first; 9935 DR.SecondDecl = 9936 SecondIt == SecondHashes.end() ? nullptr : SecondIt->first; 9937 9938 DR.FirstDiffType = 9939 DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass; 9940 DR.SecondDiffType = 9941 DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass; 9942 return DR; 9943 } 9944 return DR; 9945 }; 9946 9947 // Use this to diagnose that an unexpected Decl was encountered 9948 // or no difference was detected. This causes a generic error 9949 // message to be emitted. 9950 auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord, 9951 StringRef FirstModule, 9952 NamedDecl *SecondRecord, 9953 StringRef SecondModule) { 9954 Diag(FirstRecord->getLocation(), 9955 diag::err_module_odr_violation_different_definitions) 9956 << FirstRecord << FirstModule.empty() << FirstModule; 9957 9958 if (DR.FirstDecl) { 9959 Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference) 9960 << FirstRecord << DR.FirstDecl->getSourceRange(); 9961 } 9962 9963 Diag(SecondRecord->getLocation(), 9964 diag::note_module_odr_violation_different_definitions) 9965 << SecondModule; 9966 9967 if (DR.SecondDecl) { 9968 Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference) 9969 << DR.SecondDecl->getSourceRange(); 9970 } 9971 }; 9972 9973 auto DiagnoseODRMismatch = 9974 [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule, 9975 NamedDecl *SecondRecord, StringRef SecondModule) { 9976 SourceLocation FirstLoc; 9977 SourceRange FirstRange; 9978 auto *FirstTag = dyn_cast<TagDecl>(FirstRecord); 9979 if (DR.FirstDiffType == EndOfClass && FirstTag) { 9980 FirstLoc = FirstTag->getBraceRange().getEnd(); 9981 } else { 9982 FirstLoc = DR.FirstDecl->getLocation(); 9983 FirstRange = DR.FirstDecl->getSourceRange(); 9984 } 9985 Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl) 9986 << FirstRecord << FirstModule.empty() << FirstModule << FirstRange 9987 << DR.FirstDiffType; 9988 9989 SourceLocation SecondLoc; 9990 SourceRange SecondRange; 9991 auto *SecondTag = dyn_cast<TagDecl>(SecondRecord); 9992 if (DR.SecondDiffType == EndOfClass && SecondTag) { 9993 SecondLoc = SecondTag->getBraceRange().getEnd(); 9994 } else { 9995 SecondLoc = DR.SecondDecl->getLocation(); 9996 SecondRange = DR.SecondDecl->getSourceRange(); 9997 } 9998 Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl) 9999 << SecondModule << SecondRange << DR.SecondDiffType; 10000 }; 10001 10002 // Issue any pending ODR-failure diagnostics. 10003 for (auto &Merge : OdrMergeFailures) { 10004 // If we've already pointed out a specific problem with this class, don't 10005 // bother issuing a general "something's different" diagnostic. 10006 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 10007 continue; 10008 10009 bool Diagnosed = false; 10010 CXXRecordDecl *FirstRecord = Merge.first; 10011 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord); 10012 for (auto &RecordPair : Merge.second) { 10013 CXXRecordDecl *SecondRecord = RecordPair.first; 10014 // Multiple different declarations got merged together; tell the user 10015 // where they came from. 10016 if (FirstRecord == SecondRecord) 10017 continue; 10018 10019 std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord); 10020 10021 auto *FirstDD = FirstRecord->DefinitionData; 10022 auto *SecondDD = RecordPair.second; 10023 10024 assert(FirstDD && SecondDD && "Definitions without DefinitionData"); 10025 10026 // Diagnostics from DefinitionData are emitted here. 10027 if (FirstDD != SecondDD) { 10028 enum ODRDefinitionDataDifference { 10029 NumBases, 10030 NumVBases, 10031 BaseType, 10032 BaseVirtual, 10033 BaseAccess, 10034 }; 10035 auto ODRDiagBaseError = [FirstRecord, &FirstModule, 10036 this](SourceLocation Loc, SourceRange Range, 10037 ODRDefinitionDataDifference DiffType) { 10038 return Diag(Loc, diag::err_module_odr_violation_definition_data) 10039 << FirstRecord << FirstModule.empty() << FirstModule << Range 10040 << DiffType; 10041 }; 10042 auto ODRDiagBaseNote = [&SecondModule, 10043 this](SourceLocation Loc, SourceRange Range, 10044 ODRDefinitionDataDifference DiffType) { 10045 return Diag(Loc, diag::note_module_odr_violation_definition_data) 10046 << SecondModule << Range << DiffType; 10047 }; 10048 10049 unsigned FirstNumBases = FirstDD->NumBases; 10050 unsigned FirstNumVBases = FirstDD->NumVBases; 10051 unsigned SecondNumBases = SecondDD->NumBases; 10052 unsigned SecondNumVBases = SecondDD->NumVBases; 10053 10054 auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) { 10055 unsigned NumBases = DD->NumBases; 10056 if (NumBases == 0) return SourceRange(); 10057 auto bases = DD->bases(); 10058 return SourceRange(bases[0].getBeginLoc(), 10059 bases[NumBases - 1].getEndLoc()); 10060 }; 10061 10062 if (FirstNumBases != SecondNumBases) { 10063 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10064 NumBases) 10065 << FirstNumBases; 10066 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10067 NumBases) 10068 << SecondNumBases; 10069 Diagnosed = true; 10070 break; 10071 } 10072 10073 if (FirstNumVBases != SecondNumVBases) { 10074 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10075 NumVBases) 10076 << FirstNumVBases; 10077 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10078 NumVBases) 10079 << SecondNumVBases; 10080 Diagnosed = true; 10081 break; 10082 } 10083 10084 auto FirstBases = FirstDD->bases(); 10085 auto SecondBases = SecondDD->bases(); 10086 unsigned i = 0; 10087 for (i = 0; i < FirstNumBases; ++i) { 10088 auto FirstBase = FirstBases[i]; 10089 auto SecondBase = SecondBases[i]; 10090 if (ComputeQualTypeODRHash(FirstBase.getType()) != 10091 ComputeQualTypeODRHash(SecondBase.getType())) { 10092 ODRDiagBaseError(FirstRecord->getLocation(), 10093 FirstBase.getSourceRange(), BaseType) 10094 << (i + 1) << FirstBase.getType(); 10095 ODRDiagBaseNote(SecondRecord->getLocation(), 10096 SecondBase.getSourceRange(), BaseType) 10097 << (i + 1) << SecondBase.getType(); 10098 break; 10099 } 10100 10101 if (FirstBase.isVirtual() != SecondBase.isVirtual()) { 10102 ODRDiagBaseError(FirstRecord->getLocation(), 10103 FirstBase.getSourceRange(), BaseVirtual) 10104 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType(); 10105 ODRDiagBaseNote(SecondRecord->getLocation(), 10106 SecondBase.getSourceRange(), BaseVirtual) 10107 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType(); 10108 break; 10109 } 10110 10111 if (FirstBase.getAccessSpecifierAsWritten() != 10112 SecondBase.getAccessSpecifierAsWritten()) { 10113 ODRDiagBaseError(FirstRecord->getLocation(), 10114 FirstBase.getSourceRange(), BaseAccess) 10115 << (i + 1) << FirstBase.getType() 10116 << (int)FirstBase.getAccessSpecifierAsWritten(); 10117 ODRDiagBaseNote(SecondRecord->getLocation(), 10118 SecondBase.getSourceRange(), BaseAccess) 10119 << (i + 1) << SecondBase.getType() 10120 << (int)SecondBase.getAccessSpecifierAsWritten(); 10121 break; 10122 } 10123 } 10124 10125 if (i != FirstNumBases) { 10126 Diagnosed = true; 10127 break; 10128 } 10129 } 10130 10131 const ClassTemplateDecl *FirstTemplate = 10132 FirstRecord->getDescribedClassTemplate(); 10133 const ClassTemplateDecl *SecondTemplate = 10134 SecondRecord->getDescribedClassTemplate(); 10135 10136 assert(!FirstTemplate == !SecondTemplate && 10137 "Both pointers should be null or non-null"); 10138 10139 enum ODRTemplateDifference { 10140 ParamEmptyName, 10141 ParamName, 10142 ParamSingleDefaultArgument, 10143 ParamDifferentDefaultArgument, 10144 }; 10145 10146 if (FirstTemplate && SecondTemplate) { 10147 DeclHashes FirstTemplateHashes; 10148 DeclHashes SecondTemplateHashes; 10149 10150 auto PopulateTemplateParameterHashs = 10151 [&ComputeSubDeclODRHash](DeclHashes &Hashes, 10152 const ClassTemplateDecl *TD) { 10153 for (auto *D : TD->getTemplateParameters()->asArray()) { 10154 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 10155 } 10156 }; 10157 10158 PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate); 10159 PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate); 10160 10161 assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() && 10162 "Number of template parameters should be equal."); 10163 10164 auto FirstIt = FirstTemplateHashes.begin(); 10165 auto FirstEnd = FirstTemplateHashes.end(); 10166 auto SecondIt = SecondTemplateHashes.begin(); 10167 for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) { 10168 if (FirstIt->second == SecondIt->second) 10169 continue; 10170 10171 auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this]( 10172 SourceLocation Loc, SourceRange Range, 10173 ODRTemplateDifference DiffType) { 10174 return Diag(Loc, diag::err_module_odr_violation_template_parameter) 10175 << FirstRecord << FirstModule.empty() << FirstModule << Range 10176 << DiffType; 10177 }; 10178 auto ODRDiagTemplateNote = [&SecondModule, this]( 10179 SourceLocation Loc, SourceRange Range, 10180 ODRTemplateDifference DiffType) { 10181 return Diag(Loc, diag::note_module_odr_violation_template_parameter) 10182 << SecondModule << Range << DiffType; 10183 }; 10184 10185 const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first); 10186 const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first); 10187 10188 assert(FirstDecl->getKind() == SecondDecl->getKind() && 10189 "Parameter Decl's should be the same kind."); 10190 10191 DeclarationName FirstName = FirstDecl->getDeclName(); 10192 DeclarationName SecondName = SecondDecl->getDeclName(); 10193 10194 if (FirstName != SecondName) { 10195 const bool FirstNameEmpty = 10196 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo(); 10197 const bool SecondNameEmpty = 10198 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo(); 10199 assert((!FirstNameEmpty || !SecondNameEmpty) && 10200 "Both template parameters cannot be unnamed."); 10201 ODRDiagTemplateError(FirstDecl->getLocation(), 10202 FirstDecl->getSourceRange(), 10203 FirstNameEmpty ? ParamEmptyName : ParamName) 10204 << FirstName; 10205 ODRDiagTemplateNote(SecondDecl->getLocation(), 10206 SecondDecl->getSourceRange(), 10207 SecondNameEmpty ? ParamEmptyName : ParamName) 10208 << SecondName; 10209 break; 10210 } 10211 10212 switch (FirstDecl->getKind()) { 10213 default: 10214 llvm_unreachable("Invalid template parameter type."); 10215 case Decl::TemplateTypeParm: { 10216 const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl); 10217 const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl); 10218 const bool HasFirstDefaultArgument = 10219 FirstParam->hasDefaultArgument() && 10220 !FirstParam->defaultArgumentWasInherited(); 10221 const bool HasSecondDefaultArgument = 10222 SecondParam->hasDefaultArgument() && 10223 !SecondParam->defaultArgumentWasInherited(); 10224 10225 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10226 ODRDiagTemplateError(FirstDecl->getLocation(), 10227 FirstDecl->getSourceRange(), 10228 ParamSingleDefaultArgument) 10229 << HasFirstDefaultArgument; 10230 ODRDiagTemplateNote(SecondDecl->getLocation(), 10231 SecondDecl->getSourceRange(), 10232 ParamSingleDefaultArgument) 10233 << HasSecondDefaultArgument; 10234 break; 10235 } 10236 10237 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10238 "Expecting default arguments."); 10239 10240 ODRDiagTemplateError(FirstDecl->getLocation(), 10241 FirstDecl->getSourceRange(), 10242 ParamDifferentDefaultArgument); 10243 ODRDiagTemplateNote(SecondDecl->getLocation(), 10244 SecondDecl->getSourceRange(), 10245 ParamDifferentDefaultArgument); 10246 10247 break; 10248 } 10249 case Decl::NonTypeTemplateParm: { 10250 const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl); 10251 const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl); 10252 const bool HasFirstDefaultArgument = 10253 FirstParam->hasDefaultArgument() && 10254 !FirstParam->defaultArgumentWasInherited(); 10255 const bool HasSecondDefaultArgument = 10256 SecondParam->hasDefaultArgument() && 10257 !SecondParam->defaultArgumentWasInherited(); 10258 10259 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10260 ODRDiagTemplateError(FirstDecl->getLocation(), 10261 FirstDecl->getSourceRange(), 10262 ParamSingleDefaultArgument) 10263 << HasFirstDefaultArgument; 10264 ODRDiagTemplateNote(SecondDecl->getLocation(), 10265 SecondDecl->getSourceRange(), 10266 ParamSingleDefaultArgument) 10267 << HasSecondDefaultArgument; 10268 break; 10269 } 10270 10271 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10272 "Expecting default arguments."); 10273 10274 ODRDiagTemplateError(FirstDecl->getLocation(), 10275 FirstDecl->getSourceRange(), 10276 ParamDifferentDefaultArgument); 10277 ODRDiagTemplateNote(SecondDecl->getLocation(), 10278 SecondDecl->getSourceRange(), 10279 ParamDifferentDefaultArgument); 10280 10281 break; 10282 } 10283 case Decl::TemplateTemplateParm: { 10284 const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl); 10285 const auto *SecondParam = 10286 cast<TemplateTemplateParmDecl>(SecondDecl); 10287 const bool HasFirstDefaultArgument = 10288 FirstParam->hasDefaultArgument() && 10289 !FirstParam->defaultArgumentWasInherited(); 10290 const bool HasSecondDefaultArgument = 10291 SecondParam->hasDefaultArgument() && 10292 !SecondParam->defaultArgumentWasInherited(); 10293 10294 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10295 ODRDiagTemplateError(FirstDecl->getLocation(), 10296 FirstDecl->getSourceRange(), 10297 ParamSingleDefaultArgument) 10298 << HasFirstDefaultArgument; 10299 ODRDiagTemplateNote(SecondDecl->getLocation(), 10300 SecondDecl->getSourceRange(), 10301 ParamSingleDefaultArgument) 10302 << HasSecondDefaultArgument; 10303 break; 10304 } 10305 10306 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10307 "Expecting default arguments."); 10308 10309 ODRDiagTemplateError(FirstDecl->getLocation(), 10310 FirstDecl->getSourceRange(), 10311 ParamDifferentDefaultArgument); 10312 ODRDiagTemplateNote(SecondDecl->getLocation(), 10313 SecondDecl->getSourceRange(), 10314 ParamDifferentDefaultArgument); 10315 10316 break; 10317 } 10318 } 10319 10320 break; 10321 } 10322 10323 if (FirstIt != FirstEnd) { 10324 Diagnosed = true; 10325 break; 10326 } 10327 } 10328 10329 DeclHashes FirstHashes; 10330 DeclHashes SecondHashes; 10331 const DeclContext *DC = FirstRecord; 10332 PopulateHashes(FirstHashes, FirstRecord, DC); 10333 PopulateHashes(SecondHashes, SecondRecord, DC); 10334 10335 auto DR = FindTypeDiffs(FirstHashes, SecondHashes); 10336 ODRMismatchDecl FirstDiffType = DR.FirstDiffType; 10337 ODRMismatchDecl SecondDiffType = DR.SecondDiffType; 10338 Decl *FirstDecl = DR.FirstDecl; 10339 Decl *SecondDecl = DR.SecondDecl; 10340 10341 if (FirstDiffType == Other || SecondDiffType == Other) { 10342 DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord, 10343 SecondModule); 10344 Diagnosed = true; 10345 break; 10346 } 10347 10348 if (FirstDiffType != SecondDiffType) { 10349 DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord, 10350 SecondModule); 10351 Diagnosed = true; 10352 break; 10353 } 10354 10355 assert(FirstDiffType == SecondDiffType); 10356 10357 switch (FirstDiffType) { 10358 case Other: 10359 case EndOfClass: 10360 case PublicSpecifer: 10361 case PrivateSpecifer: 10362 case ProtectedSpecifer: 10363 llvm_unreachable("Invalid diff type"); 10364 10365 case StaticAssert: { 10366 StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl); 10367 StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl); 10368 10369 Expr *FirstExpr = FirstSA->getAssertExpr(); 10370 Expr *SecondExpr = SecondSA->getAssertExpr(); 10371 unsigned FirstODRHash = ComputeODRHash(FirstExpr); 10372 unsigned SecondODRHash = ComputeODRHash(SecondExpr); 10373 if (FirstODRHash != SecondODRHash) { 10374 ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(), 10375 FirstExpr->getSourceRange(), StaticAssertCondition); 10376 ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(), 10377 SecondExpr->getSourceRange(), StaticAssertCondition); 10378 Diagnosed = true; 10379 break; 10380 } 10381 10382 StringLiteral *FirstStr = FirstSA->getMessage(); 10383 StringLiteral *SecondStr = SecondSA->getMessage(); 10384 assert((FirstStr || SecondStr) && "Both messages cannot be empty"); 10385 if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) { 10386 SourceLocation FirstLoc, SecondLoc; 10387 SourceRange FirstRange, SecondRange; 10388 if (FirstStr) { 10389 FirstLoc = FirstStr->getBeginLoc(); 10390 FirstRange = FirstStr->getSourceRange(); 10391 } else { 10392 FirstLoc = FirstSA->getBeginLoc(); 10393 FirstRange = FirstSA->getSourceRange(); 10394 } 10395 if (SecondStr) { 10396 SecondLoc = SecondStr->getBeginLoc(); 10397 SecondRange = SecondStr->getSourceRange(); 10398 } else { 10399 SecondLoc = SecondSA->getBeginLoc(); 10400 SecondRange = SecondSA->getSourceRange(); 10401 } 10402 ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange, 10403 StaticAssertOnlyMessage) 10404 << (FirstStr == nullptr); 10405 ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange, 10406 StaticAssertOnlyMessage) 10407 << (SecondStr == nullptr); 10408 Diagnosed = true; 10409 break; 10410 } 10411 10412 if (FirstStr && SecondStr && 10413 FirstStr->getString() != SecondStr->getString()) { 10414 ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(), 10415 FirstStr->getSourceRange(), StaticAssertMessage); 10416 ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(), 10417 SecondStr->getSourceRange(), StaticAssertMessage); 10418 Diagnosed = true; 10419 break; 10420 } 10421 break; 10422 } 10423 case Field: { 10424 Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule, 10425 cast<FieldDecl>(FirstDecl), 10426 cast<FieldDecl>(SecondDecl)); 10427 break; 10428 } 10429 case CXXMethod: { 10430 enum { 10431 DiagMethod, 10432 DiagConstructor, 10433 DiagDestructor, 10434 } FirstMethodType, 10435 SecondMethodType; 10436 auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) { 10437 if (isa<CXXConstructorDecl>(D)) return DiagConstructor; 10438 if (isa<CXXDestructorDecl>(D)) return DiagDestructor; 10439 return DiagMethod; 10440 }; 10441 const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl); 10442 const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl); 10443 FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod); 10444 SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod); 10445 auto FirstName = FirstMethod->getDeclName(); 10446 auto SecondName = SecondMethod->getDeclName(); 10447 if (FirstMethodType != SecondMethodType || FirstName != SecondName) { 10448 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10449 FirstMethod->getSourceRange(), MethodName) 10450 << FirstMethodType << FirstName; 10451 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10452 SecondMethod->getSourceRange(), MethodName) 10453 << SecondMethodType << SecondName; 10454 10455 Diagnosed = true; 10456 break; 10457 } 10458 10459 const bool FirstDeleted = FirstMethod->isDeletedAsWritten(); 10460 const bool SecondDeleted = SecondMethod->isDeletedAsWritten(); 10461 if (FirstDeleted != SecondDeleted) { 10462 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10463 FirstMethod->getSourceRange(), MethodDeleted) 10464 << FirstMethodType << FirstName << FirstDeleted; 10465 10466 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10467 SecondMethod->getSourceRange(), MethodDeleted) 10468 << SecondMethodType << SecondName << SecondDeleted; 10469 Diagnosed = true; 10470 break; 10471 } 10472 10473 const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted(); 10474 const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted(); 10475 if (FirstDefaulted != SecondDefaulted) { 10476 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10477 FirstMethod->getSourceRange(), MethodDefaulted) 10478 << FirstMethodType << FirstName << FirstDefaulted; 10479 10480 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10481 SecondMethod->getSourceRange(), MethodDefaulted) 10482 << SecondMethodType << SecondName << SecondDefaulted; 10483 Diagnosed = true; 10484 break; 10485 } 10486 10487 const bool FirstVirtual = FirstMethod->isVirtualAsWritten(); 10488 const bool SecondVirtual = SecondMethod->isVirtualAsWritten(); 10489 const bool FirstPure = FirstMethod->isPure(); 10490 const bool SecondPure = SecondMethod->isPure(); 10491 if ((FirstVirtual || SecondVirtual) && 10492 (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) { 10493 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10494 FirstMethod->getSourceRange(), MethodVirtual) 10495 << FirstMethodType << FirstName << FirstPure << FirstVirtual; 10496 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10497 SecondMethod->getSourceRange(), MethodVirtual) 10498 << SecondMethodType << SecondName << SecondPure << SecondVirtual; 10499 Diagnosed = true; 10500 break; 10501 } 10502 10503 // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging, 10504 // FirstDecl is the canonical Decl of SecondDecl, so the storage 10505 // class needs to be checked instead. 10506 const auto FirstStorage = FirstMethod->getStorageClass(); 10507 const auto SecondStorage = SecondMethod->getStorageClass(); 10508 const bool FirstStatic = FirstStorage == SC_Static; 10509 const bool SecondStatic = SecondStorage == SC_Static; 10510 if (FirstStatic != SecondStatic) { 10511 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10512 FirstMethod->getSourceRange(), MethodStatic) 10513 << FirstMethodType << FirstName << FirstStatic; 10514 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10515 SecondMethod->getSourceRange(), MethodStatic) 10516 << SecondMethodType << SecondName << SecondStatic; 10517 Diagnosed = true; 10518 break; 10519 } 10520 10521 const bool FirstVolatile = FirstMethod->isVolatile(); 10522 const bool SecondVolatile = SecondMethod->isVolatile(); 10523 if (FirstVolatile != SecondVolatile) { 10524 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10525 FirstMethod->getSourceRange(), MethodVolatile) 10526 << FirstMethodType << FirstName << FirstVolatile; 10527 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10528 SecondMethod->getSourceRange(), MethodVolatile) 10529 << SecondMethodType << SecondName << SecondVolatile; 10530 Diagnosed = true; 10531 break; 10532 } 10533 10534 const bool FirstConst = FirstMethod->isConst(); 10535 const bool SecondConst = SecondMethod->isConst(); 10536 if (FirstConst != SecondConst) { 10537 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10538 FirstMethod->getSourceRange(), MethodConst) 10539 << FirstMethodType << FirstName << FirstConst; 10540 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10541 SecondMethod->getSourceRange(), MethodConst) 10542 << SecondMethodType << SecondName << SecondConst; 10543 Diagnosed = true; 10544 break; 10545 } 10546 10547 const bool FirstInline = FirstMethod->isInlineSpecified(); 10548 const bool SecondInline = SecondMethod->isInlineSpecified(); 10549 if (FirstInline != SecondInline) { 10550 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10551 FirstMethod->getSourceRange(), MethodInline) 10552 << FirstMethodType << FirstName << FirstInline; 10553 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10554 SecondMethod->getSourceRange(), MethodInline) 10555 << SecondMethodType << SecondName << SecondInline; 10556 Diagnosed = true; 10557 break; 10558 } 10559 10560 const unsigned FirstNumParameters = FirstMethod->param_size(); 10561 const unsigned SecondNumParameters = SecondMethod->param_size(); 10562 if (FirstNumParameters != SecondNumParameters) { 10563 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10564 FirstMethod->getSourceRange(), 10565 MethodNumberParameters) 10566 << FirstMethodType << FirstName << FirstNumParameters; 10567 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10568 SecondMethod->getSourceRange(), 10569 MethodNumberParameters) 10570 << SecondMethodType << SecondName << SecondNumParameters; 10571 Diagnosed = true; 10572 break; 10573 } 10574 10575 // Need this status boolean to know when break out of the switch. 10576 bool ParameterMismatch = false; 10577 for (unsigned I = 0; I < FirstNumParameters; ++I) { 10578 const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I); 10579 const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I); 10580 10581 QualType FirstParamType = FirstParam->getType(); 10582 QualType SecondParamType = SecondParam->getType(); 10583 if (FirstParamType != SecondParamType && 10584 ComputeQualTypeODRHash(FirstParamType) != 10585 ComputeQualTypeODRHash(SecondParamType)) { 10586 if (const DecayedType *ParamDecayedType = 10587 FirstParamType->getAs<DecayedType>()) { 10588 ODRDiagDeclError( 10589 FirstRecord, FirstModule, FirstMethod->getLocation(), 10590 FirstMethod->getSourceRange(), MethodParameterType) 10591 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10592 << true << ParamDecayedType->getOriginalType(); 10593 } else { 10594 ODRDiagDeclError( 10595 FirstRecord, FirstModule, FirstMethod->getLocation(), 10596 FirstMethod->getSourceRange(), MethodParameterType) 10597 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10598 << false; 10599 } 10600 10601 if (const DecayedType *ParamDecayedType = 10602 SecondParamType->getAs<DecayedType>()) { 10603 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10604 SecondMethod->getSourceRange(), 10605 MethodParameterType) 10606 << SecondMethodType << SecondName << (I + 1) 10607 << SecondParamType << true 10608 << ParamDecayedType->getOriginalType(); 10609 } else { 10610 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10611 SecondMethod->getSourceRange(), 10612 MethodParameterType) 10613 << SecondMethodType << SecondName << (I + 1) 10614 << SecondParamType << false; 10615 } 10616 ParameterMismatch = true; 10617 break; 10618 } 10619 10620 DeclarationName FirstParamName = FirstParam->getDeclName(); 10621 DeclarationName SecondParamName = SecondParam->getDeclName(); 10622 if (FirstParamName != SecondParamName) { 10623 ODRDiagDeclError(FirstRecord, FirstModule, 10624 FirstMethod->getLocation(), 10625 FirstMethod->getSourceRange(), MethodParameterName) 10626 << FirstMethodType << FirstName << (I + 1) << FirstParamName; 10627 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10628 SecondMethod->getSourceRange(), MethodParameterName) 10629 << SecondMethodType << SecondName << (I + 1) << SecondParamName; 10630 ParameterMismatch = true; 10631 break; 10632 } 10633 10634 const Expr *FirstInit = FirstParam->getInit(); 10635 const Expr *SecondInit = SecondParam->getInit(); 10636 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 10637 ODRDiagDeclError(FirstRecord, FirstModule, 10638 FirstMethod->getLocation(), 10639 FirstMethod->getSourceRange(), 10640 MethodParameterSingleDefaultArgument) 10641 << FirstMethodType << FirstName << (I + 1) 10642 << (FirstInit == nullptr) 10643 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 10644 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10645 SecondMethod->getSourceRange(), 10646 MethodParameterSingleDefaultArgument) 10647 << SecondMethodType << SecondName << (I + 1) 10648 << (SecondInit == nullptr) 10649 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 10650 ParameterMismatch = true; 10651 break; 10652 } 10653 10654 if (FirstInit && SecondInit && 10655 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 10656 ODRDiagDeclError(FirstRecord, FirstModule, 10657 FirstMethod->getLocation(), 10658 FirstMethod->getSourceRange(), 10659 MethodParameterDifferentDefaultArgument) 10660 << FirstMethodType << FirstName << (I + 1) 10661 << FirstInit->getSourceRange(); 10662 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10663 SecondMethod->getSourceRange(), 10664 MethodParameterDifferentDefaultArgument) 10665 << SecondMethodType << SecondName << (I + 1) 10666 << SecondInit->getSourceRange(); 10667 ParameterMismatch = true; 10668 break; 10669 10670 } 10671 } 10672 10673 if (ParameterMismatch) { 10674 Diagnosed = true; 10675 break; 10676 } 10677 10678 const auto *FirstTemplateArgs = 10679 FirstMethod->getTemplateSpecializationArgs(); 10680 const auto *SecondTemplateArgs = 10681 SecondMethod->getTemplateSpecializationArgs(); 10682 10683 if ((FirstTemplateArgs && !SecondTemplateArgs) || 10684 (!FirstTemplateArgs && SecondTemplateArgs)) { 10685 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10686 FirstMethod->getSourceRange(), 10687 MethodNoTemplateArguments) 10688 << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr); 10689 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10690 SecondMethod->getSourceRange(), 10691 MethodNoTemplateArguments) 10692 << SecondMethodType << SecondName 10693 << (SecondTemplateArgs != nullptr); 10694 10695 Diagnosed = true; 10696 break; 10697 } 10698 10699 if (FirstTemplateArgs && SecondTemplateArgs) { 10700 // Remove pack expansions from argument list. 10701 auto ExpandTemplateArgumentList = 10702 [](const TemplateArgumentList *TAL) { 10703 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList; 10704 for (const TemplateArgument &TA : TAL->asArray()) { 10705 if (TA.getKind() != TemplateArgument::Pack) { 10706 ExpandedList.push_back(&TA); 10707 continue; 10708 } 10709 for (const TemplateArgument &PackTA : TA.getPackAsArray()) { 10710 ExpandedList.push_back(&PackTA); 10711 } 10712 } 10713 return ExpandedList; 10714 }; 10715 llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList = 10716 ExpandTemplateArgumentList(FirstTemplateArgs); 10717 llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList = 10718 ExpandTemplateArgumentList(SecondTemplateArgs); 10719 10720 if (FirstExpandedList.size() != SecondExpandedList.size()) { 10721 ODRDiagDeclError(FirstRecord, FirstModule, 10722 FirstMethod->getLocation(), 10723 FirstMethod->getSourceRange(), 10724 MethodDifferentNumberTemplateArguments) 10725 << FirstMethodType << FirstName 10726 << (unsigned)FirstExpandedList.size(); 10727 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10728 SecondMethod->getSourceRange(), 10729 MethodDifferentNumberTemplateArguments) 10730 << SecondMethodType << SecondName 10731 << (unsigned)SecondExpandedList.size(); 10732 10733 Diagnosed = true; 10734 break; 10735 } 10736 10737 bool TemplateArgumentMismatch = false; 10738 for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) { 10739 const TemplateArgument &FirstTA = *FirstExpandedList[i], 10740 &SecondTA = *SecondExpandedList[i]; 10741 if (ComputeTemplateArgumentODRHash(FirstTA) == 10742 ComputeTemplateArgumentODRHash(SecondTA)) { 10743 continue; 10744 } 10745 10746 ODRDiagDeclError( 10747 FirstRecord, FirstModule, FirstMethod->getLocation(), 10748 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument) 10749 << FirstMethodType << FirstName << FirstTA << i + 1; 10750 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10751 SecondMethod->getSourceRange(), 10752 MethodDifferentTemplateArgument) 10753 << SecondMethodType << SecondName << SecondTA << i + 1; 10754 10755 TemplateArgumentMismatch = true; 10756 break; 10757 } 10758 10759 if (TemplateArgumentMismatch) { 10760 Diagnosed = true; 10761 break; 10762 } 10763 } 10764 10765 // Compute the hash of the method as if it has no body. 10766 auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) { 10767 Hash.clear(); 10768 Hash.AddFunctionDecl(D, true /*SkipBody*/); 10769 return Hash.CalculateHash(); 10770 }; 10771 10772 // Compare the hash generated to the hash stored. A difference means 10773 // that a body was present in the original source. Due to merging, 10774 // the stardard way of detecting a body will not work. 10775 const bool HasFirstBody = 10776 ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash(); 10777 const bool HasSecondBody = 10778 ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash(); 10779 10780 if (HasFirstBody != HasSecondBody) { 10781 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10782 FirstMethod->getSourceRange(), MethodSingleBody) 10783 << FirstMethodType << FirstName << HasFirstBody; 10784 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10785 SecondMethod->getSourceRange(), MethodSingleBody) 10786 << SecondMethodType << SecondName << HasSecondBody; 10787 Diagnosed = true; 10788 break; 10789 } 10790 10791 if (HasFirstBody && HasSecondBody) { 10792 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10793 FirstMethod->getSourceRange(), MethodDifferentBody) 10794 << FirstMethodType << FirstName; 10795 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10796 SecondMethod->getSourceRange(), MethodDifferentBody) 10797 << SecondMethodType << SecondName; 10798 Diagnosed = true; 10799 break; 10800 } 10801 10802 break; 10803 } 10804 case TypeAlias: 10805 case TypeDef: { 10806 Diagnosed = ODRDiagTypeDefOrAlias( 10807 FirstRecord, FirstModule, SecondModule, 10808 cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl), 10809 FirstDiffType == TypeAlias); 10810 break; 10811 } 10812 case Var: { 10813 Diagnosed = 10814 ODRDiagVar(FirstRecord, FirstModule, SecondModule, 10815 cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl)); 10816 break; 10817 } 10818 case Friend: { 10819 FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl); 10820 FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl); 10821 10822 NamedDecl *FirstND = FirstFriend->getFriendDecl(); 10823 NamedDecl *SecondND = SecondFriend->getFriendDecl(); 10824 10825 TypeSourceInfo *FirstTSI = FirstFriend->getFriendType(); 10826 TypeSourceInfo *SecondTSI = SecondFriend->getFriendType(); 10827 10828 if (FirstND && SecondND) { 10829 ODRDiagDeclError(FirstRecord, FirstModule, 10830 FirstFriend->getFriendLoc(), 10831 FirstFriend->getSourceRange(), FriendFunction) 10832 << FirstND; 10833 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10834 SecondFriend->getSourceRange(), FriendFunction) 10835 << SecondND; 10836 10837 Diagnosed = true; 10838 break; 10839 } 10840 10841 if (FirstTSI && SecondTSI) { 10842 QualType FirstFriendType = FirstTSI->getType(); 10843 QualType SecondFriendType = SecondTSI->getType(); 10844 assert(ComputeQualTypeODRHash(FirstFriendType) != 10845 ComputeQualTypeODRHash(SecondFriendType)); 10846 ODRDiagDeclError(FirstRecord, FirstModule, 10847 FirstFriend->getFriendLoc(), 10848 FirstFriend->getSourceRange(), FriendType) 10849 << FirstFriendType; 10850 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10851 SecondFriend->getSourceRange(), FriendType) 10852 << SecondFriendType; 10853 Diagnosed = true; 10854 break; 10855 } 10856 10857 ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(), 10858 FirstFriend->getSourceRange(), FriendTypeFunction) 10859 << (FirstTSI == nullptr); 10860 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10861 SecondFriend->getSourceRange(), FriendTypeFunction) 10862 << (SecondTSI == nullptr); 10863 10864 Diagnosed = true; 10865 break; 10866 } 10867 case FunctionTemplate: { 10868 FunctionTemplateDecl *FirstTemplate = 10869 cast<FunctionTemplateDecl>(FirstDecl); 10870 FunctionTemplateDecl *SecondTemplate = 10871 cast<FunctionTemplateDecl>(SecondDecl); 10872 10873 TemplateParameterList *FirstTPL = 10874 FirstTemplate->getTemplateParameters(); 10875 TemplateParameterList *SecondTPL = 10876 SecondTemplate->getTemplateParameters(); 10877 10878 if (FirstTPL->size() != SecondTPL->size()) { 10879 ODRDiagDeclError(FirstRecord, FirstModule, 10880 FirstTemplate->getLocation(), 10881 FirstTemplate->getSourceRange(), 10882 FunctionTemplateDifferentNumberParameters) 10883 << FirstTemplate << FirstTPL->size(); 10884 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10885 SecondTemplate->getSourceRange(), 10886 FunctionTemplateDifferentNumberParameters) 10887 << SecondTemplate << SecondTPL->size(); 10888 10889 Diagnosed = true; 10890 break; 10891 } 10892 10893 bool ParameterMismatch = false; 10894 for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) { 10895 NamedDecl *FirstParam = FirstTPL->getParam(i); 10896 NamedDecl *SecondParam = SecondTPL->getParam(i); 10897 10898 if (FirstParam->getKind() != SecondParam->getKind()) { 10899 enum { 10900 TemplateTypeParameter, 10901 NonTypeTemplateParameter, 10902 TemplateTemplateParameter, 10903 }; 10904 auto GetParamType = [](NamedDecl *D) { 10905 switch (D->getKind()) { 10906 default: 10907 llvm_unreachable("Unexpected template parameter type"); 10908 case Decl::TemplateTypeParm: 10909 return TemplateTypeParameter; 10910 case Decl::NonTypeTemplateParm: 10911 return NonTypeTemplateParameter; 10912 case Decl::TemplateTemplateParm: 10913 return TemplateTemplateParameter; 10914 } 10915 }; 10916 10917 ODRDiagDeclError(FirstRecord, FirstModule, 10918 FirstTemplate->getLocation(), 10919 FirstTemplate->getSourceRange(), 10920 FunctionTemplateParameterDifferentKind) 10921 << FirstTemplate << (i + 1) << GetParamType(FirstParam); 10922 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10923 SecondTemplate->getSourceRange(), 10924 FunctionTemplateParameterDifferentKind) 10925 << SecondTemplate << (i + 1) << GetParamType(SecondParam); 10926 10927 ParameterMismatch = true; 10928 break; 10929 } 10930 10931 if (FirstParam->getName() != SecondParam->getName()) { 10932 ODRDiagDeclError( 10933 FirstRecord, FirstModule, FirstTemplate->getLocation(), 10934 FirstTemplate->getSourceRange(), FunctionTemplateParameterName) 10935 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier() 10936 << FirstParam; 10937 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10938 SecondTemplate->getSourceRange(), 10939 FunctionTemplateParameterName) 10940 << SecondTemplate << (i + 1) 10941 << (bool)SecondParam->getIdentifier() << SecondParam; 10942 ParameterMismatch = true; 10943 break; 10944 } 10945 10946 if (isa<TemplateTypeParmDecl>(FirstParam) && 10947 isa<TemplateTypeParmDecl>(SecondParam)) { 10948 TemplateTypeParmDecl *FirstTTPD = 10949 cast<TemplateTypeParmDecl>(FirstParam); 10950 TemplateTypeParmDecl *SecondTTPD = 10951 cast<TemplateTypeParmDecl>(SecondParam); 10952 bool HasFirstDefaultArgument = 10953 FirstTTPD->hasDefaultArgument() && 10954 !FirstTTPD->defaultArgumentWasInherited(); 10955 bool HasSecondDefaultArgument = 10956 SecondTTPD->hasDefaultArgument() && 10957 !SecondTTPD->defaultArgumentWasInherited(); 10958 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10959 ODRDiagDeclError(FirstRecord, FirstModule, 10960 FirstTemplate->getLocation(), 10961 FirstTemplate->getSourceRange(), 10962 FunctionTemplateParameterSingleDefaultArgument) 10963 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 10964 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10965 SecondTemplate->getSourceRange(), 10966 FunctionTemplateParameterSingleDefaultArgument) 10967 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 10968 ParameterMismatch = true; 10969 break; 10970 } 10971 10972 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 10973 QualType FirstType = FirstTTPD->getDefaultArgument(); 10974 QualType SecondType = SecondTTPD->getDefaultArgument(); 10975 if (ComputeQualTypeODRHash(FirstType) != 10976 ComputeQualTypeODRHash(SecondType)) { 10977 ODRDiagDeclError( 10978 FirstRecord, FirstModule, FirstTemplate->getLocation(), 10979 FirstTemplate->getSourceRange(), 10980 FunctionTemplateParameterDifferentDefaultArgument) 10981 << FirstTemplate << (i + 1) << FirstType; 10982 ODRDiagDeclNote( 10983 SecondModule, SecondTemplate->getLocation(), 10984 SecondTemplate->getSourceRange(), 10985 FunctionTemplateParameterDifferentDefaultArgument) 10986 << SecondTemplate << (i + 1) << SecondType; 10987 ParameterMismatch = true; 10988 break; 10989 } 10990 } 10991 10992 if (FirstTTPD->isParameterPack() != 10993 SecondTTPD->isParameterPack()) { 10994 ODRDiagDeclError(FirstRecord, FirstModule, 10995 FirstTemplate->getLocation(), 10996 FirstTemplate->getSourceRange(), 10997 FunctionTemplatePackParameter) 10998 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 10999 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11000 SecondTemplate->getSourceRange(), 11001 FunctionTemplatePackParameter) 11002 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 11003 ParameterMismatch = true; 11004 break; 11005 } 11006 } 11007 11008 if (isa<TemplateTemplateParmDecl>(FirstParam) && 11009 isa<TemplateTemplateParmDecl>(SecondParam)) { 11010 TemplateTemplateParmDecl *FirstTTPD = 11011 cast<TemplateTemplateParmDecl>(FirstParam); 11012 TemplateTemplateParmDecl *SecondTTPD = 11013 cast<TemplateTemplateParmDecl>(SecondParam); 11014 11015 TemplateParameterList *FirstTPL = 11016 FirstTTPD->getTemplateParameters(); 11017 TemplateParameterList *SecondTPL = 11018 SecondTTPD->getTemplateParameters(); 11019 11020 if (ComputeTemplateParameterListODRHash(FirstTPL) != 11021 ComputeTemplateParameterListODRHash(SecondTPL)) { 11022 ODRDiagDeclError(FirstRecord, FirstModule, 11023 FirstTemplate->getLocation(), 11024 FirstTemplate->getSourceRange(), 11025 FunctionTemplateParameterDifferentType) 11026 << FirstTemplate << (i + 1); 11027 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11028 SecondTemplate->getSourceRange(), 11029 FunctionTemplateParameterDifferentType) 11030 << SecondTemplate << (i + 1); 11031 ParameterMismatch = true; 11032 break; 11033 } 11034 11035 bool HasFirstDefaultArgument = 11036 FirstTTPD->hasDefaultArgument() && 11037 !FirstTTPD->defaultArgumentWasInherited(); 11038 bool HasSecondDefaultArgument = 11039 SecondTTPD->hasDefaultArgument() && 11040 !SecondTTPD->defaultArgumentWasInherited(); 11041 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11042 ODRDiagDeclError(FirstRecord, FirstModule, 11043 FirstTemplate->getLocation(), 11044 FirstTemplate->getSourceRange(), 11045 FunctionTemplateParameterSingleDefaultArgument) 11046 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11047 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11048 SecondTemplate->getSourceRange(), 11049 FunctionTemplateParameterSingleDefaultArgument) 11050 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11051 ParameterMismatch = true; 11052 break; 11053 } 11054 11055 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11056 TemplateArgument FirstTA = 11057 FirstTTPD->getDefaultArgument().getArgument(); 11058 TemplateArgument SecondTA = 11059 SecondTTPD->getDefaultArgument().getArgument(); 11060 if (ComputeTemplateArgumentODRHash(FirstTA) != 11061 ComputeTemplateArgumentODRHash(SecondTA)) { 11062 ODRDiagDeclError( 11063 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11064 FirstTemplate->getSourceRange(), 11065 FunctionTemplateParameterDifferentDefaultArgument) 11066 << FirstTemplate << (i + 1) << FirstTA; 11067 ODRDiagDeclNote( 11068 SecondModule, SecondTemplate->getLocation(), 11069 SecondTemplate->getSourceRange(), 11070 FunctionTemplateParameterDifferentDefaultArgument) 11071 << SecondTemplate << (i + 1) << SecondTA; 11072 ParameterMismatch = true; 11073 break; 11074 } 11075 } 11076 11077 if (FirstTTPD->isParameterPack() != 11078 SecondTTPD->isParameterPack()) { 11079 ODRDiagDeclError(FirstRecord, FirstModule, 11080 FirstTemplate->getLocation(), 11081 FirstTemplate->getSourceRange(), 11082 FunctionTemplatePackParameter) 11083 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 11084 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11085 SecondTemplate->getSourceRange(), 11086 FunctionTemplatePackParameter) 11087 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 11088 ParameterMismatch = true; 11089 break; 11090 } 11091 } 11092 11093 if (isa<NonTypeTemplateParmDecl>(FirstParam) && 11094 isa<NonTypeTemplateParmDecl>(SecondParam)) { 11095 NonTypeTemplateParmDecl *FirstNTTPD = 11096 cast<NonTypeTemplateParmDecl>(FirstParam); 11097 NonTypeTemplateParmDecl *SecondNTTPD = 11098 cast<NonTypeTemplateParmDecl>(SecondParam); 11099 11100 QualType FirstType = FirstNTTPD->getType(); 11101 QualType SecondType = SecondNTTPD->getType(); 11102 if (ComputeQualTypeODRHash(FirstType) != 11103 ComputeQualTypeODRHash(SecondType)) { 11104 ODRDiagDeclError(FirstRecord, FirstModule, 11105 FirstTemplate->getLocation(), 11106 FirstTemplate->getSourceRange(), 11107 FunctionTemplateParameterDifferentType) 11108 << FirstTemplate << (i + 1); 11109 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11110 SecondTemplate->getSourceRange(), 11111 FunctionTemplateParameterDifferentType) 11112 << SecondTemplate << (i + 1); 11113 ParameterMismatch = true; 11114 break; 11115 } 11116 11117 bool HasFirstDefaultArgument = 11118 FirstNTTPD->hasDefaultArgument() && 11119 !FirstNTTPD->defaultArgumentWasInherited(); 11120 bool HasSecondDefaultArgument = 11121 SecondNTTPD->hasDefaultArgument() && 11122 !SecondNTTPD->defaultArgumentWasInherited(); 11123 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11124 ODRDiagDeclError(FirstRecord, FirstModule, 11125 FirstTemplate->getLocation(), 11126 FirstTemplate->getSourceRange(), 11127 FunctionTemplateParameterSingleDefaultArgument) 11128 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11129 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11130 SecondTemplate->getSourceRange(), 11131 FunctionTemplateParameterSingleDefaultArgument) 11132 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11133 ParameterMismatch = true; 11134 break; 11135 } 11136 11137 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11138 Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument(); 11139 Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument(); 11140 if (ComputeODRHash(FirstDefaultArgument) != 11141 ComputeODRHash(SecondDefaultArgument)) { 11142 ODRDiagDeclError( 11143 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11144 FirstTemplate->getSourceRange(), 11145 FunctionTemplateParameterDifferentDefaultArgument) 11146 << FirstTemplate << (i + 1) << FirstDefaultArgument; 11147 ODRDiagDeclNote( 11148 SecondModule, SecondTemplate->getLocation(), 11149 SecondTemplate->getSourceRange(), 11150 FunctionTemplateParameterDifferentDefaultArgument) 11151 << SecondTemplate << (i + 1) << SecondDefaultArgument; 11152 ParameterMismatch = true; 11153 break; 11154 } 11155 } 11156 11157 if (FirstNTTPD->isParameterPack() != 11158 SecondNTTPD->isParameterPack()) { 11159 ODRDiagDeclError(FirstRecord, FirstModule, 11160 FirstTemplate->getLocation(), 11161 FirstTemplate->getSourceRange(), 11162 FunctionTemplatePackParameter) 11163 << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack(); 11164 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11165 SecondTemplate->getSourceRange(), 11166 FunctionTemplatePackParameter) 11167 << SecondTemplate << (i + 1) 11168 << SecondNTTPD->isParameterPack(); 11169 ParameterMismatch = true; 11170 break; 11171 } 11172 } 11173 } 11174 11175 if (ParameterMismatch) { 11176 Diagnosed = true; 11177 break; 11178 } 11179 11180 break; 11181 } 11182 } 11183 11184 if (Diagnosed) 11185 continue; 11186 11187 Diag(FirstDecl->getLocation(), 11188 diag::err_module_odr_violation_mismatch_decl_unknown) 11189 << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType 11190 << FirstDecl->getSourceRange(); 11191 Diag(SecondDecl->getLocation(), 11192 diag::note_module_odr_violation_mismatch_decl_unknown) 11193 << SecondModule << FirstDiffType << SecondDecl->getSourceRange(); 11194 Diagnosed = true; 11195 } 11196 11197 if (!Diagnosed) { 11198 // All definitions are updates to the same declaration. This happens if a 11199 // module instantiates the declaration of a class template specialization 11200 // and two or more other modules instantiate its definition. 11201 // 11202 // FIXME: Indicate which modules had instantiations of this definition. 11203 // FIXME: How can this even happen? 11204 Diag(Merge.first->getLocation(), 11205 diag::err_module_odr_violation_different_instantiations) 11206 << Merge.first; 11207 } 11208 } 11209 11210 // Issue ODR failures diagnostics for functions. 11211 for (auto &Merge : FunctionOdrMergeFailures) { 11212 enum ODRFunctionDifference { 11213 ReturnType, 11214 ParameterName, 11215 ParameterType, 11216 ParameterSingleDefaultArgument, 11217 ParameterDifferentDefaultArgument, 11218 FunctionBody, 11219 }; 11220 11221 FunctionDecl *FirstFunction = Merge.first; 11222 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction); 11223 11224 bool Diagnosed = false; 11225 for (auto &SecondFunction : Merge.second) { 11226 11227 if (FirstFunction == SecondFunction) 11228 continue; 11229 11230 std::string SecondModule = 11231 getOwningModuleNameForDiagnostic(SecondFunction); 11232 11233 auto ODRDiagError = [FirstFunction, &FirstModule, 11234 this](SourceLocation Loc, SourceRange Range, 11235 ODRFunctionDifference DiffType) { 11236 return Diag(Loc, diag::err_module_odr_violation_function) 11237 << FirstFunction << FirstModule.empty() << FirstModule << Range 11238 << DiffType; 11239 }; 11240 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11241 SourceRange Range, 11242 ODRFunctionDifference DiffType) { 11243 return Diag(Loc, diag::note_module_odr_violation_function) 11244 << SecondModule << Range << DiffType; 11245 }; 11246 11247 if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) != 11248 ComputeQualTypeODRHash(SecondFunction->getReturnType())) { 11249 ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(), 11250 FirstFunction->getReturnTypeSourceRange(), ReturnType) 11251 << FirstFunction->getReturnType(); 11252 ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(), 11253 SecondFunction->getReturnTypeSourceRange(), ReturnType) 11254 << SecondFunction->getReturnType(); 11255 Diagnosed = true; 11256 break; 11257 } 11258 11259 assert(FirstFunction->param_size() == SecondFunction->param_size() && 11260 "Merged functions with different number of parameters"); 11261 11262 auto ParamSize = FirstFunction->param_size(); 11263 bool ParameterMismatch = false; 11264 for (unsigned I = 0; I < ParamSize; ++I) { 11265 auto *FirstParam = FirstFunction->getParamDecl(I); 11266 auto *SecondParam = SecondFunction->getParamDecl(I); 11267 11268 assert(getContext().hasSameType(FirstParam->getType(), 11269 SecondParam->getType()) && 11270 "Merged function has different parameter types."); 11271 11272 if (FirstParam->getDeclName() != SecondParam->getDeclName()) { 11273 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11274 ParameterName) 11275 << I + 1 << FirstParam->getDeclName(); 11276 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11277 ParameterName) 11278 << I + 1 << SecondParam->getDeclName(); 11279 ParameterMismatch = true; 11280 break; 11281 }; 11282 11283 QualType FirstParamType = FirstParam->getType(); 11284 QualType SecondParamType = SecondParam->getType(); 11285 if (FirstParamType != SecondParamType && 11286 ComputeQualTypeODRHash(FirstParamType) != 11287 ComputeQualTypeODRHash(SecondParamType)) { 11288 if (const DecayedType *ParamDecayedType = 11289 FirstParamType->getAs<DecayedType>()) { 11290 ODRDiagError(FirstParam->getLocation(), 11291 FirstParam->getSourceRange(), ParameterType) 11292 << (I + 1) << FirstParamType << true 11293 << ParamDecayedType->getOriginalType(); 11294 } else { 11295 ODRDiagError(FirstParam->getLocation(), 11296 FirstParam->getSourceRange(), ParameterType) 11297 << (I + 1) << FirstParamType << false; 11298 } 11299 11300 if (const DecayedType *ParamDecayedType = 11301 SecondParamType->getAs<DecayedType>()) { 11302 ODRDiagNote(SecondParam->getLocation(), 11303 SecondParam->getSourceRange(), ParameterType) 11304 << (I + 1) << SecondParamType << true 11305 << ParamDecayedType->getOriginalType(); 11306 } else { 11307 ODRDiagNote(SecondParam->getLocation(), 11308 SecondParam->getSourceRange(), ParameterType) 11309 << (I + 1) << SecondParamType << false; 11310 } 11311 ParameterMismatch = true; 11312 break; 11313 } 11314 11315 const Expr *FirstInit = FirstParam->getInit(); 11316 const Expr *SecondInit = SecondParam->getInit(); 11317 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 11318 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11319 ParameterSingleDefaultArgument) 11320 << (I + 1) << (FirstInit == nullptr) 11321 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 11322 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11323 ParameterSingleDefaultArgument) 11324 << (I + 1) << (SecondInit == nullptr) 11325 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 11326 ParameterMismatch = true; 11327 break; 11328 } 11329 11330 if (FirstInit && SecondInit && 11331 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11332 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11333 ParameterDifferentDefaultArgument) 11334 << (I + 1) << FirstInit->getSourceRange(); 11335 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11336 ParameterDifferentDefaultArgument) 11337 << (I + 1) << SecondInit->getSourceRange(); 11338 ParameterMismatch = true; 11339 break; 11340 } 11341 11342 assert(ComputeSubDeclODRHash(FirstParam) == 11343 ComputeSubDeclODRHash(SecondParam) && 11344 "Undiagnosed parameter difference."); 11345 } 11346 11347 if (ParameterMismatch) { 11348 Diagnosed = true; 11349 break; 11350 } 11351 11352 // If no error has been generated before now, assume the problem is in 11353 // the body and generate a message. 11354 ODRDiagError(FirstFunction->getLocation(), 11355 FirstFunction->getSourceRange(), FunctionBody); 11356 ODRDiagNote(SecondFunction->getLocation(), 11357 SecondFunction->getSourceRange(), FunctionBody); 11358 Diagnosed = true; 11359 break; 11360 } 11361 (void)Diagnosed; 11362 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11363 } 11364 11365 // Issue ODR failures diagnostics for enums. 11366 for (auto &Merge : EnumOdrMergeFailures) { 11367 enum ODREnumDifference { 11368 SingleScopedEnum, 11369 EnumTagKeywordMismatch, 11370 SingleSpecifiedType, 11371 DifferentSpecifiedTypes, 11372 DifferentNumberEnumConstants, 11373 EnumConstantName, 11374 EnumConstantSingleInitilizer, 11375 EnumConstantDifferentInitilizer, 11376 }; 11377 11378 // If we've already pointed out a specific problem with this enum, don't 11379 // bother issuing a general "something's different" diagnostic. 11380 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 11381 continue; 11382 11383 EnumDecl *FirstEnum = Merge.first; 11384 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum); 11385 11386 using DeclHashes = 11387 llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>; 11388 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum]( 11389 DeclHashes &Hashes, EnumDecl *Enum) { 11390 for (auto *D : Enum->decls()) { 11391 // Due to decl merging, the first EnumDecl is the parent of 11392 // Decls in both records. 11393 if (!ODRHash::isDeclToBeProcessed(D, FirstEnum)) 11394 continue; 11395 assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind"); 11396 Hashes.emplace_back(cast<EnumConstantDecl>(D), 11397 ComputeSubDeclODRHash(D)); 11398 } 11399 }; 11400 DeclHashes FirstHashes; 11401 PopulateHashes(FirstHashes, FirstEnum); 11402 bool Diagnosed = false; 11403 for (auto &SecondEnum : Merge.second) { 11404 11405 if (FirstEnum == SecondEnum) 11406 continue; 11407 11408 std::string SecondModule = 11409 getOwningModuleNameForDiagnostic(SecondEnum); 11410 11411 auto ODRDiagError = [FirstEnum, &FirstModule, 11412 this](SourceLocation Loc, SourceRange Range, 11413 ODREnumDifference DiffType) { 11414 return Diag(Loc, diag::err_module_odr_violation_enum) 11415 << FirstEnum << FirstModule.empty() << FirstModule << Range 11416 << DiffType; 11417 }; 11418 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11419 SourceRange Range, 11420 ODREnumDifference DiffType) { 11421 return Diag(Loc, diag::note_module_odr_violation_enum) 11422 << SecondModule << Range << DiffType; 11423 }; 11424 11425 if (FirstEnum->isScoped() != SecondEnum->isScoped()) { 11426 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11427 SingleScopedEnum) 11428 << FirstEnum->isScoped(); 11429 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11430 SingleScopedEnum) 11431 << SecondEnum->isScoped(); 11432 Diagnosed = true; 11433 continue; 11434 } 11435 11436 if (FirstEnum->isScoped() && SecondEnum->isScoped()) { 11437 if (FirstEnum->isScopedUsingClassTag() != 11438 SecondEnum->isScopedUsingClassTag()) { 11439 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11440 EnumTagKeywordMismatch) 11441 << FirstEnum->isScopedUsingClassTag(); 11442 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11443 EnumTagKeywordMismatch) 11444 << SecondEnum->isScopedUsingClassTag(); 11445 Diagnosed = true; 11446 continue; 11447 } 11448 } 11449 11450 QualType FirstUnderlyingType = 11451 FirstEnum->getIntegerTypeSourceInfo() 11452 ? FirstEnum->getIntegerTypeSourceInfo()->getType() 11453 : QualType(); 11454 QualType SecondUnderlyingType = 11455 SecondEnum->getIntegerTypeSourceInfo() 11456 ? SecondEnum->getIntegerTypeSourceInfo()->getType() 11457 : QualType(); 11458 if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) { 11459 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11460 SingleSpecifiedType) 11461 << !FirstUnderlyingType.isNull(); 11462 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11463 SingleSpecifiedType) 11464 << !SecondUnderlyingType.isNull(); 11465 Diagnosed = true; 11466 continue; 11467 } 11468 11469 if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) { 11470 if (ComputeQualTypeODRHash(FirstUnderlyingType) != 11471 ComputeQualTypeODRHash(SecondUnderlyingType)) { 11472 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11473 DifferentSpecifiedTypes) 11474 << FirstUnderlyingType; 11475 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11476 DifferentSpecifiedTypes) 11477 << SecondUnderlyingType; 11478 Diagnosed = true; 11479 continue; 11480 } 11481 } 11482 11483 DeclHashes SecondHashes; 11484 PopulateHashes(SecondHashes, SecondEnum); 11485 11486 if (FirstHashes.size() != SecondHashes.size()) { 11487 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11488 DifferentNumberEnumConstants) 11489 << (int)FirstHashes.size(); 11490 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11491 DifferentNumberEnumConstants) 11492 << (int)SecondHashes.size(); 11493 Diagnosed = true; 11494 continue; 11495 } 11496 11497 for (unsigned I = 0; I < FirstHashes.size(); ++I) { 11498 if (FirstHashes[I].second == SecondHashes[I].second) 11499 continue; 11500 const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first; 11501 const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first; 11502 11503 if (FirstEnumConstant->getDeclName() != 11504 SecondEnumConstant->getDeclName()) { 11505 11506 ODRDiagError(FirstEnumConstant->getLocation(), 11507 FirstEnumConstant->getSourceRange(), EnumConstantName) 11508 << I + 1 << FirstEnumConstant; 11509 ODRDiagNote(SecondEnumConstant->getLocation(), 11510 SecondEnumConstant->getSourceRange(), EnumConstantName) 11511 << I + 1 << SecondEnumConstant; 11512 Diagnosed = true; 11513 break; 11514 } 11515 11516 const Expr *FirstInit = FirstEnumConstant->getInitExpr(); 11517 const Expr *SecondInit = SecondEnumConstant->getInitExpr(); 11518 if (!FirstInit && !SecondInit) 11519 continue; 11520 11521 if (!FirstInit || !SecondInit) { 11522 ODRDiagError(FirstEnumConstant->getLocation(), 11523 FirstEnumConstant->getSourceRange(), 11524 EnumConstantSingleInitilizer) 11525 << I + 1 << FirstEnumConstant << (FirstInit != nullptr); 11526 ODRDiagNote(SecondEnumConstant->getLocation(), 11527 SecondEnumConstant->getSourceRange(), 11528 EnumConstantSingleInitilizer) 11529 << I + 1 << SecondEnumConstant << (SecondInit != nullptr); 11530 Diagnosed = true; 11531 break; 11532 } 11533 11534 if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11535 ODRDiagError(FirstEnumConstant->getLocation(), 11536 FirstEnumConstant->getSourceRange(), 11537 EnumConstantDifferentInitilizer) 11538 << I + 1 << FirstEnumConstant; 11539 ODRDiagNote(SecondEnumConstant->getLocation(), 11540 SecondEnumConstant->getSourceRange(), 11541 EnumConstantDifferentInitilizer) 11542 << I + 1 << SecondEnumConstant; 11543 Diagnosed = true; 11544 break; 11545 } 11546 } 11547 } 11548 11549 (void)Diagnosed; 11550 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11551 } 11552 } 11553 11554 void ASTReader::StartedDeserializing() { 11555 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get()) 11556 ReadTimer->startTimer(); 11557 } 11558 11559 void ASTReader::FinishedDeserializing() { 11560 assert(NumCurrentElementsDeserializing && 11561 "FinishedDeserializing not paired with StartedDeserializing"); 11562 if (NumCurrentElementsDeserializing == 1) { 11563 // We decrease NumCurrentElementsDeserializing only after pending actions 11564 // are finished, to avoid recursively re-calling finishPendingActions(). 11565 finishPendingActions(); 11566 } 11567 --NumCurrentElementsDeserializing; 11568 11569 if (NumCurrentElementsDeserializing == 0) { 11570 // Propagate exception specification and deduced type updates along 11571 // redeclaration chains. 11572 // 11573 // We do this now rather than in finishPendingActions because we want to 11574 // be able to walk the complete redeclaration chains of the updated decls. 11575 while (!PendingExceptionSpecUpdates.empty() || 11576 !PendingDeducedTypeUpdates.empty()) { 11577 auto ESUpdates = std::move(PendingExceptionSpecUpdates); 11578 PendingExceptionSpecUpdates.clear(); 11579 for (auto Update : ESUpdates) { 11580 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11581 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>(); 11582 auto ESI = FPT->getExtProtoInfo().ExceptionSpec; 11583 if (auto *Listener = getContext().getASTMutationListener()) 11584 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second)); 11585 for (auto *Redecl : Update.second->redecls()) 11586 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI); 11587 } 11588 11589 auto DTUpdates = std::move(PendingDeducedTypeUpdates); 11590 PendingDeducedTypeUpdates.clear(); 11591 for (auto Update : DTUpdates) { 11592 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11593 // FIXME: If the return type is already deduced, check that it matches. 11594 getContext().adjustDeducedFunctionResultType(Update.first, 11595 Update.second); 11596 } 11597 } 11598 11599 if (ReadTimer) 11600 ReadTimer->stopTimer(); 11601 11602 diagnoseOdrViolations(); 11603 11604 // We are not in recursive loading, so it's safe to pass the "interesting" 11605 // decls to the consumer. 11606 if (Consumer) 11607 PassInterestingDeclsToConsumer(); 11608 } 11609 } 11610 11611 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) { 11612 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) { 11613 // Remove any fake results before adding any real ones. 11614 auto It = PendingFakeLookupResults.find(II); 11615 if (It != PendingFakeLookupResults.end()) { 11616 for (auto *ND : It->second) 11617 SemaObj->IdResolver.RemoveDecl(ND); 11618 // FIXME: this works around module+PCH performance issue. 11619 // Rather than erase the result from the map, which is O(n), just clear 11620 // the vector of NamedDecls. 11621 It->second.clear(); 11622 } 11623 } 11624 11625 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) { 11626 SemaObj->TUScope->AddDecl(D); 11627 } else if (SemaObj->TUScope) { 11628 // Adding the decl to IdResolver may have failed because it was already in 11629 // (even though it was not added in scope). If it is already in, make sure 11630 // it gets in the scope as well. 11631 if (std::find(SemaObj->IdResolver.begin(Name), 11632 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end()) 11633 SemaObj->TUScope->AddDecl(D); 11634 } 11635 } 11636 11637 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache, 11638 ASTContext *Context, 11639 const PCHContainerReader &PCHContainerRdr, 11640 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, 11641 StringRef isysroot, 11642 DisableValidationForModuleKind DisableValidationKind, 11643 bool AllowASTWithCompilerErrors, 11644 bool AllowConfigurationMismatch, bool ValidateSystemInputs, 11645 bool ValidateASTInputFilesContent, bool UseGlobalIndex, 11646 std::unique_ptr<llvm::Timer> ReadTimer) 11647 : Listener(bool(DisableValidationKind &DisableValidationForModuleKind::PCH) 11648 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP)) 11649 : cast<ASTReaderListener>(new PCHValidator(PP, *this))), 11650 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), 11651 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP), 11652 ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache, 11653 PCHContainerRdr, PP.getHeaderSearchInfo()), 11654 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot), 11655 DisableValidationKind(DisableValidationKind), 11656 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors), 11657 AllowConfigurationMismatch(AllowConfigurationMismatch), 11658 ValidateSystemInputs(ValidateSystemInputs), 11659 ValidateASTInputFilesContent(ValidateASTInputFilesContent), 11660 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) { 11661 SourceMgr.setExternalSLocEntrySource(this); 11662 11663 for (const auto &Ext : Extensions) { 11664 auto BlockName = Ext->getExtensionMetadata().BlockName; 11665 auto Known = ModuleFileExtensions.find(BlockName); 11666 if (Known != ModuleFileExtensions.end()) { 11667 Diags.Report(diag::warn_duplicate_module_file_extension) 11668 << BlockName; 11669 continue; 11670 } 11671 11672 ModuleFileExtensions.insert({BlockName, Ext}); 11673 } 11674 } 11675 11676 ASTReader::~ASTReader() { 11677 if (OwnsDeserializationListener) 11678 delete DeserializationListener; 11679 } 11680 11681 IdentifierResolver &ASTReader::getIdResolver() { 11682 return SemaObj ? SemaObj->IdResolver : DummyIdResolver; 11683 } 11684 11685 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor, 11686 unsigned AbbrevID) { 11687 Idx = 0; 11688 Record.clear(); 11689 return Cursor.readRecord(AbbrevID, Record); 11690 } 11691 //===----------------------------------------------------------------------===// 11692 //// OMPClauseReader implementation 11693 ////===----------------------------------------------------------------------===// 11694 11695 // This has to be in namespace clang because it's friended by all 11696 // of the OMP clauses. 11697 namespace clang { 11698 11699 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> { 11700 ASTRecordReader &Record; 11701 ASTContext &Context; 11702 11703 public: 11704 OMPClauseReader(ASTRecordReader &Record) 11705 : Record(Record), Context(Record.getContext()) {} 11706 #define GEN_CLANG_CLAUSE_CLASS 11707 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C); 11708 #include "llvm/Frontend/OpenMP/OMP.inc" 11709 OMPClause *readClause(); 11710 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C); 11711 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C); 11712 }; 11713 11714 } // end namespace clang 11715 11716 OMPClause *ASTRecordReader::readOMPClause() { 11717 return OMPClauseReader(*this).readClause(); 11718 } 11719 11720 OMPClause *OMPClauseReader::readClause() { 11721 OMPClause *C = nullptr; 11722 switch (llvm::omp::Clause(Record.readInt())) { 11723 case llvm::omp::OMPC_if: 11724 C = new (Context) OMPIfClause(); 11725 break; 11726 case llvm::omp::OMPC_final: 11727 C = new (Context) OMPFinalClause(); 11728 break; 11729 case llvm::omp::OMPC_num_threads: 11730 C = new (Context) OMPNumThreadsClause(); 11731 break; 11732 case llvm::omp::OMPC_safelen: 11733 C = new (Context) OMPSafelenClause(); 11734 break; 11735 case llvm::omp::OMPC_simdlen: 11736 C = new (Context) OMPSimdlenClause(); 11737 break; 11738 case llvm::omp::OMPC_sizes: { 11739 unsigned NumSizes = Record.readInt(); 11740 C = OMPSizesClause::CreateEmpty(Context, NumSizes); 11741 break; 11742 } 11743 case llvm::omp::OMPC_allocator: 11744 C = new (Context) OMPAllocatorClause(); 11745 break; 11746 case llvm::omp::OMPC_collapse: 11747 C = new (Context) OMPCollapseClause(); 11748 break; 11749 case llvm::omp::OMPC_default: 11750 C = new (Context) OMPDefaultClause(); 11751 break; 11752 case llvm::omp::OMPC_proc_bind: 11753 C = new (Context) OMPProcBindClause(); 11754 break; 11755 case llvm::omp::OMPC_schedule: 11756 C = new (Context) OMPScheduleClause(); 11757 break; 11758 case llvm::omp::OMPC_ordered: 11759 C = OMPOrderedClause::CreateEmpty(Context, Record.readInt()); 11760 break; 11761 case llvm::omp::OMPC_nowait: 11762 C = new (Context) OMPNowaitClause(); 11763 break; 11764 case llvm::omp::OMPC_untied: 11765 C = new (Context) OMPUntiedClause(); 11766 break; 11767 case llvm::omp::OMPC_mergeable: 11768 C = new (Context) OMPMergeableClause(); 11769 break; 11770 case llvm::omp::OMPC_read: 11771 C = new (Context) OMPReadClause(); 11772 break; 11773 case llvm::omp::OMPC_write: 11774 C = new (Context) OMPWriteClause(); 11775 break; 11776 case llvm::omp::OMPC_update: 11777 C = OMPUpdateClause::CreateEmpty(Context, Record.readInt()); 11778 break; 11779 case llvm::omp::OMPC_capture: 11780 C = new (Context) OMPCaptureClause(); 11781 break; 11782 case llvm::omp::OMPC_seq_cst: 11783 C = new (Context) OMPSeqCstClause(); 11784 break; 11785 case llvm::omp::OMPC_acq_rel: 11786 C = new (Context) OMPAcqRelClause(); 11787 break; 11788 case llvm::omp::OMPC_acquire: 11789 C = new (Context) OMPAcquireClause(); 11790 break; 11791 case llvm::omp::OMPC_release: 11792 C = new (Context) OMPReleaseClause(); 11793 break; 11794 case llvm::omp::OMPC_relaxed: 11795 C = new (Context) OMPRelaxedClause(); 11796 break; 11797 case llvm::omp::OMPC_threads: 11798 C = new (Context) OMPThreadsClause(); 11799 break; 11800 case llvm::omp::OMPC_simd: 11801 C = new (Context) OMPSIMDClause(); 11802 break; 11803 case llvm::omp::OMPC_nogroup: 11804 C = new (Context) OMPNogroupClause(); 11805 break; 11806 case llvm::omp::OMPC_unified_address: 11807 C = new (Context) OMPUnifiedAddressClause(); 11808 break; 11809 case llvm::omp::OMPC_unified_shared_memory: 11810 C = new (Context) OMPUnifiedSharedMemoryClause(); 11811 break; 11812 case llvm::omp::OMPC_reverse_offload: 11813 C = new (Context) OMPReverseOffloadClause(); 11814 break; 11815 case llvm::omp::OMPC_dynamic_allocators: 11816 C = new (Context) OMPDynamicAllocatorsClause(); 11817 break; 11818 case llvm::omp::OMPC_atomic_default_mem_order: 11819 C = new (Context) OMPAtomicDefaultMemOrderClause(); 11820 break; 11821 case llvm::omp::OMPC_private: 11822 C = OMPPrivateClause::CreateEmpty(Context, Record.readInt()); 11823 break; 11824 case llvm::omp::OMPC_firstprivate: 11825 C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt()); 11826 break; 11827 case llvm::omp::OMPC_lastprivate: 11828 C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt()); 11829 break; 11830 case llvm::omp::OMPC_shared: 11831 C = OMPSharedClause::CreateEmpty(Context, Record.readInt()); 11832 break; 11833 case llvm::omp::OMPC_reduction: { 11834 unsigned N = Record.readInt(); 11835 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>(); 11836 C = OMPReductionClause::CreateEmpty(Context, N, Modifier); 11837 break; 11838 } 11839 case llvm::omp::OMPC_task_reduction: 11840 C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt()); 11841 break; 11842 case llvm::omp::OMPC_in_reduction: 11843 C = OMPInReductionClause::CreateEmpty(Context, Record.readInt()); 11844 break; 11845 case llvm::omp::OMPC_linear: 11846 C = OMPLinearClause::CreateEmpty(Context, Record.readInt()); 11847 break; 11848 case llvm::omp::OMPC_aligned: 11849 C = OMPAlignedClause::CreateEmpty(Context, Record.readInt()); 11850 break; 11851 case llvm::omp::OMPC_copyin: 11852 C = OMPCopyinClause::CreateEmpty(Context, Record.readInt()); 11853 break; 11854 case llvm::omp::OMPC_copyprivate: 11855 C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt()); 11856 break; 11857 case llvm::omp::OMPC_flush: 11858 C = OMPFlushClause::CreateEmpty(Context, Record.readInt()); 11859 break; 11860 case llvm::omp::OMPC_depobj: 11861 C = OMPDepobjClause::CreateEmpty(Context); 11862 break; 11863 case llvm::omp::OMPC_depend: { 11864 unsigned NumVars = Record.readInt(); 11865 unsigned NumLoops = Record.readInt(); 11866 C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops); 11867 break; 11868 } 11869 case llvm::omp::OMPC_device: 11870 C = new (Context) OMPDeviceClause(); 11871 break; 11872 case llvm::omp::OMPC_map: { 11873 OMPMappableExprListSizeTy Sizes; 11874 Sizes.NumVars = Record.readInt(); 11875 Sizes.NumUniqueDeclarations = Record.readInt(); 11876 Sizes.NumComponentLists = Record.readInt(); 11877 Sizes.NumComponents = Record.readInt(); 11878 C = OMPMapClause::CreateEmpty(Context, Sizes); 11879 break; 11880 } 11881 case llvm::omp::OMPC_num_teams: 11882 C = new (Context) OMPNumTeamsClause(); 11883 break; 11884 case llvm::omp::OMPC_thread_limit: 11885 C = new (Context) OMPThreadLimitClause(); 11886 break; 11887 case llvm::omp::OMPC_priority: 11888 C = new (Context) OMPPriorityClause(); 11889 break; 11890 case llvm::omp::OMPC_grainsize: 11891 C = new (Context) OMPGrainsizeClause(); 11892 break; 11893 case llvm::omp::OMPC_num_tasks: 11894 C = new (Context) OMPNumTasksClause(); 11895 break; 11896 case llvm::omp::OMPC_hint: 11897 C = new (Context) OMPHintClause(); 11898 break; 11899 case llvm::omp::OMPC_dist_schedule: 11900 C = new (Context) OMPDistScheduleClause(); 11901 break; 11902 case llvm::omp::OMPC_defaultmap: 11903 C = new (Context) OMPDefaultmapClause(); 11904 break; 11905 case llvm::omp::OMPC_to: { 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 = OMPToClause::CreateEmpty(Context, Sizes); 11912 break; 11913 } 11914 case llvm::omp::OMPC_from: { 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 = OMPFromClause::CreateEmpty(Context, Sizes); 11921 break; 11922 } 11923 case llvm::omp::OMPC_use_device_ptr: { 11924 OMPMappableExprListSizeTy Sizes; 11925 Sizes.NumVars = Record.readInt(); 11926 Sizes.NumUniqueDeclarations = Record.readInt(); 11927 Sizes.NumComponentLists = Record.readInt(); 11928 Sizes.NumComponents = Record.readInt(); 11929 C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes); 11930 break; 11931 } 11932 case llvm::omp::OMPC_use_device_addr: { 11933 OMPMappableExprListSizeTy Sizes; 11934 Sizes.NumVars = Record.readInt(); 11935 Sizes.NumUniqueDeclarations = Record.readInt(); 11936 Sizes.NumComponentLists = Record.readInt(); 11937 Sizes.NumComponents = Record.readInt(); 11938 C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes); 11939 break; 11940 } 11941 case llvm::omp::OMPC_is_device_ptr: { 11942 OMPMappableExprListSizeTy Sizes; 11943 Sizes.NumVars = Record.readInt(); 11944 Sizes.NumUniqueDeclarations = Record.readInt(); 11945 Sizes.NumComponentLists = Record.readInt(); 11946 Sizes.NumComponents = Record.readInt(); 11947 C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes); 11948 break; 11949 } 11950 case llvm::omp::OMPC_allocate: 11951 C = OMPAllocateClause::CreateEmpty(Context, Record.readInt()); 11952 break; 11953 case llvm::omp::OMPC_nontemporal: 11954 C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt()); 11955 break; 11956 case llvm::omp::OMPC_inclusive: 11957 C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt()); 11958 break; 11959 case llvm::omp::OMPC_exclusive: 11960 C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt()); 11961 break; 11962 case llvm::omp::OMPC_order: 11963 C = new (Context) OMPOrderClause(); 11964 break; 11965 case llvm::omp::OMPC_destroy: 11966 C = new (Context) OMPDestroyClause(); 11967 break; 11968 case llvm::omp::OMPC_detach: 11969 C = new (Context) OMPDetachClause(); 11970 break; 11971 case llvm::omp::OMPC_uses_allocators: 11972 C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt()); 11973 break; 11974 case llvm::omp::OMPC_affinity: 11975 C = OMPAffinityClause::CreateEmpty(Context, Record.readInt()); 11976 break; 11977 #define OMP_CLAUSE_NO_CLASS(Enum, Str) \ 11978 case llvm::omp::Enum: \ 11979 break; 11980 #include "llvm/Frontend/OpenMP/OMPKinds.def" 11981 default: 11982 break; 11983 } 11984 assert(C && "Unknown OMPClause type"); 11985 11986 Visit(C); 11987 C->setLocStart(Record.readSourceLocation()); 11988 C->setLocEnd(Record.readSourceLocation()); 11989 11990 return C; 11991 } 11992 11993 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) { 11994 C->setPreInitStmt(Record.readSubStmt(), 11995 static_cast<OpenMPDirectiveKind>(Record.readInt())); 11996 } 11997 11998 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) { 11999 VisitOMPClauseWithPreInit(C); 12000 C->setPostUpdateExpr(Record.readSubExpr()); 12001 } 12002 12003 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) { 12004 VisitOMPClauseWithPreInit(C); 12005 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt())); 12006 C->setNameModifierLoc(Record.readSourceLocation()); 12007 C->setColonLoc(Record.readSourceLocation()); 12008 C->setCondition(Record.readSubExpr()); 12009 C->setLParenLoc(Record.readSourceLocation()); 12010 } 12011 12012 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) { 12013 VisitOMPClauseWithPreInit(C); 12014 C->setCondition(Record.readSubExpr()); 12015 C->setLParenLoc(Record.readSourceLocation()); 12016 } 12017 12018 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) { 12019 VisitOMPClauseWithPreInit(C); 12020 C->setNumThreads(Record.readSubExpr()); 12021 C->setLParenLoc(Record.readSourceLocation()); 12022 } 12023 12024 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) { 12025 C->setSafelen(Record.readSubExpr()); 12026 C->setLParenLoc(Record.readSourceLocation()); 12027 } 12028 12029 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) { 12030 C->setSimdlen(Record.readSubExpr()); 12031 C->setLParenLoc(Record.readSourceLocation()); 12032 } 12033 12034 void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) { 12035 for (Expr *&E : C->getSizesRefs()) 12036 E = Record.readSubExpr(); 12037 C->setLParenLoc(Record.readSourceLocation()); 12038 } 12039 12040 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) { 12041 C->setAllocator(Record.readExpr()); 12042 C->setLParenLoc(Record.readSourceLocation()); 12043 } 12044 12045 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) { 12046 C->setNumForLoops(Record.readSubExpr()); 12047 C->setLParenLoc(Record.readSourceLocation()); 12048 } 12049 12050 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) { 12051 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt())); 12052 C->setLParenLoc(Record.readSourceLocation()); 12053 C->setDefaultKindKwLoc(Record.readSourceLocation()); 12054 } 12055 12056 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) { 12057 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt())); 12058 C->setLParenLoc(Record.readSourceLocation()); 12059 C->setProcBindKindKwLoc(Record.readSourceLocation()); 12060 } 12061 12062 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) { 12063 VisitOMPClauseWithPreInit(C); 12064 C->setScheduleKind( 12065 static_cast<OpenMPScheduleClauseKind>(Record.readInt())); 12066 C->setFirstScheduleModifier( 12067 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 12068 C->setSecondScheduleModifier( 12069 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 12070 C->setChunkSize(Record.readSubExpr()); 12071 C->setLParenLoc(Record.readSourceLocation()); 12072 C->setFirstScheduleModifierLoc(Record.readSourceLocation()); 12073 C->setSecondScheduleModifierLoc(Record.readSourceLocation()); 12074 C->setScheduleKindLoc(Record.readSourceLocation()); 12075 C->setCommaLoc(Record.readSourceLocation()); 12076 } 12077 12078 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) { 12079 C->setNumForLoops(Record.readSubExpr()); 12080 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 12081 C->setLoopNumIterations(I, Record.readSubExpr()); 12082 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 12083 C->setLoopCounter(I, Record.readSubExpr()); 12084 C->setLParenLoc(Record.readSourceLocation()); 12085 } 12086 12087 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) { 12088 C->setEventHandler(Record.readSubExpr()); 12089 C->setLParenLoc(Record.readSourceLocation()); 12090 } 12091 12092 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {} 12093 12094 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {} 12095 12096 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {} 12097 12098 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {} 12099 12100 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {} 12101 12102 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) { 12103 if (C->isExtended()) { 12104 C->setLParenLoc(Record.readSourceLocation()); 12105 C->setArgumentLoc(Record.readSourceLocation()); 12106 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>()); 12107 } 12108 } 12109 12110 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {} 12111 12112 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {} 12113 12114 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {} 12115 12116 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {} 12117 12118 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {} 12119 12120 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {} 12121 12122 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {} 12123 12124 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {} 12125 12126 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {} 12127 12128 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *) {} 12129 12130 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {} 12131 12132 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause( 12133 OMPUnifiedSharedMemoryClause *) {} 12134 12135 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {} 12136 12137 void 12138 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) { 12139 } 12140 12141 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause( 12142 OMPAtomicDefaultMemOrderClause *C) { 12143 C->setAtomicDefaultMemOrderKind( 12144 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt())); 12145 C->setLParenLoc(Record.readSourceLocation()); 12146 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation()); 12147 } 12148 12149 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) { 12150 C->setLParenLoc(Record.readSourceLocation()); 12151 unsigned NumVars = C->varlist_size(); 12152 SmallVector<Expr *, 16> Vars; 12153 Vars.reserve(NumVars); 12154 for (unsigned i = 0; i != NumVars; ++i) 12155 Vars.push_back(Record.readSubExpr()); 12156 C->setVarRefs(Vars); 12157 Vars.clear(); 12158 for (unsigned i = 0; i != NumVars; ++i) 12159 Vars.push_back(Record.readSubExpr()); 12160 C->setPrivateCopies(Vars); 12161 } 12162 12163 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) { 12164 VisitOMPClauseWithPreInit(C); 12165 C->setLParenLoc(Record.readSourceLocation()); 12166 unsigned NumVars = C->varlist_size(); 12167 SmallVector<Expr *, 16> Vars; 12168 Vars.reserve(NumVars); 12169 for (unsigned i = 0; i != NumVars; ++i) 12170 Vars.push_back(Record.readSubExpr()); 12171 C->setVarRefs(Vars); 12172 Vars.clear(); 12173 for (unsigned i = 0; i != NumVars; ++i) 12174 Vars.push_back(Record.readSubExpr()); 12175 C->setPrivateCopies(Vars); 12176 Vars.clear(); 12177 for (unsigned i = 0; i != NumVars; ++i) 12178 Vars.push_back(Record.readSubExpr()); 12179 C->setInits(Vars); 12180 } 12181 12182 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) { 12183 VisitOMPClauseWithPostUpdate(C); 12184 C->setLParenLoc(Record.readSourceLocation()); 12185 C->setKind(Record.readEnum<OpenMPLastprivateModifier>()); 12186 C->setKindLoc(Record.readSourceLocation()); 12187 C->setColonLoc(Record.readSourceLocation()); 12188 unsigned NumVars = C->varlist_size(); 12189 SmallVector<Expr *, 16> Vars; 12190 Vars.reserve(NumVars); 12191 for (unsigned i = 0; i != NumVars; ++i) 12192 Vars.push_back(Record.readSubExpr()); 12193 C->setVarRefs(Vars); 12194 Vars.clear(); 12195 for (unsigned i = 0; i != NumVars; ++i) 12196 Vars.push_back(Record.readSubExpr()); 12197 C->setPrivateCopies(Vars); 12198 Vars.clear(); 12199 for (unsigned i = 0; i != NumVars; ++i) 12200 Vars.push_back(Record.readSubExpr()); 12201 C->setSourceExprs(Vars); 12202 Vars.clear(); 12203 for (unsigned i = 0; i != NumVars; ++i) 12204 Vars.push_back(Record.readSubExpr()); 12205 C->setDestinationExprs(Vars); 12206 Vars.clear(); 12207 for (unsigned i = 0; i != NumVars; ++i) 12208 Vars.push_back(Record.readSubExpr()); 12209 C->setAssignmentOps(Vars); 12210 } 12211 12212 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) { 12213 C->setLParenLoc(Record.readSourceLocation()); 12214 unsigned NumVars = C->varlist_size(); 12215 SmallVector<Expr *, 16> Vars; 12216 Vars.reserve(NumVars); 12217 for (unsigned i = 0; i != NumVars; ++i) 12218 Vars.push_back(Record.readSubExpr()); 12219 C->setVarRefs(Vars); 12220 } 12221 12222 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) { 12223 VisitOMPClauseWithPostUpdate(C); 12224 C->setLParenLoc(Record.readSourceLocation()); 12225 C->setModifierLoc(Record.readSourceLocation()); 12226 C->setColonLoc(Record.readSourceLocation()); 12227 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12228 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12229 C->setQualifierLoc(NNSL); 12230 C->setNameInfo(DNI); 12231 12232 unsigned NumVars = C->varlist_size(); 12233 SmallVector<Expr *, 16> Vars; 12234 Vars.reserve(NumVars); 12235 for (unsigned i = 0; i != NumVars; ++i) 12236 Vars.push_back(Record.readSubExpr()); 12237 C->setVarRefs(Vars); 12238 Vars.clear(); 12239 for (unsigned i = 0; i != NumVars; ++i) 12240 Vars.push_back(Record.readSubExpr()); 12241 C->setPrivates(Vars); 12242 Vars.clear(); 12243 for (unsigned i = 0; i != NumVars; ++i) 12244 Vars.push_back(Record.readSubExpr()); 12245 C->setLHSExprs(Vars); 12246 Vars.clear(); 12247 for (unsigned i = 0; i != NumVars; ++i) 12248 Vars.push_back(Record.readSubExpr()); 12249 C->setRHSExprs(Vars); 12250 Vars.clear(); 12251 for (unsigned i = 0; i != NumVars; ++i) 12252 Vars.push_back(Record.readSubExpr()); 12253 C->setReductionOps(Vars); 12254 if (C->getModifier() == OMPC_REDUCTION_inscan) { 12255 Vars.clear(); 12256 for (unsigned i = 0; i != NumVars; ++i) 12257 Vars.push_back(Record.readSubExpr()); 12258 C->setInscanCopyOps(Vars); 12259 Vars.clear(); 12260 for (unsigned i = 0; i != NumVars; ++i) 12261 Vars.push_back(Record.readSubExpr()); 12262 C->setInscanCopyArrayTemps(Vars); 12263 Vars.clear(); 12264 for (unsigned i = 0; i != NumVars; ++i) 12265 Vars.push_back(Record.readSubExpr()); 12266 C->setInscanCopyArrayElems(Vars); 12267 } 12268 } 12269 12270 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) { 12271 VisitOMPClauseWithPostUpdate(C); 12272 C->setLParenLoc(Record.readSourceLocation()); 12273 C->setColonLoc(Record.readSourceLocation()); 12274 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12275 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12276 C->setQualifierLoc(NNSL); 12277 C->setNameInfo(DNI); 12278 12279 unsigned NumVars = C->varlist_size(); 12280 SmallVector<Expr *, 16> Vars; 12281 Vars.reserve(NumVars); 12282 for (unsigned I = 0; I != NumVars; ++I) 12283 Vars.push_back(Record.readSubExpr()); 12284 C->setVarRefs(Vars); 12285 Vars.clear(); 12286 for (unsigned I = 0; I != NumVars; ++I) 12287 Vars.push_back(Record.readSubExpr()); 12288 C->setPrivates(Vars); 12289 Vars.clear(); 12290 for (unsigned I = 0; I != NumVars; ++I) 12291 Vars.push_back(Record.readSubExpr()); 12292 C->setLHSExprs(Vars); 12293 Vars.clear(); 12294 for (unsigned I = 0; I != NumVars; ++I) 12295 Vars.push_back(Record.readSubExpr()); 12296 C->setRHSExprs(Vars); 12297 Vars.clear(); 12298 for (unsigned I = 0; I != NumVars; ++I) 12299 Vars.push_back(Record.readSubExpr()); 12300 C->setReductionOps(Vars); 12301 } 12302 12303 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) { 12304 VisitOMPClauseWithPostUpdate(C); 12305 C->setLParenLoc(Record.readSourceLocation()); 12306 C->setColonLoc(Record.readSourceLocation()); 12307 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12308 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12309 C->setQualifierLoc(NNSL); 12310 C->setNameInfo(DNI); 12311 12312 unsigned NumVars = C->varlist_size(); 12313 SmallVector<Expr *, 16> Vars; 12314 Vars.reserve(NumVars); 12315 for (unsigned I = 0; I != NumVars; ++I) 12316 Vars.push_back(Record.readSubExpr()); 12317 C->setVarRefs(Vars); 12318 Vars.clear(); 12319 for (unsigned I = 0; I != NumVars; ++I) 12320 Vars.push_back(Record.readSubExpr()); 12321 C->setPrivates(Vars); 12322 Vars.clear(); 12323 for (unsigned I = 0; I != NumVars; ++I) 12324 Vars.push_back(Record.readSubExpr()); 12325 C->setLHSExprs(Vars); 12326 Vars.clear(); 12327 for (unsigned I = 0; I != NumVars; ++I) 12328 Vars.push_back(Record.readSubExpr()); 12329 C->setRHSExprs(Vars); 12330 Vars.clear(); 12331 for (unsigned I = 0; I != NumVars; ++I) 12332 Vars.push_back(Record.readSubExpr()); 12333 C->setReductionOps(Vars); 12334 Vars.clear(); 12335 for (unsigned I = 0; I != NumVars; ++I) 12336 Vars.push_back(Record.readSubExpr()); 12337 C->setTaskgroupDescriptors(Vars); 12338 } 12339 12340 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) { 12341 VisitOMPClauseWithPostUpdate(C); 12342 C->setLParenLoc(Record.readSourceLocation()); 12343 C->setColonLoc(Record.readSourceLocation()); 12344 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt())); 12345 C->setModifierLoc(Record.readSourceLocation()); 12346 unsigned NumVars = C->varlist_size(); 12347 SmallVector<Expr *, 16> Vars; 12348 Vars.reserve(NumVars); 12349 for (unsigned i = 0; i != NumVars; ++i) 12350 Vars.push_back(Record.readSubExpr()); 12351 C->setVarRefs(Vars); 12352 Vars.clear(); 12353 for (unsigned i = 0; i != NumVars; ++i) 12354 Vars.push_back(Record.readSubExpr()); 12355 C->setPrivates(Vars); 12356 Vars.clear(); 12357 for (unsigned i = 0; i != NumVars; ++i) 12358 Vars.push_back(Record.readSubExpr()); 12359 C->setInits(Vars); 12360 Vars.clear(); 12361 for (unsigned i = 0; i != NumVars; ++i) 12362 Vars.push_back(Record.readSubExpr()); 12363 C->setUpdates(Vars); 12364 Vars.clear(); 12365 for (unsigned i = 0; i != NumVars; ++i) 12366 Vars.push_back(Record.readSubExpr()); 12367 C->setFinals(Vars); 12368 C->setStep(Record.readSubExpr()); 12369 C->setCalcStep(Record.readSubExpr()); 12370 Vars.clear(); 12371 for (unsigned I = 0; I != NumVars + 1; ++I) 12372 Vars.push_back(Record.readSubExpr()); 12373 C->setUsedExprs(Vars); 12374 } 12375 12376 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) { 12377 C->setLParenLoc(Record.readSourceLocation()); 12378 C->setColonLoc(Record.readSourceLocation()); 12379 unsigned NumVars = C->varlist_size(); 12380 SmallVector<Expr *, 16> Vars; 12381 Vars.reserve(NumVars); 12382 for (unsigned i = 0; i != NumVars; ++i) 12383 Vars.push_back(Record.readSubExpr()); 12384 C->setVarRefs(Vars); 12385 C->setAlignment(Record.readSubExpr()); 12386 } 12387 12388 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) { 12389 C->setLParenLoc(Record.readSourceLocation()); 12390 unsigned NumVars = C->varlist_size(); 12391 SmallVector<Expr *, 16> Exprs; 12392 Exprs.reserve(NumVars); 12393 for (unsigned i = 0; i != NumVars; ++i) 12394 Exprs.push_back(Record.readSubExpr()); 12395 C->setVarRefs(Exprs); 12396 Exprs.clear(); 12397 for (unsigned i = 0; i != NumVars; ++i) 12398 Exprs.push_back(Record.readSubExpr()); 12399 C->setSourceExprs(Exprs); 12400 Exprs.clear(); 12401 for (unsigned i = 0; i != NumVars; ++i) 12402 Exprs.push_back(Record.readSubExpr()); 12403 C->setDestinationExprs(Exprs); 12404 Exprs.clear(); 12405 for (unsigned i = 0; i != NumVars; ++i) 12406 Exprs.push_back(Record.readSubExpr()); 12407 C->setAssignmentOps(Exprs); 12408 } 12409 12410 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) { 12411 C->setLParenLoc(Record.readSourceLocation()); 12412 unsigned NumVars = C->varlist_size(); 12413 SmallVector<Expr *, 16> Exprs; 12414 Exprs.reserve(NumVars); 12415 for (unsigned i = 0; i != NumVars; ++i) 12416 Exprs.push_back(Record.readSubExpr()); 12417 C->setVarRefs(Exprs); 12418 Exprs.clear(); 12419 for (unsigned i = 0; i != NumVars; ++i) 12420 Exprs.push_back(Record.readSubExpr()); 12421 C->setSourceExprs(Exprs); 12422 Exprs.clear(); 12423 for (unsigned i = 0; i != NumVars; ++i) 12424 Exprs.push_back(Record.readSubExpr()); 12425 C->setDestinationExprs(Exprs); 12426 Exprs.clear(); 12427 for (unsigned i = 0; i != NumVars; ++i) 12428 Exprs.push_back(Record.readSubExpr()); 12429 C->setAssignmentOps(Exprs); 12430 } 12431 12432 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) { 12433 C->setLParenLoc(Record.readSourceLocation()); 12434 unsigned NumVars = C->varlist_size(); 12435 SmallVector<Expr *, 16> Vars; 12436 Vars.reserve(NumVars); 12437 for (unsigned i = 0; i != NumVars; ++i) 12438 Vars.push_back(Record.readSubExpr()); 12439 C->setVarRefs(Vars); 12440 } 12441 12442 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) { 12443 C->setDepobj(Record.readSubExpr()); 12444 C->setLParenLoc(Record.readSourceLocation()); 12445 } 12446 12447 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) { 12448 C->setLParenLoc(Record.readSourceLocation()); 12449 C->setModifier(Record.readSubExpr()); 12450 C->setDependencyKind( 12451 static_cast<OpenMPDependClauseKind>(Record.readInt())); 12452 C->setDependencyLoc(Record.readSourceLocation()); 12453 C->setColonLoc(Record.readSourceLocation()); 12454 unsigned NumVars = C->varlist_size(); 12455 SmallVector<Expr *, 16> Vars; 12456 Vars.reserve(NumVars); 12457 for (unsigned I = 0; I != NumVars; ++I) 12458 Vars.push_back(Record.readSubExpr()); 12459 C->setVarRefs(Vars); 12460 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I) 12461 C->setLoopData(I, Record.readSubExpr()); 12462 } 12463 12464 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) { 12465 VisitOMPClauseWithPreInit(C); 12466 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>()); 12467 C->setDevice(Record.readSubExpr()); 12468 C->setModifierLoc(Record.readSourceLocation()); 12469 C->setLParenLoc(Record.readSourceLocation()); 12470 } 12471 12472 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) { 12473 C->setLParenLoc(Record.readSourceLocation()); 12474 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) { 12475 C->setMapTypeModifier( 12476 I, static_cast<OpenMPMapModifierKind>(Record.readInt())); 12477 C->setMapTypeModifierLoc(I, Record.readSourceLocation()); 12478 } 12479 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12480 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12481 C->setMapType( 12482 static_cast<OpenMPMapClauseKind>(Record.readInt())); 12483 C->setMapLoc(Record.readSourceLocation()); 12484 C->setColonLoc(Record.readSourceLocation()); 12485 auto NumVars = C->varlist_size(); 12486 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12487 auto TotalLists = C->getTotalComponentListNum(); 12488 auto TotalComponents = C->getTotalComponentsNum(); 12489 12490 SmallVector<Expr *, 16> Vars; 12491 Vars.reserve(NumVars); 12492 for (unsigned i = 0; i != NumVars; ++i) 12493 Vars.push_back(Record.readExpr()); 12494 C->setVarRefs(Vars); 12495 12496 SmallVector<Expr *, 16> UDMappers; 12497 UDMappers.reserve(NumVars); 12498 for (unsigned I = 0; I < NumVars; ++I) 12499 UDMappers.push_back(Record.readExpr()); 12500 C->setUDMapperRefs(UDMappers); 12501 12502 SmallVector<ValueDecl *, 16> Decls; 12503 Decls.reserve(UniqueDecls); 12504 for (unsigned i = 0; i < UniqueDecls; ++i) 12505 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12506 C->setUniqueDecls(Decls); 12507 12508 SmallVector<unsigned, 16> ListsPerDecl; 12509 ListsPerDecl.reserve(UniqueDecls); 12510 for (unsigned i = 0; i < UniqueDecls; ++i) 12511 ListsPerDecl.push_back(Record.readInt()); 12512 C->setDeclNumLists(ListsPerDecl); 12513 12514 SmallVector<unsigned, 32> ListSizes; 12515 ListSizes.reserve(TotalLists); 12516 for (unsigned i = 0; i < TotalLists; ++i) 12517 ListSizes.push_back(Record.readInt()); 12518 C->setComponentListSizes(ListSizes); 12519 12520 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12521 Components.reserve(TotalComponents); 12522 for (unsigned i = 0; i < TotalComponents; ++i) { 12523 Expr *AssociatedExprPr = Record.readExpr(); 12524 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12525 Components.emplace_back(AssociatedExprPr, AssociatedDecl, 12526 /*IsNonContiguous=*/false); 12527 } 12528 C->setComponents(Components, ListSizes); 12529 } 12530 12531 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) { 12532 C->setLParenLoc(Record.readSourceLocation()); 12533 C->setColonLoc(Record.readSourceLocation()); 12534 C->setAllocator(Record.readSubExpr()); 12535 unsigned NumVars = C->varlist_size(); 12536 SmallVector<Expr *, 16> Vars; 12537 Vars.reserve(NumVars); 12538 for (unsigned i = 0; i != NumVars; ++i) 12539 Vars.push_back(Record.readSubExpr()); 12540 C->setVarRefs(Vars); 12541 } 12542 12543 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) { 12544 VisitOMPClauseWithPreInit(C); 12545 C->setNumTeams(Record.readSubExpr()); 12546 C->setLParenLoc(Record.readSourceLocation()); 12547 } 12548 12549 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) { 12550 VisitOMPClauseWithPreInit(C); 12551 C->setThreadLimit(Record.readSubExpr()); 12552 C->setLParenLoc(Record.readSourceLocation()); 12553 } 12554 12555 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) { 12556 VisitOMPClauseWithPreInit(C); 12557 C->setPriority(Record.readSubExpr()); 12558 C->setLParenLoc(Record.readSourceLocation()); 12559 } 12560 12561 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) { 12562 VisitOMPClauseWithPreInit(C); 12563 C->setGrainsize(Record.readSubExpr()); 12564 C->setLParenLoc(Record.readSourceLocation()); 12565 } 12566 12567 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) { 12568 VisitOMPClauseWithPreInit(C); 12569 C->setNumTasks(Record.readSubExpr()); 12570 C->setLParenLoc(Record.readSourceLocation()); 12571 } 12572 12573 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) { 12574 C->setHint(Record.readSubExpr()); 12575 C->setLParenLoc(Record.readSourceLocation()); 12576 } 12577 12578 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) { 12579 VisitOMPClauseWithPreInit(C); 12580 C->setDistScheduleKind( 12581 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt())); 12582 C->setChunkSize(Record.readSubExpr()); 12583 C->setLParenLoc(Record.readSourceLocation()); 12584 C->setDistScheduleKindLoc(Record.readSourceLocation()); 12585 C->setCommaLoc(Record.readSourceLocation()); 12586 } 12587 12588 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) { 12589 C->setDefaultmapKind( 12590 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt())); 12591 C->setDefaultmapModifier( 12592 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt())); 12593 C->setLParenLoc(Record.readSourceLocation()); 12594 C->setDefaultmapModifierLoc(Record.readSourceLocation()); 12595 C->setDefaultmapKindLoc(Record.readSourceLocation()); 12596 } 12597 12598 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) { 12599 C->setLParenLoc(Record.readSourceLocation()); 12600 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) { 12601 C->setMotionModifier( 12602 I, static_cast<OpenMPMotionModifierKind>(Record.readInt())); 12603 C->setMotionModifierLoc(I, Record.readSourceLocation()); 12604 } 12605 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12606 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12607 C->setColonLoc(Record.readSourceLocation()); 12608 auto NumVars = C->varlist_size(); 12609 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12610 auto TotalLists = C->getTotalComponentListNum(); 12611 auto TotalComponents = C->getTotalComponentsNum(); 12612 12613 SmallVector<Expr *, 16> Vars; 12614 Vars.reserve(NumVars); 12615 for (unsigned i = 0; i != NumVars; ++i) 12616 Vars.push_back(Record.readSubExpr()); 12617 C->setVarRefs(Vars); 12618 12619 SmallVector<Expr *, 16> UDMappers; 12620 UDMappers.reserve(NumVars); 12621 for (unsigned I = 0; I < NumVars; ++I) 12622 UDMappers.push_back(Record.readSubExpr()); 12623 C->setUDMapperRefs(UDMappers); 12624 12625 SmallVector<ValueDecl *, 16> Decls; 12626 Decls.reserve(UniqueDecls); 12627 for (unsigned i = 0; i < UniqueDecls; ++i) 12628 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12629 C->setUniqueDecls(Decls); 12630 12631 SmallVector<unsigned, 16> ListsPerDecl; 12632 ListsPerDecl.reserve(UniqueDecls); 12633 for (unsigned i = 0; i < UniqueDecls; ++i) 12634 ListsPerDecl.push_back(Record.readInt()); 12635 C->setDeclNumLists(ListsPerDecl); 12636 12637 SmallVector<unsigned, 32> ListSizes; 12638 ListSizes.reserve(TotalLists); 12639 for (unsigned i = 0; i < TotalLists; ++i) 12640 ListSizes.push_back(Record.readInt()); 12641 C->setComponentListSizes(ListSizes); 12642 12643 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12644 Components.reserve(TotalComponents); 12645 for (unsigned i = 0; i < TotalComponents; ++i) { 12646 Expr *AssociatedExprPr = Record.readSubExpr(); 12647 bool IsNonContiguous = Record.readBool(); 12648 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12649 Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous); 12650 } 12651 C->setComponents(Components, ListSizes); 12652 } 12653 12654 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) { 12655 C->setLParenLoc(Record.readSourceLocation()); 12656 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) { 12657 C->setMotionModifier( 12658 I, static_cast<OpenMPMotionModifierKind>(Record.readInt())); 12659 C->setMotionModifierLoc(I, Record.readSourceLocation()); 12660 } 12661 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12662 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12663 C->setColonLoc(Record.readSourceLocation()); 12664 auto NumVars = C->varlist_size(); 12665 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12666 auto TotalLists = C->getTotalComponentListNum(); 12667 auto TotalComponents = C->getTotalComponentsNum(); 12668 12669 SmallVector<Expr *, 16> Vars; 12670 Vars.reserve(NumVars); 12671 for (unsigned i = 0; i != NumVars; ++i) 12672 Vars.push_back(Record.readSubExpr()); 12673 C->setVarRefs(Vars); 12674 12675 SmallVector<Expr *, 16> UDMappers; 12676 UDMappers.reserve(NumVars); 12677 for (unsigned I = 0; I < NumVars; ++I) 12678 UDMappers.push_back(Record.readSubExpr()); 12679 C->setUDMapperRefs(UDMappers); 12680 12681 SmallVector<ValueDecl *, 16> Decls; 12682 Decls.reserve(UniqueDecls); 12683 for (unsigned i = 0; i < UniqueDecls; ++i) 12684 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12685 C->setUniqueDecls(Decls); 12686 12687 SmallVector<unsigned, 16> ListsPerDecl; 12688 ListsPerDecl.reserve(UniqueDecls); 12689 for (unsigned i = 0; i < UniqueDecls; ++i) 12690 ListsPerDecl.push_back(Record.readInt()); 12691 C->setDeclNumLists(ListsPerDecl); 12692 12693 SmallVector<unsigned, 32> ListSizes; 12694 ListSizes.reserve(TotalLists); 12695 for (unsigned i = 0; i < TotalLists; ++i) 12696 ListSizes.push_back(Record.readInt()); 12697 C->setComponentListSizes(ListSizes); 12698 12699 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12700 Components.reserve(TotalComponents); 12701 for (unsigned i = 0; i < TotalComponents; ++i) { 12702 Expr *AssociatedExprPr = Record.readSubExpr(); 12703 bool IsNonContiguous = Record.readBool(); 12704 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12705 Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous); 12706 } 12707 C->setComponents(Components, ListSizes); 12708 } 12709 12710 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) { 12711 C->setLParenLoc(Record.readSourceLocation()); 12712 auto NumVars = C->varlist_size(); 12713 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12714 auto TotalLists = C->getTotalComponentListNum(); 12715 auto TotalComponents = C->getTotalComponentsNum(); 12716 12717 SmallVector<Expr *, 16> Vars; 12718 Vars.reserve(NumVars); 12719 for (unsigned i = 0; i != NumVars; ++i) 12720 Vars.push_back(Record.readSubExpr()); 12721 C->setVarRefs(Vars); 12722 Vars.clear(); 12723 for (unsigned i = 0; i != NumVars; ++i) 12724 Vars.push_back(Record.readSubExpr()); 12725 C->setPrivateCopies(Vars); 12726 Vars.clear(); 12727 for (unsigned i = 0; i != NumVars; ++i) 12728 Vars.push_back(Record.readSubExpr()); 12729 C->setInits(Vars); 12730 12731 SmallVector<ValueDecl *, 16> Decls; 12732 Decls.reserve(UniqueDecls); 12733 for (unsigned i = 0; i < UniqueDecls; ++i) 12734 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12735 C->setUniqueDecls(Decls); 12736 12737 SmallVector<unsigned, 16> ListsPerDecl; 12738 ListsPerDecl.reserve(UniqueDecls); 12739 for (unsigned i = 0; i < UniqueDecls; ++i) 12740 ListsPerDecl.push_back(Record.readInt()); 12741 C->setDeclNumLists(ListsPerDecl); 12742 12743 SmallVector<unsigned, 32> ListSizes; 12744 ListSizes.reserve(TotalLists); 12745 for (unsigned i = 0; i < TotalLists; ++i) 12746 ListSizes.push_back(Record.readInt()); 12747 C->setComponentListSizes(ListSizes); 12748 12749 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12750 Components.reserve(TotalComponents); 12751 for (unsigned i = 0; i < TotalComponents; ++i) { 12752 auto *AssociatedExprPr = Record.readSubExpr(); 12753 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12754 Components.emplace_back(AssociatedExprPr, AssociatedDecl, 12755 /*IsNonContiguous=*/false); 12756 } 12757 C->setComponents(Components, ListSizes); 12758 } 12759 12760 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) { 12761 C->setLParenLoc(Record.readSourceLocation()); 12762 auto NumVars = C->varlist_size(); 12763 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12764 auto TotalLists = C->getTotalComponentListNum(); 12765 auto TotalComponents = C->getTotalComponentsNum(); 12766 12767 SmallVector<Expr *, 16> Vars; 12768 Vars.reserve(NumVars); 12769 for (unsigned i = 0; i != NumVars; ++i) 12770 Vars.push_back(Record.readSubExpr()); 12771 C->setVarRefs(Vars); 12772 12773 SmallVector<ValueDecl *, 16> Decls; 12774 Decls.reserve(UniqueDecls); 12775 for (unsigned i = 0; i < UniqueDecls; ++i) 12776 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12777 C->setUniqueDecls(Decls); 12778 12779 SmallVector<unsigned, 16> ListsPerDecl; 12780 ListsPerDecl.reserve(UniqueDecls); 12781 for (unsigned i = 0; i < UniqueDecls; ++i) 12782 ListsPerDecl.push_back(Record.readInt()); 12783 C->setDeclNumLists(ListsPerDecl); 12784 12785 SmallVector<unsigned, 32> ListSizes; 12786 ListSizes.reserve(TotalLists); 12787 for (unsigned i = 0; i < TotalLists; ++i) 12788 ListSizes.push_back(Record.readInt()); 12789 C->setComponentListSizes(ListSizes); 12790 12791 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12792 Components.reserve(TotalComponents); 12793 for (unsigned i = 0; i < TotalComponents; ++i) { 12794 Expr *AssociatedExpr = Record.readSubExpr(); 12795 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12796 Components.emplace_back(AssociatedExpr, AssociatedDecl, 12797 /*IsNonContiguous*/ false); 12798 } 12799 C->setComponents(Components, ListSizes); 12800 } 12801 12802 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) { 12803 C->setLParenLoc(Record.readSourceLocation()); 12804 auto NumVars = C->varlist_size(); 12805 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12806 auto TotalLists = C->getTotalComponentListNum(); 12807 auto TotalComponents = C->getTotalComponentsNum(); 12808 12809 SmallVector<Expr *, 16> Vars; 12810 Vars.reserve(NumVars); 12811 for (unsigned i = 0; i != NumVars; ++i) 12812 Vars.push_back(Record.readSubExpr()); 12813 C->setVarRefs(Vars); 12814 Vars.clear(); 12815 12816 SmallVector<ValueDecl *, 16> Decls; 12817 Decls.reserve(UniqueDecls); 12818 for (unsigned i = 0; i < UniqueDecls; ++i) 12819 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12820 C->setUniqueDecls(Decls); 12821 12822 SmallVector<unsigned, 16> ListsPerDecl; 12823 ListsPerDecl.reserve(UniqueDecls); 12824 for (unsigned i = 0; i < UniqueDecls; ++i) 12825 ListsPerDecl.push_back(Record.readInt()); 12826 C->setDeclNumLists(ListsPerDecl); 12827 12828 SmallVector<unsigned, 32> ListSizes; 12829 ListSizes.reserve(TotalLists); 12830 for (unsigned i = 0; i < TotalLists; ++i) 12831 ListSizes.push_back(Record.readInt()); 12832 C->setComponentListSizes(ListSizes); 12833 12834 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12835 Components.reserve(TotalComponents); 12836 for (unsigned i = 0; i < TotalComponents; ++i) { 12837 Expr *AssociatedExpr = Record.readSubExpr(); 12838 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12839 Components.emplace_back(AssociatedExpr, AssociatedDecl, 12840 /*IsNonContiguous=*/false); 12841 } 12842 C->setComponents(Components, ListSizes); 12843 } 12844 12845 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) { 12846 C->setLParenLoc(Record.readSourceLocation()); 12847 unsigned NumVars = C->varlist_size(); 12848 SmallVector<Expr *, 16> Vars; 12849 Vars.reserve(NumVars); 12850 for (unsigned i = 0; i != NumVars; ++i) 12851 Vars.push_back(Record.readSubExpr()); 12852 C->setVarRefs(Vars); 12853 Vars.clear(); 12854 Vars.reserve(NumVars); 12855 for (unsigned i = 0; i != NumVars; ++i) 12856 Vars.push_back(Record.readSubExpr()); 12857 C->setPrivateRefs(Vars); 12858 } 12859 12860 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) { 12861 C->setLParenLoc(Record.readSourceLocation()); 12862 unsigned NumVars = C->varlist_size(); 12863 SmallVector<Expr *, 16> Vars; 12864 Vars.reserve(NumVars); 12865 for (unsigned i = 0; i != NumVars; ++i) 12866 Vars.push_back(Record.readSubExpr()); 12867 C->setVarRefs(Vars); 12868 } 12869 12870 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) { 12871 C->setLParenLoc(Record.readSourceLocation()); 12872 unsigned NumVars = C->varlist_size(); 12873 SmallVector<Expr *, 16> Vars; 12874 Vars.reserve(NumVars); 12875 for (unsigned i = 0; i != NumVars; ++i) 12876 Vars.push_back(Record.readSubExpr()); 12877 C->setVarRefs(Vars); 12878 } 12879 12880 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) { 12881 C->setLParenLoc(Record.readSourceLocation()); 12882 unsigned NumOfAllocators = C->getNumberOfAllocators(); 12883 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data; 12884 Data.reserve(NumOfAllocators); 12885 for (unsigned I = 0; I != NumOfAllocators; ++I) { 12886 OMPUsesAllocatorsClause::Data &D = Data.emplace_back(); 12887 D.Allocator = Record.readSubExpr(); 12888 D.AllocatorTraits = Record.readSubExpr(); 12889 D.LParenLoc = Record.readSourceLocation(); 12890 D.RParenLoc = Record.readSourceLocation(); 12891 } 12892 C->setAllocatorsData(Data); 12893 } 12894 12895 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) { 12896 C->setLParenLoc(Record.readSourceLocation()); 12897 C->setModifier(Record.readSubExpr()); 12898 C->setColonLoc(Record.readSourceLocation()); 12899 unsigned NumOfLocators = C->varlist_size(); 12900 SmallVector<Expr *, 4> Locators; 12901 Locators.reserve(NumOfLocators); 12902 for (unsigned I = 0; I != NumOfLocators; ++I) 12903 Locators.push_back(Record.readSubExpr()); 12904 C->setVarRefs(Locators); 12905 } 12906 12907 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) { 12908 C->setKind(Record.readEnum<OpenMPOrderClauseKind>()); 12909 C->setLParenLoc(Record.readSourceLocation()); 12910 C->setKindKwLoc(Record.readSourceLocation()); 12911 } 12912 12913 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() { 12914 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo(); 12915 TI.Sets.resize(readUInt32()); 12916 for (auto &Set : TI.Sets) { 12917 Set.Kind = readEnum<llvm::omp::TraitSet>(); 12918 Set.Selectors.resize(readUInt32()); 12919 for (auto &Selector : Set.Selectors) { 12920 Selector.Kind = readEnum<llvm::omp::TraitSelector>(); 12921 Selector.ScoreOrCondition = nullptr; 12922 if (readBool()) 12923 Selector.ScoreOrCondition = readExprRef(); 12924 Selector.Properties.resize(readUInt32()); 12925 for (auto &Property : Selector.Properties) 12926 Property.Kind = readEnum<llvm::omp::TraitProperty>(); 12927 } 12928 } 12929 return &TI; 12930 } 12931 12932 void ASTRecordReader::readOMPChildren(OMPChildren *Data) { 12933 if (!Data) 12934 return; 12935 if (Reader->ReadingKind == ASTReader::Read_Stmt) { 12936 // Skip NumClauses, NumChildren and HasAssociatedStmt fields. 12937 skipInts(3); 12938 } 12939 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses()); 12940 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I) 12941 Clauses[I] = readOMPClause(); 12942 Data->setClauses(Clauses); 12943 if (Data->hasAssociatedStmt()) 12944 Data->setAssociatedStmt(readStmt()); 12945 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I) 12946 Data->getChildren()[I] = readStmt(); 12947 } 12948