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