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 } 7073 7074 assert(!T.isNull() && "Unknown predefined type"); 7075 return T.withFastQualifiers(FastQuals); 7076 } 7077 7078 Index -= NUM_PREDEF_TYPE_IDS; 7079 assert(Index < TypesLoaded.size() && "Type index out-of-range"); 7080 if (TypesLoaded[Index].isNull()) { 7081 TypesLoaded[Index] = readTypeRecord(Index); 7082 if (TypesLoaded[Index].isNull()) 7083 return QualType(); 7084 7085 TypesLoaded[Index]->setFromAST(); 7086 if (DeserializationListener) 7087 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID), 7088 TypesLoaded[Index]); 7089 } 7090 7091 return TypesLoaded[Index].withFastQualifiers(FastQuals); 7092 } 7093 7094 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) { 7095 return GetType(getGlobalTypeID(F, LocalID)); 7096 } 7097 7098 serialization::TypeID 7099 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const { 7100 unsigned FastQuals = LocalID & Qualifiers::FastMask; 7101 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth; 7102 7103 if (LocalIndex < NUM_PREDEF_TYPE_IDS) 7104 return LocalID; 7105 7106 if (!F.ModuleOffsetMap.empty()) 7107 ReadModuleOffsetMap(F); 7108 7109 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7110 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS); 7111 assert(I != F.TypeRemap.end() && "Invalid index into type index remap"); 7112 7113 unsigned GlobalIndex = LocalIndex + I->second; 7114 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals; 7115 } 7116 7117 TemplateArgumentLocInfo 7118 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) { 7119 switch (Kind) { 7120 case TemplateArgument::Expression: 7121 return readExpr(); 7122 case TemplateArgument::Type: 7123 return readTypeSourceInfo(); 7124 case TemplateArgument::Template: { 7125 NestedNameSpecifierLoc QualifierLoc = 7126 readNestedNameSpecifierLoc(); 7127 SourceLocation TemplateNameLoc = readSourceLocation(); 7128 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc, 7129 TemplateNameLoc, SourceLocation()); 7130 } 7131 case TemplateArgument::TemplateExpansion: { 7132 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc(); 7133 SourceLocation TemplateNameLoc = readSourceLocation(); 7134 SourceLocation EllipsisLoc = readSourceLocation(); 7135 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc, 7136 TemplateNameLoc, EllipsisLoc); 7137 } 7138 case TemplateArgument::Null: 7139 case TemplateArgument::Integral: 7140 case TemplateArgument::Declaration: 7141 case TemplateArgument::NullPtr: 7142 case TemplateArgument::Pack: 7143 // FIXME: Is this right? 7144 return TemplateArgumentLocInfo(); 7145 } 7146 llvm_unreachable("unexpected template argument loc"); 7147 } 7148 7149 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() { 7150 TemplateArgument Arg = readTemplateArgument(); 7151 7152 if (Arg.getKind() == TemplateArgument::Expression) { 7153 if (readBool()) // bool InfoHasSameExpr. 7154 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); 7155 } 7156 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind())); 7157 } 7158 7159 const ASTTemplateArgumentListInfo * 7160 ASTRecordReader::readASTTemplateArgumentListInfo() { 7161 SourceLocation LAngleLoc = readSourceLocation(); 7162 SourceLocation RAngleLoc = readSourceLocation(); 7163 unsigned NumArgsAsWritten = readInt(); 7164 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc); 7165 for (unsigned i = 0; i != NumArgsAsWritten; ++i) 7166 TemplArgsInfo.addArgument(readTemplateArgumentLoc()); 7167 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo); 7168 } 7169 7170 Decl *ASTReader::GetExternalDecl(uint32_t ID) { 7171 return GetDecl(ID); 7172 } 7173 7174 void ASTReader::CompleteRedeclChain(const Decl *D) { 7175 if (NumCurrentElementsDeserializing) { 7176 // We arrange to not care about the complete redeclaration chain while we're 7177 // deserializing. Just remember that the AST has marked this one as complete 7178 // but that it's not actually complete yet, so we know we still need to 7179 // complete it later. 7180 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D)); 7181 return; 7182 } 7183 7184 const DeclContext *DC = D->getDeclContext()->getRedeclContext(); 7185 7186 // If this is a named declaration, complete it by looking it up 7187 // within its context. 7188 // 7189 // FIXME: Merging a function definition should merge 7190 // all mergeable entities within it. 7191 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) || 7192 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) { 7193 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) { 7194 if (!getContext().getLangOpts().CPlusPlus && 7195 isa<TranslationUnitDecl>(DC)) { 7196 // Outside of C++, we don't have a lookup table for the TU, so update 7197 // the identifier instead. (For C++ modules, we don't store decls 7198 // in the serialized identifier table, so we do the lookup in the TU.) 7199 auto *II = Name.getAsIdentifierInfo(); 7200 assert(II && "non-identifier name in C?"); 7201 if (II->isOutOfDate()) 7202 updateOutOfDateIdentifier(*II); 7203 } else 7204 DC->lookup(Name); 7205 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) { 7206 // Find all declarations of this kind from the relevant context. 7207 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) { 7208 auto *DC = cast<DeclContext>(DCDecl); 7209 SmallVector<Decl*, 8> Decls; 7210 FindExternalLexicalDecls( 7211 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls); 7212 } 7213 } 7214 } 7215 7216 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) 7217 CTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7218 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) 7219 VTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7220 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 7221 if (auto *Template = FD->getPrimaryTemplate()) 7222 Template->LoadLazySpecializations(); 7223 } 7224 } 7225 7226 CXXCtorInitializer ** 7227 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) { 7228 RecordLocation Loc = getLocalBitOffset(Offset); 7229 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7230 SavedStreamPosition SavedPosition(Cursor); 7231 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7232 Error(std::move(Err)); 7233 return nullptr; 7234 } 7235 ReadingKindTracker ReadingKind(Read_Decl, *this); 7236 7237 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7238 if (!MaybeCode) { 7239 Error(MaybeCode.takeError()); 7240 return nullptr; 7241 } 7242 unsigned Code = MaybeCode.get(); 7243 7244 ASTRecordReader Record(*this, *Loc.F); 7245 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7246 if (!MaybeRecCode) { 7247 Error(MaybeRecCode.takeError()); 7248 return nullptr; 7249 } 7250 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) { 7251 Error("malformed AST file: missing C++ ctor initializers"); 7252 return nullptr; 7253 } 7254 7255 return Record.readCXXCtorInitializers(); 7256 } 7257 7258 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) { 7259 assert(ContextObj && "reading base specifiers with no AST context"); 7260 ASTContext &Context = *ContextObj; 7261 7262 RecordLocation Loc = getLocalBitOffset(Offset); 7263 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7264 SavedStreamPosition SavedPosition(Cursor); 7265 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7266 Error(std::move(Err)); 7267 return nullptr; 7268 } 7269 ReadingKindTracker ReadingKind(Read_Decl, *this); 7270 7271 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7272 if (!MaybeCode) { 7273 Error(MaybeCode.takeError()); 7274 return nullptr; 7275 } 7276 unsigned Code = MaybeCode.get(); 7277 7278 ASTRecordReader Record(*this, *Loc.F); 7279 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7280 if (!MaybeRecCode) { 7281 Error(MaybeCode.takeError()); 7282 return nullptr; 7283 } 7284 unsigned RecCode = MaybeRecCode.get(); 7285 7286 if (RecCode != DECL_CXX_BASE_SPECIFIERS) { 7287 Error("malformed AST file: missing C++ base specifiers"); 7288 return nullptr; 7289 } 7290 7291 unsigned NumBases = Record.readInt(); 7292 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases); 7293 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases]; 7294 for (unsigned I = 0; I != NumBases; ++I) 7295 Bases[I] = Record.readCXXBaseSpecifier(); 7296 return Bases; 7297 } 7298 7299 serialization::DeclID 7300 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const { 7301 if (LocalID < NUM_PREDEF_DECL_IDS) 7302 return LocalID; 7303 7304 if (!F.ModuleOffsetMap.empty()) 7305 ReadModuleOffsetMap(F); 7306 7307 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7308 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS); 7309 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap"); 7310 7311 return LocalID + I->second; 7312 } 7313 7314 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID, 7315 ModuleFile &M) const { 7316 // Predefined decls aren't from any module. 7317 if (ID < NUM_PREDEF_DECL_IDS) 7318 return false; 7319 7320 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID && 7321 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls; 7322 } 7323 7324 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) { 7325 if (!D->isFromASTFile()) 7326 return nullptr; 7327 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID()); 7328 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7329 return I->second; 7330 } 7331 7332 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) { 7333 if (ID < NUM_PREDEF_DECL_IDS) 7334 return SourceLocation(); 7335 7336 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7337 7338 if (Index > DeclsLoaded.size()) { 7339 Error("declaration ID out-of-range for AST file"); 7340 return SourceLocation(); 7341 } 7342 7343 if (Decl *D = DeclsLoaded[Index]) 7344 return D->getLocation(); 7345 7346 SourceLocation Loc; 7347 DeclCursorForID(ID, Loc); 7348 return Loc; 7349 } 7350 7351 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) { 7352 switch (ID) { 7353 case PREDEF_DECL_NULL_ID: 7354 return nullptr; 7355 7356 case PREDEF_DECL_TRANSLATION_UNIT_ID: 7357 return Context.getTranslationUnitDecl(); 7358 7359 case PREDEF_DECL_OBJC_ID_ID: 7360 return Context.getObjCIdDecl(); 7361 7362 case PREDEF_DECL_OBJC_SEL_ID: 7363 return Context.getObjCSelDecl(); 7364 7365 case PREDEF_DECL_OBJC_CLASS_ID: 7366 return Context.getObjCClassDecl(); 7367 7368 case PREDEF_DECL_OBJC_PROTOCOL_ID: 7369 return Context.getObjCProtocolDecl(); 7370 7371 case PREDEF_DECL_INT_128_ID: 7372 return Context.getInt128Decl(); 7373 7374 case PREDEF_DECL_UNSIGNED_INT_128_ID: 7375 return Context.getUInt128Decl(); 7376 7377 case PREDEF_DECL_OBJC_INSTANCETYPE_ID: 7378 return Context.getObjCInstanceTypeDecl(); 7379 7380 case PREDEF_DECL_BUILTIN_VA_LIST_ID: 7381 return Context.getBuiltinVaListDecl(); 7382 7383 case PREDEF_DECL_VA_LIST_TAG: 7384 return Context.getVaListTagDecl(); 7385 7386 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID: 7387 return Context.getBuiltinMSVaListDecl(); 7388 7389 case PREDEF_DECL_BUILTIN_MS_GUID_ID: 7390 return Context.getMSGuidTagDecl(); 7391 7392 case PREDEF_DECL_EXTERN_C_CONTEXT_ID: 7393 return Context.getExternCContextDecl(); 7394 7395 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID: 7396 return Context.getMakeIntegerSeqDecl(); 7397 7398 case PREDEF_DECL_CF_CONSTANT_STRING_ID: 7399 return Context.getCFConstantStringDecl(); 7400 7401 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID: 7402 return Context.getCFConstantStringTagDecl(); 7403 7404 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID: 7405 return Context.getTypePackElementDecl(); 7406 } 7407 llvm_unreachable("PredefinedDeclIDs unknown enum value"); 7408 } 7409 7410 Decl *ASTReader::GetExistingDecl(DeclID ID) { 7411 assert(ContextObj && "reading decl with no AST context"); 7412 if (ID < NUM_PREDEF_DECL_IDS) { 7413 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID); 7414 if (D) { 7415 // Track that we have merged the declaration with ID \p ID into the 7416 // pre-existing predefined declaration \p D. 7417 auto &Merged = KeyDecls[D->getCanonicalDecl()]; 7418 if (Merged.empty()) 7419 Merged.push_back(ID); 7420 } 7421 return D; 7422 } 7423 7424 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7425 7426 if (Index >= DeclsLoaded.size()) { 7427 assert(0 && "declaration ID out-of-range for AST file"); 7428 Error("declaration ID out-of-range for AST file"); 7429 return nullptr; 7430 } 7431 7432 return DeclsLoaded[Index]; 7433 } 7434 7435 Decl *ASTReader::GetDecl(DeclID ID) { 7436 if (ID < NUM_PREDEF_DECL_IDS) 7437 return GetExistingDecl(ID); 7438 7439 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7440 7441 if (Index >= DeclsLoaded.size()) { 7442 assert(0 && "declaration ID out-of-range for AST file"); 7443 Error("declaration ID out-of-range for AST file"); 7444 return nullptr; 7445 } 7446 7447 if (!DeclsLoaded[Index]) { 7448 ReadDeclRecord(ID); 7449 if (DeserializationListener) 7450 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); 7451 } 7452 7453 return DeclsLoaded[Index]; 7454 } 7455 7456 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M, 7457 DeclID GlobalID) { 7458 if (GlobalID < NUM_PREDEF_DECL_IDS) 7459 return GlobalID; 7460 7461 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID); 7462 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7463 ModuleFile *Owner = I->second; 7464 7465 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos 7466 = M.GlobalToLocalDeclIDs.find(Owner); 7467 if (Pos == M.GlobalToLocalDeclIDs.end()) 7468 return 0; 7469 7470 return GlobalID - Owner->BaseDeclID + Pos->second; 7471 } 7472 7473 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F, 7474 const RecordData &Record, 7475 unsigned &Idx) { 7476 if (Idx >= Record.size()) { 7477 Error("Corrupted AST file"); 7478 return 0; 7479 } 7480 7481 return getGlobalDeclID(F, Record[Idx++]); 7482 } 7483 7484 /// Resolve the offset of a statement into a statement. 7485 /// 7486 /// This operation will read a new statement from the external 7487 /// source each time it is called, and is meant to be used via a 7488 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 7489 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) { 7490 // Switch case IDs are per Decl. 7491 ClearSwitchCaseIDs(); 7492 7493 // Offset here is a global offset across the entire chain. 7494 RecordLocation Loc = getLocalBitOffset(Offset); 7495 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) { 7496 Error(std::move(Err)); 7497 return nullptr; 7498 } 7499 assert(NumCurrentElementsDeserializing == 0 && 7500 "should not be called while already deserializing"); 7501 Deserializing D(this); 7502 return ReadStmtFromStream(*Loc.F); 7503 } 7504 7505 void ASTReader::FindExternalLexicalDecls( 7506 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant, 7507 SmallVectorImpl<Decl *> &Decls) { 7508 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {}; 7509 7510 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) { 7511 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries"); 7512 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) { 7513 auto K = (Decl::Kind)+LexicalDecls[I]; 7514 if (!IsKindWeWant(K)) 7515 continue; 7516 7517 auto ID = (serialization::DeclID)+LexicalDecls[I + 1]; 7518 7519 // Don't add predefined declarations to the lexical context more 7520 // than once. 7521 if (ID < NUM_PREDEF_DECL_IDS) { 7522 if (PredefsVisited[ID]) 7523 continue; 7524 7525 PredefsVisited[ID] = true; 7526 } 7527 7528 if (Decl *D = GetLocalDecl(*M, ID)) { 7529 assert(D->getKind() == K && "wrong kind for lexical decl"); 7530 if (!DC->isDeclInLexicalTraversal(D)) 7531 Decls.push_back(D); 7532 } 7533 } 7534 }; 7535 7536 if (isa<TranslationUnitDecl>(DC)) { 7537 for (auto Lexical : TULexicalDecls) 7538 Visit(Lexical.first, Lexical.second); 7539 } else { 7540 auto I = LexicalDecls.find(DC); 7541 if (I != LexicalDecls.end()) 7542 Visit(I->second.first, I->second.second); 7543 } 7544 7545 ++NumLexicalDeclContextsRead; 7546 } 7547 7548 namespace { 7549 7550 class DeclIDComp { 7551 ASTReader &Reader; 7552 ModuleFile &Mod; 7553 7554 public: 7555 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {} 7556 7557 bool operator()(LocalDeclID L, LocalDeclID R) const { 7558 SourceLocation LHS = getLocation(L); 7559 SourceLocation RHS = getLocation(R); 7560 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7561 } 7562 7563 bool operator()(SourceLocation LHS, LocalDeclID R) const { 7564 SourceLocation RHS = getLocation(R); 7565 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7566 } 7567 7568 bool operator()(LocalDeclID L, SourceLocation RHS) const { 7569 SourceLocation LHS = getLocation(L); 7570 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7571 } 7572 7573 SourceLocation getLocation(LocalDeclID ID) const { 7574 return Reader.getSourceManager().getFileLoc( 7575 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID))); 7576 } 7577 }; 7578 7579 } // namespace 7580 7581 void ASTReader::FindFileRegionDecls(FileID File, 7582 unsigned Offset, unsigned Length, 7583 SmallVectorImpl<Decl *> &Decls) { 7584 SourceManager &SM = getSourceManager(); 7585 7586 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File); 7587 if (I == FileDeclIDs.end()) 7588 return; 7589 7590 FileDeclsInfo &DInfo = I->second; 7591 if (DInfo.Decls.empty()) 7592 return; 7593 7594 SourceLocation 7595 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset); 7596 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length); 7597 7598 DeclIDComp DIDComp(*this, *DInfo.Mod); 7599 ArrayRef<serialization::LocalDeclID>::iterator BeginIt = 7600 llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp); 7601 if (BeginIt != DInfo.Decls.begin()) 7602 --BeginIt; 7603 7604 // If we are pointing at a top-level decl inside an objc container, we need 7605 // to backtrack until we find it otherwise we will fail to report that the 7606 // region overlaps with an objc container. 7607 while (BeginIt != DInfo.Decls.begin() && 7608 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt)) 7609 ->isTopLevelDeclInObjCContainer()) 7610 --BeginIt; 7611 7612 ArrayRef<serialization::LocalDeclID>::iterator EndIt = 7613 llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp); 7614 if (EndIt != DInfo.Decls.end()) 7615 ++EndIt; 7616 7617 for (ArrayRef<serialization::LocalDeclID>::iterator 7618 DIt = BeginIt; DIt != EndIt; ++DIt) 7619 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt))); 7620 } 7621 7622 bool 7623 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, 7624 DeclarationName Name) { 7625 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() && 7626 "DeclContext has no visible decls in storage"); 7627 if (!Name) 7628 return false; 7629 7630 auto It = Lookups.find(DC); 7631 if (It == Lookups.end()) 7632 return false; 7633 7634 Deserializing LookupResults(this); 7635 7636 // Load the list of declarations. 7637 SmallVector<NamedDecl *, 64> Decls; 7638 for (DeclID ID : It->second.Table.find(Name)) { 7639 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7640 if (ND->getDeclName() == Name) 7641 Decls.push_back(ND); 7642 } 7643 7644 ++NumVisibleDeclContextsRead; 7645 SetExternalVisibleDeclsForName(DC, Name, Decls); 7646 return !Decls.empty(); 7647 } 7648 7649 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) { 7650 if (!DC->hasExternalVisibleStorage()) 7651 return; 7652 7653 auto It = Lookups.find(DC); 7654 assert(It != Lookups.end() && 7655 "have external visible storage but no lookup tables"); 7656 7657 DeclsMap Decls; 7658 7659 for (DeclID ID : It->second.Table.findAll()) { 7660 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7661 Decls[ND->getDeclName()].push_back(ND); 7662 } 7663 7664 ++NumVisibleDeclContextsRead; 7665 7666 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) { 7667 SetExternalVisibleDeclsForName(DC, I->first, I->second); 7668 } 7669 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false); 7670 } 7671 7672 const serialization::reader::DeclContextLookupTable * 7673 ASTReader::getLoadedLookupTables(DeclContext *Primary) const { 7674 auto I = Lookups.find(Primary); 7675 return I == Lookups.end() ? nullptr : &I->second; 7676 } 7677 7678 /// Under non-PCH compilation the consumer receives the objc methods 7679 /// before receiving the implementation, and codegen depends on this. 7680 /// We simulate this by deserializing and passing to consumer the methods of the 7681 /// implementation before passing the deserialized implementation decl. 7682 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD, 7683 ASTConsumer *Consumer) { 7684 assert(ImplD && Consumer); 7685 7686 for (auto *I : ImplD->methods()) 7687 Consumer->HandleInterestingDecl(DeclGroupRef(I)); 7688 7689 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD)); 7690 } 7691 7692 void ASTReader::PassInterestingDeclToConsumer(Decl *D) { 7693 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) 7694 PassObjCImplDeclToConsumer(ImplD, Consumer); 7695 else 7696 Consumer->HandleInterestingDecl(DeclGroupRef(D)); 7697 } 7698 7699 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) { 7700 this->Consumer = Consumer; 7701 7702 if (Consumer) 7703 PassInterestingDeclsToConsumer(); 7704 7705 if (DeserializationListener) 7706 DeserializationListener->ReaderInitialized(this); 7707 } 7708 7709 void ASTReader::PrintStats() { 7710 std::fprintf(stderr, "*** AST File Statistics:\n"); 7711 7712 unsigned NumTypesLoaded 7713 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), 7714 QualType()); 7715 unsigned NumDeclsLoaded 7716 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), 7717 (Decl *)nullptr); 7718 unsigned NumIdentifiersLoaded 7719 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), 7720 IdentifiersLoaded.end(), 7721 (IdentifierInfo *)nullptr); 7722 unsigned NumMacrosLoaded 7723 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(), 7724 MacrosLoaded.end(), 7725 (MacroInfo *)nullptr); 7726 unsigned NumSelectorsLoaded 7727 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), 7728 SelectorsLoaded.end(), 7729 Selector()); 7730 7731 if (unsigned TotalNumSLocEntries = getTotalNumSLocs()) 7732 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", 7733 NumSLocEntriesRead, TotalNumSLocEntries, 7734 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); 7735 if (!TypesLoaded.empty()) 7736 std::fprintf(stderr, " %u/%u types read (%f%%)\n", 7737 NumTypesLoaded, (unsigned)TypesLoaded.size(), 7738 ((float)NumTypesLoaded/TypesLoaded.size() * 100)); 7739 if (!DeclsLoaded.empty()) 7740 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", 7741 NumDeclsLoaded, (unsigned)DeclsLoaded.size(), 7742 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); 7743 if (!IdentifiersLoaded.empty()) 7744 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", 7745 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), 7746 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); 7747 if (!MacrosLoaded.empty()) 7748 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7749 NumMacrosLoaded, (unsigned)MacrosLoaded.size(), 7750 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100)); 7751 if (!SelectorsLoaded.empty()) 7752 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", 7753 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), 7754 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); 7755 if (TotalNumStatements) 7756 std::fprintf(stderr, " %u/%u statements read (%f%%)\n", 7757 NumStatementsRead, TotalNumStatements, 7758 ((float)NumStatementsRead/TotalNumStatements * 100)); 7759 if (TotalNumMacros) 7760 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7761 NumMacrosRead, TotalNumMacros, 7762 ((float)NumMacrosRead/TotalNumMacros * 100)); 7763 if (TotalLexicalDeclContexts) 7764 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", 7765 NumLexicalDeclContextsRead, TotalLexicalDeclContexts, 7766 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts 7767 * 100)); 7768 if (TotalVisibleDeclContexts) 7769 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", 7770 NumVisibleDeclContextsRead, TotalVisibleDeclContexts, 7771 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts 7772 * 100)); 7773 if (TotalNumMethodPoolEntries) 7774 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", 7775 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, 7776 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries 7777 * 100)); 7778 if (NumMethodPoolLookups) 7779 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n", 7780 NumMethodPoolHits, NumMethodPoolLookups, 7781 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0)); 7782 if (NumMethodPoolTableLookups) 7783 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n", 7784 NumMethodPoolTableHits, NumMethodPoolTableLookups, 7785 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups 7786 * 100.0)); 7787 if (NumIdentifierLookupHits) 7788 std::fprintf(stderr, 7789 " %u / %u identifier table lookups succeeded (%f%%)\n", 7790 NumIdentifierLookupHits, NumIdentifierLookups, 7791 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups); 7792 7793 if (GlobalIndex) { 7794 std::fprintf(stderr, "\n"); 7795 GlobalIndex->printStats(); 7796 } 7797 7798 std::fprintf(stderr, "\n"); 7799 dump(); 7800 std::fprintf(stderr, "\n"); 7801 } 7802 7803 template<typename Key, typename ModuleFile, unsigned InitialCapacity> 7804 LLVM_DUMP_METHOD static void 7805 dumpModuleIDMap(StringRef Name, 7806 const ContinuousRangeMap<Key, ModuleFile *, 7807 InitialCapacity> &Map) { 7808 if (Map.begin() == Map.end()) 7809 return; 7810 7811 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>; 7812 7813 llvm::errs() << Name << ":\n"; 7814 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end(); 7815 I != IEnd; ++I) { 7816 llvm::errs() << " " << I->first << " -> " << I->second->FileName 7817 << "\n"; 7818 } 7819 } 7820 7821 LLVM_DUMP_METHOD void ASTReader::dump() { 7822 llvm::errs() << "*** PCH/ModuleFile Remappings:\n"; 7823 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap); 7824 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap); 7825 dumpModuleIDMap("Global type map", GlobalTypeMap); 7826 dumpModuleIDMap("Global declaration map", GlobalDeclMap); 7827 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap); 7828 dumpModuleIDMap("Global macro map", GlobalMacroMap); 7829 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap); 7830 dumpModuleIDMap("Global selector map", GlobalSelectorMap); 7831 dumpModuleIDMap("Global preprocessed entity map", 7832 GlobalPreprocessedEntityMap); 7833 7834 llvm::errs() << "\n*** PCH/Modules Loaded:"; 7835 for (ModuleFile &M : ModuleMgr) 7836 M.dump(); 7837 } 7838 7839 /// Return the amount of memory used by memory buffers, breaking down 7840 /// by heap-backed versus mmap'ed memory. 7841 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const { 7842 for (ModuleFile &I : ModuleMgr) { 7843 if (llvm::MemoryBuffer *buf = I.Buffer) { 7844 size_t bytes = buf->getBufferSize(); 7845 switch (buf->getBufferKind()) { 7846 case llvm::MemoryBuffer::MemoryBuffer_Malloc: 7847 sizes.malloc_bytes += bytes; 7848 break; 7849 case llvm::MemoryBuffer::MemoryBuffer_MMap: 7850 sizes.mmap_bytes += bytes; 7851 break; 7852 } 7853 } 7854 } 7855 } 7856 7857 void ASTReader::InitializeSema(Sema &S) { 7858 SemaObj = &S; 7859 S.addExternalSource(this); 7860 7861 // Makes sure any declarations that were deserialized "too early" 7862 // still get added to the identifier's declaration chains. 7863 for (uint64_t ID : PreloadedDeclIDs) { 7864 NamedDecl *D = cast<NamedDecl>(GetDecl(ID)); 7865 pushExternalDeclIntoScope(D, D->getDeclName()); 7866 } 7867 PreloadedDeclIDs.clear(); 7868 7869 // FIXME: What happens if these are changed by a module import? 7870 if (!FPPragmaOptions.empty()) { 7871 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS"); 7872 FPOptionsOverride NewOverrides = 7873 FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]); 7874 SemaObj->CurFPFeatures = 7875 NewOverrides.applyOverrides(SemaObj->getLangOpts()); 7876 } 7877 7878 SemaObj->OpenCLFeatures = OpenCLExtensions; 7879 SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap; 7880 SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap; 7881 7882 UpdateSema(); 7883 } 7884 7885 void ASTReader::UpdateSema() { 7886 assert(SemaObj && "no Sema to update"); 7887 7888 // Load the offsets of the declarations that Sema references. 7889 // They will be lazily deserialized when needed. 7890 if (!SemaDeclRefs.empty()) { 7891 assert(SemaDeclRefs.size() % 3 == 0); 7892 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) { 7893 if (!SemaObj->StdNamespace) 7894 SemaObj->StdNamespace = SemaDeclRefs[I]; 7895 if (!SemaObj->StdBadAlloc) 7896 SemaObj->StdBadAlloc = SemaDeclRefs[I+1]; 7897 if (!SemaObj->StdAlignValT) 7898 SemaObj->StdAlignValT = SemaDeclRefs[I+2]; 7899 } 7900 SemaDeclRefs.clear(); 7901 } 7902 7903 // Update the state of pragmas. Use the same API as if we had encountered the 7904 // pragma in the source. 7905 if(OptimizeOffPragmaLocation.isValid()) 7906 SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation); 7907 if (PragmaMSStructState != -1) 7908 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState); 7909 if (PointersToMembersPragmaLocation.isValid()) { 7910 SemaObj->ActOnPragmaMSPointersToMembers( 7911 (LangOptions::PragmaMSPointersToMembersKind) 7912 PragmaMSPointersToMembersState, 7913 PointersToMembersPragmaLocation); 7914 } 7915 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth; 7916 7917 if (PragmaAlignPackCurrentValue) { 7918 // The bottom of the stack might have a default value. It must be adjusted 7919 // to the current value to ensure that the packing state is preserved after 7920 // popping entries that were included/imported from a PCH/module. 7921 bool DropFirst = false; 7922 if (!PragmaAlignPackStack.empty() && 7923 PragmaAlignPackStack.front().Location.isInvalid()) { 7924 assert(PragmaAlignPackStack.front().Value == 7925 SemaObj->AlignPackStack.DefaultValue && 7926 "Expected a default alignment value"); 7927 SemaObj->AlignPackStack.Stack.emplace_back( 7928 PragmaAlignPackStack.front().SlotLabel, 7929 SemaObj->AlignPackStack.CurrentValue, 7930 SemaObj->AlignPackStack.CurrentPragmaLocation, 7931 PragmaAlignPackStack.front().PushLocation); 7932 DropFirst = true; 7933 } 7934 for (const auto &Entry : llvm::makeArrayRef(PragmaAlignPackStack) 7935 .drop_front(DropFirst ? 1 : 0)) { 7936 SemaObj->AlignPackStack.Stack.emplace_back( 7937 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation); 7938 } 7939 if (PragmaAlignPackCurrentLocation.isInvalid()) { 7940 assert(*PragmaAlignPackCurrentValue == 7941 SemaObj->AlignPackStack.DefaultValue && 7942 "Expected a default align and pack value"); 7943 // Keep the current values. 7944 } else { 7945 SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue; 7946 SemaObj->AlignPackStack.CurrentPragmaLocation = 7947 PragmaAlignPackCurrentLocation; 7948 } 7949 } 7950 if (FpPragmaCurrentValue) { 7951 // The bottom of the stack might have a default value. It must be adjusted 7952 // to the current value to ensure that fp-pragma state is preserved after 7953 // popping entries that were included/imported from a PCH/module. 7954 bool DropFirst = false; 7955 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) { 7956 assert(FpPragmaStack.front().Value == 7957 SemaObj->FpPragmaStack.DefaultValue && 7958 "Expected a default pragma float_control value"); 7959 SemaObj->FpPragmaStack.Stack.emplace_back( 7960 FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue, 7961 SemaObj->FpPragmaStack.CurrentPragmaLocation, 7962 FpPragmaStack.front().PushLocation); 7963 DropFirst = true; 7964 } 7965 for (const auto &Entry : 7966 llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0)) 7967 SemaObj->FpPragmaStack.Stack.emplace_back( 7968 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation); 7969 if (FpPragmaCurrentLocation.isInvalid()) { 7970 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue && 7971 "Expected a default pragma float_control value"); 7972 // Keep the current values. 7973 } else { 7974 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue; 7975 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation; 7976 } 7977 } 7978 7979 // For non-modular AST files, restore visiblity of modules. 7980 for (auto &Import : ImportedModules) { 7981 if (Import.ImportLoc.isInvalid()) 7982 continue; 7983 if (Module *Imported = getSubmodule(Import.ID)) { 7984 SemaObj->makeModuleVisible(Imported, Import.ImportLoc); 7985 } 7986 } 7987 } 7988 7989 IdentifierInfo *ASTReader::get(StringRef Name) { 7990 // Note that we are loading an identifier. 7991 Deserializing AnIdentifier(this); 7992 7993 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0, 7994 NumIdentifierLookups, 7995 NumIdentifierLookupHits); 7996 7997 // We don't need to do identifier table lookups in C++ modules (we preload 7998 // all interesting declarations, and don't need to use the scope for name 7999 // lookups). Perform the lookup in PCH files, though, since we don't build 8000 // a complete initial identifier table if we're carrying on from a PCH. 8001 if (PP.getLangOpts().CPlusPlus) { 8002 for (auto F : ModuleMgr.pch_modules()) 8003 if (Visitor(*F)) 8004 break; 8005 } else { 8006 // If there is a global index, look there first to determine which modules 8007 // provably do not have any results for this identifier. 8008 GlobalModuleIndex::HitSet Hits; 8009 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 8010 if (!loadGlobalIndex()) { 8011 if (GlobalIndex->lookupIdentifier(Name, Hits)) { 8012 HitsPtr = &Hits; 8013 } 8014 } 8015 8016 ModuleMgr.visit(Visitor, HitsPtr); 8017 } 8018 8019 IdentifierInfo *II = Visitor.getIdentifierInfo(); 8020 markIdentifierUpToDate(II); 8021 return II; 8022 } 8023 8024 namespace clang { 8025 8026 /// An identifier-lookup iterator that enumerates all of the 8027 /// identifiers stored within a set of AST files. 8028 class ASTIdentifierIterator : public IdentifierIterator { 8029 /// The AST reader whose identifiers are being enumerated. 8030 const ASTReader &Reader; 8031 8032 /// The current index into the chain of AST files stored in 8033 /// the AST reader. 8034 unsigned Index; 8035 8036 /// The current position within the identifier lookup table 8037 /// of the current AST file. 8038 ASTIdentifierLookupTable::key_iterator Current; 8039 8040 /// The end position within the identifier lookup table of 8041 /// the current AST file. 8042 ASTIdentifierLookupTable::key_iterator End; 8043 8044 /// Whether to skip any modules in the ASTReader. 8045 bool SkipModules; 8046 8047 public: 8048 explicit ASTIdentifierIterator(const ASTReader &Reader, 8049 bool SkipModules = false); 8050 8051 StringRef Next() override; 8052 }; 8053 8054 } // namespace clang 8055 8056 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader, 8057 bool SkipModules) 8058 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) { 8059 } 8060 8061 StringRef ASTIdentifierIterator::Next() { 8062 while (Current == End) { 8063 // If we have exhausted all of our AST files, we're done. 8064 if (Index == 0) 8065 return StringRef(); 8066 8067 --Index; 8068 ModuleFile &F = Reader.ModuleMgr[Index]; 8069 if (SkipModules && F.isModule()) 8070 continue; 8071 8072 ASTIdentifierLookupTable *IdTable = 8073 (ASTIdentifierLookupTable *)F.IdentifierLookupTable; 8074 Current = IdTable->key_begin(); 8075 End = IdTable->key_end(); 8076 } 8077 8078 // We have any identifiers remaining in the current AST file; return 8079 // the next one. 8080 StringRef Result = *Current; 8081 ++Current; 8082 return Result; 8083 } 8084 8085 namespace { 8086 8087 /// A utility for appending two IdentifierIterators. 8088 class ChainedIdentifierIterator : public IdentifierIterator { 8089 std::unique_ptr<IdentifierIterator> Current; 8090 std::unique_ptr<IdentifierIterator> Queued; 8091 8092 public: 8093 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First, 8094 std::unique_ptr<IdentifierIterator> Second) 8095 : Current(std::move(First)), Queued(std::move(Second)) {} 8096 8097 StringRef Next() override { 8098 if (!Current) 8099 return StringRef(); 8100 8101 StringRef result = Current->Next(); 8102 if (!result.empty()) 8103 return result; 8104 8105 // Try the queued iterator, which may itself be empty. 8106 Current.reset(); 8107 std::swap(Current, Queued); 8108 return Next(); 8109 } 8110 }; 8111 8112 } // namespace 8113 8114 IdentifierIterator *ASTReader::getIdentifiers() { 8115 if (!loadGlobalIndex()) { 8116 std::unique_ptr<IdentifierIterator> ReaderIter( 8117 new ASTIdentifierIterator(*this, /*SkipModules=*/true)); 8118 std::unique_ptr<IdentifierIterator> ModulesIter( 8119 GlobalIndex->createIdentifierIterator()); 8120 return new ChainedIdentifierIterator(std::move(ReaderIter), 8121 std::move(ModulesIter)); 8122 } 8123 8124 return new ASTIdentifierIterator(*this); 8125 } 8126 8127 namespace clang { 8128 namespace serialization { 8129 8130 class ReadMethodPoolVisitor { 8131 ASTReader &Reader; 8132 Selector Sel; 8133 unsigned PriorGeneration; 8134 unsigned InstanceBits = 0; 8135 unsigned FactoryBits = 0; 8136 bool InstanceHasMoreThanOneDecl = false; 8137 bool FactoryHasMoreThanOneDecl = false; 8138 SmallVector<ObjCMethodDecl *, 4> InstanceMethods; 8139 SmallVector<ObjCMethodDecl *, 4> FactoryMethods; 8140 8141 public: 8142 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel, 8143 unsigned PriorGeneration) 8144 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {} 8145 8146 bool operator()(ModuleFile &M) { 8147 if (!M.SelectorLookupTable) 8148 return false; 8149 8150 // If we've already searched this module file, skip it now. 8151 if (M.Generation <= PriorGeneration) 8152 return true; 8153 8154 ++Reader.NumMethodPoolTableLookups; 8155 ASTSelectorLookupTable *PoolTable 8156 = (ASTSelectorLookupTable*)M.SelectorLookupTable; 8157 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel); 8158 if (Pos == PoolTable->end()) 8159 return false; 8160 8161 ++Reader.NumMethodPoolTableHits; 8162 ++Reader.NumSelectorsRead; 8163 // FIXME: Not quite happy with the statistics here. We probably should 8164 // disable this tracking when called via LoadSelector. 8165 // Also, should entries without methods count as misses? 8166 ++Reader.NumMethodPoolEntriesRead; 8167 ASTSelectorLookupTrait::data_type Data = *Pos; 8168 if (Reader.DeserializationListener) 8169 Reader.DeserializationListener->SelectorRead(Data.ID, Sel); 8170 8171 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end()); 8172 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end()); 8173 InstanceBits = Data.InstanceBits; 8174 FactoryBits = Data.FactoryBits; 8175 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl; 8176 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl; 8177 return true; 8178 } 8179 8180 /// Retrieve the instance methods found by this visitor. 8181 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const { 8182 return InstanceMethods; 8183 } 8184 8185 /// Retrieve the instance methods found by this visitor. 8186 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const { 8187 return FactoryMethods; 8188 } 8189 8190 unsigned getInstanceBits() const { return InstanceBits; } 8191 unsigned getFactoryBits() const { return FactoryBits; } 8192 8193 bool instanceHasMoreThanOneDecl() const { 8194 return InstanceHasMoreThanOneDecl; 8195 } 8196 8197 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; } 8198 }; 8199 8200 } // namespace serialization 8201 } // namespace clang 8202 8203 /// Add the given set of methods to the method list. 8204 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods, 8205 ObjCMethodList &List) { 8206 for (unsigned I = 0, N = Methods.size(); I != N; ++I) { 8207 S.addMethodToGlobalList(&List, Methods[I]); 8208 } 8209 } 8210 8211 void ASTReader::ReadMethodPool(Selector Sel) { 8212 // Get the selector generation and update it to the current generation. 8213 unsigned &Generation = SelectorGeneration[Sel]; 8214 unsigned PriorGeneration = Generation; 8215 Generation = getGeneration(); 8216 SelectorOutOfDate[Sel] = false; 8217 8218 // Search for methods defined with this selector. 8219 ++NumMethodPoolLookups; 8220 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration); 8221 ModuleMgr.visit(Visitor); 8222 8223 if (Visitor.getInstanceMethods().empty() && 8224 Visitor.getFactoryMethods().empty()) 8225 return; 8226 8227 ++NumMethodPoolHits; 8228 8229 if (!getSema()) 8230 return; 8231 8232 Sema &S = *getSema(); 8233 Sema::GlobalMethodPool::iterator Pos 8234 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first; 8235 8236 Pos->second.first.setBits(Visitor.getInstanceBits()); 8237 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl()); 8238 Pos->second.second.setBits(Visitor.getFactoryBits()); 8239 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl()); 8240 8241 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since 8242 // when building a module we keep every method individually and may need to 8243 // update hasMoreThanOneDecl as we add the methods. 8244 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first); 8245 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second); 8246 } 8247 8248 void ASTReader::updateOutOfDateSelector(Selector Sel) { 8249 if (SelectorOutOfDate[Sel]) 8250 ReadMethodPool(Sel); 8251 } 8252 8253 void ASTReader::ReadKnownNamespaces( 8254 SmallVectorImpl<NamespaceDecl *> &Namespaces) { 8255 Namespaces.clear(); 8256 8257 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) { 8258 if (NamespaceDecl *Namespace 8259 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I]))) 8260 Namespaces.push_back(Namespace); 8261 } 8262 } 8263 8264 void ASTReader::ReadUndefinedButUsed( 8265 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) { 8266 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) { 8267 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++])); 8268 SourceLocation Loc = 8269 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]); 8270 Undefined.insert(std::make_pair(D, Loc)); 8271 } 8272 } 8273 8274 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector< 8275 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> & 8276 Exprs) { 8277 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) { 8278 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++])); 8279 uint64_t Count = DelayedDeleteExprs[Idx++]; 8280 for (uint64_t C = 0; C < Count; ++C) { 8281 SourceLocation DeleteLoc = 8282 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]); 8283 const bool IsArrayForm = DelayedDeleteExprs[Idx++]; 8284 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm)); 8285 } 8286 } 8287 } 8288 8289 void ASTReader::ReadTentativeDefinitions( 8290 SmallVectorImpl<VarDecl *> &TentativeDefs) { 8291 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { 8292 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I])); 8293 if (Var) 8294 TentativeDefs.push_back(Var); 8295 } 8296 TentativeDefinitions.clear(); 8297 } 8298 8299 void ASTReader::ReadUnusedFileScopedDecls( 8300 SmallVectorImpl<const DeclaratorDecl *> &Decls) { 8301 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) { 8302 DeclaratorDecl *D 8303 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I])); 8304 if (D) 8305 Decls.push_back(D); 8306 } 8307 UnusedFileScopedDecls.clear(); 8308 } 8309 8310 void ASTReader::ReadDelegatingConstructors( 8311 SmallVectorImpl<CXXConstructorDecl *> &Decls) { 8312 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) { 8313 CXXConstructorDecl *D 8314 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I])); 8315 if (D) 8316 Decls.push_back(D); 8317 } 8318 DelegatingCtorDecls.clear(); 8319 } 8320 8321 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) { 8322 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) { 8323 TypedefNameDecl *D 8324 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])); 8325 if (D) 8326 Decls.push_back(D); 8327 } 8328 ExtVectorDecls.clear(); 8329 } 8330 8331 void ASTReader::ReadUnusedLocalTypedefNameCandidates( 8332 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) { 8333 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N; 8334 ++I) { 8335 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>( 8336 GetDecl(UnusedLocalTypedefNameCandidates[I])); 8337 if (D) 8338 Decls.insert(D); 8339 } 8340 UnusedLocalTypedefNameCandidates.clear(); 8341 } 8342 8343 void ASTReader::ReadDeclsToCheckForDeferredDiags( 8344 llvm::SmallVector<Decl *, 4> &Decls) { 8345 for (unsigned I = 0, N = DeclsToCheckForDeferredDiags.size(); I != N; 8346 ++I) { 8347 auto *D = dyn_cast_or_null<Decl>( 8348 GetDecl(DeclsToCheckForDeferredDiags[I])); 8349 if (D) 8350 Decls.push_back(D); 8351 } 8352 DeclsToCheckForDeferredDiags.clear(); 8353 } 8354 8355 8356 void ASTReader::ReadReferencedSelectors( 8357 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) { 8358 if (ReferencedSelectorsData.empty()) 8359 return; 8360 8361 // If there are @selector references added them to its pool. This is for 8362 // implementation of -Wselector. 8363 unsigned int DataSize = ReferencedSelectorsData.size()-1; 8364 unsigned I = 0; 8365 while (I < DataSize) { 8366 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]); 8367 SourceLocation SelLoc 8368 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]); 8369 Sels.push_back(std::make_pair(Sel, SelLoc)); 8370 } 8371 ReferencedSelectorsData.clear(); 8372 } 8373 8374 void ASTReader::ReadWeakUndeclaredIdentifiers( 8375 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) { 8376 if (WeakUndeclaredIdentifiers.empty()) 8377 return; 8378 8379 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) { 8380 IdentifierInfo *WeakId 8381 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8382 IdentifierInfo *AliasId 8383 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8384 SourceLocation Loc 8385 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]); 8386 bool Used = WeakUndeclaredIdentifiers[I++]; 8387 WeakInfo WI(AliasId, Loc); 8388 WI.setUsed(Used); 8389 WeakIDs.push_back(std::make_pair(WeakId, WI)); 8390 } 8391 WeakUndeclaredIdentifiers.clear(); 8392 } 8393 8394 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) { 8395 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) { 8396 ExternalVTableUse VT; 8397 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); 8398 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]); 8399 VT.DefinitionRequired = VTableUses[Idx++]; 8400 VTables.push_back(VT); 8401 } 8402 8403 VTableUses.clear(); 8404 } 8405 8406 void ASTReader::ReadPendingInstantiations( 8407 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) { 8408 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) { 8409 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++])); 8410 SourceLocation Loc 8411 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]); 8412 8413 Pending.push_back(std::make_pair(D, Loc)); 8414 } 8415 PendingInstantiations.clear(); 8416 } 8417 8418 void ASTReader::ReadLateParsedTemplates( 8419 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>> 8420 &LPTMap) { 8421 for (auto &LPT : LateParsedTemplates) { 8422 ModuleFile *FMod = LPT.first; 8423 RecordDataImpl &LateParsed = LPT.second; 8424 for (unsigned Idx = 0, N = LateParsed.size(); Idx < N; 8425 /* In loop */) { 8426 FunctionDecl *FD = 8427 cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++])); 8428 8429 auto LT = std::make_unique<LateParsedTemplate>(); 8430 LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]); 8431 8432 ModuleFile *F = getOwningModuleFile(LT->D); 8433 assert(F && "No module"); 8434 8435 unsigned TokN = LateParsed[Idx++]; 8436 LT->Toks.reserve(TokN); 8437 for (unsigned T = 0; T < TokN; ++T) 8438 LT->Toks.push_back(ReadToken(*F, LateParsed, Idx)); 8439 8440 LPTMap.insert(std::make_pair(FD, std::move(LT))); 8441 } 8442 } 8443 } 8444 8445 void ASTReader::LoadSelector(Selector Sel) { 8446 // It would be complicated to avoid reading the methods anyway. So don't. 8447 ReadMethodPool(Sel); 8448 } 8449 8450 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) { 8451 assert(ID && "Non-zero identifier ID required"); 8452 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); 8453 IdentifiersLoaded[ID - 1] = II; 8454 if (DeserializationListener) 8455 DeserializationListener->IdentifierRead(ID, II); 8456 } 8457 8458 /// Set the globally-visible declarations associated with the given 8459 /// identifier. 8460 /// 8461 /// If the AST reader is currently in a state where the given declaration IDs 8462 /// cannot safely be resolved, they are queued until it is safe to resolve 8463 /// them. 8464 /// 8465 /// \param II an IdentifierInfo that refers to one or more globally-visible 8466 /// declarations. 8467 /// 8468 /// \param DeclIDs the set of declaration IDs with the name @p II that are 8469 /// visible at global scope. 8470 /// 8471 /// \param Decls if non-null, this vector will be populated with the set of 8472 /// deserialized declarations. These declarations will not be pushed into 8473 /// scope. 8474 void 8475 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, 8476 const SmallVectorImpl<uint32_t> &DeclIDs, 8477 SmallVectorImpl<Decl *> *Decls) { 8478 if (NumCurrentElementsDeserializing && !Decls) { 8479 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end()); 8480 return; 8481 } 8482 8483 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { 8484 if (!SemaObj) { 8485 // Queue this declaration so that it will be added to the 8486 // translation unit scope and identifier's declaration chain 8487 // once a Sema object is known. 8488 PreloadedDeclIDs.push_back(DeclIDs[I]); 8489 continue; 8490 } 8491 8492 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); 8493 8494 // If we're simply supposed to record the declarations, do so now. 8495 if (Decls) { 8496 Decls->push_back(D); 8497 continue; 8498 } 8499 8500 // Introduce this declaration into the translation-unit scope 8501 // and add it to the declaration chain for this identifier, so 8502 // that (unqualified) name lookup will find it. 8503 pushExternalDeclIntoScope(D, II); 8504 } 8505 } 8506 8507 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) { 8508 if (ID == 0) 8509 return nullptr; 8510 8511 if (IdentifiersLoaded.empty()) { 8512 Error("no identifier table in AST file"); 8513 return nullptr; 8514 } 8515 8516 ID -= 1; 8517 if (!IdentifiersLoaded[ID]) { 8518 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1); 8519 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map"); 8520 ModuleFile *M = I->second; 8521 unsigned Index = ID - M->BaseIdentifierID; 8522 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index]; 8523 8524 // All of the strings in the AST file are preceded by a 16-bit length. 8525 // Extract that 16-bit length to avoid having to execute strlen(). 8526 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as 8527 // unsigned integers. This is important to avoid integer overflow when 8528 // we cast them to 'unsigned'. 8529 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2; 8530 unsigned StrLen = (((unsigned) StrLenPtr[0]) 8531 | (((unsigned) StrLenPtr[1]) << 8)) - 1; 8532 auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen)); 8533 IdentifiersLoaded[ID] = &II; 8534 markIdentifierFromAST(*this, II); 8535 if (DeserializationListener) 8536 DeserializationListener->IdentifierRead(ID + 1, &II); 8537 } 8538 8539 return IdentifiersLoaded[ID]; 8540 } 8541 8542 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) { 8543 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID)); 8544 } 8545 8546 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) { 8547 if (LocalID < NUM_PREDEF_IDENT_IDS) 8548 return LocalID; 8549 8550 if (!M.ModuleOffsetMap.empty()) 8551 ReadModuleOffsetMap(M); 8552 8553 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8554 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS); 8555 assert(I != M.IdentifierRemap.end() 8556 && "Invalid index into identifier index remap"); 8557 8558 return LocalID + I->second; 8559 } 8560 8561 MacroInfo *ASTReader::getMacro(MacroID ID) { 8562 if (ID == 0) 8563 return nullptr; 8564 8565 if (MacrosLoaded.empty()) { 8566 Error("no macro table in AST file"); 8567 return nullptr; 8568 } 8569 8570 ID -= NUM_PREDEF_MACRO_IDS; 8571 if (!MacrosLoaded[ID]) { 8572 GlobalMacroMapType::iterator I 8573 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS); 8574 assert(I != GlobalMacroMap.end() && "Corrupted global macro map"); 8575 ModuleFile *M = I->second; 8576 unsigned Index = ID - M->BaseMacroID; 8577 MacrosLoaded[ID] = 8578 ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]); 8579 8580 if (DeserializationListener) 8581 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS, 8582 MacrosLoaded[ID]); 8583 } 8584 8585 return MacrosLoaded[ID]; 8586 } 8587 8588 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) { 8589 if (LocalID < NUM_PREDEF_MACRO_IDS) 8590 return LocalID; 8591 8592 if (!M.ModuleOffsetMap.empty()) 8593 ReadModuleOffsetMap(M); 8594 8595 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8596 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS); 8597 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap"); 8598 8599 return LocalID + I->second; 8600 } 8601 8602 serialization::SubmoduleID 8603 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) { 8604 if (LocalID < NUM_PREDEF_SUBMODULE_IDS) 8605 return LocalID; 8606 8607 if (!M.ModuleOffsetMap.empty()) 8608 ReadModuleOffsetMap(M); 8609 8610 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8611 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS); 8612 assert(I != M.SubmoduleRemap.end() 8613 && "Invalid index into submodule index remap"); 8614 8615 return LocalID + I->second; 8616 } 8617 8618 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) { 8619 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) { 8620 assert(GlobalID == 0 && "Unhandled global submodule ID"); 8621 return nullptr; 8622 } 8623 8624 if (GlobalID > SubmodulesLoaded.size()) { 8625 Error("submodule ID out of range in AST file"); 8626 return nullptr; 8627 } 8628 8629 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS]; 8630 } 8631 8632 Module *ASTReader::getModule(unsigned ID) { 8633 return getSubmodule(ID); 8634 } 8635 8636 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) { 8637 if (ID & 1) { 8638 // It's a module, look it up by submodule ID. 8639 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1)); 8640 return I == GlobalSubmoduleMap.end() ? nullptr : I->second; 8641 } else { 8642 // It's a prefix (preamble, PCH, ...). Look it up by index. 8643 unsigned IndexFromEnd = ID >> 1; 8644 assert(IndexFromEnd && "got reference to unknown module file"); 8645 return getModuleManager().pch_modules().end()[-IndexFromEnd]; 8646 } 8647 } 8648 8649 unsigned ASTReader::getModuleFileID(ModuleFile *F) { 8650 if (!F) 8651 return 1; 8652 8653 // For a file representing a module, use the submodule ID of the top-level 8654 // module as the file ID. For any other kind of file, the number of such 8655 // files loaded beforehand will be the same on reload. 8656 // FIXME: Is this true even if we have an explicit module file and a PCH? 8657 if (F->isModule()) 8658 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1; 8659 8660 auto PCHModules = getModuleManager().pch_modules(); 8661 auto I = llvm::find(PCHModules, F); 8662 assert(I != PCHModules.end() && "emitting reference to unknown file"); 8663 return (I - PCHModules.end()) << 1; 8664 } 8665 8666 llvm::Optional<ASTSourceDescriptor> 8667 ASTReader::getSourceDescriptor(unsigned ID) { 8668 if (Module *M = getSubmodule(ID)) 8669 return ASTSourceDescriptor(*M); 8670 8671 // If there is only a single PCH, return it instead. 8672 // Chained PCH are not supported. 8673 const auto &PCHChain = ModuleMgr.pch_modules(); 8674 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) { 8675 ModuleFile &MF = ModuleMgr.getPrimaryModule(); 8676 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName); 8677 StringRef FileName = llvm::sys::path::filename(MF.FileName); 8678 return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName, 8679 MF.Signature); 8680 } 8681 return None; 8682 } 8683 8684 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) { 8685 auto I = DefinitionSource.find(FD); 8686 if (I == DefinitionSource.end()) 8687 return EK_ReplyHazy; 8688 return I->second ? EK_Never : EK_Always; 8689 } 8690 8691 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) { 8692 return DecodeSelector(getGlobalSelectorID(M, LocalID)); 8693 } 8694 8695 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) { 8696 if (ID == 0) 8697 return Selector(); 8698 8699 if (ID > SelectorsLoaded.size()) { 8700 Error("selector ID out of range in AST file"); 8701 return Selector(); 8702 } 8703 8704 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) { 8705 // Load this selector from the selector table. 8706 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID); 8707 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map"); 8708 ModuleFile &M = *I->second; 8709 ASTSelectorLookupTrait Trait(*this, M); 8710 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS; 8711 SelectorsLoaded[ID - 1] = 8712 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0); 8713 if (DeserializationListener) 8714 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 8715 } 8716 8717 return SelectorsLoaded[ID - 1]; 8718 } 8719 8720 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) { 8721 return DecodeSelector(ID); 8722 } 8723 8724 uint32_t ASTReader::GetNumExternalSelectors() { 8725 // ID 0 (the null selector) is considered an external selector. 8726 return getTotalNumSelectors() + 1; 8727 } 8728 8729 serialization::SelectorID 8730 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const { 8731 if (LocalID < NUM_PREDEF_SELECTOR_IDS) 8732 return LocalID; 8733 8734 if (!M.ModuleOffsetMap.empty()) 8735 ReadModuleOffsetMap(M); 8736 8737 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8738 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS); 8739 assert(I != M.SelectorRemap.end() 8740 && "Invalid index into selector index remap"); 8741 8742 return LocalID + I->second; 8743 } 8744 8745 DeclarationNameLoc 8746 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) { 8747 switch (Name.getNameKind()) { 8748 case DeclarationName::CXXConstructorName: 8749 case DeclarationName::CXXDestructorName: 8750 case DeclarationName::CXXConversionFunctionName: 8751 return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo()); 8752 8753 case DeclarationName::CXXOperatorName: 8754 return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange()); 8755 8756 case DeclarationName::CXXLiteralOperatorName: 8757 return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc( 8758 readSourceLocation()); 8759 8760 case DeclarationName::Identifier: 8761 case DeclarationName::ObjCZeroArgSelector: 8762 case DeclarationName::ObjCOneArgSelector: 8763 case DeclarationName::ObjCMultiArgSelector: 8764 case DeclarationName::CXXUsingDirective: 8765 case DeclarationName::CXXDeductionGuideName: 8766 break; 8767 } 8768 return DeclarationNameLoc(); 8769 } 8770 8771 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() { 8772 DeclarationNameInfo NameInfo; 8773 NameInfo.setName(readDeclarationName()); 8774 NameInfo.setLoc(readSourceLocation()); 8775 NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName())); 8776 return NameInfo; 8777 } 8778 8779 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) { 8780 Info.QualifierLoc = readNestedNameSpecifierLoc(); 8781 unsigned NumTPLists = readInt(); 8782 Info.NumTemplParamLists = NumTPLists; 8783 if (NumTPLists) { 8784 Info.TemplParamLists = 8785 new (getContext()) TemplateParameterList *[NumTPLists]; 8786 for (unsigned i = 0; i != NumTPLists; ++i) 8787 Info.TemplParamLists[i] = readTemplateParameterList(); 8788 } 8789 } 8790 8791 TemplateParameterList * 8792 ASTRecordReader::readTemplateParameterList() { 8793 SourceLocation TemplateLoc = readSourceLocation(); 8794 SourceLocation LAngleLoc = readSourceLocation(); 8795 SourceLocation RAngleLoc = readSourceLocation(); 8796 8797 unsigned NumParams = readInt(); 8798 SmallVector<NamedDecl *, 16> Params; 8799 Params.reserve(NumParams); 8800 while (NumParams--) 8801 Params.push_back(readDeclAs<NamedDecl>()); 8802 8803 bool HasRequiresClause = readBool(); 8804 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr; 8805 8806 TemplateParameterList *TemplateParams = TemplateParameterList::Create( 8807 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause); 8808 return TemplateParams; 8809 } 8810 8811 void ASTRecordReader::readTemplateArgumentList( 8812 SmallVectorImpl<TemplateArgument> &TemplArgs, 8813 bool Canonicalize) { 8814 unsigned NumTemplateArgs = readInt(); 8815 TemplArgs.reserve(NumTemplateArgs); 8816 while (NumTemplateArgs--) 8817 TemplArgs.push_back(readTemplateArgument(Canonicalize)); 8818 } 8819 8820 /// Read a UnresolvedSet structure. 8821 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) { 8822 unsigned NumDecls = readInt(); 8823 Set.reserve(getContext(), NumDecls); 8824 while (NumDecls--) { 8825 DeclID ID = readDeclID(); 8826 AccessSpecifier AS = (AccessSpecifier) readInt(); 8827 Set.addLazyDecl(getContext(), ID, AS); 8828 } 8829 } 8830 8831 CXXBaseSpecifier 8832 ASTRecordReader::readCXXBaseSpecifier() { 8833 bool isVirtual = readBool(); 8834 bool isBaseOfClass = readBool(); 8835 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt()); 8836 bool inheritConstructors = readBool(); 8837 TypeSourceInfo *TInfo = readTypeSourceInfo(); 8838 SourceRange Range = readSourceRange(); 8839 SourceLocation EllipsisLoc = readSourceLocation(); 8840 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo, 8841 EllipsisLoc); 8842 Result.setInheritConstructors(inheritConstructors); 8843 return Result; 8844 } 8845 8846 CXXCtorInitializer ** 8847 ASTRecordReader::readCXXCtorInitializers() { 8848 ASTContext &Context = getContext(); 8849 unsigned NumInitializers = readInt(); 8850 assert(NumInitializers && "wrote ctor initializers but have no inits"); 8851 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers]; 8852 for (unsigned i = 0; i != NumInitializers; ++i) { 8853 TypeSourceInfo *TInfo = nullptr; 8854 bool IsBaseVirtual = false; 8855 FieldDecl *Member = nullptr; 8856 IndirectFieldDecl *IndirectMember = nullptr; 8857 8858 CtorInitializerType Type = (CtorInitializerType) readInt(); 8859 switch (Type) { 8860 case CTOR_INITIALIZER_BASE: 8861 TInfo = readTypeSourceInfo(); 8862 IsBaseVirtual = readBool(); 8863 break; 8864 8865 case CTOR_INITIALIZER_DELEGATING: 8866 TInfo = readTypeSourceInfo(); 8867 break; 8868 8869 case CTOR_INITIALIZER_MEMBER: 8870 Member = readDeclAs<FieldDecl>(); 8871 break; 8872 8873 case CTOR_INITIALIZER_INDIRECT_MEMBER: 8874 IndirectMember = readDeclAs<IndirectFieldDecl>(); 8875 break; 8876 } 8877 8878 SourceLocation MemberOrEllipsisLoc = readSourceLocation(); 8879 Expr *Init = readExpr(); 8880 SourceLocation LParenLoc = readSourceLocation(); 8881 SourceLocation RParenLoc = readSourceLocation(); 8882 8883 CXXCtorInitializer *BOMInit; 8884 if (Type == CTOR_INITIALIZER_BASE) 8885 BOMInit = new (Context) 8886 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init, 8887 RParenLoc, MemberOrEllipsisLoc); 8888 else if (Type == CTOR_INITIALIZER_DELEGATING) 8889 BOMInit = new (Context) 8890 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc); 8891 else if (Member) 8892 BOMInit = new (Context) 8893 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc, 8894 Init, RParenLoc); 8895 else 8896 BOMInit = new (Context) 8897 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc, 8898 LParenLoc, Init, RParenLoc); 8899 8900 if (/*IsWritten*/readBool()) { 8901 unsigned SourceOrder = readInt(); 8902 BOMInit->setSourceOrder(SourceOrder); 8903 } 8904 8905 CtorInitializers[i] = BOMInit; 8906 } 8907 8908 return CtorInitializers; 8909 } 8910 8911 NestedNameSpecifierLoc 8912 ASTRecordReader::readNestedNameSpecifierLoc() { 8913 ASTContext &Context = getContext(); 8914 unsigned N = readInt(); 8915 NestedNameSpecifierLocBuilder Builder; 8916 for (unsigned I = 0; I != N; ++I) { 8917 auto Kind = readNestedNameSpecifierKind(); 8918 switch (Kind) { 8919 case NestedNameSpecifier::Identifier: { 8920 IdentifierInfo *II = readIdentifier(); 8921 SourceRange Range = readSourceRange(); 8922 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd()); 8923 break; 8924 } 8925 8926 case NestedNameSpecifier::Namespace: { 8927 NamespaceDecl *NS = readDeclAs<NamespaceDecl>(); 8928 SourceRange Range = readSourceRange(); 8929 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd()); 8930 break; 8931 } 8932 8933 case NestedNameSpecifier::NamespaceAlias: { 8934 NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>(); 8935 SourceRange Range = readSourceRange(); 8936 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd()); 8937 break; 8938 } 8939 8940 case NestedNameSpecifier::TypeSpec: 8941 case NestedNameSpecifier::TypeSpecWithTemplate: { 8942 bool Template = readBool(); 8943 TypeSourceInfo *T = readTypeSourceInfo(); 8944 if (!T) 8945 return NestedNameSpecifierLoc(); 8946 SourceLocation ColonColonLoc = readSourceLocation(); 8947 8948 // FIXME: 'template' keyword location not saved anywhere, so we fake it. 8949 Builder.Extend(Context, 8950 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(), 8951 T->getTypeLoc(), ColonColonLoc); 8952 break; 8953 } 8954 8955 case NestedNameSpecifier::Global: { 8956 SourceLocation ColonColonLoc = readSourceLocation(); 8957 Builder.MakeGlobal(Context, ColonColonLoc); 8958 break; 8959 } 8960 8961 case NestedNameSpecifier::Super: { 8962 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>(); 8963 SourceRange Range = readSourceRange(); 8964 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd()); 8965 break; 8966 } 8967 } 8968 } 8969 8970 return Builder.getWithLocInContext(Context); 8971 } 8972 8973 SourceRange 8974 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record, 8975 unsigned &Idx) { 8976 SourceLocation beg = ReadSourceLocation(F, Record, Idx); 8977 SourceLocation end = ReadSourceLocation(F, Record, Idx); 8978 return SourceRange(beg, end); 8979 } 8980 8981 /// Read a floating-point value 8982 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) { 8983 return llvm::APFloat(Sem, readAPInt()); 8984 } 8985 8986 // Read a string 8987 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) { 8988 unsigned Len = Record[Idx++]; 8989 std::string Result(Record.data() + Idx, Record.data() + Idx + Len); 8990 Idx += Len; 8991 return Result; 8992 } 8993 8994 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record, 8995 unsigned &Idx) { 8996 std::string Filename = ReadString(Record, Idx); 8997 ResolveImportedPath(F, Filename); 8998 return Filename; 8999 } 9000 9001 std::string ASTReader::ReadPath(StringRef BaseDirectory, 9002 const RecordData &Record, unsigned &Idx) { 9003 std::string Filename = ReadString(Record, Idx); 9004 if (!BaseDirectory.empty()) 9005 ResolveImportedPath(Filename, BaseDirectory); 9006 return Filename; 9007 } 9008 9009 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record, 9010 unsigned &Idx) { 9011 unsigned Major = Record[Idx++]; 9012 unsigned Minor = Record[Idx++]; 9013 unsigned Subminor = Record[Idx++]; 9014 if (Minor == 0) 9015 return VersionTuple(Major); 9016 if (Subminor == 0) 9017 return VersionTuple(Major, Minor - 1); 9018 return VersionTuple(Major, Minor - 1, Subminor - 1); 9019 } 9020 9021 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F, 9022 const RecordData &Record, 9023 unsigned &Idx) { 9024 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx); 9025 return CXXTemporary::Create(getContext(), Decl); 9026 } 9027 9028 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const { 9029 return Diag(CurrentImportLoc, DiagID); 9030 } 9031 9032 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const { 9033 return Diags.Report(Loc, DiagID); 9034 } 9035 9036 /// Retrieve the identifier table associated with the 9037 /// preprocessor. 9038 IdentifierTable &ASTReader::getIdentifierTable() { 9039 return PP.getIdentifierTable(); 9040 } 9041 9042 /// Record that the given ID maps to the given switch-case 9043 /// statement. 9044 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { 9045 assert((*CurrSwitchCaseStmts)[ID] == nullptr && 9046 "Already have a SwitchCase with this ID"); 9047 (*CurrSwitchCaseStmts)[ID] = SC; 9048 } 9049 9050 /// Retrieve the switch-case statement with the given ID. 9051 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) { 9052 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID"); 9053 return (*CurrSwitchCaseStmts)[ID]; 9054 } 9055 9056 void ASTReader::ClearSwitchCaseIDs() { 9057 CurrSwitchCaseStmts->clear(); 9058 } 9059 9060 void ASTReader::ReadComments() { 9061 ASTContext &Context = getContext(); 9062 std::vector<RawComment *> Comments; 9063 for (SmallVectorImpl<std::pair<BitstreamCursor, 9064 serialization::ModuleFile *>>::iterator 9065 I = CommentsCursors.begin(), 9066 E = CommentsCursors.end(); 9067 I != E; ++I) { 9068 Comments.clear(); 9069 BitstreamCursor &Cursor = I->first; 9070 serialization::ModuleFile &F = *I->second; 9071 SavedStreamPosition SavedPosition(Cursor); 9072 9073 RecordData Record; 9074 while (true) { 9075 Expected<llvm::BitstreamEntry> MaybeEntry = 9076 Cursor.advanceSkippingSubblocks( 9077 BitstreamCursor::AF_DontPopBlockAtEnd); 9078 if (!MaybeEntry) { 9079 Error(MaybeEntry.takeError()); 9080 return; 9081 } 9082 llvm::BitstreamEntry Entry = MaybeEntry.get(); 9083 9084 switch (Entry.Kind) { 9085 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 9086 case llvm::BitstreamEntry::Error: 9087 Error("malformed block record in AST file"); 9088 return; 9089 case llvm::BitstreamEntry::EndBlock: 9090 goto NextCursor; 9091 case llvm::BitstreamEntry::Record: 9092 // The interesting case. 9093 break; 9094 } 9095 9096 // Read a record. 9097 Record.clear(); 9098 Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record); 9099 if (!MaybeComment) { 9100 Error(MaybeComment.takeError()); 9101 return; 9102 } 9103 switch ((CommentRecordTypes)MaybeComment.get()) { 9104 case COMMENTS_RAW_COMMENT: { 9105 unsigned Idx = 0; 9106 SourceRange SR = ReadSourceRange(F, Record, Idx); 9107 RawComment::CommentKind Kind = 9108 (RawComment::CommentKind) Record[Idx++]; 9109 bool IsTrailingComment = Record[Idx++]; 9110 bool IsAlmostTrailingComment = Record[Idx++]; 9111 Comments.push_back(new (Context) RawComment( 9112 SR, Kind, IsTrailingComment, IsAlmostTrailingComment)); 9113 break; 9114 } 9115 } 9116 } 9117 NextCursor: 9118 llvm::DenseMap<FileID, std::map<unsigned, RawComment *>> 9119 FileToOffsetToComment; 9120 for (RawComment *C : Comments) { 9121 SourceLocation CommentLoc = C->getBeginLoc(); 9122 if (CommentLoc.isValid()) { 9123 std::pair<FileID, unsigned> Loc = 9124 SourceMgr.getDecomposedLoc(CommentLoc); 9125 if (Loc.first.isValid()) 9126 Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C); 9127 } 9128 } 9129 } 9130 } 9131 9132 void ASTReader::visitInputFiles(serialization::ModuleFile &MF, 9133 bool IncludeSystem, bool Complain, 9134 llvm::function_ref<void(const serialization::InputFile &IF, 9135 bool isSystem)> Visitor) { 9136 unsigned NumUserInputs = MF.NumUserInputFiles; 9137 unsigned NumInputs = MF.InputFilesLoaded.size(); 9138 assert(NumUserInputs <= NumInputs); 9139 unsigned N = IncludeSystem ? NumInputs : NumUserInputs; 9140 for (unsigned I = 0; I < N; ++I) { 9141 bool IsSystem = I >= NumUserInputs; 9142 InputFile IF = getInputFile(MF, I+1, Complain); 9143 Visitor(IF, IsSystem); 9144 } 9145 } 9146 9147 void ASTReader::visitTopLevelModuleMaps( 9148 serialization::ModuleFile &MF, 9149 llvm::function_ref<void(const FileEntry *FE)> Visitor) { 9150 unsigned NumInputs = MF.InputFilesLoaded.size(); 9151 for (unsigned I = 0; I < NumInputs; ++I) { 9152 InputFileInfo IFI = readInputFileInfo(MF, I + 1); 9153 if (IFI.TopLevelModuleMap) 9154 // FIXME: This unnecessarily re-reads the InputFileInfo. 9155 if (auto FE = getInputFile(MF, I + 1).getFile()) 9156 Visitor(FE); 9157 } 9158 } 9159 9160 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) { 9161 // If we know the owning module, use it. 9162 if (Module *M = D->getImportedOwningModule()) 9163 return M->getFullModuleName(); 9164 9165 // Otherwise, use the name of the top-level module the decl is within. 9166 if (ModuleFile *M = getOwningModuleFile(D)) 9167 return M->ModuleName; 9168 9169 // Not from a module. 9170 return {}; 9171 } 9172 9173 void ASTReader::finishPendingActions() { 9174 while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() || 9175 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() || 9176 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() || 9177 !PendingUpdateRecords.empty()) { 9178 // If any identifiers with corresponding top-level declarations have 9179 // been loaded, load those declarations now. 9180 using TopLevelDeclsMap = 9181 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>; 9182 TopLevelDeclsMap TopLevelDecls; 9183 9184 while (!PendingIdentifierInfos.empty()) { 9185 IdentifierInfo *II = PendingIdentifierInfos.back().first; 9186 SmallVector<uint32_t, 4> DeclIDs = 9187 std::move(PendingIdentifierInfos.back().second); 9188 PendingIdentifierInfos.pop_back(); 9189 9190 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]); 9191 } 9192 9193 // Load each function type that we deferred loading because it was a 9194 // deduced type that might refer to a local type declared within itself. 9195 for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) { 9196 auto *FD = PendingFunctionTypes[I].first; 9197 FD->setType(GetType(PendingFunctionTypes[I].second)); 9198 9199 // If we gave a function a deduced return type, remember that we need to 9200 // propagate that along the redeclaration chain. 9201 auto *DT = FD->getReturnType()->getContainedDeducedType(); 9202 if (DT && DT->isDeduced()) 9203 PendingDeducedTypeUpdates.insert( 9204 {FD->getCanonicalDecl(), FD->getReturnType()}); 9205 } 9206 PendingFunctionTypes.clear(); 9207 9208 // For each decl chain that we wanted to complete while deserializing, mark 9209 // it as "still needs to be completed". 9210 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) { 9211 markIncompleteDeclChain(PendingIncompleteDeclChains[I]); 9212 } 9213 PendingIncompleteDeclChains.clear(); 9214 9215 // Load pending declaration chains. 9216 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) 9217 loadPendingDeclChain(PendingDeclChains[I].first, 9218 PendingDeclChains[I].second); 9219 PendingDeclChains.clear(); 9220 9221 // Make the most recent of the top-level declarations visible. 9222 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(), 9223 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) { 9224 IdentifierInfo *II = TLD->first; 9225 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) { 9226 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II); 9227 } 9228 } 9229 9230 // Load any pending macro definitions. 9231 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) { 9232 IdentifierInfo *II = PendingMacroIDs.begin()[I].first; 9233 SmallVector<PendingMacroInfo, 2> GlobalIDs; 9234 GlobalIDs.swap(PendingMacroIDs.begin()[I].second); 9235 // Initialize the macro history from chained-PCHs ahead of module imports. 9236 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9237 ++IDIdx) { 9238 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9239 if (!Info.M->isModule()) 9240 resolvePendingMacro(II, Info); 9241 } 9242 // Handle module imports. 9243 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9244 ++IDIdx) { 9245 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9246 if (Info.M->isModule()) 9247 resolvePendingMacro(II, Info); 9248 } 9249 } 9250 PendingMacroIDs.clear(); 9251 9252 // Wire up the DeclContexts for Decls that we delayed setting until 9253 // recursive loading is completed. 9254 while (!PendingDeclContextInfos.empty()) { 9255 PendingDeclContextInfo Info = PendingDeclContextInfos.front(); 9256 PendingDeclContextInfos.pop_front(); 9257 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC)); 9258 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC)); 9259 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext()); 9260 } 9261 9262 // Perform any pending declaration updates. 9263 while (!PendingUpdateRecords.empty()) { 9264 auto Update = PendingUpdateRecords.pop_back_val(); 9265 ReadingKindTracker ReadingKind(Read_Decl, *this); 9266 loadDeclUpdateRecords(Update); 9267 } 9268 } 9269 9270 // At this point, all update records for loaded decls are in place, so any 9271 // fake class definitions should have become real. 9272 assert(PendingFakeDefinitionData.empty() && 9273 "faked up a class definition but never saw the real one"); 9274 9275 // If we deserialized any C++ or Objective-C class definitions, any 9276 // Objective-C protocol definitions, or any redeclarable templates, make sure 9277 // that all redeclarations point to the definitions. Note that this can only 9278 // happen now, after the redeclaration chains have been fully wired. 9279 for (Decl *D : PendingDefinitions) { 9280 if (TagDecl *TD = dyn_cast<TagDecl>(D)) { 9281 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) { 9282 // Make sure that the TagType points at the definition. 9283 const_cast<TagType*>(TagT)->decl = TD; 9284 } 9285 9286 if (auto RD = dyn_cast<CXXRecordDecl>(D)) { 9287 for (auto *R = getMostRecentExistingDecl(RD); R; 9288 R = R->getPreviousDecl()) { 9289 assert((R == D) == 9290 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() && 9291 "declaration thinks it's the definition but it isn't"); 9292 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData; 9293 } 9294 } 9295 9296 continue; 9297 } 9298 9299 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) { 9300 // Make sure that the ObjCInterfaceType points at the definition. 9301 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl)) 9302 ->Decl = ID; 9303 9304 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl()) 9305 cast<ObjCInterfaceDecl>(R)->Data = ID->Data; 9306 9307 continue; 9308 } 9309 9310 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) { 9311 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl()) 9312 cast<ObjCProtocolDecl>(R)->Data = PD->Data; 9313 9314 continue; 9315 } 9316 9317 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl(); 9318 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl()) 9319 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common; 9320 } 9321 PendingDefinitions.clear(); 9322 9323 // Load the bodies of any functions or methods we've encountered. We do 9324 // this now (delayed) so that we can be sure that the declaration chains 9325 // have been fully wired up (hasBody relies on this). 9326 // FIXME: We shouldn't require complete redeclaration chains here. 9327 for (PendingBodiesMap::iterator PB = PendingBodies.begin(), 9328 PBEnd = PendingBodies.end(); 9329 PB != PBEnd; ++PB) { 9330 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) { 9331 // For a function defined inline within a class template, force the 9332 // canonical definition to be the one inside the canonical definition of 9333 // the template. This ensures that we instantiate from a correct view 9334 // of the template. 9335 // 9336 // Sadly we can't do this more generally: we can't be sure that all 9337 // copies of an arbitrary class definition will have the same members 9338 // defined (eg, some member functions may not be instantiated, and some 9339 // special members may or may not have been implicitly defined). 9340 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent())) 9341 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition()) 9342 continue; 9343 9344 // FIXME: Check for =delete/=default? 9345 // FIXME: Complain about ODR violations here? 9346 const FunctionDecl *Defn = nullptr; 9347 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) { 9348 FD->setLazyBody(PB->second); 9349 } else { 9350 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn); 9351 mergeDefinitionVisibility(NonConstDefn, FD); 9352 9353 if (!FD->isLateTemplateParsed() && 9354 !NonConstDefn->isLateTemplateParsed() && 9355 FD->getODRHash() != NonConstDefn->getODRHash()) { 9356 if (!isa<CXXMethodDecl>(FD)) { 9357 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9358 } else if (FD->getLexicalParent()->isFileContext() && 9359 NonConstDefn->getLexicalParent()->isFileContext()) { 9360 // Only diagnose out-of-line method definitions. If they are 9361 // in class definitions, then an error will be generated when 9362 // processing the class bodies. 9363 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9364 } 9365 } 9366 } 9367 continue; 9368 } 9369 9370 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first); 9371 if (!getContext().getLangOpts().Modules || !MD->hasBody()) 9372 MD->setLazyBody(PB->second); 9373 } 9374 PendingBodies.clear(); 9375 9376 // Do some cleanup. 9377 for (auto *ND : PendingMergedDefinitionsToDeduplicate) 9378 getContext().deduplicateMergedDefinitonsFor(ND); 9379 PendingMergedDefinitionsToDeduplicate.clear(); 9380 } 9381 9382 void ASTReader::diagnoseOdrViolations() { 9383 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() && 9384 PendingFunctionOdrMergeFailures.empty() && 9385 PendingEnumOdrMergeFailures.empty()) 9386 return; 9387 9388 // Trigger the import of the full definition of each class that had any 9389 // odr-merging problems, so we can produce better diagnostics for them. 9390 // These updates may in turn find and diagnose some ODR failures, so take 9391 // ownership of the set first. 9392 auto OdrMergeFailures = std::move(PendingOdrMergeFailures); 9393 PendingOdrMergeFailures.clear(); 9394 for (auto &Merge : OdrMergeFailures) { 9395 Merge.first->buildLookup(); 9396 Merge.first->decls_begin(); 9397 Merge.first->bases_begin(); 9398 Merge.first->vbases_begin(); 9399 for (auto &RecordPair : Merge.second) { 9400 auto *RD = RecordPair.first; 9401 RD->decls_begin(); 9402 RD->bases_begin(); 9403 RD->vbases_begin(); 9404 } 9405 } 9406 9407 // Trigger the import of functions. 9408 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures); 9409 PendingFunctionOdrMergeFailures.clear(); 9410 for (auto &Merge : FunctionOdrMergeFailures) { 9411 Merge.first->buildLookup(); 9412 Merge.first->decls_begin(); 9413 Merge.first->getBody(); 9414 for (auto &FD : Merge.second) { 9415 FD->buildLookup(); 9416 FD->decls_begin(); 9417 FD->getBody(); 9418 } 9419 } 9420 9421 // Trigger the import of enums. 9422 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures); 9423 PendingEnumOdrMergeFailures.clear(); 9424 for (auto &Merge : EnumOdrMergeFailures) { 9425 Merge.first->decls_begin(); 9426 for (auto &Enum : Merge.second) { 9427 Enum->decls_begin(); 9428 } 9429 } 9430 9431 // For each declaration from a merged context, check that the canonical 9432 // definition of that context also contains a declaration of the same 9433 // entity. 9434 // 9435 // Caution: this loop does things that might invalidate iterators into 9436 // PendingOdrMergeChecks. Don't turn this into a range-based for loop! 9437 while (!PendingOdrMergeChecks.empty()) { 9438 NamedDecl *D = PendingOdrMergeChecks.pop_back_val(); 9439 9440 // FIXME: Skip over implicit declarations for now. This matters for things 9441 // like implicitly-declared special member functions. This isn't entirely 9442 // correct; we can end up with multiple unmerged declarations of the same 9443 // implicit entity. 9444 if (D->isImplicit()) 9445 continue; 9446 9447 DeclContext *CanonDef = D->getDeclContext(); 9448 9449 bool Found = false; 9450 const Decl *DCanon = D->getCanonicalDecl(); 9451 9452 for (auto RI : D->redecls()) { 9453 if (RI->getLexicalDeclContext() == CanonDef) { 9454 Found = true; 9455 break; 9456 } 9457 } 9458 if (Found) 9459 continue; 9460 9461 // Quick check failed, time to do the slow thing. Note, we can't just 9462 // look up the name of D in CanonDef here, because the member that is 9463 // in CanonDef might not be found by name lookup (it might have been 9464 // replaced by a more recent declaration in the lookup table), and we 9465 // can't necessarily find it in the redeclaration chain because it might 9466 // be merely mergeable, not redeclarable. 9467 llvm::SmallVector<const NamedDecl*, 4> Candidates; 9468 for (auto *CanonMember : CanonDef->decls()) { 9469 if (CanonMember->getCanonicalDecl() == DCanon) { 9470 // This can happen if the declaration is merely mergeable and not 9471 // actually redeclarable (we looked for redeclarations earlier). 9472 // 9473 // FIXME: We should be able to detect this more efficiently, without 9474 // pulling in all of the members of CanonDef. 9475 Found = true; 9476 break; 9477 } 9478 if (auto *ND = dyn_cast<NamedDecl>(CanonMember)) 9479 if (ND->getDeclName() == D->getDeclName()) 9480 Candidates.push_back(ND); 9481 } 9482 9483 if (!Found) { 9484 // The AST doesn't like TagDecls becoming invalid after they've been 9485 // completed. We only really need to mark FieldDecls as invalid here. 9486 if (!isa<TagDecl>(D)) 9487 D->setInvalidDecl(); 9488 9489 // Ensure we don't accidentally recursively enter deserialization while 9490 // we're producing our diagnostic. 9491 Deserializing RecursionGuard(this); 9492 9493 std::string CanonDefModule = 9494 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef)); 9495 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl) 9496 << D << getOwningModuleNameForDiagnostic(D) 9497 << CanonDef << CanonDefModule.empty() << CanonDefModule; 9498 9499 if (Candidates.empty()) 9500 Diag(cast<Decl>(CanonDef)->getLocation(), 9501 diag::note_module_odr_violation_no_possible_decls) << D; 9502 else { 9503 for (unsigned I = 0, N = Candidates.size(); I != N; ++I) 9504 Diag(Candidates[I]->getLocation(), 9505 diag::note_module_odr_violation_possible_decl) 9506 << Candidates[I]; 9507 } 9508 9509 DiagnosedOdrMergeFailures.insert(CanonDef); 9510 } 9511 } 9512 9513 if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() && 9514 EnumOdrMergeFailures.empty()) 9515 return; 9516 9517 // Ensure we don't accidentally recursively enter deserialization while 9518 // we're producing our diagnostics. 9519 Deserializing RecursionGuard(this); 9520 9521 // Common code for hashing helpers. 9522 ODRHash Hash; 9523 auto ComputeQualTypeODRHash = [&Hash](QualType Ty) { 9524 Hash.clear(); 9525 Hash.AddQualType(Ty); 9526 return Hash.CalculateHash(); 9527 }; 9528 9529 auto ComputeODRHash = [&Hash](const Stmt *S) { 9530 assert(S); 9531 Hash.clear(); 9532 Hash.AddStmt(S); 9533 return Hash.CalculateHash(); 9534 }; 9535 9536 auto ComputeSubDeclODRHash = [&Hash](const Decl *D) { 9537 assert(D); 9538 Hash.clear(); 9539 Hash.AddSubDecl(D); 9540 return Hash.CalculateHash(); 9541 }; 9542 9543 auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) { 9544 Hash.clear(); 9545 Hash.AddTemplateArgument(TA); 9546 return Hash.CalculateHash(); 9547 }; 9548 9549 auto ComputeTemplateParameterListODRHash = 9550 [&Hash](const TemplateParameterList *TPL) { 9551 assert(TPL); 9552 Hash.clear(); 9553 Hash.AddTemplateParameterList(TPL); 9554 return Hash.CalculateHash(); 9555 }; 9556 9557 // Used with err_module_odr_violation_mismatch_decl and 9558 // note_module_odr_violation_mismatch_decl 9559 // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed 9560 enum ODRMismatchDecl { 9561 EndOfClass, 9562 PublicSpecifer, 9563 PrivateSpecifer, 9564 ProtectedSpecifer, 9565 StaticAssert, 9566 Field, 9567 CXXMethod, 9568 TypeAlias, 9569 TypeDef, 9570 Var, 9571 Friend, 9572 FunctionTemplate, 9573 Other 9574 }; 9575 9576 // Used with err_module_odr_violation_mismatch_decl_diff and 9577 // note_module_odr_violation_mismatch_decl_diff 9578 enum ODRMismatchDeclDifference { 9579 StaticAssertCondition, 9580 StaticAssertMessage, 9581 StaticAssertOnlyMessage, 9582 FieldName, 9583 FieldTypeName, 9584 FieldSingleBitField, 9585 FieldDifferentWidthBitField, 9586 FieldSingleMutable, 9587 FieldSingleInitializer, 9588 FieldDifferentInitializers, 9589 MethodName, 9590 MethodDeleted, 9591 MethodDefaulted, 9592 MethodVirtual, 9593 MethodStatic, 9594 MethodVolatile, 9595 MethodConst, 9596 MethodInline, 9597 MethodNumberParameters, 9598 MethodParameterType, 9599 MethodParameterName, 9600 MethodParameterSingleDefaultArgument, 9601 MethodParameterDifferentDefaultArgument, 9602 MethodNoTemplateArguments, 9603 MethodDifferentNumberTemplateArguments, 9604 MethodDifferentTemplateArgument, 9605 MethodSingleBody, 9606 MethodDifferentBody, 9607 TypedefName, 9608 TypedefType, 9609 VarName, 9610 VarType, 9611 VarSingleInitializer, 9612 VarDifferentInitializer, 9613 VarConstexpr, 9614 FriendTypeFunction, 9615 FriendType, 9616 FriendFunction, 9617 FunctionTemplateDifferentNumberParameters, 9618 FunctionTemplateParameterDifferentKind, 9619 FunctionTemplateParameterName, 9620 FunctionTemplateParameterSingleDefaultArgument, 9621 FunctionTemplateParameterDifferentDefaultArgument, 9622 FunctionTemplateParameterDifferentType, 9623 FunctionTemplatePackParameter, 9624 }; 9625 9626 // These lambdas have the common portions of the ODR diagnostics. This 9627 // has the same return as Diag(), so addition parameters can be passed 9628 // in with operator<< 9629 auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule, 9630 SourceLocation Loc, SourceRange Range, 9631 ODRMismatchDeclDifference DiffType) { 9632 return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff) 9633 << FirstRecord << FirstModule.empty() << FirstModule << Range 9634 << DiffType; 9635 }; 9636 auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc, 9637 SourceRange Range, ODRMismatchDeclDifference DiffType) { 9638 return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff) 9639 << SecondModule << Range << DiffType; 9640 }; 9641 9642 auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote, 9643 &ComputeQualTypeODRHash, &ComputeODRHash]( 9644 NamedDecl *FirstRecord, StringRef FirstModule, 9645 StringRef SecondModule, FieldDecl *FirstField, 9646 FieldDecl *SecondField) { 9647 IdentifierInfo *FirstII = FirstField->getIdentifier(); 9648 IdentifierInfo *SecondII = SecondField->getIdentifier(); 9649 if (FirstII->getName() != SecondII->getName()) { 9650 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9651 FirstField->getSourceRange(), FieldName) 9652 << FirstII; 9653 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9654 SecondField->getSourceRange(), FieldName) 9655 << SecondII; 9656 9657 return true; 9658 } 9659 9660 assert(getContext().hasSameType(FirstField->getType(), 9661 SecondField->getType())); 9662 9663 QualType FirstType = FirstField->getType(); 9664 QualType SecondType = SecondField->getType(); 9665 if (ComputeQualTypeODRHash(FirstType) != 9666 ComputeQualTypeODRHash(SecondType)) { 9667 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9668 FirstField->getSourceRange(), FieldTypeName) 9669 << FirstII << FirstType; 9670 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9671 SecondField->getSourceRange(), FieldTypeName) 9672 << SecondII << SecondType; 9673 9674 return true; 9675 } 9676 9677 const bool IsFirstBitField = FirstField->isBitField(); 9678 const bool IsSecondBitField = SecondField->isBitField(); 9679 if (IsFirstBitField != IsSecondBitField) { 9680 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9681 FirstField->getSourceRange(), FieldSingleBitField) 9682 << FirstII << IsFirstBitField; 9683 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9684 SecondField->getSourceRange(), FieldSingleBitField) 9685 << SecondII << IsSecondBitField; 9686 return true; 9687 } 9688 9689 if (IsFirstBitField && IsSecondBitField) { 9690 unsigned FirstBitWidthHash = 9691 ComputeODRHash(FirstField->getBitWidth()); 9692 unsigned SecondBitWidthHash = 9693 ComputeODRHash(SecondField->getBitWidth()); 9694 if (FirstBitWidthHash != SecondBitWidthHash) { 9695 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9696 FirstField->getSourceRange(), 9697 FieldDifferentWidthBitField) 9698 << FirstII << FirstField->getBitWidth()->getSourceRange(); 9699 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9700 SecondField->getSourceRange(), 9701 FieldDifferentWidthBitField) 9702 << SecondII << SecondField->getBitWidth()->getSourceRange(); 9703 return true; 9704 } 9705 } 9706 9707 if (!PP.getLangOpts().CPlusPlus) 9708 return false; 9709 9710 const bool IsFirstMutable = FirstField->isMutable(); 9711 const bool IsSecondMutable = SecondField->isMutable(); 9712 if (IsFirstMutable != IsSecondMutable) { 9713 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9714 FirstField->getSourceRange(), FieldSingleMutable) 9715 << FirstII << IsFirstMutable; 9716 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9717 SecondField->getSourceRange(), FieldSingleMutable) 9718 << SecondII << IsSecondMutable; 9719 return true; 9720 } 9721 9722 const Expr *FirstInitializer = FirstField->getInClassInitializer(); 9723 const Expr *SecondInitializer = SecondField->getInClassInitializer(); 9724 if ((!FirstInitializer && SecondInitializer) || 9725 (FirstInitializer && !SecondInitializer)) { 9726 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9727 FirstField->getSourceRange(), FieldSingleInitializer) 9728 << FirstII << (FirstInitializer != nullptr); 9729 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9730 SecondField->getSourceRange(), FieldSingleInitializer) 9731 << SecondII << (SecondInitializer != nullptr); 9732 return true; 9733 } 9734 9735 if (FirstInitializer && SecondInitializer) { 9736 unsigned FirstInitHash = ComputeODRHash(FirstInitializer); 9737 unsigned SecondInitHash = ComputeODRHash(SecondInitializer); 9738 if (FirstInitHash != SecondInitHash) { 9739 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9740 FirstField->getSourceRange(), 9741 FieldDifferentInitializers) 9742 << FirstII << FirstInitializer->getSourceRange(); 9743 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9744 SecondField->getSourceRange(), 9745 FieldDifferentInitializers) 9746 << SecondII << SecondInitializer->getSourceRange(); 9747 return true; 9748 } 9749 } 9750 9751 return false; 9752 }; 9753 9754 auto ODRDiagTypeDefOrAlias = 9755 [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash]( 9756 NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule, 9757 TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD, 9758 bool IsTypeAlias) { 9759 auto FirstName = FirstTD->getDeclName(); 9760 auto SecondName = SecondTD->getDeclName(); 9761 if (FirstName != SecondName) { 9762 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(), 9763 FirstTD->getSourceRange(), TypedefName) 9764 << IsTypeAlias << FirstName; 9765 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(), 9766 SecondTD->getSourceRange(), TypedefName) 9767 << IsTypeAlias << SecondName; 9768 return true; 9769 } 9770 9771 QualType FirstType = FirstTD->getUnderlyingType(); 9772 QualType SecondType = SecondTD->getUnderlyingType(); 9773 if (ComputeQualTypeODRHash(FirstType) != 9774 ComputeQualTypeODRHash(SecondType)) { 9775 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(), 9776 FirstTD->getSourceRange(), TypedefType) 9777 << IsTypeAlias << FirstName << FirstType; 9778 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(), 9779 SecondTD->getSourceRange(), TypedefType) 9780 << IsTypeAlias << SecondName << SecondType; 9781 return true; 9782 } 9783 9784 return false; 9785 }; 9786 9787 auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote, 9788 &ComputeQualTypeODRHash, &ComputeODRHash, 9789 this](NamedDecl *FirstRecord, StringRef FirstModule, 9790 StringRef SecondModule, VarDecl *FirstVD, 9791 VarDecl *SecondVD) { 9792 auto FirstName = FirstVD->getDeclName(); 9793 auto SecondName = SecondVD->getDeclName(); 9794 if (FirstName != SecondName) { 9795 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9796 FirstVD->getSourceRange(), VarName) 9797 << FirstName; 9798 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9799 SecondVD->getSourceRange(), VarName) 9800 << SecondName; 9801 return true; 9802 } 9803 9804 QualType FirstType = FirstVD->getType(); 9805 QualType SecondType = SecondVD->getType(); 9806 if (ComputeQualTypeODRHash(FirstType) != 9807 ComputeQualTypeODRHash(SecondType)) { 9808 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9809 FirstVD->getSourceRange(), VarType) 9810 << FirstName << FirstType; 9811 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9812 SecondVD->getSourceRange(), VarType) 9813 << SecondName << SecondType; 9814 return true; 9815 } 9816 9817 if (!PP.getLangOpts().CPlusPlus) 9818 return false; 9819 9820 const Expr *FirstInit = FirstVD->getInit(); 9821 const Expr *SecondInit = SecondVD->getInit(); 9822 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 9823 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9824 FirstVD->getSourceRange(), VarSingleInitializer) 9825 << FirstName << (FirstInit == nullptr) 9826 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 9827 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9828 SecondVD->getSourceRange(), VarSingleInitializer) 9829 << SecondName << (SecondInit == nullptr) 9830 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 9831 return true; 9832 } 9833 9834 if (FirstInit && SecondInit && 9835 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 9836 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9837 FirstVD->getSourceRange(), VarDifferentInitializer) 9838 << FirstName << FirstInit->getSourceRange(); 9839 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9840 SecondVD->getSourceRange(), VarDifferentInitializer) 9841 << SecondName << SecondInit->getSourceRange(); 9842 return true; 9843 } 9844 9845 const bool FirstIsConstexpr = FirstVD->isConstexpr(); 9846 const bool SecondIsConstexpr = SecondVD->isConstexpr(); 9847 if (FirstIsConstexpr != SecondIsConstexpr) { 9848 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9849 FirstVD->getSourceRange(), VarConstexpr) 9850 << FirstName << FirstIsConstexpr; 9851 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9852 SecondVD->getSourceRange(), VarConstexpr) 9853 << SecondName << SecondIsConstexpr; 9854 return true; 9855 } 9856 return false; 9857 }; 9858 9859 auto DifferenceSelector = [](Decl *D) { 9860 assert(D && "valid Decl required"); 9861 switch (D->getKind()) { 9862 default: 9863 return Other; 9864 case Decl::AccessSpec: 9865 switch (D->getAccess()) { 9866 case AS_public: 9867 return PublicSpecifer; 9868 case AS_private: 9869 return PrivateSpecifer; 9870 case AS_protected: 9871 return ProtectedSpecifer; 9872 case AS_none: 9873 break; 9874 } 9875 llvm_unreachable("Invalid access specifier"); 9876 case Decl::StaticAssert: 9877 return StaticAssert; 9878 case Decl::Field: 9879 return Field; 9880 case Decl::CXXMethod: 9881 case Decl::CXXConstructor: 9882 case Decl::CXXDestructor: 9883 return CXXMethod; 9884 case Decl::TypeAlias: 9885 return TypeAlias; 9886 case Decl::Typedef: 9887 return TypeDef; 9888 case Decl::Var: 9889 return Var; 9890 case Decl::Friend: 9891 return Friend; 9892 case Decl::FunctionTemplate: 9893 return FunctionTemplate; 9894 } 9895 }; 9896 9897 using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>; 9898 auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes, 9899 RecordDecl *Record, 9900 const DeclContext *DC) { 9901 for (auto *D : Record->decls()) { 9902 if (!ODRHash::isDeclToBeProcessed(D, DC)) 9903 continue; 9904 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 9905 } 9906 }; 9907 9908 struct DiffResult { 9909 Decl *FirstDecl = nullptr, *SecondDecl = nullptr; 9910 ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other; 9911 }; 9912 9913 // If there is a diagnoseable difference, FirstDiffType and 9914 // SecondDiffType will not be Other and FirstDecl and SecondDecl will be 9915 // filled in if not EndOfClass. 9916 auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes, 9917 DeclHashes &SecondHashes) { 9918 DiffResult DR; 9919 auto FirstIt = FirstHashes.begin(); 9920 auto SecondIt = SecondHashes.begin(); 9921 while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) { 9922 if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() && 9923 FirstIt->second == SecondIt->second) { 9924 ++FirstIt; 9925 ++SecondIt; 9926 continue; 9927 } 9928 9929 DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first; 9930 DR.SecondDecl = 9931 SecondIt == SecondHashes.end() ? nullptr : SecondIt->first; 9932 9933 DR.FirstDiffType = 9934 DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass; 9935 DR.SecondDiffType = 9936 DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass; 9937 return DR; 9938 } 9939 return DR; 9940 }; 9941 9942 // Use this to diagnose that an unexpected Decl was encountered 9943 // or no difference was detected. This causes a generic error 9944 // message to be emitted. 9945 auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord, 9946 StringRef FirstModule, 9947 NamedDecl *SecondRecord, 9948 StringRef SecondModule) { 9949 Diag(FirstRecord->getLocation(), 9950 diag::err_module_odr_violation_different_definitions) 9951 << FirstRecord << FirstModule.empty() << FirstModule; 9952 9953 if (DR.FirstDecl) { 9954 Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference) 9955 << FirstRecord << DR.FirstDecl->getSourceRange(); 9956 } 9957 9958 Diag(SecondRecord->getLocation(), 9959 diag::note_module_odr_violation_different_definitions) 9960 << SecondModule; 9961 9962 if (DR.SecondDecl) { 9963 Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference) 9964 << DR.SecondDecl->getSourceRange(); 9965 } 9966 }; 9967 9968 auto DiagnoseODRMismatch = 9969 [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule, 9970 NamedDecl *SecondRecord, StringRef SecondModule) { 9971 SourceLocation FirstLoc; 9972 SourceRange FirstRange; 9973 auto *FirstTag = dyn_cast<TagDecl>(FirstRecord); 9974 if (DR.FirstDiffType == EndOfClass && FirstTag) { 9975 FirstLoc = FirstTag->getBraceRange().getEnd(); 9976 } else { 9977 FirstLoc = DR.FirstDecl->getLocation(); 9978 FirstRange = DR.FirstDecl->getSourceRange(); 9979 } 9980 Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl) 9981 << FirstRecord << FirstModule.empty() << FirstModule << FirstRange 9982 << DR.FirstDiffType; 9983 9984 SourceLocation SecondLoc; 9985 SourceRange SecondRange; 9986 auto *SecondTag = dyn_cast<TagDecl>(SecondRecord); 9987 if (DR.SecondDiffType == EndOfClass && SecondTag) { 9988 SecondLoc = SecondTag->getBraceRange().getEnd(); 9989 } else { 9990 SecondLoc = DR.SecondDecl->getLocation(); 9991 SecondRange = DR.SecondDecl->getSourceRange(); 9992 } 9993 Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl) 9994 << SecondModule << SecondRange << DR.SecondDiffType; 9995 }; 9996 9997 // Issue any pending ODR-failure diagnostics. 9998 for (auto &Merge : OdrMergeFailures) { 9999 // If we've already pointed out a specific problem with this class, don't 10000 // bother issuing a general "something's different" diagnostic. 10001 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 10002 continue; 10003 10004 bool Diagnosed = false; 10005 CXXRecordDecl *FirstRecord = Merge.first; 10006 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord); 10007 for (auto &RecordPair : Merge.second) { 10008 CXXRecordDecl *SecondRecord = RecordPair.first; 10009 // Multiple different declarations got merged together; tell the user 10010 // where they came from. 10011 if (FirstRecord == SecondRecord) 10012 continue; 10013 10014 std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord); 10015 10016 auto *FirstDD = FirstRecord->DefinitionData; 10017 auto *SecondDD = RecordPair.second; 10018 10019 assert(FirstDD && SecondDD && "Definitions without DefinitionData"); 10020 10021 // Diagnostics from DefinitionData are emitted here. 10022 if (FirstDD != SecondDD) { 10023 enum ODRDefinitionDataDifference { 10024 NumBases, 10025 NumVBases, 10026 BaseType, 10027 BaseVirtual, 10028 BaseAccess, 10029 }; 10030 auto ODRDiagBaseError = [FirstRecord, &FirstModule, 10031 this](SourceLocation Loc, SourceRange Range, 10032 ODRDefinitionDataDifference DiffType) { 10033 return Diag(Loc, diag::err_module_odr_violation_definition_data) 10034 << FirstRecord << FirstModule.empty() << FirstModule << Range 10035 << DiffType; 10036 }; 10037 auto ODRDiagBaseNote = [&SecondModule, 10038 this](SourceLocation Loc, SourceRange Range, 10039 ODRDefinitionDataDifference DiffType) { 10040 return Diag(Loc, diag::note_module_odr_violation_definition_data) 10041 << SecondModule << Range << DiffType; 10042 }; 10043 10044 unsigned FirstNumBases = FirstDD->NumBases; 10045 unsigned FirstNumVBases = FirstDD->NumVBases; 10046 unsigned SecondNumBases = SecondDD->NumBases; 10047 unsigned SecondNumVBases = SecondDD->NumVBases; 10048 10049 auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) { 10050 unsigned NumBases = DD->NumBases; 10051 if (NumBases == 0) return SourceRange(); 10052 auto bases = DD->bases(); 10053 return SourceRange(bases[0].getBeginLoc(), 10054 bases[NumBases - 1].getEndLoc()); 10055 }; 10056 10057 if (FirstNumBases != SecondNumBases) { 10058 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10059 NumBases) 10060 << FirstNumBases; 10061 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10062 NumBases) 10063 << SecondNumBases; 10064 Diagnosed = true; 10065 break; 10066 } 10067 10068 if (FirstNumVBases != SecondNumVBases) { 10069 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10070 NumVBases) 10071 << FirstNumVBases; 10072 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10073 NumVBases) 10074 << SecondNumVBases; 10075 Diagnosed = true; 10076 break; 10077 } 10078 10079 auto FirstBases = FirstDD->bases(); 10080 auto SecondBases = SecondDD->bases(); 10081 unsigned i = 0; 10082 for (i = 0; i < FirstNumBases; ++i) { 10083 auto FirstBase = FirstBases[i]; 10084 auto SecondBase = SecondBases[i]; 10085 if (ComputeQualTypeODRHash(FirstBase.getType()) != 10086 ComputeQualTypeODRHash(SecondBase.getType())) { 10087 ODRDiagBaseError(FirstRecord->getLocation(), 10088 FirstBase.getSourceRange(), BaseType) 10089 << (i + 1) << FirstBase.getType(); 10090 ODRDiagBaseNote(SecondRecord->getLocation(), 10091 SecondBase.getSourceRange(), BaseType) 10092 << (i + 1) << SecondBase.getType(); 10093 break; 10094 } 10095 10096 if (FirstBase.isVirtual() != SecondBase.isVirtual()) { 10097 ODRDiagBaseError(FirstRecord->getLocation(), 10098 FirstBase.getSourceRange(), BaseVirtual) 10099 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType(); 10100 ODRDiagBaseNote(SecondRecord->getLocation(), 10101 SecondBase.getSourceRange(), BaseVirtual) 10102 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType(); 10103 break; 10104 } 10105 10106 if (FirstBase.getAccessSpecifierAsWritten() != 10107 SecondBase.getAccessSpecifierAsWritten()) { 10108 ODRDiagBaseError(FirstRecord->getLocation(), 10109 FirstBase.getSourceRange(), BaseAccess) 10110 << (i + 1) << FirstBase.getType() 10111 << (int)FirstBase.getAccessSpecifierAsWritten(); 10112 ODRDiagBaseNote(SecondRecord->getLocation(), 10113 SecondBase.getSourceRange(), BaseAccess) 10114 << (i + 1) << SecondBase.getType() 10115 << (int)SecondBase.getAccessSpecifierAsWritten(); 10116 break; 10117 } 10118 } 10119 10120 if (i != FirstNumBases) { 10121 Diagnosed = true; 10122 break; 10123 } 10124 } 10125 10126 const ClassTemplateDecl *FirstTemplate = 10127 FirstRecord->getDescribedClassTemplate(); 10128 const ClassTemplateDecl *SecondTemplate = 10129 SecondRecord->getDescribedClassTemplate(); 10130 10131 assert(!FirstTemplate == !SecondTemplate && 10132 "Both pointers should be null or non-null"); 10133 10134 enum ODRTemplateDifference { 10135 ParamEmptyName, 10136 ParamName, 10137 ParamSingleDefaultArgument, 10138 ParamDifferentDefaultArgument, 10139 }; 10140 10141 if (FirstTemplate && SecondTemplate) { 10142 DeclHashes FirstTemplateHashes; 10143 DeclHashes SecondTemplateHashes; 10144 10145 auto PopulateTemplateParameterHashs = 10146 [&ComputeSubDeclODRHash](DeclHashes &Hashes, 10147 const ClassTemplateDecl *TD) { 10148 for (auto *D : TD->getTemplateParameters()->asArray()) { 10149 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 10150 } 10151 }; 10152 10153 PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate); 10154 PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate); 10155 10156 assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() && 10157 "Number of template parameters should be equal."); 10158 10159 auto FirstIt = FirstTemplateHashes.begin(); 10160 auto FirstEnd = FirstTemplateHashes.end(); 10161 auto SecondIt = SecondTemplateHashes.begin(); 10162 for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) { 10163 if (FirstIt->second == SecondIt->second) 10164 continue; 10165 10166 auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this]( 10167 SourceLocation Loc, SourceRange Range, 10168 ODRTemplateDifference DiffType) { 10169 return Diag(Loc, diag::err_module_odr_violation_template_parameter) 10170 << FirstRecord << FirstModule.empty() << FirstModule << Range 10171 << DiffType; 10172 }; 10173 auto ODRDiagTemplateNote = [&SecondModule, this]( 10174 SourceLocation Loc, SourceRange Range, 10175 ODRTemplateDifference DiffType) { 10176 return Diag(Loc, diag::note_module_odr_violation_template_parameter) 10177 << SecondModule << Range << DiffType; 10178 }; 10179 10180 const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first); 10181 const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first); 10182 10183 assert(FirstDecl->getKind() == SecondDecl->getKind() && 10184 "Parameter Decl's should be the same kind."); 10185 10186 DeclarationName FirstName = FirstDecl->getDeclName(); 10187 DeclarationName SecondName = SecondDecl->getDeclName(); 10188 10189 if (FirstName != SecondName) { 10190 const bool FirstNameEmpty = 10191 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo(); 10192 const bool SecondNameEmpty = 10193 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo(); 10194 assert((!FirstNameEmpty || !SecondNameEmpty) && 10195 "Both template parameters cannot be unnamed."); 10196 ODRDiagTemplateError(FirstDecl->getLocation(), 10197 FirstDecl->getSourceRange(), 10198 FirstNameEmpty ? ParamEmptyName : ParamName) 10199 << FirstName; 10200 ODRDiagTemplateNote(SecondDecl->getLocation(), 10201 SecondDecl->getSourceRange(), 10202 SecondNameEmpty ? ParamEmptyName : ParamName) 10203 << SecondName; 10204 break; 10205 } 10206 10207 switch (FirstDecl->getKind()) { 10208 default: 10209 llvm_unreachable("Invalid template parameter type."); 10210 case Decl::TemplateTypeParm: { 10211 const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl); 10212 const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl); 10213 const bool HasFirstDefaultArgument = 10214 FirstParam->hasDefaultArgument() && 10215 !FirstParam->defaultArgumentWasInherited(); 10216 const bool HasSecondDefaultArgument = 10217 SecondParam->hasDefaultArgument() && 10218 !SecondParam->defaultArgumentWasInherited(); 10219 10220 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10221 ODRDiagTemplateError(FirstDecl->getLocation(), 10222 FirstDecl->getSourceRange(), 10223 ParamSingleDefaultArgument) 10224 << HasFirstDefaultArgument; 10225 ODRDiagTemplateNote(SecondDecl->getLocation(), 10226 SecondDecl->getSourceRange(), 10227 ParamSingleDefaultArgument) 10228 << HasSecondDefaultArgument; 10229 break; 10230 } 10231 10232 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10233 "Expecting default arguments."); 10234 10235 ODRDiagTemplateError(FirstDecl->getLocation(), 10236 FirstDecl->getSourceRange(), 10237 ParamDifferentDefaultArgument); 10238 ODRDiagTemplateNote(SecondDecl->getLocation(), 10239 SecondDecl->getSourceRange(), 10240 ParamDifferentDefaultArgument); 10241 10242 break; 10243 } 10244 case Decl::NonTypeTemplateParm: { 10245 const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl); 10246 const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl); 10247 const bool HasFirstDefaultArgument = 10248 FirstParam->hasDefaultArgument() && 10249 !FirstParam->defaultArgumentWasInherited(); 10250 const bool HasSecondDefaultArgument = 10251 SecondParam->hasDefaultArgument() && 10252 !SecondParam->defaultArgumentWasInherited(); 10253 10254 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10255 ODRDiagTemplateError(FirstDecl->getLocation(), 10256 FirstDecl->getSourceRange(), 10257 ParamSingleDefaultArgument) 10258 << HasFirstDefaultArgument; 10259 ODRDiagTemplateNote(SecondDecl->getLocation(), 10260 SecondDecl->getSourceRange(), 10261 ParamSingleDefaultArgument) 10262 << HasSecondDefaultArgument; 10263 break; 10264 } 10265 10266 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10267 "Expecting default arguments."); 10268 10269 ODRDiagTemplateError(FirstDecl->getLocation(), 10270 FirstDecl->getSourceRange(), 10271 ParamDifferentDefaultArgument); 10272 ODRDiagTemplateNote(SecondDecl->getLocation(), 10273 SecondDecl->getSourceRange(), 10274 ParamDifferentDefaultArgument); 10275 10276 break; 10277 } 10278 case Decl::TemplateTemplateParm: { 10279 const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl); 10280 const auto *SecondParam = 10281 cast<TemplateTemplateParmDecl>(SecondDecl); 10282 const bool HasFirstDefaultArgument = 10283 FirstParam->hasDefaultArgument() && 10284 !FirstParam->defaultArgumentWasInherited(); 10285 const bool HasSecondDefaultArgument = 10286 SecondParam->hasDefaultArgument() && 10287 !SecondParam->defaultArgumentWasInherited(); 10288 10289 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10290 ODRDiagTemplateError(FirstDecl->getLocation(), 10291 FirstDecl->getSourceRange(), 10292 ParamSingleDefaultArgument) 10293 << HasFirstDefaultArgument; 10294 ODRDiagTemplateNote(SecondDecl->getLocation(), 10295 SecondDecl->getSourceRange(), 10296 ParamSingleDefaultArgument) 10297 << HasSecondDefaultArgument; 10298 break; 10299 } 10300 10301 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10302 "Expecting default arguments."); 10303 10304 ODRDiagTemplateError(FirstDecl->getLocation(), 10305 FirstDecl->getSourceRange(), 10306 ParamDifferentDefaultArgument); 10307 ODRDiagTemplateNote(SecondDecl->getLocation(), 10308 SecondDecl->getSourceRange(), 10309 ParamDifferentDefaultArgument); 10310 10311 break; 10312 } 10313 } 10314 10315 break; 10316 } 10317 10318 if (FirstIt != FirstEnd) { 10319 Diagnosed = true; 10320 break; 10321 } 10322 } 10323 10324 DeclHashes FirstHashes; 10325 DeclHashes SecondHashes; 10326 const DeclContext *DC = FirstRecord; 10327 PopulateHashes(FirstHashes, FirstRecord, DC); 10328 PopulateHashes(SecondHashes, SecondRecord, DC); 10329 10330 auto DR = FindTypeDiffs(FirstHashes, SecondHashes); 10331 ODRMismatchDecl FirstDiffType = DR.FirstDiffType; 10332 ODRMismatchDecl SecondDiffType = DR.SecondDiffType; 10333 Decl *FirstDecl = DR.FirstDecl; 10334 Decl *SecondDecl = DR.SecondDecl; 10335 10336 if (FirstDiffType == Other || SecondDiffType == Other) { 10337 DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord, 10338 SecondModule); 10339 Diagnosed = true; 10340 break; 10341 } 10342 10343 if (FirstDiffType != SecondDiffType) { 10344 DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord, 10345 SecondModule); 10346 Diagnosed = true; 10347 break; 10348 } 10349 10350 assert(FirstDiffType == SecondDiffType); 10351 10352 switch (FirstDiffType) { 10353 case Other: 10354 case EndOfClass: 10355 case PublicSpecifer: 10356 case PrivateSpecifer: 10357 case ProtectedSpecifer: 10358 llvm_unreachable("Invalid diff type"); 10359 10360 case StaticAssert: { 10361 StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl); 10362 StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl); 10363 10364 Expr *FirstExpr = FirstSA->getAssertExpr(); 10365 Expr *SecondExpr = SecondSA->getAssertExpr(); 10366 unsigned FirstODRHash = ComputeODRHash(FirstExpr); 10367 unsigned SecondODRHash = ComputeODRHash(SecondExpr); 10368 if (FirstODRHash != SecondODRHash) { 10369 ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(), 10370 FirstExpr->getSourceRange(), StaticAssertCondition); 10371 ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(), 10372 SecondExpr->getSourceRange(), StaticAssertCondition); 10373 Diagnosed = true; 10374 break; 10375 } 10376 10377 StringLiteral *FirstStr = FirstSA->getMessage(); 10378 StringLiteral *SecondStr = SecondSA->getMessage(); 10379 assert((FirstStr || SecondStr) && "Both messages cannot be empty"); 10380 if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) { 10381 SourceLocation FirstLoc, SecondLoc; 10382 SourceRange FirstRange, SecondRange; 10383 if (FirstStr) { 10384 FirstLoc = FirstStr->getBeginLoc(); 10385 FirstRange = FirstStr->getSourceRange(); 10386 } else { 10387 FirstLoc = FirstSA->getBeginLoc(); 10388 FirstRange = FirstSA->getSourceRange(); 10389 } 10390 if (SecondStr) { 10391 SecondLoc = SecondStr->getBeginLoc(); 10392 SecondRange = SecondStr->getSourceRange(); 10393 } else { 10394 SecondLoc = SecondSA->getBeginLoc(); 10395 SecondRange = SecondSA->getSourceRange(); 10396 } 10397 ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange, 10398 StaticAssertOnlyMessage) 10399 << (FirstStr == nullptr); 10400 ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange, 10401 StaticAssertOnlyMessage) 10402 << (SecondStr == nullptr); 10403 Diagnosed = true; 10404 break; 10405 } 10406 10407 if (FirstStr && SecondStr && 10408 FirstStr->getString() != SecondStr->getString()) { 10409 ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(), 10410 FirstStr->getSourceRange(), StaticAssertMessage); 10411 ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(), 10412 SecondStr->getSourceRange(), StaticAssertMessage); 10413 Diagnosed = true; 10414 break; 10415 } 10416 break; 10417 } 10418 case Field: { 10419 Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule, 10420 cast<FieldDecl>(FirstDecl), 10421 cast<FieldDecl>(SecondDecl)); 10422 break; 10423 } 10424 case CXXMethod: { 10425 enum { 10426 DiagMethod, 10427 DiagConstructor, 10428 DiagDestructor, 10429 } FirstMethodType, 10430 SecondMethodType; 10431 auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) { 10432 if (isa<CXXConstructorDecl>(D)) return DiagConstructor; 10433 if (isa<CXXDestructorDecl>(D)) return DiagDestructor; 10434 return DiagMethod; 10435 }; 10436 const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl); 10437 const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl); 10438 FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod); 10439 SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod); 10440 auto FirstName = FirstMethod->getDeclName(); 10441 auto SecondName = SecondMethod->getDeclName(); 10442 if (FirstMethodType != SecondMethodType || FirstName != SecondName) { 10443 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10444 FirstMethod->getSourceRange(), MethodName) 10445 << FirstMethodType << FirstName; 10446 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10447 SecondMethod->getSourceRange(), MethodName) 10448 << SecondMethodType << SecondName; 10449 10450 Diagnosed = true; 10451 break; 10452 } 10453 10454 const bool FirstDeleted = FirstMethod->isDeletedAsWritten(); 10455 const bool SecondDeleted = SecondMethod->isDeletedAsWritten(); 10456 if (FirstDeleted != SecondDeleted) { 10457 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10458 FirstMethod->getSourceRange(), MethodDeleted) 10459 << FirstMethodType << FirstName << FirstDeleted; 10460 10461 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10462 SecondMethod->getSourceRange(), MethodDeleted) 10463 << SecondMethodType << SecondName << SecondDeleted; 10464 Diagnosed = true; 10465 break; 10466 } 10467 10468 const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted(); 10469 const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted(); 10470 if (FirstDefaulted != SecondDefaulted) { 10471 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10472 FirstMethod->getSourceRange(), MethodDefaulted) 10473 << FirstMethodType << FirstName << FirstDefaulted; 10474 10475 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10476 SecondMethod->getSourceRange(), MethodDefaulted) 10477 << SecondMethodType << SecondName << SecondDefaulted; 10478 Diagnosed = true; 10479 break; 10480 } 10481 10482 const bool FirstVirtual = FirstMethod->isVirtualAsWritten(); 10483 const bool SecondVirtual = SecondMethod->isVirtualAsWritten(); 10484 const bool FirstPure = FirstMethod->isPure(); 10485 const bool SecondPure = SecondMethod->isPure(); 10486 if ((FirstVirtual || SecondVirtual) && 10487 (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) { 10488 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10489 FirstMethod->getSourceRange(), MethodVirtual) 10490 << FirstMethodType << FirstName << FirstPure << FirstVirtual; 10491 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10492 SecondMethod->getSourceRange(), MethodVirtual) 10493 << SecondMethodType << SecondName << SecondPure << SecondVirtual; 10494 Diagnosed = true; 10495 break; 10496 } 10497 10498 // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging, 10499 // FirstDecl is the canonical Decl of SecondDecl, so the storage 10500 // class needs to be checked instead. 10501 const auto FirstStorage = FirstMethod->getStorageClass(); 10502 const auto SecondStorage = SecondMethod->getStorageClass(); 10503 const bool FirstStatic = FirstStorage == SC_Static; 10504 const bool SecondStatic = SecondStorage == SC_Static; 10505 if (FirstStatic != SecondStatic) { 10506 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10507 FirstMethod->getSourceRange(), MethodStatic) 10508 << FirstMethodType << FirstName << FirstStatic; 10509 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10510 SecondMethod->getSourceRange(), MethodStatic) 10511 << SecondMethodType << SecondName << SecondStatic; 10512 Diagnosed = true; 10513 break; 10514 } 10515 10516 const bool FirstVolatile = FirstMethod->isVolatile(); 10517 const bool SecondVolatile = SecondMethod->isVolatile(); 10518 if (FirstVolatile != SecondVolatile) { 10519 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10520 FirstMethod->getSourceRange(), MethodVolatile) 10521 << FirstMethodType << FirstName << FirstVolatile; 10522 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10523 SecondMethod->getSourceRange(), MethodVolatile) 10524 << SecondMethodType << SecondName << SecondVolatile; 10525 Diagnosed = true; 10526 break; 10527 } 10528 10529 const bool FirstConst = FirstMethod->isConst(); 10530 const bool SecondConst = SecondMethod->isConst(); 10531 if (FirstConst != SecondConst) { 10532 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10533 FirstMethod->getSourceRange(), MethodConst) 10534 << FirstMethodType << FirstName << FirstConst; 10535 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10536 SecondMethod->getSourceRange(), MethodConst) 10537 << SecondMethodType << SecondName << SecondConst; 10538 Diagnosed = true; 10539 break; 10540 } 10541 10542 const bool FirstInline = FirstMethod->isInlineSpecified(); 10543 const bool SecondInline = SecondMethod->isInlineSpecified(); 10544 if (FirstInline != SecondInline) { 10545 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10546 FirstMethod->getSourceRange(), MethodInline) 10547 << FirstMethodType << FirstName << FirstInline; 10548 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10549 SecondMethod->getSourceRange(), MethodInline) 10550 << SecondMethodType << SecondName << SecondInline; 10551 Diagnosed = true; 10552 break; 10553 } 10554 10555 const unsigned FirstNumParameters = FirstMethod->param_size(); 10556 const unsigned SecondNumParameters = SecondMethod->param_size(); 10557 if (FirstNumParameters != SecondNumParameters) { 10558 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10559 FirstMethod->getSourceRange(), 10560 MethodNumberParameters) 10561 << FirstMethodType << FirstName << FirstNumParameters; 10562 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10563 SecondMethod->getSourceRange(), 10564 MethodNumberParameters) 10565 << SecondMethodType << SecondName << SecondNumParameters; 10566 Diagnosed = true; 10567 break; 10568 } 10569 10570 // Need this status boolean to know when break out of the switch. 10571 bool ParameterMismatch = false; 10572 for (unsigned I = 0; I < FirstNumParameters; ++I) { 10573 const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I); 10574 const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I); 10575 10576 QualType FirstParamType = FirstParam->getType(); 10577 QualType SecondParamType = SecondParam->getType(); 10578 if (FirstParamType != SecondParamType && 10579 ComputeQualTypeODRHash(FirstParamType) != 10580 ComputeQualTypeODRHash(SecondParamType)) { 10581 if (const DecayedType *ParamDecayedType = 10582 FirstParamType->getAs<DecayedType>()) { 10583 ODRDiagDeclError( 10584 FirstRecord, FirstModule, FirstMethod->getLocation(), 10585 FirstMethod->getSourceRange(), MethodParameterType) 10586 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10587 << true << ParamDecayedType->getOriginalType(); 10588 } else { 10589 ODRDiagDeclError( 10590 FirstRecord, FirstModule, FirstMethod->getLocation(), 10591 FirstMethod->getSourceRange(), MethodParameterType) 10592 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10593 << false; 10594 } 10595 10596 if (const DecayedType *ParamDecayedType = 10597 SecondParamType->getAs<DecayedType>()) { 10598 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10599 SecondMethod->getSourceRange(), 10600 MethodParameterType) 10601 << SecondMethodType << SecondName << (I + 1) 10602 << SecondParamType << true 10603 << ParamDecayedType->getOriginalType(); 10604 } else { 10605 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10606 SecondMethod->getSourceRange(), 10607 MethodParameterType) 10608 << SecondMethodType << SecondName << (I + 1) 10609 << SecondParamType << false; 10610 } 10611 ParameterMismatch = true; 10612 break; 10613 } 10614 10615 DeclarationName FirstParamName = FirstParam->getDeclName(); 10616 DeclarationName SecondParamName = SecondParam->getDeclName(); 10617 if (FirstParamName != SecondParamName) { 10618 ODRDiagDeclError(FirstRecord, FirstModule, 10619 FirstMethod->getLocation(), 10620 FirstMethod->getSourceRange(), MethodParameterName) 10621 << FirstMethodType << FirstName << (I + 1) << FirstParamName; 10622 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10623 SecondMethod->getSourceRange(), MethodParameterName) 10624 << SecondMethodType << SecondName << (I + 1) << SecondParamName; 10625 ParameterMismatch = true; 10626 break; 10627 } 10628 10629 const Expr *FirstInit = FirstParam->getInit(); 10630 const Expr *SecondInit = SecondParam->getInit(); 10631 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 10632 ODRDiagDeclError(FirstRecord, FirstModule, 10633 FirstMethod->getLocation(), 10634 FirstMethod->getSourceRange(), 10635 MethodParameterSingleDefaultArgument) 10636 << FirstMethodType << FirstName << (I + 1) 10637 << (FirstInit == nullptr) 10638 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 10639 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10640 SecondMethod->getSourceRange(), 10641 MethodParameterSingleDefaultArgument) 10642 << SecondMethodType << SecondName << (I + 1) 10643 << (SecondInit == nullptr) 10644 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 10645 ParameterMismatch = true; 10646 break; 10647 } 10648 10649 if (FirstInit && SecondInit && 10650 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 10651 ODRDiagDeclError(FirstRecord, FirstModule, 10652 FirstMethod->getLocation(), 10653 FirstMethod->getSourceRange(), 10654 MethodParameterDifferentDefaultArgument) 10655 << FirstMethodType << FirstName << (I + 1) 10656 << FirstInit->getSourceRange(); 10657 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10658 SecondMethod->getSourceRange(), 10659 MethodParameterDifferentDefaultArgument) 10660 << SecondMethodType << SecondName << (I + 1) 10661 << SecondInit->getSourceRange(); 10662 ParameterMismatch = true; 10663 break; 10664 10665 } 10666 } 10667 10668 if (ParameterMismatch) { 10669 Diagnosed = true; 10670 break; 10671 } 10672 10673 const auto *FirstTemplateArgs = 10674 FirstMethod->getTemplateSpecializationArgs(); 10675 const auto *SecondTemplateArgs = 10676 SecondMethod->getTemplateSpecializationArgs(); 10677 10678 if ((FirstTemplateArgs && !SecondTemplateArgs) || 10679 (!FirstTemplateArgs && SecondTemplateArgs)) { 10680 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10681 FirstMethod->getSourceRange(), 10682 MethodNoTemplateArguments) 10683 << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr); 10684 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10685 SecondMethod->getSourceRange(), 10686 MethodNoTemplateArguments) 10687 << SecondMethodType << SecondName 10688 << (SecondTemplateArgs != nullptr); 10689 10690 Diagnosed = true; 10691 break; 10692 } 10693 10694 if (FirstTemplateArgs && SecondTemplateArgs) { 10695 // Remove pack expansions from argument list. 10696 auto ExpandTemplateArgumentList = 10697 [](const TemplateArgumentList *TAL) { 10698 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList; 10699 for (const TemplateArgument &TA : TAL->asArray()) { 10700 if (TA.getKind() != TemplateArgument::Pack) { 10701 ExpandedList.push_back(&TA); 10702 continue; 10703 } 10704 for (const TemplateArgument &PackTA : TA.getPackAsArray()) { 10705 ExpandedList.push_back(&PackTA); 10706 } 10707 } 10708 return ExpandedList; 10709 }; 10710 llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList = 10711 ExpandTemplateArgumentList(FirstTemplateArgs); 10712 llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList = 10713 ExpandTemplateArgumentList(SecondTemplateArgs); 10714 10715 if (FirstExpandedList.size() != SecondExpandedList.size()) { 10716 ODRDiagDeclError(FirstRecord, FirstModule, 10717 FirstMethod->getLocation(), 10718 FirstMethod->getSourceRange(), 10719 MethodDifferentNumberTemplateArguments) 10720 << FirstMethodType << FirstName 10721 << (unsigned)FirstExpandedList.size(); 10722 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10723 SecondMethod->getSourceRange(), 10724 MethodDifferentNumberTemplateArguments) 10725 << SecondMethodType << SecondName 10726 << (unsigned)SecondExpandedList.size(); 10727 10728 Diagnosed = true; 10729 break; 10730 } 10731 10732 bool TemplateArgumentMismatch = false; 10733 for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) { 10734 const TemplateArgument &FirstTA = *FirstExpandedList[i], 10735 &SecondTA = *SecondExpandedList[i]; 10736 if (ComputeTemplateArgumentODRHash(FirstTA) == 10737 ComputeTemplateArgumentODRHash(SecondTA)) { 10738 continue; 10739 } 10740 10741 ODRDiagDeclError( 10742 FirstRecord, FirstModule, FirstMethod->getLocation(), 10743 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument) 10744 << FirstMethodType << FirstName << FirstTA << i + 1; 10745 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10746 SecondMethod->getSourceRange(), 10747 MethodDifferentTemplateArgument) 10748 << SecondMethodType << SecondName << SecondTA << i + 1; 10749 10750 TemplateArgumentMismatch = true; 10751 break; 10752 } 10753 10754 if (TemplateArgumentMismatch) { 10755 Diagnosed = true; 10756 break; 10757 } 10758 } 10759 10760 // Compute the hash of the method as if it has no body. 10761 auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) { 10762 Hash.clear(); 10763 Hash.AddFunctionDecl(D, true /*SkipBody*/); 10764 return Hash.CalculateHash(); 10765 }; 10766 10767 // Compare the hash generated to the hash stored. A difference means 10768 // that a body was present in the original source. Due to merging, 10769 // the stardard way of detecting a body will not work. 10770 const bool HasFirstBody = 10771 ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash(); 10772 const bool HasSecondBody = 10773 ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash(); 10774 10775 if (HasFirstBody != HasSecondBody) { 10776 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10777 FirstMethod->getSourceRange(), MethodSingleBody) 10778 << FirstMethodType << FirstName << HasFirstBody; 10779 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10780 SecondMethod->getSourceRange(), MethodSingleBody) 10781 << SecondMethodType << SecondName << HasSecondBody; 10782 Diagnosed = true; 10783 break; 10784 } 10785 10786 if (HasFirstBody && HasSecondBody) { 10787 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10788 FirstMethod->getSourceRange(), MethodDifferentBody) 10789 << FirstMethodType << FirstName; 10790 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10791 SecondMethod->getSourceRange(), MethodDifferentBody) 10792 << SecondMethodType << SecondName; 10793 Diagnosed = true; 10794 break; 10795 } 10796 10797 break; 10798 } 10799 case TypeAlias: 10800 case TypeDef: { 10801 Diagnosed = ODRDiagTypeDefOrAlias( 10802 FirstRecord, FirstModule, SecondModule, 10803 cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl), 10804 FirstDiffType == TypeAlias); 10805 break; 10806 } 10807 case Var: { 10808 Diagnosed = 10809 ODRDiagVar(FirstRecord, FirstModule, SecondModule, 10810 cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl)); 10811 break; 10812 } 10813 case Friend: { 10814 FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl); 10815 FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl); 10816 10817 NamedDecl *FirstND = FirstFriend->getFriendDecl(); 10818 NamedDecl *SecondND = SecondFriend->getFriendDecl(); 10819 10820 TypeSourceInfo *FirstTSI = FirstFriend->getFriendType(); 10821 TypeSourceInfo *SecondTSI = SecondFriend->getFriendType(); 10822 10823 if (FirstND && SecondND) { 10824 ODRDiagDeclError(FirstRecord, FirstModule, 10825 FirstFriend->getFriendLoc(), 10826 FirstFriend->getSourceRange(), FriendFunction) 10827 << FirstND; 10828 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10829 SecondFriend->getSourceRange(), FriendFunction) 10830 << SecondND; 10831 10832 Diagnosed = true; 10833 break; 10834 } 10835 10836 if (FirstTSI && SecondTSI) { 10837 QualType FirstFriendType = FirstTSI->getType(); 10838 QualType SecondFriendType = SecondTSI->getType(); 10839 assert(ComputeQualTypeODRHash(FirstFriendType) != 10840 ComputeQualTypeODRHash(SecondFriendType)); 10841 ODRDiagDeclError(FirstRecord, FirstModule, 10842 FirstFriend->getFriendLoc(), 10843 FirstFriend->getSourceRange(), FriendType) 10844 << FirstFriendType; 10845 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10846 SecondFriend->getSourceRange(), FriendType) 10847 << SecondFriendType; 10848 Diagnosed = true; 10849 break; 10850 } 10851 10852 ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(), 10853 FirstFriend->getSourceRange(), FriendTypeFunction) 10854 << (FirstTSI == nullptr); 10855 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10856 SecondFriend->getSourceRange(), FriendTypeFunction) 10857 << (SecondTSI == nullptr); 10858 10859 Diagnosed = true; 10860 break; 10861 } 10862 case FunctionTemplate: { 10863 FunctionTemplateDecl *FirstTemplate = 10864 cast<FunctionTemplateDecl>(FirstDecl); 10865 FunctionTemplateDecl *SecondTemplate = 10866 cast<FunctionTemplateDecl>(SecondDecl); 10867 10868 TemplateParameterList *FirstTPL = 10869 FirstTemplate->getTemplateParameters(); 10870 TemplateParameterList *SecondTPL = 10871 SecondTemplate->getTemplateParameters(); 10872 10873 if (FirstTPL->size() != SecondTPL->size()) { 10874 ODRDiagDeclError(FirstRecord, FirstModule, 10875 FirstTemplate->getLocation(), 10876 FirstTemplate->getSourceRange(), 10877 FunctionTemplateDifferentNumberParameters) 10878 << FirstTemplate << FirstTPL->size(); 10879 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10880 SecondTemplate->getSourceRange(), 10881 FunctionTemplateDifferentNumberParameters) 10882 << SecondTemplate << SecondTPL->size(); 10883 10884 Diagnosed = true; 10885 break; 10886 } 10887 10888 bool ParameterMismatch = false; 10889 for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) { 10890 NamedDecl *FirstParam = FirstTPL->getParam(i); 10891 NamedDecl *SecondParam = SecondTPL->getParam(i); 10892 10893 if (FirstParam->getKind() != SecondParam->getKind()) { 10894 enum { 10895 TemplateTypeParameter, 10896 NonTypeTemplateParameter, 10897 TemplateTemplateParameter, 10898 }; 10899 auto GetParamType = [](NamedDecl *D) { 10900 switch (D->getKind()) { 10901 default: 10902 llvm_unreachable("Unexpected template parameter type"); 10903 case Decl::TemplateTypeParm: 10904 return TemplateTypeParameter; 10905 case Decl::NonTypeTemplateParm: 10906 return NonTypeTemplateParameter; 10907 case Decl::TemplateTemplateParm: 10908 return TemplateTemplateParameter; 10909 } 10910 }; 10911 10912 ODRDiagDeclError(FirstRecord, FirstModule, 10913 FirstTemplate->getLocation(), 10914 FirstTemplate->getSourceRange(), 10915 FunctionTemplateParameterDifferentKind) 10916 << FirstTemplate << (i + 1) << GetParamType(FirstParam); 10917 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10918 SecondTemplate->getSourceRange(), 10919 FunctionTemplateParameterDifferentKind) 10920 << SecondTemplate << (i + 1) << GetParamType(SecondParam); 10921 10922 ParameterMismatch = true; 10923 break; 10924 } 10925 10926 if (FirstParam->getName() != SecondParam->getName()) { 10927 ODRDiagDeclError( 10928 FirstRecord, FirstModule, FirstTemplate->getLocation(), 10929 FirstTemplate->getSourceRange(), FunctionTemplateParameterName) 10930 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier() 10931 << FirstParam; 10932 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10933 SecondTemplate->getSourceRange(), 10934 FunctionTemplateParameterName) 10935 << SecondTemplate << (i + 1) 10936 << (bool)SecondParam->getIdentifier() << SecondParam; 10937 ParameterMismatch = true; 10938 break; 10939 } 10940 10941 if (isa<TemplateTypeParmDecl>(FirstParam) && 10942 isa<TemplateTypeParmDecl>(SecondParam)) { 10943 TemplateTypeParmDecl *FirstTTPD = 10944 cast<TemplateTypeParmDecl>(FirstParam); 10945 TemplateTypeParmDecl *SecondTTPD = 10946 cast<TemplateTypeParmDecl>(SecondParam); 10947 bool HasFirstDefaultArgument = 10948 FirstTTPD->hasDefaultArgument() && 10949 !FirstTTPD->defaultArgumentWasInherited(); 10950 bool HasSecondDefaultArgument = 10951 SecondTTPD->hasDefaultArgument() && 10952 !SecondTTPD->defaultArgumentWasInherited(); 10953 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10954 ODRDiagDeclError(FirstRecord, FirstModule, 10955 FirstTemplate->getLocation(), 10956 FirstTemplate->getSourceRange(), 10957 FunctionTemplateParameterSingleDefaultArgument) 10958 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 10959 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10960 SecondTemplate->getSourceRange(), 10961 FunctionTemplateParameterSingleDefaultArgument) 10962 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 10963 ParameterMismatch = true; 10964 break; 10965 } 10966 10967 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 10968 QualType FirstType = FirstTTPD->getDefaultArgument(); 10969 QualType SecondType = SecondTTPD->getDefaultArgument(); 10970 if (ComputeQualTypeODRHash(FirstType) != 10971 ComputeQualTypeODRHash(SecondType)) { 10972 ODRDiagDeclError( 10973 FirstRecord, FirstModule, FirstTemplate->getLocation(), 10974 FirstTemplate->getSourceRange(), 10975 FunctionTemplateParameterDifferentDefaultArgument) 10976 << FirstTemplate << (i + 1) << FirstType; 10977 ODRDiagDeclNote( 10978 SecondModule, SecondTemplate->getLocation(), 10979 SecondTemplate->getSourceRange(), 10980 FunctionTemplateParameterDifferentDefaultArgument) 10981 << SecondTemplate << (i + 1) << SecondType; 10982 ParameterMismatch = true; 10983 break; 10984 } 10985 } 10986 10987 if (FirstTTPD->isParameterPack() != 10988 SecondTTPD->isParameterPack()) { 10989 ODRDiagDeclError(FirstRecord, FirstModule, 10990 FirstTemplate->getLocation(), 10991 FirstTemplate->getSourceRange(), 10992 FunctionTemplatePackParameter) 10993 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 10994 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10995 SecondTemplate->getSourceRange(), 10996 FunctionTemplatePackParameter) 10997 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 10998 ParameterMismatch = true; 10999 break; 11000 } 11001 } 11002 11003 if (isa<TemplateTemplateParmDecl>(FirstParam) && 11004 isa<TemplateTemplateParmDecl>(SecondParam)) { 11005 TemplateTemplateParmDecl *FirstTTPD = 11006 cast<TemplateTemplateParmDecl>(FirstParam); 11007 TemplateTemplateParmDecl *SecondTTPD = 11008 cast<TemplateTemplateParmDecl>(SecondParam); 11009 11010 TemplateParameterList *FirstTPL = 11011 FirstTTPD->getTemplateParameters(); 11012 TemplateParameterList *SecondTPL = 11013 SecondTTPD->getTemplateParameters(); 11014 11015 if (ComputeTemplateParameterListODRHash(FirstTPL) != 11016 ComputeTemplateParameterListODRHash(SecondTPL)) { 11017 ODRDiagDeclError(FirstRecord, FirstModule, 11018 FirstTemplate->getLocation(), 11019 FirstTemplate->getSourceRange(), 11020 FunctionTemplateParameterDifferentType) 11021 << FirstTemplate << (i + 1); 11022 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11023 SecondTemplate->getSourceRange(), 11024 FunctionTemplateParameterDifferentType) 11025 << SecondTemplate << (i + 1); 11026 ParameterMismatch = true; 11027 break; 11028 } 11029 11030 bool HasFirstDefaultArgument = 11031 FirstTTPD->hasDefaultArgument() && 11032 !FirstTTPD->defaultArgumentWasInherited(); 11033 bool HasSecondDefaultArgument = 11034 SecondTTPD->hasDefaultArgument() && 11035 !SecondTTPD->defaultArgumentWasInherited(); 11036 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11037 ODRDiagDeclError(FirstRecord, FirstModule, 11038 FirstTemplate->getLocation(), 11039 FirstTemplate->getSourceRange(), 11040 FunctionTemplateParameterSingleDefaultArgument) 11041 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11042 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11043 SecondTemplate->getSourceRange(), 11044 FunctionTemplateParameterSingleDefaultArgument) 11045 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11046 ParameterMismatch = true; 11047 break; 11048 } 11049 11050 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11051 TemplateArgument FirstTA = 11052 FirstTTPD->getDefaultArgument().getArgument(); 11053 TemplateArgument SecondTA = 11054 SecondTTPD->getDefaultArgument().getArgument(); 11055 if (ComputeTemplateArgumentODRHash(FirstTA) != 11056 ComputeTemplateArgumentODRHash(SecondTA)) { 11057 ODRDiagDeclError( 11058 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11059 FirstTemplate->getSourceRange(), 11060 FunctionTemplateParameterDifferentDefaultArgument) 11061 << FirstTemplate << (i + 1) << FirstTA; 11062 ODRDiagDeclNote( 11063 SecondModule, SecondTemplate->getLocation(), 11064 SecondTemplate->getSourceRange(), 11065 FunctionTemplateParameterDifferentDefaultArgument) 11066 << SecondTemplate << (i + 1) << SecondTA; 11067 ParameterMismatch = true; 11068 break; 11069 } 11070 } 11071 11072 if (FirstTTPD->isParameterPack() != 11073 SecondTTPD->isParameterPack()) { 11074 ODRDiagDeclError(FirstRecord, FirstModule, 11075 FirstTemplate->getLocation(), 11076 FirstTemplate->getSourceRange(), 11077 FunctionTemplatePackParameter) 11078 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 11079 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11080 SecondTemplate->getSourceRange(), 11081 FunctionTemplatePackParameter) 11082 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 11083 ParameterMismatch = true; 11084 break; 11085 } 11086 } 11087 11088 if (isa<NonTypeTemplateParmDecl>(FirstParam) && 11089 isa<NonTypeTemplateParmDecl>(SecondParam)) { 11090 NonTypeTemplateParmDecl *FirstNTTPD = 11091 cast<NonTypeTemplateParmDecl>(FirstParam); 11092 NonTypeTemplateParmDecl *SecondNTTPD = 11093 cast<NonTypeTemplateParmDecl>(SecondParam); 11094 11095 QualType FirstType = FirstNTTPD->getType(); 11096 QualType SecondType = SecondNTTPD->getType(); 11097 if (ComputeQualTypeODRHash(FirstType) != 11098 ComputeQualTypeODRHash(SecondType)) { 11099 ODRDiagDeclError(FirstRecord, FirstModule, 11100 FirstTemplate->getLocation(), 11101 FirstTemplate->getSourceRange(), 11102 FunctionTemplateParameterDifferentType) 11103 << FirstTemplate << (i + 1); 11104 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11105 SecondTemplate->getSourceRange(), 11106 FunctionTemplateParameterDifferentType) 11107 << SecondTemplate << (i + 1); 11108 ParameterMismatch = true; 11109 break; 11110 } 11111 11112 bool HasFirstDefaultArgument = 11113 FirstNTTPD->hasDefaultArgument() && 11114 !FirstNTTPD->defaultArgumentWasInherited(); 11115 bool HasSecondDefaultArgument = 11116 SecondNTTPD->hasDefaultArgument() && 11117 !SecondNTTPD->defaultArgumentWasInherited(); 11118 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11119 ODRDiagDeclError(FirstRecord, FirstModule, 11120 FirstTemplate->getLocation(), 11121 FirstTemplate->getSourceRange(), 11122 FunctionTemplateParameterSingleDefaultArgument) 11123 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11124 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11125 SecondTemplate->getSourceRange(), 11126 FunctionTemplateParameterSingleDefaultArgument) 11127 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11128 ParameterMismatch = true; 11129 break; 11130 } 11131 11132 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11133 Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument(); 11134 Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument(); 11135 if (ComputeODRHash(FirstDefaultArgument) != 11136 ComputeODRHash(SecondDefaultArgument)) { 11137 ODRDiagDeclError( 11138 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11139 FirstTemplate->getSourceRange(), 11140 FunctionTemplateParameterDifferentDefaultArgument) 11141 << FirstTemplate << (i + 1) << FirstDefaultArgument; 11142 ODRDiagDeclNote( 11143 SecondModule, SecondTemplate->getLocation(), 11144 SecondTemplate->getSourceRange(), 11145 FunctionTemplateParameterDifferentDefaultArgument) 11146 << SecondTemplate << (i + 1) << SecondDefaultArgument; 11147 ParameterMismatch = true; 11148 break; 11149 } 11150 } 11151 11152 if (FirstNTTPD->isParameterPack() != 11153 SecondNTTPD->isParameterPack()) { 11154 ODRDiagDeclError(FirstRecord, FirstModule, 11155 FirstTemplate->getLocation(), 11156 FirstTemplate->getSourceRange(), 11157 FunctionTemplatePackParameter) 11158 << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack(); 11159 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11160 SecondTemplate->getSourceRange(), 11161 FunctionTemplatePackParameter) 11162 << SecondTemplate << (i + 1) 11163 << SecondNTTPD->isParameterPack(); 11164 ParameterMismatch = true; 11165 break; 11166 } 11167 } 11168 } 11169 11170 if (ParameterMismatch) { 11171 Diagnosed = true; 11172 break; 11173 } 11174 11175 break; 11176 } 11177 } 11178 11179 if (Diagnosed) 11180 continue; 11181 11182 Diag(FirstDecl->getLocation(), 11183 diag::err_module_odr_violation_mismatch_decl_unknown) 11184 << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType 11185 << FirstDecl->getSourceRange(); 11186 Diag(SecondDecl->getLocation(), 11187 diag::note_module_odr_violation_mismatch_decl_unknown) 11188 << SecondModule << FirstDiffType << SecondDecl->getSourceRange(); 11189 Diagnosed = true; 11190 } 11191 11192 if (!Diagnosed) { 11193 // All definitions are updates to the same declaration. This happens if a 11194 // module instantiates the declaration of a class template specialization 11195 // and two or more other modules instantiate its definition. 11196 // 11197 // FIXME: Indicate which modules had instantiations of this definition. 11198 // FIXME: How can this even happen? 11199 Diag(Merge.first->getLocation(), 11200 diag::err_module_odr_violation_different_instantiations) 11201 << Merge.first; 11202 } 11203 } 11204 11205 // Issue ODR failures diagnostics for functions. 11206 for (auto &Merge : FunctionOdrMergeFailures) { 11207 enum ODRFunctionDifference { 11208 ReturnType, 11209 ParameterName, 11210 ParameterType, 11211 ParameterSingleDefaultArgument, 11212 ParameterDifferentDefaultArgument, 11213 FunctionBody, 11214 }; 11215 11216 FunctionDecl *FirstFunction = Merge.first; 11217 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction); 11218 11219 bool Diagnosed = false; 11220 for (auto &SecondFunction : Merge.second) { 11221 11222 if (FirstFunction == SecondFunction) 11223 continue; 11224 11225 std::string SecondModule = 11226 getOwningModuleNameForDiagnostic(SecondFunction); 11227 11228 auto ODRDiagError = [FirstFunction, &FirstModule, 11229 this](SourceLocation Loc, SourceRange Range, 11230 ODRFunctionDifference DiffType) { 11231 return Diag(Loc, diag::err_module_odr_violation_function) 11232 << FirstFunction << FirstModule.empty() << FirstModule << Range 11233 << DiffType; 11234 }; 11235 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11236 SourceRange Range, 11237 ODRFunctionDifference DiffType) { 11238 return Diag(Loc, diag::note_module_odr_violation_function) 11239 << SecondModule << Range << DiffType; 11240 }; 11241 11242 if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) != 11243 ComputeQualTypeODRHash(SecondFunction->getReturnType())) { 11244 ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(), 11245 FirstFunction->getReturnTypeSourceRange(), ReturnType) 11246 << FirstFunction->getReturnType(); 11247 ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(), 11248 SecondFunction->getReturnTypeSourceRange(), ReturnType) 11249 << SecondFunction->getReturnType(); 11250 Diagnosed = true; 11251 break; 11252 } 11253 11254 assert(FirstFunction->param_size() == SecondFunction->param_size() && 11255 "Merged functions with different number of parameters"); 11256 11257 auto ParamSize = FirstFunction->param_size(); 11258 bool ParameterMismatch = false; 11259 for (unsigned I = 0; I < ParamSize; ++I) { 11260 auto *FirstParam = FirstFunction->getParamDecl(I); 11261 auto *SecondParam = SecondFunction->getParamDecl(I); 11262 11263 assert(getContext().hasSameType(FirstParam->getType(), 11264 SecondParam->getType()) && 11265 "Merged function has different parameter types."); 11266 11267 if (FirstParam->getDeclName() != SecondParam->getDeclName()) { 11268 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11269 ParameterName) 11270 << I + 1 << FirstParam->getDeclName(); 11271 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11272 ParameterName) 11273 << I + 1 << SecondParam->getDeclName(); 11274 ParameterMismatch = true; 11275 break; 11276 }; 11277 11278 QualType FirstParamType = FirstParam->getType(); 11279 QualType SecondParamType = SecondParam->getType(); 11280 if (FirstParamType != SecondParamType && 11281 ComputeQualTypeODRHash(FirstParamType) != 11282 ComputeQualTypeODRHash(SecondParamType)) { 11283 if (const DecayedType *ParamDecayedType = 11284 FirstParamType->getAs<DecayedType>()) { 11285 ODRDiagError(FirstParam->getLocation(), 11286 FirstParam->getSourceRange(), ParameterType) 11287 << (I + 1) << FirstParamType << true 11288 << ParamDecayedType->getOriginalType(); 11289 } else { 11290 ODRDiagError(FirstParam->getLocation(), 11291 FirstParam->getSourceRange(), ParameterType) 11292 << (I + 1) << FirstParamType << false; 11293 } 11294 11295 if (const DecayedType *ParamDecayedType = 11296 SecondParamType->getAs<DecayedType>()) { 11297 ODRDiagNote(SecondParam->getLocation(), 11298 SecondParam->getSourceRange(), ParameterType) 11299 << (I + 1) << SecondParamType << true 11300 << ParamDecayedType->getOriginalType(); 11301 } else { 11302 ODRDiagNote(SecondParam->getLocation(), 11303 SecondParam->getSourceRange(), ParameterType) 11304 << (I + 1) << SecondParamType << false; 11305 } 11306 ParameterMismatch = true; 11307 break; 11308 } 11309 11310 const Expr *FirstInit = FirstParam->getInit(); 11311 const Expr *SecondInit = SecondParam->getInit(); 11312 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 11313 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11314 ParameterSingleDefaultArgument) 11315 << (I + 1) << (FirstInit == nullptr) 11316 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 11317 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11318 ParameterSingleDefaultArgument) 11319 << (I + 1) << (SecondInit == nullptr) 11320 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 11321 ParameterMismatch = true; 11322 break; 11323 } 11324 11325 if (FirstInit && SecondInit && 11326 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11327 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11328 ParameterDifferentDefaultArgument) 11329 << (I + 1) << FirstInit->getSourceRange(); 11330 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11331 ParameterDifferentDefaultArgument) 11332 << (I + 1) << SecondInit->getSourceRange(); 11333 ParameterMismatch = true; 11334 break; 11335 } 11336 11337 assert(ComputeSubDeclODRHash(FirstParam) == 11338 ComputeSubDeclODRHash(SecondParam) && 11339 "Undiagnosed parameter difference."); 11340 } 11341 11342 if (ParameterMismatch) { 11343 Diagnosed = true; 11344 break; 11345 } 11346 11347 // If no error has been generated before now, assume the problem is in 11348 // the body and generate a message. 11349 ODRDiagError(FirstFunction->getLocation(), 11350 FirstFunction->getSourceRange(), FunctionBody); 11351 ODRDiagNote(SecondFunction->getLocation(), 11352 SecondFunction->getSourceRange(), FunctionBody); 11353 Diagnosed = true; 11354 break; 11355 } 11356 (void)Diagnosed; 11357 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11358 } 11359 11360 // Issue ODR failures diagnostics for enums. 11361 for (auto &Merge : EnumOdrMergeFailures) { 11362 enum ODREnumDifference { 11363 SingleScopedEnum, 11364 EnumTagKeywordMismatch, 11365 SingleSpecifiedType, 11366 DifferentSpecifiedTypes, 11367 DifferentNumberEnumConstants, 11368 EnumConstantName, 11369 EnumConstantSingleInitilizer, 11370 EnumConstantDifferentInitilizer, 11371 }; 11372 11373 // If we've already pointed out a specific problem with this enum, don't 11374 // bother issuing a general "something's different" diagnostic. 11375 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 11376 continue; 11377 11378 EnumDecl *FirstEnum = Merge.first; 11379 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum); 11380 11381 using DeclHashes = 11382 llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>; 11383 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum]( 11384 DeclHashes &Hashes, EnumDecl *Enum) { 11385 for (auto *D : Enum->decls()) { 11386 // Due to decl merging, the first EnumDecl is the parent of 11387 // Decls in both records. 11388 if (!ODRHash::isDeclToBeProcessed(D, FirstEnum)) 11389 continue; 11390 assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind"); 11391 Hashes.emplace_back(cast<EnumConstantDecl>(D), 11392 ComputeSubDeclODRHash(D)); 11393 } 11394 }; 11395 DeclHashes FirstHashes; 11396 PopulateHashes(FirstHashes, FirstEnum); 11397 bool Diagnosed = false; 11398 for (auto &SecondEnum : Merge.second) { 11399 11400 if (FirstEnum == SecondEnum) 11401 continue; 11402 11403 std::string SecondModule = 11404 getOwningModuleNameForDiagnostic(SecondEnum); 11405 11406 auto ODRDiagError = [FirstEnum, &FirstModule, 11407 this](SourceLocation Loc, SourceRange Range, 11408 ODREnumDifference DiffType) { 11409 return Diag(Loc, diag::err_module_odr_violation_enum) 11410 << FirstEnum << FirstModule.empty() << FirstModule << Range 11411 << DiffType; 11412 }; 11413 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11414 SourceRange Range, 11415 ODREnumDifference DiffType) { 11416 return Diag(Loc, diag::note_module_odr_violation_enum) 11417 << SecondModule << Range << DiffType; 11418 }; 11419 11420 if (FirstEnum->isScoped() != SecondEnum->isScoped()) { 11421 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11422 SingleScopedEnum) 11423 << FirstEnum->isScoped(); 11424 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11425 SingleScopedEnum) 11426 << SecondEnum->isScoped(); 11427 Diagnosed = true; 11428 continue; 11429 } 11430 11431 if (FirstEnum->isScoped() && SecondEnum->isScoped()) { 11432 if (FirstEnum->isScopedUsingClassTag() != 11433 SecondEnum->isScopedUsingClassTag()) { 11434 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11435 EnumTagKeywordMismatch) 11436 << FirstEnum->isScopedUsingClassTag(); 11437 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11438 EnumTagKeywordMismatch) 11439 << SecondEnum->isScopedUsingClassTag(); 11440 Diagnosed = true; 11441 continue; 11442 } 11443 } 11444 11445 QualType FirstUnderlyingType = 11446 FirstEnum->getIntegerTypeSourceInfo() 11447 ? FirstEnum->getIntegerTypeSourceInfo()->getType() 11448 : QualType(); 11449 QualType SecondUnderlyingType = 11450 SecondEnum->getIntegerTypeSourceInfo() 11451 ? SecondEnum->getIntegerTypeSourceInfo()->getType() 11452 : QualType(); 11453 if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) { 11454 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11455 SingleSpecifiedType) 11456 << !FirstUnderlyingType.isNull(); 11457 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11458 SingleSpecifiedType) 11459 << !SecondUnderlyingType.isNull(); 11460 Diagnosed = true; 11461 continue; 11462 } 11463 11464 if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) { 11465 if (ComputeQualTypeODRHash(FirstUnderlyingType) != 11466 ComputeQualTypeODRHash(SecondUnderlyingType)) { 11467 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11468 DifferentSpecifiedTypes) 11469 << FirstUnderlyingType; 11470 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11471 DifferentSpecifiedTypes) 11472 << SecondUnderlyingType; 11473 Diagnosed = true; 11474 continue; 11475 } 11476 } 11477 11478 DeclHashes SecondHashes; 11479 PopulateHashes(SecondHashes, SecondEnum); 11480 11481 if (FirstHashes.size() != SecondHashes.size()) { 11482 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11483 DifferentNumberEnumConstants) 11484 << (int)FirstHashes.size(); 11485 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11486 DifferentNumberEnumConstants) 11487 << (int)SecondHashes.size(); 11488 Diagnosed = true; 11489 continue; 11490 } 11491 11492 for (unsigned I = 0; I < FirstHashes.size(); ++I) { 11493 if (FirstHashes[I].second == SecondHashes[I].second) 11494 continue; 11495 const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first; 11496 const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first; 11497 11498 if (FirstEnumConstant->getDeclName() != 11499 SecondEnumConstant->getDeclName()) { 11500 11501 ODRDiagError(FirstEnumConstant->getLocation(), 11502 FirstEnumConstant->getSourceRange(), EnumConstantName) 11503 << I + 1 << FirstEnumConstant; 11504 ODRDiagNote(SecondEnumConstant->getLocation(), 11505 SecondEnumConstant->getSourceRange(), EnumConstantName) 11506 << I + 1 << SecondEnumConstant; 11507 Diagnosed = true; 11508 break; 11509 } 11510 11511 const Expr *FirstInit = FirstEnumConstant->getInitExpr(); 11512 const Expr *SecondInit = SecondEnumConstant->getInitExpr(); 11513 if (!FirstInit && !SecondInit) 11514 continue; 11515 11516 if (!FirstInit || !SecondInit) { 11517 ODRDiagError(FirstEnumConstant->getLocation(), 11518 FirstEnumConstant->getSourceRange(), 11519 EnumConstantSingleInitilizer) 11520 << I + 1 << FirstEnumConstant << (FirstInit != nullptr); 11521 ODRDiagNote(SecondEnumConstant->getLocation(), 11522 SecondEnumConstant->getSourceRange(), 11523 EnumConstantSingleInitilizer) 11524 << I + 1 << SecondEnumConstant << (SecondInit != nullptr); 11525 Diagnosed = true; 11526 break; 11527 } 11528 11529 if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11530 ODRDiagError(FirstEnumConstant->getLocation(), 11531 FirstEnumConstant->getSourceRange(), 11532 EnumConstantDifferentInitilizer) 11533 << I + 1 << FirstEnumConstant; 11534 ODRDiagNote(SecondEnumConstant->getLocation(), 11535 SecondEnumConstant->getSourceRange(), 11536 EnumConstantDifferentInitilizer) 11537 << I + 1 << SecondEnumConstant; 11538 Diagnosed = true; 11539 break; 11540 } 11541 } 11542 } 11543 11544 (void)Diagnosed; 11545 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11546 } 11547 } 11548 11549 void ASTReader::StartedDeserializing() { 11550 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get()) 11551 ReadTimer->startTimer(); 11552 } 11553 11554 void ASTReader::FinishedDeserializing() { 11555 assert(NumCurrentElementsDeserializing && 11556 "FinishedDeserializing not paired with StartedDeserializing"); 11557 if (NumCurrentElementsDeserializing == 1) { 11558 // We decrease NumCurrentElementsDeserializing only after pending actions 11559 // are finished, to avoid recursively re-calling finishPendingActions(). 11560 finishPendingActions(); 11561 } 11562 --NumCurrentElementsDeserializing; 11563 11564 if (NumCurrentElementsDeserializing == 0) { 11565 // Propagate exception specification and deduced type updates along 11566 // redeclaration chains. 11567 // 11568 // We do this now rather than in finishPendingActions because we want to 11569 // be able to walk the complete redeclaration chains of the updated decls. 11570 while (!PendingExceptionSpecUpdates.empty() || 11571 !PendingDeducedTypeUpdates.empty()) { 11572 auto ESUpdates = std::move(PendingExceptionSpecUpdates); 11573 PendingExceptionSpecUpdates.clear(); 11574 for (auto Update : ESUpdates) { 11575 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11576 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>(); 11577 auto ESI = FPT->getExtProtoInfo().ExceptionSpec; 11578 if (auto *Listener = getContext().getASTMutationListener()) 11579 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second)); 11580 for (auto *Redecl : Update.second->redecls()) 11581 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI); 11582 } 11583 11584 auto DTUpdates = std::move(PendingDeducedTypeUpdates); 11585 PendingDeducedTypeUpdates.clear(); 11586 for (auto Update : DTUpdates) { 11587 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11588 // FIXME: If the return type is already deduced, check that it matches. 11589 getContext().adjustDeducedFunctionResultType(Update.first, 11590 Update.second); 11591 } 11592 } 11593 11594 if (ReadTimer) 11595 ReadTimer->stopTimer(); 11596 11597 diagnoseOdrViolations(); 11598 11599 // We are not in recursive loading, so it's safe to pass the "interesting" 11600 // decls to the consumer. 11601 if (Consumer) 11602 PassInterestingDeclsToConsumer(); 11603 } 11604 } 11605 11606 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) { 11607 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) { 11608 // Remove any fake results before adding any real ones. 11609 auto It = PendingFakeLookupResults.find(II); 11610 if (It != PendingFakeLookupResults.end()) { 11611 for (auto *ND : It->second) 11612 SemaObj->IdResolver.RemoveDecl(ND); 11613 // FIXME: this works around module+PCH performance issue. 11614 // Rather than erase the result from the map, which is O(n), just clear 11615 // the vector of NamedDecls. 11616 It->second.clear(); 11617 } 11618 } 11619 11620 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) { 11621 SemaObj->TUScope->AddDecl(D); 11622 } else if (SemaObj->TUScope) { 11623 // Adding the decl to IdResolver may have failed because it was already in 11624 // (even though it was not added in scope). If it is already in, make sure 11625 // it gets in the scope as well. 11626 if (std::find(SemaObj->IdResolver.begin(Name), 11627 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end()) 11628 SemaObj->TUScope->AddDecl(D); 11629 } 11630 } 11631 11632 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache, 11633 ASTContext *Context, 11634 const PCHContainerReader &PCHContainerRdr, 11635 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, 11636 StringRef isysroot, 11637 DisableValidationForModuleKind DisableValidationKind, 11638 bool AllowASTWithCompilerErrors, 11639 bool AllowConfigurationMismatch, bool ValidateSystemInputs, 11640 bool ValidateASTInputFilesContent, bool UseGlobalIndex, 11641 std::unique_ptr<llvm::Timer> ReadTimer) 11642 : Listener(bool(DisableValidationKind &DisableValidationForModuleKind::PCH) 11643 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP)) 11644 : cast<ASTReaderListener>(new PCHValidator(PP, *this))), 11645 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), 11646 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP), 11647 ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache, 11648 PCHContainerRdr, PP.getHeaderSearchInfo()), 11649 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot), 11650 DisableValidationKind(DisableValidationKind), 11651 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors), 11652 AllowConfigurationMismatch(AllowConfigurationMismatch), 11653 ValidateSystemInputs(ValidateSystemInputs), 11654 ValidateASTInputFilesContent(ValidateASTInputFilesContent), 11655 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) { 11656 SourceMgr.setExternalSLocEntrySource(this); 11657 11658 for (const auto &Ext : Extensions) { 11659 auto BlockName = Ext->getExtensionMetadata().BlockName; 11660 auto Known = ModuleFileExtensions.find(BlockName); 11661 if (Known != ModuleFileExtensions.end()) { 11662 Diags.Report(diag::warn_duplicate_module_file_extension) 11663 << BlockName; 11664 continue; 11665 } 11666 11667 ModuleFileExtensions.insert({BlockName, Ext}); 11668 } 11669 } 11670 11671 ASTReader::~ASTReader() { 11672 if (OwnsDeserializationListener) 11673 delete DeserializationListener; 11674 } 11675 11676 IdentifierResolver &ASTReader::getIdResolver() { 11677 return SemaObj ? SemaObj->IdResolver : DummyIdResolver; 11678 } 11679 11680 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor, 11681 unsigned AbbrevID) { 11682 Idx = 0; 11683 Record.clear(); 11684 return Cursor.readRecord(AbbrevID, Record); 11685 } 11686 //===----------------------------------------------------------------------===// 11687 //// OMPClauseReader implementation 11688 ////===----------------------------------------------------------------------===// 11689 11690 // This has to be in namespace clang because it's friended by all 11691 // of the OMP clauses. 11692 namespace clang { 11693 11694 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> { 11695 ASTRecordReader &Record; 11696 ASTContext &Context; 11697 11698 public: 11699 OMPClauseReader(ASTRecordReader &Record) 11700 : Record(Record), Context(Record.getContext()) {} 11701 #define GEN_CLANG_CLAUSE_CLASS 11702 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C); 11703 #include "llvm/Frontend/OpenMP/OMP.inc" 11704 OMPClause *readClause(); 11705 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C); 11706 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C); 11707 }; 11708 11709 } // end namespace clang 11710 11711 OMPClause *ASTRecordReader::readOMPClause() { 11712 return OMPClauseReader(*this).readClause(); 11713 } 11714 11715 OMPClause *OMPClauseReader::readClause() { 11716 OMPClause *C = nullptr; 11717 switch (llvm::omp::Clause(Record.readInt())) { 11718 case llvm::omp::OMPC_if: 11719 C = new (Context) OMPIfClause(); 11720 break; 11721 case llvm::omp::OMPC_final: 11722 C = new (Context) OMPFinalClause(); 11723 break; 11724 case llvm::omp::OMPC_num_threads: 11725 C = new (Context) OMPNumThreadsClause(); 11726 break; 11727 case llvm::omp::OMPC_safelen: 11728 C = new (Context) OMPSafelenClause(); 11729 break; 11730 case llvm::omp::OMPC_simdlen: 11731 C = new (Context) OMPSimdlenClause(); 11732 break; 11733 case llvm::omp::OMPC_sizes: { 11734 unsigned NumSizes = Record.readInt(); 11735 C = OMPSizesClause::CreateEmpty(Context, NumSizes); 11736 break; 11737 } 11738 case llvm::omp::OMPC_allocator: 11739 C = new (Context) OMPAllocatorClause(); 11740 break; 11741 case llvm::omp::OMPC_collapse: 11742 C = new (Context) OMPCollapseClause(); 11743 break; 11744 case llvm::omp::OMPC_default: 11745 C = new (Context) OMPDefaultClause(); 11746 break; 11747 case llvm::omp::OMPC_proc_bind: 11748 C = new (Context) OMPProcBindClause(); 11749 break; 11750 case llvm::omp::OMPC_schedule: 11751 C = new (Context) OMPScheduleClause(); 11752 break; 11753 case llvm::omp::OMPC_ordered: 11754 C = OMPOrderedClause::CreateEmpty(Context, Record.readInt()); 11755 break; 11756 case llvm::omp::OMPC_nowait: 11757 C = new (Context) OMPNowaitClause(); 11758 break; 11759 case llvm::omp::OMPC_untied: 11760 C = new (Context) OMPUntiedClause(); 11761 break; 11762 case llvm::omp::OMPC_mergeable: 11763 C = new (Context) OMPMergeableClause(); 11764 break; 11765 case llvm::omp::OMPC_read: 11766 C = new (Context) OMPReadClause(); 11767 break; 11768 case llvm::omp::OMPC_write: 11769 C = new (Context) OMPWriteClause(); 11770 break; 11771 case llvm::omp::OMPC_update: 11772 C = OMPUpdateClause::CreateEmpty(Context, Record.readInt()); 11773 break; 11774 case llvm::omp::OMPC_capture: 11775 C = new (Context) OMPCaptureClause(); 11776 break; 11777 case llvm::omp::OMPC_seq_cst: 11778 C = new (Context) OMPSeqCstClause(); 11779 break; 11780 case llvm::omp::OMPC_acq_rel: 11781 C = new (Context) OMPAcqRelClause(); 11782 break; 11783 case llvm::omp::OMPC_acquire: 11784 C = new (Context) OMPAcquireClause(); 11785 break; 11786 case llvm::omp::OMPC_release: 11787 C = new (Context) OMPReleaseClause(); 11788 break; 11789 case llvm::omp::OMPC_relaxed: 11790 C = new (Context) OMPRelaxedClause(); 11791 break; 11792 case llvm::omp::OMPC_threads: 11793 C = new (Context) OMPThreadsClause(); 11794 break; 11795 case llvm::omp::OMPC_simd: 11796 C = new (Context) OMPSIMDClause(); 11797 break; 11798 case llvm::omp::OMPC_nogroup: 11799 C = new (Context) OMPNogroupClause(); 11800 break; 11801 case llvm::omp::OMPC_unified_address: 11802 C = new (Context) OMPUnifiedAddressClause(); 11803 break; 11804 case llvm::omp::OMPC_unified_shared_memory: 11805 C = new (Context) OMPUnifiedSharedMemoryClause(); 11806 break; 11807 case llvm::omp::OMPC_reverse_offload: 11808 C = new (Context) OMPReverseOffloadClause(); 11809 break; 11810 case llvm::omp::OMPC_dynamic_allocators: 11811 C = new (Context) OMPDynamicAllocatorsClause(); 11812 break; 11813 case llvm::omp::OMPC_atomic_default_mem_order: 11814 C = new (Context) OMPAtomicDefaultMemOrderClause(); 11815 break; 11816 case llvm::omp::OMPC_private: 11817 C = OMPPrivateClause::CreateEmpty(Context, Record.readInt()); 11818 break; 11819 case llvm::omp::OMPC_firstprivate: 11820 C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt()); 11821 break; 11822 case llvm::omp::OMPC_lastprivate: 11823 C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt()); 11824 break; 11825 case llvm::omp::OMPC_shared: 11826 C = OMPSharedClause::CreateEmpty(Context, Record.readInt()); 11827 break; 11828 case llvm::omp::OMPC_reduction: { 11829 unsigned N = Record.readInt(); 11830 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>(); 11831 C = OMPReductionClause::CreateEmpty(Context, N, Modifier); 11832 break; 11833 } 11834 case llvm::omp::OMPC_task_reduction: 11835 C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt()); 11836 break; 11837 case llvm::omp::OMPC_in_reduction: 11838 C = OMPInReductionClause::CreateEmpty(Context, Record.readInt()); 11839 break; 11840 case llvm::omp::OMPC_linear: 11841 C = OMPLinearClause::CreateEmpty(Context, Record.readInt()); 11842 break; 11843 case llvm::omp::OMPC_aligned: 11844 C = OMPAlignedClause::CreateEmpty(Context, Record.readInt()); 11845 break; 11846 case llvm::omp::OMPC_copyin: 11847 C = OMPCopyinClause::CreateEmpty(Context, Record.readInt()); 11848 break; 11849 case llvm::omp::OMPC_copyprivate: 11850 C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt()); 11851 break; 11852 case llvm::omp::OMPC_flush: 11853 C = OMPFlushClause::CreateEmpty(Context, Record.readInt()); 11854 break; 11855 case llvm::omp::OMPC_depobj: 11856 C = OMPDepobjClause::CreateEmpty(Context); 11857 break; 11858 case llvm::omp::OMPC_depend: { 11859 unsigned NumVars = Record.readInt(); 11860 unsigned NumLoops = Record.readInt(); 11861 C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops); 11862 break; 11863 } 11864 case llvm::omp::OMPC_device: 11865 C = new (Context) OMPDeviceClause(); 11866 break; 11867 case llvm::omp::OMPC_map: { 11868 OMPMappableExprListSizeTy Sizes; 11869 Sizes.NumVars = Record.readInt(); 11870 Sizes.NumUniqueDeclarations = Record.readInt(); 11871 Sizes.NumComponentLists = Record.readInt(); 11872 Sizes.NumComponents = Record.readInt(); 11873 C = OMPMapClause::CreateEmpty(Context, Sizes); 11874 break; 11875 } 11876 case llvm::omp::OMPC_num_teams: 11877 C = new (Context) OMPNumTeamsClause(); 11878 break; 11879 case llvm::omp::OMPC_thread_limit: 11880 C = new (Context) OMPThreadLimitClause(); 11881 break; 11882 case llvm::omp::OMPC_priority: 11883 C = new (Context) OMPPriorityClause(); 11884 break; 11885 case llvm::omp::OMPC_grainsize: 11886 C = new (Context) OMPGrainsizeClause(); 11887 break; 11888 case llvm::omp::OMPC_num_tasks: 11889 C = new (Context) OMPNumTasksClause(); 11890 break; 11891 case llvm::omp::OMPC_hint: 11892 C = new (Context) OMPHintClause(); 11893 break; 11894 case llvm::omp::OMPC_dist_schedule: 11895 C = new (Context) OMPDistScheduleClause(); 11896 break; 11897 case llvm::omp::OMPC_defaultmap: 11898 C = new (Context) OMPDefaultmapClause(); 11899 break; 11900 case llvm::omp::OMPC_to: { 11901 OMPMappableExprListSizeTy Sizes; 11902 Sizes.NumVars = Record.readInt(); 11903 Sizes.NumUniqueDeclarations = Record.readInt(); 11904 Sizes.NumComponentLists = Record.readInt(); 11905 Sizes.NumComponents = Record.readInt(); 11906 C = OMPToClause::CreateEmpty(Context, Sizes); 11907 break; 11908 } 11909 case llvm::omp::OMPC_from: { 11910 OMPMappableExprListSizeTy Sizes; 11911 Sizes.NumVars = Record.readInt(); 11912 Sizes.NumUniqueDeclarations = Record.readInt(); 11913 Sizes.NumComponentLists = Record.readInt(); 11914 Sizes.NumComponents = Record.readInt(); 11915 C = OMPFromClause::CreateEmpty(Context, Sizes); 11916 break; 11917 } 11918 case llvm::omp::OMPC_use_device_ptr: { 11919 OMPMappableExprListSizeTy Sizes; 11920 Sizes.NumVars = Record.readInt(); 11921 Sizes.NumUniqueDeclarations = Record.readInt(); 11922 Sizes.NumComponentLists = Record.readInt(); 11923 Sizes.NumComponents = Record.readInt(); 11924 C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes); 11925 break; 11926 } 11927 case llvm::omp::OMPC_use_device_addr: { 11928 OMPMappableExprListSizeTy Sizes; 11929 Sizes.NumVars = Record.readInt(); 11930 Sizes.NumUniqueDeclarations = Record.readInt(); 11931 Sizes.NumComponentLists = Record.readInt(); 11932 Sizes.NumComponents = Record.readInt(); 11933 C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes); 11934 break; 11935 } 11936 case llvm::omp::OMPC_is_device_ptr: { 11937 OMPMappableExprListSizeTy Sizes; 11938 Sizes.NumVars = Record.readInt(); 11939 Sizes.NumUniqueDeclarations = Record.readInt(); 11940 Sizes.NumComponentLists = Record.readInt(); 11941 Sizes.NumComponents = Record.readInt(); 11942 C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes); 11943 break; 11944 } 11945 case llvm::omp::OMPC_allocate: 11946 C = OMPAllocateClause::CreateEmpty(Context, Record.readInt()); 11947 break; 11948 case llvm::omp::OMPC_nontemporal: 11949 C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt()); 11950 break; 11951 case llvm::omp::OMPC_inclusive: 11952 C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt()); 11953 break; 11954 case llvm::omp::OMPC_exclusive: 11955 C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt()); 11956 break; 11957 case llvm::omp::OMPC_order: 11958 C = new (Context) OMPOrderClause(); 11959 break; 11960 case llvm::omp::OMPC_destroy: 11961 C = new (Context) OMPDestroyClause(); 11962 break; 11963 case llvm::omp::OMPC_detach: 11964 C = new (Context) OMPDetachClause(); 11965 break; 11966 case llvm::omp::OMPC_uses_allocators: 11967 C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt()); 11968 break; 11969 case llvm::omp::OMPC_affinity: 11970 C = OMPAffinityClause::CreateEmpty(Context, Record.readInt()); 11971 break; 11972 #define OMP_CLAUSE_NO_CLASS(Enum, Str) \ 11973 case llvm::omp::Enum: \ 11974 break; 11975 #include "llvm/Frontend/OpenMP/OMPKinds.def" 11976 default: 11977 break; 11978 } 11979 assert(C && "Unknown OMPClause type"); 11980 11981 Visit(C); 11982 C->setLocStart(Record.readSourceLocation()); 11983 C->setLocEnd(Record.readSourceLocation()); 11984 11985 return C; 11986 } 11987 11988 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) { 11989 C->setPreInitStmt(Record.readSubStmt(), 11990 static_cast<OpenMPDirectiveKind>(Record.readInt())); 11991 } 11992 11993 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) { 11994 VisitOMPClauseWithPreInit(C); 11995 C->setPostUpdateExpr(Record.readSubExpr()); 11996 } 11997 11998 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) { 11999 VisitOMPClauseWithPreInit(C); 12000 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt())); 12001 C->setNameModifierLoc(Record.readSourceLocation()); 12002 C->setColonLoc(Record.readSourceLocation()); 12003 C->setCondition(Record.readSubExpr()); 12004 C->setLParenLoc(Record.readSourceLocation()); 12005 } 12006 12007 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) { 12008 VisitOMPClauseWithPreInit(C); 12009 C->setCondition(Record.readSubExpr()); 12010 C->setLParenLoc(Record.readSourceLocation()); 12011 } 12012 12013 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) { 12014 VisitOMPClauseWithPreInit(C); 12015 C->setNumThreads(Record.readSubExpr()); 12016 C->setLParenLoc(Record.readSourceLocation()); 12017 } 12018 12019 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) { 12020 C->setSafelen(Record.readSubExpr()); 12021 C->setLParenLoc(Record.readSourceLocation()); 12022 } 12023 12024 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) { 12025 C->setSimdlen(Record.readSubExpr()); 12026 C->setLParenLoc(Record.readSourceLocation()); 12027 } 12028 12029 void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) { 12030 for (Expr *&E : C->getSizesRefs()) 12031 E = Record.readSubExpr(); 12032 C->setLParenLoc(Record.readSourceLocation()); 12033 } 12034 12035 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) { 12036 C->setAllocator(Record.readExpr()); 12037 C->setLParenLoc(Record.readSourceLocation()); 12038 } 12039 12040 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) { 12041 C->setNumForLoops(Record.readSubExpr()); 12042 C->setLParenLoc(Record.readSourceLocation()); 12043 } 12044 12045 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) { 12046 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt())); 12047 C->setLParenLoc(Record.readSourceLocation()); 12048 C->setDefaultKindKwLoc(Record.readSourceLocation()); 12049 } 12050 12051 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) { 12052 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt())); 12053 C->setLParenLoc(Record.readSourceLocation()); 12054 C->setProcBindKindKwLoc(Record.readSourceLocation()); 12055 } 12056 12057 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) { 12058 VisitOMPClauseWithPreInit(C); 12059 C->setScheduleKind( 12060 static_cast<OpenMPScheduleClauseKind>(Record.readInt())); 12061 C->setFirstScheduleModifier( 12062 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 12063 C->setSecondScheduleModifier( 12064 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 12065 C->setChunkSize(Record.readSubExpr()); 12066 C->setLParenLoc(Record.readSourceLocation()); 12067 C->setFirstScheduleModifierLoc(Record.readSourceLocation()); 12068 C->setSecondScheduleModifierLoc(Record.readSourceLocation()); 12069 C->setScheduleKindLoc(Record.readSourceLocation()); 12070 C->setCommaLoc(Record.readSourceLocation()); 12071 } 12072 12073 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) { 12074 C->setNumForLoops(Record.readSubExpr()); 12075 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 12076 C->setLoopNumIterations(I, Record.readSubExpr()); 12077 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 12078 C->setLoopCounter(I, Record.readSubExpr()); 12079 C->setLParenLoc(Record.readSourceLocation()); 12080 } 12081 12082 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) { 12083 C->setEventHandler(Record.readSubExpr()); 12084 C->setLParenLoc(Record.readSourceLocation()); 12085 } 12086 12087 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {} 12088 12089 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {} 12090 12091 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {} 12092 12093 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {} 12094 12095 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {} 12096 12097 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) { 12098 if (C->isExtended()) { 12099 C->setLParenLoc(Record.readSourceLocation()); 12100 C->setArgumentLoc(Record.readSourceLocation()); 12101 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>()); 12102 } 12103 } 12104 12105 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {} 12106 12107 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {} 12108 12109 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {} 12110 12111 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {} 12112 12113 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {} 12114 12115 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {} 12116 12117 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {} 12118 12119 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {} 12120 12121 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {} 12122 12123 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *) {} 12124 12125 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {} 12126 12127 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause( 12128 OMPUnifiedSharedMemoryClause *) {} 12129 12130 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {} 12131 12132 void 12133 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) { 12134 } 12135 12136 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause( 12137 OMPAtomicDefaultMemOrderClause *C) { 12138 C->setAtomicDefaultMemOrderKind( 12139 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt())); 12140 C->setLParenLoc(Record.readSourceLocation()); 12141 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation()); 12142 } 12143 12144 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) { 12145 C->setLParenLoc(Record.readSourceLocation()); 12146 unsigned NumVars = C->varlist_size(); 12147 SmallVector<Expr *, 16> Vars; 12148 Vars.reserve(NumVars); 12149 for (unsigned i = 0; i != NumVars; ++i) 12150 Vars.push_back(Record.readSubExpr()); 12151 C->setVarRefs(Vars); 12152 Vars.clear(); 12153 for (unsigned i = 0; i != NumVars; ++i) 12154 Vars.push_back(Record.readSubExpr()); 12155 C->setPrivateCopies(Vars); 12156 } 12157 12158 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) { 12159 VisitOMPClauseWithPreInit(C); 12160 C->setLParenLoc(Record.readSourceLocation()); 12161 unsigned NumVars = C->varlist_size(); 12162 SmallVector<Expr *, 16> Vars; 12163 Vars.reserve(NumVars); 12164 for (unsigned i = 0; i != NumVars; ++i) 12165 Vars.push_back(Record.readSubExpr()); 12166 C->setVarRefs(Vars); 12167 Vars.clear(); 12168 for (unsigned i = 0; i != NumVars; ++i) 12169 Vars.push_back(Record.readSubExpr()); 12170 C->setPrivateCopies(Vars); 12171 Vars.clear(); 12172 for (unsigned i = 0; i != NumVars; ++i) 12173 Vars.push_back(Record.readSubExpr()); 12174 C->setInits(Vars); 12175 } 12176 12177 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) { 12178 VisitOMPClauseWithPostUpdate(C); 12179 C->setLParenLoc(Record.readSourceLocation()); 12180 C->setKind(Record.readEnum<OpenMPLastprivateModifier>()); 12181 C->setKindLoc(Record.readSourceLocation()); 12182 C->setColonLoc(Record.readSourceLocation()); 12183 unsigned NumVars = C->varlist_size(); 12184 SmallVector<Expr *, 16> Vars; 12185 Vars.reserve(NumVars); 12186 for (unsigned i = 0; i != NumVars; ++i) 12187 Vars.push_back(Record.readSubExpr()); 12188 C->setVarRefs(Vars); 12189 Vars.clear(); 12190 for (unsigned i = 0; i != NumVars; ++i) 12191 Vars.push_back(Record.readSubExpr()); 12192 C->setPrivateCopies(Vars); 12193 Vars.clear(); 12194 for (unsigned i = 0; i != NumVars; ++i) 12195 Vars.push_back(Record.readSubExpr()); 12196 C->setSourceExprs(Vars); 12197 Vars.clear(); 12198 for (unsigned i = 0; i != NumVars; ++i) 12199 Vars.push_back(Record.readSubExpr()); 12200 C->setDestinationExprs(Vars); 12201 Vars.clear(); 12202 for (unsigned i = 0; i != NumVars; ++i) 12203 Vars.push_back(Record.readSubExpr()); 12204 C->setAssignmentOps(Vars); 12205 } 12206 12207 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) { 12208 C->setLParenLoc(Record.readSourceLocation()); 12209 unsigned NumVars = C->varlist_size(); 12210 SmallVector<Expr *, 16> Vars; 12211 Vars.reserve(NumVars); 12212 for (unsigned i = 0; i != NumVars; ++i) 12213 Vars.push_back(Record.readSubExpr()); 12214 C->setVarRefs(Vars); 12215 } 12216 12217 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) { 12218 VisitOMPClauseWithPostUpdate(C); 12219 C->setLParenLoc(Record.readSourceLocation()); 12220 C->setModifierLoc(Record.readSourceLocation()); 12221 C->setColonLoc(Record.readSourceLocation()); 12222 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12223 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12224 C->setQualifierLoc(NNSL); 12225 C->setNameInfo(DNI); 12226 12227 unsigned NumVars = C->varlist_size(); 12228 SmallVector<Expr *, 16> Vars; 12229 Vars.reserve(NumVars); 12230 for (unsigned i = 0; i != NumVars; ++i) 12231 Vars.push_back(Record.readSubExpr()); 12232 C->setVarRefs(Vars); 12233 Vars.clear(); 12234 for (unsigned i = 0; i != NumVars; ++i) 12235 Vars.push_back(Record.readSubExpr()); 12236 C->setPrivates(Vars); 12237 Vars.clear(); 12238 for (unsigned i = 0; i != NumVars; ++i) 12239 Vars.push_back(Record.readSubExpr()); 12240 C->setLHSExprs(Vars); 12241 Vars.clear(); 12242 for (unsigned i = 0; i != NumVars; ++i) 12243 Vars.push_back(Record.readSubExpr()); 12244 C->setRHSExprs(Vars); 12245 Vars.clear(); 12246 for (unsigned i = 0; i != NumVars; ++i) 12247 Vars.push_back(Record.readSubExpr()); 12248 C->setReductionOps(Vars); 12249 if (C->getModifier() == OMPC_REDUCTION_inscan) { 12250 Vars.clear(); 12251 for (unsigned i = 0; i != NumVars; ++i) 12252 Vars.push_back(Record.readSubExpr()); 12253 C->setInscanCopyOps(Vars); 12254 Vars.clear(); 12255 for (unsigned i = 0; i != NumVars; ++i) 12256 Vars.push_back(Record.readSubExpr()); 12257 C->setInscanCopyArrayTemps(Vars); 12258 Vars.clear(); 12259 for (unsigned i = 0; i != NumVars; ++i) 12260 Vars.push_back(Record.readSubExpr()); 12261 C->setInscanCopyArrayElems(Vars); 12262 } 12263 } 12264 12265 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) { 12266 VisitOMPClauseWithPostUpdate(C); 12267 C->setLParenLoc(Record.readSourceLocation()); 12268 C->setColonLoc(Record.readSourceLocation()); 12269 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12270 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12271 C->setQualifierLoc(NNSL); 12272 C->setNameInfo(DNI); 12273 12274 unsigned NumVars = C->varlist_size(); 12275 SmallVector<Expr *, 16> Vars; 12276 Vars.reserve(NumVars); 12277 for (unsigned I = 0; I != NumVars; ++I) 12278 Vars.push_back(Record.readSubExpr()); 12279 C->setVarRefs(Vars); 12280 Vars.clear(); 12281 for (unsigned I = 0; I != NumVars; ++I) 12282 Vars.push_back(Record.readSubExpr()); 12283 C->setPrivates(Vars); 12284 Vars.clear(); 12285 for (unsigned I = 0; I != NumVars; ++I) 12286 Vars.push_back(Record.readSubExpr()); 12287 C->setLHSExprs(Vars); 12288 Vars.clear(); 12289 for (unsigned I = 0; I != NumVars; ++I) 12290 Vars.push_back(Record.readSubExpr()); 12291 C->setRHSExprs(Vars); 12292 Vars.clear(); 12293 for (unsigned I = 0; I != NumVars; ++I) 12294 Vars.push_back(Record.readSubExpr()); 12295 C->setReductionOps(Vars); 12296 } 12297 12298 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) { 12299 VisitOMPClauseWithPostUpdate(C); 12300 C->setLParenLoc(Record.readSourceLocation()); 12301 C->setColonLoc(Record.readSourceLocation()); 12302 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12303 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12304 C->setQualifierLoc(NNSL); 12305 C->setNameInfo(DNI); 12306 12307 unsigned NumVars = C->varlist_size(); 12308 SmallVector<Expr *, 16> Vars; 12309 Vars.reserve(NumVars); 12310 for (unsigned I = 0; I != NumVars; ++I) 12311 Vars.push_back(Record.readSubExpr()); 12312 C->setVarRefs(Vars); 12313 Vars.clear(); 12314 for (unsigned I = 0; I != NumVars; ++I) 12315 Vars.push_back(Record.readSubExpr()); 12316 C->setPrivates(Vars); 12317 Vars.clear(); 12318 for (unsigned I = 0; I != NumVars; ++I) 12319 Vars.push_back(Record.readSubExpr()); 12320 C->setLHSExprs(Vars); 12321 Vars.clear(); 12322 for (unsigned I = 0; I != NumVars; ++I) 12323 Vars.push_back(Record.readSubExpr()); 12324 C->setRHSExprs(Vars); 12325 Vars.clear(); 12326 for (unsigned I = 0; I != NumVars; ++I) 12327 Vars.push_back(Record.readSubExpr()); 12328 C->setReductionOps(Vars); 12329 Vars.clear(); 12330 for (unsigned I = 0; I != NumVars; ++I) 12331 Vars.push_back(Record.readSubExpr()); 12332 C->setTaskgroupDescriptors(Vars); 12333 } 12334 12335 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) { 12336 VisitOMPClauseWithPostUpdate(C); 12337 C->setLParenLoc(Record.readSourceLocation()); 12338 C->setColonLoc(Record.readSourceLocation()); 12339 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt())); 12340 C->setModifierLoc(Record.readSourceLocation()); 12341 unsigned NumVars = C->varlist_size(); 12342 SmallVector<Expr *, 16> Vars; 12343 Vars.reserve(NumVars); 12344 for (unsigned i = 0; i != NumVars; ++i) 12345 Vars.push_back(Record.readSubExpr()); 12346 C->setVarRefs(Vars); 12347 Vars.clear(); 12348 for (unsigned i = 0; i != NumVars; ++i) 12349 Vars.push_back(Record.readSubExpr()); 12350 C->setPrivates(Vars); 12351 Vars.clear(); 12352 for (unsigned i = 0; i != NumVars; ++i) 12353 Vars.push_back(Record.readSubExpr()); 12354 C->setInits(Vars); 12355 Vars.clear(); 12356 for (unsigned i = 0; i != NumVars; ++i) 12357 Vars.push_back(Record.readSubExpr()); 12358 C->setUpdates(Vars); 12359 Vars.clear(); 12360 for (unsigned i = 0; i != NumVars; ++i) 12361 Vars.push_back(Record.readSubExpr()); 12362 C->setFinals(Vars); 12363 C->setStep(Record.readSubExpr()); 12364 C->setCalcStep(Record.readSubExpr()); 12365 Vars.clear(); 12366 for (unsigned I = 0; I != NumVars + 1; ++I) 12367 Vars.push_back(Record.readSubExpr()); 12368 C->setUsedExprs(Vars); 12369 } 12370 12371 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) { 12372 C->setLParenLoc(Record.readSourceLocation()); 12373 C->setColonLoc(Record.readSourceLocation()); 12374 unsigned NumVars = C->varlist_size(); 12375 SmallVector<Expr *, 16> Vars; 12376 Vars.reserve(NumVars); 12377 for (unsigned i = 0; i != NumVars; ++i) 12378 Vars.push_back(Record.readSubExpr()); 12379 C->setVarRefs(Vars); 12380 C->setAlignment(Record.readSubExpr()); 12381 } 12382 12383 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) { 12384 C->setLParenLoc(Record.readSourceLocation()); 12385 unsigned NumVars = C->varlist_size(); 12386 SmallVector<Expr *, 16> Exprs; 12387 Exprs.reserve(NumVars); 12388 for (unsigned i = 0; i != NumVars; ++i) 12389 Exprs.push_back(Record.readSubExpr()); 12390 C->setVarRefs(Exprs); 12391 Exprs.clear(); 12392 for (unsigned i = 0; i != NumVars; ++i) 12393 Exprs.push_back(Record.readSubExpr()); 12394 C->setSourceExprs(Exprs); 12395 Exprs.clear(); 12396 for (unsigned i = 0; i != NumVars; ++i) 12397 Exprs.push_back(Record.readSubExpr()); 12398 C->setDestinationExprs(Exprs); 12399 Exprs.clear(); 12400 for (unsigned i = 0; i != NumVars; ++i) 12401 Exprs.push_back(Record.readSubExpr()); 12402 C->setAssignmentOps(Exprs); 12403 } 12404 12405 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) { 12406 C->setLParenLoc(Record.readSourceLocation()); 12407 unsigned NumVars = C->varlist_size(); 12408 SmallVector<Expr *, 16> Exprs; 12409 Exprs.reserve(NumVars); 12410 for (unsigned i = 0; i != NumVars; ++i) 12411 Exprs.push_back(Record.readSubExpr()); 12412 C->setVarRefs(Exprs); 12413 Exprs.clear(); 12414 for (unsigned i = 0; i != NumVars; ++i) 12415 Exprs.push_back(Record.readSubExpr()); 12416 C->setSourceExprs(Exprs); 12417 Exprs.clear(); 12418 for (unsigned i = 0; i != NumVars; ++i) 12419 Exprs.push_back(Record.readSubExpr()); 12420 C->setDestinationExprs(Exprs); 12421 Exprs.clear(); 12422 for (unsigned i = 0; i != NumVars; ++i) 12423 Exprs.push_back(Record.readSubExpr()); 12424 C->setAssignmentOps(Exprs); 12425 } 12426 12427 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) { 12428 C->setLParenLoc(Record.readSourceLocation()); 12429 unsigned NumVars = C->varlist_size(); 12430 SmallVector<Expr *, 16> Vars; 12431 Vars.reserve(NumVars); 12432 for (unsigned i = 0; i != NumVars; ++i) 12433 Vars.push_back(Record.readSubExpr()); 12434 C->setVarRefs(Vars); 12435 } 12436 12437 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) { 12438 C->setDepobj(Record.readSubExpr()); 12439 C->setLParenLoc(Record.readSourceLocation()); 12440 } 12441 12442 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) { 12443 C->setLParenLoc(Record.readSourceLocation()); 12444 C->setModifier(Record.readSubExpr()); 12445 C->setDependencyKind( 12446 static_cast<OpenMPDependClauseKind>(Record.readInt())); 12447 C->setDependencyLoc(Record.readSourceLocation()); 12448 C->setColonLoc(Record.readSourceLocation()); 12449 unsigned NumVars = C->varlist_size(); 12450 SmallVector<Expr *, 16> Vars; 12451 Vars.reserve(NumVars); 12452 for (unsigned I = 0; I != NumVars; ++I) 12453 Vars.push_back(Record.readSubExpr()); 12454 C->setVarRefs(Vars); 12455 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I) 12456 C->setLoopData(I, Record.readSubExpr()); 12457 } 12458 12459 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) { 12460 VisitOMPClauseWithPreInit(C); 12461 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>()); 12462 C->setDevice(Record.readSubExpr()); 12463 C->setModifierLoc(Record.readSourceLocation()); 12464 C->setLParenLoc(Record.readSourceLocation()); 12465 } 12466 12467 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) { 12468 C->setLParenLoc(Record.readSourceLocation()); 12469 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) { 12470 C->setMapTypeModifier( 12471 I, static_cast<OpenMPMapModifierKind>(Record.readInt())); 12472 C->setMapTypeModifierLoc(I, Record.readSourceLocation()); 12473 } 12474 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12475 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12476 C->setMapType( 12477 static_cast<OpenMPMapClauseKind>(Record.readInt())); 12478 C->setMapLoc(Record.readSourceLocation()); 12479 C->setColonLoc(Record.readSourceLocation()); 12480 auto NumVars = C->varlist_size(); 12481 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12482 auto TotalLists = C->getTotalComponentListNum(); 12483 auto TotalComponents = C->getTotalComponentsNum(); 12484 12485 SmallVector<Expr *, 16> Vars; 12486 Vars.reserve(NumVars); 12487 for (unsigned i = 0; i != NumVars; ++i) 12488 Vars.push_back(Record.readExpr()); 12489 C->setVarRefs(Vars); 12490 12491 SmallVector<Expr *, 16> UDMappers; 12492 UDMappers.reserve(NumVars); 12493 for (unsigned I = 0; I < NumVars; ++I) 12494 UDMappers.push_back(Record.readExpr()); 12495 C->setUDMapperRefs(UDMappers); 12496 12497 SmallVector<ValueDecl *, 16> Decls; 12498 Decls.reserve(UniqueDecls); 12499 for (unsigned i = 0; i < UniqueDecls; ++i) 12500 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12501 C->setUniqueDecls(Decls); 12502 12503 SmallVector<unsigned, 16> ListsPerDecl; 12504 ListsPerDecl.reserve(UniqueDecls); 12505 for (unsigned i = 0; i < UniqueDecls; ++i) 12506 ListsPerDecl.push_back(Record.readInt()); 12507 C->setDeclNumLists(ListsPerDecl); 12508 12509 SmallVector<unsigned, 32> ListSizes; 12510 ListSizes.reserve(TotalLists); 12511 for (unsigned i = 0; i < TotalLists; ++i) 12512 ListSizes.push_back(Record.readInt()); 12513 C->setComponentListSizes(ListSizes); 12514 12515 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12516 Components.reserve(TotalComponents); 12517 for (unsigned i = 0; i < TotalComponents; ++i) { 12518 Expr *AssociatedExprPr = Record.readExpr(); 12519 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12520 Components.emplace_back(AssociatedExprPr, AssociatedDecl, 12521 /*IsNonContiguous=*/false); 12522 } 12523 C->setComponents(Components, ListSizes); 12524 } 12525 12526 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) { 12527 C->setLParenLoc(Record.readSourceLocation()); 12528 C->setColonLoc(Record.readSourceLocation()); 12529 C->setAllocator(Record.readSubExpr()); 12530 unsigned NumVars = C->varlist_size(); 12531 SmallVector<Expr *, 16> Vars; 12532 Vars.reserve(NumVars); 12533 for (unsigned i = 0; i != NumVars; ++i) 12534 Vars.push_back(Record.readSubExpr()); 12535 C->setVarRefs(Vars); 12536 } 12537 12538 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) { 12539 VisitOMPClauseWithPreInit(C); 12540 C->setNumTeams(Record.readSubExpr()); 12541 C->setLParenLoc(Record.readSourceLocation()); 12542 } 12543 12544 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) { 12545 VisitOMPClauseWithPreInit(C); 12546 C->setThreadLimit(Record.readSubExpr()); 12547 C->setLParenLoc(Record.readSourceLocation()); 12548 } 12549 12550 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) { 12551 VisitOMPClauseWithPreInit(C); 12552 C->setPriority(Record.readSubExpr()); 12553 C->setLParenLoc(Record.readSourceLocation()); 12554 } 12555 12556 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) { 12557 VisitOMPClauseWithPreInit(C); 12558 C->setGrainsize(Record.readSubExpr()); 12559 C->setLParenLoc(Record.readSourceLocation()); 12560 } 12561 12562 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) { 12563 VisitOMPClauseWithPreInit(C); 12564 C->setNumTasks(Record.readSubExpr()); 12565 C->setLParenLoc(Record.readSourceLocation()); 12566 } 12567 12568 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) { 12569 C->setHint(Record.readSubExpr()); 12570 C->setLParenLoc(Record.readSourceLocation()); 12571 } 12572 12573 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) { 12574 VisitOMPClauseWithPreInit(C); 12575 C->setDistScheduleKind( 12576 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt())); 12577 C->setChunkSize(Record.readSubExpr()); 12578 C->setLParenLoc(Record.readSourceLocation()); 12579 C->setDistScheduleKindLoc(Record.readSourceLocation()); 12580 C->setCommaLoc(Record.readSourceLocation()); 12581 } 12582 12583 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) { 12584 C->setDefaultmapKind( 12585 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt())); 12586 C->setDefaultmapModifier( 12587 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt())); 12588 C->setLParenLoc(Record.readSourceLocation()); 12589 C->setDefaultmapModifierLoc(Record.readSourceLocation()); 12590 C->setDefaultmapKindLoc(Record.readSourceLocation()); 12591 } 12592 12593 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) { 12594 C->setLParenLoc(Record.readSourceLocation()); 12595 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) { 12596 C->setMotionModifier( 12597 I, static_cast<OpenMPMotionModifierKind>(Record.readInt())); 12598 C->setMotionModifierLoc(I, Record.readSourceLocation()); 12599 } 12600 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12601 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12602 C->setColonLoc(Record.readSourceLocation()); 12603 auto NumVars = C->varlist_size(); 12604 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12605 auto TotalLists = C->getTotalComponentListNum(); 12606 auto TotalComponents = C->getTotalComponentsNum(); 12607 12608 SmallVector<Expr *, 16> Vars; 12609 Vars.reserve(NumVars); 12610 for (unsigned i = 0; i != NumVars; ++i) 12611 Vars.push_back(Record.readSubExpr()); 12612 C->setVarRefs(Vars); 12613 12614 SmallVector<Expr *, 16> UDMappers; 12615 UDMappers.reserve(NumVars); 12616 for (unsigned I = 0; I < NumVars; ++I) 12617 UDMappers.push_back(Record.readSubExpr()); 12618 C->setUDMapperRefs(UDMappers); 12619 12620 SmallVector<ValueDecl *, 16> Decls; 12621 Decls.reserve(UniqueDecls); 12622 for (unsigned i = 0; i < UniqueDecls; ++i) 12623 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12624 C->setUniqueDecls(Decls); 12625 12626 SmallVector<unsigned, 16> ListsPerDecl; 12627 ListsPerDecl.reserve(UniqueDecls); 12628 for (unsigned i = 0; i < UniqueDecls; ++i) 12629 ListsPerDecl.push_back(Record.readInt()); 12630 C->setDeclNumLists(ListsPerDecl); 12631 12632 SmallVector<unsigned, 32> ListSizes; 12633 ListSizes.reserve(TotalLists); 12634 for (unsigned i = 0; i < TotalLists; ++i) 12635 ListSizes.push_back(Record.readInt()); 12636 C->setComponentListSizes(ListSizes); 12637 12638 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12639 Components.reserve(TotalComponents); 12640 for (unsigned i = 0; i < TotalComponents; ++i) { 12641 Expr *AssociatedExprPr = Record.readSubExpr(); 12642 bool IsNonContiguous = Record.readBool(); 12643 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12644 Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous); 12645 } 12646 C->setComponents(Components, ListSizes); 12647 } 12648 12649 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) { 12650 C->setLParenLoc(Record.readSourceLocation()); 12651 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) { 12652 C->setMotionModifier( 12653 I, static_cast<OpenMPMotionModifierKind>(Record.readInt())); 12654 C->setMotionModifierLoc(I, Record.readSourceLocation()); 12655 } 12656 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12657 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12658 C->setColonLoc(Record.readSourceLocation()); 12659 auto NumVars = C->varlist_size(); 12660 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12661 auto TotalLists = C->getTotalComponentListNum(); 12662 auto TotalComponents = C->getTotalComponentsNum(); 12663 12664 SmallVector<Expr *, 16> Vars; 12665 Vars.reserve(NumVars); 12666 for (unsigned i = 0; i != NumVars; ++i) 12667 Vars.push_back(Record.readSubExpr()); 12668 C->setVarRefs(Vars); 12669 12670 SmallVector<Expr *, 16> UDMappers; 12671 UDMappers.reserve(NumVars); 12672 for (unsigned I = 0; I < NumVars; ++I) 12673 UDMappers.push_back(Record.readSubExpr()); 12674 C->setUDMapperRefs(UDMappers); 12675 12676 SmallVector<ValueDecl *, 16> Decls; 12677 Decls.reserve(UniqueDecls); 12678 for (unsigned i = 0; i < UniqueDecls; ++i) 12679 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12680 C->setUniqueDecls(Decls); 12681 12682 SmallVector<unsigned, 16> ListsPerDecl; 12683 ListsPerDecl.reserve(UniqueDecls); 12684 for (unsigned i = 0; i < UniqueDecls; ++i) 12685 ListsPerDecl.push_back(Record.readInt()); 12686 C->setDeclNumLists(ListsPerDecl); 12687 12688 SmallVector<unsigned, 32> ListSizes; 12689 ListSizes.reserve(TotalLists); 12690 for (unsigned i = 0; i < TotalLists; ++i) 12691 ListSizes.push_back(Record.readInt()); 12692 C->setComponentListSizes(ListSizes); 12693 12694 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12695 Components.reserve(TotalComponents); 12696 for (unsigned i = 0; i < TotalComponents; ++i) { 12697 Expr *AssociatedExprPr = Record.readSubExpr(); 12698 bool IsNonContiguous = Record.readBool(); 12699 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12700 Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous); 12701 } 12702 C->setComponents(Components, ListSizes); 12703 } 12704 12705 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) { 12706 C->setLParenLoc(Record.readSourceLocation()); 12707 auto NumVars = C->varlist_size(); 12708 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12709 auto TotalLists = C->getTotalComponentListNum(); 12710 auto TotalComponents = C->getTotalComponentsNum(); 12711 12712 SmallVector<Expr *, 16> Vars; 12713 Vars.reserve(NumVars); 12714 for (unsigned i = 0; i != NumVars; ++i) 12715 Vars.push_back(Record.readSubExpr()); 12716 C->setVarRefs(Vars); 12717 Vars.clear(); 12718 for (unsigned i = 0; i != NumVars; ++i) 12719 Vars.push_back(Record.readSubExpr()); 12720 C->setPrivateCopies(Vars); 12721 Vars.clear(); 12722 for (unsigned i = 0; i != NumVars; ++i) 12723 Vars.push_back(Record.readSubExpr()); 12724 C->setInits(Vars); 12725 12726 SmallVector<ValueDecl *, 16> Decls; 12727 Decls.reserve(UniqueDecls); 12728 for (unsigned i = 0; i < UniqueDecls; ++i) 12729 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12730 C->setUniqueDecls(Decls); 12731 12732 SmallVector<unsigned, 16> ListsPerDecl; 12733 ListsPerDecl.reserve(UniqueDecls); 12734 for (unsigned i = 0; i < UniqueDecls; ++i) 12735 ListsPerDecl.push_back(Record.readInt()); 12736 C->setDeclNumLists(ListsPerDecl); 12737 12738 SmallVector<unsigned, 32> ListSizes; 12739 ListSizes.reserve(TotalLists); 12740 for (unsigned i = 0; i < TotalLists; ++i) 12741 ListSizes.push_back(Record.readInt()); 12742 C->setComponentListSizes(ListSizes); 12743 12744 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12745 Components.reserve(TotalComponents); 12746 for (unsigned i = 0; i < TotalComponents; ++i) { 12747 auto *AssociatedExprPr = Record.readSubExpr(); 12748 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12749 Components.emplace_back(AssociatedExprPr, AssociatedDecl, 12750 /*IsNonContiguous=*/false); 12751 } 12752 C->setComponents(Components, ListSizes); 12753 } 12754 12755 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) { 12756 C->setLParenLoc(Record.readSourceLocation()); 12757 auto NumVars = C->varlist_size(); 12758 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12759 auto TotalLists = C->getTotalComponentListNum(); 12760 auto TotalComponents = C->getTotalComponentsNum(); 12761 12762 SmallVector<Expr *, 16> Vars; 12763 Vars.reserve(NumVars); 12764 for (unsigned i = 0; i != NumVars; ++i) 12765 Vars.push_back(Record.readSubExpr()); 12766 C->setVarRefs(Vars); 12767 12768 SmallVector<ValueDecl *, 16> Decls; 12769 Decls.reserve(UniqueDecls); 12770 for (unsigned i = 0; i < UniqueDecls; ++i) 12771 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12772 C->setUniqueDecls(Decls); 12773 12774 SmallVector<unsigned, 16> ListsPerDecl; 12775 ListsPerDecl.reserve(UniqueDecls); 12776 for (unsigned i = 0; i < UniqueDecls; ++i) 12777 ListsPerDecl.push_back(Record.readInt()); 12778 C->setDeclNumLists(ListsPerDecl); 12779 12780 SmallVector<unsigned, 32> ListSizes; 12781 ListSizes.reserve(TotalLists); 12782 for (unsigned i = 0; i < TotalLists; ++i) 12783 ListSizes.push_back(Record.readInt()); 12784 C->setComponentListSizes(ListSizes); 12785 12786 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12787 Components.reserve(TotalComponents); 12788 for (unsigned i = 0; i < TotalComponents; ++i) { 12789 Expr *AssociatedExpr = Record.readSubExpr(); 12790 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12791 Components.emplace_back(AssociatedExpr, AssociatedDecl, 12792 /*IsNonContiguous*/ false); 12793 } 12794 C->setComponents(Components, ListSizes); 12795 } 12796 12797 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) { 12798 C->setLParenLoc(Record.readSourceLocation()); 12799 auto NumVars = C->varlist_size(); 12800 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12801 auto TotalLists = C->getTotalComponentListNum(); 12802 auto TotalComponents = C->getTotalComponentsNum(); 12803 12804 SmallVector<Expr *, 16> Vars; 12805 Vars.reserve(NumVars); 12806 for (unsigned i = 0; i != NumVars; ++i) 12807 Vars.push_back(Record.readSubExpr()); 12808 C->setVarRefs(Vars); 12809 Vars.clear(); 12810 12811 SmallVector<ValueDecl *, 16> Decls; 12812 Decls.reserve(UniqueDecls); 12813 for (unsigned i = 0; i < UniqueDecls; ++i) 12814 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12815 C->setUniqueDecls(Decls); 12816 12817 SmallVector<unsigned, 16> ListsPerDecl; 12818 ListsPerDecl.reserve(UniqueDecls); 12819 for (unsigned i = 0; i < UniqueDecls; ++i) 12820 ListsPerDecl.push_back(Record.readInt()); 12821 C->setDeclNumLists(ListsPerDecl); 12822 12823 SmallVector<unsigned, 32> ListSizes; 12824 ListSizes.reserve(TotalLists); 12825 for (unsigned i = 0; i < TotalLists; ++i) 12826 ListSizes.push_back(Record.readInt()); 12827 C->setComponentListSizes(ListSizes); 12828 12829 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12830 Components.reserve(TotalComponents); 12831 for (unsigned i = 0; i < TotalComponents; ++i) { 12832 Expr *AssociatedExpr = Record.readSubExpr(); 12833 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12834 Components.emplace_back(AssociatedExpr, AssociatedDecl, 12835 /*IsNonContiguous=*/false); 12836 } 12837 C->setComponents(Components, ListSizes); 12838 } 12839 12840 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) { 12841 C->setLParenLoc(Record.readSourceLocation()); 12842 unsigned NumVars = C->varlist_size(); 12843 SmallVector<Expr *, 16> Vars; 12844 Vars.reserve(NumVars); 12845 for (unsigned i = 0; i != NumVars; ++i) 12846 Vars.push_back(Record.readSubExpr()); 12847 C->setVarRefs(Vars); 12848 Vars.clear(); 12849 Vars.reserve(NumVars); 12850 for (unsigned i = 0; i != NumVars; ++i) 12851 Vars.push_back(Record.readSubExpr()); 12852 C->setPrivateRefs(Vars); 12853 } 12854 12855 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) { 12856 C->setLParenLoc(Record.readSourceLocation()); 12857 unsigned NumVars = C->varlist_size(); 12858 SmallVector<Expr *, 16> Vars; 12859 Vars.reserve(NumVars); 12860 for (unsigned i = 0; i != NumVars; ++i) 12861 Vars.push_back(Record.readSubExpr()); 12862 C->setVarRefs(Vars); 12863 } 12864 12865 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) { 12866 C->setLParenLoc(Record.readSourceLocation()); 12867 unsigned NumVars = C->varlist_size(); 12868 SmallVector<Expr *, 16> Vars; 12869 Vars.reserve(NumVars); 12870 for (unsigned i = 0; i != NumVars; ++i) 12871 Vars.push_back(Record.readSubExpr()); 12872 C->setVarRefs(Vars); 12873 } 12874 12875 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) { 12876 C->setLParenLoc(Record.readSourceLocation()); 12877 unsigned NumOfAllocators = C->getNumberOfAllocators(); 12878 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data; 12879 Data.reserve(NumOfAllocators); 12880 for (unsigned I = 0; I != NumOfAllocators; ++I) { 12881 OMPUsesAllocatorsClause::Data &D = Data.emplace_back(); 12882 D.Allocator = Record.readSubExpr(); 12883 D.AllocatorTraits = Record.readSubExpr(); 12884 D.LParenLoc = Record.readSourceLocation(); 12885 D.RParenLoc = Record.readSourceLocation(); 12886 } 12887 C->setAllocatorsData(Data); 12888 } 12889 12890 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) { 12891 C->setLParenLoc(Record.readSourceLocation()); 12892 C->setModifier(Record.readSubExpr()); 12893 C->setColonLoc(Record.readSourceLocation()); 12894 unsigned NumOfLocators = C->varlist_size(); 12895 SmallVector<Expr *, 4> Locators; 12896 Locators.reserve(NumOfLocators); 12897 for (unsigned I = 0; I != NumOfLocators; ++I) 12898 Locators.push_back(Record.readSubExpr()); 12899 C->setVarRefs(Locators); 12900 } 12901 12902 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) { 12903 C->setKind(Record.readEnum<OpenMPOrderClauseKind>()); 12904 C->setLParenLoc(Record.readSourceLocation()); 12905 C->setKindKwLoc(Record.readSourceLocation()); 12906 } 12907 12908 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() { 12909 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo(); 12910 TI.Sets.resize(readUInt32()); 12911 for (auto &Set : TI.Sets) { 12912 Set.Kind = readEnum<llvm::omp::TraitSet>(); 12913 Set.Selectors.resize(readUInt32()); 12914 for (auto &Selector : Set.Selectors) { 12915 Selector.Kind = readEnum<llvm::omp::TraitSelector>(); 12916 Selector.ScoreOrCondition = nullptr; 12917 if (readBool()) 12918 Selector.ScoreOrCondition = readExprRef(); 12919 Selector.Properties.resize(readUInt32()); 12920 for (auto &Property : Selector.Properties) 12921 Property.Kind = readEnum<llvm::omp::TraitProperty>(); 12922 } 12923 } 12924 return &TI; 12925 } 12926 12927 void ASTRecordReader::readOMPChildren(OMPChildren *Data) { 12928 if (!Data) 12929 return; 12930 if (Reader->ReadingKind == ASTReader::Read_Stmt) { 12931 // Skip NumClauses, NumChildren and HasAssociatedStmt fields. 12932 skipInts(3); 12933 } 12934 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses()); 12935 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I) 12936 Clauses[I] = readOMPClause(); 12937 Data->setClauses(Clauses); 12938 if (Data->hasAssociatedStmt()) 12939 Data->setAssociatedStmt(readStmt()); 12940 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I) 12941 Data->getChildren()[I] = readStmt(); 12942 } 12943