1 //===- ASTReader.cpp - AST File Reader ------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the ASTReader class, which reads AST files. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "ASTCommon.h" 14 #include "ASTReaderInternals.h" 15 #include "clang/AST/ASTConsumer.h" 16 #include "clang/AST/ASTContext.h" 17 #include "clang/AST/ASTMutationListener.h" 18 #include "clang/AST/ASTUnresolvedSet.h" 19 #include "clang/AST/AbstractTypeReader.h" 20 #include "clang/AST/Decl.h" 21 #include "clang/AST/DeclBase.h" 22 #include "clang/AST/DeclCXX.h" 23 #include "clang/AST/DeclFriend.h" 24 #include "clang/AST/DeclGroup.h" 25 #include "clang/AST/DeclObjC.h" 26 #include "clang/AST/DeclTemplate.h" 27 #include "clang/AST/DeclarationName.h" 28 #include "clang/AST/Expr.h" 29 #include "clang/AST/ExprCXX.h" 30 #include "clang/AST/ExternalASTSource.h" 31 #include "clang/AST/NestedNameSpecifier.h" 32 #include "clang/AST/ODRHash.h" 33 #include "clang/AST/OpenMPClause.h" 34 #include "clang/AST/RawCommentList.h" 35 #include "clang/AST/TemplateBase.h" 36 #include "clang/AST/TemplateName.h" 37 #include "clang/AST/Type.h" 38 #include "clang/AST/TypeLoc.h" 39 #include "clang/AST/TypeLocVisitor.h" 40 #include "clang/AST/UnresolvedSet.h" 41 #include "clang/Basic/CommentOptions.h" 42 #include "clang/Basic/Diagnostic.h" 43 #include "clang/Basic/DiagnosticError.h" 44 #include "clang/Basic/DiagnosticOptions.h" 45 #include "clang/Basic/ExceptionSpecificationType.h" 46 #include "clang/Basic/FileManager.h" 47 #include "clang/Basic/FileSystemOptions.h" 48 #include "clang/Basic/IdentifierTable.h" 49 #include "clang/Basic/LLVM.h" 50 #include "clang/Basic/LangOptions.h" 51 #include "clang/Basic/Module.h" 52 #include "clang/Basic/ObjCRuntime.h" 53 #include "clang/Basic/OpenMPKinds.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/ASTRecordReader.h" 80 #include "clang/Serialization/ContinuousRangeMap.h" 81 #include "clang/Serialization/GlobalModuleIndex.h" 82 #include "clang/Serialization/InMemoryModuleCache.h" 83 #include "clang/Serialization/ModuleFile.h" 84 #include "clang/Serialization/ModuleFileExtension.h" 85 #include "clang/Serialization/ModuleManager.h" 86 #include "clang/Serialization/PCHContainerOperations.h" 87 #include "clang/Serialization/SerializationDiagnostic.h" 88 #include "llvm/ADT/APFloat.h" 89 #include "llvm/ADT/APInt.h" 90 #include "llvm/ADT/APSInt.h" 91 #include "llvm/ADT/ArrayRef.h" 92 #include "llvm/ADT/DenseMap.h" 93 #include "llvm/ADT/FloatingPointMode.h" 94 #include "llvm/ADT/FoldingSet.h" 95 #include "llvm/ADT/Hashing.h" 96 #include "llvm/ADT/IntrusiveRefCntPtr.h" 97 #include "llvm/ADT/None.h" 98 #include "llvm/ADT/Optional.h" 99 #include "llvm/ADT/STLExtras.h" 100 #include "llvm/ADT/ScopeExit.h" 101 #include "llvm/ADT/SmallPtrSet.h" 102 #include "llvm/ADT/SmallString.h" 103 #include "llvm/ADT/SmallVector.h" 104 #include "llvm/ADT/StringExtras.h" 105 #include "llvm/ADT/StringMap.h" 106 #include "llvm/ADT/StringRef.h" 107 #include "llvm/ADT/Triple.h" 108 #include "llvm/ADT/iterator_range.h" 109 #include "llvm/Bitstream/BitstreamReader.h" 110 #include "llvm/Support/Casting.h" 111 #include "llvm/Support/Compiler.h" 112 #include "llvm/Support/Compression.h" 113 #include "llvm/Support/DJB.h" 114 #include "llvm/Support/Endian.h" 115 #include "llvm/Support/Error.h" 116 #include "llvm/Support/ErrorHandling.h" 117 #include "llvm/Support/FileSystem.h" 118 #include "llvm/Support/LEB128.h" 119 #include "llvm/Support/MemoryBuffer.h" 120 #include "llvm/Support/Path.h" 121 #include "llvm/Support/SaveAndRestore.h" 122 #include "llvm/Support/Timer.h" 123 #include "llvm/Support/VersionTuple.h" 124 #include "llvm/Support/raw_ostream.h" 125 #include <algorithm> 126 #include <cassert> 127 #include <cstddef> 128 #include <cstdint> 129 #include <cstdio> 130 #include <ctime> 131 #include <iterator> 132 #include <limits> 133 #include <map> 134 #include <memory> 135 #include <string> 136 #include <system_error> 137 #include <tuple> 138 #include <utility> 139 #include <vector> 140 141 using namespace clang; 142 using namespace clang::serialization; 143 using namespace clang::serialization::reader; 144 using llvm::BitstreamCursor; 145 using llvm::RoundingMode; 146 147 //===----------------------------------------------------------------------===// 148 // ChainedASTReaderListener implementation 149 //===----------------------------------------------------------------------===// 150 151 bool 152 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) { 153 return First->ReadFullVersionInformation(FullVersion) || 154 Second->ReadFullVersionInformation(FullVersion); 155 } 156 157 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) { 158 First->ReadModuleName(ModuleName); 159 Second->ReadModuleName(ModuleName); 160 } 161 162 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) { 163 First->ReadModuleMapFile(ModuleMapPath); 164 Second->ReadModuleMapFile(ModuleMapPath); 165 } 166 167 bool 168 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts, 169 bool Complain, 170 bool AllowCompatibleDifferences) { 171 return First->ReadLanguageOptions(LangOpts, Complain, 172 AllowCompatibleDifferences) || 173 Second->ReadLanguageOptions(LangOpts, Complain, 174 AllowCompatibleDifferences); 175 } 176 177 bool ChainedASTReaderListener::ReadTargetOptions( 178 const TargetOptions &TargetOpts, bool Complain, 179 bool AllowCompatibleDifferences) { 180 return First->ReadTargetOptions(TargetOpts, Complain, 181 AllowCompatibleDifferences) || 182 Second->ReadTargetOptions(TargetOpts, Complain, 183 AllowCompatibleDifferences); 184 } 185 186 bool ChainedASTReaderListener::ReadDiagnosticOptions( 187 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 188 return First->ReadDiagnosticOptions(DiagOpts, Complain) || 189 Second->ReadDiagnosticOptions(DiagOpts, Complain); 190 } 191 192 bool 193 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts, 194 bool Complain) { 195 return First->ReadFileSystemOptions(FSOpts, Complain) || 196 Second->ReadFileSystemOptions(FSOpts, Complain); 197 } 198 199 bool ChainedASTReaderListener::ReadHeaderSearchOptions( 200 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath, 201 bool Complain) { 202 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 203 Complain) || 204 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 205 Complain); 206 } 207 208 bool ChainedASTReaderListener::ReadPreprocessorOptions( 209 const PreprocessorOptions &PPOpts, bool Complain, 210 std::string &SuggestedPredefines) { 211 return First->ReadPreprocessorOptions(PPOpts, Complain, 212 SuggestedPredefines) || 213 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines); 214 } 215 216 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M, 217 unsigned Value) { 218 First->ReadCounter(M, Value); 219 Second->ReadCounter(M, Value); 220 } 221 222 bool ChainedASTReaderListener::needsInputFileVisitation() { 223 return First->needsInputFileVisitation() || 224 Second->needsInputFileVisitation(); 225 } 226 227 bool ChainedASTReaderListener::needsSystemInputFileVisitation() { 228 return First->needsSystemInputFileVisitation() || 229 Second->needsSystemInputFileVisitation(); 230 } 231 232 void ChainedASTReaderListener::visitModuleFile(StringRef Filename, 233 ModuleKind Kind) { 234 First->visitModuleFile(Filename, Kind); 235 Second->visitModuleFile(Filename, Kind); 236 } 237 238 bool ChainedASTReaderListener::visitInputFile(StringRef Filename, 239 bool isSystem, 240 bool isOverridden, 241 bool isExplicitModule) { 242 bool Continue = false; 243 if (First->needsInputFileVisitation() && 244 (!isSystem || First->needsSystemInputFileVisitation())) 245 Continue |= First->visitInputFile(Filename, isSystem, isOverridden, 246 isExplicitModule); 247 if (Second->needsInputFileVisitation() && 248 (!isSystem || Second->needsSystemInputFileVisitation())) 249 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden, 250 isExplicitModule); 251 return Continue; 252 } 253 254 void ChainedASTReaderListener::readModuleFileExtension( 255 const ModuleFileExtensionMetadata &Metadata) { 256 First->readModuleFileExtension(Metadata); 257 Second->readModuleFileExtension(Metadata); 258 } 259 260 //===----------------------------------------------------------------------===// 261 // PCH validator implementation 262 //===----------------------------------------------------------------------===// 263 264 ASTReaderListener::~ASTReaderListener() = default; 265 266 /// Compare the given set of language options against an existing set of 267 /// language options. 268 /// 269 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 270 /// \param AllowCompatibleDifferences If true, differences between compatible 271 /// language options will be permitted. 272 /// 273 /// \returns true if the languagae options mis-match, false otherwise. 274 static bool checkLanguageOptions(const LangOptions &LangOpts, 275 const LangOptions &ExistingLangOpts, 276 DiagnosticsEngine *Diags, 277 bool AllowCompatibleDifferences = true) { 278 #define LANGOPT(Name, Bits, Default, Description) \ 279 if (ExistingLangOpts.Name != LangOpts.Name) { \ 280 if (Diags) \ 281 Diags->Report(diag::err_pch_langopt_mismatch) \ 282 << Description << LangOpts.Name << ExistingLangOpts.Name; \ 283 return true; \ 284 } 285 286 #define VALUE_LANGOPT(Name, Bits, Default, Description) \ 287 if (ExistingLangOpts.Name != LangOpts.Name) { \ 288 if (Diags) \ 289 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 290 << Description; \ 291 return true; \ 292 } 293 294 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 295 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \ 296 if (Diags) \ 297 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 298 << Description; \ 299 return true; \ 300 } 301 302 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \ 303 if (!AllowCompatibleDifferences) \ 304 LANGOPT(Name, Bits, Default, Description) 305 306 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \ 307 if (!AllowCompatibleDifferences) \ 308 ENUM_LANGOPT(Name, Bits, Default, Description) 309 310 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \ 311 if (!AllowCompatibleDifferences) \ 312 VALUE_LANGOPT(Name, Bits, Default, Description) 313 314 #define BENIGN_LANGOPT(Name, Bits, Default, Description) 315 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) 316 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description) 317 #include "clang/Basic/LangOptions.def" 318 319 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) { 320 if (Diags) 321 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features"; 322 return true; 323 } 324 325 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) { 326 if (Diags) 327 Diags->Report(diag::err_pch_langopt_value_mismatch) 328 << "target Objective-C runtime"; 329 return true; 330 } 331 332 if (ExistingLangOpts.CommentOpts.BlockCommandNames != 333 LangOpts.CommentOpts.BlockCommandNames) { 334 if (Diags) 335 Diags->Report(diag::err_pch_langopt_value_mismatch) 336 << "block command names"; 337 return true; 338 } 339 340 // Sanitizer feature mismatches are treated as compatible differences. If 341 // compatible differences aren't allowed, we still only want to check for 342 // mismatches of non-modular sanitizers (the only ones which can affect AST 343 // generation). 344 if (!AllowCompatibleDifferences) { 345 SanitizerMask ModularSanitizers = getPPTransparentSanitizers(); 346 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize; 347 SanitizerSet ImportedSanitizers = LangOpts.Sanitize; 348 ExistingSanitizers.clear(ModularSanitizers); 349 ImportedSanitizers.clear(ModularSanitizers); 350 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) { 351 const std::string Flag = "-fsanitize="; 352 if (Diags) { 353 #define SANITIZER(NAME, ID) \ 354 { \ 355 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \ 356 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \ 357 if (InExistingModule != InImportedModule) \ 358 Diags->Report(diag::err_pch_targetopt_feature_mismatch) \ 359 << InExistingModule << (Flag + NAME); \ 360 } 361 #include "clang/Basic/Sanitizers.def" 362 } 363 return true; 364 } 365 } 366 367 return false; 368 } 369 370 /// Compare the given set of target options against an existing set of 371 /// target options. 372 /// 373 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 374 /// 375 /// \returns true if the target options mis-match, false otherwise. 376 static bool checkTargetOptions(const TargetOptions &TargetOpts, 377 const TargetOptions &ExistingTargetOpts, 378 DiagnosticsEngine *Diags, 379 bool AllowCompatibleDifferences = true) { 380 #define CHECK_TARGET_OPT(Field, Name) \ 381 if (TargetOpts.Field != ExistingTargetOpts.Field) { \ 382 if (Diags) \ 383 Diags->Report(diag::err_pch_targetopt_mismatch) \ 384 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \ 385 return true; \ 386 } 387 388 // The triple and ABI must match exactly. 389 CHECK_TARGET_OPT(Triple, "target"); 390 CHECK_TARGET_OPT(ABI, "target ABI"); 391 392 // We can tolerate different CPUs in many cases, notably when one CPU 393 // supports a strict superset of another. When allowing compatible 394 // differences skip this check. 395 if (!AllowCompatibleDifferences) { 396 CHECK_TARGET_OPT(CPU, "target CPU"); 397 CHECK_TARGET_OPT(TuneCPU, "tune CPU"); 398 } 399 400 #undef CHECK_TARGET_OPT 401 402 // Compare feature sets. 403 SmallVector<StringRef, 4> ExistingFeatures( 404 ExistingTargetOpts.FeaturesAsWritten.begin(), 405 ExistingTargetOpts.FeaturesAsWritten.end()); 406 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(), 407 TargetOpts.FeaturesAsWritten.end()); 408 llvm::sort(ExistingFeatures); 409 llvm::sort(ReadFeatures); 410 411 // We compute the set difference in both directions explicitly so that we can 412 // diagnose the differences differently. 413 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures; 414 std::set_difference( 415 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(), 416 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures)); 417 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(), 418 ExistingFeatures.begin(), ExistingFeatures.end(), 419 std::back_inserter(UnmatchedReadFeatures)); 420 421 // If we are allowing compatible differences and the read feature set is 422 // a strict subset of the existing feature set, there is nothing to diagnose. 423 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty()) 424 return false; 425 426 if (Diags) { 427 for (StringRef Feature : UnmatchedReadFeatures) 428 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 429 << /* is-existing-feature */ false << Feature; 430 for (StringRef Feature : UnmatchedExistingFeatures) 431 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 432 << /* is-existing-feature */ true << Feature; 433 } 434 435 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty(); 436 } 437 438 bool 439 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts, 440 bool Complain, 441 bool AllowCompatibleDifferences) { 442 const LangOptions &ExistingLangOpts = PP.getLangOpts(); 443 return checkLanguageOptions(LangOpts, ExistingLangOpts, 444 Complain ? &Reader.Diags : nullptr, 445 AllowCompatibleDifferences); 446 } 447 448 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts, 449 bool Complain, 450 bool AllowCompatibleDifferences) { 451 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts(); 452 return checkTargetOptions(TargetOpts, ExistingTargetOpts, 453 Complain ? &Reader.Diags : nullptr, 454 AllowCompatibleDifferences); 455 } 456 457 namespace { 458 459 using MacroDefinitionsMap = 460 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>; 461 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>; 462 463 } // namespace 464 465 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags, 466 DiagnosticsEngine &Diags, 467 bool Complain) { 468 using Level = DiagnosticsEngine::Level; 469 470 // Check current mappings for new -Werror mappings, and the stored mappings 471 // for cases that were explicitly mapped to *not* be errors that are now 472 // errors because of options like -Werror. 473 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags }; 474 475 for (DiagnosticsEngine *MappingSource : MappingSources) { 476 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) { 477 diag::kind DiagID = DiagIDMappingPair.first; 478 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation()); 479 if (CurLevel < DiagnosticsEngine::Error) 480 continue; // not significant 481 Level StoredLevel = 482 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation()); 483 if (StoredLevel < DiagnosticsEngine::Error) { 484 if (Complain) 485 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" + 486 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str(); 487 return true; 488 } 489 } 490 } 491 492 return false; 493 } 494 495 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) { 496 diag::Severity Ext = Diags.getExtensionHandlingBehavior(); 497 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors()) 498 return true; 499 return Ext >= diag::Severity::Error; 500 } 501 502 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags, 503 DiagnosticsEngine &Diags, 504 bool IsSystem, bool Complain) { 505 // Top-level options 506 if (IsSystem) { 507 if (Diags.getSuppressSystemWarnings()) 508 return false; 509 // If -Wsystem-headers was not enabled before, be conservative 510 if (StoredDiags.getSuppressSystemWarnings()) { 511 if (Complain) 512 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers"; 513 return true; 514 } 515 } 516 517 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) { 518 if (Complain) 519 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror"; 520 return true; 521 } 522 523 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() && 524 !StoredDiags.getEnableAllWarnings()) { 525 if (Complain) 526 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror"; 527 return true; 528 } 529 530 if (isExtHandlingFromDiagsError(Diags) && 531 !isExtHandlingFromDiagsError(StoredDiags)) { 532 if (Complain) 533 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors"; 534 return true; 535 } 536 537 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain); 538 } 539 540 /// Return the top import module if it is implicit, nullptr otherwise. 541 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr, 542 Preprocessor &PP) { 543 // If the original import came from a file explicitly generated by the user, 544 // don't check the diagnostic mappings. 545 // FIXME: currently this is approximated by checking whether this is not a 546 // module import of an implicitly-loaded module file. 547 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in 548 // the transitive closure of its imports, since unrelated modules cannot be 549 // imported until after this module finishes validation. 550 ModuleFile *TopImport = &*ModuleMgr.rbegin(); 551 while (!TopImport->ImportedBy.empty()) 552 TopImport = TopImport->ImportedBy[0]; 553 if (TopImport->Kind != MK_ImplicitModule) 554 return nullptr; 555 556 StringRef ModuleName = TopImport->ModuleName; 557 assert(!ModuleName.empty() && "diagnostic options read before module name"); 558 559 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName); 560 assert(M && "missing module"); 561 return M; 562 } 563 564 bool PCHValidator::ReadDiagnosticOptions( 565 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 566 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics(); 567 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs()); 568 IntrusiveRefCntPtr<DiagnosticsEngine> Diags( 569 new DiagnosticsEngine(DiagIDs, DiagOpts.get())); 570 // This should never fail, because we would have processed these options 571 // before writing them to an ASTFile. 572 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false); 573 574 ModuleManager &ModuleMgr = Reader.getModuleManager(); 575 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then"); 576 577 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP); 578 if (!TopM) 579 return false; 580 581 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that 582 // contains the union of their flags. 583 return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem, 584 Complain); 585 } 586 587 /// Collect the macro definitions provided by the given preprocessor 588 /// options. 589 static void 590 collectMacroDefinitions(const PreprocessorOptions &PPOpts, 591 MacroDefinitionsMap &Macros, 592 SmallVectorImpl<StringRef> *MacroNames = nullptr) { 593 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) { 594 StringRef Macro = PPOpts.Macros[I].first; 595 bool IsUndef = PPOpts.Macros[I].second; 596 597 std::pair<StringRef, StringRef> MacroPair = Macro.split('='); 598 StringRef MacroName = MacroPair.first; 599 StringRef MacroBody = MacroPair.second; 600 601 // For an #undef'd macro, we only care about the name. 602 if (IsUndef) { 603 if (MacroNames && !Macros.count(MacroName)) 604 MacroNames->push_back(MacroName); 605 606 Macros[MacroName] = std::make_pair("", true); 607 continue; 608 } 609 610 // For a #define'd macro, figure out the actual definition. 611 if (MacroName.size() == Macro.size()) 612 MacroBody = "1"; 613 else { 614 // Note: GCC drops anything following an end-of-line character. 615 StringRef::size_type End = MacroBody.find_first_of("\n\r"); 616 MacroBody = MacroBody.substr(0, End); 617 } 618 619 if (MacroNames && !Macros.count(MacroName)) 620 MacroNames->push_back(MacroName); 621 Macros[MacroName] = std::make_pair(MacroBody, false); 622 } 623 } 624 625 /// Check the preprocessor options deserialized from the control block 626 /// against the preprocessor options in an existing preprocessor. 627 /// 628 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 629 /// \param Validate If true, validate preprocessor options. If false, allow 630 /// macros defined by \p ExistingPPOpts to override those defined by 631 /// \p PPOpts in SuggestedPredefines. 632 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts, 633 const PreprocessorOptions &ExistingPPOpts, 634 DiagnosticsEngine *Diags, 635 FileManager &FileMgr, 636 std::string &SuggestedPredefines, 637 const LangOptions &LangOpts, 638 bool Validate = true) { 639 // Check macro definitions. 640 MacroDefinitionsMap ASTFileMacros; 641 collectMacroDefinitions(PPOpts, ASTFileMacros); 642 MacroDefinitionsMap ExistingMacros; 643 SmallVector<StringRef, 4> ExistingMacroNames; 644 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames); 645 646 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) { 647 // Dig out the macro definition in the existing preprocessor options. 648 StringRef MacroName = ExistingMacroNames[I]; 649 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName]; 650 651 // Check whether we know anything about this macro name or not. 652 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known = 653 ASTFileMacros.find(MacroName); 654 if (!Validate || Known == ASTFileMacros.end()) { 655 // FIXME: Check whether this identifier was referenced anywhere in the 656 // AST file. If so, we should reject the AST file. Unfortunately, this 657 // information isn't in the control block. What shall we do about it? 658 659 if (Existing.second) { 660 SuggestedPredefines += "#undef "; 661 SuggestedPredefines += MacroName.str(); 662 SuggestedPredefines += '\n'; 663 } else { 664 SuggestedPredefines += "#define "; 665 SuggestedPredefines += MacroName.str(); 666 SuggestedPredefines += ' '; 667 SuggestedPredefines += Existing.first.str(); 668 SuggestedPredefines += '\n'; 669 } 670 continue; 671 } 672 673 // If the macro was defined in one but undef'd in the other, we have a 674 // conflict. 675 if (Existing.second != Known->second.second) { 676 if (Diags) { 677 Diags->Report(diag::err_pch_macro_def_undef) 678 << MacroName << Known->second.second; 679 } 680 return true; 681 } 682 683 // If the macro was #undef'd in both, or if the macro bodies are identical, 684 // it's fine. 685 if (Existing.second || Existing.first == Known->second.first) 686 continue; 687 688 // The macro bodies differ; complain. 689 if (Diags) { 690 Diags->Report(diag::err_pch_macro_def_conflict) 691 << MacroName << Known->second.first << Existing.first; 692 } 693 return true; 694 } 695 696 // Check whether we're using predefines. 697 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) { 698 if (Diags) { 699 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines; 700 } 701 return true; 702 } 703 704 // Detailed record is important since it is used for the module cache hash. 705 if (LangOpts.Modules && 706 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) { 707 if (Diags) { 708 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord; 709 } 710 return true; 711 } 712 713 // Compute the #include and #include_macros lines we need. 714 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) { 715 StringRef File = ExistingPPOpts.Includes[I]; 716 717 if (!ExistingPPOpts.ImplicitPCHInclude.empty() && 718 !ExistingPPOpts.PCHThroughHeader.empty()) { 719 // In case the through header is an include, we must add all the includes 720 // to the predefines so the start point can be determined. 721 SuggestedPredefines += "#include \""; 722 SuggestedPredefines += File; 723 SuggestedPredefines += "\"\n"; 724 continue; 725 } 726 727 if (File == ExistingPPOpts.ImplicitPCHInclude) 728 continue; 729 730 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File) 731 != PPOpts.Includes.end()) 732 continue; 733 734 SuggestedPredefines += "#include \""; 735 SuggestedPredefines += File; 736 SuggestedPredefines += "\"\n"; 737 } 738 739 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) { 740 StringRef File = ExistingPPOpts.MacroIncludes[I]; 741 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(), 742 File) 743 != PPOpts.MacroIncludes.end()) 744 continue; 745 746 SuggestedPredefines += "#__include_macros \""; 747 SuggestedPredefines += File; 748 SuggestedPredefines += "\"\n##\n"; 749 } 750 751 return false; 752 } 753 754 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 755 bool Complain, 756 std::string &SuggestedPredefines) { 757 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts(); 758 759 return checkPreprocessorOptions(PPOpts, ExistingPPOpts, 760 Complain? &Reader.Diags : nullptr, 761 PP.getFileManager(), 762 SuggestedPredefines, 763 PP.getLangOpts()); 764 } 765 766 bool SimpleASTReaderListener::ReadPreprocessorOptions( 767 const PreprocessorOptions &PPOpts, 768 bool Complain, 769 std::string &SuggestedPredefines) { 770 return checkPreprocessorOptions(PPOpts, 771 PP.getPreprocessorOpts(), 772 nullptr, 773 PP.getFileManager(), 774 SuggestedPredefines, 775 PP.getLangOpts(), 776 false); 777 } 778 779 /// Check the header search options deserialized from the control block 780 /// against the header search options in an existing preprocessor. 781 /// 782 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 783 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 784 StringRef SpecificModuleCachePath, 785 StringRef ExistingModuleCachePath, 786 DiagnosticsEngine *Diags, 787 const LangOptions &LangOpts, 788 const PreprocessorOptions &PPOpts) { 789 if (LangOpts.Modules) { 790 if (SpecificModuleCachePath != ExistingModuleCachePath && 791 !PPOpts.AllowPCHWithDifferentModulesCachePath) { 792 if (Diags) 793 Diags->Report(diag::err_pch_modulecache_mismatch) 794 << SpecificModuleCachePath << ExistingModuleCachePath; 795 return true; 796 } 797 } 798 799 return false; 800 } 801 802 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 803 StringRef SpecificModuleCachePath, 804 bool Complain) { 805 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 806 PP.getHeaderSearchInfo().getModuleCachePath(), 807 Complain ? &Reader.Diags : nullptr, 808 PP.getLangOpts(), PP.getPreprocessorOpts()); 809 } 810 811 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) { 812 PP.setCounterValue(Value); 813 } 814 815 //===----------------------------------------------------------------------===// 816 // AST reader implementation 817 //===----------------------------------------------------------------------===// 818 819 static uint64_t readULEB(const unsigned char *&P) { 820 unsigned Length = 0; 821 const char *Error = nullptr; 822 823 uint64_t Val = llvm::decodeULEB128(P, &Length, nullptr, &Error); 824 if (Error) 825 llvm::report_fatal_error(Error); 826 P += Length; 827 return Val; 828 } 829 830 /// Read ULEB-encoded key length and data length. 831 static std::pair<unsigned, unsigned> 832 readULEBKeyDataLength(const unsigned char *&P) { 833 unsigned KeyLen = readULEB(P); 834 if ((unsigned)KeyLen != KeyLen) 835 llvm::report_fatal_error("key too large"); 836 837 unsigned DataLen = readULEB(P); 838 if ((unsigned)DataLen != DataLen) 839 llvm::report_fatal_error("data too large"); 840 841 return std::make_pair(KeyLen, DataLen); 842 } 843 844 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener, 845 bool TakeOwnership) { 846 DeserializationListener = Listener; 847 OwnsDeserializationListener = TakeOwnership; 848 } 849 850 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) { 851 return serialization::ComputeHash(Sel); 852 } 853 854 std::pair<unsigned, unsigned> 855 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 856 return readULEBKeyDataLength(d); 857 } 858 859 ASTSelectorLookupTrait::internal_key_type 860 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) { 861 using namespace llvm::support; 862 863 SelectorTable &SelTable = Reader.getContext().Selectors; 864 unsigned N = endian::readNext<uint16_t, little, unaligned>(d); 865 IdentifierInfo *FirstII = Reader.getLocalIdentifier( 866 F, endian::readNext<uint32_t, little, unaligned>(d)); 867 if (N == 0) 868 return SelTable.getNullarySelector(FirstII); 869 else if (N == 1) 870 return SelTable.getUnarySelector(FirstII); 871 872 SmallVector<IdentifierInfo *, 16> Args; 873 Args.push_back(FirstII); 874 for (unsigned I = 1; I != N; ++I) 875 Args.push_back(Reader.getLocalIdentifier( 876 F, endian::readNext<uint32_t, little, unaligned>(d))); 877 878 return SelTable.getSelector(N, Args.data()); 879 } 880 881 ASTSelectorLookupTrait::data_type 882 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d, 883 unsigned DataLen) { 884 using namespace llvm::support; 885 886 data_type Result; 887 888 Result.ID = Reader.getGlobalSelectorID( 889 F, endian::readNext<uint32_t, little, unaligned>(d)); 890 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d); 891 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d); 892 Result.InstanceBits = FullInstanceBits & 0x3; 893 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1; 894 Result.FactoryBits = FullFactoryBits & 0x3; 895 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1; 896 unsigned NumInstanceMethods = FullInstanceBits >> 3; 897 unsigned NumFactoryMethods = FullFactoryBits >> 3; 898 899 // Load instance methods 900 for (unsigned I = 0; I != NumInstanceMethods; ++I) { 901 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 902 F, endian::readNext<uint32_t, little, unaligned>(d))) 903 Result.Instance.push_back(Method); 904 } 905 906 // Load factory methods 907 for (unsigned I = 0; I != NumFactoryMethods; ++I) { 908 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 909 F, endian::readNext<uint32_t, little, unaligned>(d))) 910 Result.Factory.push_back(Method); 911 } 912 913 return Result; 914 } 915 916 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) { 917 return llvm::djbHash(a); 918 } 919 920 std::pair<unsigned, unsigned> 921 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) { 922 return readULEBKeyDataLength(d); 923 } 924 925 ASTIdentifierLookupTraitBase::internal_key_type 926 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) { 927 assert(n >= 2 && d[n-1] == '\0'); 928 return StringRef((const char*) d, n-1); 929 } 930 931 /// Whether the given identifier is "interesting". 932 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II, 933 bool IsModule) { 934 return II.hadMacroDefinition() || II.isPoisoned() || 935 (!IsModule && II.getObjCOrBuiltinID()) || 936 II.hasRevertedTokenIDToIdentifier() || 937 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) && 938 II.getFETokenInfo()); 939 } 940 941 static bool readBit(unsigned &Bits) { 942 bool Value = Bits & 0x1; 943 Bits >>= 1; 944 return Value; 945 } 946 947 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) { 948 using namespace llvm::support; 949 950 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 951 return Reader.getGlobalIdentifierID(F, RawID >> 1); 952 } 953 954 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) { 955 if (!II.isFromAST()) { 956 II.setIsFromAST(); 957 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr; 958 if (isInterestingIdentifier(Reader, II, IsModule)) 959 II.setChangedSinceDeserialization(); 960 } 961 } 962 963 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k, 964 const unsigned char* d, 965 unsigned DataLen) { 966 using namespace llvm::support; 967 968 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 969 bool IsInteresting = RawID & 0x01; 970 971 // Wipe out the "is interesting" bit. 972 RawID = RawID >> 1; 973 974 // Build the IdentifierInfo and link the identifier ID with it. 975 IdentifierInfo *II = KnownII; 976 if (!II) { 977 II = &Reader.getIdentifierTable().getOwn(k); 978 KnownII = II; 979 } 980 markIdentifierFromAST(Reader, *II); 981 Reader.markIdentifierUpToDate(II); 982 983 IdentID ID = Reader.getGlobalIdentifierID(F, RawID); 984 if (!IsInteresting) { 985 // For uninteresting identifiers, there's nothing else to do. Just notify 986 // the reader that we've finished loading this identifier. 987 Reader.SetIdentifierInfo(ID, II); 988 return II; 989 } 990 991 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d); 992 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d); 993 bool CPlusPlusOperatorKeyword = readBit(Bits); 994 bool HasRevertedTokenIDToIdentifier = readBit(Bits); 995 bool Poisoned = readBit(Bits); 996 bool ExtensionToken = readBit(Bits); 997 bool HadMacroDefinition = readBit(Bits); 998 999 assert(Bits == 0 && "Extra bits in the identifier?"); 1000 DataLen -= 8; 1001 1002 // Set or check the various bits in the IdentifierInfo structure. 1003 // Token IDs are read-only. 1004 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier) 1005 II->revertTokenIDToIdentifier(); 1006 if (!F.isModule()) 1007 II->setObjCOrBuiltinID(ObjCOrBuiltinID); 1008 assert(II->isExtensionToken() == ExtensionToken && 1009 "Incorrect extension token flag"); 1010 (void)ExtensionToken; 1011 if (Poisoned) 1012 II->setIsPoisoned(true); 1013 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword && 1014 "Incorrect C++ operator keyword flag"); 1015 (void)CPlusPlusOperatorKeyword; 1016 1017 // If this identifier is a macro, deserialize the macro 1018 // definition. 1019 if (HadMacroDefinition) { 1020 uint32_t MacroDirectivesOffset = 1021 endian::readNext<uint32_t, little, unaligned>(d); 1022 DataLen -= 4; 1023 1024 Reader.addPendingMacro(II, &F, MacroDirectivesOffset); 1025 } 1026 1027 Reader.SetIdentifierInfo(ID, II); 1028 1029 // Read all of the declarations visible at global scope with this 1030 // name. 1031 if (DataLen > 0) { 1032 SmallVector<uint32_t, 4> DeclIDs; 1033 for (; DataLen > 0; DataLen -= 4) 1034 DeclIDs.push_back(Reader.getGlobalDeclID( 1035 F, endian::readNext<uint32_t, little, unaligned>(d))); 1036 Reader.SetGloballyVisibleDecls(II, DeclIDs); 1037 } 1038 1039 return II; 1040 } 1041 1042 DeclarationNameKey::DeclarationNameKey(DeclarationName Name) 1043 : Kind(Name.getNameKind()) { 1044 switch (Kind) { 1045 case DeclarationName::Identifier: 1046 Data = (uint64_t)Name.getAsIdentifierInfo(); 1047 break; 1048 case DeclarationName::ObjCZeroArgSelector: 1049 case DeclarationName::ObjCOneArgSelector: 1050 case DeclarationName::ObjCMultiArgSelector: 1051 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr(); 1052 break; 1053 case DeclarationName::CXXOperatorName: 1054 Data = Name.getCXXOverloadedOperator(); 1055 break; 1056 case DeclarationName::CXXLiteralOperatorName: 1057 Data = (uint64_t)Name.getCXXLiteralIdentifier(); 1058 break; 1059 case DeclarationName::CXXDeductionGuideName: 1060 Data = (uint64_t)Name.getCXXDeductionGuideTemplate() 1061 ->getDeclName().getAsIdentifierInfo(); 1062 break; 1063 case DeclarationName::CXXConstructorName: 1064 case DeclarationName::CXXDestructorName: 1065 case DeclarationName::CXXConversionFunctionName: 1066 case DeclarationName::CXXUsingDirective: 1067 Data = 0; 1068 break; 1069 } 1070 } 1071 1072 unsigned DeclarationNameKey::getHash() const { 1073 llvm::FoldingSetNodeID ID; 1074 ID.AddInteger(Kind); 1075 1076 switch (Kind) { 1077 case DeclarationName::Identifier: 1078 case DeclarationName::CXXLiteralOperatorName: 1079 case DeclarationName::CXXDeductionGuideName: 1080 ID.AddString(((IdentifierInfo*)Data)->getName()); 1081 break; 1082 case DeclarationName::ObjCZeroArgSelector: 1083 case DeclarationName::ObjCOneArgSelector: 1084 case DeclarationName::ObjCMultiArgSelector: 1085 ID.AddInteger(serialization::ComputeHash(Selector(Data))); 1086 break; 1087 case DeclarationName::CXXOperatorName: 1088 ID.AddInteger((OverloadedOperatorKind)Data); 1089 break; 1090 case DeclarationName::CXXConstructorName: 1091 case DeclarationName::CXXDestructorName: 1092 case DeclarationName::CXXConversionFunctionName: 1093 case DeclarationName::CXXUsingDirective: 1094 break; 1095 } 1096 1097 return ID.ComputeHash(); 1098 } 1099 1100 ModuleFile * 1101 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) { 1102 using namespace llvm::support; 1103 1104 uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d); 1105 return Reader.getLocalModuleFile(F, ModuleFileID); 1106 } 1107 1108 std::pair<unsigned, unsigned> 1109 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) { 1110 return readULEBKeyDataLength(d); 1111 } 1112 1113 ASTDeclContextNameLookupTrait::internal_key_type 1114 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) { 1115 using namespace llvm::support; 1116 1117 auto Kind = (DeclarationName::NameKind)*d++; 1118 uint64_t Data; 1119 switch (Kind) { 1120 case DeclarationName::Identifier: 1121 case DeclarationName::CXXLiteralOperatorName: 1122 case DeclarationName::CXXDeductionGuideName: 1123 Data = (uint64_t)Reader.getLocalIdentifier( 1124 F, endian::readNext<uint32_t, little, unaligned>(d)); 1125 break; 1126 case DeclarationName::ObjCZeroArgSelector: 1127 case DeclarationName::ObjCOneArgSelector: 1128 case DeclarationName::ObjCMultiArgSelector: 1129 Data = 1130 (uint64_t)Reader.getLocalSelector( 1131 F, endian::readNext<uint32_t, little, unaligned>( 1132 d)).getAsOpaquePtr(); 1133 break; 1134 case DeclarationName::CXXOperatorName: 1135 Data = *d++; // OverloadedOperatorKind 1136 break; 1137 case DeclarationName::CXXConstructorName: 1138 case DeclarationName::CXXDestructorName: 1139 case DeclarationName::CXXConversionFunctionName: 1140 case DeclarationName::CXXUsingDirective: 1141 Data = 0; 1142 break; 1143 } 1144 1145 return DeclarationNameKey(Kind, Data); 1146 } 1147 1148 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type, 1149 const unsigned char *d, 1150 unsigned DataLen, 1151 data_type_builder &Val) { 1152 using namespace llvm::support; 1153 1154 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) { 1155 uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d); 1156 Val.insert(Reader.getGlobalDeclID(F, LocalID)); 1157 } 1158 } 1159 1160 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M, 1161 BitstreamCursor &Cursor, 1162 uint64_t Offset, 1163 DeclContext *DC) { 1164 assert(Offset != 0); 1165 1166 SavedStreamPosition SavedPosition(Cursor); 1167 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1168 Error(std::move(Err)); 1169 return true; 1170 } 1171 1172 RecordData Record; 1173 StringRef Blob; 1174 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1175 if (!MaybeCode) { 1176 Error(MaybeCode.takeError()); 1177 return true; 1178 } 1179 unsigned Code = MaybeCode.get(); 1180 1181 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1182 if (!MaybeRecCode) { 1183 Error(MaybeRecCode.takeError()); 1184 return true; 1185 } 1186 unsigned RecCode = MaybeRecCode.get(); 1187 if (RecCode != DECL_CONTEXT_LEXICAL) { 1188 Error("Expected lexical block"); 1189 return true; 1190 } 1191 1192 assert(!isa<TranslationUnitDecl>(DC) && 1193 "expected a TU_UPDATE_LEXICAL record for TU"); 1194 // If we are handling a C++ class template instantiation, we can see multiple 1195 // lexical updates for the same record. It's important that we select only one 1196 // of them, so that field numbering works properly. Just pick the first one we 1197 // see. 1198 auto &Lex = LexicalDecls[DC]; 1199 if (!Lex.first) { 1200 Lex = std::make_pair( 1201 &M, llvm::makeArrayRef( 1202 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 1203 Blob.data()), 1204 Blob.size() / 4)); 1205 } 1206 DC->setHasExternalLexicalStorage(true); 1207 return false; 1208 } 1209 1210 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M, 1211 BitstreamCursor &Cursor, 1212 uint64_t Offset, 1213 DeclID ID) { 1214 assert(Offset != 0); 1215 1216 SavedStreamPosition SavedPosition(Cursor); 1217 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1218 Error(std::move(Err)); 1219 return true; 1220 } 1221 1222 RecordData Record; 1223 StringRef Blob; 1224 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1225 if (!MaybeCode) { 1226 Error(MaybeCode.takeError()); 1227 return true; 1228 } 1229 unsigned Code = MaybeCode.get(); 1230 1231 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1232 if (!MaybeRecCode) { 1233 Error(MaybeRecCode.takeError()); 1234 return true; 1235 } 1236 unsigned RecCode = MaybeRecCode.get(); 1237 if (RecCode != DECL_CONTEXT_VISIBLE) { 1238 Error("Expected visible lookup table block"); 1239 return true; 1240 } 1241 1242 // We can't safely determine the primary context yet, so delay attaching the 1243 // lookup table until we're done with recursive deserialization. 1244 auto *Data = (const unsigned char*)Blob.data(); 1245 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data}); 1246 return false; 1247 } 1248 1249 void ASTReader::Error(StringRef Msg) const { 1250 Error(diag::err_fe_pch_malformed, Msg); 1251 if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() && 1252 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) { 1253 Diag(diag::note_module_cache_path) 1254 << PP.getHeaderSearchInfo().getModuleCachePath(); 1255 } 1256 } 1257 1258 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2, 1259 StringRef Arg3) const { 1260 if (Diags.isDiagnosticInFlight()) 1261 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3); 1262 else 1263 Diag(DiagID) << Arg1 << Arg2 << Arg3; 1264 } 1265 1266 void ASTReader::Error(llvm::Error &&Err) const { 1267 llvm::Error RemainingErr = 1268 handleErrors(std::move(Err), [this](const DiagnosticError &E) { 1269 auto Diag = E.getDiagnostic().second; 1270 1271 // Ideally we'd just emit it, but have to handle a possible in-flight 1272 // diagnostic. Note that the location is currently ignored as well. 1273 auto NumArgs = Diag.getStorage()->NumDiagArgs; 1274 assert(NumArgs <= 3 && "Can only have up to 3 arguments"); 1275 StringRef Arg1, Arg2, Arg3; 1276 switch (NumArgs) { 1277 case 3: 1278 Arg3 = Diag.getStringArg(2); 1279 LLVM_FALLTHROUGH; 1280 case 2: 1281 Arg2 = Diag.getStringArg(1); 1282 LLVM_FALLTHROUGH; 1283 case 1: 1284 Arg1 = Diag.getStringArg(0); 1285 } 1286 Error(Diag.getDiagID(), Arg1, Arg2, Arg3); 1287 }); 1288 if (RemainingErr) 1289 Error(toString(std::move(RemainingErr))); 1290 } 1291 1292 //===----------------------------------------------------------------------===// 1293 // Source Manager Deserialization 1294 //===----------------------------------------------------------------------===// 1295 1296 /// Read the line table in the source manager block. 1297 void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) { 1298 unsigned Idx = 0; 1299 LineTableInfo &LineTable = SourceMgr.getLineTable(); 1300 1301 // Parse the file names 1302 std::map<int, int> FileIDs; 1303 FileIDs[-1] = -1; // For unspecified filenames. 1304 for (unsigned I = 0; Record[Idx]; ++I) { 1305 // Extract the file name 1306 auto Filename = ReadPath(F, Record, Idx); 1307 FileIDs[I] = LineTable.getLineTableFilenameID(Filename); 1308 } 1309 ++Idx; 1310 1311 // Parse the line entries 1312 std::vector<LineEntry> Entries; 1313 while (Idx < Record.size()) { 1314 int FID = Record[Idx++]; 1315 assert(FID >= 0 && "Serialized line entries for non-local file."); 1316 // Remap FileID from 1-based old view. 1317 FID += F.SLocEntryBaseID - 1; 1318 1319 // Extract the line entries 1320 unsigned NumEntries = Record[Idx++]; 1321 assert(NumEntries && "no line entries for file ID"); 1322 Entries.clear(); 1323 Entries.reserve(NumEntries); 1324 for (unsigned I = 0; I != NumEntries; ++I) { 1325 unsigned FileOffset = Record[Idx++]; 1326 unsigned LineNo = Record[Idx++]; 1327 int FilenameID = FileIDs[Record[Idx++]]; 1328 SrcMgr::CharacteristicKind FileKind 1329 = (SrcMgr::CharacteristicKind)Record[Idx++]; 1330 unsigned IncludeOffset = Record[Idx++]; 1331 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, 1332 FileKind, IncludeOffset)); 1333 } 1334 LineTable.AddEntry(FileID::get(FID), Entries); 1335 } 1336 } 1337 1338 /// Read a source manager block 1339 llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) { 1340 using namespace SrcMgr; 1341 1342 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor; 1343 1344 // Set the source-location entry cursor to the current position in 1345 // the stream. This cursor will be used to read the contents of the 1346 // source manager block initially, and then lazily read 1347 // source-location entries as needed. 1348 SLocEntryCursor = F.Stream; 1349 1350 // The stream itself is going to skip over the source manager block. 1351 if (llvm::Error Err = F.Stream.SkipBlock()) 1352 return Err; 1353 1354 // Enter the source manager block. 1355 if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) 1356 return Err; 1357 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo(); 1358 1359 RecordData Record; 1360 while (true) { 1361 Expected<llvm::BitstreamEntry> MaybeE = 1362 SLocEntryCursor.advanceSkippingSubblocks(); 1363 if (!MaybeE) 1364 return MaybeE.takeError(); 1365 llvm::BitstreamEntry E = MaybeE.get(); 1366 1367 switch (E.Kind) { 1368 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1369 case llvm::BitstreamEntry::Error: 1370 return llvm::createStringError(std::errc::illegal_byte_sequence, 1371 "malformed block record in AST file"); 1372 case llvm::BitstreamEntry::EndBlock: 1373 return llvm::Error::success(); 1374 case llvm::BitstreamEntry::Record: 1375 // The interesting case. 1376 break; 1377 } 1378 1379 // Read a record. 1380 Record.clear(); 1381 StringRef Blob; 1382 Expected<unsigned> MaybeRecord = 1383 SLocEntryCursor.readRecord(E.ID, Record, &Blob); 1384 if (!MaybeRecord) 1385 return MaybeRecord.takeError(); 1386 switch (MaybeRecord.get()) { 1387 default: // Default behavior: ignore. 1388 break; 1389 1390 case SM_SLOC_FILE_ENTRY: 1391 case SM_SLOC_BUFFER_ENTRY: 1392 case SM_SLOC_EXPANSION_ENTRY: 1393 // Once we hit one of the source location entries, we're done. 1394 return llvm::Error::success(); 1395 } 1396 } 1397 } 1398 1399 /// If a header file is not found at the path that we expect it to be 1400 /// and the PCH file was moved from its original location, try to resolve the 1401 /// file by assuming that header+PCH were moved together and the header is in 1402 /// the same place relative to the PCH. 1403 static std::string 1404 resolveFileRelativeToOriginalDir(const std::string &Filename, 1405 const std::string &OriginalDir, 1406 const std::string &CurrDir) { 1407 assert(OriginalDir != CurrDir && 1408 "No point trying to resolve the file if the PCH dir didn't change"); 1409 1410 using namespace llvm::sys; 1411 1412 SmallString<128> filePath(Filename); 1413 fs::make_absolute(filePath); 1414 assert(path::is_absolute(OriginalDir)); 1415 SmallString<128> currPCHPath(CurrDir); 1416 1417 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)), 1418 fileDirE = path::end(path::parent_path(filePath)); 1419 path::const_iterator origDirI = path::begin(OriginalDir), 1420 origDirE = path::end(OriginalDir); 1421 // Skip the common path components from filePath and OriginalDir. 1422 while (fileDirI != fileDirE && origDirI != origDirE && 1423 *fileDirI == *origDirI) { 1424 ++fileDirI; 1425 ++origDirI; 1426 } 1427 for (; origDirI != origDirE; ++origDirI) 1428 path::append(currPCHPath, ".."); 1429 path::append(currPCHPath, fileDirI, fileDirE); 1430 path::append(currPCHPath, path::filename(Filename)); 1431 return std::string(currPCHPath.str()); 1432 } 1433 1434 bool ASTReader::ReadSLocEntry(int ID) { 1435 if (ID == 0) 1436 return false; 1437 1438 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1439 Error("source location entry ID out-of-range for AST file"); 1440 return true; 1441 } 1442 1443 // Local helper to read the (possibly-compressed) buffer data following the 1444 // entry record. 1445 auto ReadBuffer = [this]( 1446 BitstreamCursor &SLocEntryCursor, 1447 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> { 1448 RecordData Record; 1449 StringRef Blob; 1450 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode(); 1451 if (!MaybeCode) { 1452 Error(MaybeCode.takeError()); 1453 return nullptr; 1454 } 1455 unsigned Code = MaybeCode.get(); 1456 1457 Expected<unsigned> MaybeRecCode = 1458 SLocEntryCursor.readRecord(Code, Record, &Blob); 1459 if (!MaybeRecCode) { 1460 Error(MaybeRecCode.takeError()); 1461 return nullptr; 1462 } 1463 unsigned RecCode = MaybeRecCode.get(); 1464 1465 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) { 1466 if (!llvm::zlib::isAvailable()) { 1467 Error("zlib is not available"); 1468 return nullptr; 1469 } 1470 SmallString<0> Uncompressed; 1471 if (llvm::Error E = 1472 llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) { 1473 Error("could not decompress embedded file contents: " + 1474 llvm::toString(std::move(E))); 1475 return nullptr; 1476 } 1477 return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name); 1478 } else if (RecCode == SM_SLOC_BUFFER_BLOB) { 1479 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true); 1480 } else { 1481 Error("AST record has invalid code"); 1482 return nullptr; 1483 } 1484 }; 1485 1486 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second; 1487 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit( 1488 F->SLocEntryOffsetsBase + 1489 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) { 1490 Error(std::move(Err)); 1491 return true; 1492 } 1493 1494 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor; 1495 SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset; 1496 1497 ++NumSLocEntriesRead; 1498 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance(); 1499 if (!MaybeEntry) { 1500 Error(MaybeEntry.takeError()); 1501 return true; 1502 } 1503 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1504 1505 if (Entry.Kind != llvm::BitstreamEntry::Record) { 1506 Error("incorrectly-formatted source location entry in AST file"); 1507 return true; 1508 } 1509 1510 RecordData Record; 1511 StringRef Blob; 1512 Expected<unsigned> MaybeSLOC = 1513 SLocEntryCursor.readRecord(Entry.ID, Record, &Blob); 1514 if (!MaybeSLOC) { 1515 Error(MaybeSLOC.takeError()); 1516 return true; 1517 } 1518 switch (MaybeSLOC.get()) { 1519 default: 1520 Error("incorrectly-formatted source location entry in AST file"); 1521 return true; 1522 1523 case SM_SLOC_FILE_ENTRY: { 1524 // We will detect whether a file changed and return 'Failure' for it, but 1525 // we will also try to fail gracefully by setting up the SLocEntry. 1526 unsigned InputID = Record[4]; 1527 InputFile IF = getInputFile(*F, InputID); 1528 Optional<FileEntryRef> File = IF.getFile(); 1529 bool OverriddenBuffer = IF.isOverridden(); 1530 1531 // Note that we only check if a File was returned. If it was out-of-date 1532 // we have complained but we will continue creating a FileID to recover 1533 // gracefully. 1534 if (!File) 1535 return true; 1536 1537 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1538 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) { 1539 // This is the module's main file. 1540 IncludeLoc = getImportLocation(F); 1541 } 1542 SrcMgr::CharacteristicKind 1543 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1544 FileID FID = SourceMgr.createFileID(*File, IncludeLoc, FileCharacter, ID, 1545 BaseOffset + Record[0]); 1546 SrcMgr::FileInfo &FileInfo = 1547 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()); 1548 FileInfo.NumCreatedFIDs = Record[5]; 1549 if (Record[3]) 1550 FileInfo.setHasLineDirectives(); 1551 1552 unsigned NumFileDecls = Record[7]; 1553 if (NumFileDecls && ContextObj) { 1554 const DeclID *FirstDecl = F->FileSortedDecls + Record[6]; 1555 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?"); 1556 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl, 1557 NumFileDecls)); 1558 } 1559 1560 const SrcMgr::ContentCache &ContentCache = 1561 SourceMgr.getOrCreateContentCache(*File, isSystem(FileCharacter)); 1562 if (OverriddenBuffer && !ContentCache.BufferOverridden && 1563 ContentCache.ContentsEntry == ContentCache.OrigEntry && 1564 !ContentCache.getBufferIfLoaded()) { 1565 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName()); 1566 if (!Buffer) 1567 return true; 1568 SourceMgr.overrideFileContents(*File, std::move(Buffer)); 1569 } 1570 1571 break; 1572 } 1573 1574 case SM_SLOC_BUFFER_ENTRY: { 1575 const char *Name = Blob.data(); 1576 unsigned Offset = Record[0]; 1577 SrcMgr::CharacteristicKind 1578 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1579 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1580 if (IncludeLoc.isInvalid() && F->isModule()) { 1581 IncludeLoc = getImportLocation(F); 1582 } 1583 1584 auto Buffer = ReadBuffer(SLocEntryCursor, Name); 1585 if (!Buffer) 1586 return true; 1587 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID, 1588 BaseOffset + Offset, IncludeLoc); 1589 break; 1590 } 1591 1592 case SM_SLOC_EXPANSION_ENTRY: { 1593 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]); 1594 SourceMgr.createExpansionLoc(SpellingLoc, 1595 ReadSourceLocation(*F, Record[2]), 1596 ReadSourceLocation(*F, Record[3]), 1597 Record[5], 1598 Record[4], 1599 ID, 1600 BaseOffset + Record[0]); 1601 break; 1602 } 1603 } 1604 1605 return false; 1606 } 1607 1608 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) { 1609 if (ID == 0) 1610 return std::make_pair(SourceLocation(), ""); 1611 1612 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1613 Error("source location entry ID out-of-range for AST file"); 1614 return std::make_pair(SourceLocation(), ""); 1615 } 1616 1617 // Find which module file this entry lands in. 1618 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second; 1619 if (!M->isModule()) 1620 return std::make_pair(SourceLocation(), ""); 1621 1622 // FIXME: Can we map this down to a particular submodule? That would be 1623 // ideal. 1624 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName)); 1625 } 1626 1627 /// Find the location where the module F is imported. 1628 SourceLocation ASTReader::getImportLocation(ModuleFile *F) { 1629 if (F->ImportLoc.isValid()) 1630 return F->ImportLoc; 1631 1632 // Otherwise we have a PCH. It's considered to be "imported" at the first 1633 // location of its includer. 1634 if (F->ImportedBy.empty() || !F->ImportedBy[0]) { 1635 // Main file is the importer. 1636 assert(SourceMgr.getMainFileID().isValid() && "missing main file"); 1637 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); 1638 } 1639 return F->ImportedBy[0]->FirstLoc; 1640 } 1641 1642 /// Enter a subblock of the specified BlockID with the specified cursor. Read 1643 /// the abbreviations that are at the top of the block and then leave the cursor 1644 /// pointing into the block. 1645 llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, 1646 unsigned BlockID, 1647 uint64_t *StartOfBlockOffset) { 1648 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) 1649 return Err; 1650 1651 if (StartOfBlockOffset) 1652 *StartOfBlockOffset = Cursor.GetCurrentBitNo(); 1653 1654 while (true) { 1655 uint64_t Offset = Cursor.GetCurrentBitNo(); 1656 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1657 if (!MaybeCode) 1658 return MaybeCode.takeError(); 1659 unsigned Code = MaybeCode.get(); 1660 1661 // We expect all abbrevs to be at the start of the block. 1662 if (Code != llvm::bitc::DEFINE_ABBREV) { 1663 if (llvm::Error Err = Cursor.JumpToBit(Offset)) 1664 return Err; 1665 return llvm::Error::success(); 1666 } 1667 if (llvm::Error Err = Cursor.ReadAbbrevRecord()) 1668 return Err; 1669 } 1670 } 1671 1672 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record, 1673 unsigned &Idx) { 1674 Token Tok; 1675 Tok.startToken(); 1676 Tok.setLocation(ReadSourceLocation(F, Record, Idx)); 1677 Tok.setLength(Record[Idx++]); 1678 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++])) 1679 Tok.setIdentifierInfo(II); 1680 Tok.setKind((tok::TokenKind)Record[Idx++]); 1681 Tok.setFlag((Token::TokenFlags)Record[Idx++]); 1682 return Tok; 1683 } 1684 1685 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) { 1686 BitstreamCursor &Stream = F.MacroCursor; 1687 1688 // Keep track of where we are in the stream, then jump back there 1689 // after reading this macro. 1690 SavedStreamPosition SavedPosition(Stream); 1691 1692 if (llvm::Error Err = Stream.JumpToBit(Offset)) { 1693 // FIXME this drops errors on the floor. 1694 consumeError(std::move(Err)); 1695 return nullptr; 1696 } 1697 RecordData Record; 1698 SmallVector<IdentifierInfo*, 16> MacroParams; 1699 MacroInfo *Macro = nullptr; 1700 1701 while (true) { 1702 // Advance to the next record, but if we get to the end of the block, don't 1703 // pop it (removing all the abbreviations from the cursor) since we want to 1704 // be able to reseek within the block and read entries. 1705 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd; 1706 Expected<llvm::BitstreamEntry> MaybeEntry = 1707 Stream.advanceSkippingSubblocks(Flags); 1708 if (!MaybeEntry) { 1709 Error(MaybeEntry.takeError()); 1710 return Macro; 1711 } 1712 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1713 1714 switch (Entry.Kind) { 1715 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1716 case llvm::BitstreamEntry::Error: 1717 Error("malformed block record in AST file"); 1718 return Macro; 1719 case llvm::BitstreamEntry::EndBlock: 1720 return Macro; 1721 case llvm::BitstreamEntry::Record: 1722 // The interesting case. 1723 break; 1724 } 1725 1726 // Read a record. 1727 Record.clear(); 1728 PreprocessorRecordTypes RecType; 1729 if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record)) 1730 RecType = (PreprocessorRecordTypes)MaybeRecType.get(); 1731 else { 1732 Error(MaybeRecType.takeError()); 1733 return Macro; 1734 } 1735 switch (RecType) { 1736 case PP_MODULE_MACRO: 1737 case PP_MACRO_DIRECTIVE_HISTORY: 1738 return Macro; 1739 1740 case PP_MACRO_OBJECT_LIKE: 1741 case PP_MACRO_FUNCTION_LIKE: { 1742 // If we already have a macro, that means that we've hit the end 1743 // of the definition of the macro we were looking for. We're 1744 // done. 1745 if (Macro) 1746 return Macro; 1747 1748 unsigned NextIndex = 1; // Skip identifier ID. 1749 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex); 1750 MacroInfo *MI = PP.AllocateMacroInfo(Loc); 1751 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex)); 1752 MI->setIsUsed(Record[NextIndex++]); 1753 MI->setUsedForHeaderGuard(Record[NextIndex++]); 1754 1755 if (RecType == PP_MACRO_FUNCTION_LIKE) { 1756 // Decode function-like macro info. 1757 bool isC99VarArgs = Record[NextIndex++]; 1758 bool isGNUVarArgs = Record[NextIndex++]; 1759 bool hasCommaPasting = Record[NextIndex++]; 1760 MacroParams.clear(); 1761 unsigned NumArgs = Record[NextIndex++]; 1762 for (unsigned i = 0; i != NumArgs; ++i) 1763 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++])); 1764 1765 // Install function-like macro info. 1766 MI->setIsFunctionLike(); 1767 if (isC99VarArgs) MI->setIsC99Varargs(); 1768 if (isGNUVarArgs) MI->setIsGNUVarargs(); 1769 if (hasCommaPasting) MI->setHasCommaPasting(); 1770 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator()); 1771 } 1772 1773 // Remember that we saw this macro last so that we add the tokens that 1774 // form its body to it. 1775 Macro = MI; 1776 1777 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() && 1778 Record[NextIndex]) { 1779 // We have a macro definition. Register the association 1780 PreprocessedEntityID 1781 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]); 1782 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 1783 PreprocessingRecord::PPEntityID PPID = 1784 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true); 1785 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>( 1786 PPRec.getPreprocessedEntity(PPID)); 1787 if (PPDef) 1788 PPRec.RegisterMacroDefinition(Macro, PPDef); 1789 } 1790 1791 ++NumMacrosRead; 1792 break; 1793 } 1794 1795 case PP_TOKEN: { 1796 // If we see a TOKEN before a PP_MACRO_*, then the file is 1797 // erroneous, just pretend we didn't see this. 1798 if (!Macro) break; 1799 1800 unsigned Idx = 0; 1801 Token Tok = ReadToken(F, Record, Idx); 1802 Macro->AddTokenToBody(Tok); 1803 break; 1804 } 1805 } 1806 } 1807 } 1808 1809 PreprocessedEntityID 1810 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, 1811 unsigned LocalID) const { 1812 if (!M.ModuleOffsetMap.empty()) 1813 ReadModuleOffsetMap(M); 1814 1815 ContinuousRangeMap<uint32_t, int, 2>::const_iterator 1816 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS); 1817 assert(I != M.PreprocessedEntityRemap.end() 1818 && "Invalid index into preprocessed entity index remap"); 1819 1820 return LocalID + I->second; 1821 } 1822 1823 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) { 1824 return llvm::hash_combine(ikey.Size, ikey.ModTime); 1825 } 1826 1827 HeaderFileInfoTrait::internal_key_type 1828 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) { 1829 internal_key_type ikey = {FE->getSize(), 1830 M.HasTimestamps ? FE->getModificationTime() : 0, 1831 FE->getName(), /*Imported*/ false}; 1832 return ikey; 1833 } 1834 1835 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) { 1836 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime)) 1837 return false; 1838 1839 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename) 1840 return true; 1841 1842 // Determine whether the actual files are equivalent. 1843 FileManager &FileMgr = Reader.getFileManager(); 1844 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* { 1845 if (!Key.Imported) { 1846 if (auto File = FileMgr.getFile(Key.Filename)) 1847 return *File; 1848 return nullptr; 1849 } 1850 1851 std::string Resolved = std::string(Key.Filename); 1852 Reader.ResolveImportedPath(M, Resolved); 1853 if (auto File = FileMgr.getFile(Resolved)) 1854 return *File; 1855 return nullptr; 1856 }; 1857 1858 const FileEntry *FEA = GetFile(a); 1859 const FileEntry *FEB = GetFile(b); 1860 return FEA && FEA == FEB; 1861 } 1862 1863 std::pair<unsigned, unsigned> 1864 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) { 1865 return readULEBKeyDataLength(d); 1866 } 1867 1868 HeaderFileInfoTrait::internal_key_type 1869 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) { 1870 using namespace llvm::support; 1871 1872 internal_key_type ikey; 1873 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d)); 1874 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d)); 1875 ikey.Filename = (const char *)d; 1876 ikey.Imported = true; 1877 return ikey; 1878 } 1879 1880 HeaderFileInfoTrait::data_type 1881 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d, 1882 unsigned DataLen) { 1883 using namespace llvm::support; 1884 1885 const unsigned char *End = d + DataLen; 1886 HeaderFileInfo HFI; 1887 unsigned Flags = *d++; 1888 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp. 1889 HFI.isImport |= (Flags >> 5) & 0x01; 1890 HFI.isPragmaOnce |= (Flags >> 4) & 0x01; 1891 HFI.DirInfo = (Flags >> 1) & 0x07; 1892 HFI.IndexHeaderMapHeader = Flags & 0x01; 1893 // FIXME: Find a better way to handle this. Maybe just store a 1894 // "has been included" flag? 1895 HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d), 1896 HFI.NumIncludes); 1897 HFI.ControllingMacroID = Reader.getGlobalIdentifierID( 1898 M, endian::readNext<uint32_t, little, unaligned>(d)); 1899 if (unsigned FrameworkOffset = 1900 endian::readNext<uint32_t, little, unaligned>(d)) { 1901 // The framework offset is 1 greater than the actual offset, 1902 // since 0 is used as an indicator for "no framework name". 1903 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1); 1904 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName); 1905 } 1906 1907 assert((End - d) % 4 == 0 && 1908 "Wrong data length in HeaderFileInfo deserialization"); 1909 while (d != End) { 1910 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d); 1911 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3); 1912 LocalSMID >>= 2; 1913 1914 // This header is part of a module. Associate it with the module to enable 1915 // implicit module import. 1916 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID); 1917 Module *Mod = Reader.getSubmodule(GlobalSMID); 1918 FileManager &FileMgr = Reader.getFileManager(); 1919 ModuleMap &ModMap = 1920 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap(); 1921 1922 std::string Filename = std::string(key.Filename); 1923 if (key.Imported) 1924 Reader.ResolveImportedPath(M, Filename); 1925 // FIXME: NameAsWritten 1926 Module::Header H = {std::string(key.Filename), "", 1927 *FileMgr.getFile(Filename)}; 1928 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true); 1929 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader); 1930 } 1931 1932 // This HeaderFileInfo was externally loaded. 1933 HFI.External = true; 1934 HFI.IsValid = true; 1935 return HFI; 1936 } 1937 1938 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M, 1939 uint32_t MacroDirectivesOffset) { 1940 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard"); 1941 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset)); 1942 } 1943 1944 void ASTReader::ReadDefinedMacros() { 1945 // Note that we are loading defined macros. 1946 Deserializing Macros(this); 1947 1948 for (ModuleFile &I : llvm::reverse(ModuleMgr)) { 1949 BitstreamCursor &MacroCursor = I.MacroCursor; 1950 1951 // If there was no preprocessor block, skip this file. 1952 if (MacroCursor.getBitcodeBytes().empty()) 1953 continue; 1954 1955 BitstreamCursor Cursor = MacroCursor; 1956 if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) { 1957 Error(std::move(Err)); 1958 return; 1959 } 1960 1961 RecordData Record; 1962 while (true) { 1963 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks(); 1964 if (!MaybeE) { 1965 Error(MaybeE.takeError()); 1966 return; 1967 } 1968 llvm::BitstreamEntry E = MaybeE.get(); 1969 1970 switch (E.Kind) { 1971 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1972 case llvm::BitstreamEntry::Error: 1973 Error("malformed block record in AST file"); 1974 return; 1975 case llvm::BitstreamEntry::EndBlock: 1976 goto NextCursor; 1977 1978 case llvm::BitstreamEntry::Record: { 1979 Record.clear(); 1980 Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record); 1981 if (!MaybeRecord) { 1982 Error(MaybeRecord.takeError()); 1983 return; 1984 } 1985 switch (MaybeRecord.get()) { 1986 default: // Default behavior: ignore. 1987 break; 1988 1989 case PP_MACRO_OBJECT_LIKE: 1990 case PP_MACRO_FUNCTION_LIKE: { 1991 IdentifierInfo *II = getLocalIdentifier(I, Record[0]); 1992 if (II->isOutOfDate()) 1993 updateOutOfDateIdentifier(*II); 1994 break; 1995 } 1996 1997 case PP_TOKEN: 1998 // Ignore tokens. 1999 break; 2000 } 2001 break; 2002 } 2003 } 2004 } 2005 NextCursor: ; 2006 } 2007 } 2008 2009 namespace { 2010 2011 /// Visitor class used to look up identifirs in an AST file. 2012 class IdentifierLookupVisitor { 2013 StringRef Name; 2014 unsigned NameHash; 2015 unsigned PriorGeneration; 2016 unsigned &NumIdentifierLookups; 2017 unsigned &NumIdentifierLookupHits; 2018 IdentifierInfo *Found = nullptr; 2019 2020 public: 2021 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration, 2022 unsigned &NumIdentifierLookups, 2023 unsigned &NumIdentifierLookupHits) 2024 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)), 2025 PriorGeneration(PriorGeneration), 2026 NumIdentifierLookups(NumIdentifierLookups), 2027 NumIdentifierLookupHits(NumIdentifierLookupHits) {} 2028 2029 bool operator()(ModuleFile &M) { 2030 // If we've already searched this module file, skip it now. 2031 if (M.Generation <= PriorGeneration) 2032 return true; 2033 2034 ASTIdentifierLookupTable *IdTable 2035 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable; 2036 if (!IdTable) 2037 return false; 2038 2039 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M, 2040 Found); 2041 ++NumIdentifierLookups; 2042 ASTIdentifierLookupTable::iterator Pos = 2043 IdTable->find_hashed(Name, NameHash, &Trait); 2044 if (Pos == IdTable->end()) 2045 return false; 2046 2047 // Dereferencing the iterator has the effect of building the 2048 // IdentifierInfo node and populating it with the various 2049 // declarations it needs. 2050 ++NumIdentifierLookupHits; 2051 Found = *Pos; 2052 return true; 2053 } 2054 2055 // Retrieve the identifier info found within the module 2056 // files. 2057 IdentifierInfo *getIdentifierInfo() const { return Found; } 2058 }; 2059 2060 } // namespace 2061 2062 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) { 2063 // Note that we are loading an identifier. 2064 Deserializing AnIdentifier(this); 2065 2066 unsigned PriorGeneration = 0; 2067 if (getContext().getLangOpts().Modules) 2068 PriorGeneration = IdentifierGeneration[&II]; 2069 2070 // If there is a global index, look there first to determine which modules 2071 // provably do not have any results for this identifier. 2072 GlobalModuleIndex::HitSet Hits; 2073 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 2074 if (!loadGlobalIndex()) { 2075 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) { 2076 HitsPtr = &Hits; 2077 } 2078 } 2079 2080 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration, 2081 NumIdentifierLookups, 2082 NumIdentifierLookupHits); 2083 ModuleMgr.visit(Visitor, HitsPtr); 2084 markIdentifierUpToDate(&II); 2085 } 2086 2087 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) { 2088 if (!II) 2089 return; 2090 2091 II->setOutOfDate(false); 2092 2093 // Update the generation for this identifier. 2094 if (getContext().getLangOpts().Modules) 2095 IdentifierGeneration[II] = getGeneration(); 2096 } 2097 2098 void ASTReader::resolvePendingMacro(IdentifierInfo *II, 2099 const PendingMacroInfo &PMInfo) { 2100 ModuleFile &M = *PMInfo.M; 2101 2102 BitstreamCursor &Cursor = M.MacroCursor; 2103 SavedStreamPosition SavedPosition(Cursor); 2104 if (llvm::Error Err = 2105 Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) { 2106 Error(std::move(Err)); 2107 return; 2108 } 2109 2110 struct ModuleMacroRecord { 2111 SubmoduleID SubModID; 2112 MacroInfo *MI; 2113 SmallVector<SubmoduleID, 8> Overrides; 2114 }; 2115 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros; 2116 2117 // We expect to see a sequence of PP_MODULE_MACRO records listing exported 2118 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete 2119 // macro histroy. 2120 RecordData Record; 2121 while (true) { 2122 Expected<llvm::BitstreamEntry> MaybeEntry = 2123 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 2124 if (!MaybeEntry) { 2125 Error(MaybeEntry.takeError()); 2126 return; 2127 } 2128 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2129 2130 if (Entry.Kind != llvm::BitstreamEntry::Record) { 2131 Error("malformed block record in AST file"); 2132 return; 2133 } 2134 2135 Record.clear(); 2136 Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record); 2137 if (!MaybePP) { 2138 Error(MaybePP.takeError()); 2139 return; 2140 } 2141 switch ((PreprocessorRecordTypes)MaybePP.get()) { 2142 case PP_MACRO_DIRECTIVE_HISTORY: 2143 break; 2144 2145 case PP_MODULE_MACRO: { 2146 ModuleMacros.push_back(ModuleMacroRecord()); 2147 auto &Info = ModuleMacros.back(); 2148 Info.SubModID = getGlobalSubmoduleID(M, Record[0]); 2149 Info.MI = getMacro(getGlobalMacroID(M, Record[1])); 2150 for (int I = 2, N = Record.size(); I != N; ++I) 2151 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I])); 2152 continue; 2153 } 2154 2155 default: 2156 Error("malformed block record in AST file"); 2157 return; 2158 } 2159 2160 // We found the macro directive history; that's the last record 2161 // for this macro. 2162 break; 2163 } 2164 2165 // Module macros are listed in reverse dependency order. 2166 { 2167 std::reverse(ModuleMacros.begin(), ModuleMacros.end()); 2168 llvm::SmallVector<ModuleMacro*, 8> Overrides; 2169 for (auto &MMR : ModuleMacros) { 2170 Overrides.clear(); 2171 for (unsigned ModID : MMR.Overrides) { 2172 Module *Mod = getSubmodule(ModID); 2173 auto *Macro = PP.getModuleMacro(Mod, II); 2174 assert(Macro && "missing definition for overridden macro"); 2175 Overrides.push_back(Macro); 2176 } 2177 2178 bool Inserted = false; 2179 Module *Owner = getSubmodule(MMR.SubModID); 2180 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted); 2181 } 2182 } 2183 2184 // Don't read the directive history for a module; we don't have anywhere 2185 // to put it. 2186 if (M.isModule()) 2187 return; 2188 2189 // Deserialize the macro directives history in reverse source-order. 2190 MacroDirective *Latest = nullptr, *Earliest = nullptr; 2191 unsigned Idx = 0, N = Record.size(); 2192 while (Idx < N) { 2193 MacroDirective *MD = nullptr; 2194 SourceLocation Loc = ReadSourceLocation(M, Record, Idx); 2195 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++]; 2196 switch (K) { 2197 case MacroDirective::MD_Define: { 2198 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++])); 2199 MD = PP.AllocateDefMacroDirective(MI, Loc); 2200 break; 2201 } 2202 case MacroDirective::MD_Undefine: 2203 MD = PP.AllocateUndefMacroDirective(Loc); 2204 break; 2205 case MacroDirective::MD_Visibility: 2206 bool isPublic = Record[Idx++]; 2207 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic); 2208 break; 2209 } 2210 2211 if (!Latest) 2212 Latest = MD; 2213 if (Earliest) 2214 Earliest->setPrevious(MD); 2215 Earliest = MD; 2216 } 2217 2218 if (Latest) 2219 PP.setLoadedMacroDirective(II, Earliest, Latest); 2220 } 2221 2222 bool ASTReader::shouldDisableValidationForFile( 2223 const serialization::ModuleFile &M) const { 2224 if (DisableValidationKind == DisableValidationForModuleKind::None) 2225 return false; 2226 2227 // If a PCH is loaded and validation is disabled for PCH then disable 2228 // validation for the PCH and the modules it loads. 2229 ModuleKind K = CurrentDeserializingModuleKind.getValueOr(M.Kind); 2230 2231 switch (K) { 2232 case MK_MainFile: 2233 case MK_Preamble: 2234 case MK_PCH: 2235 return bool(DisableValidationKind & DisableValidationForModuleKind::PCH); 2236 case MK_ImplicitModule: 2237 case MK_ExplicitModule: 2238 case MK_PrebuiltModule: 2239 return bool(DisableValidationKind & DisableValidationForModuleKind::Module); 2240 } 2241 2242 return false; 2243 } 2244 2245 ASTReader::InputFileInfo 2246 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) { 2247 // Go find this input file. 2248 BitstreamCursor &Cursor = F.InputFilesCursor; 2249 SavedStreamPosition SavedPosition(Cursor); 2250 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2251 // FIXME this drops errors on the floor. 2252 consumeError(std::move(Err)); 2253 } 2254 2255 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 2256 if (!MaybeCode) { 2257 // FIXME this drops errors on the floor. 2258 consumeError(MaybeCode.takeError()); 2259 } 2260 unsigned Code = MaybeCode.get(); 2261 RecordData Record; 2262 StringRef Blob; 2263 2264 if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob)) 2265 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE && 2266 "invalid record type for input file"); 2267 else { 2268 // FIXME this drops errors on the floor. 2269 consumeError(Maybe.takeError()); 2270 } 2271 2272 assert(Record[0] == ID && "Bogus stored ID or offset"); 2273 InputFileInfo R; 2274 R.StoredSize = static_cast<off_t>(Record[1]); 2275 R.StoredTime = static_cast<time_t>(Record[2]); 2276 R.Overridden = static_cast<bool>(Record[3]); 2277 R.Transient = static_cast<bool>(Record[4]); 2278 R.TopLevelModuleMap = static_cast<bool>(Record[5]); 2279 R.Filename = std::string(Blob); 2280 ResolveImportedPath(F, R.Filename); 2281 2282 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 2283 if (!MaybeEntry) // FIXME this drops errors on the floor. 2284 consumeError(MaybeEntry.takeError()); 2285 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2286 assert(Entry.Kind == llvm::BitstreamEntry::Record && 2287 "expected record type for input file hash"); 2288 2289 Record.clear(); 2290 if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record)) 2291 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH && 2292 "invalid record type for input file hash"); 2293 else { 2294 // FIXME this drops errors on the floor. 2295 consumeError(Maybe.takeError()); 2296 } 2297 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) | 2298 static_cast<uint64_t>(Record[0]); 2299 return R; 2300 } 2301 2302 static unsigned moduleKindForDiagnostic(ModuleKind Kind); 2303 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) { 2304 // If this ID is bogus, just return an empty input file. 2305 if (ID == 0 || ID > F.InputFilesLoaded.size()) 2306 return InputFile(); 2307 2308 // If we've already loaded this input file, return it. 2309 if (F.InputFilesLoaded[ID-1].getFile()) 2310 return F.InputFilesLoaded[ID-1]; 2311 2312 if (F.InputFilesLoaded[ID-1].isNotFound()) 2313 return InputFile(); 2314 2315 // Go find this input file. 2316 BitstreamCursor &Cursor = F.InputFilesCursor; 2317 SavedStreamPosition SavedPosition(Cursor); 2318 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2319 // FIXME this drops errors on the floor. 2320 consumeError(std::move(Err)); 2321 } 2322 2323 InputFileInfo FI = readInputFileInfo(F, ID); 2324 off_t StoredSize = FI.StoredSize; 2325 time_t StoredTime = FI.StoredTime; 2326 bool Overridden = FI.Overridden; 2327 bool Transient = FI.Transient; 2328 StringRef Filename = FI.Filename; 2329 uint64_t StoredContentHash = FI.ContentHash; 2330 2331 OptionalFileEntryRefDegradesToFileEntryPtr File = 2332 expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false)); 2333 2334 // If we didn't find the file, resolve it relative to the 2335 // original directory from which this AST file was created. 2336 if (!File && !F.OriginalDir.empty() && !F.BaseDirectory.empty() && 2337 F.OriginalDir != F.BaseDirectory) { 2338 std::string Resolved = resolveFileRelativeToOriginalDir( 2339 std::string(Filename), F.OriginalDir, F.BaseDirectory); 2340 if (!Resolved.empty()) 2341 File = expectedToOptional(FileMgr.getFileRef(Resolved)); 2342 } 2343 2344 // For an overridden file, create a virtual file with the stored 2345 // size/timestamp. 2346 if ((Overridden || Transient) && !File) 2347 File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime); 2348 2349 if (!File) { 2350 if (Complain) { 2351 std::string ErrorStr = "could not find file '"; 2352 ErrorStr += Filename; 2353 ErrorStr += "' referenced by AST file '"; 2354 ErrorStr += F.FileName; 2355 ErrorStr += "'"; 2356 Error(ErrorStr); 2357 } 2358 // Record that we didn't find the file. 2359 F.InputFilesLoaded[ID-1] = InputFile::getNotFound(); 2360 return InputFile(); 2361 } 2362 2363 // Check if there was a request to override the contents of the file 2364 // that was part of the precompiled header. Overriding such a file 2365 // can lead to problems when lexing using the source locations from the 2366 // PCH. 2367 SourceManager &SM = getSourceManager(); 2368 // FIXME: Reject if the overrides are different. 2369 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) { 2370 if (Complain) 2371 Error(diag::err_fe_pch_file_overridden, Filename); 2372 2373 // After emitting the diagnostic, bypass the overriding file to recover 2374 // (this creates a separate FileEntry). 2375 File = SM.bypassFileContentsOverride(*File); 2376 if (!File) { 2377 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound(); 2378 return InputFile(); 2379 } 2380 } 2381 2382 struct Change { 2383 enum ModificationKind { 2384 Size, 2385 ModTime, 2386 Content, 2387 None, 2388 } Kind; 2389 llvm::Optional<int64_t> Old = llvm::None; 2390 llvm::Optional<int64_t> New = llvm::None; 2391 }; 2392 auto HasInputFileChanged = [&]() { 2393 if (StoredSize != File->getSize()) 2394 return Change{Change::Size, StoredSize, File->getSize()}; 2395 if (!shouldDisableValidationForFile(F) && StoredTime && 2396 StoredTime != File->getModificationTime()) { 2397 Change MTimeChange = {Change::ModTime, StoredTime, 2398 File->getModificationTime()}; 2399 2400 // In case the modification time changes but not the content, 2401 // accept the cached file as legit. 2402 if (ValidateASTInputFilesContent && 2403 StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) { 2404 auto MemBuffOrError = FileMgr.getBufferForFile(File); 2405 if (!MemBuffOrError) { 2406 if (!Complain) 2407 return MTimeChange; 2408 std::string ErrorStr = "could not get buffer for file '"; 2409 ErrorStr += File->getName(); 2410 ErrorStr += "'"; 2411 Error(ErrorStr); 2412 return MTimeChange; 2413 } 2414 2415 // FIXME: hash_value is not guaranteed to be stable! 2416 auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer()); 2417 if (StoredContentHash == static_cast<uint64_t>(ContentHash)) 2418 return Change{Change::None}; 2419 2420 return Change{Change::Content}; 2421 } 2422 return MTimeChange; 2423 } 2424 return Change{Change::None}; 2425 }; 2426 2427 bool IsOutOfDate = false; 2428 auto FileChange = HasInputFileChanged(); 2429 // For an overridden file, there is nothing to validate. 2430 if (!Overridden && FileChange.Kind != Change::None) { 2431 if (Complain && !Diags.isDiagnosticInFlight()) { 2432 // Build a list of the PCH imports that got us here (in reverse). 2433 SmallVector<ModuleFile *, 4> ImportStack(1, &F); 2434 while (!ImportStack.back()->ImportedBy.empty()) 2435 ImportStack.push_back(ImportStack.back()->ImportedBy[0]); 2436 2437 // The top-level PCH is stale. 2438 StringRef TopLevelPCHName(ImportStack.back()->FileName); 2439 Diag(diag::err_fe_ast_file_modified) 2440 << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind) 2441 << TopLevelPCHName << FileChange.Kind 2442 << (FileChange.Old && FileChange.New) 2443 << llvm::itostr(FileChange.Old.getValueOr(0)) 2444 << llvm::itostr(FileChange.New.getValueOr(0)); 2445 2446 // Print the import stack. 2447 if (ImportStack.size() > 1) { 2448 Diag(diag::note_pch_required_by) 2449 << Filename << ImportStack[0]->FileName; 2450 for (unsigned I = 1; I < ImportStack.size(); ++I) 2451 Diag(diag::note_pch_required_by) 2452 << ImportStack[I-1]->FileName << ImportStack[I]->FileName; 2453 } 2454 2455 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName; 2456 } 2457 2458 IsOutOfDate = true; 2459 } 2460 // FIXME: If the file is overridden and we've already opened it, 2461 // issue an error (or split it into a separate FileEntry). 2462 2463 InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate); 2464 2465 // Note that we've loaded this input file. 2466 F.InputFilesLoaded[ID-1] = IF; 2467 return IF; 2468 } 2469 2470 /// If we are loading a relocatable PCH or module file, and the filename 2471 /// is not an absolute path, add the system or module root to the beginning of 2472 /// the file name. 2473 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) { 2474 // Resolve relative to the base directory, if we have one. 2475 if (!M.BaseDirectory.empty()) 2476 return ResolveImportedPath(Filename, M.BaseDirectory); 2477 } 2478 2479 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) { 2480 if (Filename.empty() || llvm::sys::path::is_absolute(Filename)) 2481 return; 2482 2483 SmallString<128> Buffer; 2484 llvm::sys::path::append(Buffer, Prefix, Filename); 2485 Filename.assign(Buffer.begin(), Buffer.end()); 2486 } 2487 2488 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) { 2489 switch (ARR) { 2490 case ASTReader::Failure: return true; 2491 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing); 2492 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate); 2493 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch); 2494 case ASTReader::ConfigurationMismatch: 2495 return !(Caps & ASTReader::ARR_ConfigurationMismatch); 2496 case ASTReader::HadErrors: return true; 2497 case ASTReader::Success: return false; 2498 } 2499 2500 llvm_unreachable("unknown ASTReadResult"); 2501 } 2502 2503 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock( 2504 BitstreamCursor &Stream, unsigned ClientLoadCapabilities, 2505 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener, 2506 std::string &SuggestedPredefines) { 2507 if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) { 2508 // FIXME this drops errors on the floor. 2509 consumeError(std::move(Err)); 2510 return Failure; 2511 } 2512 2513 // Read all of the records in the options block. 2514 RecordData Record; 2515 ASTReadResult Result = Success; 2516 while (true) { 2517 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2518 if (!MaybeEntry) { 2519 // FIXME this drops errors on the floor. 2520 consumeError(MaybeEntry.takeError()); 2521 return Failure; 2522 } 2523 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2524 2525 switch (Entry.Kind) { 2526 case llvm::BitstreamEntry::Error: 2527 case llvm::BitstreamEntry::SubBlock: 2528 return Failure; 2529 2530 case llvm::BitstreamEntry::EndBlock: 2531 return Result; 2532 2533 case llvm::BitstreamEntry::Record: 2534 // The interesting case. 2535 break; 2536 } 2537 2538 // Read and process a record. 2539 Record.clear(); 2540 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record); 2541 if (!MaybeRecordType) { 2542 // FIXME this drops errors on the floor. 2543 consumeError(MaybeRecordType.takeError()); 2544 return Failure; 2545 } 2546 switch ((OptionsRecordTypes)MaybeRecordType.get()) { 2547 case LANGUAGE_OPTIONS: { 2548 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2549 if (ParseLanguageOptions(Record, Complain, Listener, 2550 AllowCompatibleConfigurationMismatch)) 2551 Result = ConfigurationMismatch; 2552 break; 2553 } 2554 2555 case TARGET_OPTIONS: { 2556 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2557 if (ParseTargetOptions(Record, Complain, Listener, 2558 AllowCompatibleConfigurationMismatch)) 2559 Result = ConfigurationMismatch; 2560 break; 2561 } 2562 2563 case FILE_SYSTEM_OPTIONS: { 2564 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2565 if (!AllowCompatibleConfigurationMismatch && 2566 ParseFileSystemOptions(Record, Complain, Listener)) 2567 Result = ConfigurationMismatch; 2568 break; 2569 } 2570 2571 case HEADER_SEARCH_OPTIONS: { 2572 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2573 if (!AllowCompatibleConfigurationMismatch && 2574 ParseHeaderSearchOptions(Record, Complain, Listener)) 2575 Result = ConfigurationMismatch; 2576 break; 2577 } 2578 2579 case PREPROCESSOR_OPTIONS: 2580 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2581 if (!AllowCompatibleConfigurationMismatch && 2582 ParsePreprocessorOptions(Record, Complain, Listener, 2583 SuggestedPredefines)) 2584 Result = ConfigurationMismatch; 2585 break; 2586 } 2587 } 2588 } 2589 2590 ASTReader::ASTReadResult 2591 ASTReader::ReadControlBlock(ModuleFile &F, 2592 SmallVectorImpl<ImportedModule> &Loaded, 2593 const ModuleFile *ImportedBy, 2594 unsigned ClientLoadCapabilities) { 2595 BitstreamCursor &Stream = F.Stream; 2596 2597 if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) { 2598 Error(std::move(Err)); 2599 return Failure; 2600 } 2601 2602 // Lambda to read the unhashed control block the first time it's called. 2603 // 2604 // For PCM files, the unhashed control block cannot be read until after the 2605 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still 2606 // need to look ahead before reading the IMPORTS record. For consistency, 2607 // this block is always read somehow (see BitstreamEntry::EndBlock). 2608 bool HasReadUnhashedControlBlock = false; 2609 auto readUnhashedControlBlockOnce = [&]() { 2610 if (!HasReadUnhashedControlBlock) { 2611 HasReadUnhashedControlBlock = true; 2612 if (ASTReadResult Result = 2613 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities)) 2614 return Result; 2615 } 2616 return Success; 2617 }; 2618 2619 bool DisableValidation = shouldDisableValidationForFile(F); 2620 2621 // Read all of the records and blocks in the control block. 2622 RecordData Record; 2623 unsigned NumInputs = 0; 2624 unsigned NumUserInputs = 0; 2625 StringRef BaseDirectoryAsWritten; 2626 while (true) { 2627 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2628 if (!MaybeEntry) { 2629 Error(MaybeEntry.takeError()); 2630 return Failure; 2631 } 2632 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2633 2634 switch (Entry.Kind) { 2635 case llvm::BitstreamEntry::Error: 2636 Error("malformed block record in AST file"); 2637 return Failure; 2638 case llvm::BitstreamEntry::EndBlock: { 2639 // Validate the module before returning. This call catches an AST with 2640 // no module name and no imports. 2641 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2642 return Result; 2643 2644 // Validate input files. 2645 const HeaderSearchOptions &HSOpts = 2646 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 2647 2648 // All user input files reside at the index range [0, NumUserInputs), and 2649 // system input files reside at [NumUserInputs, NumInputs). For explicitly 2650 // loaded module files, ignore missing inputs. 2651 if (!DisableValidation && F.Kind != MK_ExplicitModule && 2652 F.Kind != MK_PrebuiltModule) { 2653 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 2654 2655 // If we are reading a module, we will create a verification timestamp, 2656 // so we verify all input files. Otherwise, verify only user input 2657 // files. 2658 2659 unsigned N = NumUserInputs; 2660 if (ValidateSystemInputs || 2661 (HSOpts.ModulesValidateOncePerBuildSession && 2662 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp && 2663 F.Kind == MK_ImplicitModule)) 2664 N = NumInputs; 2665 2666 for (unsigned I = 0; I < N; ++I) { 2667 InputFile IF = getInputFile(F, I+1, Complain); 2668 if (!IF.getFile() || IF.isOutOfDate()) 2669 return OutOfDate; 2670 } 2671 } 2672 2673 if (Listener) 2674 Listener->visitModuleFile(F.FileName, F.Kind); 2675 2676 if (Listener && Listener->needsInputFileVisitation()) { 2677 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs 2678 : NumUserInputs; 2679 for (unsigned I = 0; I < N; ++I) { 2680 bool IsSystem = I >= NumUserInputs; 2681 InputFileInfo FI = readInputFileInfo(F, I+1); 2682 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden, 2683 F.Kind == MK_ExplicitModule || 2684 F.Kind == MK_PrebuiltModule); 2685 } 2686 } 2687 2688 return Success; 2689 } 2690 2691 case llvm::BitstreamEntry::SubBlock: 2692 switch (Entry.ID) { 2693 case INPUT_FILES_BLOCK_ID: 2694 F.InputFilesCursor = Stream; 2695 if (llvm::Error Err = Stream.SkipBlock()) { 2696 Error(std::move(Err)); 2697 return Failure; 2698 } 2699 if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) { 2700 Error("malformed block record in AST file"); 2701 return Failure; 2702 } 2703 continue; 2704 2705 case OPTIONS_BLOCK_ID: 2706 // If we're reading the first module for this group, check its options 2707 // are compatible with ours. For modules it imports, no further checking 2708 // is required, because we checked them when we built it. 2709 if (Listener && !ImportedBy) { 2710 // Should we allow the configuration of the module file to differ from 2711 // the configuration of the current translation unit in a compatible 2712 // way? 2713 // 2714 // FIXME: Allow this for files explicitly specified with -include-pch. 2715 bool AllowCompatibleConfigurationMismatch = 2716 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 2717 2718 ASTReadResult Result = 2719 ReadOptionsBlock(Stream, ClientLoadCapabilities, 2720 AllowCompatibleConfigurationMismatch, *Listener, 2721 SuggestedPredefines); 2722 if (Result == Failure) { 2723 Error("malformed block record in AST file"); 2724 return Result; 2725 } 2726 2727 if (DisableValidation || 2728 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 2729 Result = Success; 2730 2731 // If we can't load the module, exit early since we likely 2732 // will rebuild the module anyway. The stream may be in the 2733 // middle of a block. 2734 if (Result != Success) 2735 return Result; 2736 } else if (llvm::Error Err = Stream.SkipBlock()) { 2737 Error(std::move(Err)); 2738 return Failure; 2739 } 2740 continue; 2741 2742 default: 2743 if (llvm::Error Err = Stream.SkipBlock()) { 2744 Error(std::move(Err)); 2745 return Failure; 2746 } 2747 continue; 2748 } 2749 2750 case llvm::BitstreamEntry::Record: 2751 // The interesting case. 2752 break; 2753 } 2754 2755 // Read and process a record. 2756 Record.clear(); 2757 StringRef Blob; 2758 Expected<unsigned> MaybeRecordType = 2759 Stream.readRecord(Entry.ID, Record, &Blob); 2760 if (!MaybeRecordType) { 2761 Error(MaybeRecordType.takeError()); 2762 return Failure; 2763 } 2764 switch ((ControlRecordTypes)MaybeRecordType.get()) { 2765 case METADATA: { 2766 if (Record[0] != VERSION_MAJOR && !DisableValidation) { 2767 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2768 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old 2769 : diag::err_pch_version_too_new); 2770 return VersionMismatch; 2771 } 2772 2773 bool hasErrors = Record[6]; 2774 if (hasErrors && !DisableValidation) { 2775 // If requested by the caller and the module hasn't already been read 2776 // or compiled, mark modules on error as out-of-date. 2777 if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) && 2778 canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 2779 return OutOfDate; 2780 2781 if (!AllowASTWithCompilerErrors) { 2782 Diag(diag::err_pch_with_compiler_errors); 2783 return HadErrors; 2784 } 2785 } 2786 if (hasErrors) { 2787 Diags.ErrorOccurred = true; 2788 Diags.UncompilableErrorOccurred = true; 2789 Diags.UnrecoverableErrorOccurred = true; 2790 } 2791 2792 F.RelocatablePCH = Record[4]; 2793 // Relative paths in a relocatable PCH are relative to our sysroot. 2794 if (F.RelocatablePCH) 2795 F.BaseDirectory = isysroot.empty() ? "/" : isysroot; 2796 2797 F.HasTimestamps = Record[5]; 2798 2799 const std::string &CurBranch = getClangFullRepositoryVersion(); 2800 StringRef ASTBranch = Blob; 2801 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) { 2802 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2803 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch; 2804 return VersionMismatch; 2805 } 2806 break; 2807 } 2808 2809 case IMPORTS: { 2810 // Validate the AST before processing any imports (otherwise, untangling 2811 // them can be error-prone and expensive). A module will have a name and 2812 // will already have been validated, but this catches the PCH case. 2813 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2814 return Result; 2815 2816 // Load each of the imported PCH files. 2817 unsigned Idx = 0, N = Record.size(); 2818 while (Idx < N) { 2819 // Read information about the AST file. 2820 ModuleKind ImportedKind = (ModuleKind)Record[Idx++]; 2821 // The import location will be the local one for now; we will adjust 2822 // all import locations of module imports after the global source 2823 // location info are setup, in ReadAST. 2824 SourceLocation ImportLoc = 2825 ReadUntranslatedSourceLocation(Record[Idx++]); 2826 off_t StoredSize = (off_t)Record[Idx++]; 2827 time_t StoredModTime = (time_t)Record[Idx++]; 2828 auto FirstSignatureByte = Record.begin() + Idx; 2829 ASTFileSignature StoredSignature = ASTFileSignature::create( 2830 FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size); 2831 Idx += ASTFileSignature::size; 2832 2833 std::string ImportedName = ReadString(Record, Idx); 2834 std::string ImportedFile; 2835 2836 // For prebuilt and explicit modules first consult the file map for 2837 // an override. Note that here we don't search prebuilt module 2838 // directories, only the explicit name to file mappings. Also, we will 2839 // still verify the size/signature making sure it is essentially the 2840 // same file but perhaps in a different location. 2841 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule) 2842 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName( 2843 ImportedName, /*FileMapOnly*/ true); 2844 2845 if (ImportedFile.empty()) 2846 // Use BaseDirectoryAsWritten to ensure we use the same path in the 2847 // ModuleCache as when writing. 2848 ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx); 2849 else 2850 SkipPath(Record, Idx); 2851 2852 // If our client can't cope with us being out of date, we can't cope with 2853 // our dependency being missing. 2854 unsigned Capabilities = ClientLoadCapabilities; 2855 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 2856 Capabilities &= ~ARR_Missing; 2857 2858 // Load the AST file. 2859 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, 2860 Loaded, StoredSize, StoredModTime, 2861 StoredSignature, Capabilities); 2862 2863 // If we diagnosed a problem, produce a backtrace. 2864 bool recompilingFinalized = 2865 Result == OutOfDate && (Capabilities & ARR_OutOfDate) && 2866 getModuleManager().getModuleCache().isPCMFinal(F.FileName); 2867 if (isDiagnosedResult(Result, Capabilities) || recompilingFinalized) 2868 Diag(diag::note_module_file_imported_by) 2869 << F.FileName << !F.ModuleName.empty() << F.ModuleName; 2870 if (recompilingFinalized) 2871 Diag(diag::note_module_file_conflict); 2872 2873 switch (Result) { 2874 case Failure: return Failure; 2875 // If we have to ignore the dependency, we'll have to ignore this too. 2876 case Missing: 2877 case OutOfDate: return OutOfDate; 2878 case VersionMismatch: return VersionMismatch; 2879 case ConfigurationMismatch: return ConfigurationMismatch; 2880 case HadErrors: return HadErrors; 2881 case Success: break; 2882 } 2883 } 2884 break; 2885 } 2886 2887 case ORIGINAL_FILE: 2888 F.OriginalSourceFileID = FileID::get(Record[0]); 2889 F.ActualOriginalSourceFileName = std::string(Blob); 2890 F.OriginalSourceFileName = F.ActualOriginalSourceFileName; 2891 ResolveImportedPath(F, F.OriginalSourceFileName); 2892 break; 2893 2894 case ORIGINAL_FILE_ID: 2895 F.OriginalSourceFileID = FileID::get(Record[0]); 2896 break; 2897 2898 case ORIGINAL_PCH_DIR: 2899 F.OriginalDir = std::string(Blob); 2900 break; 2901 2902 case MODULE_NAME: 2903 F.ModuleName = std::string(Blob); 2904 Diag(diag::remark_module_import) 2905 << F.ModuleName << F.FileName << (ImportedBy ? true : false) 2906 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 2907 if (Listener) 2908 Listener->ReadModuleName(F.ModuleName); 2909 2910 // Validate the AST as soon as we have a name so we can exit early on 2911 // failure. 2912 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2913 return Result; 2914 2915 break; 2916 2917 case MODULE_DIRECTORY: { 2918 // Save the BaseDirectory as written in the PCM for computing the module 2919 // filename for the ModuleCache. 2920 BaseDirectoryAsWritten = Blob; 2921 assert(!F.ModuleName.empty() && 2922 "MODULE_DIRECTORY found before MODULE_NAME"); 2923 // If we've already loaded a module map file covering this module, we may 2924 // have a better path for it (relative to the current build). 2925 Module *M = PP.getHeaderSearchInfo().lookupModule( 2926 F.ModuleName, /*AllowSearch*/ true, 2927 /*AllowExtraModuleMapSearch*/ true); 2928 if (M && M->Directory) { 2929 // If we're implicitly loading a module, the base directory can't 2930 // change between the build and use. 2931 // Don't emit module relocation error if we have -fno-validate-pch 2932 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation & 2933 DisableValidationForModuleKind::Module) && 2934 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) { 2935 auto BuildDir = PP.getFileManager().getDirectory(Blob); 2936 if (!BuildDir || *BuildDir != M->Directory) { 2937 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 2938 Diag(diag::err_imported_module_relocated) 2939 << F.ModuleName << Blob << M->Directory->getName(); 2940 return OutOfDate; 2941 } 2942 } 2943 F.BaseDirectory = std::string(M->Directory->getName()); 2944 } else { 2945 F.BaseDirectory = std::string(Blob); 2946 } 2947 break; 2948 } 2949 2950 case MODULE_MAP_FILE: 2951 if (ASTReadResult Result = 2952 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities)) 2953 return Result; 2954 break; 2955 2956 case INPUT_FILE_OFFSETS: 2957 NumInputs = Record[0]; 2958 NumUserInputs = Record[1]; 2959 F.InputFileOffsets = 2960 (const llvm::support::unaligned_uint64_t *)Blob.data(); 2961 F.InputFilesLoaded.resize(NumInputs); 2962 F.NumUserInputFiles = NumUserInputs; 2963 break; 2964 } 2965 } 2966 } 2967 2968 llvm::Error ASTReader::ReadASTBlock(ModuleFile &F, 2969 unsigned ClientLoadCapabilities) { 2970 BitstreamCursor &Stream = F.Stream; 2971 2972 if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) 2973 return Err; 2974 F.ASTBlockStartOffset = Stream.GetCurrentBitNo(); 2975 2976 // Read all of the records and blocks for the AST file. 2977 RecordData Record; 2978 while (true) { 2979 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2980 if (!MaybeEntry) 2981 return MaybeEntry.takeError(); 2982 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2983 2984 switch (Entry.Kind) { 2985 case llvm::BitstreamEntry::Error: 2986 return llvm::createStringError( 2987 std::errc::illegal_byte_sequence, 2988 "error at end of module block in AST file"); 2989 case llvm::BitstreamEntry::EndBlock: 2990 // Outside of C++, we do not store a lookup map for the translation unit. 2991 // Instead, mark it as needing a lookup map to be built if this module 2992 // contains any declarations lexically within it (which it always does!). 2993 // This usually has no cost, since we very rarely need the lookup map for 2994 // the translation unit outside C++. 2995 if (ASTContext *Ctx = ContextObj) { 2996 DeclContext *DC = Ctx->getTranslationUnitDecl(); 2997 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus) 2998 DC->setMustBuildLookupTable(); 2999 } 3000 3001 return llvm::Error::success(); 3002 case llvm::BitstreamEntry::SubBlock: 3003 switch (Entry.ID) { 3004 case DECLTYPES_BLOCK_ID: 3005 // We lazily load the decls block, but we want to set up the 3006 // DeclsCursor cursor to point into it. Clone our current bitcode 3007 // cursor to it, enter the block and read the abbrevs in that block. 3008 // With the main cursor, we just skip over it. 3009 F.DeclsCursor = Stream; 3010 if (llvm::Error Err = Stream.SkipBlock()) 3011 return Err; 3012 if (llvm::Error Err = ReadBlockAbbrevs( 3013 F.DeclsCursor, DECLTYPES_BLOCK_ID, &F.DeclsBlockStartOffset)) 3014 return Err; 3015 break; 3016 3017 case PREPROCESSOR_BLOCK_ID: 3018 F.MacroCursor = Stream; 3019 if (!PP.getExternalSource()) 3020 PP.setExternalSource(this); 3021 3022 if (llvm::Error Err = Stream.SkipBlock()) 3023 return Err; 3024 if (llvm::Error Err = 3025 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) 3026 return Err; 3027 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo(); 3028 break; 3029 3030 case PREPROCESSOR_DETAIL_BLOCK_ID: 3031 F.PreprocessorDetailCursor = Stream; 3032 3033 if (llvm::Error Err = Stream.SkipBlock()) { 3034 return Err; 3035 } 3036 if (llvm::Error Err = ReadBlockAbbrevs(F.PreprocessorDetailCursor, 3037 PREPROCESSOR_DETAIL_BLOCK_ID)) 3038 return Err; 3039 F.PreprocessorDetailStartOffset 3040 = F.PreprocessorDetailCursor.GetCurrentBitNo(); 3041 3042 if (!PP.getPreprocessingRecord()) 3043 PP.createPreprocessingRecord(); 3044 if (!PP.getPreprocessingRecord()->getExternalSource()) 3045 PP.getPreprocessingRecord()->SetExternalSource(*this); 3046 break; 3047 3048 case SOURCE_MANAGER_BLOCK_ID: 3049 if (llvm::Error Err = ReadSourceManagerBlock(F)) 3050 return Err; 3051 break; 3052 3053 case SUBMODULE_BLOCK_ID: 3054 if (llvm::Error Err = ReadSubmoduleBlock(F, ClientLoadCapabilities)) 3055 return Err; 3056 break; 3057 3058 case COMMENTS_BLOCK_ID: { 3059 BitstreamCursor C = Stream; 3060 3061 if (llvm::Error Err = Stream.SkipBlock()) 3062 return Err; 3063 if (llvm::Error Err = ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) 3064 return Err; 3065 CommentsCursors.push_back(std::make_pair(C, &F)); 3066 break; 3067 } 3068 3069 default: 3070 if (llvm::Error Err = Stream.SkipBlock()) 3071 return Err; 3072 break; 3073 } 3074 continue; 3075 3076 case llvm::BitstreamEntry::Record: 3077 // The interesting case. 3078 break; 3079 } 3080 3081 // Read and process a record. 3082 Record.clear(); 3083 StringRef Blob; 3084 Expected<unsigned> MaybeRecordType = 3085 Stream.readRecord(Entry.ID, Record, &Blob); 3086 if (!MaybeRecordType) 3087 return MaybeRecordType.takeError(); 3088 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get(); 3089 3090 // If we're not loading an AST context, we don't care about most records. 3091 if (!ContextObj) { 3092 switch (RecordType) { 3093 case IDENTIFIER_TABLE: 3094 case IDENTIFIER_OFFSET: 3095 case INTERESTING_IDENTIFIERS: 3096 case STATISTICS: 3097 case PP_CONDITIONAL_STACK: 3098 case PP_COUNTER_VALUE: 3099 case SOURCE_LOCATION_OFFSETS: 3100 case MODULE_OFFSET_MAP: 3101 case SOURCE_MANAGER_LINE_TABLE: 3102 case SOURCE_LOCATION_PRELOADS: 3103 case PPD_ENTITIES_OFFSETS: 3104 case HEADER_SEARCH_TABLE: 3105 case IMPORTED_MODULES: 3106 case MACRO_OFFSET: 3107 break; 3108 default: 3109 continue; 3110 } 3111 } 3112 3113 switch (RecordType) { 3114 default: // Default behavior: ignore. 3115 break; 3116 3117 case TYPE_OFFSET: { 3118 if (F.LocalNumTypes != 0) 3119 return llvm::createStringError( 3120 std::errc::illegal_byte_sequence, 3121 "duplicate TYPE_OFFSET record in AST file"); 3122 F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data()); 3123 F.LocalNumTypes = Record[0]; 3124 unsigned LocalBaseTypeIndex = Record[1]; 3125 F.BaseTypeIndex = getTotalNumTypes(); 3126 3127 if (F.LocalNumTypes > 0) { 3128 // Introduce the global -> local mapping for types within this module. 3129 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F)); 3130 3131 // Introduce the local -> global mapping for types within this module. 3132 F.TypeRemap.insertOrReplace( 3133 std::make_pair(LocalBaseTypeIndex, 3134 F.BaseTypeIndex - LocalBaseTypeIndex)); 3135 3136 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes); 3137 } 3138 break; 3139 } 3140 3141 case DECL_OFFSET: { 3142 if (F.LocalNumDecls != 0) 3143 return llvm::createStringError( 3144 std::errc::illegal_byte_sequence, 3145 "duplicate DECL_OFFSET record in AST file"); 3146 F.DeclOffsets = (const DeclOffset *)Blob.data(); 3147 F.LocalNumDecls = Record[0]; 3148 unsigned LocalBaseDeclID = Record[1]; 3149 F.BaseDeclID = getTotalNumDecls(); 3150 3151 if (F.LocalNumDecls > 0) { 3152 // Introduce the global -> local mapping for declarations within this 3153 // module. 3154 GlobalDeclMap.insert( 3155 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F)); 3156 3157 // Introduce the local -> global mapping for declarations within this 3158 // module. 3159 F.DeclRemap.insertOrReplace( 3160 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID)); 3161 3162 // Introduce the global -> local mapping for declarations within this 3163 // module. 3164 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID; 3165 3166 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls); 3167 } 3168 break; 3169 } 3170 3171 case TU_UPDATE_LEXICAL: { 3172 DeclContext *TU = ContextObj->getTranslationUnitDecl(); 3173 LexicalContents Contents( 3174 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 3175 Blob.data()), 3176 static_cast<unsigned int>(Blob.size() / 4)); 3177 TULexicalDecls.push_back(std::make_pair(&F, Contents)); 3178 TU->setHasExternalLexicalStorage(true); 3179 break; 3180 } 3181 3182 case UPDATE_VISIBLE: { 3183 unsigned Idx = 0; 3184 serialization::DeclID ID = ReadDeclID(F, Record, Idx); 3185 auto *Data = (const unsigned char*)Blob.data(); 3186 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data}); 3187 // If we've already loaded the decl, perform the updates when we finish 3188 // loading this block. 3189 if (Decl *D = GetExistingDecl(ID)) 3190 PendingUpdateRecords.push_back( 3191 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3192 break; 3193 } 3194 3195 case IDENTIFIER_TABLE: 3196 F.IdentifierTableData = 3197 reinterpret_cast<const unsigned char *>(Blob.data()); 3198 if (Record[0]) { 3199 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create( 3200 F.IdentifierTableData + Record[0], 3201 F.IdentifierTableData + sizeof(uint32_t), 3202 F.IdentifierTableData, 3203 ASTIdentifierLookupTrait(*this, F)); 3204 3205 PP.getIdentifierTable().setExternalIdentifierLookup(this); 3206 } 3207 break; 3208 3209 case IDENTIFIER_OFFSET: { 3210 if (F.LocalNumIdentifiers != 0) 3211 return llvm::createStringError( 3212 std::errc::illegal_byte_sequence, 3213 "duplicate IDENTIFIER_OFFSET record in AST file"); 3214 F.IdentifierOffsets = (const uint32_t *)Blob.data(); 3215 F.LocalNumIdentifiers = Record[0]; 3216 unsigned LocalBaseIdentifierID = Record[1]; 3217 F.BaseIdentifierID = getTotalNumIdentifiers(); 3218 3219 if (F.LocalNumIdentifiers > 0) { 3220 // Introduce the global -> local mapping for identifiers within this 3221 // module. 3222 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1, 3223 &F)); 3224 3225 // Introduce the local -> global mapping for identifiers within this 3226 // module. 3227 F.IdentifierRemap.insertOrReplace( 3228 std::make_pair(LocalBaseIdentifierID, 3229 F.BaseIdentifierID - LocalBaseIdentifierID)); 3230 3231 IdentifiersLoaded.resize(IdentifiersLoaded.size() 3232 + F.LocalNumIdentifiers); 3233 } 3234 break; 3235 } 3236 3237 case INTERESTING_IDENTIFIERS: 3238 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end()); 3239 break; 3240 3241 case EAGERLY_DESERIALIZED_DECLS: 3242 // FIXME: Skip reading this record if our ASTConsumer doesn't care 3243 // about "interesting" decls (for instance, if we're building a module). 3244 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3245 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3246 break; 3247 3248 case MODULAR_CODEGEN_DECLS: 3249 // FIXME: Skip reading this record if our ASTConsumer doesn't care about 3250 // them (ie: if we're not codegenerating this module). 3251 if (F.Kind == MK_MainFile || 3252 getContext().getLangOpts().BuildingPCHWithObjectFile) 3253 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3254 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3255 break; 3256 3257 case SPECIAL_TYPES: 3258 if (SpecialTypes.empty()) { 3259 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3260 SpecialTypes.push_back(getGlobalTypeID(F, Record[I])); 3261 break; 3262 } 3263 3264 if (SpecialTypes.size() != Record.size()) 3265 return llvm::createStringError(std::errc::illegal_byte_sequence, 3266 "invalid special-types record"); 3267 3268 for (unsigned I = 0, N = Record.size(); I != N; ++I) { 3269 serialization::TypeID ID = getGlobalTypeID(F, Record[I]); 3270 if (!SpecialTypes[I]) 3271 SpecialTypes[I] = ID; 3272 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate 3273 // merge step? 3274 } 3275 break; 3276 3277 case STATISTICS: 3278 TotalNumStatements += Record[0]; 3279 TotalNumMacros += Record[1]; 3280 TotalLexicalDeclContexts += Record[2]; 3281 TotalVisibleDeclContexts += Record[3]; 3282 break; 3283 3284 case UNUSED_FILESCOPED_DECLS: 3285 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3286 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I])); 3287 break; 3288 3289 case DELEGATING_CTORS: 3290 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3291 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I])); 3292 break; 3293 3294 case WEAK_UNDECLARED_IDENTIFIERS: 3295 if (Record.size() % 4 != 0) 3296 return llvm::createStringError(std::errc::illegal_byte_sequence, 3297 "invalid weak identifiers record"); 3298 3299 // FIXME: Ignore weak undeclared identifiers from non-original PCH 3300 // files. This isn't the way to do it :) 3301 WeakUndeclaredIdentifiers.clear(); 3302 3303 // Translate the weak, undeclared identifiers into global IDs. 3304 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) { 3305 WeakUndeclaredIdentifiers.push_back( 3306 getGlobalIdentifierID(F, Record[I++])); 3307 WeakUndeclaredIdentifiers.push_back( 3308 getGlobalIdentifierID(F, Record[I++])); 3309 WeakUndeclaredIdentifiers.push_back( 3310 ReadSourceLocation(F, Record, I).getRawEncoding()); 3311 WeakUndeclaredIdentifiers.push_back(Record[I++]); 3312 } 3313 break; 3314 3315 case SELECTOR_OFFSETS: { 3316 F.SelectorOffsets = (const uint32_t *)Blob.data(); 3317 F.LocalNumSelectors = Record[0]; 3318 unsigned LocalBaseSelectorID = Record[1]; 3319 F.BaseSelectorID = getTotalNumSelectors(); 3320 3321 if (F.LocalNumSelectors > 0) { 3322 // Introduce the global -> local mapping for selectors within this 3323 // module. 3324 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F)); 3325 3326 // Introduce the local -> global mapping for selectors within this 3327 // module. 3328 F.SelectorRemap.insertOrReplace( 3329 std::make_pair(LocalBaseSelectorID, 3330 F.BaseSelectorID - LocalBaseSelectorID)); 3331 3332 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors); 3333 } 3334 break; 3335 } 3336 3337 case METHOD_POOL: 3338 F.SelectorLookupTableData = (const unsigned char *)Blob.data(); 3339 if (Record[0]) 3340 F.SelectorLookupTable 3341 = ASTSelectorLookupTable::Create( 3342 F.SelectorLookupTableData + Record[0], 3343 F.SelectorLookupTableData, 3344 ASTSelectorLookupTrait(*this, F)); 3345 TotalNumMethodPoolEntries += Record[1]; 3346 break; 3347 3348 case REFERENCED_SELECTOR_POOL: 3349 if (!Record.empty()) { 3350 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) { 3351 ReferencedSelectorsData.push_back(getGlobalSelectorID(F, 3352 Record[Idx++])); 3353 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx). 3354 getRawEncoding()); 3355 } 3356 } 3357 break; 3358 3359 case PP_CONDITIONAL_STACK: 3360 if (!Record.empty()) { 3361 unsigned Idx = 0, End = Record.size() - 1; 3362 bool ReachedEOFWhileSkipping = Record[Idx++]; 3363 llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo; 3364 if (ReachedEOFWhileSkipping) { 3365 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx); 3366 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx); 3367 bool FoundNonSkipPortion = Record[Idx++]; 3368 bool FoundElse = Record[Idx++]; 3369 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx); 3370 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion, 3371 FoundElse, ElseLoc); 3372 } 3373 SmallVector<PPConditionalInfo, 4> ConditionalStack; 3374 while (Idx < End) { 3375 auto Loc = ReadSourceLocation(F, Record, Idx); 3376 bool WasSkipping = Record[Idx++]; 3377 bool FoundNonSkip = Record[Idx++]; 3378 bool FoundElse = Record[Idx++]; 3379 ConditionalStack.push_back( 3380 {Loc, WasSkipping, FoundNonSkip, FoundElse}); 3381 } 3382 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo); 3383 } 3384 break; 3385 3386 case PP_COUNTER_VALUE: 3387 if (!Record.empty() && Listener) 3388 Listener->ReadCounter(F, Record[0]); 3389 break; 3390 3391 case FILE_SORTED_DECLS: 3392 F.FileSortedDecls = (const DeclID *)Blob.data(); 3393 F.NumFileSortedDecls = Record[0]; 3394 break; 3395 3396 case SOURCE_LOCATION_OFFSETS: { 3397 F.SLocEntryOffsets = (const uint32_t *)Blob.data(); 3398 F.LocalNumSLocEntries = Record[0]; 3399 SourceLocation::UIntTy SLocSpaceSize = Record[1]; 3400 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset; 3401 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) = 3402 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, 3403 SLocSpaceSize); 3404 if (!F.SLocEntryBaseID) 3405 return llvm::createStringError(std::errc::invalid_argument, 3406 "ran out of source locations"); 3407 // Make our entry in the range map. BaseID is negative and growing, so 3408 // we invert it. Because we invert it, though, we need the other end of 3409 // the range. 3410 unsigned RangeStart = 3411 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1; 3412 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F)); 3413 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset); 3414 3415 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing. 3416 assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0); 3417 GlobalSLocOffsetMap.insert( 3418 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset 3419 - SLocSpaceSize,&F)); 3420 3421 // Initialize the remapping table. 3422 // Invalid stays invalid. 3423 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0)); 3424 // This module. Base was 2 when being compiled. 3425 F.SLocRemap.insertOrReplace(std::make_pair( 3426 2U, static_cast<SourceLocation::IntTy>(F.SLocEntryBaseOffset - 2))); 3427 3428 TotalNumSLocEntries += F.LocalNumSLocEntries; 3429 break; 3430 } 3431 3432 case MODULE_OFFSET_MAP: 3433 F.ModuleOffsetMap = Blob; 3434 break; 3435 3436 case SOURCE_MANAGER_LINE_TABLE: 3437 ParseLineTable(F, Record); 3438 break; 3439 3440 case SOURCE_LOCATION_PRELOADS: { 3441 // Need to transform from the local view (1-based IDs) to the global view, 3442 // which is based off F.SLocEntryBaseID. 3443 if (!F.PreloadSLocEntries.empty()) 3444 return llvm::createStringError( 3445 std::errc::illegal_byte_sequence, 3446 "Multiple SOURCE_LOCATION_PRELOADS records in AST file"); 3447 3448 F.PreloadSLocEntries.swap(Record); 3449 break; 3450 } 3451 3452 case EXT_VECTOR_DECLS: 3453 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3454 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I])); 3455 break; 3456 3457 case VTABLE_USES: 3458 if (Record.size() % 3 != 0) 3459 return llvm::createStringError(std::errc::illegal_byte_sequence, 3460 "Invalid VTABLE_USES record"); 3461 3462 // Later tables overwrite earlier ones. 3463 // FIXME: Modules will have some trouble with this. This is clearly not 3464 // the right way to do this. 3465 VTableUses.clear(); 3466 3467 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) { 3468 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++])); 3469 VTableUses.push_back( 3470 ReadSourceLocation(F, Record, Idx).getRawEncoding()); 3471 VTableUses.push_back(Record[Idx++]); 3472 } 3473 break; 3474 3475 case PENDING_IMPLICIT_INSTANTIATIONS: 3476 if (PendingInstantiations.size() % 2 != 0) 3477 return llvm::createStringError( 3478 std::errc::illegal_byte_sequence, 3479 "Invalid existing PendingInstantiations"); 3480 3481 if (Record.size() % 2 != 0) 3482 return llvm::createStringError( 3483 std::errc::illegal_byte_sequence, 3484 "Invalid PENDING_IMPLICIT_INSTANTIATIONS block"); 3485 3486 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3487 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++])); 3488 PendingInstantiations.push_back( 3489 ReadSourceLocation(F, Record, I).getRawEncoding()); 3490 } 3491 break; 3492 3493 case SEMA_DECL_REFS: 3494 if (Record.size() != 3) 3495 return llvm::createStringError(std::errc::illegal_byte_sequence, 3496 "Invalid SEMA_DECL_REFS block"); 3497 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3498 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3499 break; 3500 3501 case PPD_ENTITIES_OFFSETS: { 3502 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data(); 3503 assert(Blob.size() % sizeof(PPEntityOffset) == 0); 3504 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset); 3505 3506 unsigned LocalBasePreprocessedEntityID = Record[0]; 3507 3508 unsigned StartingID; 3509 if (!PP.getPreprocessingRecord()) 3510 PP.createPreprocessingRecord(); 3511 if (!PP.getPreprocessingRecord()->getExternalSource()) 3512 PP.getPreprocessingRecord()->SetExternalSource(*this); 3513 StartingID 3514 = PP.getPreprocessingRecord() 3515 ->allocateLoadedEntities(F.NumPreprocessedEntities); 3516 F.BasePreprocessedEntityID = StartingID; 3517 3518 if (F.NumPreprocessedEntities > 0) { 3519 // Introduce the global -> local mapping for preprocessed entities in 3520 // this module. 3521 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F)); 3522 3523 // Introduce the local -> global mapping for preprocessed entities in 3524 // this module. 3525 F.PreprocessedEntityRemap.insertOrReplace( 3526 std::make_pair(LocalBasePreprocessedEntityID, 3527 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID)); 3528 } 3529 3530 break; 3531 } 3532 3533 case PPD_SKIPPED_RANGES: { 3534 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data(); 3535 assert(Blob.size() % sizeof(PPSkippedRange) == 0); 3536 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange); 3537 3538 if (!PP.getPreprocessingRecord()) 3539 PP.createPreprocessingRecord(); 3540 if (!PP.getPreprocessingRecord()->getExternalSource()) 3541 PP.getPreprocessingRecord()->SetExternalSource(*this); 3542 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord() 3543 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges); 3544 3545 if (F.NumPreprocessedSkippedRanges > 0) 3546 GlobalSkippedRangeMap.insert( 3547 std::make_pair(F.BasePreprocessedSkippedRangeID, &F)); 3548 break; 3549 } 3550 3551 case DECL_UPDATE_OFFSETS: 3552 if (Record.size() % 2 != 0) 3553 return llvm::createStringError( 3554 std::errc::illegal_byte_sequence, 3555 "invalid DECL_UPDATE_OFFSETS block in AST file"); 3556 for (unsigned I = 0, N = Record.size(); I != N; I += 2) { 3557 GlobalDeclID ID = getGlobalDeclID(F, Record[I]); 3558 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1])); 3559 3560 // If we've already loaded the decl, perform the updates when we finish 3561 // loading this block. 3562 if (Decl *D = GetExistingDecl(ID)) 3563 PendingUpdateRecords.push_back( 3564 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3565 } 3566 break; 3567 3568 case OBJC_CATEGORIES_MAP: 3569 if (F.LocalNumObjCCategoriesInMap != 0) 3570 return llvm::createStringError( 3571 std::errc::illegal_byte_sequence, 3572 "duplicate OBJC_CATEGORIES_MAP record in AST file"); 3573 3574 F.LocalNumObjCCategoriesInMap = Record[0]; 3575 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data(); 3576 break; 3577 3578 case OBJC_CATEGORIES: 3579 F.ObjCCategories.swap(Record); 3580 break; 3581 3582 case CUDA_SPECIAL_DECL_REFS: 3583 // Later tables overwrite earlier ones. 3584 // FIXME: Modules will have trouble with this. 3585 CUDASpecialDeclRefs.clear(); 3586 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3587 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3588 break; 3589 3590 case HEADER_SEARCH_TABLE: 3591 F.HeaderFileInfoTableData = Blob.data(); 3592 F.LocalNumHeaderFileInfos = Record[1]; 3593 if (Record[0]) { 3594 F.HeaderFileInfoTable 3595 = HeaderFileInfoLookupTable::Create( 3596 (const unsigned char *)F.HeaderFileInfoTableData + Record[0], 3597 (const unsigned char *)F.HeaderFileInfoTableData, 3598 HeaderFileInfoTrait(*this, F, 3599 &PP.getHeaderSearchInfo(), 3600 Blob.data() + Record[2])); 3601 3602 PP.getHeaderSearchInfo().SetExternalSource(this); 3603 if (!PP.getHeaderSearchInfo().getExternalLookup()) 3604 PP.getHeaderSearchInfo().SetExternalLookup(this); 3605 } 3606 break; 3607 3608 case FP_PRAGMA_OPTIONS: 3609 // Later tables overwrite earlier ones. 3610 FPPragmaOptions.swap(Record); 3611 break; 3612 3613 case OPENCL_EXTENSIONS: 3614 for (unsigned I = 0, E = Record.size(); I != E; ) { 3615 auto Name = ReadString(Record, I); 3616 auto &OptInfo = OpenCLExtensions.OptMap[Name]; 3617 OptInfo.Supported = Record[I++] != 0; 3618 OptInfo.Enabled = Record[I++] != 0; 3619 OptInfo.WithPragma = Record[I++] != 0; 3620 OptInfo.Avail = Record[I++]; 3621 OptInfo.Core = Record[I++]; 3622 OptInfo.Opt = Record[I++]; 3623 } 3624 break; 3625 3626 case TENTATIVE_DEFINITIONS: 3627 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3628 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I])); 3629 break; 3630 3631 case KNOWN_NAMESPACES: 3632 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3633 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I])); 3634 break; 3635 3636 case UNDEFINED_BUT_USED: 3637 if (UndefinedButUsed.size() % 2 != 0) 3638 return llvm::createStringError(std::errc::illegal_byte_sequence, 3639 "Invalid existing UndefinedButUsed"); 3640 3641 if (Record.size() % 2 != 0) 3642 return llvm::createStringError(std::errc::illegal_byte_sequence, 3643 "invalid undefined-but-used record"); 3644 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3645 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++])); 3646 UndefinedButUsed.push_back( 3647 ReadSourceLocation(F, Record, I).getRawEncoding()); 3648 } 3649 break; 3650 3651 case DELETE_EXPRS_TO_ANALYZE: 3652 for (unsigned I = 0, N = Record.size(); I != N;) { 3653 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++])); 3654 const uint64_t Count = Record[I++]; 3655 DelayedDeleteExprs.push_back(Count); 3656 for (uint64_t C = 0; C < Count; ++C) { 3657 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding()); 3658 bool IsArrayForm = Record[I++] == 1; 3659 DelayedDeleteExprs.push_back(IsArrayForm); 3660 } 3661 } 3662 break; 3663 3664 case IMPORTED_MODULES: 3665 if (!F.isModule()) { 3666 // If we aren't loading a module (which has its own exports), make 3667 // all of the imported modules visible. 3668 // FIXME: Deal with macros-only imports. 3669 for (unsigned I = 0, N = Record.size(); I != N; /**/) { 3670 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]); 3671 SourceLocation Loc = ReadSourceLocation(F, Record, I); 3672 if (GlobalID) { 3673 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc)); 3674 if (DeserializationListener) 3675 DeserializationListener->ModuleImportRead(GlobalID, Loc); 3676 } 3677 } 3678 } 3679 break; 3680 3681 case MACRO_OFFSET: { 3682 if (F.LocalNumMacros != 0) 3683 return llvm::createStringError( 3684 std::errc::illegal_byte_sequence, 3685 "duplicate MACRO_OFFSET record in AST file"); 3686 F.MacroOffsets = (const uint32_t *)Blob.data(); 3687 F.LocalNumMacros = Record[0]; 3688 unsigned LocalBaseMacroID = Record[1]; 3689 F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset; 3690 F.BaseMacroID = getTotalNumMacros(); 3691 3692 if (F.LocalNumMacros > 0) { 3693 // Introduce the global -> local mapping for macros within this module. 3694 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F)); 3695 3696 // Introduce the local -> global mapping for macros within this module. 3697 F.MacroRemap.insertOrReplace( 3698 std::make_pair(LocalBaseMacroID, 3699 F.BaseMacroID - LocalBaseMacroID)); 3700 3701 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros); 3702 } 3703 break; 3704 } 3705 3706 case LATE_PARSED_TEMPLATE: 3707 LateParsedTemplates.emplace_back( 3708 std::piecewise_construct, std::forward_as_tuple(&F), 3709 std::forward_as_tuple(Record.begin(), Record.end())); 3710 break; 3711 3712 case OPTIMIZE_PRAGMA_OPTIONS: 3713 if (Record.size() != 1) 3714 return llvm::createStringError(std::errc::illegal_byte_sequence, 3715 "invalid pragma optimize record"); 3716 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]); 3717 break; 3718 3719 case MSSTRUCT_PRAGMA_OPTIONS: 3720 if (Record.size() != 1) 3721 return llvm::createStringError(std::errc::illegal_byte_sequence, 3722 "invalid pragma ms_struct record"); 3723 PragmaMSStructState = Record[0]; 3724 break; 3725 3726 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS: 3727 if (Record.size() != 2) 3728 return llvm::createStringError( 3729 std::errc::illegal_byte_sequence, 3730 "invalid pragma pointers to members record"); 3731 PragmaMSPointersToMembersState = Record[0]; 3732 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]); 3733 break; 3734 3735 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES: 3736 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3737 UnusedLocalTypedefNameCandidates.push_back( 3738 getGlobalDeclID(F, Record[I])); 3739 break; 3740 3741 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH: 3742 if (Record.size() != 1) 3743 return llvm::createStringError(std::errc::illegal_byte_sequence, 3744 "invalid cuda pragma options record"); 3745 ForceCUDAHostDeviceDepth = Record[0]; 3746 break; 3747 3748 case ALIGN_PACK_PRAGMA_OPTIONS: { 3749 if (Record.size() < 3) 3750 return llvm::createStringError(std::errc::illegal_byte_sequence, 3751 "invalid pragma pack record"); 3752 PragmaAlignPackCurrentValue = ReadAlignPackInfo(Record[0]); 3753 PragmaAlignPackCurrentLocation = ReadSourceLocation(F, Record[1]); 3754 unsigned NumStackEntries = Record[2]; 3755 unsigned Idx = 3; 3756 // Reset the stack when importing a new module. 3757 PragmaAlignPackStack.clear(); 3758 for (unsigned I = 0; I < NumStackEntries; ++I) { 3759 PragmaAlignPackStackEntry Entry; 3760 Entry.Value = ReadAlignPackInfo(Record[Idx++]); 3761 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3762 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3763 PragmaAlignPackStrings.push_back(ReadString(Record, Idx)); 3764 Entry.SlotLabel = PragmaAlignPackStrings.back(); 3765 PragmaAlignPackStack.push_back(Entry); 3766 } 3767 break; 3768 } 3769 3770 case FLOAT_CONTROL_PRAGMA_OPTIONS: { 3771 if (Record.size() < 3) 3772 return llvm::createStringError(std::errc::illegal_byte_sequence, 3773 "invalid pragma float control record"); 3774 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]); 3775 FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]); 3776 unsigned NumStackEntries = Record[2]; 3777 unsigned Idx = 3; 3778 // Reset the stack when importing a new module. 3779 FpPragmaStack.clear(); 3780 for (unsigned I = 0; I < NumStackEntries; ++I) { 3781 FpPragmaStackEntry Entry; 3782 Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]); 3783 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3784 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3785 FpPragmaStrings.push_back(ReadString(Record, Idx)); 3786 Entry.SlotLabel = FpPragmaStrings.back(); 3787 FpPragmaStack.push_back(Entry); 3788 } 3789 break; 3790 } 3791 3792 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS: 3793 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3794 DeclsToCheckForDeferredDiags.insert(getGlobalDeclID(F, Record[I])); 3795 break; 3796 } 3797 } 3798 } 3799 3800 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const { 3801 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read"); 3802 3803 // Additional remapping information. 3804 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data(); 3805 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size(); 3806 F.ModuleOffsetMap = StringRef(); 3807 3808 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders. 3809 if (F.SLocRemap.find(0) == F.SLocRemap.end()) { 3810 F.SLocRemap.insert(std::make_pair(0U, 0)); 3811 F.SLocRemap.insert(std::make_pair(2U, 1)); 3812 } 3813 3814 // Continuous range maps we may be updating in our module. 3815 using SLocRemapBuilder = 3816 ContinuousRangeMap<SourceLocation::UIntTy, SourceLocation::IntTy, 3817 2>::Builder; 3818 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder; 3819 SLocRemapBuilder SLocRemap(F.SLocRemap); 3820 RemapBuilder IdentifierRemap(F.IdentifierRemap); 3821 RemapBuilder MacroRemap(F.MacroRemap); 3822 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap); 3823 RemapBuilder SubmoduleRemap(F.SubmoduleRemap); 3824 RemapBuilder SelectorRemap(F.SelectorRemap); 3825 RemapBuilder DeclRemap(F.DeclRemap); 3826 RemapBuilder TypeRemap(F.TypeRemap); 3827 3828 while (Data < DataEnd) { 3829 // FIXME: Looking up dependency modules by filename is horrible. Let's 3830 // start fixing this with prebuilt, explicit and implicit modules and see 3831 // how it goes... 3832 using namespace llvm::support; 3833 ModuleKind Kind = static_cast<ModuleKind>( 3834 endian::readNext<uint8_t, little, unaligned>(Data)); 3835 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data); 3836 StringRef Name = StringRef((const char*)Data, Len); 3837 Data += Len; 3838 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule || 3839 Kind == MK_ImplicitModule 3840 ? ModuleMgr.lookupByModuleName(Name) 3841 : ModuleMgr.lookupByFileName(Name)); 3842 if (!OM) { 3843 std::string Msg = 3844 "SourceLocation remap refers to unknown module, cannot find "; 3845 Msg.append(std::string(Name)); 3846 Error(Msg); 3847 return; 3848 } 3849 3850 SourceLocation::UIntTy SLocOffset = 3851 endian::readNext<uint32_t, little, unaligned>(Data); 3852 uint32_t IdentifierIDOffset = 3853 endian::readNext<uint32_t, little, unaligned>(Data); 3854 uint32_t MacroIDOffset = 3855 endian::readNext<uint32_t, little, unaligned>(Data); 3856 uint32_t PreprocessedEntityIDOffset = 3857 endian::readNext<uint32_t, little, unaligned>(Data); 3858 uint32_t SubmoduleIDOffset = 3859 endian::readNext<uint32_t, little, unaligned>(Data); 3860 uint32_t SelectorIDOffset = 3861 endian::readNext<uint32_t, little, unaligned>(Data); 3862 uint32_t DeclIDOffset = 3863 endian::readNext<uint32_t, little, unaligned>(Data); 3864 uint32_t TypeIndexOffset = 3865 endian::readNext<uint32_t, little, unaligned>(Data); 3866 3867 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset, 3868 RemapBuilder &Remap) { 3869 constexpr uint32_t None = std::numeric_limits<uint32_t>::max(); 3870 if (Offset != None) 3871 Remap.insert(std::make_pair(Offset, 3872 static_cast<int>(BaseOffset - Offset))); 3873 }; 3874 3875 constexpr SourceLocation::UIntTy SLocNone = 3876 std::numeric_limits<SourceLocation::UIntTy>::max(); 3877 if (SLocOffset != SLocNone) 3878 SLocRemap.insert(std::make_pair( 3879 SLocOffset, static_cast<SourceLocation::IntTy>( 3880 OM->SLocEntryBaseOffset - SLocOffset))); 3881 3882 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap); 3883 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap); 3884 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID, 3885 PreprocessedEntityRemap); 3886 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap); 3887 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap); 3888 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap); 3889 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap); 3890 3891 // Global -> local mappings. 3892 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset; 3893 } 3894 } 3895 3896 ASTReader::ASTReadResult 3897 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F, 3898 const ModuleFile *ImportedBy, 3899 unsigned ClientLoadCapabilities) { 3900 unsigned Idx = 0; 3901 F.ModuleMapPath = ReadPath(F, Record, Idx); 3902 3903 // Try to resolve ModuleName in the current header search context and 3904 // verify that it is found in the same module map file as we saved. If the 3905 // top-level AST file is a main file, skip this check because there is no 3906 // usable header search context. 3907 assert(!F.ModuleName.empty() && 3908 "MODULE_NAME should come before MODULE_MAP_FILE"); 3909 if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) { 3910 // An implicitly-loaded module file should have its module listed in some 3911 // module map file that we've already loaded. 3912 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName); 3913 auto &Map = PP.getHeaderSearchInfo().getModuleMap(); 3914 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr; 3915 // Don't emit module relocation error if we have -fno-validate-pch 3916 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation & 3917 DisableValidationForModuleKind::Module) && 3918 !ModMap) { 3919 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) { 3920 if (auto ASTFE = M ? M->getASTFile() : None) { 3921 // This module was defined by an imported (explicit) module. 3922 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName 3923 << ASTFE->getName(); 3924 } else { 3925 // This module was built with a different module map. 3926 Diag(diag::err_imported_module_not_found) 3927 << F.ModuleName << F.FileName 3928 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath 3929 << !ImportedBy; 3930 // In case it was imported by a PCH, there's a chance the user is 3931 // just missing to include the search path to the directory containing 3932 // the modulemap. 3933 if (ImportedBy && ImportedBy->Kind == MK_PCH) 3934 Diag(diag::note_imported_by_pch_module_not_found) 3935 << llvm::sys::path::parent_path(F.ModuleMapPath); 3936 } 3937 } 3938 return OutOfDate; 3939 } 3940 3941 assert(M && M->Name == F.ModuleName && "found module with different name"); 3942 3943 // Check the primary module map file. 3944 auto StoredModMap = FileMgr.getFile(F.ModuleMapPath); 3945 if (!StoredModMap || *StoredModMap != ModMap) { 3946 assert(ModMap && "found module is missing module map file"); 3947 assert((ImportedBy || F.Kind == MK_ImplicitModule) && 3948 "top-level import should be verified"); 3949 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy; 3950 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 3951 Diag(diag::err_imported_module_modmap_changed) 3952 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName) 3953 << ModMap->getName() << F.ModuleMapPath << NotImported; 3954 return OutOfDate; 3955 } 3956 3957 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps; 3958 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) { 3959 // FIXME: we should use input files rather than storing names. 3960 std::string Filename = ReadPath(F, Record, Idx); 3961 auto SF = FileMgr.getFile(Filename, false, false); 3962 if (!SF) { 3963 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 3964 Error("could not find file '" + Filename +"' referenced by AST file"); 3965 return OutOfDate; 3966 } 3967 AdditionalStoredMaps.insert(*SF); 3968 } 3969 3970 // Check any additional module map files (e.g. module.private.modulemap) 3971 // that are not in the pcm. 3972 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) { 3973 for (const FileEntry *ModMap : *AdditionalModuleMaps) { 3974 // Remove files that match 3975 // Note: SmallPtrSet::erase is really remove 3976 if (!AdditionalStoredMaps.erase(ModMap)) { 3977 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 3978 Diag(diag::err_module_different_modmap) 3979 << F.ModuleName << /*new*/0 << ModMap->getName(); 3980 return OutOfDate; 3981 } 3982 } 3983 } 3984 3985 // Check any additional module map files that are in the pcm, but not 3986 // found in header search. Cases that match are already removed. 3987 for (const FileEntry *ModMap : AdditionalStoredMaps) { 3988 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 3989 Diag(diag::err_module_different_modmap) 3990 << F.ModuleName << /*not new*/1 << ModMap->getName(); 3991 return OutOfDate; 3992 } 3993 } 3994 3995 if (Listener) 3996 Listener->ReadModuleMapFile(F.ModuleMapPath); 3997 return Success; 3998 } 3999 4000 /// Move the given method to the back of the global list of methods. 4001 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) { 4002 // Find the entry for this selector in the method pool. 4003 Sema::GlobalMethodPool::iterator Known 4004 = S.MethodPool.find(Method->getSelector()); 4005 if (Known == S.MethodPool.end()) 4006 return; 4007 4008 // Retrieve the appropriate method list. 4009 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first 4010 : Known->second.second; 4011 bool Found = false; 4012 for (ObjCMethodList *List = &Start; List; List = List->getNext()) { 4013 if (!Found) { 4014 if (List->getMethod() == Method) { 4015 Found = true; 4016 } else { 4017 // Keep searching. 4018 continue; 4019 } 4020 } 4021 4022 if (List->getNext()) 4023 List->setMethod(List->getNext()->getMethod()); 4024 else 4025 List->setMethod(Method); 4026 } 4027 } 4028 4029 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) { 4030 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?"); 4031 for (Decl *D : Names) { 4032 bool wasHidden = !D->isUnconditionallyVisible(); 4033 D->setVisibleDespiteOwningModule(); 4034 4035 if (wasHidden && SemaObj) { 4036 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) { 4037 moveMethodToBackOfGlobalList(*SemaObj, Method); 4038 } 4039 } 4040 } 4041 } 4042 4043 void ASTReader::makeModuleVisible(Module *Mod, 4044 Module::NameVisibilityKind NameVisibility, 4045 SourceLocation ImportLoc) { 4046 llvm::SmallPtrSet<Module *, 4> Visited; 4047 SmallVector<Module *, 4> Stack; 4048 Stack.push_back(Mod); 4049 while (!Stack.empty()) { 4050 Mod = Stack.pop_back_val(); 4051 4052 if (NameVisibility <= Mod->NameVisibility) { 4053 // This module already has this level of visibility (or greater), so 4054 // there is nothing more to do. 4055 continue; 4056 } 4057 4058 if (Mod->isUnimportable()) { 4059 // Modules that aren't importable cannot be made visible. 4060 continue; 4061 } 4062 4063 // Update the module's name visibility. 4064 Mod->NameVisibility = NameVisibility; 4065 4066 // If we've already deserialized any names from this module, 4067 // mark them as visible. 4068 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod); 4069 if (Hidden != HiddenNamesMap.end()) { 4070 auto HiddenNames = std::move(*Hidden); 4071 HiddenNamesMap.erase(Hidden); 4072 makeNamesVisible(HiddenNames.second, HiddenNames.first); 4073 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() && 4074 "making names visible added hidden names"); 4075 } 4076 4077 // Push any exported modules onto the stack to be marked as visible. 4078 SmallVector<Module *, 16> Exports; 4079 Mod->getExportedModules(Exports); 4080 for (SmallVectorImpl<Module *>::iterator 4081 I = Exports.begin(), E = Exports.end(); I != E; ++I) { 4082 Module *Exported = *I; 4083 if (Visited.insert(Exported).second) 4084 Stack.push_back(Exported); 4085 } 4086 } 4087 } 4088 4089 /// We've merged the definition \p MergedDef into the existing definition 4090 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made 4091 /// visible. 4092 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def, 4093 NamedDecl *MergedDef) { 4094 if (!Def->isUnconditionallyVisible()) { 4095 // If MergedDef is visible or becomes visible, make the definition visible. 4096 if (MergedDef->isUnconditionallyVisible()) 4097 Def->setVisibleDespiteOwningModule(); 4098 else { 4099 getContext().mergeDefinitionIntoModule( 4100 Def, MergedDef->getImportedOwningModule(), 4101 /*NotifyListeners*/ false); 4102 PendingMergedDefinitionsToDeduplicate.insert(Def); 4103 } 4104 } 4105 } 4106 4107 bool ASTReader::loadGlobalIndex() { 4108 if (GlobalIndex) 4109 return false; 4110 4111 if (TriedLoadingGlobalIndex || !UseGlobalIndex || 4112 !PP.getLangOpts().Modules) 4113 return true; 4114 4115 // Try to load the global index. 4116 TriedLoadingGlobalIndex = true; 4117 StringRef ModuleCachePath 4118 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath(); 4119 std::pair<GlobalModuleIndex *, llvm::Error> Result = 4120 GlobalModuleIndex::readIndex(ModuleCachePath); 4121 if (llvm::Error Err = std::move(Result.second)) { 4122 assert(!Result.first); 4123 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 4124 return true; 4125 } 4126 4127 GlobalIndex.reset(Result.first); 4128 ModuleMgr.setGlobalIndex(GlobalIndex.get()); 4129 return false; 4130 } 4131 4132 bool ASTReader::isGlobalIndexUnavailable() const { 4133 return PP.getLangOpts().Modules && UseGlobalIndex && 4134 !hasGlobalIndex() && TriedLoadingGlobalIndex; 4135 } 4136 4137 static void updateModuleTimestamp(ModuleFile &MF) { 4138 // Overwrite the timestamp file contents so that file's mtime changes. 4139 std::string TimestampFilename = MF.getTimestampFilename(); 4140 std::error_code EC; 4141 llvm::raw_fd_ostream OS(TimestampFilename, EC, 4142 llvm::sys::fs::OF_TextWithCRLF); 4143 if (EC) 4144 return; 4145 OS << "Timestamp file\n"; 4146 OS.close(); 4147 OS.clear_error(); // Avoid triggering a fatal error. 4148 } 4149 4150 /// Given a cursor at the start of an AST file, scan ahead and drop the 4151 /// cursor into the start of the given block ID, returning false on success and 4152 /// true on failure. 4153 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) { 4154 while (true) { 4155 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 4156 if (!MaybeEntry) { 4157 // FIXME this drops errors on the floor. 4158 consumeError(MaybeEntry.takeError()); 4159 return true; 4160 } 4161 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4162 4163 switch (Entry.Kind) { 4164 case llvm::BitstreamEntry::Error: 4165 case llvm::BitstreamEntry::EndBlock: 4166 return true; 4167 4168 case llvm::BitstreamEntry::Record: 4169 // Ignore top-level records. 4170 if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID)) 4171 break; 4172 else { 4173 // FIXME this drops errors on the floor. 4174 consumeError(Skipped.takeError()); 4175 return true; 4176 } 4177 4178 case llvm::BitstreamEntry::SubBlock: 4179 if (Entry.ID == BlockID) { 4180 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) { 4181 // FIXME this drops the error on the floor. 4182 consumeError(std::move(Err)); 4183 return true; 4184 } 4185 // Found it! 4186 return false; 4187 } 4188 4189 if (llvm::Error Err = Cursor.SkipBlock()) { 4190 // FIXME this drops the error on the floor. 4191 consumeError(std::move(Err)); 4192 return true; 4193 } 4194 } 4195 } 4196 } 4197 4198 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, 4199 ModuleKind Type, 4200 SourceLocation ImportLoc, 4201 unsigned ClientLoadCapabilities, 4202 SmallVectorImpl<ImportedSubmodule> *Imported) { 4203 llvm::SaveAndRestore<SourceLocation> 4204 SetCurImportLocRAII(CurrentImportLoc, ImportLoc); 4205 llvm::SaveAndRestore<Optional<ModuleKind>> SetCurModuleKindRAII( 4206 CurrentDeserializingModuleKind, Type); 4207 4208 // Defer any pending actions until we get to the end of reading the AST file. 4209 Deserializing AnASTFile(this); 4210 4211 // Bump the generation number. 4212 unsigned PreviousGeneration = 0; 4213 if (ContextObj) 4214 PreviousGeneration = incrementGeneration(*ContextObj); 4215 4216 unsigned NumModules = ModuleMgr.size(); 4217 SmallVector<ImportedModule, 4> Loaded; 4218 if (ASTReadResult ReadResult = 4219 ReadASTCore(FileName, Type, ImportLoc, 4220 /*ImportedBy=*/nullptr, Loaded, 0, 0, ASTFileSignature(), 4221 ClientLoadCapabilities)) { 4222 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, 4223 PP.getLangOpts().Modules 4224 ? &PP.getHeaderSearchInfo().getModuleMap() 4225 : nullptr); 4226 4227 // If we find that any modules are unusable, the global index is going 4228 // to be out-of-date. Just remove it. 4229 GlobalIndex.reset(); 4230 ModuleMgr.setGlobalIndex(nullptr); 4231 return ReadResult; 4232 } 4233 4234 // Here comes stuff that we only do once the entire chain is loaded. Do *not* 4235 // remove modules from this point. Various fields are updated during reading 4236 // the AST block and removing the modules would result in dangling pointers. 4237 // They are generally only incidentally dereferenced, ie. a binary search 4238 // runs over `GlobalSLocEntryMap`, which could cause an invalid module to 4239 // be dereferenced but it wouldn't actually be used. 4240 4241 // Load the AST blocks of all of the modules that we loaded. We can still 4242 // hit errors parsing the ASTs at this point. 4243 for (ImportedModule &M : Loaded) { 4244 ModuleFile &F = *M.Mod; 4245 4246 // Read the AST block. 4247 if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) { 4248 Error(std::move(Err)); 4249 return Failure; 4250 } 4251 4252 // The AST block should always have a definition for the main module. 4253 if (F.isModule() && !F.DidReadTopLevelSubmodule) { 4254 Error(diag::err_module_file_missing_top_level_submodule, F.FileName); 4255 return Failure; 4256 } 4257 4258 // Read the extension blocks. 4259 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) { 4260 if (llvm::Error Err = ReadExtensionBlock(F)) { 4261 Error(std::move(Err)); 4262 return Failure; 4263 } 4264 } 4265 4266 // Once read, set the ModuleFile bit base offset and update the size in 4267 // bits of all files we've seen. 4268 F.GlobalBitOffset = TotalModulesSizeInBits; 4269 TotalModulesSizeInBits += F.SizeInBits; 4270 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F)); 4271 } 4272 4273 // Preload source locations and interesting indentifiers. 4274 for (ImportedModule &M : Loaded) { 4275 ModuleFile &F = *M.Mod; 4276 4277 // Preload SLocEntries. 4278 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) { 4279 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID; 4280 // Load it through the SourceManager and don't call ReadSLocEntry() 4281 // directly because the entry may have already been loaded in which case 4282 // calling ReadSLocEntry() directly would trigger an assertion in 4283 // SourceManager. 4284 SourceMgr.getLoadedSLocEntryByID(Index); 4285 } 4286 4287 // Map the original source file ID into the ID space of the current 4288 // compilation. 4289 if (F.OriginalSourceFileID.isValid()) { 4290 F.OriginalSourceFileID = FileID::get( 4291 F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1); 4292 } 4293 4294 // Preload all the pending interesting identifiers by marking them out of 4295 // date. 4296 for (auto Offset : F.PreloadIdentifierOffsets) { 4297 const unsigned char *Data = F.IdentifierTableData + Offset; 4298 4299 ASTIdentifierLookupTrait Trait(*this, F); 4300 auto KeyDataLen = Trait.ReadKeyDataLength(Data); 4301 auto Key = Trait.ReadKey(Data, KeyDataLen.first); 4302 auto &II = PP.getIdentifierTable().getOwn(Key); 4303 II.setOutOfDate(true); 4304 4305 // Mark this identifier as being from an AST file so that we can track 4306 // whether we need to serialize it. 4307 markIdentifierFromAST(*this, II); 4308 4309 // Associate the ID with the identifier so that the writer can reuse it. 4310 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first); 4311 SetIdentifierInfo(ID, &II); 4312 } 4313 } 4314 4315 // Setup the import locations and notify the module manager that we've 4316 // committed to these module files. 4317 for (ImportedModule &M : Loaded) { 4318 ModuleFile &F = *M.Mod; 4319 4320 ModuleMgr.moduleFileAccepted(&F); 4321 4322 // Set the import location. 4323 F.DirectImportLoc = ImportLoc; 4324 // FIXME: We assume that locations from PCH / preamble do not need 4325 // any translation. 4326 if (!M.ImportedBy) 4327 F.ImportLoc = M.ImportLoc; 4328 else 4329 F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc); 4330 } 4331 4332 if (!PP.getLangOpts().CPlusPlus || 4333 (Type != MK_ImplicitModule && Type != MK_ExplicitModule && 4334 Type != MK_PrebuiltModule)) { 4335 // Mark all of the identifiers in the identifier table as being out of date, 4336 // so that various accessors know to check the loaded modules when the 4337 // identifier is used. 4338 // 4339 // For C++ modules, we don't need information on many identifiers (just 4340 // those that provide macros or are poisoned), so we mark all of 4341 // the interesting ones via PreloadIdentifierOffsets. 4342 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(), 4343 IdEnd = PP.getIdentifierTable().end(); 4344 Id != IdEnd; ++Id) 4345 Id->second->setOutOfDate(true); 4346 } 4347 // Mark selectors as out of date. 4348 for (auto Sel : SelectorGeneration) 4349 SelectorOutOfDate[Sel.first] = true; 4350 4351 // Resolve any unresolved module exports. 4352 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) { 4353 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I]; 4354 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID); 4355 Module *ResolvedMod = getSubmodule(GlobalID); 4356 4357 switch (Unresolved.Kind) { 4358 case UnresolvedModuleRef::Conflict: 4359 if (ResolvedMod) { 4360 Module::Conflict Conflict; 4361 Conflict.Other = ResolvedMod; 4362 Conflict.Message = Unresolved.String.str(); 4363 Unresolved.Mod->Conflicts.push_back(Conflict); 4364 } 4365 continue; 4366 4367 case UnresolvedModuleRef::Import: 4368 if (ResolvedMod) 4369 Unresolved.Mod->Imports.insert(ResolvedMod); 4370 continue; 4371 4372 case UnresolvedModuleRef::Export: 4373 if (ResolvedMod || Unresolved.IsWildcard) 4374 Unresolved.Mod->Exports.push_back( 4375 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard)); 4376 continue; 4377 } 4378 } 4379 UnresolvedModuleRefs.clear(); 4380 4381 if (Imported) 4382 Imported->append(ImportedModules.begin(), 4383 ImportedModules.end()); 4384 4385 // FIXME: How do we load the 'use'd modules? They may not be submodules. 4386 // Might be unnecessary as use declarations are only used to build the 4387 // module itself. 4388 4389 if (ContextObj) 4390 InitializeContext(); 4391 4392 if (SemaObj) 4393 UpdateSema(); 4394 4395 if (DeserializationListener) 4396 DeserializationListener->ReaderInitialized(this); 4397 4398 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule(); 4399 if (PrimaryModule.OriginalSourceFileID.isValid()) { 4400 // If this AST file is a precompiled preamble, then set the 4401 // preamble file ID of the source manager to the file source file 4402 // from which the preamble was built. 4403 if (Type == MK_Preamble) { 4404 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID); 4405 } else if (Type == MK_MainFile) { 4406 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID); 4407 } 4408 } 4409 4410 // For any Objective-C class definitions we have already loaded, make sure 4411 // that we load any additional categories. 4412 if (ContextObj) { 4413 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) { 4414 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(), 4415 ObjCClassesLoaded[I], 4416 PreviousGeneration); 4417 } 4418 } 4419 4420 if (PP.getHeaderSearchInfo() 4421 .getHeaderSearchOpts() 4422 .ModulesValidateOncePerBuildSession) { 4423 // Now we are certain that the module and all modules it depends on are 4424 // up to date. Create or update timestamp files for modules that are 4425 // located in the module cache (not for PCH files that could be anywhere 4426 // in the filesystem). 4427 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) { 4428 ImportedModule &M = Loaded[I]; 4429 if (M.Mod->Kind == MK_ImplicitModule) { 4430 updateModuleTimestamp(*M.Mod); 4431 } 4432 } 4433 } 4434 4435 return Success; 4436 } 4437 4438 static ASTFileSignature readASTFileSignature(StringRef PCH); 4439 4440 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'. 4441 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) { 4442 // FIXME checking magic headers is done in other places such as 4443 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't 4444 // always done the same. Unify it all with a helper. 4445 if (!Stream.canSkipToPos(4)) 4446 return llvm::createStringError(std::errc::illegal_byte_sequence, 4447 "file too small to contain AST file magic"); 4448 for (unsigned C : {'C', 'P', 'C', 'H'}) 4449 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) { 4450 if (Res.get() != C) 4451 return llvm::createStringError( 4452 std::errc::illegal_byte_sequence, 4453 "file doesn't start with AST file magic"); 4454 } else 4455 return Res.takeError(); 4456 return llvm::Error::success(); 4457 } 4458 4459 static unsigned moduleKindForDiagnostic(ModuleKind Kind) { 4460 switch (Kind) { 4461 case MK_PCH: 4462 return 0; // PCH 4463 case MK_ImplicitModule: 4464 case MK_ExplicitModule: 4465 case MK_PrebuiltModule: 4466 return 1; // module 4467 case MK_MainFile: 4468 case MK_Preamble: 4469 return 2; // main source file 4470 } 4471 llvm_unreachable("unknown module kind"); 4472 } 4473 4474 ASTReader::ASTReadResult 4475 ASTReader::ReadASTCore(StringRef FileName, 4476 ModuleKind Type, 4477 SourceLocation ImportLoc, 4478 ModuleFile *ImportedBy, 4479 SmallVectorImpl<ImportedModule> &Loaded, 4480 off_t ExpectedSize, time_t ExpectedModTime, 4481 ASTFileSignature ExpectedSignature, 4482 unsigned ClientLoadCapabilities) { 4483 ModuleFile *M; 4484 std::string ErrorStr; 4485 ModuleManager::AddModuleResult AddResult 4486 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy, 4487 getGeneration(), ExpectedSize, ExpectedModTime, 4488 ExpectedSignature, readASTFileSignature, 4489 M, ErrorStr); 4490 4491 switch (AddResult) { 4492 case ModuleManager::AlreadyLoaded: 4493 Diag(diag::remark_module_import) 4494 << M->ModuleName << M->FileName << (ImportedBy ? true : false) 4495 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 4496 return Success; 4497 4498 case ModuleManager::NewlyLoaded: 4499 // Load module file below. 4500 break; 4501 4502 case ModuleManager::Missing: 4503 // The module file was missing; if the client can handle that, return 4504 // it. 4505 if (ClientLoadCapabilities & ARR_Missing) 4506 return Missing; 4507 4508 // Otherwise, return an error. 4509 Diag(diag::err_ast_file_not_found) 4510 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty() 4511 << ErrorStr; 4512 return Failure; 4513 4514 case ModuleManager::OutOfDate: 4515 // We couldn't load the module file because it is out-of-date. If the 4516 // client can handle out-of-date, return it. 4517 if (ClientLoadCapabilities & ARR_OutOfDate) 4518 return OutOfDate; 4519 4520 // Otherwise, return an error. 4521 Diag(diag::err_ast_file_out_of_date) 4522 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty() 4523 << ErrorStr; 4524 return Failure; 4525 } 4526 4527 assert(M && "Missing module file"); 4528 4529 bool ShouldFinalizePCM = false; 4530 auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() { 4531 auto &MC = getModuleManager().getModuleCache(); 4532 if (ShouldFinalizePCM) 4533 MC.finalizePCM(FileName); 4534 else 4535 MC.tryToDropPCM(FileName); 4536 }); 4537 ModuleFile &F = *M; 4538 BitstreamCursor &Stream = F.Stream; 4539 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer)); 4540 F.SizeInBits = F.Buffer->getBufferSize() * 8; 4541 4542 // Sniff for the signature. 4543 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4544 Diag(diag::err_ast_file_invalid) 4545 << moduleKindForDiagnostic(Type) << FileName << std::move(Err); 4546 return Failure; 4547 } 4548 4549 // This is used for compatibility with older PCH formats. 4550 bool HaveReadControlBlock = false; 4551 while (true) { 4552 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4553 if (!MaybeEntry) { 4554 Error(MaybeEntry.takeError()); 4555 return Failure; 4556 } 4557 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4558 4559 switch (Entry.Kind) { 4560 case llvm::BitstreamEntry::Error: 4561 case llvm::BitstreamEntry::Record: 4562 case llvm::BitstreamEntry::EndBlock: 4563 Error("invalid record at top-level of AST file"); 4564 return Failure; 4565 4566 case llvm::BitstreamEntry::SubBlock: 4567 break; 4568 } 4569 4570 switch (Entry.ID) { 4571 case CONTROL_BLOCK_ID: 4572 HaveReadControlBlock = true; 4573 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) { 4574 case Success: 4575 // Check that we didn't try to load a non-module AST file as a module. 4576 // 4577 // FIXME: Should we also perform the converse check? Loading a module as 4578 // a PCH file sort of works, but it's a bit wonky. 4579 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule || 4580 Type == MK_PrebuiltModule) && 4581 F.ModuleName.empty()) { 4582 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure; 4583 if (Result != OutOfDate || 4584 (ClientLoadCapabilities & ARR_OutOfDate) == 0) 4585 Diag(diag::err_module_file_not_module) << FileName; 4586 return Result; 4587 } 4588 break; 4589 4590 case Failure: return Failure; 4591 case Missing: return Missing; 4592 case OutOfDate: return OutOfDate; 4593 case VersionMismatch: return VersionMismatch; 4594 case ConfigurationMismatch: return ConfigurationMismatch; 4595 case HadErrors: return HadErrors; 4596 } 4597 break; 4598 4599 case AST_BLOCK_ID: 4600 if (!HaveReadControlBlock) { 4601 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 4602 Diag(diag::err_pch_version_too_old); 4603 return VersionMismatch; 4604 } 4605 4606 // Record that we've loaded this module. 4607 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc)); 4608 ShouldFinalizePCM = true; 4609 return Success; 4610 4611 case UNHASHED_CONTROL_BLOCK_ID: 4612 // This block is handled using look-ahead during ReadControlBlock. We 4613 // shouldn't get here! 4614 Error("malformed block record in AST file"); 4615 return Failure; 4616 4617 default: 4618 if (llvm::Error Err = Stream.SkipBlock()) { 4619 Error(std::move(Err)); 4620 return Failure; 4621 } 4622 break; 4623 } 4624 } 4625 4626 llvm_unreachable("unexpected break; expected return"); 4627 } 4628 4629 ASTReader::ASTReadResult 4630 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy, 4631 unsigned ClientLoadCapabilities) { 4632 const HeaderSearchOptions &HSOpts = 4633 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 4634 bool AllowCompatibleConfigurationMismatch = 4635 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 4636 bool DisableValidation = shouldDisableValidationForFile(F); 4637 4638 ASTReadResult Result = readUnhashedControlBlockImpl( 4639 &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch, 4640 Listener.get(), 4641 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions); 4642 4643 // If F was directly imported by another module, it's implicitly validated by 4644 // the importing module. 4645 if (DisableValidation || WasImportedBy || 4646 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 4647 return Success; 4648 4649 if (Result == Failure) { 4650 Error("malformed block record in AST file"); 4651 return Failure; 4652 } 4653 4654 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) { 4655 // If this module has already been finalized in the ModuleCache, we're stuck 4656 // with it; we can only load a single version of each module. 4657 // 4658 // This can happen when a module is imported in two contexts: in one, as a 4659 // user module; in another, as a system module (due to an import from 4660 // another module marked with the [system] flag). It usually indicates a 4661 // bug in the module map: this module should also be marked with [system]. 4662 // 4663 // If -Wno-system-headers (the default), and the first import is as a 4664 // system module, then validation will fail during the as-user import, 4665 // since -Werror flags won't have been validated. However, it's reasonable 4666 // to treat this consistently as a system module. 4667 // 4668 // If -Wsystem-headers, the PCM on disk was built with 4669 // -Wno-system-headers, and the first import is as a user module, then 4670 // validation will fail during the as-system import since the PCM on disk 4671 // doesn't guarantee that -Werror was respected. However, the -Werror 4672 // flags were checked during the initial as-user import. 4673 if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) { 4674 Diag(diag::warn_module_system_bit_conflict) << F.FileName; 4675 return Success; 4676 } 4677 } 4678 4679 return Result; 4680 } 4681 4682 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl( 4683 ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities, 4684 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener, 4685 bool ValidateDiagnosticOptions) { 4686 // Initialize a stream. 4687 BitstreamCursor Stream(StreamData); 4688 4689 // Sniff for the signature. 4690 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4691 // FIXME this drops the error on the floor. 4692 consumeError(std::move(Err)); 4693 return Failure; 4694 } 4695 4696 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 4697 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 4698 return Failure; 4699 4700 // Read all of the records in the options block. 4701 RecordData Record; 4702 ASTReadResult Result = Success; 4703 while (true) { 4704 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4705 if (!MaybeEntry) { 4706 // FIXME this drops the error on the floor. 4707 consumeError(MaybeEntry.takeError()); 4708 return Failure; 4709 } 4710 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4711 4712 switch (Entry.Kind) { 4713 case llvm::BitstreamEntry::Error: 4714 case llvm::BitstreamEntry::SubBlock: 4715 return Failure; 4716 4717 case llvm::BitstreamEntry::EndBlock: 4718 return Result; 4719 4720 case llvm::BitstreamEntry::Record: 4721 // The interesting case. 4722 break; 4723 } 4724 4725 // Read and process a record. 4726 Record.clear(); 4727 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record); 4728 if (!MaybeRecordType) { 4729 // FIXME this drops the error. 4730 return Failure; 4731 } 4732 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) { 4733 case SIGNATURE: 4734 if (F) 4735 F->Signature = ASTFileSignature::create(Record.begin(), Record.end()); 4736 break; 4737 case AST_BLOCK_HASH: 4738 if (F) 4739 F->ASTBlockHash = 4740 ASTFileSignature::create(Record.begin(), Record.end()); 4741 break; 4742 case DIAGNOSTIC_OPTIONS: { 4743 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 4744 if (Listener && ValidateDiagnosticOptions && 4745 !AllowCompatibleConfigurationMismatch && 4746 ParseDiagnosticOptions(Record, Complain, *Listener)) 4747 Result = OutOfDate; // Don't return early. Read the signature. 4748 break; 4749 } 4750 case DIAG_PRAGMA_MAPPINGS: 4751 if (!F) 4752 break; 4753 if (F->PragmaDiagMappings.empty()) 4754 F->PragmaDiagMappings.swap(Record); 4755 else 4756 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(), 4757 Record.begin(), Record.end()); 4758 break; 4759 } 4760 } 4761 } 4762 4763 /// Parse a record and blob containing module file extension metadata. 4764 static bool parseModuleFileExtensionMetadata( 4765 const SmallVectorImpl<uint64_t> &Record, 4766 StringRef Blob, 4767 ModuleFileExtensionMetadata &Metadata) { 4768 if (Record.size() < 4) return true; 4769 4770 Metadata.MajorVersion = Record[0]; 4771 Metadata.MinorVersion = Record[1]; 4772 4773 unsigned BlockNameLen = Record[2]; 4774 unsigned UserInfoLen = Record[3]; 4775 4776 if (BlockNameLen + UserInfoLen > Blob.size()) return true; 4777 4778 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen); 4779 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen, 4780 Blob.data() + BlockNameLen + UserInfoLen); 4781 return false; 4782 } 4783 4784 llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) { 4785 BitstreamCursor &Stream = F.Stream; 4786 4787 RecordData Record; 4788 while (true) { 4789 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4790 if (!MaybeEntry) 4791 return MaybeEntry.takeError(); 4792 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4793 4794 switch (Entry.Kind) { 4795 case llvm::BitstreamEntry::SubBlock: 4796 if (llvm::Error Err = Stream.SkipBlock()) 4797 return Err; 4798 continue; 4799 case llvm::BitstreamEntry::EndBlock: 4800 return llvm::Error::success(); 4801 case llvm::BitstreamEntry::Error: 4802 return llvm::createStringError(std::errc::illegal_byte_sequence, 4803 "malformed block record in AST file"); 4804 case llvm::BitstreamEntry::Record: 4805 break; 4806 } 4807 4808 Record.clear(); 4809 StringRef Blob; 4810 Expected<unsigned> MaybeRecCode = 4811 Stream.readRecord(Entry.ID, Record, &Blob); 4812 if (!MaybeRecCode) 4813 return MaybeRecCode.takeError(); 4814 switch (MaybeRecCode.get()) { 4815 case EXTENSION_METADATA: { 4816 ModuleFileExtensionMetadata Metadata; 4817 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) 4818 return llvm::createStringError( 4819 std::errc::illegal_byte_sequence, 4820 "malformed EXTENSION_METADATA in AST file"); 4821 4822 // Find a module file extension with this block name. 4823 auto Known = ModuleFileExtensions.find(Metadata.BlockName); 4824 if (Known == ModuleFileExtensions.end()) break; 4825 4826 // Form a reader. 4827 if (auto Reader = Known->second->createExtensionReader(Metadata, *this, 4828 F, Stream)) { 4829 F.ExtensionReaders.push_back(std::move(Reader)); 4830 } 4831 4832 break; 4833 } 4834 } 4835 } 4836 4837 return llvm::Error::success(); 4838 } 4839 4840 void ASTReader::InitializeContext() { 4841 assert(ContextObj && "no context to initialize"); 4842 ASTContext &Context = *ContextObj; 4843 4844 // If there's a listener, notify them that we "read" the translation unit. 4845 if (DeserializationListener) 4846 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, 4847 Context.getTranslationUnitDecl()); 4848 4849 // FIXME: Find a better way to deal with collisions between these 4850 // built-in types. Right now, we just ignore the problem. 4851 4852 // Load the special types. 4853 if (SpecialTypes.size() >= NumSpecialTypeIDs) { 4854 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) { 4855 if (!Context.CFConstantStringTypeDecl) 4856 Context.setCFConstantStringType(GetType(String)); 4857 } 4858 4859 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) { 4860 QualType FileType = GetType(File); 4861 if (FileType.isNull()) { 4862 Error("FILE type is NULL"); 4863 return; 4864 } 4865 4866 if (!Context.FILEDecl) { 4867 if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) 4868 Context.setFILEDecl(Typedef->getDecl()); 4869 else { 4870 const TagType *Tag = FileType->getAs<TagType>(); 4871 if (!Tag) { 4872 Error("Invalid FILE type in AST file"); 4873 return; 4874 } 4875 Context.setFILEDecl(Tag->getDecl()); 4876 } 4877 } 4878 } 4879 4880 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) { 4881 QualType Jmp_bufType = GetType(Jmp_buf); 4882 if (Jmp_bufType.isNull()) { 4883 Error("jmp_buf type is NULL"); 4884 return; 4885 } 4886 4887 if (!Context.jmp_bufDecl) { 4888 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) 4889 Context.setjmp_bufDecl(Typedef->getDecl()); 4890 else { 4891 const TagType *Tag = Jmp_bufType->getAs<TagType>(); 4892 if (!Tag) { 4893 Error("Invalid jmp_buf type in AST file"); 4894 return; 4895 } 4896 Context.setjmp_bufDecl(Tag->getDecl()); 4897 } 4898 } 4899 } 4900 4901 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) { 4902 QualType Sigjmp_bufType = GetType(Sigjmp_buf); 4903 if (Sigjmp_bufType.isNull()) { 4904 Error("sigjmp_buf type is NULL"); 4905 return; 4906 } 4907 4908 if (!Context.sigjmp_bufDecl) { 4909 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) 4910 Context.setsigjmp_bufDecl(Typedef->getDecl()); 4911 else { 4912 const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); 4913 assert(Tag && "Invalid sigjmp_buf type in AST file"); 4914 Context.setsigjmp_bufDecl(Tag->getDecl()); 4915 } 4916 } 4917 } 4918 4919 if (unsigned ObjCIdRedef 4920 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) { 4921 if (Context.ObjCIdRedefinitionType.isNull()) 4922 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef); 4923 } 4924 4925 if (unsigned ObjCClassRedef 4926 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) { 4927 if (Context.ObjCClassRedefinitionType.isNull()) 4928 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef); 4929 } 4930 4931 if (unsigned ObjCSelRedef 4932 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) { 4933 if (Context.ObjCSelRedefinitionType.isNull()) 4934 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef); 4935 } 4936 4937 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) { 4938 QualType Ucontext_tType = GetType(Ucontext_t); 4939 if (Ucontext_tType.isNull()) { 4940 Error("ucontext_t type is NULL"); 4941 return; 4942 } 4943 4944 if (!Context.ucontext_tDecl) { 4945 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>()) 4946 Context.setucontext_tDecl(Typedef->getDecl()); 4947 else { 4948 const TagType *Tag = Ucontext_tType->getAs<TagType>(); 4949 assert(Tag && "Invalid ucontext_t type in AST file"); 4950 Context.setucontext_tDecl(Tag->getDecl()); 4951 } 4952 } 4953 } 4954 } 4955 4956 ReadPragmaDiagnosticMappings(Context.getDiagnostics()); 4957 4958 // If there were any CUDA special declarations, deserialize them. 4959 if (!CUDASpecialDeclRefs.empty()) { 4960 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!"); 4961 Context.setcudaConfigureCallDecl( 4962 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0]))); 4963 } 4964 4965 // Re-export any modules that were imported by a non-module AST file. 4966 // FIXME: This does not make macro-only imports visible again. 4967 for (auto &Import : ImportedModules) { 4968 if (Module *Imported = getSubmodule(Import.ID)) { 4969 makeModuleVisible(Imported, Module::AllVisible, 4970 /*ImportLoc=*/Import.ImportLoc); 4971 if (Import.ImportLoc.isValid()) 4972 PP.makeModuleVisible(Imported, Import.ImportLoc); 4973 // This updates visibility for Preprocessor only. For Sema, which can be 4974 // nullptr here, we do the same later, in UpdateSema(). 4975 } 4976 } 4977 } 4978 4979 void ASTReader::finalizeForWriting() { 4980 // Nothing to do for now. 4981 } 4982 4983 /// Reads and return the signature record from \p PCH's control block, or 4984 /// else returns 0. 4985 static ASTFileSignature readASTFileSignature(StringRef PCH) { 4986 BitstreamCursor Stream(PCH); 4987 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4988 // FIXME this drops the error on the floor. 4989 consumeError(std::move(Err)); 4990 return ASTFileSignature(); 4991 } 4992 4993 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 4994 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 4995 return ASTFileSignature(); 4996 4997 // Scan for SIGNATURE inside the diagnostic options block. 4998 ASTReader::RecordData Record; 4999 while (true) { 5000 Expected<llvm::BitstreamEntry> MaybeEntry = 5001 Stream.advanceSkippingSubblocks(); 5002 if (!MaybeEntry) { 5003 // FIXME this drops the error on the floor. 5004 consumeError(MaybeEntry.takeError()); 5005 return ASTFileSignature(); 5006 } 5007 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5008 5009 if (Entry.Kind != llvm::BitstreamEntry::Record) 5010 return ASTFileSignature(); 5011 5012 Record.clear(); 5013 StringRef Blob; 5014 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 5015 if (!MaybeRecord) { 5016 // FIXME this drops the error on the floor. 5017 consumeError(MaybeRecord.takeError()); 5018 return ASTFileSignature(); 5019 } 5020 if (SIGNATURE == MaybeRecord.get()) 5021 return ASTFileSignature::create(Record.begin(), 5022 Record.begin() + ASTFileSignature::size); 5023 } 5024 } 5025 5026 /// Retrieve the name of the original source file name 5027 /// directly from the AST file, without actually loading the AST 5028 /// file. 5029 std::string ASTReader::getOriginalSourceFile( 5030 const std::string &ASTFileName, FileManager &FileMgr, 5031 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) { 5032 // Open the AST file. 5033 auto Buffer = FileMgr.getBufferForFile(ASTFileName); 5034 if (!Buffer) { 5035 Diags.Report(diag::err_fe_unable_to_read_pch_file) 5036 << ASTFileName << Buffer.getError().message(); 5037 return std::string(); 5038 } 5039 5040 // Initialize the stream 5041 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer)); 5042 5043 // Sniff for the signature. 5044 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5045 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err); 5046 return std::string(); 5047 } 5048 5049 // Scan for the CONTROL_BLOCK_ID block. 5050 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) { 5051 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 5052 return std::string(); 5053 } 5054 5055 // Scan for ORIGINAL_FILE inside the control block. 5056 RecordData Record; 5057 while (true) { 5058 Expected<llvm::BitstreamEntry> MaybeEntry = 5059 Stream.advanceSkippingSubblocks(); 5060 if (!MaybeEntry) { 5061 // FIXME this drops errors on the floor. 5062 consumeError(MaybeEntry.takeError()); 5063 return std::string(); 5064 } 5065 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5066 5067 if (Entry.Kind == llvm::BitstreamEntry::EndBlock) 5068 return std::string(); 5069 5070 if (Entry.Kind != llvm::BitstreamEntry::Record) { 5071 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 5072 return std::string(); 5073 } 5074 5075 Record.clear(); 5076 StringRef Blob; 5077 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 5078 if (!MaybeRecord) { 5079 // FIXME this drops the errors on the floor. 5080 consumeError(MaybeRecord.takeError()); 5081 return std::string(); 5082 } 5083 if (ORIGINAL_FILE == MaybeRecord.get()) 5084 return Blob.str(); 5085 } 5086 } 5087 5088 namespace { 5089 5090 class SimplePCHValidator : public ASTReaderListener { 5091 const LangOptions &ExistingLangOpts; 5092 const TargetOptions &ExistingTargetOpts; 5093 const PreprocessorOptions &ExistingPPOpts; 5094 std::string ExistingModuleCachePath; 5095 FileManager &FileMgr; 5096 5097 public: 5098 SimplePCHValidator(const LangOptions &ExistingLangOpts, 5099 const TargetOptions &ExistingTargetOpts, 5100 const PreprocessorOptions &ExistingPPOpts, 5101 StringRef ExistingModuleCachePath, FileManager &FileMgr) 5102 : ExistingLangOpts(ExistingLangOpts), 5103 ExistingTargetOpts(ExistingTargetOpts), 5104 ExistingPPOpts(ExistingPPOpts), 5105 ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {} 5106 5107 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain, 5108 bool AllowCompatibleDifferences) override { 5109 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr, 5110 AllowCompatibleDifferences); 5111 } 5112 5113 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain, 5114 bool AllowCompatibleDifferences) override { 5115 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr, 5116 AllowCompatibleDifferences); 5117 } 5118 5119 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 5120 StringRef SpecificModuleCachePath, 5121 bool Complain) override { 5122 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5123 ExistingModuleCachePath, nullptr, 5124 ExistingLangOpts, ExistingPPOpts); 5125 } 5126 5127 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 5128 bool Complain, 5129 std::string &SuggestedPredefines) override { 5130 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr, 5131 SuggestedPredefines, ExistingLangOpts); 5132 } 5133 }; 5134 5135 } // namespace 5136 5137 bool ASTReader::readASTFileControlBlock( 5138 StringRef Filename, FileManager &FileMgr, 5139 const PCHContainerReader &PCHContainerRdr, 5140 bool FindModuleFileExtensions, 5141 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) { 5142 // Open the AST file. 5143 // FIXME: This allows use of the VFS; we do not allow use of the 5144 // VFS when actually loading a module. 5145 auto Buffer = FileMgr.getBufferForFile(Filename); 5146 if (!Buffer) { 5147 return true; 5148 } 5149 5150 // Initialize the stream 5151 StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer); 5152 BitstreamCursor Stream(Bytes); 5153 5154 // Sniff for the signature. 5155 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5156 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 5157 return true; 5158 } 5159 5160 // Scan for the CONTROL_BLOCK_ID block. 5161 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) 5162 return true; 5163 5164 bool NeedsInputFiles = Listener.needsInputFileVisitation(); 5165 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation(); 5166 bool NeedsImports = Listener.needsImportVisitation(); 5167 BitstreamCursor InputFilesCursor; 5168 5169 RecordData Record; 5170 std::string ModuleDir; 5171 bool DoneWithControlBlock = false; 5172 while (!DoneWithControlBlock) { 5173 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5174 if (!MaybeEntry) { 5175 // FIXME this drops the error on the floor. 5176 consumeError(MaybeEntry.takeError()); 5177 return true; 5178 } 5179 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5180 5181 switch (Entry.Kind) { 5182 case llvm::BitstreamEntry::SubBlock: { 5183 switch (Entry.ID) { 5184 case OPTIONS_BLOCK_ID: { 5185 std::string IgnoredSuggestedPredefines; 5186 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate, 5187 /*AllowCompatibleConfigurationMismatch*/ false, 5188 Listener, IgnoredSuggestedPredefines) != Success) 5189 return true; 5190 break; 5191 } 5192 5193 case INPUT_FILES_BLOCK_ID: 5194 InputFilesCursor = Stream; 5195 if (llvm::Error Err = Stream.SkipBlock()) { 5196 // FIXME this drops the error on the floor. 5197 consumeError(std::move(Err)); 5198 return true; 5199 } 5200 if (NeedsInputFiles && 5201 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID)) 5202 return true; 5203 break; 5204 5205 default: 5206 if (llvm::Error Err = Stream.SkipBlock()) { 5207 // FIXME this drops the error on the floor. 5208 consumeError(std::move(Err)); 5209 return true; 5210 } 5211 break; 5212 } 5213 5214 continue; 5215 } 5216 5217 case llvm::BitstreamEntry::EndBlock: 5218 DoneWithControlBlock = true; 5219 break; 5220 5221 case llvm::BitstreamEntry::Error: 5222 return true; 5223 5224 case llvm::BitstreamEntry::Record: 5225 break; 5226 } 5227 5228 if (DoneWithControlBlock) break; 5229 5230 Record.clear(); 5231 StringRef Blob; 5232 Expected<unsigned> MaybeRecCode = 5233 Stream.readRecord(Entry.ID, Record, &Blob); 5234 if (!MaybeRecCode) { 5235 // FIXME this drops the error. 5236 return Failure; 5237 } 5238 switch ((ControlRecordTypes)MaybeRecCode.get()) { 5239 case METADATA: 5240 if (Record[0] != VERSION_MAJOR) 5241 return true; 5242 if (Listener.ReadFullVersionInformation(Blob)) 5243 return true; 5244 break; 5245 case MODULE_NAME: 5246 Listener.ReadModuleName(Blob); 5247 break; 5248 case MODULE_DIRECTORY: 5249 ModuleDir = std::string(Blob); 5250 break; 5251 case MODULE_MAP_FILE: { 5252 unsigned Idx = 0; 5253 auto Path = ReadString(Record, Idx); 5254 ResolveImportedPath(Path, ModuleDir); 5255 Listener.ReadModuleMapFile(Path); 5256 break; 5257 } 5258 case INPUT_FILE_OFFSETS: { 5259 if (!NeedsInputFiles) 5260 break; 5261 5262 unsigned NumInputFiles = Record[0]; 5263 unsigned NumUserFiles = Record[1]; 5264 const llvm::support::unaligned_uint64_t *InputFileOffs = 5265 (const llvm::support::unaligned_uint64_t *)Blob.data(); 5266 for (unsigned I = 0; I != NumInputFiles; ++I) { 5267 // Go find this input file. 5268 bool isSystemFile = I >= NumUserFiles; 5269 5270 if (isSystemFile && !NeedsSystemInputFiles) 5271 break; // the rest are system input files 5272 5273 BitstreamCursor &Cursor = InputFilesCursor; 5274 SavedStreamPosition SavedPosition(Cursor); 5275 if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) { 5276 // FIXME this drops errors on the floor. 5277 consumeError(std::move(Err)); 5278 } 5279 5280 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 5281 if (!MaybeCode) { 5282 // FIXME this drops errors on the floor. 5283 consumeError(MaybeCode.takeError()); 5284 } 5285 unsigned Code = MaybeCode.get(); 5286 5287 RecordData Record; 5288 StringRef Blob; 5289 bool shouldContinue = false; 5290 Expected<unsigned> MaybeRecordType = 5291 Cursor.readRecord(Code, Record, &Blob); 5292 if (!MaybeRecordType) { 5293 // FIXME this drops errors on the floor. 5294 consumeError(MaybeRecordType.takeError()); 5295 } 5296 switch ((InputFileRecordTypes)MaybeRecordType.get()) { 5297 case INPUT_FILE_HASH: 5298 break; 5299 case INPUT_FILE: 5300 bool Overridden = static_cast<bool>(Record[3]); 5301 std::string Filename = std::string(Blob); 5302 ResolveImportedPath(Filename, ModuleDir); 5303 shouldContinue = Listener.visitInputFile( 5304 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false); 5305 break; 5306 } 5307 if (!shouldContinue) 5308 break; 5309 } 5310 break; 5311 } 5312 5313 case IMPORTS: { 5314 if (!NeedsImports) 5315 break; 5316 5317 unsigned Idx = 0, N = Record.size(); 5318 while (Idx < N) { 5319 // Read information about the AST file. 5320 Idx += 5321 1 + 1 + 1 + 1 + 5322 ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature 5323 std::string ModuleName = ReadString(Record, Idx); 5324 std::string Filename = ReadString(Record, Idx); 5325 ResolveImportedPath(Filename, ModuleDir); 5326 Listener.visitImport(ModuleName, Filename); 5327 } 5328 break; 5329 } 5330 5331 default: 5332 // No other validation to perform. 5333 break; 5334 } 5335 } 5336 5337 // Look for module file extension blocks, if requested. 5338 if (FindModuleFileExtensions) { 5339 BitstreamCursor SavedStream = Stream; 5340 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) { 5341 bool DoneWithExtensionBlock = false; 5342 while (!DoneWithExtensionBlock) { 5343 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5344 if (!MaybeEntry) { 5345 // FIXME this drops the error. 5346 return true; 5347 } 5348 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5349 5350 switch (Entry.Kind) { 5351 case llvm::BitstreamEntry::SubBlock: 5352 if (llvm::Error Err = Stream.SkipBlock()) { 5353 // FIXME this drops the error on the floor. 5354 consumeError(std::move(Err)); 5355 return true; 5356 } 5357 continue; 5358 5359 case llvm::BitstreamEntry::EndBlock: 5360 DoneWithExtensionBlock = true; 5361 continue; 5362 5363 case llvm::BitstreamEntry::Error: 5364 return true; 5365 5366 case llvm::BitstreamEntry::Record: 5367 break; 5368 } 5369 5370 Record.clear(); 5371 StringRef Blob; 5372 Expected<unsigned> MaybeRecCode = 5373 Stream.readRecord(Entry.ID, Record, &Blob); 5374 if (!MaybeRecCode) { 5375 // FIXME this drops the error. 5376 return true; 5377 } 5378 switch (MaybeRecCode.get()) { 5379 case EXTENSION_METADATA: { 5380 ModuleFileExtensionMetadata Metadata; 5381 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) 5382 return true; 5383 5384 Listener.readModuleFileExtension(Metadata); 5385 break; 5386 } 5387 } 5388 } 5389 } 5390 Stream = SavedStream; 5391 } 5392 5393 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 5394 if (readUnhashedControlBlockImpl( 5395 nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate, 5396 /*AllowCompatibleConfigurationMismatch*/ false, &Listener, 5397 ValidateDiagnosticOptions) != Success) 5398 return true; 5399 5400 return false; 5401 } 5402 5403 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr, 5404 const PCHContainerReader &PCHContainerRdr, 5405 const LangOptions &LangOpts, 5406 const TargetOptions &TargetOpts, 5407 const PreprocessorOptions &PPOpts, 5408 StringRef ExistingModuleCachePath) { 5409 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, 5410 ExistingModuleCachePath, FileMgr); 5411 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr, 5412 /*FindModuleFileExtensions=*/false, 5413 validator, 5414 /*ValidateDiagnosticOptions=*/true); 5415 } 5416 5417 llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F, 5418 unsigned ClientLoadCapabilities) { 5419 // Enter the submodule block. 5420 if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) 5421 return Err; 5422 5423 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap(); 5424 bool First = true; 5425 Module *CurrentModule = nullptr; 5426 RecordData Record; 5427 while (true) { 5428 Expected<llvm::BitstreamEntry> MaybeEntry = 5429 F.Stream.advanceSkippingSubblocks(); 5430 if (!MaybeEntry) 5431 return MaybeEntry.takeError(); 5432 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5433 5434 switch (Entry.Kind) { 5435 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 5436 case llvm::BitstreamEntry::Error: 5437 return llvm::createStringError(std::errc::illegal_byte_sequence, 5438 "malformed block record in AST file"); 5439 case llvm::BitstreamEntry::EndBlock: 5440 return llvm::Error::success(); 5441 case llvm::BitstreamEntry::Record: 5442 // The interesting case. 5443 break; 5444 } 5445 5446 // Read a record. 5447 StringRef Blob; 5448 Record.clear(); 5449 Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob); 5450 if (!MaybeKind) 5451 return MaybeKind.takeError(); 5452 unsigned Kind = MaybeKind.get(); 5453 5454 if ((Kind == SUBMODULE_METADATA) != First) 5455 return llvm::createStringError( 5456 std::errc::illegal_byte_sequence, 5457 "submodule metadata record should be at beginning of block"); 5458 First = false; 5459 5460 // Submodule information is only valid if we have a current module. 5461 // FIXME: Should we error on these cases? 5462 if (!CurrentModule && Kind != SUBMODULE_METADATA && 5463 Kind != SUBMODULE_DEFINITION) 5464 continue; 5465 5466 switch (Kind) { 5467 default: // Default behavior: ignore. 5468 break; 5469 5470 case SUBMODULE_DEFINITION: { 5471 if (Record.size() < 12) 5472 return llvm::createStringError(std::errc::illegal_byte_sequence, 5473 "malformed module definition"); 5474 5475 StringRef Name = Blob; 5476 unsigned Idx = 0; 5477 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]); 5478 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]); 5479 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++]; 5480 bool IsFramework = Record[Idx++]; 5481 bool IsExplicit = Record[Idx++]; 5482 bool IsSystem = Record[Idx++]; 5483 bool IsExternC = Record[Idx++]; 5484 bool InferSubmodules = Record[Idx++]; 5485 bool InferExplicitSubmodules = Record[Idx++]; 5486 bool InferExportWildcard = Record[Idx++]; 5487 bool ConfigMacrosExhaustive = Record[Idx++]; 5488 bool ModuleMapIsPrivate = Record[Idx++]; 5489 5490 Module *ParentModule = nullptr; 5491 if (Parent) 5492 ParentModule = getSubmodule(Parent); 5493 5494 // Retrieve this (sub)module from the module map, creating it if 5495 // necessary. 5496 CurrentModule = 5497 ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit) 5498 .first; 5499 5500 // FIXME: set the definition loc for CurrentModule, or call 5501 // ModMap.setInferredModuleAllowedBy() 5502 5503 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS; 5504 if (GlobalIndex >= SubmodulesLoaded.size() || 5505 SubmodulesLoaded[GlobalIndex]) 5506 return llvm::createStringError(std::errc::invalid_argument, 5507 "too many submodules"); 5508 5509 if (!ParentModule) { 5510 if (const FileEntry *CurFile = CurrentModule->getASTFile()) { 5511 // Don't emit module relocation error if we have -fno-validate-pch 5512 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation & 5513 DisableValidationForModuleKind::Module) && 5514 CurFile != F.File) { 5515 auto ConflictError = 5516 PartialDiagnostic(diag::err_module_file_conflict, 5517 ContextObj->DiagAllocator) 5518 << CurrentModule->getTopLevelModuleName() << CurFile->getName() 5519 << F.File->getName(); 5520 return DiagnosticError::create(CurrentImportLoc, ConflictError); 5521 } 5522 } 5523 5524 F.DidReadTopLevelSubmodule = true; 5525 CurrentModule->setASTFile(F.File); 5526 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath; 5527 } 5528 5529 CurrentModule->Kind = Kind; 5530 CurrentModule->Signature = F.Signature; 5531 CurrentModule->IsFromModuleFile = true; 5532 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem; 5533 CurrentModule->IsExternC = IsExternC; 5534 CurrentModule->InferSubmodules = InferSubmodules; 5535 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules; 5536 CurrentModule->InferExportWildcard = InferExportWildcard; 5537 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive; 5538 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate; 5539 if (DeserializationListener) 5540 DeserializationListener->ModuleRead(GlobalID, CurrentModule); 5541 5542 SubmodulesLoaded[GlobalIndex] = CurrentModule; 5543 5544 // Clear out data that will be replaced by what is in the module file. 5545 CurrentModule->LinkLibraries.clear(); 5546 CurrentModule->ConfigMacros.clear(); 5547 CurrentModule->UnresolvedConflicts.clear(); 5548 CurrentModule->Conflicts.clear(); 5549 5550 // The module is available unless it's missing a requirement; relevant 5551 // requirements will be (re-)added by SUBMODULE_REQUIRES records. 5552 // Missing headers that were present when the module was built do not 5553 // make it unavailable -- if we got this far, this must be an explicitly 5554 // imported module file. 5555 CurrentModule->Requirements.clear(); 5556 CurrentModule->MissingHeaders.clear(); 5557 CurrentModule->IsUnimportable = 5558 ParentModule && ParentModule->IsUnimportable; 5559 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable; 5560 break; 5561 } 5562 5563 case SUBMODULE_UMBRELLA_HEADER: { 5564 // FIXME: This doesn't work for framework modules as `Filename` is the 5565 // name as written in the module file and does not include 5566 // `Headers/`, so this path will never exist. 5567 std::string Filename = std::string(Blob); 5568 ResolveImportedPath(F, Filename); 5569 if (auto Umbrella = PP.getFileManager().getFile(Filename)) { 5570 if (!CurrentModule->getUmbrellaHeader()) { 5571 // FIXME: NameAsWritten 5572 ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob, ""); 5573 } 5574 // Note that it's too late at this point to return out of date if the 5575 // name from the PCM doesn't match up with the one in the module map, 5576 // but also quite unlikely since we will have already checked the 5577 // modification time and size of the module map file itself. 5578 } 5579 break; 5580 } 5581 5582 case SUBMODULE_HEADER: 5583 case SUBMODULE_EXCLUDED_HEADER: 5584 case SUBMODULE_PRIVATE_HEADER: 5585 // We lazily associate headers with their modules via the HeaderInfo table. 5586 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead 5587 // of complete filenames or remove it entirely. 5588 break; 5589 5590 case SUBMODULE_TEXTUAL_HEADER: 5591 case SUBMODULE_PRIVATE_TEXTUAL_HEADER: 5592 // FIXME: Textual headers are not marked in the HeaderInfo table. Load 5593 // them here. 5594 break; 5595 5596 case SUBMODULE_TOPHEADER: 5597 CurrentModule->addTopHeaderFilename(Blob); 5598 break; 5599 5600 case SUBMODULE_UMBRELLA_DIR: { 5601 // See comments in SUBMODULE_UMBRELLA_HEADER 5602 std::string Dirname = std::string(Blob); 5603 ResolveImportedPath(F, Dirname); 5604 if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) { 5605 if (!CurrentModule->getUmbrellaDir()) { 5606 // FIXME: NameAsWritten 5607 ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob, ""); 5608 } 5609 } 5610 break; 5611 } 5612 5613 case SUBMODULE_METADATA: { 5614 F.BaseSubmoduleID = getTotalNumSubmodules(); 5615 F.LocalNumSubmodules = Record[0]; 5616 unsigned LocalBaseSubmoduleID = Record[1]; 5617 if (F.LocalNumSubmodules > 0) { 5618 // Introduce the global -> local mapping for submodules within this 5619 // module. 5620 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F)); 5621 5622 // Introduce the local -> global mapping for submodules within this 5623 // module. 5624 F.SubmoduleRemap.insertOrReplace( 5625 std::make_pair(LocalBaseSubmoduleID, 5626 F.BaseSubmoduleID - LocalBaseSubmoduleID)); 5627 5628 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules); 5629 } 5630 break; 5631 } 5632 5633 case SUBMODULE_IMPORTS: 5634 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) { 5635 UnresolvedModuleRef Unresolved; 5636 Unresolved.File = &F; 5637 Unresolved.Mod = CurrentModule; 5638 Unresolved.ID = Record[Idx]; 5639 Unresolved.Kind = UnresolvedModuleRef::Import; 5640 Unresolved.IsWildcard = false; 5641 UnresolvedModuleRefs.push_back(Unresolved); 5642 } 5643 break; 5644 5645 case SUBMODULE_EXPORTS: 5646 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) { 5647 UnresolvedModuleRef Unresolved; 5648 Unresolved.File = &F; 5649 Unresolved.Mod = CurrentModule; 5650 Unresolved.ID = Record[Idx]; 5651 Unresolved.Kind = UnresolvedModuleRef::Export; 5652 Unresolved.IsWildcard = Record[Idx + 1]; 5653 UnresolvedModuleRefs.push_back(Unresolved); 5654 } 5655 5656 // Once we've loaded the set of exports, there's no reason to keep 5657 // the parsed, unresolved exports around. 5658 CurrentModule->UnresolvedExports.clear(); 5659 break; 5660 5661 case SUBMODULE_REQUIRES: 5662 CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(), 5663 PP.getTargetInfo()); 5664 break; 5665 5666 case SUBMODULE_LINK_LIBRARY: 5667 ModMap.resolveLinkAsDependencies(CurrentModule); 5668 CurrentModule->LinkLibraries.push_back( 5669 Module::LinkLibrary(std::string(Blob), Record[0])); 5670 break; 5671 5672 case SUBMODULE_CONFIG_MACRO: 5673 CurrentModule->ConfigMacros.push_back(Blob.str()); 5674 break; 5675 5676 case SUBMODULE_CONFLICT: { 5677 UnresolvedModuleRef Unresolved; 5678 Unresolved.File = &F; 5679 Unresolved.Mod = CurrentModule; 5680 Unresolved.ID = Record[0]; 5681 Unresolved.Kind = UnresolvedModuleRef::Conflict; 5682 Unresolved.IsWildcard = false; 5683 Unresolved.String = Blob; 5684 UnresolvedModuleRefs.push_back(Unresolved); 5685 break; 5686 } 5687 5688 case SUBMODULE_INITIALIZERS: { 5689 if (!ContextObj) 5690 break; 5691 SmallVector<uint32_t, 16> Inits; 5692 for (auto &ID : Record) 5693 Inits.push_back(getGlobalDeclID(F, ID)); 5694 ContextObj->addLazyModuleInitializers(CurrentModule, Inits); 5695 break; 5696 } 5697 5698 case SUBMODULE_EXPORT_AS: 5699 CurrentModule->ExportAsModule = Blob.str(); 5700 ModMap.addLinkAsDependency(CurrentModule); 5701 break; 5702 } 5703 } 5704 } 5705 5706 /// Parse the record that corresponds to a LangOptions data 5707 /// structure. 5708 /// 5709 /// This routine parses the language options from the AST file and then gives 5710 /// them to the AST listener if one is set. 5711 /// 5712 /// \returns true if the listener deems the file unacceptable, false otherwise. 5713 bool ASTReader::ParseLanguageOptions(const RecordData &Record, 5714 bool Complain, 5715 ASTReaderListener &Listener, 5716 bool AllowCompatibleDifferences) { 5717 LangOptions LangOpts; 5718 unsigned Idx = 0; 5719 #define LANGOPT(Name, Bits, Default, Description) \ 5720 LangOpts.Name = Record[Idx++]; 5721 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 5722 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++])); 5723 #include "clang/Basic/LangOptions.def" 5724 #define SANITIZER(NAME, ID) \ 5725 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]); 5726 #include "clang/Basic/Sanitizers.def" 5727 5728 for (unsigned N = Record[Idx++]; N; --N) 5729 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx)); 5730 5731 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++]; 5732 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx); 5733 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion); 5734 5735 LangOpts.CurrentModule = ReadString(Record, Idx); 5736 5737 // Comment options. 5738 for (unsigned N = Record[Idx++]; N; --N) { 5739 LangOpts.CommentOpts.BlockCommandNames.push_back( 5740 ReadString(Record, Idx)); 5741 } 5742 LangOpts.CommentOpts.ParseAllComments = Record[Idx++]; 5743 5744 // OpenMP offloading options. 5745 for (unsigned N = Record[Idx++]; N; --N) { 5746 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx))); 5747 } 5748 5749 LangOpts.OMPHostIRFile = ReadString(Record, Idx); 5750 5751 return Listener.ReadLanguageOptions(LangOpts, Complain, 5752 AllowCompatibleDifferences); 5753 } 5754 5755 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain, 5756 ASTReaderListener &Listener, 5757 bool AllowCompatibleDifferences) { 5758 unsigned Idx = 0; 5759 TargetOptions TargetOpts; 5760 TargetOpts.Triple = ReadString(Record, Idx); 5761 TargetOpts.CPU = ReadString(Record, Idx); 5762 TargetOpts.TuneCPU = ReadString(Record, Idx); 5763 TargetOpts.ABI = ReadString(Record, Idx); 5764 for (unsigned N = Record[Idx++]; N; --N) { 5765 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx)); 5766 } 5767 for (unsigned N = Record[Idx++]; N; --N) { 5768 TargetOpts.Features.push_back(ReadString(Record, Idx)); 5769 } 5770 5771 return Listener.ReadTargetOptions(TargetOpts, Complain, 5772 AllowCompatibleDifferences); 5773 } 5774 5775 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain, 5776 ASTReaderListener &Listener) { 5777 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions); 5778 unsigned Idx = 0; 5779 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++]; 5780 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \ 5781 DiagOpts->set##Name(static_cast<Type>(Record[Idx++])); 5782 #include "clang/Basic/DiagnosticOptions.def" 5783 5784 for (unsigned N = Record[Idx++]; N; --N) 5785 DiagOpts->Warnings.push_back(ReadString(Record, Idx)); 5786 for (unsigned N = Record[Idx++]; N; --N) 5787 DiagOpts->Remarks.push_back(ReadString(Record, Idx)); 5788 5789 return Listener.ReadDiagnosticOptions(DiagOpts, Complain); 5790 } 5791 5792 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain, 5793 ASTReaderListener &Listener) { 5794 FileSystemOptions FSOpts; 5795 unsigned Idx = 0; 5796 FSOpts.WorkingDir = ReadString(Record, Idx); 5797 return Listener.ReadFileSystemOptions(FSOpts, Complain); 5798 } 5799 5800 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record, 5801 bool Complain, 5802 ASTReaderListener &Listener) { 5803 HeaderSearchOptions HSOpts; 5804 unsigned Idx = 0; 5805 HSOpts.Sysroot = ReadString(Record, Idx); 5806 5807 // Include entries. 5808 for (unsigned N = Record[Idx++]; N; --N) { 5809 std::string Path = ReadString(Record, Idx); 5810 frontend::IncludeDirGroup Group 5811 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]); 5812 bool IsFramework = Record[Idx++]; 5813 bool IgnoreSysRoot = Record[Idx++]; 5814 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework, 5815 IgnoreSysRoot); 5816 } 5817 5818 // System header prefixes. 5819 for (unsigned N = Record[Idx++]; N; --N) { 5820 std::string Prefix = ReadString(Record, Idx); 5821 bool IsSystemHeader = Record[Idx++]; 5822 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader); 5823 } 5824 5825 HSOpts.ResourceDir = ReadString(Record, Idx); 5826 HSOpts.ModuleCachePath = ReadString(Record, Idx); 5827 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx); 5828 HSOpts.DisableModuleHash = Record[Idx++]; 5829 HSOpts.ImplicitModuleMaps = Record[Idx++]; 5830 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++]; 5831 HSOpts.EnablePrebuiltImplicitModules = Record[Idx++]; 5832 HSOpts.UseBuiltinIncludes = Record[Idx++]; 5833 HSOpts.UseStandardSystemIncludes = Record[Idx++]; 5834 HSOpts.UseStandardCXXIncludes = Record[Idx++]; 5835 HSOpts.UseLibcxx = Record[Idx++]; 5836 std::string SpecificModuleCachePath = ReadString(Record, Idx); 5837 5838 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5839 Complain); 5840 } 5841 5842 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record, 5843 bool Complain, 5844 ASTReaderListener &Listener, 5845 std::string &SuggestedPredefines) { 5846 PreprocessorOptions PPOpts; 5847 unsigned Idx = 0; 5848 5849 // Macro definitions/undefs 5850 for (unsigned N = Record[Idx++]; N; --N) { 5851 std::string Macro = ReadString(Record, Idx); 5852 bool IsUndef = Record[Idx++]; 5853 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef)); 5854 } 5855 5856 // Includes 5857 for (unsigned N = Record[Idx++]; N; --N) { 5858 PPOpts.Includes.push_back(ReadString(Record, Idx)); 5859 } 5860 5861 // Macro Includes 5862 for (unsigned N = Record[Idx++]; N; --N) { 5863 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx)); 5864 } 5865 5866 PPOpts.UsePredefines = Record[Idx++]; 5867 PPOpts.DetailedRecord = Record[Idx++]; 5868 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx); 5869 PPOpts.ObjCXXARCStandardLibrary = 5870 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]); 5871 SuggestedPredefines.clear(); 5872 return Listener.ReadPreprocessorOptions(PPOpts, Complain, 5873 SuggestedPredefines); 5874 } 5875 5876 std::pair<ModuleFile *, unsigned> 5877 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) { 5878 GlobalPreprocessedEntityMapType::iterator 5879 I = GlobalPreprocessedEntityMap.find(GlobalIndex); 5880 assert(I != GlobalPreprocessedEntityMap.end() && 5881 "Corrupted global preprocessed entity map"); 5882 ModuleFile *M = I->second; 5883 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID; 5884 return std::make_pair(M, LocalIndex); 5885 } 5886 5887 llvm::iterator_range<PreprocessingRecord::iterator> 5888 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const { 5889 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord()) 5890 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID, 5891 Mod.NumPreprocessedEntities); 5892 5893 return llvm::make_range(PreprocessingRecord::iterator(), 5894 PreprocessingRecord::iterator()); 5895 } 5896 5897 bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName, 5898 unsigned int ClientLoadCapabilities) { 5899 return ClientLoadCapabilities & ARR_OutOfDate && 5900 !getModuleManager().getModuleCache().isPCMFinal(ModuleFileName); 5901 } 5902 5903 llvm::iterator_range<ASTReader::ModuleDeclIterator> 5904 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) { 5905 return llvm::make_range( 5906 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls), 5907 ModuleDeclIterator(this, &Mod, 5908 Mod.FileSortedDecls + Mod.NumFileSortedDecls)); 5909 } 5910 5911 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) { 5912 auto I = GlobalSkippedRangeMap.find(GlobalIndex); 5913 assert(I != GlobalSkippedRangeMap.end() && 5914 "Corrupted global skipped range map"); 5915 ModuleFile *M = I->second; 5916 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID; 5917 assert(LocalIndex < M->NumPreprocessedSkippedRanges); 5918 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex]; 5919 SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()), 5920 TranslateSourceLocation(*M, RawRange.getEnd())); 5921 assert(Range.isValid()); 5922 return Range; 5923 } 5924 5925 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) { 5926 PreprocessedEntityID PPID = Index+1; 5927 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 5928 ModuleFile &M = *PPInfo.first; 5929 unsigned LocalIndex = PPInfo.second; 5930 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 5931 5932 if (!PP.getPreprocessingRecord()) { 5933 Error("no preprocessing record"); 5934 return nullptr; 5935 } 5936 5937 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor); 5938 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit( 5939 M.MacroOffsetsBase + PPOffs.BitOffset)) { 5940 Error(std::move(Err)); 5941 return nullptr; 5942 } 5943 5944 Expected<llvm::BitstreamEntry> MaybeEntry = 5945 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 5946 if (!MaybeEntry) { 5947 Error(MaybeEntry.takeError()); 5948 return nullptr; 5949 } 5950 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5951 5952 if (Entry.Kind != llvm::BitstreamEntry::Record) 5953 return nullptr; 5954 5955 // Read the record. 5956 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()), 5957 TranslateSourceLocation(M, PPOffs.getEnd())); 5958 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 5959 StringRef Blob; 5960 RecordData Record; 5961 Expected<unsigned> MaybeRecType = 5962 M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob); 5963 if (!MaybeRecType) { 5964 Error(MaybeRecType.takeError()); 5965 return nullptr; 5966 } 5967 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) { 5968 case PPD_MACRO_EXPANSION: { 5969 bool isBuiltin = Record[0]; 5970 IdentifierInfo *Name = nullptr; 5971 MacroDefinitionRecord *Def = nullptr; 5972 if (isBuiltin) 5973 Name = getLocalIdentifier(M, Record[1]); 5974 else { 5975 PreprocessedEntityID GlobalID = 5976 getGlobalPreprocessedEntityID(M, Record[1]); 5977 Def = cast<MacroDefinitionRecord>( 5978 PPRec.getLoadedPreprocessedEntity(GlobalID - 1)); 5979 } 5980 5981 MacroExpansion *ME; 5982 if (isBuiltin) 5983 ME = new (PPRec) MacroExpansion(Name, Range); 5984 else 5985 ME = new (PPRec) MacroExpansion(Def, Range); 5986 5987 return ME; 5988 } 5989 5990 case PPD_MACRO_DEFINITION: { 5991 // Decode the identifier info and then check again; if the macro is 5992 // still defined and associated with the identifier, 5993 IdentifierInfo *II = getLocalIdentifier(M, Record[0]); 5994 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range); 5995 5996 if (DeserializationListener) 5997 DeserializationListener->MacroDefinitionRead(PPID, MD); 5998 5999 return MD; 6000 } 6001 6002 case PPD_INCLUSION_DIRECTIVE: { 6003 const char *FullFileNameStart = Blob.data() + Record[0]; 6004 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]); 6005 const FileEntry *File = nullptr; 6006 if (!FullFileName.empty()) 6007 if (auto FE = PP.getFileManager().getFile(FullFileName)) 6008 File = *FE; 6009 6010 // FIXME: Stable encoding 6011 InclusionDirective::InclusionKind Kind 6012 = static_cast<InclusionDirective::InclusionKind>(Record[2]); 6013 InclusionDirective *ID 6014 = new (PPRec) InclusionDirective(PPRec, Kind, 6015 StringRef(Blob.data(), Record[0]), 6016 Record[1], Record[3], 6017 File, 6018 Range); 6019 return ID; 6020 } 6021 } 6022 6023 llvm_unreachable("Invalid PreprocessorDetailRecordTypes"); 6024 } 6025 6026 /// Find the next module that contains entities and return the ID 6027 /// of the first entry. 6028 /// 6029 /// \param SLocMapI points at a chunk of a module that contains no 6030 /// preprocessed entities or the entities it contains are not the ones we are 6031 /// looking for. 6032 PreprocessedEntityID ASTReader::findNextPreprocessedEntity( 6033 GlobalSLocOffsetMapType::const_iterator SLocMapI) const { 6034 ++SLocMapI; 6035 for (GlobalSLocOffsetMapType::const_iterator 6036 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) { 6037 ModuleFile &M = *SLocMapI->second; 6038 if (M.NumPreprocessedEntities) 6039 return M.BasePreprocessedEntityID; 6040 } 6041 6042 return getTotalNumPreprocessedEntities(); 6043 } 6044 6045 namespace { 6046 6047 struct PPEntityComp { 6048 const ASTReader &Reader; 6049 ModuleFile &M; 6050 6051 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {} 6052 6053 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const { 6054 SourceLocation LHS = getLoc(L); 6055 SourceLocation RHS = getLoc(R); 6056 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6057 } 6058 6059 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const { 6060 SourceLocation LHS = getLoc(L); 6061 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6062 } 6063 6064 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const { 6065 SourceLocation RHS = getLoc(R); 6066 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6067 } 6068 6069 SourceLocation getLoc(const PPEntityOffset &PPE) const { 6070 return Reader.TranslateSourceLocation(M, PPE.getBegin()); 6071 } 6072 }; 6073 6074 } // namespace 6075 6076 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc, 6077 bool EndsAfter) const { 6078 if (SourceMgr.isLocalSourceLocation(Loc)) 6079 return getTotalNumPreprocessedEntities(); 6080 6081 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find( 6082 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1); 6083 assert(SLocMapI != GlobalSLocOffsetMap.end() && 6084 "Corrupted global sloc offset map"); 6085 6086 if (SLocMapI->second->NumPreprocessedEntities == 0) 6087 return findNextPreprocessedEntity(SLocMapI); 6088 6089 ModuleFile &M = *SLocMapI->second; 6090 6091 using pp_iterator = const PPEntityOffset *; 6092 6093 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 6094 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 6095 6096 size_t Count = M.NumPreprocessedEntities; 6097 size_t Half; 6098 pp_iterator First = pp_begin; 6099 pp_iterator PPI; 6100 6101 if (EndsAfter) { 6102 PPI = std::upper_bound(pp_begin, pp_end, Loc, 6103 PPEntityComp(*this, M)); 6104 } else { 6105 // Do a binary search manually instead of using std::lower_bound because 6106 // The end locations of entities may be unordered (when a macro expansion 6107 // is inside another macro argument), but for this case it is not important 6108 // whether we get the first macro expansion or its containing macro. 6109 while (Count > 0) { 6110 Half = Count / 2; 6111 PPI = First; 6112 std::advance(PPI, Half); 6113 if (SourceMgr.isBeforeInTranslationUnit( 6114 TranslateSourceLocation(M, PPI->getEnd()), Loc)) { 6115 First = PPI; 6116 ++First; 6117 Count = Count - Half - 1; 6118 } else 6119 Count = Half; 6120 } 6121 } 6122 6123 if (PPI == pp_end) 6124 return findNextPreprocessedEntity(SLocMapI); 6125 6126 return M.BasePreprocessedEntityID + (PPI - pp_begin); 6127 } 6128 6129 /// Returns a pair of [Begin, End) indices of preallocated 6130 /// preprocessed entities that \arg Range encompasses. 6131 std::pair<unsigned, unsigned> 6132 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) { 6133 if (Range.isInvalid()) 6134 return std::make_pair(0,0); 6135 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); 6136 6137 PreprocessedEntityID BeginID = 6138 findPreprocessedEntity(Range.getBegin(), false); 6139 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true); 6140 return std::make_pair(BeginID, EndID); 6141 } 6142 6143 /// Optionally returns true or false if the preallocated preprocessed 6144 /// entity with index \arg Index came from file \arg FID. 6145 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index, 6146 FileID FID) { 6147 if (FID.isInvalid()) 6148 return false; 6149 6150 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 6151 ModuleFile &M = *PPInfo.first; 6152 unsigned LocalIndex = PPInfo.second; 6153 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 6154 6155 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin()); 6156 if (Loc.isInvalid()) 6157 return false; 6158 6159 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID)) 6160 return true; 6161 else 6162 return false; 6163 } 6164 6165 namespace { 6166 6167 /// Visitor used to search for information about a header file. 6168 class HeaderFileInfoVisitor { 6169 const FileEntry *FE; 6170 Optional<HeaderFileInfo> HFI; 6171 6172 public: 6173 explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {} 6174 6175 bool operator()(ModuleFile &M) { 6176 HeaderFileInfoLookupTable *Table 6177 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable); 6178 if (!Table) 6179 return false; 6180 6181 // Look in the on-disk hash table for an entry for this file name. 6182 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE); 6183 if (Pos == Table->end()) 6184 return false; 6185 6186 HFI = *Pos; 6187 return true; 6188 } 6189 6190 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; } 6191 }; 6192 6193 } // namespace 6194 6195 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) { 6196 HeaderFileInfoVisitor Visitor(FE); 6197 ModuleMgr.visit(Visitor); 6198 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) 6199 return *HFI; 6200 6201 return HeaderFileInfo(); 6202 } 6203 6204 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) { 6205 using DiagState = DiagnosticsEngine::DiagState; 6206 SmallVector<DiagState *, 32> DiagStates; 6207 6208 for (ModuleFile &F : ModuleMgr) { 6209 unsigned Idx = 0; 6210 auto &Record = F.PragmaDiagMappings; 6211 if (Record.empty()) 6212 continue; 6213 6214 DiagStates.clear(); 6215 6216 auto ReadDiagState = 6217 [&](const DiagState &BasedOn, SourceLocation Loc, 6218 bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * { 6219 unsigned BackrefID = Record[Idx++]; 6220 if (BackrefID != 0) 6221 return DiagStates[BackrefID - 1]; 6222 6223 // A new DiagState was created here. 6224 Diag.DiagStates.push_back(BasedOn); 6225 DiagState *NewState = &Diag.DiagStates.back(); 6226 DiagStates.push_back(NewState); 6227 unsigned Size = Record[Idx++]; 6228 assert(Idx + Size * 2 <= Record.size() && 6229 "Invalid data, not enough diag/map pairs"); 6230 while (Size--) { 6231 unsigned DiagID = Record[Idx++]; 6232 DiagnosticMapping NewMapping = 6233 DiagnosticMapping::deserialize(Record[Idx++]); 6234 if (!NewMapping.isPragma() && !IncludeNonPragmaStates) 6235 continue; 6236 6237 DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID); 6238 6239 // If this mapping was specified as a warning but the severity was 6240 // upgraded due to diagnostic settings, simulate the current diagnostic 6241 // settings (and use a warning). 6242 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) { 6243 NewMapping.setSeverity(diag::Severity::Warning); 6244 NewMapping.setUpgradedFromWarning(false); 6245 } 6246 6247 Mapping = NewMapping; 6248 } 6249 return NewState; 6250 }; 6251 6252 // Read the first state. 6253 DiagState *FirstState; 6254 if (F.Kind == MK_ImplicitModule) { 6255 // Implicitly-built modules are reused with different diagnostic 6256 // settings. Use the initial diagnostic state from Diag to simulate this 6257 // compilation's diagnostic settings. 6258 FirstState = Diag.DiagStatesByLoc.FirstDiagState; 6259 DiagStates.push_back(FirstState); 6260 6261 // Skip the initial diagnostic state from the serialized module. 6262 assert(Record[1] == 0 && 6263 "Invalid data, unexpected backref in initial state"); 6264 Idx = 3 + Record[2] * 2; 6265 assert(Idx < Record.size() && 6266 "Invalid data, not enough state change pairs in initial state"); 6267 } else if (F.isModule()) { 6268 // For an explicit module, preserve the flags from the module build 6269 // command line (-w, -Weverything, -Werror, ...) along with any explicit 6270 // -Wblah flags. 6271 unsigned Flags = Record[Idx++]; 6272 DiagState Initial; 6273 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1; 6274 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1; 6275 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1; 6276 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1; 6277 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1; 6278 Initial.ExtBehavior = (diag::Severity)Flags; 6279 FirstState = ReadDiagState(Initial, SourceLocation(), true); 6280 6281 assert(F.OriginalSourceFileID.isValid()); 6282 6283 // Set up the root buffer of the module to start with the initial 6284 // diagnostic state of the module itself, to cover files that contain no 6285 // explicit transitions (for which we did not serialize anything). 6286 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID] 6287 .StateTransitions.push_back({FirstState, 0}); 6288 } else { 6289 // For prefix ASTs, start with whatever the user configured on the 6290 // command line. 6291 Idx++; // Skip flags. 6292 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, 6293 SourceLocation(), false); 6294 } 6295 6296 // Read the state transitions. 6297 unsigned NumLocations = Record[Idx++]; 6298 while (NumLocations--) { 6299 assert(Idx < Record.size() && 6300 "Invalid data, missing pragma diagnostic states"); 6301 SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]); 6302 auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc); 6303 assert(IDAndOffset.first.isValid() && "invalid FileID for transition"); 6304 assert(IDAndOffset.second == 0 && "not a start location for a FileID"); 6305 unsigned Transitions = Record[Idx++]; 6306 6307 // Note that we don't need to set up Parent/ParentOffset here, because 6308 // we won't be changing the diagnostic state within imported FileIDs 6309 // (other than perhaps appending to the main source file, which has no 6310 // parent). 6311 auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first]; 6312 F.StateTransitions.reserve(F.StateTransitions.size() + Transitions); 6313 for (unsigned I = 0; I != Transitions; ++I) { 6314 unsigned Offset = Record[Idx++]; 6315 auto *State = 6316 ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false); 6317 F.StateTransitions.push_back({State, Offset}); 6318 } 6319 } 6320 6321 // Read the final state. 6322 assert(Idx < Record.size() && 6323 "Invalid data, missing final pragma diagnostic state"); 6324 SourceLocation CurStateLoc = 6325 ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]); 6326 auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false); 6327 6328 if (!F.isModule()) { 6329 Diag.DiagStatesByLoc.CurDiagState = CurState; 6330 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc; 6331 6332 // Preserve the property that the imaginary root file describes the 6333 // current state. 6334 FileID NullFile; 6335 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions; 6336 if (T.empty()) 6337 T.push_back({CurState, 0}); 6338 else 6339 T[0].State = CurState; 6340 } 6341 6342 // Don't try to read these mappings again. 6343 Record.clear(); 6344 } 6345 } 6346 6347 /// Get the correct cursor and offset for loading a type. 6348 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) { 6349 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index); 6350 assert(I != GlobalTypeMap.end() && "Corrupted global type map"); 6351 ModuleFile *M = I->second; 6352 return RecordLocation( 6353 M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() + 6354 M->DeclsBlockStartOffset); 6355 } 6356 6357 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) { 6358 switch (code) { 6359 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \ 6360 case TYPE_##CODE_ID: return Type::CLASS_ID; 6361 #include "clang/Serialization/TypeBitCodes.def" 6362 default: return llvm::None; 6363 } 6364 } 6365 6366 /// Read and return the type with the given index.. 6367 /// 6368 /// The index is the type ID, shifted and minus the number of predefs. This 6369 /// routine actually reads the record corresponding to the type at the given 6370 /// location. It is a helper routine for GetType, which deals with reading type 6371 /// IDs. 6372 QualType ASTReader::readTypeRecord(unsigned Index) { 6373 assert(ContextObj && "reading type with no AST context"); 6374 ASTContext &Context = *ContextObj; 6375 RecordLocation Loc = TypeCursorForIndex(Index); 6376 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 6377 6378 // Keep track of where we are in the stream, then jump back there 6379 // after reading this type. 6380 SavedStreamPosition SavedPosition(DeclsCursor); 6381 6382 ReadingKindTracker ReadingKind(Read_Type, *this); 6383 6384 // Note that we are loading a type record. 6385 Deserializing AType(this); 6386 6387 if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) { 6388 Error(std::move(Err)); 6389 return QualType(); 6390 } 6391 Expected<unsigned> RawCode = DeclsCursor.ReadCode(); 6392 if (!RawCode) { 6393 Error(RawCode.takeError()); 6394 return QualType(); 6395 } 6396 6397 ASTRecordReader Record(*this, *Loc.F); 6398 Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get()); 6399 if (!Code) { 6400 Error(Code.takeError()); 6401 return QualType(); 6402 } 6403 if (Code.get() == TYPE_EXT_QUAL) { 6404 QualType baseType = Record.readQualType(); 6405 Qualifiers quals = Record.readQualifiers(); 6406 return Context.getQualifiedType(baseType, quals); 6407 } 6408 6409 auto maybeClass = getTypeClassForCode((TypeCode) Code.get()); 6410 if (!maybeClass) { 6411 Error("Unexpected code for type"); 6412 return QualType(); 6413 } 6414 6415 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record); 6416 return TypeReader.read(*maybeClass); 6417 } 6418 6419 namespace clang { 6420 6421 class TypeLocReader : public TypeLocVisitor<TypeLocReader> { 6422 ASTRecordReader &Reader; 6423 6424 SourceLocation readSourceLocation() { 6425 return Reader.readSourceLocation(); 6426 } 6427 6428 TypeSourceInfo *GetTypeSourceInfo() { 6429 return Reader.readTypeSourceInfo(); 6430 } 6431 6432 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() { 6433 return Reader.readNestedNameSpecifierLoc(); 6434 } 6435 6436 Attr *ReadAttr() { 6437 return Reader.readAttr(); 6438 } 6439 6440 public: 6441 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {} 6442 6443 // We want compile-time assurance that we've enumerated all of 6444 // these, so unfortunately we have to declare them first, then 6445 // define them out-of-line. 6446 #define ABSTRACT_TYPELOC(CLASS, PARENT) 6447 #define TYPELOC(CLASS, PARENT) \ 6448 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 6449 #include "clang/AST/TypeLocNodes.def" 6450 6451 void VisitFunctionTypeLoc(FunctionTypeLoc); 6452 void VisitArrayTypeLoc(ArrayTypeLoc); 6453 }; 6454 6455 } // namespace clang 6456 6457 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 6458 // nothing to do 6459 } 6460 6461 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 6462 TL.setBuiltinLoc(readSourceLocation()); 6463 if (TL.needsExtraLocalData()) { 6464 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt())); 6465 TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt())); 6466 TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt())); 6467 TL.setModeAttr(Reader.readInt()); 6468 } 6469 } 6470 6471 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { 6472 TL.setNameLoc(readSourceLocation()); 6473 } 6474 6475 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { 6476 TL.setStarLoc(readSourceLocation()); 6477 } 6478 6479 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) { 6480 // nothing to do 6481 } 6482 6483 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { 6484 // nothing to do 6485 } 6486 6487 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) { 6488 TL.setExpansionLoc(readSourceLocation()); 6489 } 6490 6491 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 6492 TL.setCaretLoc(readSourceLocation()); 6493 } 6494 6495 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 6496 TL.setAmpLoc(readSourceLocation()); 6497 } 6498 6499 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 6500 TL.setAmpAmpLoc(readSourceLocation()); 6501 } 6502 6503 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 6504 TL.setStarLoc(readSourceLocation()); 6505 TL.setClassTInfo(GetTypeSourceInfo()); 6506 } 6507 6508 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { 6509 TL.setLBracketLoc(readSourceLocation()); 6510 TL.setRBracketLoc(readSourceLocation()); 6511 if (Reader.readBool()) 6512 TL.setSizeExpr(Reader.readExpr()); 6513 else 6514 TL.setSizeExpr(nullptr); 6515 } 6516 6517 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 6518 VisitArrayTypeLoc(TL); 6519 } 6520 6521 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 6522 VisitArrayTypeLoc(TL); 6523 } 6524 6525 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 6526 VisitArrayTypeLoc(TL); 6527 } 6528 6529 void TypeLocReader::VisitDependentSizedArrayTypeLoc( 6530 DependentSizedArrayTypeLoc TL) { 6531 VisitArrayTypeLoc(TL); 6532 } 6533 6534 void TypeLocReader::VisitDependentAddressSpaceTypeLoc( 6535 DependentAddressSpaceTypeLoc TL) { 6536 6537 TL.setAttrNameLoc(readSourceLocation()); 6538 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6539 TL.setAttrExprOperand(Reader.readExpr()); 6540 } 6541 6542 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( 6543 DependentSizedExtVectorTypeLoc TL) { 6544 TL.setNameLoc(readSourceLocation()); 6545 } 6546 6547 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { 6548 TL.setNameLoc(readSourceLocation()); 6549 } 6550 6551 void TypeLocReader::VisitDependentVectorTypeLoc( 6552 DependentVectorTypeLoc TL) { 6553 TL.setNameLoc(readSourceLocation()); 6554 } 6555 6556 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 6557 TL.setNameLoc(readSourceLocation()); 6558 } 6559 6560 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) { 6561 TL.setAttrNameLoc(readSourceLocation()); 6562 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6563 TL.setAttrRowOperand(Reader.readExpr()); 6564 TL.setAttrColumnOperand(Reader.readExpr()); 6565 } 6566 6567 void TypeLocReader::VisitDependentSizedMatrixTypeLoc( 6568 DependentSizedMatrixTypeLoc TL) { 6569 TL.setAttrNameLoc(readSourceLocation()); 6570 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6571 TL.setAttrRowOperand(Reader.readExpr()); 6572 TL.setAttrColumnOperand(Reader.readExpr()); 6573 } 6574 6575 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 6576 TL.setLocalRangeBegin(readSourceLocation()); 6577 TL.setLParenLoc(readSourceLocation()); 6578 TL.setRParenLoc(readSourceLocation()); 6579 TL.setExceptionSpecRange(Reader.readSourceRange()); 6580 TL.setLocalRangeEnd(readSourceLocation()); 6581 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) { 6582 TL.setParam(i, Reader.readDeclAs<ParmVarDecl>()); 6583 } 6584 } 6585 6586 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 6587 VisitFunctionTypeLoc(TL); 6588 } 6589 6590 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 6591 VisitFunctionTypeLoc(TL); 6592 } 6593 6594 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 6595 TL.setNameLoc(readSourceLocation()); 6596 } 6597 6598 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 6599 TL.setNameLoc(readSourceLocation()); 6600 } 6601 6602 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 6603 TL.setTypeofLoc(readSourceLocation()); 6604 TL.setLParenLoc(readSourceLocation()); 6605 TL.setRParenLoc(readSourceLocation()); 6606 } 6607 6608 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 6609 TL.setTypeofLoc(readSourceLocation()); 6610 TL.setLParenLoc(readSourceLocation()); 6611 TL.setRParenLoc(readSourceLocation()); 6612 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6613 } 6614 6615 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 6616 TL.setNameLoc(readSourceLocation()); 6617 } 6618 6619 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 6620 TL.setKWLoc(readSourceLocation()); 6621 TL.setLParenLoc(readSourceLocation()); 6622 TL.setRParenLoc(readSourceLocation()); 6623 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6624 } 6625 6626 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) { 6627 TL.setNameLoc(readSourceLocation()); 6628 if (Reader.readBool()) { 6629 TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc()); 6630 TL.setTemplateKWLoc(readSourceLocation()); 6631 TL.setConceptNameLoc(readSourceLocation()); 6632 TL.setFoundDecl(Reader.readDeclAs<NamedDecl>()); 6633 TL.setLAngleLoc(readSourceLocation()); 6634 TL.setRAngleLoc(readSourceLocation()); 6635 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6636 TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo( 6637 TL.getTypePtr()->getArg(i).getKind())); 6638 } 6639 } 6640 6641 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc( 6642 DeducedTemplateSpecializationTypeLoc TL) { 6643 TL.setTemplateNameLoc(readSourceLocation()); 6644 } 6645 6646 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { 6647 TL.setNameLoc(readSourceLocation()); 6648 } 6649 6650 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { 6651 TL.setNameLoc(readSourceLocation()); 6652 } 6653 6654 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 6655 TL.setAttr(ReadAttr()); 6656 } 6657 6658 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 6659 TL.setNameLoc(readSourceLocation()); 6660 } 6661 6662 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( 6663 SubstTemplateTypeParmTypeLoc TL) { 6664 TL.setNameLoc(readSourceLocation()); 6665 } 6666 6667 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc( 6668 SubstTemplateTypeParmPackTypeLoc TL) { 6669 TL.setNameLoc(readSourceLocation()); 6670 } 6671 6672 void TypeLocReader::VisitTemplateSpecializationTypeLoc( 6673 TemplateSpecializationTypeLoc TL) { 6674 TL.setTemplateKeywordLoc(readSourceLocation()); 6675 TL.setTemplateNameLoc(readSourceLocation()); 6676 TL.setLAngleLoc(readSourceLocation()); 6677 TL.setRAngleLoc(readSourceLocation()); 6678 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6679 TL.setArgLocInfo( 6680 i, 6681 Reader.readTemplateArgumentLocInfo( 6682 TL.getTypePtr()->getArg(i).getKind())); 6683 } 6684 6685 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) { 6686 TL.setLParenLoc(readSourceLocation()); 6687 TL.setRParenLoc(readSourceLocation()); 6688 } 6689 6690 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 6691 TL.setElaboratedKeywordLoc(readSourceLocation()); 6692 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6693 } 6694 6695 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 6696 TL.setNameLoc(readSourceLocation()); 6697 } 6698 6699 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 6700 TL.setElaboratedKeywordLoc(readSourceLocation()); 6701 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6702 TL.setNameLoc(readSourceLocation()); 6703 } 6704 6705 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( 6706 DependentTemplateSpecializationTypeLoc TL) { 6707 TL.setElaboratedKeywordLoc(readSourceLocation()); 6708 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6709 TL.setTemplateKeywordLoc(readSourceLocation()); 6710 TL.setTemplateNameLoc(readSourceLocation()); 6711 TL.setLAngleLoc(readSourceLocation()); 6712 TL.setRAngleLoc(readSourceLocation()); 6713 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 6714 TL.setArgLocInfo( 6715 I, 6716 Reader.readTemplateArgumentLocInfo( 6717 TL.getTypePtr()->getArg(I).getKind())); 6718 } 6719 6720 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 6721 TL.setEllipsisLoc(readSourceLocation()); 6722 } 6723 6724 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 6725 TL.setNameLoc(readSourceLocation()); 6726 } 6727 6728 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) { 6729 if (TL.getNumProtocols()) { 6730 TL.setProtocolLAngleLoc(readSourceLocation()); 6731 TL.setProtocolRAngleLoc(readSourceLocation()); 6732 } 6733 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6734 TL.setProtocolLoc(i, readSourceLocation()); 6735 } 6736 6737 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 6738 TL.setHasBaseTypeAsWritten(Reader.readBool()); 6739 TL.setTypeArgsLAngleLoc(readSourceLocation()); 6740 TL.setTypeArgsRAngleLoc(readSourceLocation()); 6741 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i) 6742 TL.setTypeArgTInfo(i, GetTypeSourceInfo()); 6743 TL.setProtocolLAngleLoc(readSourceLocation()); 6744 TL.setProtocolRAngleLoc(readSourceLocation()); 6745 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6746 TL.setProtocolLoc(i, readSourceLocation()); 6747 } 6748 6749 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 6750 TL.setStarLoc(readSourceLocation()); 6751 } 6752 6753 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) { 6754 TL.setKWLoc(readSourceLocation()); 6755 TL.setLParenLoc(readSourceLocation()); 6756 TL.setRParenLoc(readSourceLocation()); 6757 } 6758 6759 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) { 6760 TL.setKWLoc(readSourceLocation()); 6761 } 6762 6763 void TypeLocReader::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) { 6764 TL.setNameLoc(readSourceLocation()); 6765 } 6766 void TypeLocReader::VisitDependentExtIntTypeLoc( 6767 clang::DependentExtIntTypeLoc TL) { 6768 TL.setNameLoc(readSourceLocation()); 6769 } 6770 6771 6772 void ASTRecordReader::readTypeLoc(TypeLoc TL) { 6773 TypeLocReader TLR(*this); 6774 for (; !TL.isNull(); TL = TL.getNextTypeLoc()) 6775 TLR.Visit(TL); 6776 } 6777 6778 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() { 6779 QualType InfoTy = readType(); 6780 if (InfoTy.isNull()) 6781 return nullptr; 6782 6783 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy); 6784 readTypeLoc(TInfo->getTypeLoc()); 6785 return TInfo; 6786 } 6787 6788 QualType ASTReader::GetType(TypeID ID) { 6789 assert(ContextObj && "reading type with no AST context"); 6790 ASTContext &Context = *ContextObj; 6791 6792 unsigned FastQuals = ID & Qualifiers::FastMask; 6793 unsigned Index = ID >> Qualifiers::FastWidth; 6794 6795 if (Index < NUM_PREDEF_TYPE_IDS) { 6796 QualType T; 6797 switch ((PredefinedTypeIDs)Index) { 6798 case PREDEF_TYPE_NULL_ID: 6799 return QualType(); 6800 case PREDEF_TYPE_VOID_ID: 6801 T = Context.VoidTy; 6802 break; 6803 case PREDEF_TYPE_BOOL_ID: 6804 T = Context.BoolTy; 6805 break; 6806 case PREDEF_TYPE_CHAR_U_ID: 6807 case PREDEF_TYPE_CHAR_S_ID: 6808 // FIXME: Check that the signedness of CharTy is correct! 6809 T = Context.CharTy; 6810 break; 6811 case PREDEF_TYPE_UCHAR_ID: 6812 T = Context.UnsignedCharTy; 6813 break; 6814 case PREDEF_TYPE_USHORT_ID: 6815 T = Context.UnsignedShortTy; 6816 break; 6817 case PREDEF_TYPE_UINT_ID: 6818 T = Context.UnsignedIntTy; 6819 break; 6820 case PREDEF_TYPE_ULONG_ID: 6821 T = Context.UnsignedLongTy; 6822 break; 6823 case PREDEF_TYPE_ULONGLONG_ID: 6824 T = Context.UnsignedLongLongTy; 6825 break; 6826 case PREDEF_TYPE_UINT128_ID: 6827 T = Context.UnsignedInt128Ty; 6828 break; 6829 case PREDEF_TYPE_SCHAR_ID: 6830 T = Context.SignedCharTy; 6831 break; 6832 case PREDEF_TYPE_WCHAR_ID: 6833 T = Context.WCharTy; 6834 break; 6835 case PREDEF_TYPE_SHORT_ID: 6836 T = Context.ShortTy; 6837 break; 6838 case PREDEF_TYPE_INT_ID: 6839 T = Context.IntTy; 6840 break; 6841 case PREDEF_TYPE_LONG_ID: 6842 T = Context.LongTy; 6843 break; 6844 case PREDEF_TYPE_LONGLONG_ID: 6845 T = Context.LongLongTy; 6846 break; 6847 case PREDEF_TYPE_INT128_ID: 6848 T = Context.Int128Ty; 6849 break; 6850 case PREDEF_TYPE_BFLOAT16_ID: 6851 T = Context.BFloat16Ty; 6852 break; 6853 case PREDEF_TYPE_HALF_ID: 6854 T = Context.HalfTy; 6855 break; 6856 case PREDEF_TYPE_FLOAT_ID: 6857 T = Context.FloatTy; 6858 break; 6859 case PREDEF_TYPE_DOUBLE_ID: 6860 T = Context.DoubleTy; 6861 break; 6862 case PREDEF_TYPE_LONGDOUBLE_ID: 6863 T = Context.LongDoubleTy; 6864 break; 6865 case PREDEF_TYPE_SHORT_ACCUM_ID: 6866 T = Context.ShortAccumTy; 6867 break; 6868 case PREDEF_TYPE_ACCUM_ID: 6869 T = Context.AccumTy; 6870 break; 6871 case PREDEF_TYPE_LONG_ACCUM_ID: 6872 T = Context.LongAccumTy; 6873 break; 6874 case PREDEF_TYPE_USHORT_ACCUM_ID: 6875 T = Context.UnsignedShortAccumTy; 6876 break; 6877 case PREDEF_TYPE_UACCUM_ID: 6878 T = Context.UnsignedAccumTy; 6879 break; 6880 case PREDEF_TYPE_ULONG_ACCUM_ID: 6881 T = Context.UnsignedLongAccumTy; 6882 break; 6883 case PREDEF_TYPE_SHORT_FRACT_ID: 6884 T = Context.ShortFractTy; 6885 break; 6886 case PREDEF_TYPE_FRACT_ID: 6887 T = Context.FractTy; 6888 break; 6889 case PREDEF_TYPE_LONG_FRACT_ID: 6890 T = Context.LongFractTy; 6891 break; 6892 case PREDEF_TYPE_USHORT_FRACT_ID: 6893 T = Context.UnsignedShortFractTy; 6894 break; 6895 case PREDEF_TYPE_UFRACT_ID: 6896 T = Context.UnsignedFractTy; 6897 break; 6898 case PREDEF_TYPE_ULONG_FRACT_ID: 6899 T = Context.UnsignedLongFractTy; 6900 break; 6901 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID: 6902 T = Context.SatShortAccumTy; 6903 break; 6904 case PREDEF_TYPE_SAT_ACCUM_ID: 6905 T = Context.SatAccumTy; 6906 break; 6907 case PREDEF_TYPE_SAT_LONG_ACCUM_ID: 6908 T = Context.SatLongAccumTy; 6909 break; 6910 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID: 6911 T = Context.SatUnsignedShortAccumTy; 6912 break; 6913 case PREDEF_TYPE_SAT_UACCUM_ID: 6914 T = Context.SatUnsignedAccumTy; 6915 break; 6916 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID: 6917 T = Context.SatUnsignedLongAccumTy; 6918 break; 6919 case PREDEF_TYPE_SAT_SHORT_FRACT_ID: 6920 T = Context.SatShortFractTy; 6921 break; 6922 case PREDEF_TYPE_SAT_FRACT_ID: 6923 T = Context.SatFractTy; 6924 break; 6925 case PREDEF_TYPE_SAT_LONG_FRACT_ID: 6926 T = Context.SatLongFractTy; 6927 break; 6928 case PREDEF_TYPE_SAT_USHORT_FRACT_ID: 6929 T = Context.SatUnsignedShortFractTy; 6930 break; 6931 case PREDEF_TYPE_SAT_UFRACT_ID: 6932 T = Context.SatUnsignedFractTy; 6933 break; 6934 case PREDEF_TYPE_SAT_ULONG_FRACT_ID: 6935 T = Context.SatUnsignedLongFractTy; 6936 break; 6937 case PREDEF_TYPE_FLOAT16_ID: 6938 T = Context.Float16Ty; 6939 break; 6940 case PREDEF_TYPE_FLOAT128_ID: 6941 T = Context.Float128Ty; 6942 break; 6943 case PREDEF_TYPE_IBM128_ID: 6944 T = Context.Ibm128Ty; 6945 break; 6946 case PREDEF_TYPE_OVERLOAD_ID: 6947 T = Context.OverloadTy; 6948 break; 6949 case PREDEF_TYPE_BOUND_MEMBER: 6950 T = Context.BoundMemberTy; 6951 break; 6952 case PREDEF_TYPE_PSEUDO_OBJECT: 6953 T = Context.PseudoObjectTy; 6954 break; 6955 case PREDEF_TYPE_DEPENDENT_ID: 6956 T = Context.DependentTy; 6957 break; 6958 case PREDEF_TYPE_UNKNOWN_ANY: 6959 T = Context.UnknownAnyTy; 6960 break; 6961 case PREDEF_TYPE_NULLPTR_ID: 6962 T = Context.NullPtrTy; 6963 break; 6964 case PREDEF_TYPE_CHAR8_ID: 6965 T = Context.Char8Ty; 6966 break; 6967 case PREDEF_TYPE_CHAR16_ID: 6968 T = Context.Char16Ty; 6969 break; 6970 case PREDEF_TYPE_CHAR32_ID: 6971 T = Context.Char32Ty; 6972 break; 6973 case PREDEF_TYPE_OBJC_ID: 6974 T = Context.ObjCBuiltinIdTy; 6975 break; 6976 case PREDEF_TYPE_OBJC_CLASS: 6977 T = Context.ObjCBuiltinClassTy; 6978 break; 6979 case PREDEF_TYPE_OBJC_SEL: 6980 T = Context.ObjCBuiltinSelTy; 6981 break; 6982 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 6983 case PREDEF_TYPE_##Id##_ID: \ 6984 T = Context.SingletonId; \ 6985 break; 6986 #include "clang/Basic/OpenCLImageTypes.def" 6987 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ 6988 case PREDEF_TYPE_##Id##_ID: \ 6989 T = Context.Id##Ty; \ 6990 break; 6991 #include "clang/Basic/OpenCLExtensionTypes.def" 6992 case PREDEF_TYPE_SAMPLER_ID: 6993 T = Context.OCLSamplerTy; 6994 break; 6995 case PREDEF_TYPE_EVENT_ID: 6996 T = Context.OCLEventTy; 6997 break; 6998 case PREDEF_TYPE_CLK_EVENT_ID: 6999 T = Context.OCLClkEventTy; 7000 break; 7001 case PREDEF_TYPE_QUEUE_ID: 7002 T = Context.OCLQueueTy; 7003 break; 7004 case PREDEF_TYPE_RESERVE_ID_ID: 7005 T = Context.OCLReserveIDTy; 7006 break; 7007 case PREDEF_TYPE_AUTO_DEDUCT: 7008 T = Context.getAutoDeductType(); 7009 break; 7010 case PREDEF_TYPE_AUTO_RREF_DEDUCT: 7011 T = Context.getAutoRRefDeductType(); 7012 break; 7013 case PREDEF_TYPE_ARC_UNBRIDGED_CAST: 7014 T = Context.ARCUnbridgedCastTy; 7015 break; 7016 case PREDEF_TYPE_BUILTIN_FN: 7017 T = Context.BuiltinFnTy; 7018 break; 7019 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX: 7020 T = Context.IncompleteMatrixIdxTy; 7021 break; 7022 case PREDEF_TYPE_OMP_ARRAY_SECTION: 7023 T = Context.OMPArraySectionTy; 7024 break; 7025 case PREDEF_TYPE_OMP_ARRAY_SHAPING: 7026 T = Context.OMPArraySectionTy; 7027 break; 7028 case PREDEF_TYPE_OMP_ITERATOR: 7029 T = Context.OMPIteratorTy; 7030 break; 7031 #define SVE_TYPE(Name, Id, SingletonId) \ 7032 case PREDEF_TYPE_##Id##_ID: \ 7033 T = Context.SingletonId; \ 7034 break; 7035 #include "clang/Basic/AArch64SVEACLETypes.def" 7036 #define PPC_VECTOR_TYPE(Name, Id, Size) \ 7037 case PREDEF_TYPE_##Id##_ID: \ 7038 T = Context.Id##Ty; \ 7039 break; 7040 #include "clang/Basic/PPCTypes.def" 7041 #define RVV_TYPE(Name, Id, SingletonId) \ 7042 case PREDEF_TYPE_##Id##_ID: \ 7043 T = Context.SingletonId; \ 7044 break; 7045 #include "clang/Basic/RISCVVTypes.def" 7046 } 7047 7048 assert(!T.isNull() && "Unknown predefined type"); 7049 return T.withFastQualifiers(FastQuals); 7050 } 7051 7052 Index -= NUM_PREDEF_TYPE_IDS; 7053 assert(Index < TypesLoaded.size() && "Type index out-of-range"); 7054 if (TypesLoaded[Index].isNull()) { 7055 TypesLoaded[Index] = readTypeRecord(Index); 7056 if (TypesLoaded[Index].isNull()) 7057 return QualType(); 7058 7059 TypesLoaded[Index]->setFromAST(); 7060 if (DeserializationListener) 7061 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID), 7062 TypesLoaded[Index]); 7063 } 7064 7065 return TypesLoaded[Index].withFastQualifiers(FastQuals); 7066 } 7067 7068 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) { 7069 return GetType(getGlobalTypeID(F, LocalID)); 7070 } 7071 7072 serialization::TypeID 7073 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const { 7074 unsigned FastQuals = LocalID & Qualifiers::FastMask; 7075 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth; 7076 7077 if (LocalIndex < NUM_PREDEF_TYPE_IDS) 7078 return LocalID; 7079 7080 if (!F.ModuleOffsetMap.empty()) 7081 ReadModuleOffsetMap(F); 7082 7083 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7084 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS); 7085 assert(I != F.TypeRemap.end() && "Invalid index into type index remap"); 7086 7087 unsigned GlobalIndex = LocalIndex + I->second; 7088 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals; 7089 } 7090 7091 TemplateArgumentLocInfo 7092 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) { 7093 switch (Kind) { 7094 case TemplateArgument::Expression: 7095 return readExpr(); 7096 case TemplateArgument::Type: 7097 return readTypeSourceInfo(); 7098 case TemplateArgument::Template: { 7099 NestedNameSpecifierLoc QualifierLoc = 7100 readNestedNameSpecifierLoc(); 7101 SourceLocation TemplateNameLoc = readSourceLocation(); 7102 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc, 7103 TemplateNameLoc, SourceLocation()); 7104 } 7105 case TemplateArgument::TemplateExpansion: { 7106 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc(); 7107 SourceLocation TemplateNameLoc = readSourceLocation(); 7108 SourceLocation EllipsisLoc = readSourceLocation(); 7109 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc, 7110 TemplateNameLoc, EllipsisLoc); 7111 } 7112 case TemplateArgument::Null: 7113 case TemplateArgument::Integral: 7114 case TemplateArgument::Declaration: 7115 case TemplateArgument::NullPtr: 7116 case TemplateArgument::Pack: 7117 // FIXME: Is this right? 7118 return TemplateArgumentLocInfo(); 7119 } 7120 llvm_unreachable("unexpected template argument loc"); 7121 } 7122 7123 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() { 7124 TemplateArgument Arg = readTemplateArgument(); 7125 7126 if (Arg.getKind() == TemplateArgument::Expression) { 7127 if (readBool()) // bool InfoHasSameExpr. 7128 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); 7129 } 7130 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind())); 7131 } 7132 7133 const ASTTemplateArgumentListInfo * 7134 ASTRecordReader::readASTTemplateArgumentListInfo() { 7135 SourceLocation LAngleLoc = readSourceLocation(); 7136 SourceLocation RAngleLoc = readSourceLocation(); 7137 unsigned NumArgsAsWritten = readInt(); 7138 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc); 7139 for (unsigned i = 0; i != NumArgsAsWritten; ++i) 7140 TemplArgsInfo.addArgument(readTemplateArgumentLoc()); 7141 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo); 7142 } 7143 7144 Decl *ASTReader::GetExternalDecl(uint32_t ID) { 7145 return GetDecl(ID); 7146 } 7147 7148 void ASTReader::CompleteRedeclChain(const Decl *D) { 7149 if (NumCurrentElementsDeserializing) { 7150 // We arrange to not care about the complete redeclaration chain while we're 7151 // deserializing. Just remember that the AST has marked this one as complete 7152 // but that it's not actually complete yet, so we know we still need to 7153 // complete it later. 7154 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D)); 7155 return; 7156 } 7157 7158 if (!D->getDeclContext()) { 7159 assert(isa<TranslationUnitDecl>(D) && "Not a TU?"); 7160 return; 7161 } 7162 7163 const DeclContext *DC = D->getDeclContext()->getRedeclContext(); 7164 7165 // If this is a named declaration, complete it by looking it up 7166 // within its context. 7167 // 7168 // FIXME: Merging a function definition should merge 7169 // all mergeable entities within it. 7170 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) || 7171 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) { 7172 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) { 7173 if (!getContext().getLangOpts().CPlusPlus && 7174 isa<TranslationUnitDecl>(DC)) { 7175 // Outside of C++, we don't have a lookup table for the TU, so update 7176 // the identifier instead. (For C++ modules, we don't store decls 7177 // in the serialized identifier table, so we do the lookup in the TU.) 7178 auto *II = Name.getAsIdentifierInfo(); 7179 assert(II && "non-identifier name in C?"); 7180 if (II->isOutOfDate()) 7181 updateOutOfDateIdentifier(*II); 7182 } else 7183 DC->lookup(Name); 7184 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) { 7185 // Find all declarations of this kind from the relevant context. 7186 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) { 7187 auto *DC = cast<DeclContext>(DCDecl); 7188 SmallVector<Decl*, 8> Decls; 7189 FindExternalLexicalDecls( 7190 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls); 7191 } 7192 } 7193 } 7194 7195 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) 7196 CTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7197 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) 7198 VTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7199 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 7200 if (auto *Template = FD->getPrimaryTemplate()) 7201 Template->LoadLazySpecializations(); 7202 } 7203 } 7204 7205 CXXCtorInitializer ** 7206 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) { 7207 RecordLocation Loc = getLocalBitOffset(Offset); 7208 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7209 SavedStreamPosition SavedPosition(Cursor); 7210 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7211 Error(std::move(Err)); 7212 return nullptr; 7213 } 7214 ReadingKindTracker ReadingKind(Read_Decl, *this); 7215 7216 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7217 if (!MaybeCode) { 7218 Error(MaybeCode.takeError()); 7219 return nullptr; 7220 } 7221 unsigned Code = MaybeCode.get(); 7222 7223 ASTRecordReader Record(*this, *Loc.F); 7224 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7225 if (!MaybeRecCode) { 7226 Error(MaybeRecCode.takeError()); 7227 return nullptr; 7228 } 7229 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) { 7230 Error("malformed AST file: missing C++ ctor initializers"); 7231 return nullptr; 7232 } 7233 7234 return Record.readCXXCtorInitializers(); 7235 } 7236 7237 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) { 7238 assert(ContextObj && "reading base specifiers with no AST context"); 7239 ASTContext &Context = *ContextObj; 7240 7241 RecordLocation Loc = getLocalBitOffset(Offset); 7242 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7243 SavedStreamPosition SavedPosition(Cursor); 7244 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7245 Error(std::move(Err)); 7246 return nullptr; 7247 } 7248 ReadingKindTracker ReadingKind(Read_Decl, *this); 7249 7250 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7251 if (!MaybeCode) { 7252 Error(MaybeCode.takeError()); 7253 return nullptr; 7254 } 7255 unsigned Code = MaybeCode.get(); 7256 7257 ASTRecordReader Record(*this, *Loc.F); 7258 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7259 if (!MaybeRecCode) { 7260 Error(MaybeCode.takeError()); 7261 return nullptr; 7262 } 7263 unsigned RecCode = MaybeRecCode.get(); 7264 7265 if (RecCode != DECL_CXX_BASE_SPECIFIERS) { 7266 Error("malformed AST file: missing C++ base specifiers"); 7267 return nullptr; 7268 } 7269 7270 unsigned NumBases = Record.readInt(); 7271 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases); 7272 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases]; 7273 for (unsigned I = 0; I != NumBases; ++I) 7274 Bases[I] = Record.readCXXBaseSpecifier(); 7275 return Bases; 7276 } 7277 7278 serialization::DeclID 7279 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const { 7280 if (LocalID < NUM_PREDEF_DECL_IDS) 7281 return LocalID; 7282 7283 if (!F.ModuleOffsetMap.empty()) 7284 ReadModuleOffsetMap(F); 7285 7286 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7287 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS); 7288 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap"); 7289 7290 return LocalID + I->second; 7291 } 7292 7293 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID, 7294 ModuleFile &M) const { 7295 // Predefined decls aren't from any module. 7296 if (ID < NUM_PREDEF_DECL_IDS) 7297 return false; 7298 7299 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID && 7300 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls; 7301 } 7302 7303 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) { 7304 if (!D->isFromASTFile()) 7305 return nullptr; 7306 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID()); 7307 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7308 return I->second; 7309 } 7310 7311 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) { 7312 if (ID < NUM_PREDEF_DECL_IDS) 7313 return SourceLocation(); 7314 7315 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7316 7317 if (Index > DeclsLoaded.size()) { 7318 Error("declaration ID out-of-range for AST file"); 7319 return SourceLocation(); 7320 } 7321 7322 if (Decl *D = DeclsLoaded[Index]) 7323 return D->getLocation(); 7324 7325 SourceLocation Loc; 7326 DeclCursorForID(ID, Loc); 7327 return Loc; 7328 } 7329 7330 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) { 7331 switch (ID) { 7332 case PREDEF_DECL_NULL_ID: 7333 return nullptr; 7334 7335 case PREDEF_DECL_TRANSLATION_UNIT_ID: 7336 return Context.getTranslationUnitDecl(); 7337 7338 case PREDEF_DECL_OBJC_ID_ID: 7339 return Context.getObjCIdDecl(); 7340 7341 case PREDEF_DECL_OBJC_SEL_ID: 7342 return Context.getObjCSelDecl(); 7343 7344 case PREDEF_DECL_OBJC_CLASS_ID: 7345 return Context.getObjCClassDecl(); 7346 7347 case PREDEF_DECL_OBJC_PROTOCOL_ID: 7348 return Context.getObjCProtocolDecl(); 7349 7350 case PREDEF_DECL_INT_128_ID: 7351 return Context.getInt128Decl(); 7352 7353 case PREDEF_DECL_UNSIGNED_INT_128_ID: 7354 return Context.getUInt128Decl(); 7355 7356 case PREDEF_DECL_OBJC_INSTANCETYPE_ID: 7357 return Context.getObjCInstanceTypeDecl(); 7358 7359 case PREDEF_DECL_BUILTIN_VA_LIST_ID: 7360 return Context.getBuiltinVaListDecl(); 7361 7362 case PREDEF_DECL_VA_LIST_TAG: 7363 return Context.getVaListTagDecl(); 7364 7365 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID: 7366 return Context.getBuiltinMSVaListDecl(); 7367 7368 case PREDEF_DECL_BUILTIN_MS_GUID_ID: 7369 return Context.getMSGuidTagDecl(); 7370 7371 case PREDEF_DECL_EXTERN_C_CONTEXT_ID: 7372 return Context.getExternCContextDecl(); 7373 7374 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID: 7375 return Context.getMakeIntegerSeqDecl(); 7376 7377 case PREDEF_DECL_CF_CONSTANT_STRING_ID: 7378 return Context.getCFConstantStringDecl(); 7379 7380 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID: 7381 return Context.getCFConstantStringTagDecl(); 7382 7383 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID: 7384 return Context.getTypePackElementDecl(); 7385 } 7386 llvm_unreachable("PredefinedDeclIDs unknown enum value"); 7387 } 7388 7389 Decl *ASTReader::GetExistingDecl(DeclID ID) { 7390 assert(ContextObj && "reading decl with no AST context"); 7391 if (ID < NUM_PREDEF_DECL_IDS) { 7392 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID); 7393 if (D) { 7394 // Track that we have merged the declaration with ID \p ID into the 7395 // pre-existing predefined declaration \p D. 7396 auto &Merged = KeyDecls[D->getCanonicalDecl()]; 7397 if (Merged.empty()) 7398 Merged.push_back(ID); 7399 } 7400 return D; 7401 } 7402 7403 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7404 7405 if (Index >= DeclsLoaded.size()) { 7406 assert(0 && "declaration ID out-of-range for AST file"); 7407 Error("declaration ID out-of-range for AST file"); 7408 return nullptr; 7409 } 7410 7411 return DeclsLoaded[Index]; 7412 } 7413 7414 Decl *ASTReader::GetDecl(DeclID ID) { 7415 if (ID < NUM_PREDEF_DECL_IDS) 7416 return GetExistingDecl(ID); 7417 7418 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7419 7420 if (Index >= DeclsLoaded.size()) { 7421 assert(0 && "declaration ID out-of-range for AST file"); 7422 Error("declaration ID out-of-range for AST file"); 7423 return nullptr; 7424 } 7425 7426 if (!DeclsLoaded[Index]) { 7427 ReadDeclRecord(ID); 7428 if (DeserializationListener) 7429 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); 7430 } 7431 7432 return DeclsLoaded[Index]; 7433 } 7434 7435 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M, 7436 DeclID GlobalID) { 7437 if (GlobalID < NUM_PREDEF_DECL_IDS) 7438 return GlobalID; 7439 7440 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID); 7441 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7442 ModuleFile *Owner = I->second; 7443 7444 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos 7445 = M.GlobalToLocalDeclIDs.find(Owner); 7446 if (Pos == M.GlobalToLocalDeclIDs.end()) 7447 return 0; 7448 7449 return GlobalID - Owner->BaseDeclID + Pos->second; 7450 } 7451 7452 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F, 7453 const RecordData &Record, 7454 unsigned &Idx) { 7455 if (Idx >= Record.size()) { 7456 Error("Corrupted AST file"); 7457 return 0; 7458 } 7459 7460 return getGlobalDeclID(F, Record[Idx++]); 7461 } 7462 7463 /// Resolve the offset of a statement into a statement. 7464 /// 7465 /// This operation will read a new statement from the external 7466 /// source each time it is called, and is meant to be used via a 7467 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 7468 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) { 7469 // Switch case IDs are per Decl. 7470 ClearSwitchCaseIDs(); 7471 7472 // Offset here is a global offset across the entire chain. 7473 RecordLocation Loc = getLocalBitOffset(Offset); 7474 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) { 7475 Error(std::move(Err)); 7476 return nullptr; 7477 } 7478 assert(NumCurrentElementsDeserializing == 0 && 7479 "should not be called while already deserializing"); 7480 Deserializing D(this); 7481 return ReadStmtFromStream(*Loc.F); 7482 } 7483 7484 void ASTReader::FindExternalLexicalDecls( 7485 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant, 7486 SmallVectorImpl<Decl *> &Decls) { 7487 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {}; 7488 7489 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) { 7490 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries"); 7491 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) { 7492 auto K = (Decl::Kind)+LexicalDecls[I]; 7493 if (!IsKindWeWant(K)) 7494 continue; 7495 7496 auto ID = (serialization::DeclID)+LexicalDecls[I + 1]; 7497 7498 // Don't add predefined declarations to the lexical context more 7499 // than once. 7500 if (ID < NUM_PREDEF_DECL_IDS) { 7501 if (PredefsVisited[ID]) 7502 continue; 7503 7504 PredefsVisited[ID] = true; 7505 } 7506 7507 if (Decl *D = GetLocalDecl(*M, ID)) { 7508 assert(D->getKind() == K && "wrong kind for lexical decl"); 7509 if (!DC->isDeclInLexicalTraversal(D)) 7510 Decls.push_back(D); 7511 } 7512 } 7513 }; 7514 7515 if (isa<TranslationUnitDecl>(DC)) { 7516 for (auto Lexical : TULexicalDecls) 7517 Visit(Lexical.first, Lexical.second); 7518 } else { 7519 auto I = LexicalDecls.find(DC); 7520 if (I != LexicalDecls.end()) 7521 Visit(I->second.first, I->second.second); 7522 } 7523 7524 ++NumLexicalDeclContextsRead; 7525 } 7526 7527 namespace { 7528 7529 class DeclIDComp { 7530 ASTReader &Reader; 7531 ModuleFile &Mod; 7532 7533 public: 7534 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {} 7535 7536 bool operator()(LocalDeclID L, LocalDeclID R) const { 7537 SourceLocation LHS = getLocation(L); 7538 SourceLocation RHS = getLocation(R); 7539 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7540 } 7541 7542 bool operator()(SourceLocation LHS, LocalDeclID R) const { 7543 SourceLocation RHS = getLocation(R); 7544 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7545 } 7546 7547 bool operator()(LocalDeclID L, SourceLocation RHS) const { 7548 SourceLocation LHS = getLocation(L); 7549 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7550 } 7551 7552 SourceLocation getLocation(LocalDeclID ID) const { 7553 return Reader.getSourceManager().getFileLoc( 7554 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID))); 7555 } 7556 }; 7557 7558 } // namespace 7559 7560 void ASTReader::FindFileRegionDecls(FileID File, 7561 unsigned Offset, unsigned Length, 7562 SmallVectorImpl<Decl *> &Decls) { 7563 SourceManager &SM = getSourceManager(); 7564 7565 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File); 7566 if (I == FileDeclIDs.end()) 7567 return; 7568 7569 FileDeclsInfo &DInfo = I->second; 7570 if (DInfo.Decls.empty()) 7571 return; 7572 7573 SourceLocation 7574 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset); 7575 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length); 7576 7577 DeclIDComp DIDComp(*this, *DInfo.Mod); 7578 ArrayRef<serialization::LocalDeclID>::iterator BeginIt = 7579 llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp); 7580 if (BeginIt != DInfo.Decls.begin()) 7581 --BeginIt; 7582 7583 // If we are pointing at a top-level decl inside an objc container, we need 7584 // to backtrack until we find it otherwise we will fail to report that the 7585 // region overlaps with an objc container. 7586 while (BeginIt != DInfo.Decls.begin() && 7587 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt)) 7588 ->isTopLevelDeclInObjCContainer()) 7589 --BeginIt; 7590 7591 ArrayRef<serialization::LocalDeclID>::iterator EndIt = 7592 llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp); 7593 if (EndIt != DInfo.Decls.end()) 7594 ++EndIt; 7595 7596 for (ArrayRef<serialization::LocalDeclID>::iterator 7597 DIt = BeginIt; DIt != EndIt; ++DIt) 7598 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt))); 7599 } 7600 7601 bool 7602 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, 7603 DeclarationName Name) { 7604 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() && 7605 "DeclContext has no visible decls in storage"); 7606 if (!Name) 7607 return false; 7608 7609 auto It = Lookups.find(DC); 7610 if (It == Lookups.end()) 7611 return false; 7612 7613 Deserializing LookupResults(this); 7614 7615 // Load the list of declarations. 7616 SmallVector<NamedDecl *, 64> Decls; 7617 llvm::SmallPtrSet<NamedDecl *, 8> Found; 7618 for (DeclID ID : It->second.Table.find(Name)) { 7619 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7620 if (ND->getDeclName() == Name && Found.insert(ND).second) 7621 Decls.push_back(ND); 7622 } 7623 7624 ++NumVisibleDeclContextsRead; 7625 SetExternalVisibleDeclsForName(DC, Name, Decls); 7626 return !Decls.empty(); 7627 } 7628 7629 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) { 7630 if (!DC->hasExternalVisibleStorage()) 7631 return; 7632 7633 auto It = Lookups.find(DC); 7634 assert(It != Lookups.end() && 7635 "have external visible storage but no lookup tables"); 7636 7637 DeclsMap Decls; 7638 7639 for (DeclID ID : It->second.Table.findAll()) { 7640 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7641 Decls[ND->getDeclName()].push_back(ND); 7642 } 7643 7644 ++NumVisibleDeclContextsRead; 7645 7646 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) { 7647 SetExternalVisibleDeclsForName(DC, I->first, I->second); 7648 } 7649 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false); 7650 } 7651 7652 const serialization::reader::DeclContextLookupTable * 7653 ASTReader::getLoadedLookupTables(DeclContext *Primary) const { 7654 auto I = Lookups.find(Primary); 7655 return I == Lookups.end() ? nullptr : &I->second; 7656 } 7657 7658 /// Under non-PCH compilation the consumer receives the objc methods 7659 /// before receiving the implementation, and codegen depends on this. 7660 /// We simulate this by deserializing and passing to consumer the methods of the 7661 /// implementation before passing the deserialized implementation decl. 7662 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD, 7663 ASTConsumer *Consumer) { 7664 assert(ImplD && Consumer); 7665 7666 for (auto *I : ImplD->methods()) 7667 Consumer->HandleInterestingDecl(DeclGroupRef(I)); 7668 7669 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD)); 7670 } 7671 7672 void ASTReader::PassInterestingDeclToConsumer(Decl *D) { 7673 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) 7674 PassObjCImplDeclToConsumer(ImplD, Consumer); 7675 else 7676 Consumer->HandleInterestingDecl(DeclGroupRef(D)); 7677 } 7678 7679 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) { 7680 this->Consumer = Consumer; 7681 7682 if (Consumer) 7683 PassInterestingDeclsToConsumer(); 7684 7685 if (DeserializationListener) 7686 DeserializationListener->ReaderInitialized(this); 7687 } 7688 7689 void ASTReader::PrintStats() { 7690 std::fprintf(stderr, "*** AST File Statistics:\n"); 7691 7692 unsigned NumTypesLoaded 7693 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), 7694 QualType()); 7695 unsigned NumDeclsLoaded 7696 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), 7697 (Decl *)nullptr); 7698 unsigned NumIdentifiersLoaded 7699 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), 7700 IdentifiersLoaded.end(), 7701 (IdentifierInfo *)nullptr); 7702 unsigned NumMacrosLoaded 7703 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(), 7704 MacrosLoaded.end(), 7705 (MacroInfo *)nullptr); 7706 unsigned NumSelectorsLoaded 7707 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), 7708 SelectorsLoaded.end(), 7709 Selector()); 7710 7711 if (unsigned TotalNumSLocEntries = getTotalNumSLocs()) 7712 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", 7713 NumSLocEntriesRead, TotalNumSLocEntries, 7714 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); 7715 if (!TypesLoaded.empty()) 7716 std::fprintf(stderr, " %u/%u types read (%f%%)\n", 7717 NumTypesLoaded, (unsigned)TypesLoaded.size(), 7718 ((float)NumTypesLoaded/TypesLoaded.size() * 100)); 7719 if (!DeclsLoaded.empty()) 7720 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", 7721 NumDeclsLoaded, (unsigned)DeclsLoaded.size(), 7722 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); 7723 if (!IdentifiersLoaded.empty()) 7724 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", 7725 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), 7726 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); 7727 if (!MacrosLoaded.empty()) 7728 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7729 NumMacrosLoaded, (unsigned)MacrosLoaded.size(), 7730 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100)); 7731 if (!SelectorsLoaded.empty()) 7732 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", 7733 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), 7734 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); 7735 if (TotalNumStatements) 7736 std::fprintf(stderr, " %u/%u statements read (%f%%)\n", 7737 NumStatementsRead, TotalNumStatements, 7738 ((float)NumStatementsRead/TotalNumStatements * 100)); 7739 if (TotalNumMacros) 7740 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7741 NumMacrosRead, TotalNumMacros, 7742 ((float)NumMacrosRead/TotalNumMacros * 100)); 7743 if (TotalLexicalDeclContexts) 7744 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", 7745 NumLexicalDeclContextsRead, TotalLexicalDeclContexts, 7746 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts 7747 * 100)); 7748 if (TotalVisibleDeclContexts) 7749 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", 7750 NumVisibleDeclContextsRead, TotalVisibleDeclContexts, 7751 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts 7752 * 100)); 7753 if (TotalNumMethodPoolEntries) 7754 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", 7755 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, 7756 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries 7757 * 100)); 7758 if (NumMethodPoolLookups) 7759 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n", 7760 NumMethodPoolHits, NumMethodPoolLookups, 7761 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0)); 7762 if (NumMethodPoolTableLookups) 7763 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n", 7764 NumMethodPoolTableHits, NumMethodPoolTableLookups, 7765 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups 7766 * 100.0)); 7767 if (NumIdentifierLookupHits) 7768 std::fprintf(stderr, 7769 " %u / %u identifier table lookups succeeded (%f%%)\n", 7770 NumIdentifierLookupHits, NumIdentifierLookups, 7771 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups); 7772 7773 if (GlobalIndex) { 7774 std::fprintf(stderr, "\n"); 7775 GlobalIndex->printStats(); 7776 } 7777 7778 std::fprintf(stderr, "\n"); 7779 dump(); 7780 std::fprintf(stderr, "\n"); 7781 } 7782 7783 template<typename Key, typename ModuleFile, unsigned InitialCapacity> 7784 LLVM_DUMP_METHOD static void 7785 dumpModuleIDMap(StringRef Name, 7786 const ContinuousRangeMap<Key, ModuleFile *, 7787 InitialCapacity> &Map) { 7788 if (Map.begin() == Map.end()) 7789 return; 7790 7791 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>; 7792 7793 llvm::errs() << Name << ":\n"; 7794 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end(); 7795 I != IEnd; ++I) { 7796 llvm::errs() << " " << I->first << " -> " << I->second->FileName 7797 << "\n"; 7798 } 7799 } 7800 7801 LLVM_DUMP_METHOD void ASTReader::dump() { 7802 llvm::errs() << "*** PCH/ModuleFile Remappings:\n"; 7803 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap); 7804 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap); 7805 dumpModuleIDMap("Global type map", GlobalTypeMap); 7806 dumpModuleIDMap("Global declaration map", GlobalDeclMap); 7807 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap); 7808 dumpModuleIDMap("Global macro map", GlobalMacroMap); 7809 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap); 7810 dumpModuleIDMap("Global selector map", GlobalSelectorMap); 7811 dumpModuleIDMap("Global preprocessed entity map", 7812 GlobalPreprocessedEntityMap); 7813 7814 llvm::errs() << "\n*** PCH/Modules Loaded:"; 7815 for (ModuleFile &M : ModuleMgr) 7816 M.dump(); 7817 } 7818 7819 /// Return the amount of memory used by memory buffers, breaking down 7820 /// by heap-backed versus mmap'ed memory. 7821 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const { 7822 for (ModuleFile &I : ModuleMgr) { 7823 if (llvm::MemoryBuffer *buf = I.Buffer) { 7824 size_t bytes = buf->getBufferSize(); 7825 switch (buf->getBufferKind()) { 7826 case llvm::MemoryBuffer::MemoryBuffer_Malloc: 7827 sizes.malloc_bytes += bytes; 7828 break; 7829 case llvm::MemoryBuffer::MemoryBuffer_MMap: 7830 sizes.mmap_bytes += bytes; 7831 break; 7832 } 7833 } 7834 } 7835 } 7836 7837 void ASTReader::InitializeSema(Sema &S) { 7838 SemaObj = &S; 7839 S.addExternalSource(this); 7840 7841 // Makes sure any declarations that were deserialized "too early" 7842 // still get added to the identifier's declaration chains. 7843 for (uint64_t ID : PreloadedDeclIDs) { 7844 NamedDecl *D = cast<NamedDecl>(GetDecl(ID)); 7845 pushExternalDeclIntoScope(D, D->getDeclName()); 7846 } 7847 PreloadedDeclIDs.clear(); 7848 7849 // FIXME: What happens if these are changed by a module import? 7850 if (!FPPragmaOptions.empty()) { 7851 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS"); 7852 FPOptionsOverride NewOverrides = 7853 FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]); 7854 SemaObj->CurFPFeatures = 7855 NewOverrides.applyOverrides(SemaObj->getLangOpts()); 7856 } 7857 7858 SemaObj->OpenCLFeatures = OpenCLExtensions; 7859 7860 UpdateSema(); 7861 } 7862 7863 void ASTReader::UpdateSema() { 7864 assert(SemaObj && "no Sema to update"); 7865 7866 // Load the offsets of the declarations that Sema references. 7867 // They will be lazily deserialized when needed. 7868 if (!SemaDeclRefs.empty()) { 7869 assert(SemaDeclRefs.size() % 3 == 0); 7870 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) { 7871 if (!SemaObj->StdNamespace) 7872 SemaObj->StdNamespace = SemaDeclRefs[I]; 7873 if (!SemaObj->StdBadAlloc) 7874 SemaObj->StdBadAlloc = SemaDeclRefs[I+1]; 7875 if (!SemaObj->StdAlignValT) 7876 SemaObj->StdAlignValT = SemaDeclRefs[I+2]; 7877 } 7878 SemaDeclRefs.clear(); 7879 } 7880 7881 // Update the state of pragmas. Use the same API as if we had encountered the 7882 // pragma in the source. 7883 if(OptimizeOffPragmaLocation.isValid()) 7884 SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation); 7885 if (PragmaMSStructState != -1) 7886 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState); 7887 if (PointersToMembersPragmaLocation.isValid()) { 7888 SemaObj->ActOnPragmaMSPointersToMembers( 7889 (LangOptions::PragmaMSPointersToMembersKind) 7890 PragmaMSPointersToMembersState, 7891 PointersToMembersPragmaLocation); 7892 } 7893 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth; 7894 7895 if (PragmaAlignPackCurrentValue) { 7896 // The bottom of the stack might have a default value. It must be adjusted 7897 // to the current value to ensure that the packing state is preserved after 7898 // popping entries that were included/imported from a PCH/module. 7899 bool DropFirst = false; 7900 if (!PragmaAlignPackStack.empty() && 7901 PragmaAlignPackStack.front().Location.isInvalid()) { 7902 assert(PragmaAlignPackStack.front().Value == 7903 SemaObj->AlignPackStack.DefaultValue && 7904 "Expected a default alignment value"); 7905 SemaObj->AlignPackStack.Stack.emplace_back( 7906 PragmaAlignPackStack.front().SlotLabel, 7907 SemaObj->AlignPackStack.CurrentValue, 7908 SemaObj->AlignPackStack.CurrentPragmaLocation, 7909 PragmaAlignPackStack.front().PushLocation); 7910 DropFirst = true; 7911 } 7912 for (const auto &Entry : llvm::makeArrayRef(PragmaAlignPackStack) 7913 .drop_front(DropFirst ? 1 : 0)) { 7914 SemaObj->AlignPackStack.Stack.emplace_back( 7915 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation); 7916 } 7917 if (PragmaAlignPackCurrentLocation.isInvalid()) { 7918 assert(*PragmaAlignPackCurrentValue == 7919 SemaObj->AlignPackStack.DefaultValue && 7920 "Expected a default align and pack value"); 7921 // Keep the current values. 7922 } else { 7923 SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue; 7924 SemaObj->AlignPackStack.CurrentPragmaLocation = 7925 PragmaAlignPackCurrentLocation; 7926 } 7927 } 7928 if (FpPragmaCurrentValue) { 7929 // The bottom of the stack might have a default value. It must be adjusted 7930 // to the current value to ensure that fp-pragma state is preserved after 7931 // popping entries that were included/imported from a PCH/module. 7932 bool DropFirst = false; 7933 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) { 7934 assert(FpPragmaStack.front().Value == 7935 SemaObj->FpPragmaStack.DefaultValue && 7936 "Expected a default pragma float_control value"); 7937 SemaObj->FpPragmaStack.Stack.emplace_back( 7938 FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue, 7939 SemaObj->FpPragmaStack.CurrentPragmaLocation, 7940 FpPragmaStack.front().PushLocation); 7941 DropFirst = true; 7942 } 7943 for (const auto &Entry : 7944 llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0)) 7945 SemaObj->FpPragmaStack.Stack.emplace_back( 7946 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation); 7947 if (FpPragmaCurrentLocation.isInvalid()) { 7948 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue && 7949 "Expected a default pragma float_control value"); 7950 // Keep the current values. 7951 } else { 7952 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue; 7953 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation; 7954 } 7955 } 7956 7957 // For non-modular AST files, restore visiblity of modules. 7958 for (auto &Import : ImportedModules) { 7959 if (Import.ImportLoc.isInvalid()) 7960 continue; 7961 if (Module *Imported = getSubmodule(Import.ID)) { 7962 SemaObj->makeModuleVisible(Imported, Import.ImportLoc); 7963 } 7964 } 7965 } 7966 7967 IdentifierInfo *ASTReader::get(StringRef Name) { 7968 // Note that we are loading an identifier. 7969 Deserializing AnIdentifier(this); 7970 7971 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0, 7972 NumIdentifierLookups, 7973 NumIdentifierLookupHits); 7974 7975 // We don't need to do identifier table lookups in C++ modules (we preload 7976 // all interesting declarations, and don't need to use the scope for name 7977 // lookups). Perform the lookup in PCH files, though, since we don't build 7978 // a complete initial identifier table if we're carrying on from a PCH. 7979 if (PP.getLangOpts().CPlusPlus) { 7980 for (auto F : ModuleMgr.pch_modules()) 7981 if (Visitor(*F)) 7982 break; 7983 } else { 7984 // If there is a global index, look there first to determine which modules 7985 // provably do not have any results for this identifier. 7986 GlobalModuleIndex::HitSet Hits; 7987 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 7988 if (!loadGlobalIndex()) { 7989 if (GlobalIndex->lookupIdentifier(Name, Hits)) { 7990 HitsPtr = &Hits; 7991 } 7992 } 7993 7994 ModuleMgr.visit(Visitor, HitsPtr); 7995 } 7996 7997 IdentifierInfo *II = Visitor.getIdentifierInfo(); 7998 markIdentifierUpToDate(II); 7999 return II; 8000 } 8001 8002 namespace clang { 8003 8004 /// An identifier-lookup iterator that enumerates all of the 8005 /// identifiers stored within a set of AST files. 8006 class ASTIdentifierIterator : public IdentifierIterator { 8007 /// The AST reader whose identifiers are being enumerated. 8008 const ASTReader &Reader; 8009 8010 /// The current index into the chain of AST files stored in 8011 /// the AST reader. 8012 unsigned Index; 8013 8014 /// The current position within the identifier lookup table 8015 /// of the current AST file. 8016 ASTIdentifierLookupTable::key_iterator Current; 8017 8018 /// The end position within the identifier lookup table of 8019 /// the current AST file. 8020 ASTIdentifierLookupTable::key_iterator End; 8021 8022 /// Whether to skip any modules in the ASTReader. 8023 bool SkipModules; 8024 8025 public: 8026 explicit ASTIdentifierIterator(const ASTReader &Reader, 8027 bool SkipModules = false); 8028 8029 StringRef Next() override; 8030 }; 8031 8032 } // namespace clang 8033 8034 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader, 8035 bool SkipModules) 8036 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) { 8037 } 8038 8039 StringRef ASTIdentifierIterator::Next() { 8040 while (Current == End) { 8041 // If we have exhausted all of our AST files, we're done. 8042 if (Index == 0) 8043 return StringRef(); 8044 8045 --Index; 8046 ModuleFile &F = Reader.ModuleMgr[Index]; 8047 if (SkipModules && F.isModule()) 8048 continue; 8049 8050 ASTIdentifierLookupTable *IdTable = 8051 (ASTIdentifierLookupTable *)F.IdentifierLookupTable; 8052 Current = IdTable->key_begin(); 8053 End = IdTable->key_end(); 8054 } 8055 8056 // We have any identifiers remaining in the current AST file; return 8057 // the next one. 8058 StringRef Result = *Current; 8059 ++Current; 8060 return Result; 8061 } 8062 8063 namespace { 8064 8065 /// A utility for appending two IdentifierIterators. 8066 class ChainedIdentifierIterator : public IdentifierIterator { 8067 std::unique_ptr<IdentifierIterator> Current; 8068 std::unique_ptr<IdentifierIterator> Queued; 8069 8070 public: 8071 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First, 8072 std::unique_ptr<IdentifierIterator> Second) 8073 : Current(std::move(First)), Queued(std::move(Second)) {} 8074 8075 StringRef Next() override { 8076 if (!Current) 8077 return StringRef(); 8078 8079 StringRef result = Current->Next(); 8080 if (!result.empty()) 8081 return result; 8082 8083 // Try the queued iterator, which may itself be empty. 8084 Current.reset(); 8085 std::swap(Current, Queued); 8086 return Next(); 8087 } 8088 }; 8089 8090 } // namespace 8091 8092 IdentifierIterator *ASTReader::getIdentifiers() { 8093 if (!loadGlobalIndex()) { 8094 std::unique_ptr<IdentifierIterator> ReaderIter( 8095 new ASTIdentifierIterator(*this, /*SkipModules=*/true)); 8096 std::unique_ptr<IdentifierIterator> ModulesIter( 8097 GlobalIndex->createIdentifierIterator()); 8098 return new ChainedIdentifierIterator(std::move(ReaderIter), 8099 std::move(ModulesIter)); 8100 } 8101 8102 return new ASTIdentifierIterator(*this); 8103 } 8104 8105 namespace clang { 8106 namespace serialization { 8107 8108 class ReadMethodPoolVisitor { 8109 ASTReader &Reader; 8110 Selector Sel; 8111 unsigned PriorGeneration; 8112 unsigned InstanceBits = 0; 8113 unsigned FactoryBits = 0; 8114 bool InstanceHasMoreThanOneDecl = false; 8115 bool FactoryHasMoreThanOneDecl = false; 8116 SmallVector<ObjCMethodDecl *, 4> InstanceMethods; 8117 SmallVector<ObjCMethodDecl *, 4> FactoryMethods; 8118 8119 public: 8120 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel, 8121 unsigned PriorGeneration) 8122 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {} 8123 8124 bool operator()(ModuleFile &M) { 8125 if (!M.SelectorLookupTable) 8126 return false; 8127 8128 // If we've already searched this module file, skip it now. 8129 if (M.Generation <= PriorGeneration) 8130 return true; 8131 8132 ++Reader.NumMethodPoolTableLookups; 8133 ASTSelectorLookupTable *PoolTable 8134 = (ASTSelectorLookupTable*)M.SelectorLookupTable; 8135 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel); 8136 if (Pos == PoolTable->end()) 8137 return false; 8138 8139 ++Reader.NumMethodPoolTableHits; 8140 ++Reader.NumSelectorsRead; 8141 // FIXME: Not quite happy with the statistics here. We probably should 8142 // disable this tracking when called via LoadSelector. 8143 // Also, should entries without methods count as misses? 8144 ++Reader.NumMethodPoolEntriesRead; 8145 ASTSelectorLookupTrait::data_type Data = *Pos; 8146 if (Reader.DeserializationListener) 8147 Reader.DeserializationListener->SelectorRead(Data.ID, Sel); 8148 8149 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end()); 8150 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end()); 8151 InstanceBits = Data.InstanceBits; 8152 FactoryBits = Data.FactoryBits; 8153 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl; 8154 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl; 8155 return true; 8156 } 8157 8158 /// Retrieve the instance methods found by this visitor. 8159 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const { 8160 return InstanceMethods; 8161 } 8162 8163 /// Retrieve the instance methods found by this visitor. 8164 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const { 8165 return FactoryMethods; 8166 } 8167 8168 unsigned getInstanceBits() const { return InstanceBits; } 8169 unsigned getFactoryBits() const { return FactoryBits; } 8170 8171 bool instanceHasMoreThanOneDecl() const { 8172 return InstanceHasMoreThanOneDecl; 8173 } 8174 8175 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; } 8176 }; 8177 8178 } // namespace serialization 8179 } // namespace clang 8180 8181 /// Add the given set of methods to the method list. 8182 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods, 8183 ObjCMethodList &List) { 8184 for (unsigned I = 0, N = Methods.size(); I != N; ++I) { 8185 S.addMethodToGlobalList(&List, Methods[I]); 8186 } 8187 } 8188 8189 void ASTReader::ReadMethodPool(Selector Sel) { 8190 // Get the selector generation and update it to the current generation. 8191 unsigned &Generation = SelectorGeneration[Sel]; 8192 unsigned PriorGeneration = Generation; 8193 Generation = getGeneration(); 8194 SelectorOutOfDate[Sel] = false; 8195 8196 // Search for methods defined with this selector. 8197 ++NumMethodPoolLookups; 8198 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration); 8199 ModuleMgr.visit(Visitor); 8200 8201 if (Visitor.getInstanceMethods().empty() && 8202 Visitor.getFactoryMethods().empty()) 8203 return; 8204 8205 ++NumMethodPoolHits; 8206 8207 if (!getSema()) 8208 return; 8209 8210 Sema &S = *getSema(); 8211 Sema::GlobalMethodPool::iterator Pos 8212 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first; 8213 8214 Pos->second.first.setBits(Visitor.getInstanceBits()); 8215 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl()); 8216 Pos->second.second.setBits(Visitor.getFactoryBits()); 8217 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl()); 8218 8219 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since 8220 // when building a module we keep every method individually and may need to 8221 // update hasMoreThanOneDecl as we add the methods. 8222 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first); 8223 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second); 8224 } 8225 8226 void ASTReader::updateOutOfDateSelector(Selector Sel) { 8227 if (SelectorOutOfDate[Sel]) 8228 ReadMethodPool(Sel); 8229 } 8230 8231 void ASTReader::ReadKnownNamespaces( 8232 SmallVectorImpl<NamespaceDecl *> &Namespaces) { 8233 Namespaces.clear(); 8234 8235 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) { 8236 if (NamespaceDecl *Namespace 8237 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I]))) 8238 Namespaces.push_back(Namespace); 8239 } 8240 } 8241 8242 void ASTReader::ReadUndefinedButUsed( 8243 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) { 8244 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) { 8245 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++])); 8246 SourceLocation Loc = 8247 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]); 8248 Undefined.insert(std::make_pair(D, Loc)); 8249 } 8250 } 8251 8252 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector< 8253 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> & 8254 Exprs) { 8255 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) { 8256 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++])); 8257 uint64_t Count = DelayedDeleteExprs[Idx++]; 8258 for (uint64_t C = 0; C < Count; ++C) { 8259 SourceLocation DeleteLoc = 8260 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]); 8261 const bool IsArrayForm = DelayedDeleteExprs[Idx++]; 8262 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm)); 8263 } 8264 } 8265 } 8266 8267 void ASTReader::ReadTentativeDefinitions( 8268 SmallVectorImpl<VarDecl *> &TentativeDefs) { 8269 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { 8270 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I])); 8271 if (Var) 8272 TentativeDefs.push_back(Var); 8273 } 8274 TentativeDefinitions.clear(); 8275 } 8276 8277 void ASTReader::ReadUnusedFileScopedDecls( 8278 SmallVectorImpl<const DeclaratorDecl *> &Decls) { 8279 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) { 8280 DeclaratorDecl *D 8281 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I])); 8282 if (D) 8283 Decls.push_back(D); 8284 } 8285 UnusedFileScopedDecls.clear(); 8286 } 8287 8288 void ASTReader::ReadDelegatingConstructors( 8289 SmallVectorImpl<CXXConstructorDecl *> &Decls) { 8290 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) { 8291 CXXConstructorDecl *D 8292 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I])); 8293 if (D) 8294 Decls.push_back(D); 8295 } 8296 DelegatingCtorDecls.clear(); 8297 } 8298 8299 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) { 8300 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) { 8301 TypedefNameDecl *D 8302 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])); 8303 if (D) 8304 Decls.push_back(D); 8305 } 8306 ExtVectorDecls.clear(); 8307 } 8308 8309 void ASTReader::ReadUnusedLocalTypedefNameCandidates( 8310 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) { 8311 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N; 8312 ++I) { 8313 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>( 8314 GetDecl(UnusedLocalTypedefNameCandidates[I])); 8315 if (D) 8316 Decls.insert(D); 8317 } 8318 UnusedLocalTypedefNameCandidates.clear(); 8319 } 8320 8321 void ASTReader::ReadDeclsToCheckForDeferredDiags( 8322 llvm::SmallSetVector<Decl *, 4> &Decls) { 8323 for (auto I : DeclsToCheckForDeferredDiags) { 8324 auto *D = dyn_cast_or_null<Decl>(GetDecl(I)); 8325 if (D) 8326 Decls.insert(D); 8327 } 8328 DeclsToCheckForDeferredDiags.clear(); 8329 } 8330 8331 void ASTReader::ReadReferencedSelectors( 8332 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) { 8333 if (ReferencedSelectorsData.empty()) 8334 return; 8335 8336 // If there are @selector references added them to its pool. This is for 8337 // implementation of -Wselector. 8338 unsigned int DataSize = ReferencedSelectorsData.size()-1; 8339 unsigned I = 0; 8340 while (I < DataSize) { 8341 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]); 8342 SourceLocation SelLoc 8343 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]); 8344 Sels.push_back(std::make_pair(Sel, SelLoc)); 8345 } 8346 ReferencedSelectorsData.clear(); 8347 } 8348 8349 void ASTReader::ReadWeakUndeclaredIdentifiers( 8350 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) { 8351 if (WeakUndeclaredIdentifiers.empty()) 8352 return; 8353 8354 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) { 8355 IdentifierInfo *WeakId 8356 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8357 IdentifierInfo *AliasId 8358 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8359 SourceLocation Loc 8360 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]); 8361 bool Used = WeakUndeclaredIdentifiers[I++]; 8362 WeakInfo WI(AliasId, Loc); 8363 WI.setUsed(Used); 8364 WeakIDs.push_back(std::make_pair(WeakId, WI)); 8365 } 8366 WeakUndeclaredIdentifiers.clear(); 8367 } 8368 8369 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) { 8370 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) { 8371 ExternalVTableUse VT; 8372 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); 8373 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]); 8374 VT.DefinitionRequired = VTableUses[Idx++]; 8375 VTables.push_back(VT); 8376 } 8377 8378 VTableUses.clear(); 8379 } 8380 8381 void ASTReader::ReadPendingInstantiations( 8382 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) { 8383 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) { 8384 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++])); 8385 SourceLocation Loc 8386 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]); 8387 8388 Pending.push_back(std::make_pair(D, Loc)); 8389 } 8390 PendingInstantiations.clear(); 8391 } 8392 8393 void ASTReader::ReadLateParsedTemplates( 8394 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>> 8395 &LPTMap) { 8396 for (auto &LPT : LateParsedTemplates) { 8397 ModuleFile *FMod = LPT.first; 8398 RecordDataImpl &LateParsed = LPT.second; 8399 for (unsigned Idx = 0, N = LateParsed.size(); Idx < N; 8400 /* In loop */) { 8401 FunctionDecl *FD = 8402 cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++])); 8403 8404 auto LT = std::make_unique<LateParsedTemplate>(); 8405 LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]); 8406 8407 ModuleFile *F = getOwningModuleFile(LT->D); 8408 assert(F && "No module"); 8409 8410 unsigned TokN = LateParsed[Idx++]; 8411 LT->Toks.reserve(TokN); 8412 for (unsigned T = 0; T < TokN; ++T) 8413 LT->Toks.push_back(ReadToken(*F, LateParsed, Idx)); 8414 8415 LPTMap.insert(std::make_pair(FD, std::move(LT))); 8416 } 8417 } 8418 8419 LateParsedTemplates.clear(); 8420 } 8421 8422 void ASTReader::LoadSelector(Selector Sel) { 8423 // It would be complicated to avoid reading the methods anyway. So don't. 8424 ReadMethodPool(Sel); 8425 } 8426 8427 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) { 8428 assert(ID && "Non-zero identifier ID required"); 8429 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); 8430 IdentifiersLoaded[ID - 1] = II; 8431 if (DeserializationListener) 8432 DeserializationListener->IdentifierRead(ID, II); 8433 } 8434 8435 /// Set the globally-visible declarations associated with the given 8436 /// identifier. 8437 /// 8438 /// If the AST reader is currently in a state where the given declaration IDs 8439 /// cannot safely be resolved, they are queued until it is safe to resolve 8440 /// them. 8441 /// 8442 /// \param II an IdentifierInfo that refers to one or more globally-visible 8443 /// declarations. 8444 /// 8445 /// \param DeclIDs the set of declaration IDs with the name @p II that are 8446 /// visible at global scope. 8447 /// 8448 /// \param Decls if non-null, this vector will be populated with the set of 8449 /// deserialized declarations. These declarations will not be pushed into 8450 /// scope. 8451 void 8452 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, 8453 const SmallVectorImpl<uint32_t> &DeclIDs, 8454 SmallVectorImpl<Decl *> *Decls) { 8455 if (NumCurrentElementsDeserializing && !Decls) { 8456 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end()); 8457 return; 8458 } 8459 8460 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { 8461 if (!SemaObj) { 8462 // Queue this declaration so that it will be added to the 8463 // translation unit scope and identifier's declaration chain 8464 // once a Sema object is known. 8465 PreloadedDeclIDs.push_back(DeclIDs[I]); 8466 continue; 8467 } 8468 8469 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); 8470 8471 // If we're simply supposed to record the declarations, do so now. 8472 if (Decls) { 8473 Decls->push_back(D); 8474 continue; 8475 } 8476 8477 // Introduce this declaration into the translation-unit scope 8478 // and add it to the declaration chain for this identifier, so 8479 // that (unqualified) name lookup will find it. 8480 pushExternalDeclIntoScope(D, II); 8481 } 8482 } 8483 8484 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) { 8485 if (ID == 0) 8486 return nullptr; 8487 8488 if (IdentifiersLoaded.empty()) { 8489 Error("no identifier table in AST file"); 8490 return nullptr; 8491 } 8492 8493 ID -= 1; 8494 if (!IdentifiersLoaded[ID]) { 8495 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1); 8496 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map"); 8497 ModuleFile *M = I->second; 8498 unsigned Index = ID - M->BaseIdentifierID; 8499 const unsigned char *Data = 8500 M->IdentifierTableData + M->IdentifierOffsets[Index]; 8501 8502 ASTIdentifierLookupTrait Trait(*this, *M); 8503 auto KeyDataLen = Trait.ReadKeyDataLength(Data); 8504 auto Key = Trait.ReadKey(Data, KeyDataLen.first); 8505 auto &II = PP.getIdentifierTable().get(Key); 8506 IdentifiersLoaded[ID] = &II; 8507 markIdentifierFromAST(*this, II); 8508 if (DeserializationListener) 8509 DeserializationListener->IdentifierRead(ID + 1, &II); 8510 } 8511 8512 return IdentifiersLoaded[ID]; 8513 } 8514 8515 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) { 8516 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID)); 8517 } 8518 8519 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) { 8520 if (LocalID < NUM_PREDEF_IDENT_IDS) 8521 return LocalID; 8522 8523 if (!M.ModuleOffsetMap.empty()) 8524 ReadModuleOffsetMap(M); 8525 8526 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8527 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS); 8528 assert(I != M.IdentifierRemap.end() 8529 && "Invalid index into identifier index remap"); 8530 8531 return LocalID + I->second; 8532 } 8533 8534 MacroInfo *ASTReader::getMacro(MacroID ID) { 8535 if (ID == 0) 8536 return nullptr; 8537 8538 if (MacrosLoaded.empty()) { 8539 Error("no macro table in AST file"); 8540 return nullptr; 8541 } 8542 8543 ID -= NUM_PREDEF_MACRO_IDS; 8544 if (!MacrosLoaded[ID]) { 8545 GlobalMacroMapType::iterator I 8546 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS); 8547 assert(I != GlobalMacroMap.end() && "Corrupted global macro map"); 8548 ModuleFile *M = I->second; 8549 unsigned Index = ID - M->BaseMacroID; 8550 MacrosLoaded[ID] = 8551 ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]); 8552 8553 if (DeserializationListener) 8554 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS, 8555 MacrosLoaded[ID]); 8556 } 8557 8558 return MacrosLoaded[ID]; 8559 } 8560 8561 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) { 8562 if (LocalID < NUM_PREDEF_MACRO_IDS) 8563 return LocalID; 8564 8565 if (!M.ModuleOffsetMap.empty()) 8566 ReadModuleOffsetMap(M); 8567 8568 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8569 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS); 8570 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap"); 8571 8572 return LocalID + I->second; 8573 } 8574 8575 serialization::SubmoduleID 8576 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) { 8577 if (LocalID < NUM_PREDEF_SUBMODULE_IDS) 8578 return LocalID; 8579 8580 if (!M.ModuleOffsetMap.empty()) 8581 ReadModuleOffsetMap(M); 8582 8583 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8584 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS); 8585 assert(I != M.SubmoduleRemap.end() 8586 && "Invalid index into submodule index remap"); 8587 8588 return LocalID + I->second; 8589 } 8590 8591 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) { 8592 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) { 8593 assert(GlobalID == 0 && "Unhandled global submodule ID"); 8594 return nullptr; 8595 } 8596 8597 if (GlobalID > SubmodulesLoaded.size()) { 8598 Error("submodule ID out of range in AST file"); 8599 return nullptr; 8600 } 8601 8602 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS]; 8603 } 8604 8605 Module *ASTReader::getModule(unsigned ID) { 8606 return getSubmodule(ID); 8607 } 8608 8609 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) { 8610 if (ID & 1) { 8611 // It's a module, look it up by submodule ID. 8612 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1)); 8613 return I == GlobalSubmoduleMap.end() ? nullptr : I->second; 8614 } else { 8615 // It's a prefix (preamble, PCH, ...). Look it up by index. 8616 unsigned IndexFromEnd = ID >> 1; 8617 assert(IndexFromEnd && "got reference to unknown module file"); 8618 return getModuleManager().pch_modules().end()[-IndexFromEnd]; 8619 } 8620 } 8621 8622 unsigned ASTReader::getModuleFileID(ModuleFile *F) { 8623 if (!F) 8624 return 1; 8625 8626 // For a file representing a module, use the submodule ID of the top-level 8627 // module as the file ID. For any other kind of file, the number of such 8628 // files loaded beforehand will be the same on reload. 8629 // FIXME: Is this true even if we have an explicit module file and a PCH? 8630 if (F->isModule()) 8631 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1; 8632 8633 auto PCHModules = getModuleManager().pch_modules(); 8634 auto I = llvm::find(PCHModules, F); 8635 assert(I != PCHModules.end() && "emitting reference to unknown file"); 8636 return (I - PCHModules.end()) << 1; 8637 } 8638 8639 llvm::Optional<ASTSourceDescriptor> 8640 ASTReader::getSourceDescriptor(unsigned ID) { 8641 if (Module *M = getSubmodule(ID)) 8642 return ASTSourceDescriptor(*M); 8643 8644 // If there is only a single PCH, return it instead. 8645 // Chained PCH are not supported. 8646 const auto &PCHChain = ModuleMgr.pch_modules(); 8647 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) { 8648 ModuleFile &MF = ModuleMgr.getPrimaryModule(); 8649 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName); 8650 StringRef FileName = llvm::sys::path::filename(MF.FileName); 8651 return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName, 8652 MF.Signature); 8653 } 8654 return None; 8655 } 8656 8657 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) { 8658 auto I = DefinitionSource.find(FD); 8659 if (I == DefinitionSource.end()) 8660 return EK_ReplyHazy; 8661 return I->second ? EK_Never : EK_Always; 8662 } 8663 8664 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) { 8665 return DecodeSelector(getGlobalSelectorID(M, LocalID)); 8666 } 8667 8668 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) { 8669 if (ID == 0) 8670 return Selector(); 8671 8672 if (ID > SelectorsLoaded.size()) { 8673 Error("selector ID out of range in AST file"); 8674 return Selector(); 8675 } 8676 8677 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) { 8678 // Load this selector from the selector table. 8679 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID); 8680 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map"); 8681 ModuleFile &M = *I->second; 8682 ASTSelectorLookupTrait Trait(*this, M); 8683 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS; 8684 SelectorsLoaded[ID - 1] = 8685 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0); 8686 if (DeserializationListener) 8687 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 8688 } 8689 8690 return SelectorsLoaded[ID - 1]; 8691 } 8692 8693 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) { 8694 return DecodeSelector(ID); 8695 } 8696 8697 uint32_t ASTReader::GetNumExternalSelectors() { 8698 // ID 0 (the null selector) is considered an external selector. 8699 return getTotalNumSelectors() + 1; 8700 } 8701 8702 serialization::SelectorID 8703 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const { 8704 if (LocalID < NUM_PREDEF_SELECTOR_IDS) 8705 return LocalID; 8706 8707 if (!M.ModuleOffsetMap.empty()) 8708 ReadModuleOffsetMap(M); 8709 8710 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8711 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS); 8712 assert(I != M.SelectorRemap.end() 8713 && "Invalid index into selector index remap"); 8714 8715 return LocalID + I->second; 8716 } 8717 8718 DeclarationNameLoc 8719 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) { 8720 switch (Name.getNameKind()) { 8721 case DeclarationName::CXXConstructorName: 8722 case DeclarationName::CXXDestructorName: 8723 case DeclarationName::CXXConversionFunctionName: 8724 return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo()); 8725 8726 case DeclarationName::CXXOperatorName: 8727 return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange()); 8728 8729 case DeclarationName::CXXLiteralOperatorName: 8730 return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc( 8731 readSourceLocation()); 8732 8733 case DeclarationName::Identifier: 8734 case DeclarationName::ObjCZeroArgSelector: 8735 case DeclarationName::ObjCOneArgSelector: 8736 case DeclarationName::ObjCMultiArgSelector: 8737 case DeclarationName::CXXUsingDirective: 8738 case DeclarationName::CXXDeductionGuideName: 8739 break; 8740 } 8741 return DeclarationNameLoc(); 8742 } 8743 8744 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() { 8745 DeclarationNameInfo NameInfo; 8746 NameInfo.setName(readDeclarationName()); 8747 NameInfo.setLoc(readSourceLocation()); 8748 NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName())); 8749 return NameInfo; 8750 } 8751 8752 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) { 8753 Info.QualifierLoc = readNestedNameSpecifierLoc(); 8754 unsigned NumTPLists = readInt(); 8755 Info.NumTemplParamLists = NumTPLists; 8756 if (NumTPLists) { 8757 Info.TemplParamLists = 8758 new (getContext()) TemplateParameterList *[NumTPLists]; 8759 for (unsigned i = 0; i != NumTPLists; ++i) 8760 Info.TemplParamLists[i] = readTemplateParameterList(); 8761 } 8762 } 8763 8764 TemplateParameterList * 8765 ASTRecordReader::readTemplateParameterList() { 8766 SourceLocation TemplateLoc = readSourceLocation(); 8767 SourceLocation LAngleLoc = readSourceLocation(); 8768 SourceLocation RAngleLoc = readSourceLocation(); 8769 8770 unsigned NumParams = readInt(); 8771 SmallVector<NamedDecl *, 16> Params; 8772 Params.reserve(NumParams); 8773 while (NumParams--) 8774 Params.push_back(readDeclAs<NamedDecl>()); 8775 8776 bool HasRequiresClause = readBool(); 8777 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr; 8778 8779 TemplateParameterList *TemplateParams = TemplateParameterList::Create( 8780 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause); 8781 return TemplateParams; 8782 } 8783 8784 void ASTRecordReader::readTemplateArgumentList( 8785 SmallVectorImpl<TemplateArgument> &TemplArgs, 8786 bool Canonicalize) { 8787 unsigned NumTemplateArgs = readInt(); 8788 TemplArgs.reserve(NumTemplateArgs); 8789 while (NumTemplateArgs--) 8790 TemplArgs.push_back(readTemplateArgument(Canonicalize)); 8791 } 8792 8793 /// Read a UnresolvedSet structure. 8794 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) { 8795 unsigned NumDecls = readInt(); 8796 Set.reserve(getContext(), NumDecls); 8797 while (NumDecls--) { 8798 DeclID ID = readDeclID(); 8799 AccessSpecifier AS = (AccessSpecifier) readInt(); 8800 Set.addLazyDecl(getContext(), ID, AS); 8801 } 8802 } 8803 8804 CXXBaseSpecifier 8805 ASTRecordReader::readCXXBaseSpecifier() { 8806 bool isVirtual = readBool(); 8807 bool isBaseOfClass = readBool(); 8808 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt()); 8809 bool inheritConstructors = readBool(); 8810 TypeSourceInfo *TInfo = readTypeSourceInfo(); 8811 SourceRange Range = readSourceRange(); 8812 SourceLocation EllipsisLoc = readSourceLocation(); 8813 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo, 8814 EllipsisLoc); 8815 Result.setInheritConstructors(inheritConstructors); 8816 return Result; 8817 } 8818 8819 CXXCtorInitializer ** 8820 ASTRecordReader::readCXXCtorInitializers() { 8821 ASTContext &Context = getContext(); 8822 unsigned NumInitializers = readInt(); 8823 assert(NumInitializers && "wrote ctor initializers but have no inits"); 8824 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers]; 8825 for (unsigned i = 0; i != NumInitializers; ++i) { 8826 TypeSourceInfo *TInfo = nullptr; 8827 bool IsBaseVirtual = false; 8828 FieldDecl *Member = nullptr; 8829 IndirectFieldDecl *IndirectMember = nullptr; 8830 8831 CtorInitializerType Type = (CtorInitializerType) readInt(); 8832 switch (Type) { 8833 case CTOR_INITIALIZER_BASE: 8834 TInfo = readTypeSourceInfo(); 8835 IsBaseVirtual = readBool(); 8836 break; 8837 8838 case CTOR_INITIALIZER_DELEGATING: 8839 TInfo = readTypeSourceInfo(); 8840 break; 8841 8842 case CTOR_INITIALIZER_MEMBER: 8843 Member = readDeclAs<FieldDecl>(); 8844 break; 8845 8846 case CTOR_INITIALIZER_INDIRECT_MEMBER: 8847 IndirectMember = readDeclAs<IndirectFieldDecl>(); 8848 break; 8849 } 8850 8851 SourceLocation MemberOrEllipsisLoc = readSourceLocation(); 8852 Expr *Init = readExpr(); 8853 SourceLocation LParenLoc = readSourceLocation(); 8854 SourceLocation RParenLoc = readSourceLocation(); 8855 8856 CXXCtorInitializer *BOMInit; 8857 if (Type == CTOR_INITIALIZER_BASE) 8858 BOMInit = new (Context) 8859 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init, 8860 RParenLoc, MemberOrEllipsisLoc); 8861 else if (Type == CTOR_INITIALIZER_DELEGATING) 8862 BOMInit = new (Context) 8863 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc); 8864 else if (Member) 8865 BOMInit = new (Context) 8866 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc, 8867 Init, RParenLoc); 8868 else 8869 BOMInit = new (Context) 8870 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc, 8871 LParenLoc, Init, RParenLoc); 8872 8873 if (/*IsWritten*/readBool()) { 8874 unsigned SourceOrder = readInt(); 8875 BOMInit->setSourceOrder(SourceOrder); 8876 } 8877 8878 CtorInitializers[i] = BOMInit; 8879 } 8880 8881 return CtorInitializers; 8882 } 8883 8884 NestedNameSpecifierLoc 8885 ASTRecordReader::readNestedNameSpecifierLoc() { 8886 ASTContext &Context = getContext(); 8887 unsigned N = readInt(); 8888 NestedNameSpecifierLocBuilder Builder; 8889 for (unsigned I = 0; I != N; ++I) { 8890 auto Kind = readNestedNameSpecifierKind(); 8891 switch (Kind) { 8892 case NestedNameSpecifier::Identifier: { 8893 IdentifierInfo *II = readIdentifier(); 8894 SourceRange Range = readSourceRange(); 8895 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd()); 8896 break; 8897 } 8898 8899 case NestedNameSpecifier::Namespace: { 8900 NamespaceDecl *NS = readDeclAs<NamespaceDecl>(); 8901 SourceRange Range = readSourceRange(); 8902 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd()); 8903 break; 8904 } 8905 8906 case NestedNameSpecifier::NamespaceAlias: { 8907 NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>(); 8908 SourceRange Range = readSourceRange(); 8909 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd()); 8910 break; 8911 } 8912 8913 case NestedNameSpecifier::TypeSpec: 8914 case NestedNameSpecifier::TypeSpecWithTemplate: { 8915 bool Template = readBool(); 8916 TypeSourceInfo *T = readTypeSourceInfo(); 8917 if (!T) 8918 return NestedNameSpecifierLoc(); 8919 SourceLocation ColonColonLoc = readSourceLocation(); 8920 8921 // FIXME: 'template' keyword location not saved anywhere, so we fake it. 8922 Builder.Extend(Context, 8923 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(), 8924 T->getTypeLoc(), ColonColonLoc); 8925 break; 8926 } 8927 8928 case NestedNameSpecifier::Global: { 8929 SourceLocation ColonColonLoc = readSourceLocation(); 8930 Builder.MakeGlobal(Context, ColonColonLoc); 8931 break; 8932 } 8933 8934 case NestedNameSpecifier::Super: { 8935 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>(); 8936 SourceRange Range = readSourceRange(); 8937 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd()); 8938 break; 8939 } 8940 } 8941 } 8942 8943 return Builder.getWithLocInContext(Context); 8944 } 8945 8946 SourceRange 8947 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record, 8948 unsigned &Idx) { 8949 SourceLocation beg = ReadSourceLocation(F, Record, Idx); 8950 SourceLocation end = ReadSourceLocation(F, Record, Idx); 8951 return SourceRange(beg, end); 8952 } 8953 8954 /// Read a floating-point value 8955 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) { 8956 return llvm::APFloat(Sem, readAPInt()); 8957 } 8958 8959 // Read a string 8960 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) { 8961 unsigned Len = Record[Idx++]; 8962 std::string Result(Record.data() + Idx, Record.data() + Idx + Len); 8963 Idx += Len; 8964 return Result; 8965 } 8966 8967 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record, 8968 unsigned &Idx) { 8969 std::string Filename = ReadString(Record, Idx); 8970 ResolveImportedPath(F, Filename); 8971 return Filename; 8972 } 8973 8974 std::string ASTReader::ReadPath(StringRef BaseDirectory, 8975 const RecordData &Record, unsigned &Idx) { 8976 std::string Filename = ReadString(Record, Idx); 8977 if (!BaseDirectory.empty()) 8978 ResolveImportedPath(Filename, BaseDirectory); 8979 return Filename; 8980 } 8981 8982 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record, 8983 unsigned &Idx) { 8984 unsigned Major = Record[Idx++]; 8985 unsigned Minor = Record[Idx++]; 8986 unsigned Subminor = Record[Idx++]; 8987 if (Minor == 0) 8988 return VersionTuple(Major); 8989 if (Subminor == 0) 8990 return VersionTuple(Major, Minor - 1); 8991 return VersionTuple(Major, Minor - 1, Subminor - 1); 8992 } 8993 8994 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F, 8995 const RecordData &Record, 8996 unsigned &Idx) { 8997 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx); 8998 return CXXTemporary::Create(getContext(), Decl); 8999 } 9000 9001 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const { 9002 return Diag(CurrentImportLoc, DiagID); 9003 } 9004 9005 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const { 9006 return Diags.Report(Loc, DiagID); 9007 } 9008 9009 /// Retrieve the identifier table associated with the 9010 /// preprocessor. 9011 IdentifierTable &ASTReader::getIdentifierTable() { 9012 return PP.getIdentifierTable(); 9013 } 9014 9015 /// Record that the given ID maps to the given switch-case 9016 /// statement. 9017 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { 9018 assert((*CurrSwitchCaseStmts)[ID] == nullptr && 9019 "Already have a SwitchCase with this ID"); 9020 (*CurrSwitchCaseStmts)[ID] = SC; 9021 } 9022 9023 /// Retrieve the switch-case statement with the given ID. 9024 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) { 9025 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID"); 9026 return (*CurrSwitchCaseStmts)[ID]; 9027 } 9028 9029 void ASTReader::ClearSwitchCaseIDs() { 9030 CurrSwitchCaseStmts->clear(); 9031 } 9032 9033 void ASTReader::ReadComments() { 9034 ASTContext &Context = getContext(); 9035 std::vector<RawComment *> Comments; 9036 for (SmallVectorImpl<std::pair<BitstreamCursor, 9037 serialization::ModuleFile *>>::iterator 9038 I = CommentsCursors.begin(), 9039 E = CommentsCursors.end(); 9040 I != E; ++I) { 9041 Comments.clear(); 9042 BitstreamCursor &Cursor = I->first; 9043 serialization::ModuleFile &F = *I->second; 9044 SavedStreamPosition SavedPosition(Cursor); 9045 9046 RecordData Record; 9047 while (true) { 9048 Expected<llvm::BitstreamEntry> MaybeEntry = 9049 Cursor.advanceSkippingSubblocks( 9050 BitstreamCursor::AF_DontPopBlockAtEnd); 9051 if (!MaybeEntry) { 9052 Error(MaybeEntry.takeError()); 9053 return; 9054 } 9055 llvm::BitstreamEntry Entry = MaybeEntry.get(); 9056 9057 switch (Entry.Kind) { 9058 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 9059 case llvm::BitstreamEntry::Error: 9060 Error("malformed block record in AST file"); 9061 return; 9062 case llvm::BitstreamEntry::EndBlock: 9063 goto NextCursor; 9064 case llvm::BitstreamEntry::Record: 9065 // The interesting case. 9066 break; 9067 } 9068 9069 // Read a record. 9070 Record.clear(); 9071 Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record); 9072 if (!MaybeComment) { 9073 Error(MaybeComment.takeError()); 9074 return; 9075 } 9076 switch ((CommentRecordTypes)MaybeComment.get()) { 9077 case COMMENTS_RAW_COMMENT: { 9078 unsigned Idx = 0; 9079 SourceRange SR = ReadSourceRange(F, Record, Idx); 9080 RawComment::CommentKind Kind = 9081 (RawComment::CommentKind) Record[Idx++]; 9082 bool IsTrailingComment = Record[Idx++]; 9083 bool IsAlmostTrailingComment = Record[Idx++]; 9084 Comments.push_back(new (Context) RawComment( 9085 SR, Kind, IsTrailingComment, IsAlmostTrailingComment)); 9086 break; 9087 } 9088 } 9089 } 9090 NextCursor: 9091 llvm::DenseMap<FileID, std::map<unsigned, RawComment *>> 9092 FileToOffsetToComment; 9093 for (RawComment *C : Comments) { 9094 SourceLocation CommentLoc = C->getBeginLoc(); 9095 if (CommentLoc.isValid()) { 9096 std::pair<FileID, unsigned> Loc = 9097 SourceMgr.getDecomposedLoc(CommentLoc); 9098 if (Loc.first.isValid()) 9099 Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C); 9100 } 9101 } 9102 } 9103 } 9104 9105 void ASTReader::visitInputFiles(serialization::ModuleFile &MF, 9106 bool IncludeSystem, bool Complain, 9107 llvm::function_ref<void(const serialization::InputFile &IF, 9108 bool isSystem)> Visitor) { 9109 unsigned NumUserInputs = MF.NumUserInputFiles; 9110 unsigned NumInputs = MF.InputFilesLoaded.size(); 9111 assert(NumUserInputs <= NumInputs); 9112 unsigned N = IncludeSystem ? NumInputs : NumUserInputs; 9113 for (unsigned I = 0; I < N; ++I) { 9114 bool IsSystem = I >= NumUserInputs; 9115 InputFile IF = getInputFile(MF, I+1, Complain); 9116 Visitor(IF, IsSystem); 9117 } 9118 } 9119 9120 void ASTReader::visitTopLevelModuleMaps( 9121 serialization::ModuleFile &MF, 9122 llvm::function_ref<void(const FileEntry *FE)> Visitor) { 9123 unsigned NumInputs = MF.InputFilesLoaded.size(); 9124 for (unsigned I = 0; I < NumInputs; ++I) { 9125 InputFileInfo IFI = readInputFileInfo(MF, I + 1); 9126 if (IFI.TopLevelModuleMap) 9127 // FIXME: This unnecessarily re-reads the InputFileInfo. 9128 if (auto FE = getInputFile(MF, I + 1).getFile()) 9129 Visitor(FE); 9130 } 9131 } 9132 9133 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) { 9134 // If we know the owning module, use it. 9135 if (Module *M = D->getImportedOwningModule()) 9136 return M->getFullModuleName(); 9137 9138 // Otherwise, use the name of the top-level module the decl is within. 9139 if (ModuleFile *M = getOwningModuleFile(D)) 9140 return M->ModuleName; 9141 9142 // Not from a module. 9143 return {}; 9144 } 9145 9146 void ASTReader::finishPendingActions() { 9147 while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() || 9148 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() || 9149 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() || 9150 !PendingUpdateRecords.empty()) { 9151 // If any identifiers with corresponding top-level declarations have 9152 // been loaded, load those declarations now. 9153 using TopLevelDeclsMap = 9154 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>; 9155 TopLevelDeclsMap TopLevelDecls; 9156 9157 while (!PendingIdentifierInfos.empty()) { 9158 IdentifierInfo *II = PendingIdentifierInfos.back().first; 9159 SmallVector<uint32_t, 4> DeclIDs = 9160 std::move(PendingIdentifierInfos.back().second); 9161 PendingIdentifierInfos.pop_back(); 9162 9163 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]); 9164 } 9165 9166 // Load each function type that we deferred loading because it was a 9167 // deduced type that might refer to a local type declared within itself. 9168 for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) { 9169 auto *FD = PendingFunctionTypes[I].first; 9170 FD->setType(GetType(PendingFunctionTypes[I].second)); 9171 9172 // If we gave a function a deduced return type, remember that we need to 9173 // propagate that along the redeclaration chain. 9174 auto *DT = FD->getReturnType()->getContainedDeducedType(); 9175 if (DT && DT->isDeduced()) 9176 PendingDeducedTypeUpdates.insert( 9177 {FD->getCanonicalDecl(), FD->getReturnType()}); 9178 } 9179 PendingFunctionTypes.clear(); 9180 9181 // For each decl chain that we wanted to complete while deserializing, mark 9182 // it as "still needs to be completed". 9183 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) { 9184 markIncompleteDeclChain(PendingIncompleteDeclChains[I]); 9185 } 9186 PendingIncompleteDeclChains.clear(); 9187 9188 // Load pending declaration chains. 9189 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) 9190 loadPendingDeclChain(PendingDeclChains[I].first, 9191 PendingDeclChains[I].second); 9192 PendingDeclChains.clear(); 9193 9194 // Make the most recent of the top-level declarations visible. 9195 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(), 9196 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) { 9197 IdentifierInfo *II = TLD->first; 9198 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) { 9199 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II); 9200 } 9201 } 9202 9203 // Load any pending macro definitions. 9204 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) { 9205 IdentifierInfo *II = PendingMacroIDs.begin()[I].first; 9206 SmallVector<PendingMacroInfo, 2> GlobalIDs; 9207 GlobalIDs.swap(PendingMacroIDs.begin()[I].second); 9208 // Initialize the macro history from chained-PCHs ahead of module imports. 9209 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9210 ++IDIdx) { 9211 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9212 if (!Info.M->isModule()) 9213 resolvePendingMacro(II, Info); 9214 } 9215 // Handle module imports. 9216 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9217 ++IDIdx) { 9218 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9219 if (Info.M->isModule()) 9220 resolvePendingMacro(II, Info); 9221 } 9222 } 9223 PendingMacroIDs.clear(); 9224 9225 // Wire up the DeclContexts for Decls that we delayed setting until 9226 // recursive loading is completed. 9227 while (!PendingDeclContextInfos.empty()) { 9228 PendingDeclContextInfo Info = PendingDeclContextInfos.front(); 9229 PendingDeclContextInfos.pop_front(); 9230 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC)); 9231 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC)); 9232 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext()); 9233 } 9234 9235 // Perform any pending declaration updates. 9236 while (!PendingUpdateRecords.empty()) { 9237 auto Update = PendingUpdateRecords.pop_back_val(); 9238 ReadingKindTracker ReadingKind(Read_Decl, *this); 9239 loadDeclUpdateRecords(Update); 9240 } 9241 } 9242 9243 // At this point, all update records for loaded decls are in place, so any 9244 // fake class definitions should have become real. 9245 assert(PendingFakeDefinitionData.empty() && 9246 "faked up a class definition but never saw the real one"); 9247 9248 // If we deserialized any C++ or Objective-C class definitions, any 9249 // Objective-C protocol definitions, or any redeclarable templates, make sure 9250 // that all redeclarations point to the definitions. Note that this can only 9251 // happen now, after the redeclaration chains have been fully wired. 9252 for (Decl *D : PendingDefinitions) { 9253 if (TagDecl *TD = dyn_cast<TagDecl>(D)) { 9254 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) { 9255 // Make sure that the TagType points at the definition. 9256 const_cast<TagType*>(TagT)->decl = TD; 9257 } 9258 9259 if (auto RD = dyn_cast<CXXRecordDecl>(D)) { 9260 for (auto *R = getMostRecentExistingDecl(RD); R; 9261 R = R->getPreviousDecl()) { 9262 assert((R == D) == 9263 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() && 9264 "declaration thinks it's the definition but it isn't"); 9265 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData; 9266 } 9267 } 9268 9269 continue; 9270 } 9271 9272 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) { 9273 // Make sure that the ObjCInterfaceType points at the definition. 9274 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl)) 9275 ->Decl = ID; 9276 9277 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl()) 9278 cast<ObjCInterfaceDecl>(R)->Data = ID->Data; 9279 9280 continue; 9281 } 9282 9283 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) { 9284 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl()) 9285 cast<ObjCProtocolDecl>(R)->Data = PD->Data; 9286 9287 continue; 9288 } 9289 9290 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl(); 9291 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl()) 9292 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common; 9293 } 9294 PendingDefinitions.clear(); 9295 9296 // Load the bodies of any functions or methods we've encountered. We do 9297 // this now (delayed) so that we can be sure that the declaration chains 9298 // have been fully wired up (hasBody relies on this). 9299 // FIXME: We shouldn't require complete redeclaration chains here. 9300 for (PendingBodiesMap::iterator PB = PendingBodies.begin(), 9301 PBEnd = PendingBodies.end(); 9302 PB != PBEnd; ++PB) { 9303 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) { 9304 // For a function defined inline within a class template, force the 9305 // canonical definition to be the one inside the canonical definition of 9306 // the template. This ensures that we instantiate from a correct view 9307 // of the template. 9308 // 9309 // Sadly we can't do this more generally: we can't be sure that all 9310 // copies of an arbitrary class definition will have the same members 9311 // defined (eg, some member functions may not be instantiated, and some 9312 // special members may or may not have been implicitly defined). 9313 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent())) 9314 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition()) 9315 continue; 9316 9317 // FIXME: Check for =delete/=default? 9318 // FIXME: Complain about ODR violations here? 9319 const FunctionDecl *Defn = nullptr; 9320 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) { 9321 FD->setLazyBody(PB->second); 9322 } else { 9323 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn); 9324 mergeDefinitionVisibility(NonConstDefn, FD); 9325 9326 if (!FD->isLateTemplateParsed() && 9327 !NonConstDefn->isLateTemplateParsed() && 9328 FD->getODRHash() != NonConstDefn->getODRHash()) { 9329 if (!isa<CXXMethodDecl>(FD)) { 9330 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9331 } else if (FD->getLexicalParent()->isFileContext() && 9332 NonConstDefn->getLexicalParent()->isFileContext()) { 9333 // Only diagnose out-of-line method definitions. If they are 9334 // in class definitions, then an error will be generated when 9335 // processing the class bodies. 9336 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9337 } 9338 } 9339 } 9340 continue; 9341 } 9342 9343 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first); 9344 if (!getContext().getLangOpts().Modules || !MD->hasBody()) 9345 MD->setLazyBody(PB->second); 9346 } 9347 PendingBodies.clear(); 9348 9349 // Do some cleanup. 9350 for (auto *ND : PendingMergedDefinitionsToDeduplicate) 9351 getContext().deduplicateMergedDefinitonsFor(ND); 9352 PendingMergedDefinitionsToDeduplicate.clear(); 9353 } 9354 9355 void ASTReader::diagnoseOdrViolations() { 9356 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() && 9357 PendingFunctionOdrMergeFailures.empty() && 9358 PendingEnumOdrMergeFailures.empty()) 9359 return; 9360 9361 // Trigger the import of the full definition of each class that had any 9362 // odr-merging problems, so we can produce better diagnostics for them. 9363 // These updates may in turn find and diagnose some ODR failures, so take 9364 // ownership of the set first. 9365 auto OdrMergeFailures = std::move(PendingOdrMergeFailures); 9366 PendingOdrMergeFailures.clear(); 9367 for (auto &Merge : OdrMergeFailures) { 9368 Merge.first->buildLookup(); 9369 Merge.first->decls_begin(); 9370 Merge.first->bases_begin(); 9371 Merge.first->vbases_begin(); 9372 for (auto &RecordPair : Merge.second) { 9373 auto *RD = RecordPair.first; 9374 RD->decls_begin(); 9375 RD->bases_begin(); 9376 RD->vbases_begin(); 9377 } 9378 } 9379 9380 // Trigger the import of functions. 9381 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures); 9382 PendingFunctionOdrMergeFailures.clear(); 9383 for (auto &Merge : FunctionOdrMergeFailures) { 9384 Merge.first->buildLookup(); 9385 Merge.first->decls_begin(); 9386 Merge.first->getBody(); 9387 for (auto &FD : Merge.second) { 9388 FD->buildLookup(); 9389 FD->decls_begin(); 9390 FD->getBody(); 9391 } 9392 } 9393 9394 // Trigger the import of enums. 9395 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures); 9396 PendingEnumOdrMergeFailures.clear(); 9397 for (auto &Merge : EnumOdrMergeFailures) { 9398 Merge.first->decls_begin(); 9399 for (auto &Enum : Merge.second) { 9400 Enum->decls_begin(); 9401 } 9402 } 9403 9404 // For each declaration from a merged context, check that the canonical 9405 // definition of that context also contains a declaration of the same 9406 // entity. 9407 // 9408 // Caution: this loop does things that might invalidate iterators into 9409 // PendingOdrMergeChecks. Don't turn this into a range-based for loop! 9410 while (!PendingOdrMergeChecks.empty()) { 9411 NamedDecl *D = PendingOdrMergeChecks.pop_back_val(); 9412 9413 // FIXME: Skip over implicit declarations for now. This matters for things 9414 // like implicitly-declared special member functions. This isn't entirely 9415 // correct; we can end up with multiple unmerged declarations of the same 9416 // implicit entity. 9417 if (D->isImplicit()) 9418 continue; 9419 9420 DeclContext *CanonDef = D->getDeclContext(); 9421 9422 bool Found = false; 9423 const Decl *DCanon = D->getCanonicalDecl(); 9424 9425 for (auto RI : D->redecls()) { 9426 if (RI->getLexicalDeclContext() == CanonDef) { 9427 Found = true; 9428 break; 9429 } 9430 } 9431 if (Found) 9432 continue; 9433 9434 // Quick check failed, time to do the slow thing. Note, we can't just 9435 // look up the name of D in CanonDef here, because the member that is 9436 // in CanonDef might not be found by name lookup (it might have been 9437 // replaced by a more recent declaration in the lookup table), and we 9438 // can't necessarily find it in the redeclaration chain because it might 9439 // be merely mergeable, not redeclarable. 9440 llvm::SmallVector<const NamedDecl*, 4> Candidates; 9441 for (auto *CanonMember : CanonDef->decls()) { 9442 if (CanonMember->getCanonicalDecl() == DCanon) { 9443 // This can happen if the declaration is merely mergeable and not 9444 // actually redeclarable (we looked for redeclarations earlier). 9445 // 9446 // FIXME: We should be able to detect this more efficiently, without 9447 // pulling in all of the members of CanonDef. 9448 Found = true; 9449 break; 9450 } 9451 if (auto *ND = dyn_cast<NamedDecl>(CanonMember)) 9452 if (ND->getDeclName() == D->getDeclName()) 9453 Candidates.push_back(ND); 9454 } 9455 9456 if (!Found) { 9457 // The AST doesn't like TagDecls becoming invalid after they've been 9458 // completed. We only really need to mark FieldDecls as invalid here. 9459 if (!isa<TagDecl>(D)) 9460 D->setInvalidDecl(); 9461 9462 // Ensure we don't accidentally recursively enter deserialization while 9463 // we're producing our diagnostic. 9464 Deserializing RecursionGuard(this); 9465 9466 std::string CanonDefModule = 9467 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef)); 9468 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl) 9469 << D << getOwningModuleNameForDiagnostic(D) 9470 << CanonDef << CanonDefModule.empty() << CanonDefModule; 9471 9472 if (Candidates.empty()) 9473 Diag(cast<Decl>(CanonDef)->getLocation(), 9474 diag::note_module_odr_violation_no_possible_decls) << D; 9475 else { 9476 for (unsigned I = 0, N = Candidates.size(); I != N; ++I) 9477 Diag(Candidates[I]->getLocation(), 9478 diag::note_module_odr_violation_possible_decl) 9479 << Candidates[I]; 9480 } 9481 9482 DiagnosedOdrMergeFailures.insert(CanonDef); 9483 } 9484 } 9485 9486 if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() && 9487 EnumOdrMergeFailures.empty()) 9488 return; 9489 9490 // Ensure we don't accidentally recursively enter deserialization while 9491 // we're producing our diagnostics. 9492 Deserializing RecursionGuard(this); 9493 9494 // Common code for hashing helpers. 9495 ODRHash Hash; 9496 auto ComputeQualTypeODRHash = [&Hash](QualType Ty) { 9497 Hash.clear(); 9498 Hash.AddQualType(Ty); 9499 return Hash.CalculateHash(); 9500 }; 9501 9502 auto ComputeODRHash = [&Hash](const Stmt *S) { 9503 assert(S); 9504 Hash.clear(); 9505 Hash.AddStmt(S); 9506 return Hash.CalculateHash(); 9507 }; 9508 9509 auto ComputeSubDeclODRHash = [&Hash](const Decl *D) { 9510 assert(D); 9511 Hash.clear(); 9512 Hash.AddSubDecl(D); 9513 return Hash.CalculateHash(); 9514 }; 9515 9516 auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) { 9517 Hash.clear(); 9518 Hash.AddTemplateArgument(TA); 9519 return Hash.CalculateHash(); 9520 }; 9521 9522 auto ComputeTemplateParameterListODRHash = 9523 [&Hash](const TemplateParameterList *TPL) { 9524 assert(TPL); 9525 Hash.clear(); 9526 Hash.AddTemplateParameterList(TPL); 9527 return Hash.CalculateHash(); 9528 }; 9529 9530 // Used with err_module_odr_violation_mismatch_decl and 9531 // note_module_odr_violation_mismatch_decl 9532 // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed 9533 enum ODRMismatchDecl { 9534 EndOfClass, 9535 PublicSpecifer, 9536 PrivateSpecifer, 9537 ProtectedSpecifer, 9538 StaticAssert, 9539 Field, 9540 CXXMethod, 9541 TypeAlias, 9542 TypeDef, 9543 Var, 9544 Friend, 9545 FunctionTemplate, 9546 Other 9547 }; 9548 9549 // Used with err_module_odr_violation_mismatch_decl_diff and 9550 // note_module_odr_violation_mismatch_decl_diff 9551 enum ODRMismatchDeclDifference { 9552 StaticAssertCondition, 9553 StaticAssertMessage, 9554 StaticAssertOnlyMessage, 9555 FieldName, 9556 FieldTypeName, 9557 FieldSingleBitField, 9558 FieldDifferentWidthBitField, 9559 FieldSingleMutable, 9560 FieldSingleInitializer, 9561 FieldDifferentInitializers, 9562 MethodName, 9563 MethodDeleted, 9564 MethodDefaulted, 9565 MethodVirtual, 9566 MethodStatic, 9567 MethodVolatile, 9568 MethodConst, 9569 MethodInline, 9570 MethodNumberParameters, 9571 MethodParameterType, 9572 MethodParameterName, 9573 MethodParameterSingleDefaultArgument, 9574 MethodParameterDifferentDefaultArgument, 9575 MethodNoTemplateArguments, 9576 MethodDifferentNumberTemplateArguments, 9577 MethodDifferentTemplateArgument, 9578 MethodSingleBody, 9579 MethodDifferentBody, 9580 TypedefName, 9581 TypedefType, 9582 VarName, 9583 VarType, 9584 VarSingleInitializer, 9585 VarDifferentInitializer, 9586 VarConstexpr, 9587 FriendTypeFunction, 9588 FriendType, 9589 FriendFunction, 9590 FunctionTemplateDifferentNumberParameters, 9591 FunctionTemplateParameterDifferentKind, 9592 FunctionTemplateParameterName, 9593 FunctionTemplateParameterSingleDefaultArgument, 9594 FunctionTemplateParameterDifferentDefaultArgument, 9595 FunctionTemplateParameterDifferentType, 9596 FunctionTemplatePackParameter, 9597 }; 9598 9599 // These lambdas have the common portions of the ODR diagnostics. This 9600 // has the same return as Diag(), so addition parameters can be passed 9601 // in with operator<< 9602 auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule, 9603 SourceLocation Loc, SourceRange Range, 9604 ODRMismatchDeclDifference DiffType) { 9605 return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff) 9606 << FirstRecord << FirstModule.empty() << FirstModule << Range 9607 << DiffType; 9608 }; 9609 auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc, 9610 SourceRange Range, ODRMismatchDeclDifference DiffType) { 9611 return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff) 9612 << SecondModule << Range << DiffType; 9613 }; 9614 9615 auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote, 9616 &ComputeQualTypeODRHash, &ComputeODRHash]( 9617 NamedDecl *FirstRecord, StringRef FirstModule, 9618 StringRef SecondModule, FieldDecl *FirstField, 9619 FieldDecl *SecondField) { 9620 IdentifierInfo *FirstII = FirstField->getIdentifier(); 9621 IdentifierInfo *SecondII = SecondField->getIdentifier(); 9622 if (FirstII->getName() != SecondII->getName()) { 9623 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9624 FirstField->getSourceRange(), FieldName) 9625 << FirstII; 9626 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9627 SecondField->getSourceRange(), FieldName) 9628 << SecondII; 9629 9630 return true; 9631 } 9632 9633 assert(getContext().hasSameType(FirstField->getType(), 9634 SecondField->getType())); 9635 9636 QualType FirstType = FirstField->getType(); 9637 QualType SecondType = SecondField->getType(); 9638 if (ComputeQualTypeODRHash(FirstType) != 9639 ComputeQualTypeODRHash(SecondType)) { 9640 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9641 FirstField->getSourceRange(), FieldTypeName) 9642 << FirstII << FirstType; 9643 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9644 SecondField->getSourceRange(), FieldTypeName) 9645 << SecondII << SecondType; 9646 9647 return true; 9648 } 9649 9650 const bool IsFirstBitField = FirstField->isBitField(); 9651 const bool IsSecondBitField = SecondField->isBitField(); 9652 if (IsFirstBitField != IsSecondBitField) { 9653 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9654 FirstField->getSourceRange(), FieldSingleBitField) 9655 << FirstII << IsFirstBitField; 9656 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9657 SecondField->getSourceRange(), FieldSingleBitField) 9658 << SecondII << IsSecondBitField; 9659 return true; 9660 } 9661 9662 if (IsFirstBitField && IsSecondBitField) { 9663 unsigned FirstBitWidthHash = 9664 ComputeODRHash(FirstField->getBitWidth()); 9665 unsigned SecondBitWidthHash = 9666 ComputeODRHash(SecondField->getBitWidth()); 9667 if (FirstBitWidthHash != SecondBitWidthHash) { 9668 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9669 FirstField->getSourceRange(), 9670 FieldDifferentWidthBitField) 9671 << FirstII << FirstField->getBitWidth()->getSourceRange(); 9672 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9673 SecondField->getSourceRange(), 9674 FieldDifferentWidthBitField) 9675 << SecondII << SecondField->getBitWidth()->getSourceRange(); 9676 return true; 9677 } 9678 } 9679 9680 if (!PP.getLangOpts().CPlusPlus) 9681 return false; 9682 9683 const bool IsFirstMutable = FirstField->isMutable(); 9684 const bool IsSecondMutable = SecondField->isMutable(); 9685 if (IsFirstMutable != IsSecondMutable) { 9686 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9687 FirstField->getSourceRange(), FieldSingleMutable) 9688 << FirstII << IsFirstMutable; 9689 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9690 SecondField->getSourceRange(), FieldSingleMutable) 9691 << SecondII << IsSecondMutable; 9692 return true; 9693 } 9694 9695 const Expr *FirstInitializer = FirstField->getInClassInitializer(); 9696 const Expr *SecondInitializer = SecondField->getInClassInitializer(); 9697 if ((!FirstInitializer && SecondInitializer) || 9698 (FirstInitializer && !SecondInitializer)) { 9699 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9700 FirstField->getSourceRange(), FieldSingleInitializer) 9701 << FirstII << (FirstInitializer != nullptr); 9702 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9703 SecondField->getSourceRange(), FieldSingleInitializer) 9704 << SecondII << (SecondInitializer != nullptr); 9705 return true; 9706 } 9707 9708 if (FirstInitializer && SecondInitializer) { 9709 unsigned FirstInitHash = ComputeODRHash(FirstInitializer); 9710 unsigned SecondInitHash = ComputeODRHash(SecondInitializer); 9711 if (FirstInitHash != SecondInitHash) { 9712 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9713 FirstField->getSourceRange(), 9714 FieldDifferentInitializers) 9715 << FirstII << FirstInitializer->getSourceRange(); 9716 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9717 SecondField->getSourceRange(), 9718 FieldDifferentInitializers) 9719 << SecondII << SecondInitializer->getSourceRange(); 9720 return true; 9721 } 9722 } 9723 9724 return false; 9725 }; 9726 9727 auto ODRDiagTypeDefOrAlias = 9728 [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash]( 9729 NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule, 9730 TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD, 9731 bool IsTypeAlias) { 9732 auto FirstName = FirstTD->getDeclName(); 9733 auto SecondName = SecondTD->getDeclName(); 9734 if (FirstName != SecondName) { 9735 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(), 9736 FirstTD->getSourceRange(), TypedefName) 9737 << IsTypeAlias << FirstName; 9738 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(), 9739 SecondTD->getSourceRange(), TypedefName) 9740 << IsTypeAlias << SecondName; 9741 return true; 9742 } 9743 9744 QualType FirstType = FirstTD->getUnderlyingType(); 9745 QualType SecondType = SecondTD->getUnderlyingType(); 9746 if (ComputeQualTypeODRHash(FirstType) != 9747 ComputeQualTypeODRHash(SecondType)) { 9748 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(), 9749 FirstTD->getSourceRange(), TypedefType) 9750 << IsTypeAlias << FirstName << FirstType; 9751 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(), 9752 SecondTD->getSourceRange(), TypedefType) 9753 << IsTypeAlias << SecondName << SecondType; 9754 return true; 9755 } 9756 9757 return false; 9758 }; 9759 9760 auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote, 9761 &ComputeQualTypeODRHash, &ComputeODRHash, 9762 this](NamedDecl *FirstRecord, StringRef FirstModule, 9763 StringRef SecondModule, VarDecl *FirstVD, 9764 VarDecl *SecondVD) { 9765 auto FirstName = FirstVD->getDeclName(); 9766 auto SecondName = SecondVD->getDeclName(); 9767 if (FirstName != SecondName) { 9768 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9769 FirstVD->getSourceRange(), VarName) 9770 << FirstName; 9771 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9772 SecondVD->getSourceRange(), VarName) 9773 << SecondName; 9774 return true; 9775 } 9776 9777 QualType FirstType = FirstVD->getType(); 9778 QualType SecondType = SecondVD->getType(); 9779 if (ComputeQualTypeODRHash(FirstType) != 9780 ComputeQualTypeODRHash(SecondType)) { 9781 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9782 FirstVD->getSourceRange(), VarType) 9783 << FirstName << FirstType; 9784 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9785 SecondVD->getSourceRange(), VarType) 9786 << SecondName << SecondType; 9787 return true; 9788 } 9789 9790 if (!PP.getLangOpts().CPlusPlus) 9791 return false; 9792 9793 const Expr *FirstInit = FirstVD->getInit(); 9794 const Expr *SecondInit = SecondVD->getInit(); 9795 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 9796 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9797 FirstVD->getSourceRange(), VarSingleInitializer) 9798 << FirstName << (FirstInit == nullptr) 9799 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 9800 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9801 SecondVD->getSourceRange(), VarSingleInitializer) 9802 << SecondName << (SecondInit == nullptr) 9803 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 9804 return true; 9805 } 9806 9807 if (FirstInit && SecondInit && 9808 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 9809 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9810 FirstVD->getSourceRange(), VarDifferentInitializer) 9811 << FirstName << FirstInit->getSourceRange(); 9812 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9813 SecondVD->getSourceRange(), VarDifferentInitializer) 9814 << SecondName << SecondInit->getSourceRange(); 9815 return true; 9816 } 9817 9818 const bool FirstIsConstexpr = FirstVD->isConstexpr(); 9819 const bool SecondIsConstexpr = SecondVD->isConstexpr(); 9820 if (FirstIsConstexpr != SecondIsConstexpr) { 9821 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9822 FirstVD->getSourceRange(), VarConstexpr) 9823 << FirstName << FirstIsConstexpr; 9824 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9825 SecondVD->getSourceRange(), VarConstexpr) 9826 << SecondName << SecondIsConstexpr; 9827 return true; 9828 } 9829 return false; 9830 }; 9831 9832 auto DifferenceSelector = [](Decl *D) { 9833 assert(D && "valid Decl required"); 9834 switch (D->getKind()) { 9835 default: 9836 return Other; 9837 case Decl::AccessSpec: 9838 switch (D->getAccess()) { 9839 case AS_public: 9840 return PublicSpecifer; 9841 case AS_private: 9842 return PrivateSpecifer; 9843 case AS_protected: 9844 return ProtectedSpecifer; 9845 case AS_none: 9846 break; 9847 } 9848 llvm_unreachable("Invalid access specifier"); 9849 case Decl::StaticAssert: 9850 return StaticAssert; 9851 case Decl::Field: 9852 return Field; 9853 case Decl::CXXMethod: 9854 case Decl::CXXConstructor: 9855 case Decl::CXXDestructor: 9856 return CXXMethod; 9857 case Decl::TypeAlias: 9858 return TypeAlias; 9859 case Decl::Typedef: 9860 return TypeDef; 9861 case Decl::Var: 9862 return Var; 9863 case Decl::Friend: 9864 return Friend; 9865 case Decl::FunctionTemplate: 9866 return FunctionTemplate; 9867 } 9868 }; 9869 9870 using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>; 9871 auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes, 9872 RecordDecl *Record, 9873 const DeclContext *DC) { 9874 for (auto *D : Record->decls()) { 9875 if (!ODRHash::isDeclToBeProcessed(D, DC)) 9876 continue; 9877 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 9878 } 9879 }; 9880 9881 struct DiffResult { 9882 Decl *FirstDecl = nullptr, *SecondDecl = nullptr; 9883 ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other; 9884 }; 9885 9886 // If there is a diagnoseable difference, FirstDiffType and 9887 // SecondDiffType will not be Other and FirstDecl and SecondDecl will be 9888 // filled in if not EndOfClass. 9889 auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes, 9890 DeclHashes &SecondHashes) { 9891 DiffResult DR; 9892 auto FirstIt = FirstHashes.begin(); 9893 auto SecondIt = SecondHashes.begin(); 9894 while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) { 9895 if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() && 9896 FirstIt->second == SecondIt->second) { 9897 ++FirstIt; 9898 ++SecondIt; 9899 continue; 9900 } 9901 9902 DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first; 9903 DR.SecondDecl = 9904 SecondIt == SecondHashes.end() ? nullptr : SecondIt->first; 9905 9906 DR.FirstDiffType = 9907 DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass; 9908 DR.SecondDiffType = 9909 DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass; 9910 return DR; 9911 } 9912 return DR; 9913 }; 9914 9915 // Use this to diagnose that an unexpected Decl was encountered 9916 // or no difference was detected. This causes a generic error 9917 // message to be emitted. 9918 auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord, 9919 StringRef FirstModule, 9920 NamedDecl *SecondRecord, 9921 StringRef SecondModule) { 9922 Diag(FirstRecord->getLocation(), 9923 diag::err_module_odr_violation_different_definitions) 9924 << FirstRecord << FirstModule.empty() << FirstModule; 9925 9926 if (DR.FirstDecl) { 9927 Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference) 9928 << FirstRecord << DR.FirstDecl->getSourceRange(); 9929 } 9930 9931 Diag(SecondRecord->getLocation(), 9932 diag::note_module_odr_violation_different_definitions) 9933 << SecondModule; 9934 9935 if (DR.SecondDecl) { 9936 Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference) 9937 << DR.SecondDecl->getSourceRange(); 9938 } 9939 }; 9940 9941 auto DiagnoseODRMismatch = 9942 [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule, 9943 NamedDecl *SecondRecord, StringRef SecondModule) { 9944 SourceLocation FirstLoc; 9945 SourceRange FirstRange; 9946 auto *FirstTag = dyn_cast<TagDecl>(FirstRecord); 9947 if (DR.FirstDiffType == EndOfClass && FirstTag) { 9948 FirstLoc = FirstTag->getBraceRange().getEnd(); 9949 } else { 9950 FirstLoc = DR.FirstDecl->getLocation(); 9951 FirstRange = DR.FirstDecl->getSourceRange(); 9952 } 9953 Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl) 9954 << FirstRecord << FirstModule.empty() << FirstModule << FirstRange 9955 << DR.FirstDiffType; 9956 9957 SourceLocation SecondLoc; 9958 SourceRange SecondRange; 9959 auto *SecondTag = dyn_cast<TagDecl>(SecondRecord); 9960 if (DR.SecondDiffType == EndOfClass && SecondTag) { 9961 SecondLoc = SecondTag->getBraceRange().getEnd(); 9962 } else { 9963 SecondLoc = DR.SecondDecl->getLocation(); 9964 SecondRange = DR.SecondDecl->getSourceRange(); 9965 } 9966 Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl) 9967 << SecondModule << SecondRange << DR.SecondDiffType; 9968 }; 9969 9970 // Issue any pending ODR-failure diagnostics. 9971 for (auto &Merge : OdrMergeFailures) { 9972 // If we've already pointed out a specific problem with this class, don't 9973 // bother issuing a general "something's different" diagnostic. 9974 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 9975 continue; 9976 9977 bool Diagnosed = false; 9978 CXXRecordDecl *FirstRecord = Merge.first; 9979 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord); 9980 for (auto &RecordPair : Merge.second) { 9981 CXXRecordDecl *SecondRecord = RecordPair.first; 9982 // Multiple different declarations got merged together; tell the user 9983 // where they came from. 9984 if (FirstRecord == SecondRecord) 9985 continue; 9986 9987 std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord); 9988 9989 auto *FirstDD = FirstRecord->DefinitionData; 9990 auto *SecondDD = RecordPair.second; 9991 9992 assert(FirstDD && SecondDD && "Definitions without DefinitionData"); 9993 9994 // Diagnostics from DefinitionData are emitted here. 9995 if (FirstDD != SecondDD) { 9996 enum ODRDefinitionDataDifference { 9997 NumBases, 9998 NumVBases, 9999 BaseType, 10000 BaseVirtual, 10001 BaseAccess, 10002 }; 10003 auto ODRDiagBaseError = [FirstRecord, &FirstModule, 10004 this](SourceLocation Loc, SourceRange Range, 10005 ODRDefinitionDataDifference DiffType) { 10006 return Diag(Loc, diag::err_module_odr_violation_definition_data) 10007 << FirstRecord << FirstModule.empty() << FirstModule << Range 10008 << DiffType; 10009 }; 10010 auto ODRDiagBaseNote = [&SecondModule, 10011 this](SourceLocation Loc, SourceRange Range, 10012 ODRDefinitionDataDifference DiffType) { 10013 return Diag(Loc, diag::note_module_odr_violation_definition_data) 10014 << SecondModule << Range << DiffType; 10015 }; 10016 10017 unsigned FirstNumBases = FirstDD->NumBases; 10018 unsigned FirstNumVBases = FirstDD->NumVBases; 10019 unsigned SecondNumBases = SecondDD->NumBases; 10020 unsigned SecondNumVBases = SecondDD->NumVBases; 10021 10022 auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) { 10023 unsigned NumBases = DD->NumBases; 10024 if (NumBases == 0) return SourceRange(); 10025 auto bases = DD->bases(); 10026 return SourceRange(bases[0].getBeginLoc(), 10027 bases[NumBases - 1].getEndLoc()); 10028 }; 10029 10030 if (FirstNumBases != SecondNumBases) { 10031 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10032 NumBases) 10033 << FirstNumBases; 10034 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10035 NumBases) 10036 << SecondNumBases; 10037 Diagnosed = true; 10038 break; 10039 } 10040 10041 if (FirstNumVBases != SecondNumVBases) { 10042 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10043 NumVBases) 10044 << FirstNumVBases; 10045 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10046 NumVBases) 10047 << SecondNumVBases; 10048 Diagnosed = true; 10049 break; 10050 } 10051 10052 auto FirstBases = FirstDD->bases(); 10053 auto SecondBases = SecondDD->bases(); 10054 unsigned i = 0; 10055 for (i = 0; i < FirstNumBases; ++i) { 10056 auto FirstBase = FirstBases[i]; 10057 auto SecondBase = SecondBases[i]; 10058 if (ComputeQualTypeODRHash(FirstBase.getType()) != 10059 ComputeQualTypeODRHash(SecondBase.getType())) { 10060 ODRDiagBaseError(FirstRecord->getLocation(), 10061 FirstBase.getSourceRange(), BaseType) 10062 << (i + 1) << FirstBase.getType(); 10063 ODRDiagBaseNote(SecondRecord->getLocation(), 10064 SecondBase.getSourceRange(), BaseType) 10065 << (i + 1) << SecondBase.getType(); 10066 break; 10067 } 10068 10069 if (FirstBase.isVirtual() != SecondBase.isVirtual()) { 10070 ODRDiagBaseError(FirstRecord->getLocation(), 10071 FirstBase.getSourceRange(), BaseVirtual) 10072 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType(); 10073 ODRDiagBaseNote(SecondRecord->getLocation(), 10074 SecondBase.getSourceRange(), BaseVirtual) 10075 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType(); 10076 break; 10077 } 10078 10079 if (FirstBase.getAccessSpecifierAsWritten() != 10080 SecondBase.getAccessSpecifierAsWritten()) { 10081 ODRDiagBaseError(FirstRecord->getLocation(), 10082 FirstBase.getSourceRange(), BaseAccess) 10083 << (i + 1) << FirstBase.getType() 10084 << (int)FirstBase.getAccessSpecifierAsWritten(); 10085 ODRDiagBaseNote(SecondRecord->getLocation(), 10086 SecondBase.getSourceRange(), BaseAccess) 10087 << (i + 1) << SecondBase.getType() 10088 << (int)SecondBase.getAccessSpecifierAsWritten(); 10089 break; 10090 } 10091 } 10092 10093 if (i != FirstNumBases) { 10094 Diagnosed = true; 10095 break; 10096 } 10097 } 10098 10099 const ClassTemplateDecl *FirstTemplate = 10100 FirstRecord->getDescribedClassTemplate(); 10101 const ClassTemplateDecl *SecondTemplate = 10102 SecondRecord->getDescribedClassTemplate(); 10103 10104 assert(!FirstTemplate == !SecondTemplate && 10105 "Both pointers should be null or non-null"); 10106 10107 enum ODRTemplateDifference { 10108 ParamEmptyName, 10109 ParamName, 10110 ParamSingleDefaultArgument, 10111 ParamDifferentDefaultArgument, 10112 }; 10113 10114 if (FirstTemplate && SecondTemplate) { 10115 DeclHashes FirstTemplateHashes; 10116 DeclHashes SecondTemplateHashes; 10117 10118 auto PopulateTemplateParameterHashs = 10119 [&ComputeSubDeclODRHash](DeclHashes &Hashes, 10120 const ClassTemplateDecl *TD) { 10121 for (auto *D : TD->getTemplateParameters()->asArray()) { 10122 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 10123 } 10124 }; 10125 10126 PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate); 10127 PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate); 10128 10129 assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() && 10130 "Number of template parameters should be equal."); 10131 10132 auto FirstIt = FirstTemplateHashes.begin(); 10133 auto FirstEnd = FirstTemplateHashes.end(); 10134 auto SecondIt = SecondTemplateHashes.begin(); 10135 for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) { 10136 if (FirstIt->second == SecondIt->second) 10137 continue; 10138 10139 auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this]( 10140 SourceLocation Loc, SourceRange Range, 10141 ODRTemplateDifference DiffType) { 10142 return Diag(Loc, diag::err_module_odr_violation_template_parameter) 10143 << FirstRecord << FirstModule.empty() << FirstModule << Range 10144 << DiffType; 10145 }; 10146 auto ODRDiagTemplateNote = [&SecondModule, this]( 10147 SourceLocation Loc, SourceRange Range, 10148 ODRTemplateDifference DiffType) { 10149 return Diag(Loc, diag::note_module_odr_violation_template_parameter) 10150 << SecondModule << Range << DiffType; 10151 }; 10152 10153 const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first); 10154 const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first); 10155 10156 assert(FirstDecl->getKind() == SecondDecl->getKind() && 10157 "Parameter Decl's should be the same kind."); 10158 10159 DeclarationName FirstName = FirstDecl->getDeclName(); 10160 DeclarationName SecondName = SecondDecl->getDeclName(); 10161 10162 if (FirstName != SecondName) { 10163 const bool FirstNameEmpty = 10164 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo(); 10165 const bool SecondNameEmpty = 10166 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo(); 10167 assert((!FirstNameEmpty || !SecondNameEmpty) && 10168 "Both template parameters cannot be unnamed."); 10169 ODRDiagTemplateError(FirstDecl->getLocation(), 10170 FirstDecl->getSourceRange(), 10171 FirstNameEmpty ? ParamEmptyName : ParamName) 10172 << FirstName; 10173 ODRDiagTemplateNote(SecondDecl->getLocation(), 10174 SecondDecl->getSourceRange(), 10175 SecondNameEmpty ? ParamEmptyName : ParamName) 10176 << SecondName; 10177 break; 10178 } 10179 10180 switch (FirstDecl->getKind()) { 10181 default: 10182 llvm_unreachable("Invalid template parameter type."); 10183 case Decl::TemplateTypeParm: { 10184 const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl); 10185 const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl); 10186 const bool HasFirstDefaultArgument = 10187 FirstParam->hasDefaultArgument() && 10188 !FirstParam->defaultArgumentWasInherited(); 10189 const bool HasSecondDefaultArgument = 10190 SecondParam->hasDefaultArgument() && 10191 !SecondParam->defaultArgumentWasInherited(); 10192 10193 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10194 ODRDiagTemplateError(FirstDecl->getLocation(), 10195 FirstDecl->getSourceRange(), 10196 ParamSingleDefaultArgument) 10197 << HasFirstDefaultArgument; 10198 ODRDiagTemplateNote(SecondDecl->getLocation(), 10199 SecondDecl->getSourceRange(), 10200 ParamSingleDefaultArgument) 10201 << HasSecondDefaultArgument; 10202 break; 10203 } 10204 10205 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10206 "Expecting default arguments."); 10207 10208 ODRDiagTemplateError(FirstDecl->getLocation(), 10209 FirstDecl->getSourceRange(), 10210 ParamDifferentDefaultArgument); 10211 ODRDiagTemplateNote(SecondDecl->getLocation(), 10212 SecondDecl->getSourceRange(), 10213 ParamDifferentDefaultArgument); 10214 10215 break; 10216 } 10217 case Decl::NonTypeTemplateParm: { 10218 const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl); 10219 const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl); 10220 const bool HasFirstDefaultArgument = 10221 FirstParam->hasDefaultArgument() && 10222 !FirstParam->defaultArgumentWasInherited(); 10223 const bool HasSecondDefaultArgument = 10224 SecondParam->hasDefaultArgument() && 10225 !SecondParam->defaultArgumentWasInherited(); 10226 10227 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10228 ODRDiagTemplateError(FirstDecl->getLocation(), 10229 FirstDecl->getSourceRange(), 10230 ParamSingleDefaultArgument) 10231 << HasFirstDefaultArgument; 10232 ODRDiagTemplateNote(SecondDecl->getLocation(), 10233 SecondDecl->getSourceRange(), 10234 ParamSingleDefaultArgument) 10235 << HasSecondDefaultArgument; 10236 break; 10237 } 10238 10239 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10240 "Expecting default arguments."); 10241 10242 ODRDiagTemplateError(FirstDecl->getLocation(), 10243 FirstDecl->getSourceRange(), 10244 ParamDifferentDefaultArgument); 10245 ODRDiagTemplateNote(SecondDecl->getLocation(), 10246 SecondDecl->getSourceRange(), 10247 ParamDifferentDefaultArgument); 10248 10249 break; 10250 } 10251 case Decl::TemplateTemplateParm: { 10252 const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl); 10253 const auto *SecondParam = 10254 cast<TemplateTemplateParmDecl>(SecondDecl); 10255 const bool HasFirstDefaultArgument = 10256 FirstParam->hasDefaultArgument() && 10257 !FirstParam->defaultArgumentWasInherited(); 10258 const bool HasSecondDefaultArgument = 10259 SecondParam->hasDefaultArgument() && 10260 !SecondParam->defaultArgumentWasInherited(); 10261 10262 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10263 ODRDiagTemplateError(FirstDecl->getLocation(), 10264 FirstDecl->getSourceRange(), 10265 ParamSingleDefaultArgument) 10266 << HasFirstDefaultArgument; 10267 ODRDiagTemplateNote(SecondDecl->getLocation(), 10268 SecondDecl->getSourceRange(), 10269 ParamSingleDefaultArgument) 10270 << HasSecondDefaultArgument; 10271 break; 10272 } 10273 10274 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10275 "Expecting default arguments."); 10276 10277 ODRDiagTemplateError(FirstDecl->getLocation(), 10278 FirstDecl->getSourceRange(), 10279 ParamDifferentDefaultArgument); 10280 ODRDiagTemplateNote(SecondDecl->getLocation(), 10281 SecondDecl->getSourceRange(), 10282 ParamDifferentDefaultArgument); 10283 10284 break; 10285 } 10286 } 10287 10288 break; 10289 } 10290 10291 if (FirstIt != FirstEnd) { 10292 Diagnosed = true; 10293 break; 10294 } 10295 } 10296 10297 DeclHashes FirstHashes; 10298 DeclHashes SecondHashes; 10299 const DeclContext *DC = FirstRecord; 10300 PopulateHashes(FirstHashes, FirstRecord, DC); 10301 PopulateHashes(SecondHashes, SecondRecord, DC); 10302 10303 auto DR = FindTypeDiffs(FirstHashes, SecondHashes); 10304 ODRMismatchDecl FirstDiffType = DR.FirstDiffType; 10305 ODRMismatchDecl SecondDiffType = DR.SecondDiffType; 10306 Decl *FirstDecl = DR.FirstDecl; 10307 Decl *SecondDecl = DR.SecondDecl; 10308 10309 if (FirstDiffType == Other || SecondDiffType == Other) { 10310 DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord, 10311 SecondModule); 10312 Diagnosed = true; 10313 break; 10314 } 10315 10316 if (FirstDiffType != SecondDiffType) { 10317 DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord, 10318 SecondModule); 10319 Diagnosed = true; 10320 break; 10321 } 10322 10323 assert(FirstDiffType == SecondDiffType); 10324 10325 switch (FirstDiffType) { 10326 case Other: 10327 case EndOfClass: 10328 case PublicSpecifer: 10329 case PrivateSpecifer: 10330 case ProtectedSpecifer: 10331 llvm_unreachable("Invalid diff type"); 10332 10333 case StaticAssert: { 10334 StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl); 10335 StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl); 10336 10337 Expr *FirstExpr = FirstSA->getAssertExpr(); 10338 Expr *SecondExpr = SecondSA->getAssertExpr(); 10339 unsigned FirstODRHash = ComputeODRHash(FirstExpr); 10340 unsigned SecondODRHash = ComputeODRHash(SecondExpr); 10341 if (FirstODRHash != SecondODRHash) { 10342 ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(), 10343 FirstExpr->getSourceRange(), StaticAssertCondition); 10344 ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(), 10345 SecondExpr->getSourceRange(), StaticAssertCondition); 10346 Diagnosed = true; 10347 break; 10348 } 10349 10350 StringLiteral *FirstStr = FirstSA->getMessage(); 10351 StringLiteral *SecondStr = SecondSA->getMessage(); 10352 assert((FirstStr || SecondStr) && "Both messages cannot be empty"); 10353 if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) { 10354 SourceLocation FirstLoc, SecondLoc; 10355 SourceRange FirstRange, SecondRange; 10356 if (FirstStr) { 10357 FirstLoc = FirstStr->getBeginLoc(); 10358 FirstRange = FirstStr->getSourceRange(); 10359 } else { 10360 FirstLoc = FirstSA->getBeginLoc(); 10361 FirstRange = FirstSA->getSourceRange(); 10362 } 10363 if (SecondStr) { 10364 SecondLoc = SecondStr->getBeginLoc(); 10365 SecondRange = SecondStr->getSourceRange(); 10366 } else { 10367 SecondLoc = SecondSA->getBeginLoc(); 10368 SecondRange = SecondSA->getSourceRange(); 10369 } 10370 ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange, 10371 StaticAssertOnlyMessage) 10372 << (FirstStr == nullptr); 10373 ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange, 10374 StaticAssertOnlyMessage) 10375 << (SecondStr == nullptr); 10376 Diagnosed = true; 10377 break; 10378 } 10379 10380 if (FirstStr && SecondStr && 10381 FirstStr->getString() != SecondStr->getString()) { 10382 ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(), 10383 FirstStr->getSourceRange(), StaticAssertMessage); 10384 ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(), 10385 SecondStr->getSourceRange(), StaticAssertMessage); 10386 Diagnosed = true; 10387 break; 10388 } 10389 break; 10390 } 10391 case Field: { 10392 Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule, 10393 cast<FieldDecl>(FirstDecl), 10394 cast<FieldDecl>(SecondDecl)); 10395 break; 10396 } 10397 case CXXMethod: { 10398 enum { 10399 DiagMethod, 10400 DiagConstructor, 10401 DiagDestructor, 10402 } FirstMethodType, 10403 SecondMethodType; 10404 auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) { 10405 if (isa<CXXConstructorDecl>(D)) return DiagConstructor; 10406 if (isa<CXXDestructorDecl>(D)) return DiagDestructor; 10407 return DiagMethod; 10408 }; 10409 const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl); 10410 const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl); 10411 FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod); 10412 SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod); 10413 auto FirstName = FirstMethod->getDeclName(); 10414 auto SecondName = SecondMethod->getDeclName(); 10415 if (FirstMethodType != SecondMethodType || FirstName != SecondName) { 10416 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10417 FirstMethod->getSourceRange(), MethodName) 10418 << FirstMethodType << FirstName; 10419 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10420 SecondMethod->getSourceRange(), MethodName) 10421 << SecondMethodType << SecondName; 10422 10423 Diagnosed = true; 10424 break; 10425 } 10426 10427 const bool FirstDeleted = FirstMethod->isDeletedAsWritten(); 10428 const bool SecondDeleted = SecondMethod->isDeletedAsWritten(); 10429 if (FirstDeleted != SecondDeleted) { 10430 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10431 FirstMethod->getSourceRange(), MethodDeleted) 10432 << FirstMethodType << FirstName << FirstDeleted; 10433 10434 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10435 SecondMethod->getSourceRange(), MethodDeleted) 10436 << SecondMethodType << SecondName << SecondDeleted; 10437 Diagnosed = true; 10438 break; 10439 } 10440 10441 const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted(); 10442 const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted(); 10443 if (FirstDefaulted != SecondDefaulted) { 10444 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10445 FirstMethod->getSourceRange(), MethodDefaulted) 10446 << FirstMethodType << FirstName << FirstDefaulted; 10447 10448 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10449 SecondMethod->getSourceRange(), MethodDefaulted) 10450 << SecondMethodType << SecondName << SecondDefaulted; 10451 Diagnosed = true; 10452 break; 10453 } 10454 10455 const bool FirstVirtual = FirstMethod->isVirtualAsWritten(); 10456 const bool SecondVirtual = SecondMethod->isVirtualAsWritten(); 10457 const bool FirstPure = FirstMethod->isPure(); 10458 const bool SecondPure = SecondMethod->isPure(); 10459 if ((FirstVirtual || SecondVirtual) && 10460 (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) { 10461 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10462 FirstMethod->getSourceRange(), MethodVirtual) 10463 << FirstMethodType << FirstName << FirstPure << FirstVirtual; 10464 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10465 SecondMethod->getSourceRange(), MethodVirtual) 10466 << SecondMethodType << SecondName << SecondPure << SecondVirtual; 10467 Diagnosed = true; 10468 break; 10469 } 10470 10471 // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging, 10472 // FirstDecl is the canonical Decl of SecondDecl, so the storage 10473 // class needs to be checked instead. 10474 const auto FirstStorage = FirstMethod->getStorageClass(); 10475 const auto SecondStorage = SecondMethod->getStorageClass(); 10476 const bool FirstStatic = FirstStorage == SC_Static; 10477 const bool SecondStatic = SecondStorage == SC_Static; 10478 if (FirstStatic != SecondStatic) { 10479 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10480 FirstMethod->getSourceRange(), MethodStatic) 10481 << FirstMethodType << FirstName << FirstStatic; 10482 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10483 SecondMethod->getSourceRange(), MethodStatic) 10484 << SecondMethodType << SecondName << SecondStatic; 10485 Diagnosed = true; 10486 break; 10487 } 10488 10489 const bool FirstVolatile = FirstMethod->isVolatile(); 10490 const bool SecondVolatile = SecondMethod->isVolatile(); 10491 if (FirstVolatile != SecondVolatile) { 10492 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10493 FirstMethod->getSourceRange(), MethodVolatile) 10494 << FirstMethodType << FirstName << FirstVolatile; 10495 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10496 SecondMethod->getSourceRange(), MethodVolatile) 10497 << SecondMethodType << SecondName << SecondVolatile; 10498 Diagnosed = true; 10499 break; 10500 } 10501 10502 const bool FirstConst = FirstMethod->isConst(); 10503 const bool SecondConst = SecondMethod->isConst(); 10504 if (FirstConst != SecondConst) { 10505 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10506 FirstMethod->getSourceRange(), MethodConst) 10507 << FirstMethodType << FirstName << FirstConst; 10508 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10509 SecondMethod->getSourceRange(), MethodConst) 10510 << SecondMethodType << SecondName << SecondConst; 10511 Diagnosed = true; 10512 break; 10513 } 10514 10515 const bool FirstInline = FirstMethod->isInlineSpecified(); 10516 const bool SecondInline = SecondMethod->isInlineSpecified(); 10517 if (FirstInline != SecondInline) { 10518 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10519 FirstMethod->getSourceRange(), MethodInline) 10520 << FirstMethodType << FirstName << FirstInline; 10521 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10522 SecondMethod->getSourceRange(), MethodInline) 10523 << SecondMethodType << SecondName << SecondInline; 10524 Diagnosed = true; 10525 break; 10526 } 10527 10528 const unsigned FirstNumParameters = FirstMethod->param_size(); 10529 const unsigned SecondNumParameters = SecondMethod->param_size(); 10530 if (FirstNumParameters != SecondNumParameters) { 10531 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10532 FirstMethod->getSourceRange(), 10533 MethodNumberParameters) 10534 << FirstMethodType << FirstName << FirstNumParameters; 10535 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10536 SecondMethod->getSourceRange(), 10537 MethodNumberParameters) 10538 << SecondMethodType << SecondName << SecondNumParameters; 10539 Diagnosed = true; 10540 break; 10541 } 10542 10543 // Need this status boolean to know when break out of the switch. 10544 bool ParameterMismatch = false; 10545 for (unsigned I = 0; I < FirstNumParameters; ++I) { 10546 const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I); 10547 const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I); 10548 10549 QualType FirstParamType = FirstParam->getType(); 10550 QualType SecondParamType = SecondParam->getType(); 10551 if (FirstParamType != SecondParamType && 10552 ComputeQualTypeODRHash(FirstParamType) != 10553 ComputeQualTypeODRHash(SecondParamType)) { 10554 if (const DecayedType *ParamDecayedType = 10555 FirstParamType->getAs<DecayedType>()) { 10556 ODRDiagDeclError( 10557 FirstRecord, FirstModule, FirstMethod->getLocation(), 10558 FirstMethod->getSourceRange(), MethodParameterType) 10559 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10560 << true << ParamDecayedType->getOriginalType(); 10561 } else { 10562 ODRDiagDeclError( 10563 FirstRecord, FirstModule, FirstMethod->getLocation(), 10564 FirstMethod->getSourceRange(), MethodParameterType) 10565 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10566 << false; 10567 } 10568 10569 if (const DecayedType *ParamDecayedType = 10570 SecondParamType->getAs<DecayedType>()) { 10571 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10572 SecondMethod->getSourceRange(), 10573 MethodParameterType) 10574 << SecondMethodType << SecondName << (I + 1) 10575 << SecondParamType << true 10576 << ParamDecayedType->getOriginalType(); 10577 } else { 10578 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10579 SecondMethod->getSourceRange(), 10580 MethodParameterType) 10581 << SecondMethodType << SecondName << (I + 1) 10582 << SecondParamType << false; 10583 } 10584 ParameterMismatch = true; 10585 break; 10586 } 10587 10588 DeclarationName FirstParamName = FirstParam->getDeclName(); 10589 DeclarationName SecondParamName = SecondParam->getDeclName(); 10590 if (FirstParamName != SecondParamName) { 10591 ODRDiagDeclError(FirstRecord, FirstModule, 10592 FirstMethod->getLocation(), 10593 FirstMethod->getSourceRange(), MethodParameterName) 10594 << FirstMethodType << FirstName << (I + 1) << FirstParamName; 10595 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10596 SecondMethod->getSourceRange(), MethodParameterName) 10597 << SecondMethodType << SecondName << (I + 1) << SecondParamName; 10598 ParameterMismatch = true; 10599 break; 10600 } 10601 10602 const Expr *FirstInit = FirstParam->getInit(); 10603 const Expr *SecondInit = SecondParam->getInit(); 10604 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 10605 ODRDiagDeclError(FirstRecord, FirstModule, 10606 FirstMethod->getLocation(), 10607 FirstMethod->getSourceRange(), 10608 MethodParameterSingleDefaultArgument) 10609 << FirstMethodType << FirstName << (I + 1) 10610 << (FirstInit == nullptr) 10611 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 10612 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10613 SecondMethod->getSourceRange(), 10614 MethodParameterSingleDefaultArgument) 10615 << SecondMethodType << SecondName << (I + 1) 10616 << (SecondInit == nullptr) 10617 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 10618 ParameterMismatch = true; 10619 break; 10620 } 10621 10622 if (FirstInit && SecondInit && 10623 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 10624 ODRDiagDeclError(FirstRecord, FirstModule, 10625 FirstMethod->getLocation(), 10626 FirstMethod->getSourceRange(), 10627 MethodParameterDifferentDefaultArgument) 10628 << FirstMethodType << FirstName << (I + 1) 10629 << FirstInit->getSourceRange(); 10630 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10631 SecondMethod->getSourceRange(), 10632 MethodParameterDifferentDefaultArgument) 10633 << SecondMethodType << SecondName << (I + 1) 10634 << SecondInit->getSourceRange(); 10635 ParameterMismatch = true; 10636 break; 10637 10638 } 10639 } 10640 10641 if (ParameterMismatch) { 10642 Diagnosed = true; 10643 break; 10644 } 10645 10646 const auto *FirstTemplateArgs = 10647 FirstMethod->getTemplateSpecializationArgs(); 10648 const auto *SecondTemplateArgs = 10649 SecondMethod->getTemplateSpecializationArgs(); 10650 10651 if ((FirstTemplateArgs && !SecondTemplateArgs) || 10652 (!FirstTemplateArgs && SecondTemplateArgs)) { 10653 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10654 FirstMethod->getSourceRange(), 10655 MethodNoTemplateArguments) 10656 << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr); 10657 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10658 SecondMethod->getSourceRange(), 10659 MethodNoTemplateArguments) 10660 << SecondMethodType << SecondName 10661 << (SecondTemplateArgs != nullptr); 10662 10663 Diagnosed = true; 10664 break; 10665 } 10666 10667 if (FirstTemplateArgs && SecondTemplateArgs) { 10668 // Remove pack expansions from argument list. 10669 auto ExpandTemplateArgumentList = 10670 [](const TemplateArgumentList *TAL) { 10671 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList; 10672 for (const TemplateArgument &TA : TAL->asArray()) { 10673 if (TA.getKind() != TemplateArgument::Pack) { 10674 ExpandedList.push_back(&TA); 10675 continue; 10676 } 10677 for (const TemplateArgument &PackTA : TA.getPackAsArray()) { 10678 ExpandedList.push_back(&PackTA); 10679 } 10680 } 10681 return ExpandedList; 10682 }; 10683 llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList = 10684 ExpandTemplateArgumentList(FirstTemplateArgs); 10685 llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList = 10686 ExpandTemplateArgumentList(SecondTemplateArgs); 10687 10688 if (FirstExpandedList.size() != SecondExpandedList.size()) { 10689 ODRDiagDeclError(FirstRecord, FirstModule, 10690 FirstMethod->getLocation(), 10691 FirstMethod->getSourceRange(), 10692 MethodDifferentNumberTemplateArguments) 10693 << FirstMethodType << FirstName 10694 << (unsigned)FirstExpandedList.size(); 10695 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10696 SecondMethod->getSourceRange(), 10697 MethodDifferentNumberTemplateArguments) 10698 << SecondMethodType << SecondName 10699 << (unsigned)SecondExpandedList.size(); 10700 10701 Diagnosed = true; 10702 break; 10703 } 10704 10705 bool TemplateArgumentMismatch = false; 10706 for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) { 10707 const TemplateArgument &FirstTA = *FirstExpandedList[i], 10708 &SecondTA = *SecondExpandedList[i]; 10709 if (ComputeTemplateArgumentODRHash(FirstTA) == 10710 ComputeTemplateArgumentODRHash(SecondTA)) { 10711 continue; 10712 } 10713 10714 ODRDiagDeclError( 10715 FirstRecord, FirstModule, FirstMethod->getLocation(), 10716 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument) 10717 << FirstMethodType << FirstName << FirstTA << i + 1; 10718 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10719 SecondMethod->getSourceRange(), 10720 MethodDifferentTemplateArgument) 10721 << SecondMethodType << SecondName << SecondTA << i + 1; 10722 10723 TemplateArgumentMismatch = true; 10724 break; 10725 } 10726 10727 if (TemplateArgumentMismatch) { 10728 Diagnosed = true; 10729 break; 10730 } 10731 } 10732 10733 // Compute the hash of the method as if it has no body. 10734 auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) { 10735 Hash.clear(); 10736 Hash.AddFunctionDecl(D, true /*SkipBody*/); 10737 return Hash.CalculateHash(); 10738 }; 10739 10740 // Compare the hash generated to the hash stored. A difference means 10741 // that a body was present in the original source. Due to merging, 10742 // the stardard way of detecting a body will not work. 10743 const bool HasFirstBody = 10744 ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash(); 10745 const bool HasSecondBody = 10746 ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash(); 10747 10748 if (HasFirstBody != HasSecondBody) { 10749 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10750 FirstMethod->getSourceRange(), MethodSingleBody) 10751 << FirstMethodType << FirstName << HasFirstBody; 10752 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10753 SecondMethod->getSourceRange(), MethodSingleBody) 10754 << SecondMethodType << SecondName << HasSecondBody; 10755 Diagnosed = true; 10756 break; 10757 } 10758 10759 if (HasFirstBody && HasSecondBody) { 10760 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10761 FirstMethod->getSourceRange(), MethodDifferentBody) 10762 << FirstMethodType << FirstName; 10763 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10764 SecondMethod->getSourceRange(), MethodDifferentBody) 10765 << SecondMethodType << SecondName; 10766 Diagnosed = true; 10767 break; 10768 } 10769 10770 break; 10771 } 10772 case TypeAlias: 10773 case TypeDef: { 10774 Diagnosed = ODRDiagTypeDefOrAlias( 10775 FirstRecord, FirstModule, SecondModule, 10776 cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl), 10777 FirstDiffType == TypeAlias); 10778 break; 10779 } 10780 case Var: { 10781 Diagnosed = 10782 ODRDiagVar(FirstRecord, FirstModule, SecondModule, 10783 cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl)); 10784 break; 10785 } 10786 case Friend: { 10787 FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl); 10788 FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl); 10789 10790 NamedDecl *FirstND = FirstFriend->getFriendDecl(); 10791 NamedDecl *SecondND = SecondFriend->getFriendDecl(); 10792 10793 TypeSourceInfo *FirstTSI = FirstFriend->getFriendType(); 10794 TypeSourceInfo *SecondTSI = SecondFriend->getFriendType(); 10795 10796 if (FirstND && SecondND) { 10797 ODRDiagDeclError(FirstRecord, FirstModule, 10798 FirstFriend->getFriendLoc(), 10799 FirstFriend->getSourceRange(), FriendFunction) 10800 << FirstND; 10801 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10802 SecondFriend->getSourceRange(), FriendFunction) 10803 << SecondND; 10804 10805 Diagnosed = true; 10806 break; 10807 } 10808 10809 if (FirstTSI && SecondTSI) { 10810 QualType FirstFriendType = FirstTSI->getType(); 10811 QualType SecondFriendType = SecondTSI->getType(); 10812 assert(ComputeQualTypeODRHash(FirstFriendType) != 10813 ComputeQualTypeODRHash(SecondFriendType)); 10814 ODRDiagDeclError(FirstRecord, FirstModule, 10815 FirstFriend->getFriendLoc(), 10816 FirstFriend->getSourceRange(), FriendType) 10817 << FirstFriendType; 10818 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10819 SecondFriend->getSourceRange(), FriendType) 10820 << SecondFriendType; 10821 Diagnosed = true; 10822 break; 10823 } 10824 10825 ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(), 10826 FirstFriend->getSourceRange(), FriendTypeFunction) 10827 << (FirstTSI == nullptr); 10828 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10829 SecondFriend->getSourceRange(), FriendTypeFunction) 10830 << (SecondTSI == nullptr); 10831 10832 Diagnosed = true; 10833 break; 10834 } 10835 case FunctionTemplate: { 10836 FunctionTemplateDecl *FirstTemplate = 10837 cast<FunctionTemplateDecl>(FirstDecl); 10838 FunctionTemplateDecl *SecondTemplate = 10839 cast<FunctionTemplateDecl>(SecondDecl); 10840 10841 TemplateParameterList *FirstTPL = 10842 FirstTemplate->getTemplateParameters(); 10843 TemplateParameterList *SecondTPL = 10844 SecondTemplate->getTemplateParameters(); 10845 10846 if (FirstTPL->size() != SecondTPL->size()) { 10847 ODRDiagDeclError(FirstRecord, FirstModule, 10848 FirstTemplate->getLocation(), 10849 FirstTemplate->getSourceRange(), 10850 FunctionTemplateDifferentNumberParameters) 10851 << FirstTemplate << FirstTPL->size(); 10852 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10853 SecondTemplate->getSourceRange(), 10854 FunctionTemplateDifferentNumberParameters) 10855 << SecondTemplate << SecondTPL->size(); 10856 10857 Diagnosed = true; 10858 break; 10859 } 10860 10861 bool ParameterMismatch = false; 10862 for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) { 10863 NamedDecl *FirstParam = FirstTPL->getParam(i); 10864 NamedDecl *SecondParam = SecondTPL->getParam(i); 10865 10866 if (FirstParam->getKind() != SecondParam->getKind()) { 10867 enum { 10868 TemplateTypeParameter, 10869 NonTypeTemplateParameter, 10870 TemplateTemplateParameter, 10871 }; 10872 auto GetParamType = [](NamedDecl *D) { 10873 switch (D->getKind()) { 10874 default: 10875 llvm_unreachable("Unexpected template parameter type"); 10876 case Decl::TemplateTypeParm: 10877 return TemplateTypeParameter; 10878 case Decl::NonTypeTemplateParm: 10879 return NonTypeTemplateParameter; 10880 case Decl::TemplateTemplateParm: 10881 return TemplateTemplateParameter; 10882 } 10883 }; 10884 10885 ODRDiagDeclError(FirstRecord, FirstModule, 10886 FirstTemplate->getLocation(), 10887 FirstTemplate->getSourceRange(), 10888 FunctionTemplateParameterDifferentKind) 10889 << FirstTemplate << (i + 1) << GetParamType(FirstParam); 10890 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10891 SecondTemplate->getSourceRange(), 10892 FunctionTemplateParameterDifferentKind) 10893 << SecondTemplate << (i + 1) << GetParamType(SecondParam); 10894 10895 ParameterMismatch = true; 10896 break; 10897 } 10898 10899 if (FirstParam->getName() != SecondParam->getName()) { 10900 ODRDiagDeclError( 10901 FirstRecord, FirstModule, FirstTemplate->getLocation(), 10902 FirstTemplate->getSourceRange(), FunctionTemplateParameterName) 10903 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier() 10904 << FirstParam; 10905 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10906 SecondTemplate->getSourceRange(), 10907 FunctionTemplateParameterName) 10908 << SecondTemplate << (i + 1) 10909 << (bool)SecondParam->getIdentifier() << SecondParam; 10910 ParameterMismatch = true; 10911 break; 10912 } 10913 10914 if (isa<TemplateTypeParmDecl>(FirstParam) && 10915 isa<TemplateTypeParmDecl>(SecondParam)) { 10916 TemplateTypeParmDecl *FirstTTPD = 10917 cast<TemplateTypeParmDecl>(FirstParam); 10918 TemplateTypeParmDecl *SecondTTPD = 10919 cast<TemplateTypeParmDecl>(SecondParam); 10920 bool HasFirstDefaultArgument = 10921 FirstTTPD->hasDefaultArgument() && 10922 !FirstTTPD->defaultArgumentWasInherited(); 10923 bool HasSecondDefaultArgument = 10924 SecondTTPD->hasDefaultArgument() && 10925 !SecondTTPD->defaultArgumentWasInherited(); 10926 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10927 ODRDiagDeclError(FirstRecord, FirstModule, 10928 FirstTemplate->getLocation(), 10929 FirstTemplate->getSourceRange(), 10930 FunctionTemplateParameterSingleDefaultArgument) 10931 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 10932 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10933 SecondTemplate->getSourceRange(), 10934 FunctionTemplateParameterSingleDefaultArgument) 10935 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 10936 ParameterMismatch = true; 10937 break; 10938 } 10939 10940 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 10941 QualType FirstType = FirstTTPD->getDefaultArgument(); 10942 QualType SecondType = SecondTTPD->getDefaultArgument(); 10943 if (ComputeQualTypeODRHash(FirstType) != 10944 ComputeQualTypeODRHash(SecondType)) { 10945 ODRDiagDeclError( 10946 FirstRecord, FirstModule, FirstTemplate->getLocation(), 10947 FirstTemplate->getSourceRange(), 10948 FunctionTemplateParameterDifferentDefaultArgument) 10949 << FirstTemplate << (i + 1) << FirstType; 10950 ODRDiagDeclNote( 10951 SecondModule, SecondTemplate->getLocation(), 10952 SecondTemplate->getSourceRange(), 10953 FunctionTemplateParameterDifferentDefaultArgument) 10954 << SecondTemplate << (i + 1) << SecondType; 10955 ParameterMismatch = true; 10956 break; 10957 } 10958 } 10959 10960 if (FirstTTPD->isParameterPack() != 10961 SecondTTPD->isParameterPack()) { 10962 ODRDiagDeclError(FirstRecord, FirstModule, 10963 FirstTemplate->getLocation(), 10964 FirstTemplate->getSourceRange(), 10965 FunctionTemplatePackParameter) 10966 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 10967 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10968 SecondTemplate->getSourceRange(), 10969 FunctionTemplatePackParameter) 10970 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 10971 ParameterMismatch = true; 10972 break; 10973 } 10974 } 10975 10976 if (isa<TemplateTemplateParmDecl>(FirstParam) && 10977 isa<TemplateTemplateParmDecl>(SecondParam)) { 10978 TemplateTemplateParmDecl *FirstTTPD = 10979 cast<TemplateTemplateParmDecl>(FirstParam); 10980 TemplateTemplateParmDecl *SecondTTPD = 10981 cast<TemplateTemplateParmDecl>(SecondParam); 10982 10983 TemplateParameterList *FirstTPL = 10984 FirstTTPD->getTemplateParameters(); 10985 TemplateParameterList *SecondTPL = 10986 SecondTTPD->getTemplateParameters(); 10987 10988 if (ComputeTemplateParameterListODRHash(FirstTPL) != 10989 ComputeTemplateParameterListODRHash(SecondTPL)) { 10990 ODRDiagDeclError(FirstRecord, FirstModule, 10991 FirstTemplate->getLocation(), 10992 FirstTemplate->getSourceRange(), 10993 FunctionTemplateParameterDifferentType) 10994 << FirstTemplate << (i + 1); 10995 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10996 SecondTemplate->getSourceRange(), 10997 FunctionTemplateParameterDifferentType) 10998 << SecondTemplate << (i + 1); 10999 ParameterMismatch = true; 11000 break; 11001 } 11002 11003 bool HasFirstDefaultArgument = 11004 FirstTTPD->hasDefaultArgument() && 11005 !FirstTTPD->defaultArgumentWasInherited(); 11006 bool HasSecondDefaultArgument = 11007 SecondTTPD->hasDefaultArgument() && 11008 !SecondTTPD->defaultArgumentWasInherited(); 11009 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11010 ODRDiagDeclError(FirstRecord, FirstModule, 11011 FirstTemplate->getLocation(), 11012 FirstTemplate->getSourceRange(), 11013 FunctionTemplateParameterSingleDefaultArgument) 11014 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11015 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11016 SecondTemplate->getSourceRange(), 11017 FunctionTemplateParameterSingleDefaultArgument) 11018 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11019 ParameterMismatch = true; 11020 break; 11021 } 11022 11023 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11024 TemplateArgument FirstTA = 11025 FirstTTPD->getDefaultArgument().getArgument(); 11026 TemplateArgument SecondTA = 11027 SecondTTPD->getDefaultArgument().getArgument(); 11028 if (ComputeTemplateArgumentODRHash(FirstTA) != 11029 ComputeTemplateArgumentODRHash(SecondTA)) { 11030 ODRDiagDeclError( 11031 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11032 FirstTemplate->getSourceRange(), 11033 FunctionTemplateParameterDifferentDefaultArgument) 11034 << FirstTemplate << (i + 1) << FirstTA; 11035 ODRDiagDeclNote( 11036 SecondModule, SecondTemplate->getLocation(), 11037 SecondTemplate->getSourceRange(), 11038 FunctionTemplateParameterDifferentDefaultArgument) 11039 << SecondTemplate << (i + 1) << SecondTA; 11040 ParameterMismatch = true; 11041 break; 11042 } 11043 } 11044 11045 if (FirstTTPD->isParameterPack() != 11046 SecondTTPD->isParameterPack()) { 11047 ODRDiagDeclError(FirstRecord, FirstModule, 11048 FirstTemplate->getLocation(), 11049 FirstTemplate->getSourceRange(), 11050 FunctionTemplatePackParameter) 11051 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 11052 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11053 SecondTemplate->getSourceRange(), 11054 FunctionTemplatePackParameter) 11055 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 11056 ParameterMismatch = true; 11057 break; 11058 } 11059 } 11060 11061 if (isa<NonTypeTemplateParmDecl>(FirstParam) && 11062 isa<NonTypeTemplateParmDecl>(SecondParam)) { 11063 NonTypeTemplateParmDecl *FirstNTTPD = 11064 cast<NonTypeTemplateParmDecl>(FirstParam); 11065 NonTypeTemplateParmDecl *SecondNTTPD = 11066 cast<NonTypeTemplateParmDecl>(SecondParam); 11067 11068 QualType FirstType = FirstNTTPD->getType(); 11069 QualType SecondType = SecondNTTPD->getType(); 11070 if (ComputeQualTypeODRHash(FirstType) != 11071 ComputeQualTypeODRHash(SecondType)) { 11072 ODRDiagDeclError(FirstRecord, FirstModule, 11073 FirstTemplate->getLocation(), 11074 FirstTemplate->getSourceRange(), 11075 FunctionTemplateParameterDifferentType) 11076 << FirstTemplate << (i + 1); 11077 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11078 SecondTemplate->getSourceRange(), 11079 FunctionTemplateParameterDifferentType) 11080 << SecondTemplate << (i + 1); 11081 ParameterMismatch = true; 11082 break; 11083 } 11084 11085 bool HasFirstDefaultArgument = 11086 FirstNTTPD->hasDefaultArgument() && 11087 !FirstNTTPD->defaultArgumentWasInherited(); 11088 bool HasSecondDefaultArgument = 11089 SecondNTTPD->hasDefaultArgument() && 11090 !SecondNTTPD->defaultArgumentWasInherited(); 11091 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11092 ODRDiagDeclError(FirstRecord, FirstModule, 11093 FirstTemplate->getLocation(), 11094 FirstTemplate->getSourceRange(), 11095 FunctionTemplateParameterSingleDefaultArgument) 11096 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11097 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11098 SecondTemplate->getSourceRange(), 11099 FunctionTemplateParameterSingleDefaultArgument) 11100 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11101 ParameterMismatch = true; 11102 break; 11103 } 11104 11105 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11106 Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument(); 11107 Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument(); 11108 if (ComputeODRHash(FirstDefaultArgument) != 11109 ComputeODRHash(SecondDefaultArgument)) { 11110 ODRDiagDeclError( 11111 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11112 FirstTemplate->getSourceRange(), 11113 FunctionTemplateParameterDifferentDefaultArgument) 11114 << FirstTemplate << (i + 1) << FirstDefaultArgument; 11115 ODRDiagDeclNote( 11116 SecondModule, SecondTemplate->getLocation(), 11117 SecondTemplate->getSourceRange(), 11118 FunctionTemplateParameterDifferentDefaultArgument) 11119 << SecondTemplate << (i + 1) << SecondDefaultArgument; 11120 ParameterMismatch = true; 11121 break; 11122 } 11123 } 11124 11125 if (FirstNTTPD->isParameterPack() != 11126 SecondNTTPD->isParameterPack()) { 11127 ODRDiagDeclError(FirstRecord, FirstModule, 11128 FirstTemplate->getLocation(), 11129 FirstTemplate->getSourceRange(), 11130 FunctionTemplatePackParameter) 11131 << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack(); 11132 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11133 SecondTemplate->getSourceRange(), 11134 FunctionTemplatePackParameter) 11135 << SecondTemplate << (i + 1) 11136 << SecondNTTPD->isParameterPack(); 11137 ParameterMismatch = true; 11138 break; 11139 } 11140 } 11141 } 11142 11143 if (ParameterMismatch) { 11144 Diagnosed = true; 11145 break; 11146 } 11147 11148 break; 11149 } 11150 } 11151 11152 if (Diagnosed) 11153 continue; 11154 11155 Diag(FirstDecl->getLocation(), 11156 diag::err_module_odr_violation_mismatch_decl_unknown) 11157 << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType 11158 << FirstDecl->getSourceRange(); 11159 Diag(SecondDecl->getLocation(), 11160 diag::note_module_odr_violation_mismatch_decl_unknown) 11161 << SecondModule << FirstDiffType << SecondDecl->getSourceRange(); 11162 Diagnosed = true; 11163 } 11164 11165 if (!Diagnosed) { 11166 // All definitions are updates to the same declaration. This happens if a 11167 // module instantiates the declaration of a class template specialization 11168 // and two or more other modules instantiate its definition. 11169 // 11170 // FIXME: Indicate which modules had instantiations of this definition. 11171 // FIXME: How can this even happen? 11172 Diag(Merge.first->getLocation(), 11173 diag::err_module_odr_violation_different_instantiations) 11174 << Merge.first; 11175 } 11176 } 11177 11178 // Issue ODR failures diagnostics for functions. 11179 for (auto &Merge : FunctionOdrMergeFailures) { 11180 enum ODRFunctionDifference { 11181 ReturnType, 11182 ParameterName, 11183 ParameterType, 11184 ParameterSingleDefaultArgument, 11185 ParameterDifferentDefaultArgument, 11186 FunctionBody, 11187 }; 11188 11189 FunctionDecl *FirstFunction = Merge.first; 11190 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction); 11191 11192 bool Diagnosed = false; 11193 for (auto &SecondFunction : Merge.second) { 11194 11195 if (FirstFunction == SecondFunction) 11196 continue; 11197 11198 std::string SecondModule = 11199 getOwningModuleNameForDiagnostic(SecondFunction); 11200 11201 auto ODRDiagError = [FirstFunction, &FirstModule, 11202 this](SourceLocation Loc, SourceRange Range, 11203 ODRFunctionDifference DiffType) { 11204 return Diag(Loc, diag::err_module_odr_violation_function) 11205 << FirstFunction << FirstModule.empty() << FirstModule << Range 11206 << DiffType; 11207 }; 11208 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11209 SourceRange Range, 11210 ODRFunctionDifference DiffType) { 11211 return Diag(Loc, diag::note_module_odr_violation_function) 11212 << SecondModule << Range << DiffType; 11213 }; 11214 11215 if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) != 11216 ComputeQualTypeODRHash(SecondFunction->getReturnType())) { 11217 ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(), 11218 FirstFunction->getReturnTypeSourceRange(), ReturnType) 11219 << FirstFunction->getReturnType(); 11220 ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(), 11221 SecondFunction->getReturnTypeSourceRange(), ReturnType) 11222 << SecondFunction->getReturnType(); 11223 Diagnosed = true; 11224 break; 11225 } 11226 11227 assert(FirstFunction->param_size() == SecondFunction->param_size() && 11228 "Merged functions with different number of parameters"); 11229 11230 auto ParamSize = FirstFunction->param_size(); 11231 bool ParameterMismatch = false; 11232 for (unsigned I = 0; I < ParamSize; ++I) { 11233 auto *FirstParam = FirstFunction->getParamDecl(I); 11234 auto *SecondParam = SecondFunction->getParamDecl(I); 11235 11236 assert(getContext().hasSameType(FirstParam->getType(), 11237 SecondParam->getType()) && 11238 "Merged function has different parameter types."); 11239 11240 if (FirstParam->getDeclName() != SecondParam->getDeclName()) { 11241 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11242 ParameterName) 11243 << I + 1 << FirstParam->getDeclName(); 11244 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11245 ParameterName) 11246 << I + 1 << SecondParam->getDeclName(); 11247 ParameterMismatch = true; 11248 break; 11249 }; 11250 11251 QualType FirstParamType = FirstParam->getType(); 11252 QualType SecondParamType = SecondParam->getType(); 11253 if (FirstParamType != SecondParamType && 11254 ComputeQualTypeODRHash(FirstParamType) != 11255 ComputeQualTypeODRHash(SecondParamType)) { 11256 if (const DecayedType *ParamDecayedType = 11257 FirstParamType->getAs<DecayedType>()) { 11258 ODRDiagError(FirstParam->getLocation(), 11259 FirstParam->getSourceRange(), ParameterType) 11260 << (I + 1) << FirstParamType << true 11261 << ParamDecayedType->getOriginalType(); 11262 } else { 11263 ODRDiagError(FirstParam->getLocation(), 11264 FirstParam->getSourceRange(), ParameterType) 11265 << (I + 1) << FirstParamType << false; 11266 } 11267 11268 if (const DecayedType *ParamDecayedType = 11269 SecondParamType->getAs<DecayedType>()) { 11270 ODRDiagNote(SecondParam->getLocation(), 11271 SecondParam->getSourceRange(), ParameterType) 11272 << (I + 1) << SecondParamType << true 11273 << ParamDecayedType->getOriginalType(); 11274 } else { 11275 ODRDiagNote(SecondParam->getLocation(), 11276 SecondParam->getSourceRange(), ParameterType) 11277 << (I + 1) << SecondParamType << false; 11278 } 11279 ParameterMismatch = true; 11280 break; 11281 } 11282 11283 const Expr *FirstInit = FirstParam->getInit(); 11284 const Expr *SecondInit = SecondParam->getInit(); 11285 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 11286 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11287 ParameterSingleDefaultArgument) 11288 << (I + 1) << (FirstInit == nullptr) 11289 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 11290 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11291 ParameterSingleDefaultArgument) 11292 << (I + 1) << (SecondInit == nullptr) 11293 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 11294 ParameterMismatch = true; 11295 break; 11296 } 11297 11298 if (FirstInit && SecondInit && 11299 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11300 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11301 ParameterDifferentDefaultArgument) 11302 << (I + 1) << FirstInit->getSourceRange(); 11303 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11304 ParameterDifferentDefaultArgument) 11305 << (I + 1) << SecondInit->getSourceRange(); 11306 ParameterMismatch = true; 11307 break; 11308 } 11309 11310 assert(ComputeSubDeclODRHash(FirstParam) == 11311 ComputeSubDeclODRHash(SecondParam) && 11312 "Undiagnosed parameter difference."); 11313 } 11314 11315 if (ParameterMismatch) { 11316 Diagnosed = true; 11317 break; 11318 } 11319 11320 // If no error has been generated before now, assume the problem is in 11321 // the body and generate a message. 11322 ODRDiagError(FirstFunction->getLocation(), 11323 FirstFunction->getSourceRange(), FunctionBody); 11324 ODRDiagNote(SecondFunction->getLocation(), 11325 SecondFunction->getSourceRange(), FunctionBody); 11326 Diagnosed = true; 11327 break; 11328 } 11329 (void)Diagnosed; 11330 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11331 } 11332 11333 // Issue ODR failures diagnostics for enums. 11334 for (auto &Merge : EnumOdrMergeFailures) { 11335 enum ODREnumDifference { 11336 SingleScopedEnum, 11337 EnumTagKeywordMismatch, 11338 SingleSpecifiedType, 11339 DifferentSpecifiedTypes, 11340 DifferentNumberEnumConstants, 11341 EnumConstantName, 11342 EnumConstantSingleInitilizer, 11343 EnumConstantDifferentInitilizer, 11344 }; 11345 11346 // If we've already pointed out a specific problem with this enum, don't 11347 // bother issuing a general "something's different" diagnostic. 11348 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 11349 continue; 11350 11351 EnumDecl *FirstEnum = Merge.first; 11352 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum); 11353 11354 using DeclHashes = 11355 llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>; 11356 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum]( 11357 DeclHashes &Hashes, EnumDecl *Enum) { 11358 for (auto *D : Enum->decls()) { 11359 // Due to decl merging, the first EnumDecl is the parent of 11360 // Decls in both records. 11361 if (!ODRHash::isDeclToBeProcessed(D, FirstEnum)) 11362 continue; 11363 assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind"); 11364 Hashes.emplace_back(cast<EnumConstantDecl>(D), 11365 ComputeSubDeclODRHash(D)); 11366 } 11367 }; 11368 DeclHashes FirstHashes; 11369 PopulateHashes(FirstHashes, FirstEnum); 11370 bool Diagnosed = false; 11371 for (auto &SecondEnum : Merge.second) { 11372 11373 if (FirstEnum == SecondEnum) 11374 continue; 11375 11376 std::string SecondModule = 11377 getOwningModuleNameForDiagnostic(SecondEnum); 11378 11379 auto ODRDiagError = [FirstEnum, &FirstModule, 11380 this](SourceLocation Loc, SourceRange Range, 11381 ODREnumDifference DiffType) { 11382 return Diag(Loc, diag::err_module_odr_violation_enum) 11383 << FirstEnum << FirstModule.empty() << FirstModule << Range 11384 << DiffType; 11385 }; 11386 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11387 SourceRange Range, 11388 ODREnumDifference DiffType) { 11389 return Diag(Loc, diag::note_module_odr_violation_enum) 11390 << SecondModule << Range << DiffType; 11391 }; 11392 11393 if (FirstEnum->isScoped() != SecondEnum->isScoped()) { 11394 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11395 SingleScopedEnum) 11396 << FirstEnum->isScoped(); 11397 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11398 SingleScopedEnum) 11399 << SecondEnum->isScoped(); 11400 Diagnosed = true; 11401 continue; 11402 } 11403 11404 if (FirstEnum->isScoped() && SecondEnum->isScoped()) { 11405 if (FirstEnum->isScopedUsingClassTag() != 11406 SecondEnum->isScopedUsingClassTag()) { 11407 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11408 EnumTagKeywordMismatch) 11409 << FirstEnum->isScopedUsingClassTag(); 11410 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11411 EnumTagKeywordMismatch) 11412 << SecondEnum->isScopedUsingClassTag(); 11413 Diagnosed = true; 11414 continue; 11415 } 11416 } 11417 11418 QualType FirstUnderlyingType = 11419 FirstEnum->getIntegerTypeSourceInfo() 11420 ? FirstEnum->getIntegerTypeSourceInfo()->getType() 11421 : QualType(); 11422 QualType SecondUnderlyingType = 11423 SecondEnum->getIntegerTypeSourceInfo() 11424 ? SecondEnum->getIntegerTypeSourceInfo()->getType() 11425 : QualType(); 11426 if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) { 11427 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11428 SingleSpecifiedType) 11429 << !FirstUnderlyingType.isNull(); 11430 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11431 SingleSpecifiedType) 11432 << !SecondUnderlyingType.isNull(); 11433 Diagnosed = true; 11434 continue; 11435 } 11436 11437 if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) { 11438 if (ComputeQualTypeODRHash(FirstUnderlyingType) != 11439 ComputeQualTypeODRHash(SecondUnderlyingType)) { 11440 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11441 DifferentSpecifiedTypes) 11442 << FirstUnderlyingType; 11443 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11444 DifferentSpecifiedTypes) 11445 << SecondUnderlyingType; 11446 Diagnosed = true; 11447 continue; 11448 } 11449 } 11450 11451 DeclHashes SecondHashes; 11452 PopulateHashes(SecondHashes, SecondEnum); 11453 11454 if (FirstHashes.size() != SecondHashes.size()) { 11455 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11456 DifferentNumberEnumConstants) 11457 << (int)FirstHashes.size(); 11458 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11459 DifferentNumberEnumConstants) 11460 << (int)SecondHashes.size(); 11461 Diagnosed = true; 11462 continue; 11463 } 11464 11465 for (unsigned I = 0; I < FirstHashes.size(); ++I) { 11466 if (FirstHashes[I].second == SecondHashes[I].second) 11467 continue; 11468 const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first; 11469 const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first; 11470 11471 if (FirstEnumConstant->getDeclName() != 11472 SecondEnumConstant->getDeclName()) { 11473 11474 ODRDiagError(FirstEnumConstant->getLocation(), 11475 FirstEnumConstant->getSourceRange(), EnumConstantName) 11476 << I + 1 << FirstEnumConstant; 11477 ODRDiagNote(SecondEnumConstant->getLocation(), 11478 SecondEnumConstant->getSourceRange(), EnumConstantName) 11479 << I + 1 << SecondEnumConstant; 11480 Diagnosed = true; 11481 break; 11482 } 11483 11484 const Expr *FirstInit = FirstEnumConstant->getInitExpr(); 11485 const Expr *SecondInit = SecondEnumConstant->getInitExpr(); 11486 if (!FirstInit && !SecondInit) 11487 continue; 11488 11489 if (!FirstInit || !SecondInit) { 11490 ODRDiagError(FirstEnumConstant->getLocation(), 11491 FirstEnumConstant->getSourceRange(), 11492 EnumConstantSingleInitilizer) 11493 << I + 1 << FirstEnumConstant << (FirstInit != nullptr); 11494 ODRDiagNote(SecondEnumConstant->getLocation(), 11495 SecondEnumConstant->getSourceRange(), 11496 EnumConstantSingleInitilizer) 11497 << I + 1 << SecondEnumConstant << (SecondInit != nullptr); 11498 Diagnosed = true; 11499 break; 11500 } 11501 11502 if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11503 ODRDiagError(FirstEnumConstant->getLocation(), 11504 FirstEnumConstant->getSourceRange(), 11505 EnumConstantDifferentInitilizer) 11506 << I + 1 << FirstEnumConstant; 11507 ODRDiagNote(SecondEnumConstant->getLocation(), 11508 SecondEnumConstant->getSourceRange(), 11509 EnumConstantDifferentInitilizer) 11510 << I + 1 << SecondEnumConstant; 11511 Diagnosed = true; 11512 break; 11513 } 11514 } 11515 } 11516 11517 (void)Diagnosed; 11518 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11519 } 11520 } 11521 11522 void ASTReader::StartedDeserializing() { 11523 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get()) 11524 ReadTimer->startTimer(); 11525 } 11526 11527 void ASTReader::FinishedDeserializing() { 11528 assert(NumCurrentElementsDeserializing && 11529 "FinishedDeserializing not paired with StartedDeserializing"); 11530 if (NumCurrentElementsDeserializing == 1) { 11531 // We decrease NumCurrentElementsDeserializing only after pending actions 11532 // are finished, to avoid recursively re-calling finishPendingActions(). 11533 finishPendingActions(); 11534 } 11535 --NumCurrentElementsDeserializing; 11536 11537 if (NumCurrentElementsDeserializing == 0) { 11538 // Propagate exception specification and deduced type updates along 11539 // redeclaration chains. 11540 // 11541 // We do this now rather than in finishPendingActions because we want to 11542 // be able to walk the complete redeclaration chains of the updated decls. 11543 while (!PendingExceptionSpecUpdates.empty() || 11544 !PendingDeducedTypeUpdates.empty()) { 11545 auto ESUpdates = std::move(PendingExceptionSpecUpdates); 11546 PendingExceptionSpecUpdates.clear(); 11547 for (auto Update : ESUpdates) { 11548 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11549 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>(); 11550 auto ESI = FPT->getExtProtoInfo().ExceptionSpec; 11551 if (auto *Listener = getContext().getASTMutationListener()) 11552 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second)); 11553 for (auto *Redecl : Update.second->redecls()) 11554 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI); 11555 } 11556 11557 auto DTUpdates = std::move(PendingDeducedTypeUpdates); 11558 PendingDeducedTypeUpdates.clear(); 11559 for (auto Update : DTUpdates) { 11560 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11561 // FIXME: If the return type is already deduced, check that it matches. 11562 getContext().adjustDeducedFunctionResultType(Update.first, 11563 Update.second); 11564 } 11565 } 11566 11567 if (ReadTimer) 11568 ReadTimer->stopTimer(); 11569 11570 diagnoseOdrViolations(); 11571 11572 // We are not in recursive loading, so it's safe to pass the "interesting" 11573 // decls to the consumer. 11574 if (Consumer) 11575 PassInterestingDeclsToConsumer(); 11576 } 11577 } 11578 11579 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) { 11580 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) { 11581 // Remove any fake results before adding any real ones. 11582 auto It = PendingFakeLookupResults.find(II); 11583 if (It != PendingFakeLookupResults.end()) { 11584 for (auto *ND : It->second) 11585 SemaObj->IdResolver.RemoveDecl(ND); 11586 // FIXME: this works around module+PCH performance issue. 11587 // Rather than erase the result from the map, which is O(n), just clear 11588 // the vector of NamedDecls. 11589 It->second.clear(); 11590 } 11591 } 11592 11593 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) { 11594 SemaObj->TUScope->AddDecl(D); 11595 } else if (SemaObj->TUScope) { 11596 // Adding the decl to IdResolver may have failed because it was already in 11597 // (even though it was not added in scope). If it is already in, make sure 11598 // it gets in the scope as well. 11599 if (std::find(SemaObj->IdResolver.begin(Name), 11600 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end()) 11601 SemaObj->TUScope->AddDecl(D); 11602 } 11603 } 11604 11605 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache, 11606 ASTContext *Context, 11607 const PCHContainerReader &PCHContainerRdr, 11608 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, 11609 StringRef isysroot, 11610 DisableValidationForModuleKind DisableValidationKind, 11611 bool AllowASTWithCompilerErrors, 11612 bool AllowConfigurationMismatch, bool ValidateSystemInputs, 11613 bool ValidateASTInputFilesContent, bool UseGlobalIndex, 11614 std::unique_ptr<llvm::Timer> ReadTimer) 11615 : Listener(bool(DisableValidationKind &DisableValidationForModuleKind::PCH) 11616 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP)) 11617 : cast<ASTReaderListener>(new PCHValidator(PP, *this))), 11618 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), 11619 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP), 11620 ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache, 11621 PCHContainerRdr, PP.getHeaderSearchInfo()), 11622 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot), 11623 DisableValidationKind(DisableValidationKind), 11624 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors), 11625 AllowConfigurationMismatch(AllowConfigurationMismatch), 11626 ValidateSystemInputs(ValidateSystemInputs), 11627 ValidateASTInputFilesContent(ValidateASTInputFilesContent), 11628 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) { 11629 SourceMgr.setExternalSLocEntrySource(this); 11630 11631 for (const auto &Ext : Extensions) { 11632 auto BlockName = Ext->getExtensionMetadata().BlockName; 11633 auto Known = ModuleFileExtensions.find(BlockName); 11634 if (Known != ModuleFileExtensions.end()) { 11635 Diags.Report(diag::warn_duplicate_module_file_extension) 11636 << BlockName; 11637 continue; 11638 } 11639 11640 ModuleFileExtensions.insert({BlockName, Ext}); 11641 } 11642 } 11643 11644 ASTReader::~ASTReader() { 11645 if (OwnsDeserializationListener) 11646 delete DeserializationListener; 11647 } 11648 11649 IdentifierResolver &ASTReader::getIdResolver() { 11650 return SemaObj ? SemaObj->IdResolver : DummyIdResolver; 11651 } 11652 11653 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor, 11654 unsigned AbbrevID) { 11655 Idx = 0; 11656 Record.clear(); 11657 return Cursor.readRecord(AbbrevID, Record); 11658 } 11659 //===----------------------------------------------------------------------===// 11660 //// OMPClauseReader implementation 11661 ////===----------------------------------------------------------------------===// 11662 11663 // This has to be in namespace clang because it's friended by all 11664 // of the OMP clauses. 11665 namespace clang { 11666 11667 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> { 11668 ASTRecordReader &Record; 11669 ASTContext &Context; 11670 11671 public: 11672 OMPClauseReader(ASTRecordReader &Record) 11673 : Record(Record), Context(Record.getContext()) {} 11674 #define GEN_CLANG_CLAUSE_CLASS 11675 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C); 11676 #include "llvm/Frontend/OpenMP/OMP.inc" 11677 OMPClause *readClause(); 11678 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C); 11679 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C); 11680 }; 11681 11682 } // end namespace clang 11683 11684 OMPClause *ASTRecordReader::readOMPClause() { 11685 return OMPClauseReader(*this).readClause(); 11686 } 11687 11688 OMPClause *OMPClauseReader::readClause() { 11689 OMPClause *C = nullptr; 11690 switch (llvm::omp::Clause(Record.readInt())) { 11691 case llvm::omp::OMPC_if: 11692 C = new (Context) OMPIfClause(); 11693 break; 11694 case llvm::omp::OMPC_final: 11695 C = new (Context) OMPFinalClause(); 11696 break; 11697 case llvm::omp::OMPC_num_threads: 11698 C = new (Context) OMPNumThreadsClause(); 11699 break; 11700 case llvm::omp::OMPC_safelen: 11701 C = new (Context) OMPSafelenClause(); 11702 break; 11703 case llvm::omp::OMPC_simdlen: 11704 C = new (Context) OMPSimdlenClause(); 11705 break; 11706 case llvm::omp::OMPC_sizes: { 11707 unsigned NumSizes = Record.readInt(); 11708 C = OMPSizesClause::CreateEmpty(Context, NumSizes); 11709 break; 11710 } 11711 case llvm::omp::OMPC_full: 11712 C = OMPFullClause::CreateEmpty(Context); 11713 break; 11714 case llvm::omp::OMPC_partial: 11715 C = OMPPartialClause::CreateEmpty(Context); 11716 break; 11717 case llvm::omp::OMPC_allocator: 11718 C = new (Context) OMPAllocatorClause(); 11719 break; 11720 case llvm::omp::OMPC_collapse: 11721 C = new (Context) OMPCollapseClause(); 11722 break; 11723 case llvm::omp::OMPC_default: 11724 C = new (Context) OMPDefaultClause(); 11725 break; 11726 case llvm::omp::OMPC_proc_bind: 11727 C = new (Context) OMPProcBindClause(); 11728 break; 11729 case llvm::omp::OMPC_schedule: 11730 C = new (Context) OMPScheduleClause(); 11731 break; 11732 case llvm::omp::OMPC_ordered: 11733 C = OMPOrderedClause::CreateEmpty(Context, Record.readInt()); 11734 break; 11735 case llvm::omp::OMPC_nowait: 11736 C = new (Context) OMPNowaitClause(); 11737 break; 11738 case llvm::omp::OMPC_untied: 11739 C = new (Context) OMPUntiedClause(); 11740 break; 11741 case llvm::omp::OMPC_mergeable: 11742 C = new (Context) OMPMergeableClause(); 11743 break; 11744 case llvm::omp::OMPC_read: 11745 C = new (Context) OMPReadClause(); 11746 break; 11747 case llvm::omp::OMPC_write: 11748 C = new (Context) OMPWriteClause(); 11749 break; 11750 case llvm::omp::OMPC_update: 11751 C = OMPUpdateClause::CreateEmpty(Context, Record.readInt()); 11752 break; 11753 case llvm::omp::OMPC_capture: 11754 C = new (Context) OMPCaptureClause(); 11755 break; 11756 case llvm::omp::OMPC_seq_cst: 11757 C = new (Context) OMPSeqCstClause(); 11758 break; 11759 case llvm::omp::OMPC_acq_rel: 11760 C = new (Context) OMPAcqRelClause(); 11761 break; 11762 case llvm::omp::OMPC_acquire: 11763 C = new (Context) OMPAcquireClause(); 11764 break; 11765 case llvm::omp::OMPC_release: 11766 C = new (Context) OMPReleaseClause(); 11767 break; 11768 case llvm::omp::OMPC_relaxed: 11769 C = new (Context) OMPRelaxedClause(); 11770 break; 11771 case llvm::omp::OMPC_threads: 11772 C = new (Context) OMPThreadsClause(); 11773 break; 11774 case llvm::omp::OMPC_simd: 11775 C = new (Context) OMPSIMDClause(); 11776 break; 11777 case llvm::omp::OMPC_nogroup: 11778 C = new (Context) OMPNogroupClause(); 11779 break; 11780 case llvm::omp::OMPC_unified_address: 11781 C = new (Context) OMPUnifiedAddressClause(); 11782 break; 11783 case llvm::omp::OMPC_unified_shared_memory: 11784 C = new (Context) OMPUnifiedSharedMemoryClause(); 11785 break; 11786 case llvm::omp::OMPC_reverse_offload: 11787 C = new (Context) OMPReverseOffloadClause(); 11788 break; 11789 case llvm::omp::OMPC_dynamic_allocators: 11790 C = new (Context) OMPDynamicAllocatorsClause(); 11791 break; 11792 case llvm::omp::OMPC_atomic_default_mem_order: 11793 C = new (Context) OMPAtomicDefaultMemOrderClause(); 11794 break; 11795 case llvm::omp::OMPC_private: 11796 C = OMPPrivateClause::CreateEmpty(Context, Record.readInt()); 11797 break; 11798 case llvm::omp::OMPC_firstprivate: 11799 C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt()); 11800 break; 11801 case llvm::omp::OMPC_lastprivate: 11802 C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt()); 11803 break; 11804 case llvm::omp::OMPC_shared: 11805 C = OMPSharedClause::CreateEmpty(Context, Record.readInt()); 11806 break; 11807 case llvm::omp::OMPC_reduction: { 11808 unsigned N = Record.readInt(); 11809 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>(); 11810 C = OMPReductionClause::CreateEmpty(Context, N, Modifier); 11811 break; 11812 } 11813 case llvm::omp::OMPC_task_reduction: 11814 C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt()); 11815 break; 11816 case llvm::omp::OMPC_in_reduction: 11817 C = OMPInReductionClause::CreateEmpty(Context, Record.readInt()); 11818 break; 11819 case llvm::omp::OMPC_linear: 11820 C = OMPLinearClause::CreateEmpty(Context, Record.readInt()); 11821 break; 11822 case llvm::omp::OMPC_aligned: 11823 C = OMPAlignedClause::CreateEmpty(Context, Record.readInt()); 11824 break; 11825 case llvm::omp::OMPC_copyin: 11826 C = OMPCopyinClause::CreateEmpty(Context, Record.readInt()); 11827 break; 11828 case llvm::omp::OMPC_copyprivate: 11829 C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt()); 11830 break; 11831 case llvm::omp::OMPC_flush: 11832 C = OMPFlushClause::CreateEmpty(Context, Record.readInt()); 11833 break; 11834 case llvm::omp::OMPC_depobj: 11835 C = OMPDepobjClause::CreateEmpty(Context); 11836 break; 11837 case llvm::omp::OMPC_depend: { 11838 unsigned NumVars = Record.readInt(); 11839 unsigned NumLoops = Record.readInt(); 11840 C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops); 11841 break; 11842 } 11843 case llvm::omp::OMPC_device: 11844 C = new (Context) OMPDeviceClause(); 11845 break; 11846 case llvm::omp::OMPC_map: { 11847 OMPMappableExprListSizeTy Sizes; 11848 Sizes.NumVars = Record.readInt(); 11849 Sizes.NumUniqueDeclarations = Record.readInt(); 11850 Sizes.NumComponentLists = Record.readInt(); 11851 Sizes.NumComponents = Record.readInt(); 11852 C = OMPMapClause::CreateEmpty(Context, Sizes); 11853 break; 11854 } 11855 case llvm::omp::OMPC_num_teams: 11856 C = new (Context) OMPNumTeamsClause(); 11857 break; 11858 case llvm::omp::OMPC_thread_limit: 11859 C = new (Context) OMPThreadLimitClause(); 11860 break; 11861 case llvm::omp::OMPC_priority: 11862 C = new (Context) OMPPriorityClause(); 11863 break; 11864 case llvm::omp::OMPC_grainsize: 11865 C = new (Context) OMPGrainsizeClause(); 11866 break; 11867 case llvm::omp::OMPC_num_tasks: 11868 C = new (Context) OMPNumTasksClause(); 11869 break; 11870 case llvm::omp::OMPC_hint: 11871 C = new (Context) OMPHintClause(); 11872 break; 11873 case llvm::omp::OMPC_dist_schedule: 11874 C = new (Context) OMPDistScheduleClause(); 11875 break; 11876 case llvm::omp::OMPC_defaultmap: 11877 C = new (Context) OMPDefaultmapClause(); 11878 break; 11879 case llvm::omp::OMPC_to: { 11880 OMPMappableExprListSizeTy Sizes; 11881 Sizes.NumVars = Record.readInt(); 11882 Sizes.NumUniqueDeclarations = Record.readInt(); 11883 Sizes.NumComponentLists = Record.readInt(); 11884 Sizes.NumComponents = Record.readInt(); 11885 C = OMPToClause::CreateEmpty(Context, Sizes); 11886 break; 11887 } 11888 case llvm::omp::OMPC_from: { 11889 OMPMappableExprListSizeTy Sizes; 11890 Sizes.NumVars = Record.readInt(); 11891 Sizes.NumUniqueDeclarations = Record.readInt(); 11892 Sizes.NumComponentLists = Record.readInt(); 11893 Sizes.NumComponents = Record.readInt(); 11894 C = OMPFromClause::CreateEmpty(Context, Sizes); 11895 break; 11896 } 11897 case llvm::omp::OMPC_use_device_ptr: { 11898 OMPMappableExprListSizeTy Sizes; 11899 Sizes.NumVars = Record.readInt(); 11900 Sizes.NumUniqueDeclarations = Record.readInt(); 11901 Sizes.NumComponentLists = Record.readInt(); 11902 Sizes.NumComponents = Record.readInt(); 11903 C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes); 11904 break; 11905 } 11906 case llvm::omp::OMPC_use_device_addr: { 11907 OMPMappableExprListSizeTy Sizes; 11908 Sizes.NumVars = Record.readInt(); 11909 Sizes.NumUniqueDeclarations = Record.readInt(); 11910 Sizes.NumComponentLists = Record.readInt(); 11911 Sizes.NumComponents = Record.readInt(); 11912 C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes); 11913 break; 11914 } 11915 case llvm::omp::OMPC_is_device_ptr: { 11916 OMPMappableExprListSizeTy Sizes; 11917 Sizes.NumVars = Record.readInt(); 11918 Sizes.NumUniqueDeclarations = Record.readInt(); 11919 Sizes.NumComponentLists = Record.readInt(); 11920 Sizes.NumComponents = Record.readInt(); 11921 C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes); 11922 break; 11923 } 11924 case llvm::omp::OMPC_allocate: 11925 C = OMPAllocateClause::CreateEmpty(Context, Record.readInt()); 11926 break; 11927 case llvm::omp::OMPC_nontemporal: 11928 C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt()); 11929 break; 11930 case llvm::omp::OMPC_inclusive: 11931 C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt()); 11932 break; 11933 case llvm::omp::OMPC_exclusive: 11934 C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt()); 11935 break; 11936 case llvm::omp::OMPC_order: 11937 C = new (Context) OMPOrderClause(); 11938 break; 11939 case llvm::omp::OMPC_init: 11940 C = OMPInitClause::CreateEmpty(Context, Record.readInt()); 11941 break; 11942 case llvm::omp::OMPC_use: 11943 C = new (Context) OMPUseClause(); 11944 break; 11945 case llvm::omp::OMPC_destroy: 11946 C = new (Context) OMPDestroyClause(); 11947 break; 11948 case llvm::omp::OMPC_novariants: 11949 C = new (Context) OMPNovariantsClause(); 11950 break; 11951 case llvm::omp::OMPC_nocontext: 11952 C = new (Context) OMPNocontextClause(); 11953 break; 11954 case llvm::omp::OMPC_detach: 11955 C = new (Context) OMPDetachClause(); 11956 break; 11957 case llvm::omp::OMPC_uses_allocators: 11958 C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt()); 11959 break; 11960 case llvm::omp::OMPC_affinity: 11961 C = OMPAffinityClause::CreateEmpty(Context, Record.readInt()); 11962 break; 11963 case llvm::omp::OMPC_filter: 11964 C = new (Context) OMPFilterClause(); 11965 break; 11966 #define OMP_CLAUSE_NO_CLASS(Enum, Str) \ 11967 case llvm::omp::Enum: \ 11968 break; 11969 #include "llvm/Frontend/OpenMP/OMPKinds.def" 11970 default: 11971 break; 11972 } 11973 assert(C && "Unknown OMPClause type"); 11974 11975 Visit(C); 11976 C->setLocStart(Record.readSourceLocation()); 11977 C->setLocEnd(Record.readSourceLocation()); 11978 11979 return C; 11980 } 11981 11982 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) { 11983 C->setPreInitStmt(Record.readSubStmt(), 11984 static_cast<OpenMPDirectiveKind>(Record.readInt())); 11985 } 11986 11987 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) { 11988 VisitOMPClauseWithPreInit(C); 11989 C->setPostUpdateExpr(Record.readSubExpr()); 11990 } 11991 11992 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) { 11993 VisitOMPClauseWithPreInit(C); 11994 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt())); 11995 C->setNameModifierLoc(Record.readSourceLocation()); 11996 C->setColonLoc(Record.readSourceLocation()); 11997 C->setCondition(Record.readSubExpr()); 11998 C->setLParenLoc(Record.readSourceLocation()); 11999 } 12000 12001 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) { 12002 VisitOMPClauseWithPreInit(C); 12003 C->setCondition(Record.readSubExpr()); 12004 C->setLParenLoc(Record.readSourceLocation()); 12005 } 12006 12007 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) { 12008 VisitOMPClauseWithPreInit(C); 12009 C->setNumThreads(Record.readSubExpr()); 12010 C->setLParenLoc(Record.readSourceLocation()); 12011 } 12012 12013 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) { 12014 C->setSafelen(Record.readSubExpr()); 12015 C->setLParenLoc(Record.readSourceLocation()); 12016 } 12017 12018 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) { 12019 C->setSimdlen(Record.readSubExpr()); 12020 C->setLParenLoc(Record.readSourceLocation()); 12021 } 12022 12023 void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) { 12024 for (Expr *&E : C->getSizesRefs()) 12025 E = Record.readSubExpr(); 12026 C->setLParenLoc(Record.readSourceLocation()); 12027 } 12028 12029 void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {} 12030 12031 void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) { 12032 C->setFactor(Record.readSubExpr()); 12033 C->setLParenLoc(Record.readSourceLocation()); 12034 } 12035 12036 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) { 12037 C->setAllocator(Record.readExpr()); 12038 C->setLParenLoc(Record.readSourceLocation()); 12039 } 12040 12041 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) { 12042 C->setNumForLoops(Record.readSubExpr()); 12043 C->setLParenLoc(Record.readSourceLocation()); 12044 } 12045 12046 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) { 12047 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt())); 12048 C->setLParenLoc(Record.readSourceLocation()); 12049 C->setDefaultKindKwLoc(Record.readSourceLocation()); 12050 } 12051 12052 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) { 12053 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt())); 12054 C->setLParenLoc(Record.readSourceLocation()); 12055 C->setProcBindKindKwLoc(Record.readSourceLocation()); 12056 } 12057 12058 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) { 12059 VisitOMPClauseWithPreInit(C); 12060 C->setScheduleKind( 12061 static_cast<OpenMPScheduleClauseKind>(Record.readInt())); 12062 C->setFirstScheduleModifier( 12063 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 12064 C->setSecondScheduleModifier( 12065 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 12066 C->setChunkSize(Record.readSubExpr()); 12067 C->setLParenLoc(Record.readSourceLocation()); 12068 C->setFirstScheduleModifierLoc(Record.readSourceLocation()); 12069 C->setSecondScheduleModifierLoc(Record.readSourceLocation()); 12070 C->setScheduleKindLoc(Record.readSourceLocation()); 12071 C->setCommaLoc(Record.readSourceLocation()); 12072 } 12073 12074 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) { 12075 C->setNumForLoops(Record.readSubExpr()); 12076 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 12077 C->setLoopNumIterations(I, Record.readSubExpr()); 12078 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 12079 C->setLoopCounter(I, Record.readSubExpr()); 12080 C->setLParenLoc(Record.readSourceLocation()); 12081 } 12082 12083 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) { 12084 C->setEventHandler(Record.readSubExpr()); 12085 C->setLParenLoc(Record.readSourceLocation()); 12086 } 12087 12088 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {} 12089 12090 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {} 12091 12092 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {} 12093 12094 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {} 12095 12096 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {} 12097 12098 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) { 12099 if (C->isExtended()) { 12100 C->setLParenLoc(Record.readSourceLocation()); 12101 C->setArgumentLoc(Record.readSourceLocation()); 12102 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>()); 12103 } 12104 } 12105 12106 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {} 12107 12108 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {} 12109 12110 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {} 12111 12112 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {} 12113 12114 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {} 12115 12116 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {} 12117 12118 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {} 12119 12120 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {} 12121 12122 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {} 12123 12124 void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) { 12125 unsigned NumVars = C->varlist_size(); 12126 SmallVector<Expr *, 16> Vars; 12127 Vars.reserve(NumVars); 12128 for (unsigned I = 0; I != NumVars; ++I) 12129 Vars.push_back(Record.readSubExpr()); 12130 C->setVarRefs(Vars); 12131 C->setIsTarget(Record.readBool()); 12132 C->setIsTargetSync(Record.readBool()); 12133 C->setLParenLoc(Record.readSourceLocation()); 12134 C->setVarLoc(Record.readSourceLocation()); 12135 } 12136 12137 void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) { 12138 C->setInteropVar(Record.readSubExpr()); 12139 C->setLParenLoc(Record.readSourceLocation()); 12140 C->setVarLoc(Record.readSourceLocation()); 12141 } 12142 12143 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) { 12144 C->setInteropVar(Record.readSubExpr()); 12145 C->setLParenLoc(Record.readSourceLocation()); 12146 C->setVarLoc(Record.readSourceLocation()); 12147 } 12148 12149 void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) { 12150 VisitOMPClauseWithPreInit(C); 12151 C->setCondition(Record.readSubExpr()); 12152 C->setLParenLoc(Record.readSourceLocation()); 12153 } 12154 12155 void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) { 12156 VisitOMPClauseWithPreInit(C); 12157 C->setCondition(Record.readSubExpr()); 12158 C->setLParenLoc(Record.readSourceLocation()); 12159 } 12160 12161 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {} 12162 12163 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause( 12164 OMPUnifiedSharedMemoryClause *) {} 12165 12166 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {} 12167 12168 void 12169 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) { 12170 } 12171 12172 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause( 12173 OMPAtomicDefaultMemOrderClause *C) { 12174 C->setAtomicDefaultMemOrderKind( 12175 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt())); 12176 C->setLParenLoc(Record.readSourceLocation()); 12177 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation()); 12178 } 12179 12180 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) { 12181 C->setLParenLoc(Record.readSourceLocation()); 12182 unsigned NumVars = C->varlist_size(); 12183 SmallVector<Expr *, 16> Vars; 12184 Vars.reserve(NumVars); 12185 for (unsigned i = 0; i != NumVars; ++i) 12186 Vars.push_back(Record.readSubExpr()); 12187 C->setVarRefs(Vars); 12188 Vars.clear(); 12189 for (unsigned i = 0; i != NumVars; ++i) 12190 Vars.push_back(Record.readSubExpr()); 12191 C->setPrivateCopies(Vars); 12192 } 12193 12194 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) { 12195 VisitOMPClauseWithPreInit(C); 12196 C->setLParenLoc(Record.readSourceLocation()); 12197 unsigned NumVars = C->varlist_size(); 12198 SmallVector<Expr *, 16> Vars; 12199 Vars.reserve(NumVars); 12200 for (unsigned i = 0; i != NumVars; ++i) 12201 Vars.push_back(Record.readSubExpr()); 12202 C->setVarRefs(Vars); 12203 Vars.clear(); 12204 for (unsigned i = 0; i != NumVars; ++i) 12205 Vars.push_back(Record.readSubExpr()); 12206 C->setPrivateCopies(Vars); 12207 Vars.clear(); 12208 for (unsigned i = 0; i != NumVars; ++i) 12209 Vars.push_back(Record.readSubExpr()); 12210 C->setInits(Vars); 12211 } 12212 12213 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) { 12214 VisitOMPClauseWithPostUpdate(C); 12215 C->setLParenLoc(Record.readSourceLocation()); 12216 C->setKind(Record.readEnum<OpenMPLastprivateModifier>()); 12217 C->setKindLoc(Record.readSourceLocation()); 12218 C->setColonLoc(Record.readSourceLocation()); 12219 unsigned NumVars = C->varlist_size(); 12220 SmallVector<Expr *, 16> Vars; 12221 Vars.reserve(NumVars); 12222 for (unsigned i = 0; i != NumVars; ++i) 12223 Vars.push_back(Record.readSubExpr()); 12224 C->setVarRefs(Vars); 12225 Vars.clear(); 12226 for (unsigned i = 0; i != NumVars; ++i) 12227 Vars.push_back(Record.readSubExpr()); 12228 C->setPrivateCopies(Vars); 12229 Vars.clear(); 12230 for (unsigned i = 0; i != NumVars; ++i) 12231 Vars.push_back(Record.readSubExpr()); 12232 C->setSourceExprs(Vars); 12233 Vars.clear(); 12234 for (unsigned i = 0; i != NumVars; ++i) 12235 Vars.push_back(Record.readSubExpr()); 12236 C->setDestinationExprs(Vars); 12237 Vars.clear(); 12238 for (unsigned i = 0; i != NumVars; ++i) 12239 Vars.push_back(Record.readSubExpr()); 12240 C->setAssignmentOps(Vars); 12241 } 12242 12243 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) { 12244 C->setLParenLoc(Record.readSourceLocation()); 12245 unsigned NumVars = C->varlist_size(); 12246 SmallVector<Expr *, 16> Vars; 12247 Vars.reserve(NumVars); 12248 for (unsigned i = 0; i != NumVars; ++i) 12249 Vars.push_back(Record.readSubExpr()); 12250 C->setVarRefs(Vars); 12251 } 12252 12253 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) { 12254 VisitOMPClauseWithPostUpdate(C); 12255 C->setLParenLoc(Record.readSourceLocation()); 12256 C->setModifierLoc(Record.readSourceLocation()); 12257 C->setColonLoc(Record.readSourceLocation()); 12258 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12259 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12260 C->setQualifierLoc(NNSL); 12261 C->setNameInfo(DNI); 12262 12263 unsigned NumVars = C->varlist_size(); 12264 SmallVector<Expr *, 16> Vars; 12265 Vars.reserve(NumVars); 12266 for (unsigned i = 0; i != NumVars; ++i) 12267 Vars.push_back(Record.readSubExpr()); 12268 C->setVarRefs(Vars); 12269 Vars.clear(); 12270 for (unsigned i = 0; i != NumVars; ++i) 12271 Vars.push_back(Record.readSubExpr()); 12272 C->setPrivates(Vars); 12273 Vars.clear(); 12274 for (unsigned i = 0; i != NumVars; ++i) 12275 Vars.push_back(Record.readSubExpr()); 12276 C->setLHSExprs(Vars); 12277 Vars.clear(); 12278 for (unsigned i = 0; i != NumVars; ++i) 12279 Vars.push_back(Record.readSubExpr()); 12280 C->setRHSExprs(Vars); 12281 Vars.clear(); 12282 for (unsigned i = 0; i != NumVars; ++i) 12283 Vars.push_back(Record.readSubExpr()); 12284 C->setReductionOps(Vars); 12285 if (C->getModifier() == OMPC_REDUCTION_inscan) { 12286 Vars.clear(); 12287 for (unsigned i = 0; i != NumVars; ++i) 12288 Vars.push_back(Record.readSubExpr()); 12289 C->setInscanCopyOps(Vars); 12290 Vars.clear(); 12291 for (unsigned i = 0; i != NumVars; ++i) 12292 Vars.push_back(Record.readSubExpr()); 12293 C->setInscanCopyArrayTemps(Vars); 12294 Vars.clear(); 12295 for (unsigned i = 0; i != NumVars; ++i) 12296 Vars.push_back(Record.readSubExpr()); 12297 C->setInscanCopyArrayElems(Vars); 12298 } 12299 } 12300 12301 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) { 12302 VisitOMPClauseWithPostUpdate(C); 12303 C->setLParenLoc(Record.readSourceLocation()); 12304 C->setColonLoc(Record.readSourceLocation()); 12305 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12306 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12307 C->setQualifierLoc(NNSL); 12308 C->setNameInfo(DNI); 12309 12310 unsigned NumVars = C->varlist_size(); 12311 SmallVector<Expr *, 16> Vars; 12312 Vars.reserve(NumVars); 12313 for (unsigned I = 0; I != NumVars; ++I) 12314 Vars.push_back(Record.readSubExpr()); 12315 C->setVarRefs(Vars); 12316 Vars.clear(); 12317 for (unsigned I = 0; I != NumVars; ++I) 12318 Vars.push_back(Record.readSubExpr()); 12319 C->setPrivates(Vars); 12320 Vars.clear(); 12321 for (unsigned I = 0; I != NumVars; ++I) 12322 Vars.push_back(Record.readSubExpr()); 12323 C->setLHSExprs(Vars); 12324 Vars.clear(); 12325 for (unsigned I = 0; I != NumVars; ++I) 12326 Vars.push_back(Record.readSubExpr()); 12327 C->setRHSExprs(Vars); 12328 Vars.clear(); 12329 for (unsigned I = 0; I != NumVars; ++I) 12330 Vars.push_back(Record.readSubExpr()); 12331 C->setReductionOps(Vars); 12332 } 12333 12334 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) { 12335 VisitOMPClauseWithPostUpdate(C); 12336 C->setLParenLoc(Record.readSourceLocation()); 12337 C->setColonLoc(Record.readSourceLocation()); 12338 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12339 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12340 C->setQualifierLoc(NNSL); 12341 C->setNameInfo(DNI); 12342 12343 unsigned NumVars = C->varlist_size(); 12344 SmallVector<Expr *, 16> Vars; 12345 Vars.reserve(NumVars); 12346 for (unsigned I = 0; I != NumVars; ++I) 12347 Vars.push_back(Record.readSubExpr()); 12348 C->setVarRefs(Vars); 12349 Vars.clear(); 12350 for (unsigned I = 0; I != NumVars; ++I) 12351 Vars.push_back(Record.readSubExpr()); 12352 C->setPrivates(Vars); 12353 Vars.clear(); 12354 for (unsigned I = 0; I != NumVars; ++I) 12355 Vars.push_back(Record.readSubExpr()); 12356 C->setLHSExprs(Vars); 12357 Vars.clear(); 12358 for (unsigned I = 0; I != NumVars; ++I) 12359 Vars.push_back(Record.readSubExpr()); 12360 C->setRHSExprs(Vars); 12361 Vars.clear(); 12362 for (unsigned I = 0; I != NumVars; ++I) 12363 Vars.push_back(Record.readSubExpr()); 12364 C->setReductionOps(Vars); 12365 Vars.clear(); 12366 for (unsigned I = 0; I != NumVars; ++I) 12367 Vars.push_back(Record.readSubExpr()); 12368 C->setTaskgroupDescriptors(Vars); 12369 } 12370 12371 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) { 12372 VisitOMPClauseWithPostUpdate(C); 12373 C->setLParenLoc(Record.readSourceLocation()); 12374 C->setColonLoc(Record.readSourceLocation()); 12375 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt())); 12376 C->setModifierLoc(Record.readSourceLocation()); 12377 unsigned NumVars = C->varlist_size(); 12378 SmallVector<Expr *, 16> Vars; 12379 Vars.reserve(NumVars); 12380 for (unsigned i = 0; i != NumVars; ++i) 12381 Vars.push_back(Record.readSubExpr()); 12382 C->setVarRefs(Vars); 12383 Vars.clear(); 12384 for (unsigned i = 0; i != NumVars; ++i) 12385 Vars.push_back(Record.readSubExpr()); 12386 C->setPrivates(Vars); 12387 Vars.clear(); 12388 for (unsigned i = 0; i != NumVars; ++i) 12389 Vars.push_back(Record.readSubExpr()); 12390 C->setInits(Vars); 12391 Vars.clear(); 12392 for (unsigned i = 0; i != NumVars; ++i) 12393 Vars.push_back(Record.readSubExpr()); 12394 C->setUpdates(Vars); 12395 Vars.clear(); 12396 for (unsigned i = 0; i != NumVars; ++i) 12397 Vars.push_back(Record.readSubExpr()); 12398 C->setFinals(Vars); 12399 C->setStep(Record.readSubExpr()); 12400 C->setCalcStep(Record.readSubExpr()); 12401 Vars.clear(); 12402 for (unsigned I = 0; I != NumVars + 1; ++I) 12403 Vars.push_back(Record.readSubExpr()); 12404 C->setUsedExprs(Vars); 12405 } 12406 12407 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) { 12408 C->setLParenLoc(Record.readSourceLocation()); 12409 C->setColonLoc(Record.readSourceLocation()); 12410 unsigned NumVars = C->varlist_size(); 12411 SmallVector<Expr *, 16> Vars; 12412 Vars.reserve(NumVars); 12413 for (unsigned i = 0; i != NumVars; ++i) 12414 Vars.push_back(Record.readSubExpr()); 12415 C->setVarRefs(Vars); 12416 C->setAlignment(Record.readSubExpr()); 12417 } 12418 12419 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) { 12420 C->setLParenLoc(Record.readSourceLocation()); 12421 unsigned NumVars = C->varlist_size(); 12422 SmallVector<Expr *, 16> Exprs; 12423 Exprs.reserve(NumVars); 12424 for (unsigned i = 0; i != NumVars; ++i) 12425 Exprs.push_back(Record.readSubExpr()); 12426 C->setVarRefs(Exprs); 12427 Exprs.clear(); 12428 for (unsigned i = 0; i != NumVars; ++i) 12429 Exprs.push_back(Record.readSubExpr()); 12430 C->setSourceExprs(Exprs); 12431 Exprs.clear(); 12432 for (unsigned i = 0; i != NumVars; ++i) 12433 Exprs.push_back(Record.readSubExpr()); 12434 C->setDestinationExprs(Exprs); 12435 Exprs.clear(); 12436 for (unsigned i = 0; i != NumVars; ++i) 12437 Exprs.push_back(Record.readSubExpr()); 12438 C->setAssignmentOps(Exprs); 12439 } 12440 12441 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) { 12442 C->setLParenLoc(Record.readSourceLocation()); 12443 unsigned NumVars = C->varlist_size(); 12444 SmallVector<Expr *, 16> Exprs; 12445 Exprs.reserve(NumVars); 12446 for (unsigned i = 0; i != NumVars; ++i) 12447 Exprs.push_back(Record.readSubExpr()); 12448 C->setVarRefs(Exprs); 12449 Exprs.clear(); 12450 for (unsigned i = 0; i != NumVars; ++i) 12451 Exprs.push_back(Record.readSubExpr()); 12452 C->setSourceExprs(Exprs); 12453 Exprs.clear(); 12454 for (unsigned i = 0; i != NumVars; ++i) 12455 Exprs.push_back(Record.readSubExpr()); 12456 C->setDestinationExprs(Exprs); 12457 Exprs.clear(); 12458 for (unsigned i = 0; i != NumVars; ++i) 12459 Exprs.push_back(Record.readSubExpr()); 12460 C->setAssignmentOps(Exprs); 12461 } 12462 12463 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) { 12464 C->setLParenLoc(Record.readSourceLocation()); 12465 unsigned NumVars = C->varlist_size(); 12466 SmallVector<Expr *, 16> Vars; 12467 Vars.reserve(NumVars); 12468 for (unsigned i = 0; i != NumVars; ++i) 12469 Vars.push_back(Record.readSubExpr()); 12470 C->setVarRefs(Vars); 12471 } 12472 12473 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) { 12474 C->setDepobj(Record.readSubExpr()); 12475 C->setLParenLoc(Record.readSourceLocation()); 12476 } 12477 12478 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) { 12479 C->setLParenLoc(Record.readSourceLocation()); 12480 C->setModifier(Record.readSubExpr()); 12481 C->setDependencyKind( 12482 static_cast<OpenMPDependClauseKind>(Record.readInt())); 12483 C->setDependencyLoc(Record.readSourceLocation()); 12484 C->setColonLoc(Record.readSourceLocation()); 12485 unsigned NumVars = C->varlist_size(); 12486 SmallVector<Expr *, 16> Vars; 12487 Vars.reserve(NumVars); 12488 for (unsigned I = 0; I != NumVars; ++I) 12489 Vars.push_back(Record.readSubExpr()); 12490 C->setVarRefs(Vars); 12491 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I) 12492 C->setLoopData(I, Record.readSubExpr()); 12493 } 12494 12495 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) { 12496 VisitOMPClauseWithPreInit(C); 12497 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>()); 12498 C->setDevice(Record.readSubExpr()); 12499 C->setModifierLoc(Record.readSourceLocation()); 12500 C->setLParenLoc(Record.readSourceLocation()); 12501 } 12502 12503 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) { 12504 C->setLParenLoc(Record.readSourceLocation()); 12505 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) { 12506 C->setMapTypeModifier( 12507 I, static_cast<OpenMPMapModifierKind>(Record.readInt())); 12508 C->setMapTypeModifierLoc(I, Record.readSourceLocation()); 12509 } 12510 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12511 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12512 C->setMapType( 12513 static_cast<OpenMPMapClauseKind>(Record.readInt())); 12514 C->setMapLoc(Record.readSourceLocation()); 12515 C->setColonLoc(Record.readSourceLocation()); 12516 auto NumVars = C->varlist_size(); 12517 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12518 auto TotalLists = C->getTotalComponentListNum(); 12519 auto TotalComponents = C->getTotalComponentsNum(); 12520 12521 SmallVector<Expr *, 16> Vars; 12522 Vars.reserve(NumVars); 12523 for (unsigned i = 0; i != NumVars; ++i) 12524 Vars.push_back(Record.readExpr()); 12525 C->setVarRefs(Vars); 12526 12527 SmallVector<Expr *, 16> UDMappers; 12528 UDMappers.reserve(NumVars); 12529 for (unsigned I = 0; I < NumVars; ++I) 12530 UDMappers.push_back(Record.readExpr()); 12531 C->setUDMapperRefs(UDMappers); 12532 12533 SmallVector<ValueDecl *, 16> Decls; 12534 Decls.reserve(UniqueDecls); 12535 for (unsigned i = 0; i < UniqueDecls; ++i) 12536 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12537 C->setUniqueDecls(Decls); 12538 12539 SmallVector<unsigned, 16> ListsPerDecl; 12540 ListsPerDecl.reserve(UniqueDecls); 12541 for (unsigned i = 0; i < UniqueDecls; ++i) 12542 ListsPerDecl.push_back(Record.readInt()); 12543 C->setDeclNumLists(ListsPerDecl); 12544 12545 SmallVector<unsigned, 32> ListSizes; 12546 ListSizes.reserve(TotalLists); 12547 for (unsigned i = 0; i < TotalLists; ++i) 12548 ListSizes.push_back(Record.readInt()); 12549 C->setComponentListSizes(ListSizes); 12550 12551 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12552 Components.reserve(TotalComponents); 12553 for (unsigned i = 0; i < TotalComponents; ++i) { 12554 Expr *AssociatedExprPr = Record.readExpr(); 12555 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12556 Components.emplace_back(AssociatedExprPr, AssociatedDecl, 12557 /*IsNonContiguous=*/false); 12558 } 12559 C->setComponents(Components, ListSizes); 12560 } 12561 12562 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) { 12563 C->setLParenLoc(Record.readSourceLocation()); 12564 C->setColonLoc(Record.readSourceLocation()); 12565 C->setAllocator(Record.readSubExpr()); 12566 unsigned NumVars = C->varlist_size(); 12567 SmallVector<Expr *, 16> Vars; 12568 Vars.reserve(NumVars); 12569 for (unsigned i = 0; i != NumVars; ++i) 12570 Vars.push_back(Record.readSubExpr()); 12571 C->setVarRefs(Vars); 12572 } 12573 12574 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) { 12575 VisitOMPClauseWithPreInit(C); 12576 C->setNumTeams(Record.readSubExpr()); 12577 C->setLParenLoc(Record.readSourceLocation()); 12578 } 12579 12580 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) { 12581 VisitOMPClauseWithPreInit(C); 12582 C->setThreadLimit(Record.readSubExpr()); 12583 C->setLParenLoc(Record.readSourceLocation()); 12584 } 12585 12586 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) { 12587 VisitOMPClauseWithPreInit(C); 12588 C->setPriority(Record.readSubExpr()); 12589 C->setLParenLoc(Record.readSourceLocation()); 12590 } 12591 12592 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) { 12593 VisitOMPClauseWithPreInit(C); 12594 C->setGrainsize(Record.readSubExpr()); 12595 C->setLParenLoc(Record.readSourceLocation()); 12596 } 12597 12598 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) { 12599 VisitOMPClauseWithPreInit(C); 12600 C->setNumTasks(Record.readSubExpr()); 12601 C->setLParenLoc(Record.readSourceLocation()); 12602 } 12603 12604 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) { 12605 C->setHint(Record.readSubExpr()); 12606 C->setLParenLoc(Record.readSourceLocation()); 12607 } 12608 12609 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) { 12610 VisitOMPClauseWithPreInit(C); 12611 C->setDistScheduleKind( 12612 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt())); 12613 C->setChunkSize(Record.readSubExpr()); 12614 C->setLParenLoc(Record.readSourceLocation()); 12615 C->setDistScheduleKindLoc(Record.readSourceLocation()); 12616 C->setCommaLoc(Record.readSourceLocation()); 12617 } 12618 12619 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) { 12620 C->setDefaultmapKind( 12621 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt())); 12622 C->setDefaultmapModifier( 12623 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt())); 12624 C->setLParenLoc(Record.readSourceLocation()); 12625 C->setDefaultmapModifierLoc(Record.readSourceLocation()); 12626 C->setDefaultmapKindLoc(Record.readSourceLocation()); 12627 } 12628 12629 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) { 12630 C->setLParenLoc(Record.readSourceLocation()); 12631 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) { 12632 C->setMotionModifier( 12633 I, static_cast<OpenMPMotionModifierKind>(Record.readInt())); 12634 C->setMotionModifierLoc(I, Record.readSourceLocation()); 12635 } 12636 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12637 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12638 C->setColonLoc(Record.readSourceLocation()); 12639 auto NumVars = C->varlist_size(); 12640 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12641 auto TotalLists = C->getTotalComponentListNum(); 12642 auto TotalComponents = C->getTotalComponentsNum(); 12643 12644 SmallVector<Expr *, 16> Vars; 12645 Vars.reserve(NumVars); 12646 for (unsigned i = 0; i != NumVars; ++i) 12647 Vars.push_back(Record.readSubExpr()); 12648 C->setVarRefs(Vars); 12649 12650 SmallVector<Expr *, 16> UDMappers; 12651 UDMappers.reserve(NumVars); 12652 for (unsigned I = 0; I < NumVars; ++I) 12653 UDMappers.push_back(Record.readSubExpr()); 12654 C->setUDMapperRefs(UDMappers); 12655 12656 SmallVector<ValueDecl *, 16> Decls; 12657 Decls.reserve(UniqueDecls); 12658 for (unsigned i = 0; i < UniqueDecls; ++i) 12659 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12660 C->setUniqueDecls(Decls); 12661 12662 SmallVector<unsigned, 16> ListsPerDecl; 12663 ListsPerDecl.reserve(UniqueDecls); 12664 for (unsigned i = 0; i < UniqueDecls; ++i) 12665 ListsPerDecl.push_back(Record.readInt()); 12666 C->setDeclNumLists(ListsPerDecl); 12667 12668 SmallVector<unsigned, 32> ListSizes; 12669 ListSizes.reserve(TotalLists); 12670 for (unsigned i = 0; i < TotalLists; ++i) 12671 ListSizes.push_back(Record.readInt()); 12672 C->setComponentListSizes(ListSizes); 12673 12674 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12675 Components.reserve(TotalComponents); 12676 for (unsigned i = 0; i < TotalComponents; ++i) { 12677 Expr *AssociatedExprPr = Record.readSubExpr(); 12678 bool IsNonContiguous = Record.readBool(); 12679 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12680 Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous); 12681 } 12682 C->setComponents(Components, ListSizes); 12683 } 12684 12685 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) { 12686 C->setLParenLoc(Record.readSourceLocation()); 12687 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) { 12688 C->setMotionModifier( 12689 I, static_cast<OpenMPMotionModifierKind>(Record.readInt())); 12690 C->setMotionModifierLoc(I, Record.readSourceLocation()); 12691 } 12692 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12693 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12694 C->setColonLoc(Record.readSourceLocation()); 12695 auto NumVars = C->varlist_size(); 12696 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12697 auto TotalLists = C->getTotalComponentListNum(); 12698 auto TotalComponents = C->getTotalComponentsNum(); 12699 12700 SmallVector<Expr *, 16> Vars; 12701 Vars.reserve(NumVars); 12702 for (unsigned i = 0; i != NumVars; ++i) 12703 Vars.push_back(Record.readSubExpr()); 12704 C->setVarRefs(Vars); 12705 12706 SmallVector<Expr *, 16> UDMappers; 12707 UDMappers.reserve(NumVars); 12708 for (unsigned I = 0; I < NumVars; ++I) 12709 UDMappers.push_back(Record.readSubExpr()); 12710 C->setUDMapperRefs(UDMappers); 12711 12712 SmallVector<ValueDecl *, 16> Decls; 12713 Decls.reserve(UniqueDecls); 12714 for (unsigned i = 0; i < UniqueDecls; ++i) 12715 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12716 C->setUniqueDecls(Decls); 12717 12718 SmallVector<unsigned, 16> ListsPerDecl; 12719 ListsPerDecl.reserve(UniqueDecls); 12720 for (unsigned i = 0; i < UniqueDecls; ++i) 12721 ListsPerDecl.push_back(Record.readInt()); 12722 C->setDeclNumLists(ListsPerDecl); 12723 12724 SmallVector<unsigned, 32> ListSizes; 12725 ListSizes.reserve(TotalLists); 12726 for (unsigned i = 0; i < TotalLists; ++i) 12727 ListSizes.push_back(Record.readInt()); 12728 C->setComponentListSizes(ListSizes); 12729 12730 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12731 Components.reserve(TotalComponents); 12732 for (unsigned i = 0; i < TotalComponents; ++i) { 12733 Expr *AssociatedExprPr = Record.readSubExpr(); 12734 bool IsNonContiguous = Record.readBool(); 12735 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12736 Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous); 12737 } 12738 C->setComponents(Components, ListSizes); 12739 } 12740 12741 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) { 12742 C->setLParenLoc(Record.readSourceLocation()); 12743 auto NumVars = C->varlist_size(); 12744 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12745 auto TotalLists = C->getTotalComponentListNum(); 12746 auto TotalComponents = C->getTotalComponentsNum(); 12747 12748 SmallVector<Expr *, 16> Vars; 12749 Vars.reserve(NumVars); 12750 for (unsigned i = 0; i != NumVars; ++i) 12751 Vars.push_back(Record.readSubExpr()); 12752 C->setVarRefs(Vars); 12753 Vars.clear(); 12754 for (unsigned i = 0; i != NumVars; ++i) 12755 Vars.push_back(Record.readSubExpr()); 12756 C->setPrivateCopies(Vars); 12757 Vars.clear(); 12758 for (unsigned i = 0; i != NumVars; ++i) 12759 Vars.push_back(Record.readSubExpr()); 12760 C->setInits(Vars); 12761 12762 SmallVector<ValueDecl *, 16> Decls; 12763 Decls.reserve(UniqueDecls); 12764 for (unsigned i = 0; i < UniqueDecls; ++i) 12765 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12766 C->setUniqueDecls(Decls); 12767 12768 SmallVector<unsigned, 16> ListsPerDecl; 12769 ListsPerDecl.reserve(UniqueDecls); 12770 for (unsigned i = 0; i < UniqueDecls; ++i) 12771 ListsPerDecl.push_back(Record.readInt()); 12772 C->setDeclNumLists(ListsPerDecl); 12773 12774 SmallVector<unsigned, 32> ListSizes; 12775 ListSizes.reserve(TotalLists); 12776 for (unsigned i = 0; i < TotalLists; ++i) 12777 ListSizes.push_back(Record.readInt()); 12778 C->setComponentListSizes(ListSizes); 12779 12780 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12781 Components.reserve(TotalComponents); 12782 for (unsigned i = 0; i < TotalComponents; ++i) { 12783 auto *AssociatedExprPr = Record.readSubExpr(); 12784 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12785 Components.emplace_back(AssociatedExprPr, AssociatedDecl, 12786 /*IsNonContiguous=*/false); 12787 } 12788 C->setComponents(Components, ListSizes); 12789 } 12790 12791 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) { 12792 C->setLParenLoc(Record.readSourceLocation()); 12793 auto NumVars = C->varlist_size(); 12794 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12795 auto TotalLists = C->getTotalComponentListNum(); 12796 auto TotalComponents = C->getTotalComponentsNum(); 12797 12798 SmallVector<Expr *, 16> Vars; 12799 Vars.reserve(NumVars); 12800 for (unsigned i = 0; i != NumVars; ++i) 12801 Vars.push_back(Record.readSubExpr()); 12802 C->setVarRefs(Vars); 12803 12804 SmallVector<ValueDecl *, 16> Decls; 12805 Decls.reserve(UniqueDecls); 12806 for (unsigned i = 0; i < UniqueDecls; ++i) 12807 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12808 C->setUniqueDecls(Decls); 12809 12810 SmallVector<unsigned, 16> ListsPerDecl; 12811 ListsPerDecl.reserve(UniqueDecls); 12812 for (unsigned i = 0; i < UniqueDecls; ++i) 12813 ListsPerDecl.push_back(Record.readInt()); 12814 C->setDeclNumLists(ListsPerDecl); 12815 12816 SmallVector<unsigned, 32> ListSizes; 12817 ListSizes.reserve(TotalLists); 12818 for (unsigned i = 0; i < TotalLists; ++i) 12819 ListSizes.push_back(Record.readInt()); 12820 C->setComponentListSizes(ListSizes); 12821 12822 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12823 Components.reserve(TotalComponents); 12824 for (unsigned i = 0; i < TotalComponents; ++i) { 12825 Expr *AssociatedExpr = Record.readSubExpr(); 12826 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12827 Components.emplace_back(AssociatedExpr, AssociatedDecl, 12828 /*IsNonContiguous*/ false); 12829 } 12830 C->setComponents(Components, ListSizes); 12831 } 12832 12833 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) { 12834 C->setLParenLoc(Record.readSourceLocation()); 12835 auto NumVars = C->varlist_size(); 12836 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12837 auto TotalLists = C->getTotalComponentListNum(); 12838 auto TotalComponents = C->getTotalComponentsNum(); 12839 12840 SmallVector<Expr *, 16> Vars; 12841 Vars.reserve(NumVars); 12842 for (unsigned i = 0; i != NumVars; ++i) 12843 Vars.push_back(Record.readSubExpr()); 12844 C->setVarRefs(Vars); 12845 Vars.clear(); 12846 12847 SmallVector<ValueDecl *, 16> Decls; 12848 Decls.reserve(UniqueDecls); 12849 for (unsigned i = 0; i < UniqueDecls; ++i) 12850 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12851 C->setUniqueDecls(Decls); 12852 12853 SmallVector<unsigned, 16> ListsPerDecl; 12854 ListsPerDecl.reserve(UniqueDecls); 12855 for (unsigned i = 0; i < UniqueDecls; ++i) 12856 ListsPerDecl.push_back(Record.readInt()); 12857 C->setDeclNumLists(ListsPerDecl); 12858 12859 SmallVector<unsigned, 32> ListSizes; 12860 ListSizes.reserve(TotalLists); 12861 for (unsigned i = 0; i < TotalLists; ++i) 12862 ListSizes.push_back(Record.readInt()); 12863 C->setComponentListSizes(ListSizes); 12864 12865 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12866 Components.reserve(TotalComponents); 12867 for (unsigned i = 0; i < TotalComponents; ++i) { 12868 Expr *AssociatedExpr = Record.readSubExpr(); 12869 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12870 Components.emplace_back(AssociatedExpr, AssociatedDecl, 12871 /*IsNonContiguous=*/false); 12872 } 12873 C->setComponents(Components, ListSizes); 12874 } 12875 12876 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) { 12877 C->setLParenLoc(Record.readSourceLocation()); 12878 unsigned NumVars = C->varlist_size(); 12879 SmallVector<Expr *, 16> Vars; 12880 Vars.reserve(NumVars); 12881 for (unsigned i = 0; i != NumVars; ++i) 12882 Vars.push_back(Record.readSubExpr()); 12883 C->setVarRefs(Vars); 12884 Vars.clear(); 12885 Vars.reserve(NumVars); 12886 for (unsigned i = 0; i != NumVars; ++i) 12887 Vars.push_back(Record.readSubExpr()); 12888 C->setPrivateRefs(Vars); 12889 } 12890 12891 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) { 12892 C->setLParenLoc(Record.readSourceLocation()); 12893 unsigned NumVars = C->varlist_size(); 12894 SmallVector<Expr *, 16> Vars; 12895 Vars.reserve(NumVars); 12896 for (unsigned i = 0; i != NumVars; ++i) 12897 Vars.push_back(Record.readSubExpr()); 12898 C->setVarRefs(Vars); 12899 } 12900 12901 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) { 12902 C->setLParenLoc(Record.readSourceLocation()); 12903 unsigned NumVars = C->varlist_size(); 12904 SmallVector<Expr *, 16> Vars; 12905 Vars.reserve(NumVars); 12906 for (unsigned i = 0; i != NumVars; ++i) 12907 Vars.push_back(Record.readSubExpr()); 12908 C->setVarRefs(Vars); 12909 } 12910 12911 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) { 12912 C->setLParenLoc(Record.readSourceLocation()); 12913 unsigned NumOfAllocators = C->getNumberOfAllocators(); 12914 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data; 12915 Data.reserve(NumOfAllocators); 12916 for (unsigned I = 0; I != NumOfAllocators; ++I) { 12917 OMPUsesAllocatorsClause::Data &D = Data.emplace_back(); 12918 D.Allocator = Record.readSubExpr(); 12919 D.AllocatorTraits = Record.readSubExpr(); 12920 D.LParenLoc = Record.readSourceLocation(); 12921 D.RParenLoc = Record.readSourceLocation(); 12922 } 12923 C->setAllocatorsData(Data); 12924 } 12925 12926 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) { 12927 C->setLParenLoc(Record.readSourceLocation()); 12928 C->setModifier(Record.readSubExpr()); 12929 C->setColonLoc(Record.readSourceLocation()); 12930 unsigned NumOfLocators = C->varlist_size(); 12931 SmallVector<Expr *, 4> Locators; 12932 Locators.reserve(NumOfLocators); 12933 for (unsigned I = 0; I != NumOfLocators; ++I) 12934 Locators.push_back(Record.readSubExpr()); 12935 C->setVarRefs(Locators); 12936 } 12937 12938 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) { 12939 C->setKind(Record.readEnum<OpenMPOrderClauseKind>()); 12940 C->setLParenLoc(Record.readSourceLocation()); 12941 C->setKindKwLoc(Record.readSourceLocation()); 12942 } 12943 12944 void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) { 12945 VisitOMPClauseWithPreInit(C); 12946 C->setThreadID(Record.readSubExpr()); 12947 C->setLParenLoc(Record.readSourceLocation()); 12948 } 12949 12950 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() { 12951 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo(); 12952 TI.Sets.resize(readUInt32()); 12953 for (auto &Set : TI.Sets) { 12954 Set.Kind = readEnum<llvm::omp::TraitSet>(); 12955 Set.Selectors.resize(readUInt32()); 12956 for (auto &Selector : Set.Selectors) { 12957 Selector.Kind = readEnum<llvm::omp::TraitSelector>(); 12958 Selector.ScoreOrCondition = nullptr; 12959 if (readBool()) 12960 Selector.ScoreOrCondition = readExprRef(); 12961 Selector.Properties.resize(readUInt32()); 12962 for (auto &Property : Selector.Properties) 12963 Property.Kind = readEnum<llvm::omp::TraitProperty>(); 12964 } 12965 } 12966 return &TI; 12967 } 12968 12969 void ASTRecordReader::readOMPChildren(OMPChildren *Data) { 12970 if (!Data) 12971 return; 12972 if (Reader->ReadingKind == ASTReader::Read_Stmt) { 12973 // Skip NumClauses, NumChildren and HasAssociatedStmt fields. 12974 skipInts(3); 12975 } 12976 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses()); 12977 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I) 12978 Clauses[I] = readOMPClause(); 12979 Data->setClauses(Clauses); 12980 if (Data->hasAssociatedStmt()) 12981 Data->setAssociatedStmt(readStmt()); 12982 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I) 12983 Data->getChildren()[I] = readStmt(); 12984 } 12985