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 146 //===----------------------------------------------------------------------===// 147 // ChainedASTReaderListener implementation 148 //===----------------------------------------------------------------------===// 149 150 bool 151 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) { 152 return First->ReadFullVersionInformation(FullVersion) || 153 Second->ReadFullVersionInformation(FullVersion); 154 } 155 156 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) { 157 First->ReadModuleName(ModuleName); 158 Second->ReadModuleName(ModuleName); 159 } 160 161 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) { 162 First->ReadModuleMapFile(ModuleMapPath); 163 Second->ReadModuleMapFile(ModuleMapPath); 164 } 165 166 bool 167 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts, 168 bool Complain, 169 bool AllowCompatibleDifferences) { 170 return First->ReadLanguageOptions(LangOpts, Complain, 171 AllowCompatibleDifferences) || 172 Second->ReadLanguageOptions(LangOpts, Complain, 173 AllowCompatibleDifferences); 174 } 175 176 bool ChainedASTReaderListener::ReadTargetOptions( 177 const TargetOptions &TargetOpts, bool Complain, 178 bool AllowCompatibleDifferences) { 179 return First->ReadTargetOptions(TargetOpts, Complain, 180 AllowCompatibleDifferences) || 181 Second->ReadTargetOptions(TargetOpts, Complain, 182 AllowCompatibleDifferences); 183 } 184 185 bool ChainedASTReaderListener::ReadDiagnosticOptions( 186 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 187 return First->ReadDiagnosticOptions(DiagOpts, Complain) || 188 Second->ReadDiagnosticOptions(DiagOpts, Complain); 189 } 190 191 bool 192 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts, 193 bool Complain) { 194 return First->ReadFileSystemOptions(FSOpts, Complain) || 195 Second->ReadFileSystemOptions(FSOpts, Complain); 196 } 197 198 bool ChainedASTReaderListener::ReadHeaderSearchOptions( 199 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath, 200 bool Complain) { 201 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 202 Complain) || 203 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 204 Complain); 205 } 206 207 bool ChainedASTReaderListener::ReadPreprocessorOptions( 208 const PreprocessorOptions &PPOpts, bool Complain, 209 std::string &SuggestedPredefines) { 210 return First->ReadPreprocessorOptions(PPOpts, Complain, 211 SuggestedPredefines) || 212 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines); 213 } 214 215 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M, 216 unsigned Value) { 217 First->ReadCounter(M, Value); 218 Second->ReadCounter(M, Value); 219 } 220 221 bool ChainedASTReaderListener::needsInputFileVisitation() { 222 return First->needsInputFileVisitation() || 223 Second->needsInputFileVisitation(); 224 } 225 226 bool ChainedASTReaderListener::needsSystemInputFileVisitation() { 227 return First->needsSystemInputFileVisitation() || 228 Second->needsSystemInputFileVisitation(); 229 } 230 231 void ChainedASTReaderListener::visitModuleFile(StringRef Filename, 232 ModuleKind Kind) { 233 First->visitModuleFile(Filename, Kind); 234 Second->visitModuleFile(Filename, Kind); 235 } 236 237 bool ChainedASTReaderListener::visitInputFile(StringRef Filename, 238 bool isSystem, 239 bool isOverridden, 240 bool isExplicitModule) { 241 bool Continue = false; 242 if (First->needsInputFileVisitation() && 243 (!isSystem || First->needsSystemInputFileVisitation())) 244 Continue |= First->visitInputFile(Filename, isSystem, isOverridden, 245 isExplicitModule); 246 if (Second->needsInputFileVisitation() && 247 (!isSystem || Second->needsSystemInputFileVisitation())) 248 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden, 249 isExplicitModule); 250 return Continue; 251 } 252 253 void ChainedASTReaderListener::readModuleFileExtension( 254 const ModuleFileExtensionMetadata &Metadata) { 255 First->readModuleFileExtension(Metadata); 256 Second->readModuleFileExtension(Metadata); 257 } 258 259 //===----------------------------------------------------------------------===// 260 // PCH validator implementation 261 //===----------------------------------------------------------------------===// 262 263 ASTReaderListener::~ASTReaderListener() = default; 264 265 /// Compare the given set of language options against an existing set of 266 /// language options. 267 /// 268 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 269 /// \param AllowCompatibleDifferences If true, differences between compatible 270 /// language options will be permitted. 271 /// 272 /// \returns true if the languagae options mis-match, false otherwise. 273 static bool checkLanguageOptions(const LangOptions &LangOpts, 274 const LangOptions &ExistingLangOpts, 275 DiagnosticsEngine *Diags, 276 bool AllowCompatibleDifferences = true) { 277 #define LANGOPT(Name, Bits, Default, Description) \ 278 if (ExistingLangOpts.Name != LangOpts.Name) { \ 279 if (Diags) \ 280 Diags->Report(diag::err_pch_langopt_mismatch) \ 281 << Description << LangOpts.Name << ExistingLangOpts.Name; \ 282 return true; \ 283 } 284 285 #define VALUE_LANGOPT(Name, Bits, Default, Description) \ 286 if (ExistingLangOpts.Name != LangOpts.Name) { \ 287 if (Diags) \ 288 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 289 << Description; \ 290 return true; \ 291 } 292 293 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 294 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \ 295 if (Diags) \ 296 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 297 << Description; \ 298 return true; \ 299 } 300 301 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \ 302 if (!AllowCompatibleDifferences) \ 303 LANGOPT(Name, Bits, Default, Description) 304 305 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \ 306 if (!AllowCompatibleDifferences) \ 307 ENUM_LANGOPT(Name, Bits, Default, Description) 308 309 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \ 310 if (!AllowCompatibleDifferences) \ 311 VALUE_LANGOPT(Name, Bits, Default, Description) 312 313 #define BENIGN_LANGOPT(Name, Bits, Default, Description) 314 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) 315 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description) 316 #include "clang/Basic/LangOptions.def" 317 318 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) { 319 if (Diags) 320 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features"; 321 return true; 322 } 323 324 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) { 325 if (Diags) 326 Diags->Report(diag::err_pch_langopt_value_mismatch) 327 << "target Objective-C runtime"; 328 return true; 329 } 330 331 if (ExistingLangOpts.CommentOpts.BlockCommandNames != 332 LangOpts.CommentOpts.BlockCommandNames) { 333 if (Diags) 334 Diags->Report(diag::err_pch_langopt_value_mismatch) 335 << "block command names"; 336 return true; 337 } 338 339 // Sanitizer feature mismatches are treated as compatible differences. If 340 // compatible differences aren't allowed, we still only want to check for 341 // mismatches of non-modular sanitizers (the only ones which can affect AST 342 // generation). 343 if (!AllowCompatibleDifferences) { 344 SanitizerMask ModularSanitizers = getPPTransparentSanitizers(); 345 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize; 346 SanitizerSet ImportedSanitizers = LangOpts.Sanitize; 347 ExistingSanitizers.clear(ModularSanitizers); 348 ImportedSanitizers.clear(ModularSanitizers); 349 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) { 350 const std::string Flag = "-fsanitize="; 351 if (Diags) { 352 #define SANITIZER(NAME, ID) \ 353 { \ 354 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \ 355 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \ 356 if (InExistingModule != InImportedModule) \ 357 Diags->Report(diag::err_pch_targetopt_feature_mismatch) \ 358 << InExistingModule << (Flag + NAME); \ 359 } 360 #include "clang/Basic/Sanitizers.def" 361 } 362 return true; 363 } 364 } 365 366 return false; 367 } 368 369 /// Compare the given set of target options against an existing set of 370 /// target options. 371 /// 372 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 373 /// 374 /// \returns true if the target options mis-match, false otherwise. 375 static bool checkTargetOptions(const TargetOptions &TargetOpts, 376 const TargetOptions &ExistingTargetOpts, 377 DiagnosticsEngine *Diags, 378 bool AllowCompatibleDifferences = true) { 379 #define CHECK_TARGET_OPT(Field, Name) \ 380 if (TargetOpts.Field != ExistingTargetOpts.Field) { \ 381 if (Diags) \ 382 Diags->Report(diag::err_pch_targetopt_mismatch) \ 383 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \ 384 return true; \ 385 } 386 387 // The triple and ABI must match exactly. 388 CHECK_TARGET_OPT(Triple, "target"); 389 CHECK_TARGET_OPT(ABI, "target ABI"); 390 391 // We can tolerate different CPUs in many cases, notably when one CPU 392 // supports a strict superset of another. When allowing compatible 393 // differences skip this check. 394 if (!AllowCompatibleDifferences) { 395 CHECK_TARGET_OPT(CPU, "target CPU"); 396 CHECK_TARGET_OPT(TuneCPU, "tune CPU"); 397 } 398 399 #undef CHECK_TARGET_OPT 400 401 // Compare feature sets. 402 SmallVector<StringRef, 4> ExistingFeatures( 403 ExistingTargetOpts.FeaturesAsWritten.begin(), 404 ExistingTargetOpts.FeaturesAsWritten.end()); 405 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(), 406 TargetOpts.FeaturesAsWritten.end()); 407 llvm::sort(ExistingFeatures); 408 llvm::sort(ReadFeatures); 409 410 // We compute the set difference in both directions explicitly so that we can 411 // diagnose the differences differently. 412 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures; 413 std::set_difference( 414 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(), 415 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures)); 416 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(), 417 ExistingFeatures.begin(), ExistingFeatures.end(), 418 std::back_inserter(UnmatchedReadFeatures)); 419 420 // If we are allowing compatible differences and the read feature set is 421 // a strict subset of the existing feature set, there is nothing to diagnose. 422 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty()) 423 return false; 424 425 if (Diags) { 426 for (StringRef Feature : UnmatchedReadFeatures) 427 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 428 << /* is-existing-feature */ false << Feature; 429 for (StringRef Feature : UnmatchedExistingFeatures) 430 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 431 << /* is-existing-feature */ true << Feature; 432 } 433 434 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty(); 435 } 436 437 bool 438 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts, 439 bool Complain, 440 bool AllowCompatibleDifferences) { 441 const LangOptions &ExistingLangOpts = PP.getLangOpts(); 442 return checkLanguageOptions(LangOpts, ExistingLangOpts, 443 Complain ? &Reader.Diags : nullptr, 444 AllowCompatibleDifferences); 445 } 446 447 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts, 448 bool Complain, 449 bool AllowCompatibleDifferences) { 450 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts(); 451 return checkTargetOptions(TargetOpts, ExistingTargetOpts, 452 Complain ? &Reader.Diags : nullptr, 453 AllowCompatibleDifferences); 454 } 455 456 namespace { 457 458 using MacroDefinitionsMap = 459 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>; 460 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>; 461 462 } // namespace 463 464 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags, 465 DiagnosticsEngine &Diags, 466 bool Complain) { 467 using Level = DiagnosticsEngine::Level; 468 469 // Check current mappings for new -Werror mappings, and the stored mappings 470 // for cases that were explicitly mapped to *not* be errors that are now 471 // errors because of options like -Werror. 472 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags }; 473 474 for (DiagnosticsEngine *MappingSource : MappingSources) { 475 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) { 476 diag::kind DiagID = DiagIDMappingPair.first; 477 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation()); 478 if (CurLevel < DiagnosticsEngine::Error) 479 continue; // not significant 480 Level StoredLevel = 481 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation()); 482 if (StoredLevel < DiagnosticsEngine::Error) { 483 if (Complain) 484 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" + 485 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str(); 486 return true; 487 } 488 } 489 } 490 491 return false; 492 } 493 494 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) { 495 diag::Severity Ext = Diags.getExtensionHandlingBehavior(); 496 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors()) 497 return true; 498 return Ext >= diag::Severity::Error; 499 } 500 501 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags, 502 DiagnosticsEngine &Diags, 503 bool IsSystem, bool Complain) { 504 // Top-level options 505 if (IsSystem) { 506 if (Diags.getSuppressSystemWarnings()) 507 return false; 508 // If -Wsystem-headers was not enabled before, be conservative 509 if (StoredDiags.getSuppressSystemWarnings()) { 510 if (Complain) 511 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers"; 512 return true; 513 } 514 } 515 516 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) { 517 if (Complain) 518 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror"; 519 return true; 520 } 521 522 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() && 523 !StoredDiags.getEnableAllWarnings()) { 524 if (Complain) 525 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror"; 526 return true; 527 } 528 529 if (isExtHandlingFromDiagsError(Diags) && 530 !isExtHandlingFromDiagsError(StoredDiags)) { 531 if (Complain) 532 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors"; 533 return true; 534 } 535 536 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain); 537 } 538 539 /// Return the top import module if it is implicit, nullptr otherwise. 540 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr, 541 Preprocessor &PP) { 542 // If the original import came from a file explicitly generated by the user, 543 // don't check the diagnostic mappings. 544 // FIXME: currently this is approximated by checking whether this is not a 545 // module import of an implicitly-loaded module file. 546 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in 547 // the transitive closure of its imports, since unrelated modules cannot be 548 // imported until after this module finishes validation. 549 ModuleFile *TopImport = &*ModuleMgr.rbegin(); 550 while (!TopImport->ImportedBy.empty()) 551 TopImport = TopImport->ImportedBy[0]; 552 if (TopImport->Kind != MK_ImplicitModule) 553 return nullptr; 554 555 StringRef ModuleName = TopImport->ModuleName; 556 assert(!ModuleName.empty() && "diagnostic options read before module name"); 557 558 Module *M = 559 PP.getHeaderSearchInfo().lookupModule(ModuleName, TopImport->ImportLoc); 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 (llvm::is_contained(PPOpts.Includes, File)) 731 continue; 732 733 SuggestedPredefines += "#include \""; 734 SuggestedPredefines += File; 735 SuggestedPredefines += "\"\n"; 736 } 737 738 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) { 739 StringRef File = ExistingPPOpts.MacroIncludes[I]; 740 if (llvm::is_contained(PPOpts.MacroIncludes, File)) 741 continue; 742 743 SuggestedPredefines += "#__include_macros \""; 744 SuggestedPredefines += File; 745 SuggestedPredefines += "\"\n##\n"; 746 } 747 748 return false; 749 } 750 751 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 752 bool Complain, 753 std::string &SuggestedPredefines) { 754 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts(); 755 756 return checkPreprocessorOptions(PPOpts, ExistingPPOpts, 757 Complain? &Reader.Diags : nullptr, 758 PP.getFileManager(), 759 SuggestedPredefines, 760 PP.getLangOpts()); 761 } 762 763 bool SimpleASTReaderListener::ReadPreprocessorOptions( 764 const PreprocessorOptions &PPOpts, 765 bool Complain, 766 std::string &SuggestedPredefines) { 767 return checkPreprocessorOptions(PPOpts, 768 PP.getPreprocessorOpts(), 769 nullptr, 770 PP.getFileManager(), 771 SuggestedPredefines, 772 PP.getLangOpts(), 773 false); 774 } 775 776 /// Check the header search options deserialized from the control block 777 /// against the header search options in an existing preprocessor. 778 /// 779 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 780 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 781 StringRef SpecificModuleCachePath, 782 StringRef ExistingModuleCachePath, 783 DiagnosticsEngine *Diags, 784 const LangOptions &LangOpts, 785 const PreprocessorOptions &PPOpts) { 786 if (LangOpts.Modules) { 787 if (SpecificModuleCachePath != ExistingModuleCachePath && 788 !PPOpts.AllowPCHWithDifferentModulesCachePath) { 789 if (Diags) 790 Diags->Report(diag::err_pch_modulecache_mismatch) 791 << SpecificModuleCachePath << ExistingModuleCachePath; 792 return true; 793 } 794 } 795 796 return false; 797 } 798 799 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 800 StringRef SpecificModuleCachePath, 801 bool Complain) { 802 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 803 PP.getHeaderSearchInfo().getModuleCachePath(), 804 Complain ? &Reader.Diags : nullptr, 805 PP.getLangOpts(), PP.getPreprocessorOpts()); 806 } 807 808 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) { 809 PP.setCounterValue(Value); 810 } 811 812 //===----------------------------------------------------------------------===// 813 // AST reader implementation 814 //===----------------------------------------------------------------------===// 815 816 static uint64_t readULEB(const unsigned char *&P) { 817 unsigned Length = 0; 818 const char *Error = nullptr; 819 820 uint64_t Val = llvm::decodeULEB128(P, &Length, nullptr, &Error); 821 if (Error) 822 llvm::report_fatal_error(Error); 823 P += Length; 824 return Val; 825 } 826 827 /// Read ULEB-encoded key length and data length. 828 static std::pair<unsigned, unsigned> 829 readULEBKeyDataLength(const unsigned char *&P) { 830 unsigned KeyLen = readULEB(P); 831 if ((unsigned)KeyLen != KeyLen) 832 llvm::report_fatal_error("key too large"); 833 834 unsigned DataLen = readULEB(P); 835 if ((unsigned)DataLen != DataLen) 836 llvm::report_fatal_error("data too large"); 837 838 return std::make_pair(KeyLen, DataLen); 839 } 840 841 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener, 842 bool TakeOwnership) { 843 DeserializationListener = Listener; 844 OwnsDeserializationListener = TakeOwnership; 845 } 846 847 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) { 848 return serialization::ComputeHash(Sel); 849 } 850 851 std::pair<unsigned, unsigned> 852 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 853 return readULEBKeyDataLength(d); 854 } 855 856 ASTSelectorLookupTrait::internal_key_type 857 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) { 858 using namespace llvm::support; 859 860 SelectorTable &SelTable = Reader.getContext().Selectors; 861 unsigned N = endian::readNext<uint16_t, little, unaligned>(d); 862 IdentifierInfo *FirstII = Reader.getLocalIdentifier( 863 F, endian::readNext<uint32_t, little, unaligned>(d)); 864 if (N == 0) 865 return SelTable.getNullarySelector(FirstII); 866 else if (N == 1) 867 return SelTable.getUnarySelector(FirstII); 868 869 SmallVector<IdentifierInfo *, 16> Args; 870 Args.push_back(FirstII); 871 for (unsigned I = 1; I != N; ++I) 872 Args.push_back(Reader.getLocalIdentifier( 873 F, endian::readNext<uint32_t, little, unaligned>(d))); 874 875 return SelTable.getSelector(N, Args.data()); 876 } 877 878 ASTSelectorLookupTrait::data_type 879 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d, 880 unsigned DataLen) { 881 using namespace llvm::support; 882 883 data_type Result; 884 885 Result.ID = Reader.getGlobalSelectorID( 886 F, endian::readNext<uint32_t, little, unaligned>(d)); 887 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d); 888 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d); 889 Result.InstanceBits = FullInstanceBits & 0x3; 890 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1; 891 Result.FactoryBits = FullFactoryBits & 0x3; 892 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1; 893 unsigned NumInstanceMethods = FullInstanceBits >> 3; 894 unsigned NumFactoryMethods = FullFactoryBits >> 3; 895 896 // Load instance methods 897 for (unsigned I = 0; I != NumInstanceMethods; ++I) { 898 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 899 F, endian::readNext<uint32_t, little, unaligned>(d))) 900 Result.Instance.push_back(Method); 901 } 902 903 // Load factory methods 904 for (unsigned I = 0; I != NumFactoryMethods; ++I) { 905 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 906 F, endian::readNext<uint32_t, little, unaligned>(d))) 907 Result.Factory.push_back(Method); 908 } 909 910 return Result; 911 } 912 913 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) { 914 return llvm::djbHash(a); 915 } 916 917 std::pair<unsigned, unsigned> 918 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) { 919 return readULEBKeyDataLength(d); 920 } 921 922 ASTIdentifierLookupTraitBase::internal_key_type 923 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) { 924 assert(n >= 2 && d[n-1] == '\0'); 925 return StringRef((const char*) d, n-1); 926 } 927 928 /// Whether the given identifier is "interesting". 929 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II, 930 bool IsModule) { 931 return II.hadMacroDefinition() || II.isPoisoned() || 932 (!IsModule && II.getObjCOrBuiltinID()) || 933 II.hasRevertedTokenIDToIdentifier() || 934 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) && 935 II.getFETokenInfo()); 936 } 937 938 static bool readBit(unsigned &Bits) { 939 bool Value = Bits & 0x1; 940 Bits >>= 1; 941 return Value; 942 } 943 944 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) { 945 using namespace llvm::support; 946 947 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 948 return Reader.getGlobalIdentifierID(F, RawID >> 1); 949 } 950 951 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) { 952 if (!II.isFromAST()) { 953 II.setIsFromAST(); 954 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr; 955 if (isInterestingIdentifier(Reader, II, IsModule)) 956 II.setChangedSinceDeserialization(); 957 } 958 } 959 960 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k, 961 const unsigned char* d, 962 unsigned DataLen) { 963 using namespace llvm::support; 964 965 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 966 bool IsInteresting = RawID & 0x01; 967 968 // Wipe out the "is interesting" bit. 969 RawID = RawID >> 1; 970 971 // Build the IdentifierInfo and link the identifier ID with it. 972 IdentifierInfo *II = KnownII; 973 if (!II) { 974 II = &Reader.getIdentifierTable().getOwn(k); 975 KnownII = II; 976 } 977 markIdentifierFromAST(Reader, *II); 978 Reader.markIdentifierUpToDate(II); 979 980 IdentID ID = Reader.getGlobalIdentifierID(F, RawID); 981 if (!IsInteresting) { 982 // For uninteresting identifiers, there's nothing else to do. Just notify 983 // the reader that we've finished loading this identifier. 984 Reader.SetIdentifierInfo(ID, II); 985 return II; 986 } 987 988 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d); 989 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d); 990 bool CPlusPlusOperatorKeyword = readBit(Bits); 991 bool HasRevertedTokenIDToIdentifier = readBit(Bits); 992 bool Poisoned = readBit(Bits); 993 bool ExtensionToken = readBit(Bits); 994 bool HadMacroDefinition = readBit(Bits); 995 996 assert(Bits == 0 && "Extra bits in the identifier?"); 997 DataLen -= 8; 998 999 // Set or check the various bits in the IdentifierInfo structure. 1000 // Token IDs are read-only. 1001 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier) 1002 II->revertTokenIDToIdentifier(); 1003 if (!F.isModule()) 1004 II->setObjCOrBuiltinID(ObjCOrBuiltinID); 1005 assert(II->isExtensionToken() == ExtensionToken && 1006 "Incorrect extension token flag"); 1007 (void)ExtensionToken; 1008 if (Poisoned) 1009 II->setIsPoisoned(true); 1010 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword && 1011 "Incorrect C++ operator keyword flag"); 1012 (void)CPlusPlusOperatorKeyword; 1013 1014 // If this identifier is a macro, deserialize the macro 1015 // definition. 1016 if (HadMacroDefinition) { 1017 uint32_t MacroDirectivesOffset = 1018 endian::readNext<uint32_t, little, unaligned>(d); 1019 DataLen -= 4; 1020 1021 Reader.addPendingMacro(II, &F, MacroDirectivesOffset); 1022 } 1023 1024 Reader.SetIdentifierInfo(ID, II); 1025 1026 // Read all of the declarations visible at global scope with this 1027 // name. 1028 if (DataLen > 0) { 1029 SmallVector<uint32_t, 4> DeclIDs; 1030 for (; DataLen > 0; DataLen -= 4) 1031 DeclIDs.push_back(Reader.getGlobalDeclID( 1032 F, endian::readNext<uint32_t, little, unaligned>(d))); 1033 Reader.SetGloballyVisibleDecls(II, DeclIDs); 1034 } 1035 1036 return II; 1037 } 1038 1039 DeclarationNameKey::DeclarationNameKey(DeclarationName Name) 1040 : Kind(Name.getNameKind()) { 1041 switch (Kind) { 1042 case DeclarationName::Identifier: 1043 Data = (uint64_t)Name.getAsIdentifierInfo(); 1044 break; 1045 case DeclarationName::ObjCZeroArgSelector: 1046 case DeclarationName::ObjCOneArgSelector: 1047 case DeclarationName::ObjCMultiArgSelector: 1048 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr(); 1049 break; 1050 case DeclarationName::CXXOperatorName: 1051 Data = Name.getCXXOverloadedOperator(); 1052 break; 1053 case DeclarationName::CXXLiteralOperatorName: 1054 Data = (uint64_t)Name.getCXXLiteralIdentifier(); 1055 break; 1056 case DeclarationName::CXXDeductionGuideName: 1057 Data = (uint64_t)Name.getCXXDeductionGuideTemplate() 1058 ->getDeclName().getAsIdentifierInfo(); 1059 break; 1060 case DeclarationName::CXXConstructorName: 1061 case DeclarationName::CXXDestructorName: 1062 case DeclarationName::CXXConversionFunctionName: 1063 case DeclarationName::CXXUsingDirective: 1064 Data = 0; 1065 break; 1066 } 1067 } 1068 1069 unsigned DeclarationNameKey::getHash() const { 1070 llvm::FoldingSetNodeID ID; 1071 ID.AddInteger(Kind); 1072 1073 switch (Kind) { 1074 case DeclarationName::Identifier: 1075 case DeclarationName::CXXLiteralOperatorName: 1076 case DeclarationName::CXXDeductionGuideName: 1077 ID.AddString(((IdentifierInfo*)Data)->getName()); 1078 break; 1079 case DeclarationName::ObjCZeroArgSelector: 1080 case DeclarationName::ObjCOneArgSelector: 1081 case DeclarationName::ObjCMultiArgSelector: 1082 ID.AddInteger(serialization::ComputeHash(Selector(Data))); 1083 break; 1084 case DeclarationName::CXXOperatorName: 1085 ID.AddInteger((OverloadedOperatorKind)Data); 1086 break; 1087 case DeclarationName::CXXConstructorName: 1088 case DeclarationName::CXXDestructorName: 1089 case DeclarationName::CXXConversionFunctionName: 1090 case DeclarationName::CXXUsingDirective: 1091 break; 1092 } 1093 1094 return ID.ComputeHash(); 1095 } 1096 1097 ModuleFile * 1098 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) { 1099 using namespace llvm::support; 1100 1101 uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d); 1102 return Reader.getLocalModuleFile(F, ModuleFileID); 1103 } 1104 1105 std::pair<unsigned, unsigned> 1106 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) { 1107 return readULEBKeyDataLength(d); 1108 } 1109 1110 ASTDeclContextNameLookupTrait::internal_key_type 1111 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) { 1112 using namespace llvm::support; 1113 1114 auto Kind = (DeclarationName::NameKind)*d++; 1115 uint64_t Data; 1116 switch (Kind) { 1117 case DeclarationName::Identifier: 1118 case DeclarationName::CXXLiteralOperatorName: 1119 case DeclarationName::CXXDeductionGuideName: 1120 Data = (uint64_t)Reader.getLocalIdentifier( 1121 F, endian::readNext<uint32_t, little, unaligned>(d)); 1122 break; 1123 case DeclarationName::ObjCZeroArgSelector: 1124 case DeclarationName::ObjCOneArgSelector: 1125 case DeclarationName::ObjCMultiArgSelector: 1126 Data = 1127 (uint64_t)Reader.getLocalSelector( 1128 F, endian::readNext<uint32_t, little, unaligned>( 1129 d)).getAsOpaquePtr(); 1130 break; 1131 case DeclarationName::CXXOperatorName: 1132 Data = *d++; // OverloadedOperatorKind 1133 break; 1134 case DeclarationName::CXXConstructorName: 1135 case DeclarationName::CXXDestructorName: 1136 case DeclarationName::CXXConversionFunctionName: 1137 case DeclarationName::CXXUsingDirective: 1138 Data = 0; 1139 break; 1140 } 1141 1142 return DeclarationNameKey(Kind, Data); 1143 } 1144 1145 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type, 1146 const unsigned char *d, 1147 unsigned DataLen, 1148 data_type_builder &Val) { 1149 using namespace llvm::support; 1150 1151 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) { 1152 uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d); 1153 Val.insert(Reader.getGlobalDeclID(F, LocalID)); 1154 } 1155 } 1156 1157 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M, 1158 BitstreamCursor &Cursor, 1159 uint64_t Offset, 1160 DeclContext *DC) { 1161 assert(Offset != 0); 1162 1163 SavedStreamPosition SavedPosition(Cursor); 1164 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1165 Error(std::move(Err)); 1166 return true; 1167 } 1168 1169 RecordData Record; 1170 StringRef Blob; 1171 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1172 if (!MaybeCode) { 1173 Error(MaybeCode.takeError()); 1174 return true; 1175 } 1176 unsigned Code = MaybeCode.get(); 1177 1178 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1179 if (!MaybeRecCode) { 1180 Error(MaybeRecCode.takeError()); 1181 return true; 1182 } 1183 unsigned RecCode = MaybeRecCode.get(); 1184 if (RecCode != DECL_CONTEXT_LEXICAL) { 1185 Error("Expected lexical block"); 1186 return true; 1187 } 1188 1189 assert(!isa<TranslationUnitDecl>(DC) && 1190 "expected a TU_UPDATE_LEXICAL record for TU"); 1191 // If we are handling a C++ class template instantiation, we can see multiple 1192 // lexical updates for the same record. It's important that we select only one 1193 // of them, so that field numbering works properly. Just pick the first one we 1194 // see. 1195 auto &Lex = LexicalDecls[DC]; 1196 if (!Lex.first) { 1197 Lex = std::make_pair( 1198 &M, llvm::makeArrayRef( 1199 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 1200 Blob.data()), 1201 Blob.size() / 4)); 1202 } 1203 DC->setHasExternalLexicalStorage(true); 1204 return false; 1205 } 1206 1207 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M, 1208 BitstreamCursor &Cursor, 1209 uint64_t Offset, 1210 DeclID ID) { 1211 assert(Offset != 0); 1212 1213 SavedStreamPosition SavedPosition(Cursor); 1214 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1215 Error(std::move(Err)); 1216 return true; 1217 } 1218 1219 RecordData Record; 1220 StringRef Blob; 1221 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1222 if (!MaybeCode) { 1223 Error(MaybeCode.takeError()); 1224 return true; 1225 } 1226 unsigned Code = MaybeCode.get(); 1227 1228 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1229 if (!MaybeRecCode) { 1230 Error(MaybeRecCode.takeError()); 1231 return true; 1232 } 1233 unsigned RecCode = MaybeRecCode.get(); 1234 if (RecCode != DECL_CONTEXT_VISIBLE) { 1235 Error("Expected visible lookup table block"); 1236 return true; 1237 } 1238 1239 // We can't safely determine the primary context yet, so delay attaching the 1240 // lookup table until we're done with recursive deserialization. 1241 auto *Data = (const unsigned char*)Blob.data(); 1242 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data}); 1243 return false; 1244 } 1245 1246 void ASTReader::Error(StringRef Msg) const { 1247 Error(diag::err_fe_pch_malformed, Msg); 1248 if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() && 1249 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) { 1250 Diag(diag::note_module_cache_path) 1251 << PP.getHeaderSearchInfo().getModuleCachePath(); 1252 } 1253 } 1254 1255 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2, 1256 StringRef Arg3) const { 1257 if (Diags.isDiagnosticInFlight()) 1258 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3); 1259 else 1260 Diag(DiagID) << Arg1 << Arg2 << Arg3; 1261 } 1262 1263 void ASTReader::Error(llvm::Error &&Err) const { 1264 llvm::Error RemainingErr = 1265 handleErrors(std::move(Err), [this](const DiagnosticError &E) { 1266 auto Diag = E.getDiagnostic().second; 1267 1268 // Ideally we'd just emit it, but have to handle a possible in-flight 1269 // diagnostic. Note that the location is currently ignored as well. 1270 auto NumArgs = Diag.getStorage()->NumDiagArgs; 1271 assert(NumArgs <= 3 && "Can only have up to 3 arguments"); 1272 StringRef Arg1, Arg2, Arg3; 1273 switch (NumArgs) { 1274 case 3: 1275 Arg3 = Diag.getStringArg(2); 1276 LLVM_FALLTHROUGH; 1277 case 2: 1278 Arg2 = Diag.getStringArg(1); 1279 LLVM_FALLTHROUGH; 1280 case 1: 1281 Arg1 = Diag.getStringArg(0); 1282 } 1283 Error(Diag.getDiagID(), Arg1, Arg2, Arg3); 1284 }); 1285 if (RemainingErr) 1286 Error(toString(std::move(RemainingErr))); 1287 } 1288 1289 //===----------------------------------------------------------------------===// 1290 // Source Manager Deserialization 1291 //===----------------------------------------------------------------------===// 1292 1293 /// Read the line table in the source manager block. 1294 void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) { 1295 unsigned Idx = 0; 1296 LineTableInfo &LineTable = SourceMgr.getLineTable(); 1297 1298 // Parse the file names 1299 std::map<int, int> FileIDs; 1300 FileIDs[-1] = -1; // For unspecified filenames. 1301 for (unsigned I = 0; Record[Idx]; ++I) { 1302 // Extract the file name 1303 auto Filename = ReadPath(F, Record, Idx); 1304 FileIDs[I] = LineTable.getLineTableFilenameID(Filename); 1305 } 1306 ++Idx; 1307 1308 // Parse the line entries 1309 std::vector<LineEntry> Entries; 1310 while (Idx < Record.size()) { 1311 int FID = Record[Idx++]; 1312 assert(FID >= 0 && "Serialized line entries for non-local file."); 1313 // Remap FileID from 1-based old view. 1314 FID += F.SLocEntryBaseID - 1; 1315 1316 // Extract the line entries 1317 unsigned NumEntries = Record[Idx++]; 1318 assert(NumEntries && "no line entries for file ID"); 1319 Entries.clear(); 1320 Entries.reserve(NumEntries); 1321 for (unsigned I = 0; I != NumEntries; ++I) { 1322 unsigned FileOffset = Record[Idx++]; 1323 unsigned LineNo = Record[Idx++]; 1324 int FilenameID = FileIDs[Record[Idx++]]; 1325 SrcMgr::CharacteristicKind FileKind 1326 = (SrcMgr::CharacteristicKind)Record[Idx++]; 1327 unsigned IncludeOffset = Record[Idx++]; 1328 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, 1329 FileKind, IncludeOffset)); 1330 } 1331 LineTable.AddEntry(FileID::get(FID), Entries); 1332 } 1333 } 1334 1335 /// Read a source manager block 1336 llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) { 1337 using namespace SrcMgr; 1338 1339 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor; 1340 1341 // Set the source-location entry cursor to the current position in 1342 // the stream. This cursor will be used to read the contents of the 1343 // source manager block initially, and then lazily read 1344 // source-location entries as needed. 1345 SLocEntryCursor = F.Stream; 1346 1347 // The stream itself is going to skip over the source manager block. 1348 if (llvm::Error Err = F.Stream.SkipBlock()) 1349 return Err; 1350 1351 // Enter the source manager block. 1352 if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) 1353 return Err; 1354 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo(); 1355 1356 RecordData Record; 1357 while (true) { 1358 Expected<llvm::BitstreamEntry> MaybeE = 1359 SLocEntryCursor.advanceSkippingSubblocks(); 1360 if (!MaybeE) 1361 return MaybeE.takeError(); 1362 llvm::BitstreamEntry E = MaybeE.get(); 1363 1364 switch (E.Kind) { 1365 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1366 case llvm::BitstreamEntry::Error: 1367 return llvm::createStringError(std::errc::illegal_byte_sequence, 1368 "malformed block record in AST file"); 1369 case llvm::BitstreamEntry::EndBlock: 1370 return llvm::Error::success(); 1371 case llvm::BitstreamEntry::Record: 1372 // The interesting case. 1373 break; 1374 } 1375 1376 // Read a record. 1377 Record.clear(); 1378 StringRef Blob; 1379 Expected<unsigned> MaybeRecord = 1380 SLocEntryCursor.readRecord(E.ID, Record, &Blob); 1381 if (!MaybeRecord) 1382 return MaybeRecord.takeError(); 1383 switch (MaybeRecord.get()) { 1384 default: // Default behavior: ignore. 1385 break; 1386 1387 case SM_SLOC_FILE_ENTRY: 1388 case SM_SLOC_BUFFER_ENTRY: 1389 case SM_SLOC_EXPANSION_ENTRY: 1390 // Once we hit one of the source location entries, we're done. 1391 return llvm::Error::success(); 1392 } 1393 } 1394 } 1395 1396 /// If a header file is not found at the path that we expect it to be 1397 /// and the PCH file was moved from its original location, try to resolve the 1398 /// file by assuming that header+PCH were moved together and the header is in 1399 /// the same place relative to the PCH. 1400 static std::string 1401 resolveFileRelativeToOriginalDir(const std::string &Filename, 1402 const std::string &OriginalDir, 1403 const std::string &CurrDir) { 1404 assert(OriginalDir != CurrDir && 1405 "No point trying to resolve the file if the PCH dir didn't change"); 1406 1407 using namespace llvm::sys; 1408 1409 SmallString<128> filePath(Filename); 1410 fs::make_absolute(filePath); 1411 assert(path::is_absolute(OriginalDir)); 1412 SmallString<128> currPCHPath(CurrDir); 1413 1414 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)), 1415 fileDirE = path::end(path::parent_path(filePath)); 1416 path::const_iterator origDirI = path::begin(OriginalDir), 1417 origDirE = path::end(OriginalDir); 1418 // Skip the common path components from filePath and OriginalDir. 1419 while (fileDirI != fileDirE && origDirI != origDirE && 1420 *fileDirI == *origDirI) { 1421 ++fileDirI; 1422 ++origDirI; 1423 } 1424 for (; origDirI != origDirE; ++origDirI) 1425 path::append(currPCHPath, ".."); 1426 path::append(currPCHPath, fileDirI, fileDirE); 1427 path::append(currPCHPath, path::filename(Filename)); 1428 return std::string(currPCHPath.str()); 1429 } 1430 1431 bool ASTReader::ReadSLocEntry(int ID) { 1432 if (ID == 0) 1433 return false; 1434 1435 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1436 Error("source location entry ID out-of-range for AST file"); 1437 return true; 1438 } 1439 1440 // Local helper to read the (possibly-compressed) buffer data following the 1441 // entry record. 1442 auto ReadBuffer = [this]( 1443 BitstreamCursor &SLocEntryCursor, 1444 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> { 1445 RecordData Record; 1446 StringRef Blob; 1447 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode(); 1448 if (!MaybeCode) { 1449 Error(MaybeCode.takeError()); 1450 return nullptr; 1451 } 1452 unsigned Code = MaybeCode.get(); 1453 1454 Expected<unsigned> MaybeRecCode = 1455 SLocEntryCursor.readRecord(Code, Record, &Blob); 1456 if (!MaybeRecCode) { 1457 Error(MaybeRecCode.takeError()); 1458 return nullptr; 1459 } 1460 unsigned RecCode = MaybeRecCode.get(); 1461 1462 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) { 1463 if (!llvm::zlib::isAvailable()) { 1464 Error("zlib is not available"); 1465 return nullptr; 1466 } 1467 SmallString<0> Uncompressed; 1468 if (llvm::Error E = 1469 llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) { 1470 Error("could not decompress embedded file contents: " + 1471 llvm::toString(std::move(E))); 1472 return nullptr; 1473 } 1474 return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name); 1475 } else if (RecCode == SM_SLOC_BUFFER_BLOB) { 1476 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true); 1477 } else { 1478 Error("AST record has invalid code"); 1479 return nullptr; 1480 } 1481 }; 1482 1483 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second; 1484 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit( 1485 F->SLocEntryOffsetsBase + 1486 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) { 1487 Error(std::move(Err)); 1488 return true; 1489 } 1490 1491 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor; 1492 SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset; 1493 1494 ++NumSLocEntriesRead; 1495 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance(); 1496 if (!MaybeEntry) { 1497 Error(MaybeEntry.takeError()); 1498 return true; 1499 } 1500 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1501 1502 if (Entry.Kind != llvm::BitstreamEntry::Record) { 1503 Error("incorrectly-formatted source location entry in AST file"); 1504 return true; 1505 } 1506 1507 RecordData Record; 1508 StringRef Blob; 1509 Expected<unsigned> MaybeSLOC = 1510 SLocEntryCursor.readRecord(Entry.ID, Record, &Blob); 1511 if (!MaybeSLOC) { 1512 Error(MaybeSLOC.takeError()); 1513 return true; 1514 } 1515 switch (MaybeSLOC.get()) { 1516 default: 1517 Error("incorrectly-formatted source location entry in AST file"); 1518 return true; 1519 1520 case SM_SLOC_FILE_ENTRY: { 1521 // We will detect whether a file changed and return 'Failure' for it, but 1522 // we will also try to fail gracefully by setting up the SLocEntry. 1523 unsigned InputID = Record[4]; 1524 InputFile IF = getInputFile(*F, InputID); 1525 Optional<FileEntryRef> File = IF.getFile(); 1526 bool OverriddenBuffer = IF.isOverridden(); 1527 1528 // Note that we only check if a File was returned. If it was out-of-date 1529 // we have complained but we will continue creating a FileID to recover 1530 // gracefully. 1531 if (!File) 1532 return true; 1533 1534 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1535 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) { 1536 // This is the module's main file. 1537 IncludeLoc = getImportLocation(F); 1538 } 1539 SrcMgr::CharacteristicKind 1540 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1541 FileID FID = SourceMgr.createFileID(*File, IncludeLoc, FileCharacter, ID, 1542 BaseOffset + Record[0]); 1543 SrcMgr::FileInfo &FileInfo = 1544 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()); 1545 FileInfo.NumCreatedFIDs = Record[5]; 1546 if (Record[3]) 1547 FileInfo.setHasLineDirectives(); 1548 1549 unsigned NumFileDecls = Record[7]; 1550 if (NumFileDecls && ContextObj) { 1551 const DeclID *FirstDecl = F->FileSortedDecls + Record[6]; 1552 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?"); 1553 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl, 1554 NumFileDecls)); 1555 } 1556 1557 const SrcMgr::ContentCache &ContentCache = 1558 SourceMgr.getOrCreateContentCache(*File, isSystem(FileCharacter)); 1559 if (OverriddenBuffer && !ContentCache.BufferOverridden && 1560 ContentCache.ContentsEntry == ContentCache.OrigEntry && 1561 !ContentCache.getBufferIfLoaded()) { 1562 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName()); 1563 if (!Buffer) 1564 return true; 1565 SourceMgr.overrideFileContents(*File, std::move(Buffer)); 1566 } 1567 1568 break; 1569 } 1570 1571 case SM_SLOC_BUFFER_ENTRY: { 1572 const char *Name = Blob.data(); 1573 unsigned Offset = Record[0]; 1574 SrcMgr::CharacteristicKind 1575 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1576 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1577 if (IncludeLoc.isInvalid() && F->isModule()) { 1578 IncludeLoc = getImportLocation(F); 1579 } 1580 1581 auto Buffer = ReadBuffer(SLocEntryCursor, Name); 1582 if (!Buffer) 1583 return true; 1584 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID, 1585 BaseOffset + Offset, IncludeLoc); 1586 break; 1587 } 1588 1589 case SM_SLOC_EXPANSION_ENTRY: { 1590 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]); 1591 SourceMgr.createExpansionLoc(SpellingLoc, 1592 ReadSourceLocation(*F, Record[2]), 1593 ReadSourceLocation(*F, Record[3]), 1594 Record[5], 1595 Record[4], 1596 ID, 1597 BaseOffset + Record[0]); 1598 break; 1599 } 1600 } 1601 1602 return false; 1603 } 1604 1605 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) { 1606 if (ID == 0) 1607 return std::make_pair(SourceLocation(), ""); 1608 1609 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1610 Error("source location entry ID out-of-range for AST file"); 1611 return std::make_pair(SourceLocation(), ""); 1612 } 1613 1614 // Find which module file this entry lands in. 1615 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second; 1616 if (!M->isModule()) 1617 return std::make_pair(SourceLocation(), ""); 1618 1619 // FIXME: Can we map this down to a particular submodule? That would be 1620 // ideal. 1621 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName)); 1622 } 1623 1624 /// Find the location where the module F is imported. 1625 SourceLocation ASTReader::getImportLocation(ModuleFile *F) { 1626 if (F->ImportLoc.isValid()) 1627 return F->ImportLoc; 1628 1629 // Otherwise we have a PCH. It's considered to be "imported" at the first 1630 // location of its includer. 1631 if (F->ImportedBy.empty() || !F->ImportedBy[0]) { 1632 // Main file is the importer. 1633 assert(SourceMgr.getMainFileID().isValid() && "missing main file"); 1634 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); 1635 } 1636 return F->ImportedBy[0]->FirstLoc; 1637 } 1638 1639 /// Enter a subblock of the specified BlockID with the specified cursor. Read 1640 /// the abbreviations that are at the top of the block and then leave the cursor 1641 /// pointing into the block. 1642 llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, 1643 unsigned BlockID, 1644 uint64_t *StartOfBlockOffset) { 1645 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) 1646 return Err; 1647 1648 if (StartOfBlockOffset) 1649 *StartOfBlockOffset = Cursor.GetCurrentBitNo(); 1650 1651 while (true) { 1652 uint64_t Offset = Cursor.GetCurrentBitNo(); 1653 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1654 if (!MaybeCode) 1655 return MaybeCode.takeError(); 1656 unsigned Code = MaybeCode.get(); 1657 1658 // We expect all abbrevs to be at the start of the block. 1659 if (Code != llvm::bitc::DEFINE_ABBREV) { 1660 if (llvm::Error Err = Cursor.JumpToBit(Offset)) 1661 return Err; 1662 return llvm::Error::success(); 1663 } 1664 if (llvm::Error Err = Cursor.ReadAbbrevRecord()) 1665 return Err; 1666 } 1667 } 1668 1669 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record, 1670 unsigned &Idx) { 1671 Token Tok; 1672 Tok.startToken(); 1673 Tok.setLocation(ReadSourceLocation(F, Record, Idx)); 1674 Tok.setLength(Record[Idx++]); 1675 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++])) 1676 Tok.setIdentifierInfo(II); 1677 Tok.setKind((tok::TokenKind)Record[Idx++]); 1678 Tok.setFlag((Token::TokenFlags)Record[Idx++]); 1679 return Tok; 1680 } 1681 1682 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) { 1683 BitstreamCursor &Stream = F.MacroCursor; 1684 1685 // Keep track of where we are in the stream, then jump back there 1686 // after reading this macro. 1687 SavedStreamPosition SavedPosition(Stream); 1688 1689 if (llvm::Error Err = Stream.JumpToBit(Offset)) { 1690 // FIXME this drops errors on the floor. 1691 consumeError(std::move(Err)); 1692 return nullptr; 1693 } 1694 RecordData Record; 1695 SmallVector<IdentifierInfo*, 16> MacroParams; 1696 MacroInfo *Macro = nullptr; 1697 llvm::MutableArrayRef<Token> MacroTokens; 1698 1699 while (true) { 1700 // Advance to the next record, but if we get to the end of the block, don't 1701 // pop it (removing all the abbreviations from the cursor) since we want to 1702 // be able to reseek within the block and read entries. 1703 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd; 1704 Expected<llvm::BitstreamEntry> MaybeEntry = 1705 Stream.advanceSkippingSubblocks(Flags); 1706 if (!MaybeEntry) { 1707 Error(MaybeEntry.takeError()); 1708 return Macro; 1709 } 1710 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1711 1712 switch (Entry.Kind) { 1713 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1714 case llvm::BitstreamEntry::Error: 1715 Error("malformed block record in AST file"); 1716 return Macro; 1717 case llvm::BitstreamEntry::EndBlock: 1718 return Macro; 1719 case llvm::BitstreamEntry::Record: 1720 // The interesting case. 1721 break; 1722 } 1723 1724 // Read a record. 1725 Record.clear(); 1726 PreprocessorRecordTypes RecType; 1727 if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record)) 1728 RecType = (PreprocessorRecordTypes)MaybeRecType.get(); 1729 else { 1730 Error(MaybeRecType.takeError()); 1731 return Macro; 1732 } 1733 switch (RecType) { 1734 case PP_MODULE_MACRO: 1735 case PP_MACRO_DIRECTIVE_HISTORY: 1736 return Macro; 1737 1738 case PP_MACRO_OBJECT_LIKE: 1739 case PP_MACRO_FUNCTION_LIKE: { 1740 // If we already have a macro, that means that we've hit the end 1741 // of the definition of the macro we were looking for. We're 1742 // done. 1743 if (Macro) 1744 return Macro; 1745 1746 unsigned NextIndex = 1; // Skip identifier ID. 1747 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex); 1748 MacroInfo *MI = PP.AllocateMacroInfo(Loc); 1749 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex)); 1750 MI->setIsUsed(Record[NextIndex++]); 1751 MI->setUsedForHeaderGuard(Record[NextIndex++]); 1752 MacroTokens = MI->allocateTokens(Record[NextIndex++], 1753 PP.getPreprocessorAllocator()); 1754 if (RecType == PP_MACRO_FUNCTION_LIKE) { 1755 // Decode function-like macro info. 1756 bool isC99VarArgs = Record[NextIndex++]; 1757 bool isGNUVarArgs = Record[NextIndex++]; 1758 bool hasCommaPasting = Record[NextIndex++]; 1759 MacroParams.clear(); 1760 unsigned NumArgs = Record[NextIndex++]; 1761 for (unsigned i = 0; i != NumArgs; ++i) 1762 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++])); 1763 1764 // Install function-like macro info. 1765 MI->setIsFunctionLike(); 1766 if (isC99VarArgs) MI->setIsC99Varargs(); 1767 if (isGNUVarArgs) MI->setIsGNUVarargs(); 1768 if (hasCommaPasting) MI->setHasCommaPasting(); 1769 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator()); 1770 } 1771 1772 // Remember that we saw this macro last so that we add the tokens that 1773 // form its body to it. 1774 Macro = MI; 1775 1776 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() && 1777 Record[NextIndex]) { 1778 // We have a macro definition. Register the association 1779 PreprocessedEntityID 1780 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]); 1781 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 1782 PreprocessingRecord::PPEntityID PPID = 1783 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true); 1784 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>( 1785 PPRec.getPreprocessedEntity(PPID)); 1786 if (PPDef) 1787 PPRec.RegisterMacroDefinition(Macro, PPDef); 1788 } 1789 1790 ++NumMacrosRead; 1791 break; 1792 } 1793 1794 case PP_TOKEN: { 1795 // If we see a TOKEN before a PP_MACRO_*, then the file is 1796 // erroneous, just pretend we didn't see this. 1797 if (!Macro) break; 1798 if (MacroTokens.empty()) { 1799 Error("unexpected number of macro tokens for a macro in AST file"); 1800 return Macro; 1801 } 1802 1803 unsigned Idx = 0; 1804 MacroTokens[0] = ReadToken(F, Record, Idx); 1805 MacroTokens = MacroTokens.drop_front(); 1806 break; 1807 } 1808 } 1809 } 1810 } 1811 1812 PreprocessedEntityID 1813 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, 1814 unsigned LocalID) const { 1815 if (!M.ModuleOffsetMap.empty()) 1816 ReadModuleOffsetMap(M); 1817 1818 ContinuousRangeMap<uint32_t, int, 2>::const_iterator 1819 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS); 1820 assert(I != M.PreprocessedEntityRemap.end() 1821 && "Invalid index into preprocessed entity index remap"); 1822 1823 return LocalID + I->second; 1824 } 1825 1826 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) { 1827 return llvm::hash_combine(ikey.Size, ikey.ModTime); 1828 } 1829 1830 HeaderFileInfoTrait::internal_key_type 1831 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) { 1832 internal_key_type ikey = {FE->getSize(), 1833 M.HasTimestamps ? FE->getModificationTime() : 0, 1834 FE->getName(), /*Imported*/ false}; 1835 return ikey; 1836 } 1837 1838 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) { 1839 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime)) 1840 return false; 1841 1842 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename) 1843 return true; 1844 1845 // Determine whether the actual files are equivalent. 1846 FileManager &FileMgr = Reader.getFileManager(); 1847 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* { 1848 if (!Key.Imported) { 1849 if (auto File = FileMgr.getFile(Key.Filename)) 1850 return *File; 1851 return nullptr; 1852 } 1853 1854 std::string Resolved = std::string(Key.Filename); 1855 Reader.ResolveImportedPath(M, Resolved); 1856 if (auto File = FileMgr.getFile(Resolved)) 1857 return *File; 1858 return nullptr; 1859 }; 1860 1861 const FileEntry *FEA = GetFile(a); 1862 const FileEntry *FEB = GetFile(b); 1863 return FEA && FEA == FEB; 1864 } 1865 1866 std::pair<unsigned, unsigned> 1867 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) { 1868 return readULEBKeyDataLength(d); 1869 } 1870 1871 HeaderFileInfoTrait::internal_key_type 1872 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) { 1873 using namespace llvm::support; 1874 1875 internal_key_type ikey; 1876 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d)); 1877 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d)); 1878 ikey.Filename = (const char *)d; 1879 ikey.Imported = true; 1880 return ikey; 1881 } 1882 1883 HeaderFileInfoTrait::data_type 1884 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d, 1885 unsigned DataLen) { 1886 using namespace llvm::support; 1887 1888 const unsigned char *End = d + DataLen; 1889 HeaderFileInfo HFI; 1890 unsigned Flags = *d++; 1891 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp. 1892 HFI.isImport |= (Flags >> 5) & 0x01; 1893 HFI.isPragmaOnce |= (Flags >> 4) & 0x01; 1894 HFI.DirInfo = (Flags >> 1) & 0x07; 1895 HFI.IndexHeaderMapHeader = Flags & 0x01; 1896 HFI.ControllingMacroID = Reader.getGlobalIdentifierID( 1897 M, endian::readNext<uint32_t, little, unaligned>(d)); 1898 if (unsigned FrameworkOffset = 1899 endian::readNext<uint32_t, little, unaligned>(d)) { 1900 // The framework offset is 1 greater than the actual offset, 1901 // since 0 is used as an indicator for "no framework name". 1902 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1); 1903 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName); 1904 } 1905 1906 assert((End - d) % 4 == 0 && 1907 "Wrong data length in HeaderFileInfo deserialization"); 1908 while (d != End) { 1909 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d); 1910 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3); 1911 LocalSMID >>= 2; 1912 1913 // This header is part of a module. Associate it with the module to enable 1914 // implicit module import. 1915 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID); 1916 Module *Mod = Reader.getSubmodule(GlobalSMID); 1917 FileManager &FileMgr = Reader.getFileManager(); 1918 ModuleMap &ModMap = 1919 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap(); 1920 1921 std::string Filename = std::string(key.Filename); 1922 if (key.Imported) 1923 Reader.ResolveImportedPath(M, Filename); 1924 // FIXME: NameAsWritten 1925 Module::Header H = {std::string(key.Filename), "", 1926 *FileMgr.getFile(Filename)}; 1927 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true); 1928 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader); 1929 } 1930 1931 // This HeaderFileInfo was externally loaded. 1932 HFI.External = true; 1933 HFI.IsValid = true; 1934 return HFI; 1935 } 1936 1937 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M, 1938 uint32_t MacroDirectivesOffset) { 1939 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard"); 1940 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset)); 1941 } 1942 1943 void ASTReader::ReadDefinedMacros() { 1944 // Note that we are loading defined macros. 1945 Deserializing Macros(this); 1946 1947 for (ModuleFile &I : llvm::reverse(ModuleMgr)) { 1948 BitstreamCursor &MacroCursor = I.MacroCursor; 1949 1950 // If there was no preprocessor block, skip this file. 1951 if (MacroCursor.getBitcodeBytes().empty()) 1952 continue; 1953 1954 BitstreamCursor Cursor = MacroCursor; 1955 if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) { 1956 Error(std::move(Err)); 1957 return; 1958 } 1959 1960 RecordData Record; 1961 while (true) { 1962 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks(); 1963 if (!MaybeE) { 1964 Error(MaybeE.takeError()); 1965 return; 1966 } 1967 llvm::BitstreamEntry E = MaybeE.get(); 1968 1969 switch (E.Kind) { 1970 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1971 case llvm::BitstreamEntry::Error: 1972 Error("malformed block record in AST file"); 1973 return; 1974 case llvm::BitstreamEntry::EndBlock: 1975 goto NextCursor; 1976 1977 case llvm::BitstreamEntry::Record: { 1978 Record.clear(); 1979 Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record); 1980 if (!MaybeRecord) { 1981 Error(MaybeRecord.takeError()); 1982 return; 1983 } 1984 switch (MaybeRecord.get()) { 1985 default: // Default behavior: ignore. 1986 break; 1987 1988 case PP_MACRO_OBJECT_LIKE: 1989 case PP_MACRO_FUNCTION_LIKE: { 1990 IdentifierInfo *II = getLocalIdentifier(I, Record[0]); 1991 if (II->isOutOfDate()) 1992 updateOutOfDateIdentifier(*II); 1993 break; 1994 } 1995 1996 case PP_TOKEN: 1997 // Ignore tokens. 1998 break; 1999 } 2000 break; 2001 } 2002 } 2003 } 2004 NextCursor: ; 2005 } 2006 } 2007 2008 namespace { 2009 2010 /// Visitor class used to look up identifirs in an AST file. 2011 class IdentifierLookupVisitor { 2012 StringRef Name; 2013 unsigned NameHash; 2014 unsigned PriorGeneration; 2015 unsigned &NumIdentifierLookups; 2016 unsigned &NumIdentifierLookupHits; 2017 IdentifierInfo *Found = nullptr; 2018 2019 public: 2020 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration, 2021 unsigned &NumIdentifierLookups, 2022 unsigned &NumIdentifierLookupHits) 2023 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)), 2024 PriorGeneration(PriorGeneration), 2025 NumIdentifierLookups(NumIdentifierLookups), 2026 NumIdentifierLookupHits(NumIdentifierLookupHits) {} 2027 2028 bool operator()(ModuleFile &M) { 2029 // If we've already searched this module file, skip it now. 2030 if (M.Generation <= PriorGeneration) 2031 return true; 2032 2033 ASTIdentifierLookupTable *IdTable 2034 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable; 2035 if (!IdTable) 2036 return false; 2037 2038 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M, 2039 Found); 2040 ++NumIdentifierLookups; 2041 ASTIdentifierLookupTable::iterator Pos = 2042 IdTable->find_hashed(Name, NameHash, &Trait); 2043 if (Pos == IdTable->end()) 2044 return false; 2045 2046 // Dereferencing the iterator has the effect of building the 2047 // IdentifierInfo node and populating it with the various 2048 // declarations it needs. 2049 ++NumIdentifierLookupHits; 2050 Found = *Pos; 2051 return true; 2052 } 2053 2054 // Retrieve the identifier info found within the module 2055 // files. 2056 IdentifierInfo *getIdentifierInfo() const { return Found; } 2057 }; 2058 2059 } // namespace 2060 2061 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) { 2062 // Note that we are loading an identifier. 2063 Deserializing AnIdentifier(this); 2064 2065 unsigned PriorGeneration = 0; 2066 if (getContext().getLangOpts().Modules) 2067 PriorGeneration = IdentifierGeneration[&II]; 2068 2069 // If there is a global index, look there first to determine which modules 2070 // provably do not have any results for this identifier. 2071 GlobalModuleIndex::HitSet Hits; 2072 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 2073 if (!loadGlobalIndex()) { 2074 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) { 2075 HitsPtr = &Hits; 2076 } 2077 } 2078 2079 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration, 2080 NumIdentifierLookups, 2081 NumIdentifierLookupHits); 2082 ModuleMgr.visit(Visitor, HitsPtr); 2083 markIdentifierUpToDate(&II); 2084 } 2085 2086 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) { 2087 if (!II) 2088 return; 2089 2090 II->setOutOfDate(false); 2091 2092 // Update the generation for this identifier. 2093 if (getContext().getLangOpts().Modules) 2094 IdentifierGeneration[II] = getGeneration(); 2095 } 2096 2097 void ASTReader::resolvePendingMacro(IdentifierInfo *II, 2098 const PendingMacroInfo &PMInfo) { 2099 ModuleFile &M = *PMInfo.M; 2100 2101 BitstreamCursor &Cursor = M.MacroCursor; 2102 SavedStreamPosition SavedPosition(Cursor); 2103 if (llvm::Error Err = 2104 Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) { 2105 Error(std::move(Err)); 2106 return; 2107 } 2108 2109 struct ModuleMacroRecord { 2110 SubmoduleID SubModID; 2111 MacroInfo *MI; 2112 SmallVector<SubmoduleID, 8> Overrides; 2113 }; 2114 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros; 2115 2116 // We expect to see a sequence of PP_MODULE_MACRO records listing exported 2117 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete 2118 // macro histroy. 2119 RecordData Record; 2120 while (true) { 2121 Expected<llvm::BitstreamEntry> MaybeEntry = 2122 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 2123 if (!MaybeEntry) { 2124 Error(MaybeEntry.takeError()); 2125 return; 2126 } 2127 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2128 2129 if (Entry.Kind != llvm::BitstreamEntry::Record) { 2130 Error("malformed block record in AST file"); 2131 return; 2132 } 2133 2134 Record.clear(); 2135 Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record); 2136 if (!MaybePP) { 2137 Error(MaybePP.takeError()); 2138 return; 2139 } 2140 switch ((PreprocessorRecordTypes)MaybePP.get()) { 2141 case PP_MACRO_DIRECTIVE_HISTORY: 2142 break; 2143 2144 case PP_MODULE_MACRO: { 2145 ModuleMacros.push_back(ModuleMacroRecord()); 2146 auto &Info = ModuleMacros.back(); 2147 Info.SubModID = getGlobalSubmoduleID(M, Record[0]); 2148 Info.MI = getMacro(getGlobalMacroID(M, Record[1])); 2149 for (int I = 2, N = Record.size(); I != N; ++I) 2150 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I])); 2151 continue; 2152 } 2153 2154 default: 2155 Error("malformed block record in AST file"); 2156 return; 2157 } 2158 2159 // We found the macro directive history; that's the last record 2160 // for this macro. 2161 break; 2162 } 2163 2164 // Module macros are listed in reverse dependency order. 2165 { 2166 std::reverse(ModuleMacros.begin(), ModuleMacros.end()); 2167 llvm::SmallVector<ModuleMacro*, 8> Overrides; 2168 for (auto &MMR : ModuleMacros) { 2169 Overrides.clear(); 2170 for (unsigned ModID : MMR.Overrides) { 2171 Module *Mod = getSubmodule(ModID); 2172 auto *Macro = PP.getModuleMacro(Mod, II); 2173 assert(Macro && "missing definition for overridden macro"); 2174 Overrides.push_back(Macro); 2175 } 2176 2177 bool Inserted = false; 2178 Module *Owner = getSubmodule(MMR.SubModID); 2179 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted); 2180 } 2181 } 2182 2183 // Don't read the directive history for a module; we don't have anywhere 2184 // to put it. 2185 if (M.isModule()) 2186 return; 2187 2188 // Deserialize the macro directives history in reverse source-order. 2189 MacroDirective *Latest = nullptr, *Earliest = nullptr; 2190 unsigned Idx = 0, N = Record.size(); 2191 while (Idx < N) { 2192 MacroDirective *MD = nullptr; 2193 SourceLocation Loc = ReadSourceLocation(M, Record, Idx); 2194 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++]; 2195 switch (K) { 2196 case MacroDirective::MD_Define: { 2197 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++])); 2198 MD = PP.AllocateDefMacroDirective(MI, Loc); 2199 break; 2200 } 2201 case MacroDirective::MD_Undefine: 2202 MD = PP.AllocateUndefMacroDirective(Loc); 2203 break; 2204 case MacroDirective::MD_Visibility: 2205 bool isPublic = Record[Idx++]; 2206 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic); 2207 break; 2208 } 2209 2210 if (!Latest) 2211 Latest = MD; 2212 if (Earliest) 2213 Earliest->setPrevious(MD); 2214 Earliest = MD; 2215 } 2216 2217 if (Latest) 2218 PP.setLoadedMacroDirective(II, Earliest, Latest); 2219 } 2220 2221 bool ASTReader::shouldDisableValidationForFile( 2222 const serialization::ModuleFile &M) const { 2223 if (DisableValidationKind == DisableValidationForModuleKind::None) 2224 return false; 2225 2226 // If a PCH is loaded and validation is disabled for PCH then disable 2227 // validation for the PCH and the modules it loads. 2228 ModuleKind K = CurrentDeserializingModuleKind.getValueOr(M.Kind); 2229 2230 switch (K) { 2231 case MK_MainFile: 2232 case MK_Preamble: 2233 case MK_PCH: 2234 return bool(DisableValidationKind & DisableValidationForModuleKind::PCH); 2235 case MK_ImplicitModule: 2236 case MK_ExplicitModule: 2237 case MK_PrebuiltModule: 2238 return bool(DisableValidationKind & DisableValidationForModuleKind::Module); 2239 } 2240 2241 return false; 2242 } 2243 2244 ASTReader::InputFileInfo 2245 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) { 2246 // Go find this input file. 2247 BitstreamCursor &Cursor = F.InputFilesCursor; 2248 SavedStreamPosition SavedPosition(Cursor); 2249 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2250 // FIXME this drops errors on the floor. 2251 consumeError(std::move(Err)); 2252 } 2253 2254 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 2255 if (!MaybeCode) { 2256 // FIXME this drops errors on the floor. 2257 consumeError(MaybeCode.takeError()); 2258 } 2259 unsigned Code = MaybeCode.get(); 2260 RecordData Record; 2261 StringRef Blob; 2262 2263 if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob)) 2264 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE && 2265 "invalid record type for input file"); 2266 else { 2267 // FIXME this drops errors on the floor. 2268 consumeError(Maybe.takeError()); 2269 } 2270 2271 assert(Record[0] == ID && "Bogus stored ID or offset"); 2272 InputFileInfo R; 2273 R.StoredSize = static_cast<off_t>(Record[1]); 2274 R.StoredTime = static_cast<time_t>(Record[2]); 2275 R.Overridden = static_cast<bool>(Record[3]); 2276 R.Transient = static_cast<bool>(Record[4]); 2277 R.TopLevelModuleMap = static_cast<bool>(Record[5]); 2278 R.Filename = std::string(Blob); 2279 ResolveImportedPath(F, R.Filename); 2280 2281 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 2282 if (!MaybeEntry) // FIXME this drops errors on the floor. 2283 consumeError(MaybeEntry.takeError()); 2284 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2285 assert(Entry.Kind == llvm::BitstreamEntry::Record && 2286 "expected record type for input file hash"); 2287 2288 Record.clear(); 2289 if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record)) 2290 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH && 2291 "invalid record type for input file hash"); 2292 else { 2293 // FIXME this drops errors on the floor. 2294 consumeError(Maybe.takeError()); 2295 } 2296 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) | 2297 static_cast<uint64_t>(Record[0]); 2298 return R; 2299 } 2300 2301 static unsigned moduleKindForDiagnostic(ModuleKind Kind); 2302 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) { 2303 // If this ID is bogus, just return an empty input file. 2304 if (ID == 0 || ID > F.InputFilesLoaded.size()) 2305 return InputFile(); 2306 2307 // If we've already loaded this input file, return it. 2308 if (F.InputFilesLoaded[ID-1].getFile()) 2309 return F.InputFilesLoaded[ID-1]; 2310 2311 if (F.InputFilesLoaded[ID-1].isNotFound()) 2312 return InputFile(); 2313 2314 // Go find this input file. 2315 BitstreamCursor &Cursor = F.InputFilesCursor; 2316 SavedStreamPosition SavedPosition(Cursor); 2317 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2318 // FIXME this drops errors on the floor. 2319 consumeError(std::move(Err)); 2320 } 2321 2322 InputFileInfo FI = readInputFileInfo(F, ID); 2323 off_t StoredSize = FI.StoredSize; 2324 time_t StoredTime = FI.StoredTime; 2325 bool Overridden = FI.Overridden; 2326 bool Transient = FI.Transient; 2327 StringRef Filename = FI.Filename; 2328 uint64_t StoredContentHash = FI.ContentHash; 2329 2330 OptionalFileEntryRefDegradesToFileEntryPtr File = 2331 expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false)); 2332 2333 // If we didn't find the file, resolve it relative to the 2334 // original directory from which this AST file was created. 2335 if (!File && !F.OriginalDir.empty() && !F.BaseDirectory.empty() && 2336 F.OriginalDir != F.BaseDirectory) { 2337 std::string Resolved = resolveFileRelativeToOriginalDir( 2338 std::string(Filename), F.OriginalDir, F.BaseDirectory); 2339 if (!Resolved.empty()) 2340 File = expectedToOptional(FileMgr.getFileRef(Resolved)); 2341 } 2342 2343 // For an overridden file, create a virtual file with the stored 2344 // size/timestamp. 2345 if ((Overridden || Transient) && !File) 2346 File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime); 2347 2348 if (!File) { 2349 if (Complain) { 2350 std::string ErrorStr = "could not find file '"; 2351 ErrorStr += Filename; 2352 ErrorStr += "' referenced by AST file '"; 2353 ErrorStr += F.FileName; 2354 ErrorStr += "'"; 2355 Error(ErrorStr); 2356 } 2357 // Record that we didn't find the file. 2358 F.InputFilesLoaded[ID-1] = InputFile::getNotFound(); 2359 return InputFile(); 2360 } 2361 2362 // Check if there was a request to override the contents of the file 2363 // that was part of the precompiled header. Overriding such a file 2364 // can lead to problems when lexing using the source locations from the 2365 // PCH. 2366 SourceManager &SM = getSourceManager(); 2367 // FIXME: Reject if the overrides are different. 2368 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) { 2369 if (Complain) 2370 Error(diag::err_fe_pch_file_overridden, Filename); 2371 2372 // After emitting the diagnostic, bypass the overriding file to recover 2373 // (this creates a separate FileEntry). 2374 File = SM.bypassFileContentsOverride(*File); 2375 if (!File) { 2376 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound(); 2377 return InputFile(); 2378 } 2379 } 2380 2381 struct Change { 2382 enum ModificationKind { 2383 Size, 2384 ModTime, 2385 Content, 2386 None, 2387 } Kind; 2388 llvm::Optional<int64_t> Old = llvm::None; 2389 llvm::Optional<int64_t> New = llvm::None; 2390 }; 2391 auto HasInputFileChanged = [&]() { 2392 if (StoredSize != File->getSize()) 2393 return Change{Change::Size, StoredSize, File->getSize()}; 2394 if (!shouldDisableValidationForFile(F) && StoredTime && 2395 StoredTime != File->getModificationTime()) { 2396 Change MTimeChange = {Change::ModTime, StoredTime, 2397 File->getModificationTime()}; 2398 2399 // In case the modification time changes but not the content, 2400 // accept the cached file as legit. 2401 if (ValidateASTInputFilesContent && 2402 StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) { 2403 auto MemBuffOrError = FileMgr.getBufferForFile(File); 2404 if (!MemBuffOrError) { 2405 if (!Complain) 2406 return MTimeChange; 2407 std::string ErrorStr = "could not get buffer for file '"; 2408 ErrorStr += File->getName(); 2409 ErrorStr += "'"; 2410 Error(ErrorStr); 2411 return MTimeChange; 2412 } 2413 2414 // FIXME: hash_value is not guaranteed to be stable! 2415 auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer()); 2416 if (StoredContentHash == static_cast<uint64_t>(ContentHash)) 2417 return Change{Change::None}; 2418 2419 return Change{Change::Content}; 2420 } 2421 return MTimeChange; 2422 } 2423 return Change{Change::None}; 2424 }; 2425 2426 bool IsOutOfDate = false; 2427 auto FileChange = HasInputFileChanged(); 2428 // For an overridden file, there is nothing to validate. 2429 if (!Overridden && FileChange.Kind != Change::None) { 2430 if (Complain && !Diags.isDiagnosticInFlight()) { 2431 // Build a list of the PCH imports that got us here (in reverse). 2432 SmallVector<ModuleFile *, 4> ImportStack(1, &F); 2433 while (!ImportStack.back()->ImportedBy.empty()) 2434 ImportStack.push_back(ImportStack.back()->ImportedBy[0]); 2435 2436 // The top-level PCH is stale. 2437 StringRef TopLevelPCHName(ImportStack.back()->FileName); 2438 Diag(diag::err_fe_ast_file_modified) 2439 << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind) 2440 << TopLevelPCHName << FileChange.Kind 2441 << (FileChange.Old && FileChange.New) 2442 << llvm::itostr(FileChange.Old.getValueOr(0)) 2443 << llvm::itostr(FileChange.New.getValueOr(0)); 2444 2445 // Print the import stack. 2446 if (ImportStack.size() > 1) { 2447 Diag(diag::note_pch_required_by) 2448 << Filename << ImportStack[0]->FileName; 2449 for (unsigned I = 1; I < ImportStack.size(); ++I) 2450 Diag(diag::note_pch_required_by) 2451 << ImportStack[I-1]->FileName << ImportStack[I]->FileName; 2452 } 2453 2454 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName; 2455 } 2456 2457 IsOutOfDate = true; 2458 } 2459 // FIXME: If the file is overridden and we've already opened it, 2460 // issue an error (or split it into a separate FileEntry). 2461 2462 InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate); 2463 2464 // Note that we've loaded this input file. 2465 F.InputFilesLoaded[ID-1] = IF; 2466 return IF; 2467 } 2468 2469 /// If we are loading a relocatable PCH or module file, and the filename 2470 /// is not an absolute path, add the system or module root to the beginning of 2471 /// the file name. 2472 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) { 2473 // Resolve relative to the base directory, if we have one. 2474 if (!M.BaseDirectory.empty()) 2475 return ResolveImportedPath(Filename, M.BaseDirectory); 2476 } 2477 2478 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) { 2479 if (Filename.empty() || llvm::sys::path::is_absolute(Filename)) 2480 return; 2481 2482 SmallString<128> Buffer; 2483 llvm::sys::path::append(Buffer, Prefix, Filename); 2484 Filename.assign(Buffer.begin(), Buffer.end()); 2485 } 2486 2487 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) { 2488 switch (ARR) { 2489 case ASTReader::Failure: return true; 2490 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing); 2491 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate); 2492 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch); 2493 case ASTReader::ConfigurationMismatch: 2494 return !(Caps & ASTReader::ARR_ConfigurationMismatch); 2495 case ASTReader::HadErrors: return true; 2496 case ASTReader::Success: return false; 2497 } 2498 2499 llvm_unreachable("unknown ASTReadResult"); 2500 } 2501 2502 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock( 2503 BitstreamCursor &Stream, unsigned ClientLoadCapabilities, 2504 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener, 2505 std::string &SuggestedPredefines) { 2506 if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) { 2507 // FIXME this drops errors on the floor. 2508 consumeError(std::move(Err)); 2509 return Failure; 2510 } 2511 2512 // Read all of the records in the options block. 2513 RecordData Record; 2514 ASTReadResult Result = Success; 2515 while (true) { 2516 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2517 if (!MaybeEntry) { 2518 // FIXME this drops errors on the floor. 2519 consumeError(MaybeEntry.takeError()); 2520 return Failure; 2521 } 2522 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2523 2524 switch (Entry.Kind) { 2525 case llvm::BitstreamEntry::Error: 2526 case llvm::BitstreamEntry::SubBlock: 2527 return Failure; 2528 2529 case llvm::BitstreamEntry::EndBlock: 2530 return Result; 2531 2532 case llvm::BitstreamEntry::Record: 2533 // The interesting case. 2534 break; 2535 } 2536 2537 // Read and process a record. 2538 Record.clear(); 2539 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record); 2540 if (!MaybeRecordType) { 2541 // FIXME this drops errors on the floor. 2542 consumeError(MaybeRecordType.takeError()); 2543 return Failure; 2544 } 2545 switch ((OptionsRecordTypes)MaybeRecordType.get()) { 2546 case LANGUAGE_OPTIONS: { 2547 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2548 if (ParseLanguageOptions(Record, Complain, Listener, 2549 AllowCompatibleConfigurationMismatch)) 2550 Result = ConfigurationMismatch; 2551 break; 2552 } 2553 2554 case TARGET_OPTIONS: { 2555 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2556 if (ParseTargetOptions(Record, Complain, Listener, 2557 AllowCompatibleConfigurationMismatch)) 2558 Result = ConfigurationMismatch; 2559 break; 2560 } 2561 2562 case FILE_SYSTEM_OPTIONS: { 2563 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2564 if (!AllowCompatibleConfigurationMismatch && 2565 ParseFileSystemOptions(Record, Complain, Listener)) 2566 Result = ConfigurationMismatch; 2567 break; 2568 } 2569 2570 case HEADER_SEARCH_OPTIONS: { 2571 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2572 if (!AllowCompatibleConfigurationMismatch && 2573 ParseHeaderSearchOptions(Record, Complain, Listener)) 2574 Result = ConfigurationMismatch; 2575 break; 2576 } 2577 2578 case PREPROCESSOR_OPTIONS: 2579 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2580 if (!AllowCompatibleConfigurationMismatch && 2581 ParsePreprocessorOptions(Record, Complain, Listener, 2582 SuggestedPredefines)) 2583 Result = ConfigurationMismatch; 2584 break; 2585 } 2586 } 2587 } 2588 2589 ASTReader::ASTReadResult 2590 ASTReader::ReadControlBlock(ModuleFile &F, 2591 SmallVectorImpl<ImportedModule> &Loaded, 2592 const ModuleFile *ImportedBy, 2593 unsigned ClientLoadCapabilities) { 2594 BitstreamCursor &Stream = F.Stream; 2595 2596 if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) { 2597 Error(std::move(Err)); 2598 return Failure; 2599 } 2600 2601 // Lambda to read the unhashed control block the first time it's called. 2602 // 2603 // For PCM files, the unhashed control block cannot be read until after the 2604 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still 2605 // need to look ahead before reading the IMPORTS record. For consistency, 2606 // this block is always read somehow (see BitstreamEntry::EndBlock). 2607 bool HasReadUnhashedControlBlock = false; 2608 auto readUnhashedControlBlockOnce = [&]() { 2609 if (!HasReadUnhashedControlBlock) { 2610 HasReadUnhashedControlBlock = true; 2611 if (ASTReadResult Result = 2612 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities)) 2613 return Result; 2614 } 2615 return Success; 2616 }; 2617 2618 bool DisableValidation = shouldDisableValidationForFile(F); 2619 2620 // Read all of the records and blocks in the control block. 2621 RecordData Record; 2622 unsigned NumInputs = 0; 2623 unsigned NumUserInputs = 0; 2624 StringRef BaseDirectoryAsWritten; 2625 while (true) { 2626 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2627 if (!MaybeEntry) { 2628 Error(MaybeEntry.takeError()); 2629 return Failure; 2630 } 2631 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2632 2633 switch (Entry.Kind) { 2634 case llvm::BitstreamEntry::Error: 2635 Error("malformed block record in AST file"); 2636 return Failure; 2637 case llvm::BitstreamEntry::EndBlock: { 2638 // Validate the module before returning. This call catches an AST with 2639 // no module name and no imports. 2640 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2641 return Result; 2642 2643 // Validate input files. 2644 const HeaderSearchOptions &HSOpts = 2645 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 2646 2647 // All user input files reside at the index range [0, NumUserInputs), and 2648 // system input files reside at [NumUserInputs, NumInputs). For explicitly 2649 // loaded module files, ignore missing inputs. 2650 if (!DisableValidation && F.Kind != MK_ExplicitModule && 2651 F.Kind != MK_PrebuiltModule) { 2652 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 2653 2654 // If we are reading a module, we will create a verification timestamp, 2655 // so we verify all input files. Otherwise, verify only user input 2656 // files. 2657 2658 unsigned N = NumUserInputs; 2659 if (ValidateSystemInputs || 2660 (HSOpts.ModulesValidateOncePerBuildSession && 2661 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp && 2662 F.Kind == MK_ImplicitModule)) 2663 N = NumInputs; 2664 2665 for (unsigned I = 0; I < N; ++I) { 2666 InputFile IF = getInputFile(F, I+1, Complain); 2667 if (!IF.getFile() || IF.isOutOfDate()) 2668 return OutOfDate; 2669 } 2670 } 2671 2672 if (Listener) 2673 Listener->visitModuleFile(F.FileName, F.Kind); 2674 2675 if (Listener && Listener->needsInputFileVisitation()) { 2676 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs 2677 : NumUserInputs; 2678 for (unsigned I = 0; I < N; ++I) { 2679 bool IsSystem = I >= NumUserInputs; 2680 InputFileInfo FI = readInputFileInfo(F, I+1); 2681 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden, 2682 F.Kind == MK_ExplicitModule || 2683 F.Kind == MK_PrebuiltModule); 2684 } 2685 } 2686 2687 return Success; 2688 } 2689 2690 case llvm::BitstreamEntry::SubBlock: 2691 switch (Entry.ID) { 2692 case INPUT_FILES_BLOCK_ID: 2693 F.InputFilesCursor = Stream; 2694 if (llvm::Error Err = Stream.SkipBlock()) { 2695 Error(std::move(Err)); 2696 return Failure; 2697 } 2698 if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) { 2699 Error("malformed block record in AST file"); 2700 return Failure; 2701 } 2702 continue; 2703 2704 case OPTIONS_BLOCK_ID: 2705 // If we're reading the first module for this group, check its options 2706 // are compatible with ours. For modules it imports, no further checking 2707 // is required, because we checked them when we built it. 2708 if (Listener && !ImportedBy) { 2709 // Should we allow the configuration of the module file to differ from 2710 // the configuration of the current translation unit in a compatible 2711 // way? 2712 // 2713 // FIXME: Allow this for files explicitly specified with -include-pch. 2714 bool AllowCompatibleConfigurationMismatch = 2715 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 2716 2717 ASTReadResult Result = 2718 ReadOptionsBlock(Stream, ClientLoadCapabilities, 2719 AllowCompatibleConfigurationMismatch, *Listener, 2720 SuggestedPredefines); 2721 if (Result == Failure) { 2722 Error("malformed block record in AST file"); 2723 return Result; 2724 } 2725 2726 if (DisableValidation || 2727 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 2728 Result = Success; 2729 2730 // If we can't load the module, exit early since we likely 2731 // will rebuild the module anyway. The stream may be in the 2732 // middle of a block. 2733 if (Result != Success) 2734 return Result; 2735 } else if (llvm::Error Err = Stream.SkipBlock()) { 2736 Error(std::move(Err)); 2737 return Failure; 2738 } 2739 continue; 2740 2741 default: 2742 if (llvm::Error Err = Stream.SkipBlock()) { 2743 Error(std::move(Err)); 2744 return Failure; 2745 } 2746 continue; 2747 } 2748 2749 case llvm::BitstreamEntry::Record: 2750 // The interesting case. 2751 break; 2752 } 2753 2754 // Read and process a record. 2755 Record.clear(); 2756 StringRef Blob; 2757 Expected<unsigned> MaybeRecordType = 2758 Stream.readRecord(Entry.ID, Record, &Blob); 2759 if (!MaybeRecordType) { 2760 Error(MaybeRecordType.takeError()); 2761 return Failure; 2762 } 2763 switch ((ControlRecordTypes)MaybeRecordType.get()) { 2764 case METADATA: { 2765 if (Record[0] != VERSION_MAJOR && !DisableValidation) { 2766 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2767 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old 2768 : diag::err_pch_version_too_new); 2769 return VersionMismatch; 2770 } 2771 2772 bool hasErrors = Record[6]; 2773 if (hasErrors && !DisableValidation) { 2774 // If requested by the caller and the module hasn't already been read 2775 // or compiled, mark modules on error as out-of-date. 2776 if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) && 2777 canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 2778 return OutOfDate; 2779 2780 if (!AllowASTWithCompilerErrors) { 2781 Diag(diag::err_pch_with_compiler_errors); 2782 return HadErrors; 2783 } 2784 } 2785 if (hasErrors) { 2786 Diags.ErrorOccurred = true; 2787 Diags.UncompilableErrorOccurred = true; 2788 Diags.UnrecoverableErrorOccurred = true; 2789 } 2790 2791 F.RelocatablePCH = Record[4]; 2792 // Relative paths in a relocatable PCH are relative to our sysroot. 2793 if (F.RelocatablePCH) 2794 F.BaseDirectory = isysroot.empty() ? "/" : isysroot; 2795 2796 F.HasTimestamps = Record[5]; 2797 2798 const std::string &CurBranch = getClangFullRepositoryVersion(); 2799 StringRef ASTBranch = Blob; 2800 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) { 2801 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2802 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch; 2803 return VersionMismatch; 2804 } 2805 break; 2806 } 2807 2808 case IMPORTS: { 2809 // Validate the AST before processing any imports (otherwise, untangling 2810 // them can be error-prone and expensive). A module will have a name and 2811 // will already have been validated, but this catches the PCH case. 2812 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2813 return Result; 2814 2815 // Load each of the imported PCH files. 2816 unsigned Idx = 0, N = Record.size(); 2817 while (Idx < N) { 2818 // Read information about the AST file. 2819 ModuleKind ImportedKind = (ModuleKind)Record[Idx++]; 2820 // The import location will be the local one for now; we will adjust 2821 // all import locations of module imports after the global source 2822 // location info are setup, in ReadAST. 2823 SourceLocation ImportLoc = 2824 ReadUntranslatedSourceLocation(Record[Idx++]); 2825 off_t StoredSize = (off_t)Record[Idx++]; 2826 time_t StoredModTime = (time_t)Record[Idx++]; 2827 auto FirstSignatureByte = Record.begin() + Idx; 2828 ASTFileSignature StoredSignature = ASTFileSignature::create( 2829 FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size); 2830 Idx += ASTFileSignature::size; 2831 2832 std::string ImportedName = ReadString(Record, Idx); 2833 std::string ImportedFile; 2834 2835 // For prebuilt and explicit modules first consult the file map for 2836 // an override. Note that here we don't search prebuilt module 2837 // directories, only the explicit name to file mappings. Also, we will 2838 // still verify the size/signature making sure it is essentially the 2839 // same file but perhaps in a different location. 2840 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule) 2841 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName( 2842 ImportedName, /*FileMapOnly*/ true); 2843 2844 if (ImportedFile.empty()) 2845 // Use BaseDirectoryAsWritten to ensure we use the same path in the 2846 // ModuleCache as when writing. 2847 ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx); 2848 else 2849 SkipPath(Record, Idx); 2850 2851 // If our client can't cope with us being out of date, we can't cope with 2852 // our dependency being missing. 2853 unsigned Capabilities = ClientLoadCapabilities; 2854 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 2855 Capabilities &= ~ARR_Missing; 2856 2857 // Load the AST file. 2858 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, 2859 Loaded, StoredSize, StoredModTime, 2860 StoredSignature, Capabilities); 2861 2862 // If we diagnosed a problem, produce a backtrace. 2863 bool recompilingFinalized = 2864 Result == OutOfDate && (Capabilities & ARR_OutOfDate) && 2865 getModuleManager().getModuleCache().isPCMFinal(F.FileName); 2866 if (isDiagnosedResult(Result, Capabilities) || recompilingFinalized) 2867 Diag(diag::note_module_file_imported_by) 2868 << F.FileName << !F.ModuleName.empty() << F.ModuleName; 2869 if (recompilingFinalized) 2870 Diag(diag::note_module_file_conflict); 2871 2872 switch (Result) { 2873 case Failure: return Failure; 2874 // If we have to ignore the dependency, we'll have to ignore this too. 2875 case Missing: 2876 case OutOfDate: return OutOfDate; 2877 case VersionMismatch: return VersionMismatch; 2878 case ConfigurationMismatch: return ConfigurationMismatch; 2879 case HadErrors: return HadErrors; 2880 case Success: break; 2881 } 2882 } 2883 break; 2884 } 2885 2886 case ORIGINAL_FILE: 2887 F.OriginalSourceFileID = FileID::get(Record[0]); 2888 F.ActualOriginalSourceFileName = std::string(Blob); 2889 F.OriginalSourceFileName = F.ActualOriginalSourceFileName; 2890 ResolveImportedPath(F, F.OriginalSourceFileName); 2891 break; 2892 2893 case ORIGINAL_FILE_ID: 2894 F.OriginalSourceFileID = FileID::get(Record[0]); 2895 break; 2896 2897 case ORIGINAL_PCH_DIR: 2898 F.OriginalDir = std::string(Blob); 2899 break; 2900 2901 case MODULE_NAME: 2902 F.ModuleName = std::string(Blob); 2903 Diag(diag::remark_module_import) 2904 << F.ModuleName << F.FileName << (ImportedBy ? true : false) 2905 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 2906 if (Listener) 2907 Listener->ReadModuleName(F.ModuleName); 2908 2909 // Validate the AST as soon as we have a name so we can exit early on 2910 // failure. 2911 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2912 return Result; 2913 2914 break; 2915 2916 case MODULE_DIRECTORY: { 2917 // Save the BaseDirectory as written in the PCM for computing the module 2918 // filename for the ModuleCache. 2919 BaseDirectoryAsWritten = Blob; 2920 assert(!F.ModuleName.empty() && 2921 "MODULE_DIRECTORY found before MODULE_NAME"); 2922 // If we've already loaded a module map file covering this module, we may 2923 // have a better path for it (relative to the current build). 2924 Module *M = PP.getHeaderSearchInfo().lookupModule( 2925 F.ModuleName, SourceLocation(), /*AllowSearch*/ true, 2926 /*AllowExtraModuleMapSearch*/ true); 2927 if (M && M->Directory) { 2928 // If we're implicitly loading a module, the base directory can't 2929 // change between the build and use. 2930 // Don't emit module relocation error if we have -fno-validate-pch 2931 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation & 2932 DisableValidationForModuleKind::Module) && 2933 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) { 2934 auto BuildDir = PP.getFileManager().getDirectory(Blob); 2935 if (!BuildDir || *BuildDir != M->Directory) { 2936 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 2937 Diag(diag::err_imported_module_relocated) 2938 << F.ModuleName << Blob << M->Directory->getName(); 2939 return OutOfDate; 2940 } 2941 } 2942 F.BaseDirectory = std::string(M->Directory->getName()); 2943 } else { 2944 F.BaseDirectory = std::string(Blob); 2945 } 2946 break; 2947 } 2948 2949 case MODULE_MAP_FILE: 2950 if (ASTReadResult Result = 2951 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities)) 2952 return Result; 2953 break; 2954 2955 case INPUT_FILE_OFFSETS: 2956 NumInputs = Record[0]; 2957 NumUserInputs = Record[1]; 2958 F.InputFileOffsets = 2959 (const llvm::support::unaligned_uint64_t *)Blob.data(); 2960 F.InputFilesLoaded.resize(NumInputs); 2961 F.NumUserInputFiles = NumUserInputs; 2962 break; 2963 } 2964 } 2965 } 2966 2967 void ASTReader::readIncludedFiles(ModuleFile &F, StringRef Blob, 2968 Preprocessor &PP) { 2969 using namespace llvm::support; 2970 2971 const unsigned char *D = (const unsigned char *)Blob.data(); 2972 unsigned FileCount = endian::readNext<uint32_t, little, unaligned>(D); 2973 2974 for (unsigned I = 0; I < FileCount; ++I) { 2975 size_t ID = endian::readNext<uint32_t, little, unaligned>(D); 2976 InputFileInfo IFI = readInputFileInfo(F, ID); 2977 if (llvm::ErrorOr<const FileEntry *> File = 2978 PP.getFileManager().getFile(IFI.Filename)) 2979 PP.getIncludedFiles().insert(*File); 2980 } 2981 } 2982 2983 llvm::Error ASTReader::ReadASTBlock(ModuleFile &F, 2984 unsigned ClientLoadCapabilities) { 2985 BitstreamCursor &Stream = F.Stream; 2986 2987 if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) 2988 return Err; 2989 F.ASTBlockStartOffset = Stream.GetCurrentBitNo(); 2990 2991 // Read all of the records and blocks for the AST file. 2992 RecordData Record; 2993 while (true) { 2994 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2995 if (!MaybeEntry) 2996 return MaybeEntry.takeError(); 2997 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2998 2999 switch (Entry.Kind) { 3000 case llvm::BitstreamEntry::Error: 3001 return llvm::createStringError( 3002 std::errc::illegal_byte_sequence, 3003 "error at end of module block in AST file"); 3004 case llvm::BitstreamEntry::EndBlock: 3005 // Outside of C++, we do not store a lookup map for the translation unit. 3006 // Instead, mark it as needing a lookup map to be built if this module 3007 // contains any declarations lexically within it (which it always does!). 3008 // This usually has no cost, since we very rarely need the lookup map for 3009 // the translation unit outside C++. 3010 if (ASTContext *Ctx = ContextObj) { 3011 DeclContext *DC = Ctx->getTranslationUnitDecl(); 3012 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus) 3013 DC->setMustBuildLookupTable(); 3014 } 3015 3016 return llvm::Error::success(); 3017 case llvm::BitstreamEntry::SubBlock: 3018 switch (Entry.ID) { 3019 case DECLTYPES_BLOCK_ID: 3020 // We lazily load the decls block, but we want to set up the 3021 // DeclsCursor cursor to point into it. Clone our current bitcode 3022 // cursor to it, enter the block and read the abbrevs in that block. 3023 // With the main cursor, we just skip over it. 3024 F.DeclsCursor = Stream; 3025 if (llvm::Error Err = Stream.SkipBlock()) 3026 return Err; 3027 if (llvm::Error Err = ReadBlockAbbrevs( 3028 F.DeclsCursor, DECLTYPES_BLOCK_ID, &F.DeclsBlockStartOffset)) 3029 return Err; 3030 break; 3031 3032 case PREPROCESSOR_BLOCK_ID: 3033 F.MacroCursor = Stream; 3034 if (!PP.getExternalSource()) 3035 PP.setExternalSource(this); 3036 3037 if (llvm::Error Err = Stream.SkipBlock()) 3038 return Err; 3039 if (llvm::Error Err = 3040 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) 3041 return Err; 3042 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo(); 3043 break; 3044 3045 case PREPROCESSOR_DETAIL_BLOCK_ID: 3046 F.PreprocessorDetailCursor = Stream; 3047 3048 if (llvm::Error Err = Stream.SkipBlock()) { 3049 return Err; 3050 } 3051 if (llvm::Error Err = ReadBlockAbbrevs(F.PreprocessorDetailCursor, 3052 PREPROCESSOR_DETAIL_BLOCK_ID)) 3053 return Err; 3054 F.PreprocessorDetailStartOffset 3055 = F.PreprocessorDetailCursor.GetCurrentBitNo(); 3056 3057 if (!PP.getPreprocessingRecord()) 3058 PP.createPreprocessingRecord(); 3059 if (!PP.getPreprocessingRecord()->getExternalSource()) 3060 PP.getPreprocessingRecord()->SetExternalSource(*this); 3061 break; 3062 3063 case SOURCE_MANAGER_BLOCK_ID: 3064 if (llvm::Error Err = ReadSourceManagerBlock(F)) 3065 return Err; 3066 break; 3067 3068 case SUBMODULE_BLOCK_ID: 3069 if (llvm::Error Err = ReadSubmoduleBlock(F, ClientLoadCapabilities)) 3070 return Err; 3071 break; 3072 3073 case COMMENTS_BLOCK_ID: { 3074 BitstreamCursor C = Stream; 3075 3076 if (llvm::Error Err = Stream.SkipBlock()) 3077 return Err; 3078 if (llvm::Error Err = ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) 3079 return Err; 3080 CommentsCursors.push_back(std::make_pair(C, &F)); 3081 break; 3082 } 3083 3084 default: 3085 if (llvm::Error Err = Stream.SkipBlock()) 3086 return Err; 3087 break; 3088 } 3089 continue; 3090 3091 case llvm::BitstreamEntry::Record: 3092 // The interesting case. 3093 break; 3094 } 3095 3096 // Read and process a record. 3097 Record.clear(); 3098 StringRef Blob; 3099 Expected<unsigned> MaybeRecordType = 3100 Stream.readRecord(Entry.ID, Record, &Blob); 3101 if (!MaybeRecordType) 3102 return MaybeRecordType.takeError(); 3103 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get(); 3104 3105 // If we're not loading an AST context, we don't care about most records. 3106 if (!ContextObj) { 3107 switch (RecordType) { 3108 case IDENTIFIER_TABLE: 3109 case IDENTIFIER_OFFSET: 3110 case INTERESTING_IDENTIFIERS: 3111 case STATISTICS: 3112 case PP_ASSUME_NONNULL_LOC: 3113 case PP_CONDITIONAL_STACK: 3114 case PP_COUNTER_VALUE: 3115 case SOURCE_LOCATION_OFFSETS: 3116 case MODULE_OFFSET_MAP: 3117 case SOURCE_MANAGER_LINE_TABLE: 3118 case SOURCE_LOCATION_PRELOADS: 3119 case PPD_ENTITIES_OFFSETS: 3120 case HEADER_SEARCH_TABLE: 3121 case IMPORTED_MODULES: 3122 case MACRO_OFFSET: 3123 break; 3124 default: 3125 continue; 3126 } 3127 } 3128 3129 switch (RecordType) { 3130 default: // Default behavior: ignore. 3131 break; 3132 3133 case TYPE_OFFSET: { 3134 if (F.LocalNumTypes != 0) 3135 return llvm::createStringError( 3136 std::errc::illegal_byte_sequence, 3137 "duplicate TYPE_OFFSET record in AST file"); 3138 F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data()); 3139 F.LocalNumTypes = Record[0]; 3140 unsigned LocalBaseTypeIndex = Record[1]; 3141 F.BaseTypeIndex = getTotalNumTypes(); 3142 3143 if (F.LocalNumTypes > 0) { 3144 // Introduce the global -> local mapping for types within this module. 3145 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F)); 3146 3147 // Introduce the local -> global mapping for types within this module. 3148 F.TypeRemap.insertOrReplace( 3149 std::make_pair(LocalBaseTypeIndex, 3150 F.BaseTypeIndex - LocalBaseTypeIndex)); 3151 3152 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes); 3153 } 3154 break; 3155 } 3156 3157 case DECL_OFFSET: { 3158 if (F.LocalNumDecls != 0) 3159 return llvm::createStringError( 3160 std::errc::illegal_byte_sequence, 3161 "duplicate DECL_OFFSET record in AST file"); 3162 F.DeclOffsets = (const DeclOffset *)Blob.data(); 3163 F.LocalNumDecls = Record[0]; 3164 unsigned LocalBaseDeclID = Record[1]; 3165 F.BaseDeclID = getTotalNumDecls(); 3166 3167 if (F.LocalNumDecls > 0) { 3168 // Introduce the global -> local mapping for declarations within this 3169 // module. 3170 GlobalDeclMap.insert( 3171 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F)); 3172 3173 // Introduce the local -> global mapping for declarations within this 3174 // module. 3175 F.DeclRemap.insertOrReplace( 3176 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID)); 3177 3178 // Introduce the global -> local mapping for declarations within this 3179 // module. 3180 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID; 3181 3182 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls); 3183 } 3184 break; 3185 } 3186 3187 case TU_UPDATE_LEXICAL: { 3188 DeclContext *TU = ContextObj->getTranslationUnitDecl(); 3189 LexicalContents Contents( 3190 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 3191 Blob.data()), 3192 static_cast<unsigned int>(Blob.size() / 4)); 3193 TULexicalDecls.push_back(std::make_pair(&F, Contents)); 3194 TU->setHasExternalLexicalStorage(true); 3195 break; 3196 } 3197 3198 case UPDATE_VISIBLE: { 3199 unsigned Idx = 0; 3200 serialization::DeclID ID = ReadDeclID(F, Record, Idx); 3201 auto *Data = (const unsigned char*)Blob.data(); 3202 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data}); 3203 // If we've already loaded the decl, perform the updates when we finish 3204 // loading this block. 3205 if (Decl *D = GetExistingDecl(ID)) 3206 PendingUpdateRecords.push_back( 3207 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3208 break; 3209 } 3210 3211 case IDENTIFIER_TABLE: 3212 F.IdentifierTableData = 3213 reinterpret_cast<const unsigned char *>(Blob.data()); 3214 if (Record[0]) { 3215 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create( 3216 F.IdentifierTableData + Record[0], 3217 F.IdentifierTableData + sizeof(uint32_t), 3218 F.IdentifierTableData, 3219 ASTIdentifierLookupTrait(*this, F)); 3220 3221 PP.getIdentifierTable().setExternalIdentifierLookup(this); 3222 } 3223 break; 3224 3225 case IDENTIFIER_OFFSET: { 3226 if (F.LocalNumIdentifiers != 0) 3227 return llvm::createStringError( 3228 std::errc::illegal_byte_sequence, 3229 "duplicate IDENTIFIER_OFFSET record in AST file"); 3230 F.IdentifierOffsets = (const uint32_t *)Blob.data(); 3231 F.LocalNumIdentifiers = Record[0]; 3232 unsigned LocalBaseIdentifierID = Record[1]; 3233 F.BaseIdentifierID = getTotalNumIdentifiers(); 3234 3235 if (F.LocalNumIdentifiers > 0) { 3236 // Introduce the global -> local mapping for identifiers within this 3237 // module. 3238 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1, 3239 &F)); 3240 3241 // Introduce the local -> global mapping for identifiers within this 3242 // module. 3243 F.IdentifierRemap.insertOrReplace( 3244 std::make_pair(LocalBaseIdentifierID, 3245 F.BaseIdentifierID - LocalBaseIdentifierID)); 3246 3247 IdentifiersLoaded.resize(IdentifiersLoaded.size() 3248 + F.LocalNumIdentifiers); 3249 } 3250 break; 3251 } 3252 3253 case INTERESTING_IDENTIFIERS: 3254 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end()); 3255 break; 3256 3257 case EAGERLY_DESERIALIZED_DECLS: 3258 // FIXME: Skip reading this record if our ASTConsumer doesn't care 3259 // about "interesting" decls (for instance, if we're building a module). 3260 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3261 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3262 break; 3263 3264 case MODULAR_CODEGEN_DECLS: 3265 // FIXME: Skip reading this record if our ASTConsumer doesn't care about 3266 // them (ie: if we're not codegenerating this module). 3267 if (F.Kind == MK_MainFile || 3268 getContext().getLangOpts().BuildingPCHWithObjectFile) 3269 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3270 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3271 break; 3272 3273 case SPECIAL_TYPES: 3274 if (SpecialTypes.empty()) { 3275 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3276 SpecialTypes.push_back(getGlobalTypeID(F, Record[I])); 3277 break; 3278 } 3279 3280 if (SpecialTypes.size() != Record.size()) 3281 return llvm::createStringError(std::errc::illegal_byte_sequence, 3282 "invalid special-types record"); 3283 3284 for (unsigned I = 0, N = Record.size(); I != N; ++I) { 3285 serialization::TypeID ID = getGlobalTypeID(F, Record[I]); 3286 if (!SpecialTypes[I]) 3287 SpecialTypes[I] = ID; 3288 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate 3289 // merge step? 3290 } 3291 break; 3292 3293 case STATISTICS: 3294 TotalNumStatements += Record[0]; 3295 TotalNumMacros += Record[1]; 3296 TotalLexicalDeclContexts += Record[2]; 3297 TotalVisibleDeclContexts += Record[3]; 3298 break; 3299 3300 case UNUSED_FILESCOPED_DECLS: 3301 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3302 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I])); 3303 break; 3304 3305 case DELEGATING_CTORS: 3306 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3307 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I])); 3308 break; 3309 3310 case WEAK_UNDECLARED_IDENTIFIERS: 3311 if (Record.size() % 3 != 0) 3312 return llvm::createStringError(std::errc::illegal_byte_sequence, 3313 "invalid weak identifiers record"); 3314 3315 // FIXME: Ignore weak undeclared identifiers from non-original PCH 3316 // files. This isn't the way to do it :) 3317 WeakUndeclaredIdentifiers.clear(); 3318 3319 // Translate the weak, undeclared identifiers into global IDs. 3320 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) { 3321 WeakUndeclaredIdentifiers.push_back( 3322 getGlobalIdentifierID(F, Record[I++])); 3323 WeakUndeclaredIdentifiers.push_back( 3324 getGlobalIdentifierID(F, Record[I++])); 3325 WeakUndeclaredIdentifiers.push_back( 3326 ReadSourceLocation(F, Record, I).getRawEncoding()); 3327 } 3328 break; 3329 3330 case SELECTOR_OFFSETS: { 3331 F.SelectorOffsets = (const uint32_t *)Blob.data(); 3332 F.LocalNumSelectors = Record[0]; 3333 unsigned LocalBaseSelectorID = Record[1]; 3334 F.BaseSelectorID = getTotalNumSelectors(); 3335 3336 if (F.LocalNumSelectors > 0) { 3337 // Introduce the global -> local mapping for selectors within this 3338 // module. 3339 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F)); 3340 3341 // Introduce the local -> global mapping for selectors within this 3342 // module. 3343 F.SelectorRemap.insertOrReplace( 3344 std::make_pair(LocalBaseSelectorID, 3345 F.BaseSelectorID - LocalBaseSelectorID)); 3346 3347 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors); 3348 } 3349 break; 3350 } 3351 3352 case METHOD_POOL: 3353 F.SelectorLookupTableData = (const unsigned char *)Blob.data(); 3354 if (Record[0]) 3355 F.SelectorLookupTable 3356 = ASTSelectorLookupTable::Create( 3357 F.SelectorLookupTableData + Record[0], 3358 F.SelectorLookupTableData, 3359 ASTSelectorLookupTrait(*this, F)); 3360 TotalNumMethodPoolEntries += Record[1]; 3361 break; 3362 3363 case REFERENCED_SELECTOR_POOL: 3364 if (!Record.empty()) { 3365 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) { 3366 ReferencedSelectorsData.push_back(getGlobalSelectorID(F, 3367 Record[Idx++])); 3368 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx). 3369 getRawEncoding()); 3370 } 3371 } 3372 break; 3373 3374 case PP_ASSUME_NONNULL_LOC: { 3375 unsigned Idx = 0; 3376 if (!Record.empty()) 3377 PP.setPreambleRecordedPragmaAssumeNonNullLoc( 3378 ReadSourceLocation(F, Record, Idx)); 3379 break; 3380 } 3381 3382 case PP_CONDITIONAL_STACK: 3383 if (!Record.empty()) { 3384 unsigned Idx = 0, End = Record.size() - 1; 3385 bool ReachedEOFWhileSkipping = Record[Idx++]; 3386 llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo; 3387 if (ReachedEOFWhileSkipping) { 3388 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx); 3389 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx); 3390 bool FoundNonSkipPortion = Record[Idx++]; 3391 bool FoundElse = Record[Idx++]; 3392 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx); 3393 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion, 3394 FoundElse, ElseLoc); 3395 } 3396 SmallVector<PPConditionalInfo, 4> ConditionalStack; 3397 while (Idx < End) { 3398 auto Loc = ReadSourceLocation(F, Record, Idx); 3399 bool WasSkipping = Record[Idx++]; 3400 bool FoundNonSkip = Record[Idx++]; 3401 bool FoundElse = Record[Idx++]; 3402 ConditionalStack.push_back( 3403 {Loc, WasSkipping, FoundNonSkip, FoundElse}); 3404 } 3405 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo); 3406 } 3407 break; 3408 3409 case PP_COUNTER_VALUE: 3410 if (!Record.empty() && Listener) 3411 Listener->ReadCounter(F, Record[0]); 3412 break; 3413 3414 case FILE_SORTED_DECLS: 3415 F.FileSortedDecls = (const DeclID *)Blob.data(); 3416 F.NumFileSortedDecls = Record[0]; 3417 break; 3418 3419 case SOURCE_LOCATION_OFFSETS: { 3420 F.SLocEntryOffsets = (const uint32_t *)Blob.data(); 3421 F.LocalNumSLocEntries = Record[0]; 3422 SourceLocation::UIntTy SLocSpaceSize = Record[1]; 3423 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset; 3424 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) = 3425 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, 3426 SLocSpaceSize); 3427 if (!F.SLocEntryBaseID) 3428 return llvm::createStringError(std::errc::invalid_argument, 3429 "ran out of source locations"); 3430 // Make our entry in the range map. BaseID is negative and growing, so 3431 // we invert it. Because we invert it, though, we need the other end of 3432 // the range. 3433 unsigned RangeStart = 3434 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1; 3435 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F)); 3436 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset); 3437 3438 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing. 3439 assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0); 3440 GlobalSLocOffsetMap.insert( 3441 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset 3442 - SLocSpaceSize,&F)); 3443 3444 // Initialize the remapping table. 3445 // Invalid stays invalid. 3446 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0)); 3447 // This module. Base was 2 when being compiled. 3448 F.SLocRemap.insertOrReplace(std::make_pair( 3449 2U, static_cast<SourceLocation::IntTy>(F.SLocEntryBaseOffset - 2))); 3450 3451 TotalNumSLocEntries += F.LocalNumSLocEntries; 3452 break; 3453 } 3454 3455 case MODULE_OFFSET_MAP: 3456 F.ModuleOffsetMap = Blob; 3457 break; 3458 3459 case SOURCE_MANAGER_LINE_TABLE: 3460 ParseLineTable(F, Record); 3461 break; 3462 3463 case SOURCE_LOCATION_PRELOADS: { 3464 // Need to transform from the local view (1-based IDs) to the global view, 3465 // which is based off F.SLocEntryBaseID. 3466 if (!F.PreloadSLocEntries.empty()) 3467 return llvm::createStringError( 3468 std::errc::illegal_byte_sequence, 3469 "Multiple SOURCE_LOCATION_PRELOADS records in AST file"); 3470 3471 F.PreloadSLocEntries.swap(Record); 3472 break; 3473 } 3474 3475 case EXT_VECTOR_DECLS: 3476 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3477 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I])); 3478 break; 3479 3480 case VTABLE_USES: 3481 if (Record.size() % 3 != 0) 3482 return llvm::createStringError(std::errc::illegal_byte_sequence, 3483 "Invalid VTABLE_USES record"); 3484 3485 // Later tables overwrite earlier ones. 3486 // FIXME: Modules will have some trouble with this. This is clearly not 3487 // the right way to do this. 3488 VTableUses.clear(); 3489 3490 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) { 3491 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++])); 3492 VTableUses.push_back( 3493 ReadSourceLocation(F, Record, Idx).getRawEncoding()); 3494 VTableUses.push_back(Record[Idx++]); 3495 } 3496 break; 3497 3498 case PENDING_IMPLICIT_INSTANTIATIONS: 3499 if (PendingInstantiations.size() % 2 != 0) 3500 return llvm::createStringError( 3501 std::errc::illegal_byte_sequence, 3502 "Invalid existing PendingInstantiations"); 3503 3504 if (Record.size() % 2 != 0) 3505 return llvm::createStringError( 3506 std::errc::illegal_byte_sequence, 3507 "Invalid PENDING_IMPLICIT_INSTANTIATIONS block"); 3508 3509 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3510 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++])); 3511 PendingInstantiations.push_back( 3512 ReadSourceLocation(F, Record, I).getRawEncoding()); 3513 } 3514 break; 3515 3516 case SEMA_DECL_REFS: 3517 if (Record.size() != 3) 3518 return llvm::createStringError(std::errc::illegal_byte_sequence, 3519 "Invalid SEMA_DECL_REFS block"); 3520 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3521 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3522 break; 3523 3524 case PPD_ENTITIES_OFFSETS: { 3525 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data(); 3526 assert(Blob.size() % sizeof(PPEntityOffset) == 0); 3527 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset); 3528 3529 unsigned LocalBasePreprocessedEntityID = Record[0]; 3530 3531 unsigned StartingID; 3532 if (!PP.getPreprocessingRecord()) 3533 PP.createPreprocessingRecord(); 3534 if (!PP.getPreprocessingRecord()->getExternalSource()) 3535 PP.getPreprocessingRecord()->SetExternalSource(*this); 3536 StartingID 3537 = PP.getPreprocessingRecord() 3538 ->allocateLoadedEntities(F.NumPreprocessedEntities); 3539 F.BasePreprocessedEntityID = StartingID; 3540 3541 if (F.NumPreprocessedEntities > 0) { 3542 // Introduce the global -> local mapping for preprocessed entities in 3543 // this module. 3544 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F)); 3545 3546 // Introduce the local -> global mapping for preprocessed entities in 3547 // this module. 3548 F.PreprocessedEntityRemap.insertOrReplace( 3549 std::make_pair(LocalBasePreprocessedEntityID, 3550 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID)); 3551 } 3552 3553 break; 3554 } 3555 3556 case PPD_SKIPPED_RANGES: { 3557 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data(); 3558 assert(Blob.size() % sizeof(PPSkippedRange) == 0); 3559 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange); 3560 3561 if (!PP.getPreprocessingRecord()) 3562 PP.createPreprocessingRecord(); 3563 if (!PP.getPreprocessingRecord()->getExternalSource()) 3564 PP.getPreprocessingRecord()->SetExternalSource(*this); 3565 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord() 3566 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges); 3567 3568 if (F.NumPreprocessedSkippedRanges > 0) 3569 GlobalSkippedRangeMap.insert( 3570 std::make_pair(F.BasePreprocessedSkippedRangeID, &F)); 3571 break; 3572 } 3573 3574 case DECL_UPDATE_OFFSETS: 3575 if (Record.size() % 2 != 0) 3576 return llvm::createStringError( 3577 std::errc::illegal_byte_sequence, 3578 "invalid DECL_UPDATE_OFFSETS block in AST file"); 3579 for (unsigned I = 0, N = Record.size(); I != N; I += 2) { 3580 GlobalDeclID ID = getGlobalDeclID(F, Record[I]); 3581 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1])); 3582 3583 // If we've already loaded the decl, perform the updates when we finish 3584 // loading this block. 3585 if (Decl *D = GetExistingDecl(ID)) 3586 PendingUpdateRecords.push_back( 3587 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3588 } 3589 break; 3590 3591 case OBJC_CATEGORIES_MAP: 3592 if (F.LocalNumObjCCategoriesInMap != 0) 3593 return llvm::createStringError( 3594 std::errc::illegal_byte_sequence, 3595 "duplicate OBJC_CATEGORIES_MAP record in AST file"); 3596 3597 F.LocalNumObjCCategoriesInMap = Record[0]; 3598 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data(); 3599 break; 3600 3601 case OBJC_CATEGORIES: 3602 F.ObjCCategories.swap(Record); 3603 break; 3604 3605 case CUDA_SPECIAL_DECL_REFS: 3606 // Later tables overwrite earlier ones. 3607 // FIXME: Modules will have trouble with this. 3608 CUDASpecialDeclRefs.clear(); 3609 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3610 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3611 break; 3612 3613 case HEADER_SEARCH_TABLE: 3614 F.HeaderFileInfoTableData = Blob.data(); 3615 F.LocalNumHeaderFileInfos = Record[1]; 3616 if (Record[0]) { 3617 F.HeaderFileInfoTable 3618 = HeaderFileInfoLookupTable::Create( 3619 (const unsigned char *)F.HeaderFileInfoTableData + Record[0], 3620 (const unsigned char *)F.HeaderFileInfoTableData, 3621 HeaderFileInfoTrait(*this, F, 3622 &PP.getHeaderSearchInfo(), 3623 Blob.data() + Record[2])); 3624 3625 PP.getHeaderSearchInfo().SetExternalSource(this); 3626 if (!PP.getHeaderSearchInfo().getExternalLookup()) 3627 PP.getHeaderSearchInfo().SetExternalLookup(this); 3628 } 3629 break; 3630 3631 case FP_PRAGMA_OPTIONS: 3632 // Later tables overwrite earlier ones. 3633 FPPragmaOptions.swap(Record); 3634 break; 3635 3636 case OPENCL_EXTENSIONS: 3637 for (unsigned I = 0, E = Record.size(); I != E; ) { 3638 auto Name = ReadString(Record, I); 3639 auto &OptInfo = OpenCLExtensions.OptMap[Name]; 3640 OptInfo.Supported = Record[I++] != 0; 3641 OptInfo.Enabled = Record[I++] != 0; 3642 OptInfo.WithPragma = Record[I++] != 0; 3643 OptInfo.Avail = Record[I++]; 3644 OptInfo.Core = Record[I++]; 3645 OptInfo.Opt = Record[I++]; 3646 } 3647 break; 3648 3649 case TENTATIVE_DEFINITIONS: 3650 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3651 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I])); 3652 break; 3653 3654 case KNOWN_NAMESPACES: 3655 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3656 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I])); 3657 break; 3658 3659 case UNDEFINED_BUT_USED: 3660 if (UndefinedButUsed.size() % 2 != 0) 3661 return llvm::createStringError(std::errc::illegal_byte_sequence, 3662 "Invalid existing UndefinedButUsed"); 3663 3664 if (Record.size() % 2 != 0) 3665 return llvm::createStringError(std::errc::illegal_byte_sequence, 3666 "invalid undefined-but-used record"); 3667 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3668 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++])); 3669 UndefinedButUsed.push_back( 3670 ReadSourceLocation(F, Record, I).getRawEncoding()); 3671 } 3672 break; 3673 3674 case DELETE_EXPRS_TO_ANALYZE: 3675 for (unsigned I = 0, N = Record.size(); I != N;) { 3676 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++])); 3677 const uint64_t Count = Record[I++]; 3678 DelayedDeleteExprs.push_back(Count); 3679 for (uint64_t C = 0; C < Count; ++C) { 3680 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding()); 3681 bool IsArrayForm = Record[I++] == 1; 3682 DelayedDeleteExprs.push_back(IsArrayForm); 3683 } 3684 } 3685 break; 3686 3687 case IMPORTED_MODULES: 3688 if (!F.isModule()) { 3689 // If we aren't loading a module (which has its own exports), make 3690 // all of the imported modules visible. 3691 // FIXME: Deal with macros-only imports. 3692 for (unsigned I = 0, N = Record.size(); I != N; /**/) { 3693 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]); 3694 SourceLocation Loc = ReadSourceLocation(F, Record, I); 3695 if (GlobalID) { 3696 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc)); 3697 if (DeserializationListener) 3698 DeserializationListener->ModuleImportRead(GlobalID, Loc); 3699 } 3700 } 3701 } 3702 break; 3703 3704 case MACRO_OFFSET: { 3705 if (F.LocalNumMacros != 0) 3706 return llvm::createStringError( 3707 std::errc::illegal_byte_sequence, 3708 "duplicate MACRO_OFFSET record in AST file"); 3709 F.MacroOffsets = (const uint32_t *)Blob.data(); 3710 F.LocalNumMacros = Record[0]; 3711 unsigned LocalBaseMacroID = Record[1]; 3712 F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset; 3713 F.BaseMacroID = getTotalNumMacros(); 3714 3715 if (F.LocalNumMacros > 0) { 3716 // Introduce the global -> local mapping for macros within this module. 3717 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F)); 3718 3719 // Introduce the local -> global mapping for macros within this module. 3720 F.MacroRemap.insertOrReplace( 3721 std::make_pair(LocalBaseMacroID, 3722 F.BaseMacroID - LocalBaseMacroID)); 3723 3724 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros); 3725 } 3726 break; 3727 } 3728 3729 case PP_INCLUDED_FILES: 3730 readIncludedFiles(F, Blob, PP); 3731 break; 3732 3733 case LATE_PARSED_TEMPLATE: 3734 LateParsedTemplates.emplace_back( 3735 std::piecewise_construct, std::forward_as_tuple(&F), 3736 std::forward_as_tuple(Record.begin(), Record.end())); 3737 break; 3738 3739 case OPTIMIZE_PRAGMA_OPTIONS: 3740 if (Record.size() != 1) 3741 return llvm::createStringError(std::errc::illegal_byte_sequence, 3742 "invalid pragma optimize record"); 3743 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]); 3744 break; 3745 3746 case MSSTRUCT_PRAGMA_OPTIONS: 3747 if (Record.size() != 1) 3748 return llvm::createStringError(std::errc::illegal_byte_sequence, 3749 "invalid pragma ms_struct record"); 3750 PragmaMSStructState = Record[0]; 3751 break; 3752 3753 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS: 3754 if (Record.size() != 2) 3755 return llvm::createStringError( 3756 std::errc::illegal_byte_sequence, 3757 "invalid pragma pointers to members record"); 3758 PragmaMSPointersToMembersState = Record[0]; 3759 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]); 3760 break; 3761 3762 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES: 3763 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3764 UnusedLocalTypedefNameCandidates.push_back( 3765 getGlobalDeclID(F, Record[I])); 3766 break; 3767 3768 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH: 3769 if (Record.size() != 1) 3770 return llvm::createStringError(std::errc::illegal_byte_sequence, 3771 "invalid cuda pragma options record"); 3772 ForceCUDAHostDeviceDepth = Record[0]; 3773 break; 3774 3775 case ALIGN_PACK_PRAGMA_OPTIONS: { 3776 if (Record.size() < 3) 3777 return llvm::createStringError(std::errc::illegal_byte_sequence, 3778 "invalid pragma pack record"); 3779 PragmaAlignPackCurrentValue = ReadAlignPackInfo(Record[0]); 3780 PragmaAlignPackCurrentLocation = ReadSourceLocation(F, Record[1]); 3781 unsigned NumStackEntries = Record[2]; 3782 unsigned Idx = 3; 3783 // Reset the stack when importing a new module. 3784 PragmaAlignPackStack.clear(); 3785 for (unsigned I = 0; I < NumStackEntries; ++I) { 3786 PragmaAlignPackStackEntry Entry; 3787 Entry.Value = ReadAlignPackInfo(Record[Idx++]); 3788 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3789 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3790 PragmaAlignPackStrings.push_back(ReadString(Record, Idx)); 3791 Entry.SlotLabel = PragmaAlignPackStrings.back(); 3792 PragmaAlignPackStack.push_back(Entry); 3793 } 3794 break; 3795 } 3796 3797 case FLOAT_CONTROL_PRAGMA_OPTIONS: { 3798 if (Record.size() < 3) 3799 return llvm::createStringError(std::errc::illegal_byte_sequence, 3800 "invalid pragma float control record"); 3801 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]); 3802 FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]); 3803 unsigned NumStackEntries = Record[2]; 3804 unsigned Idx = 3; 3805 // Reset the stack when importing a new module. 3806 FpPragmaStack.clear(); 3807 for (unsigned I = 0; I < NumStackEntries; ++I) { 3808 FpPragmaStackEntry Entry; 3809 Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]); 3810 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3811 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3812 FpPragmaStrings.push_back(ReadString(Record, Idx)); 3813 Entry.SlotLabel = FpPragmaStrings.back(); 3814 FpPragmaStack.push_back(Entry); 3815 } 3816 break; 3817 } 3818 3819 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS: 3820 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3821 DeclsToCheckForDeferredDiags.insert(getGlobalDeclID(F, Record[I])); 3822 break; 3823 } 3824 } 3825 } 3826 3827 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const { 3828 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read"); 3829 3830 // Additional remapping information. 3831 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data(); 3832 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size(); 3833 F.ModuleOffsetMap = StringRef(); 3834 3835 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders. 3836 if (F.SLocRemap.find(0) == F.SLocRemap.end()) { 3837 F.SLocRemap.insert(std::make_pair(0U, 0)); 3838 F.SLocRemap.insert(std::make_pair(2U, 1)); 3839 } 3840 3841 // Continuous range maps we may be updating in our module. 3842 using SLocRemapBuilder = 3843 ContinuousRangeMap<SourceLocation::UIntTy, SourceLocation::IntTy, 3844 2>::Builder; 3845 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder; 3846 SLocRemapBuilder SLocRemap(F.SLocRemap); 3847 RemapBuilder IdentifierRemap(F.IdentifierRemap); 3848 RemapBuilder MacroRemap(F.MacroRemap); 3849 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap); 3850 RemapBuilder SubmoduleRemap(F.SubmoduleRemap); 3851 RemapBuilder SelectorRemap(F.SelectorRemap); 3852 RemapBuilder DeclRemap(F.DeclRemap); 3853 RemapBuilder TypeRemap(F.TypeRemap); 3854 3855 while (Data < DataEnd) { 3856 // FIXME: Looking up dependency modules by filename is horrible. Let's 3857 // start fixing this with prebuilt, explicit and implicit modules and see 3858 // how it goes... 3859 using namespace llvm::support; 3860 ModuleKind Kind = static_cast<ModuleKind>( 3861 endian::readNext<uint8_t, little, unaligned>(Data)); 3862 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data); 3863 StringRef Name = StringRef((const char*)Data, Len); 3864 Data += Len; 3865 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule || 3866 Kind == MK_ImplicitModule 3867 ? ModuleMgr.lookupByModuleName(Name) 3868 : ModuleMgr.lookupByFileName(Name)); 3869 if (!OM) { 3870 std::string Msg = 3871 "SourceLocation remap refers to unknown module, cannot find "; 3872 Msg.append(std::string(Name)); 3873 Error(Msg); 3874 return; 3875 } 3876 3877 SourceLocation::UIntTy SLocOffset = 3878 endian::readNext<uint32_t, little, unaligned>(Data); 3879 uint32_t IdentifierIDOffset = 3880 endian::readNext<uint32_t, little, unaligned>(Data); 3881 uint32_t MacroIDOffset = 3882 endian::readNext<uint32_t, little, unaligned>(Data); 3883 uint32_t PreprocessedEntityIDOffset = 3884 endian::readNext<uint32_t, little, unaligned>(Data); 3885 uint32_t SubmoduleIDOffset = 3886 endian::readNext<uint32_t, little, unaligned>(Data); 3887 uint32_t SelectorIDOffset = 3888 endian::readNext<uint32_t, little, unaligned>(Data); 3889 uint32_t DeclIDOffset = 3890 endian::readNext<uint32_t, little, unaligned>(Data); 3891 uint32_t TypeIndexOffset = 3892 endian::readNext<uint32_t, little, unaligned>(Data); 3893 3894 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset, 3895 RemapBuilder &Remap) { 3896 constexpr uint32_t None = std::numeric_limits<uint32_t>::max(); 3897 if (Offset != None) 3898 Remap.insert(std::make_pair(Offset, 3899 static_cast<int>(BaseOffset - Offset))); 3900 }; 3901 3902 constexpr SourceLocation::UIntTy SLocNone = 3903 std::numeric_limits<SourceLocation::UIntTy>::max(); 3904 if (SLocOffset != SLocNone) 3905 SLocRemap.insert(std::make_pair( 3906 SLocOffset, static_cast<SourceLocation::IntTy>( 3907 OM->SLocEntryBaseOffset - SLocOffset))); 3908 3909 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap); 3910 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap); 3911 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID, 3912 PreprocessedEntityRemap); 3913 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap); 3914 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap); 3915 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap); 3916 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap); 3917 3918 // Global -> local mappings. 3919 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset; 3920 } 3921 } 3922 3923 ASTReader::ASTReadResult 3924 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F, 3925 const ModuleFile *ImportedBy, 3926 unsigned ClientLoadCapabilities) { 3927 unsigned Idx = 0; 3928 F.ModuleMapPath = ReadPath(F, Record, Idx); 3929 3930 // Try to resolve ModuleName in the current header search context and 3931 // verify that it is found in the same module map file as we saved. If the 3932 // top-level AST file is a main file, skip this check because there is no 3933 // usable header search context. 3934 assert(!F.ModuleName.empty() && 3935 "MODULE_NAME should come before MODULE_MAP_FILE"); 3936 if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) { 3937 // An implicitly-loaded module file should have its module listed in some 3938 // module map file that we've already loaded. 3939 Module *M = 3940 PP.getHeaderSearchInfo().lookupModule(F.ModuleName, F.ImportLoc); 3941 auto &Map = PP.getHeaderSearchInfo().getModuleMap(); 3942 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr; 3943 // Don't emit module relocation error if we have -fno-validate-pch 3944 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation & 3945 DisableValidationForModuleKind::Module) && 3946 !ModMap) { 3947 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) { 3948 if (auto ASTFE = M ? M->getASTFile() : None) { 3949 // This module was defined by an imported (explicit) module. 3950 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName 3951 << ASTFE->getName(); 3952 } else { 3953 // This module was built with a different module map. 3954 Diag(diag::err_imported_module_not_found) 3955 << F.ModuleName << F.FileName 3956 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath 3957 << !ImportedBy; 3958 // In case it was imported by a PCH, there's a chance the user is 3959 // just missing to include the search path to the directory containing 3960 // the modulemap. 3961 if (ImportedBy && ImportedBy->Kind == MK_PCH) 3962 Diag(diag::note_imported_by_pch_module_not_found) 3963 << llvm::sys::path::parent_path(F.ModuleMapPath); 3964 } 3965 } 3966 return OutOfDate; 3967 } 3968 3969 assert(M && M->Name == F.ModuleName && "found module with different name"); 3970 3971 // Check the primary module map file. 3972 auto StoredModMap = FileMgr.getFile(F.ModuleMapPath); 3973 if (!StoredModMap || *StoredModMap != ModMap) { 3974 assert(ModMap && "found module is missing module map file"); 3975 assert((ImportedBy || F.Kind == MK_ImplicitModule) && 3976 "top-level import should be verified"); 3977 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy; 3978 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 3979 Diag(diag::err_imported_module_modmap_changed) 3980 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName) 3981 << ModMap->getName() << F.ModuleMapPath << NotImported; 3982 return OutOfDate; 3983 } 3984 3985 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps; 3986 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) { 3987 // FIXME: we should use input files rather than storing names. 3988 std::string Filename = ReadPath(F, Record, Idx); 3989 auto SF = FileMgr.getFile(Filename, false, false); 3990 if (!SF) { 3991 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 3992 Error("could not find file '" + Filename +"' referenced by AST file"); 3993 return OutOfDate; 3994 } 3995 AdditionalStoredMaps.insert(*SF); 3996 } 3997 3998 // Check any additional module map files (e.g. module.private.modulemap) 3999 // that are not in the pcm. 4000 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) { 4001 for (const FileEntry *ModMap : *AdditionalModuleMaps) { 4002 // Remove files that match 4003 // Note: SmallPtrSet::erase is really remove 4004 if (!AdditionalStoredMaps.erase(ModMap)) { 4005 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 4006 Diag(diag::err_module_different_modmap) 4007 << F.ModuleName << /*new*/0 << ModMap->getName(); 4008 return OutOfDate; 4009 } 4010 } 4011 } 4012 4013 // Check any additional module map files that are in the pcm, but not 4014 // found in header search. Cases that match are already removed. 4015 for (const FileEntry *ModMap : AdditionalStoredMaps) { 4016 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 4017 Diag(diag::err_module_different_modmap) 4018 << F.ModuleName << /*not new*/1 << ModMap->getName(); 4019 return OutOfDate; 4020 } 4021 } 4022 4023 if (Listener) 4024 Listener->ReadModuleMapFile(F.ModuleMapPath); 4025 return Success; 4026 } 4027 4028 /// Move the given method to the back of the global list of methods. 4029 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) { 4030 // Find the entry for this selector in the method pool. 4031 Sema::GlobalMethodPool::iterator Known 4032 = S.MethodPool.find(Method->getSelector()); 4033 if (Known == S.MethodPool.end()) 4034 return; 4035 4036 // Retrieve the appropriate method list. 4037 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first 4038 : Known->second.second; 4039 bool Found = false; 4040 for (ObjCMethodList *List = &Start; List; List = List->getNext()) { 4041 if (!Found) { 4042 if (List->getMethod() == Method) { 4043 Found = true; 4044 } else { 4045 // Keep searching. 4046 continue; 4047 } 4048 } 4049 4050 if (List->getNext()) 4051 List->setMethod(List->getNext()->getMethod()); 4052 else 4053 List->setMethod(Method); 4054 } 4055 } 4056 4057 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) { 4058 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?"); 4059 for (Decl *D : Names) { 4060 bool wasHidden = !D->isUnconditionallyVisible(); 4061 D->setVisibleDespiteOwningModule(); 4062 4063 if (wasHidden && SemaObj) { 4064 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) { 4065 moveMethodToBackOfGlobalList(*SemaObj, Method); 4066 } 4067 } 4068 } 4069 } 4070 4071 void ASTReader::makeModuleVisible(Module *Mod, 4072 Module::NameVisibilityKind NameVisibility, 4073 SourceLocation ImportLoc) { 4074 llvm::SmallPtrSet<Module *, 4> Visited; 4075 SmallVector<Module *, 4> Stack; 4076 Stack.push_back(Mod); 4077 while (!Stack.empty()) { 4078 Mod = Stack.pop_back_val(); 4079 4080 if (NameVisibility <= Mod->NameVisibility) { 4081 // This module already has this level of visibility (or greater), so 4082 // there is nothing more to do. 4083 continue; 4084 } 4085 4086 if (Mod->isUnimportable()) { 4087 // Modules that aren't importable cannot be made visible. 4088 continue; 4089 } 4090 4091 // Update the module's name visibility. 4092 Mod->NameVisibility = NameVisibility; 4093 4094 // If we've already deserialized any names from this module, 4095 // mark them as visible. 4096 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod); 4097 if (Hidden != HiddenNamesMap.end()) { 4098 auto HiddenNames = std::move(*Hidden); 4099 HiddenNamesMap.erase(Hidden); 4100 makeNamesVisible(HiddenNames.second, HiddenNames.first); 4101 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() && 4102 "making names visible added hidden names"); 4103 } 4104 4105 // Push any exported modules onto the stack to be marked as visible. 4106 SmallVector<Module *, 16> Exports; 4107 Mod->getExportedModules(Exports); 4108 for (SmallVectorImpl<Module *>::iterator 4109 I = Exports.begin(), E = Exports.end(); I != E; ++I) { 4110 Module *Exported = *I; 4111 if (Visited.insert(Exported).second) 4112 Stack.push_back(Exported); 4113 } 4114 } 4115 } 4116 4117 /// We've merged the definition \p MergedDef into the existing definition 4118 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made 4119 /// visible. 4120 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def, 4121 NamedDecl *MergedDef) { 4122 if (!Def->isUnconditionallyVisible()) { 4123 // If MergedDef is visible or becomes visible, make the definition visible. 4124 if (MergedDef->isUnconditionallyVisible()) 4125 Def->setVisibleDespiteOwningModule(); 4126 else { 4127 getContext().mergeDefinitionIntoModule( 4128 Def, MergedDef->getImportedOwningModule(), 4129 /*NotifyListeners*/ false); 4130 PendingMergedDefinitionsToDeduplicate.insert(Def); 4131 } 4132 } 4133 } 4134 4135 bool ASTReader::loadGlobalIndex() { 4136 if (GlobalIndex) 4137 return false; 4138 4139 if (TriedLoadingGlobalIndex || !UseGlobalIndex || 4140 !PP.getLangOpts().Modules) 4141 return true; 4142 4143 // Try to load the global index. 4144 TriedLoadingGlobalIndex = true; 4145 StringRef ModuleCachePath 4146 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath(); 4147 std::pair<GlobalModuleIndex *, llvm::Error> Result = 4148 GlobalModuleIndex::readIndex(ModuleCachePath); 4149 if (llvm::Error Err = std::move(Result.second)) { 4150 assert(!Result.first); 4151 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 4152 return true; 4153 } 4154 4155 GlobalIndex.reset(Result.first); 4156 ModuleMgr.setGlobalIndex(GlobalIndex.get()); 4157 return false; 4158 } 4159 4160 bool ASTReader::isGlobalIndexUnavailable() const { 4161 return PP.getLangOpts().Modules && UseGlobalIndex && 4162 !hasGlobalIndex() && TriedLoadingGlobalIndex; 4163 } 4164 4165 static void updateModuleTimestamp(ModuleFile &MF) { 4166 // Overwrite the timestamp file contents so that file's mtime changes. 4167 std::string TimestampFilename = MF.getTimestampFilename(); 4168 std::error_code EC; 4169 llvm::raw_fd_ostream OS(TimestampFilename, EC, 4170 llvm::sys::fs::OF_TextWithCRLF); 4171 if (EC) 4172 return; 4173 OS << "Timestamp file\n"; 4174 OS.close(); 4175 OS.clear_error(); // Avoid triggering a fatal error. 4176 } 4177 4178 /// Given a cursor at the start of an AST file, scan ahead and drop the 4179 /// cursor into the start of the given block ID, returning false on success and 4180 /// true on failure. 4181 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) { 4182 while (true) { 4183 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 4184 if (!MaybeEntry) { 4185 // FIXME this drops errors on the floor. 4186 consumeError(MaybeEntry.takeError()); 4187 return true; 4188 } 4189 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4190 4191 switch (Entry.Kind) { 4192 case llvm::BitstreamEntry::Error: 4193 case llvm::BitstreamEntry::EndBlock: 4194 return true; 4195 4196 case llvm::BitstreamEntry::Record: 4197 // Ignore top-level records. 4198 if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID)) 4199 break; 4200 else { 4201 // FIXME this drops errors on the floor. 4202 consumeError(Skipped.takeError()); 4203 return true; 4204 } 4205 4206 case llvm::BitstreamEntry::SubBlock: 4207 if (Entry.ID == BlockID) { 4208 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) { 4209 // FIXME this drops the error on the floor. 4210 consumeError(std::move(Err)); 4211 return true; 4212 } 4213 // Found it! 4214 return false; 4215 } 4216 4217 if (llvm::Error Err = Cursor.SkipBlock()) { 4218 // FIXME this drops the error on the floor. 4219 consumeError(std::move(Err)); 4220 return true; 4221 } 4222 } 4223 } 4224 } 4225 4226 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, 4227 ModuleKind Type, 4228 SourceLocation ImportLoc, 4229 unsigned ClientLoadCapabilities, 4230 SmallVectorImpl<ImportedSubmodule> *Imported) { 4231 llvm::SaveAndRestore<SourceLocation> 4232 SetCurImportLocRAII(CurrentImportLoc, ImportLoc); 4233 llvm::SaveAndRestore<Optional<ModuleKind>> SetCurModuleKindRAII( 4234 CurrentDeserializingModuleKind, Type); 4235 4236 // Defer any pending actions until we get to the end of reading the AST file. 4237 Deserializing AnASTFile(this); 4238 4239 // Bump the generation number. 4240 unsigned PreviousGeneration = 0; 4241 if (ContextObj) 4242 PreviousGeneration = incrementGeneration(*ContextObj); 4243 4244 unsigned NumModules = ModuleMgr.size(); 4245 SmallVector<ImportedModule, 4> Loaded; 4246 if (ASTReadResult ReadResult = 4247 ReadASTCore(FileName, Type, ImportLoc, 4248 /*ImportedBy=*/nullptr, Loaded, 0, 0, ASTFileSignature(), 4249 ClientLoadCapabilities)) { 4250 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, 4251 PP.getLangOpts().Modules 4252 ? &PP.getHeaderSearchInfo().getModuleMap() 4253 : nullptr); 4254 4255 // If we find that any modules are unusable, the global index is going 4256 // to be out-of-date. Just remove it. 4257 GlobalIndex.reset(); 4258 ModuleMgr.setGlobalIndex(nullptr); 4259 return ReadResult; 4260 } 4261 4262 // Here comes stuff that we only do once the entire chain is loaded. Do *not* 4263 // remove modules from this point. Various fields are updated during reading 4264 // the AST block and removing the modules would result in dangling pointers. 4265 // They are generally only incidentally dereferenced, ie. a binary search 4266 // runs over `GlobalSLocEntryMap`, which could cause an invalid module to 4267 // be dereferenced but it wouldn't actually be used. 4268 4269 // Load the AST blocks of all of the modules that we loaded. We can still 4270 // hit errors parsing the ASTs at this point. 4271 for (ImportedModule &M : Loaded) { 4272 ModuleFile &F = *M.Mod; 4273 4274 // Read the AST block. 4275 if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) { 4276 Error(std::move(Err)); 4277 return Failure; 4278 } 4279 4280 // The AST block should always have a definition for the main module. 4281 if (F.isModule() && !F.DidReadTopLevelSubmodule) { 4282 Error(diag::err_module_file_missing_top_level_submodule, F.FileName); 4283 return Failure; 4284 } 4285 4286 // Read the extension blocks. 4287 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) { 4288 if (llvm::Error Err = ReadExtensionBlock(F)) { 4289 Error(std::move(Err)); 4290 return Failure; 4291 } 4292 } 4293 4294 // Once read, set the ModuleFile bit base offset and update the size in 4295 // bits of all files we've seen. 4296 F.GlobalBitOffset = TotalModulesSizeInBits; 4297 TotalModulesSizeInBits += F.SizeInBits; 4298 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F)); 4299 } 4300 4301 // Preload source locations and interesting indentifiers. 4302 for (ImportedModule &M : Loaded) { 4303 ModuleFile &F = *M.Mod; 4304 4305 // Preload SLocEntries. 4306 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) { 4307 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID; 4308 // Load it through the SourceManager and don't call ReadSLocEntry() 4309 // directly because the entry may have already been loaded in which case 4310 // calling ReadSLocEntry() directly would trigger an assertion in 4311 // SourceManager. 4312 SourceMgr.getLoadedSLocEntryByID(Index); 4313 } 4314 4315 // Map the original source file ID into the ID space of the current 4316 // compilation. 4317 if (F.OriginalSourceFileID.isValid()) { 4318 F.OriginalSourceFileID = FileID::get( 4319 F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1); 4320 } 4321 4322 // Preload all the pending interesting identifiers by marking them out of 4323 // date. 4324 for (auto Offset : F.PreloadIdentifierOffsets) { 4325 const unsigned char *Data = F.IdentifierTableData + Offset; 4326 4327 ASTIdentifierLookupTrait Trait(*this, F); 4328 auto KeyDataLen = Trait.ReadKeyDataLength(Data); 4329 auto Key = Trait.ReadKey(Data, KeyDataLen.first); 4330 auto &II = PP.getIdentifierTable().getOwn(Key); 4331 II.setOutOfDate(true); 4332 4333 // Mark this identifier as being from an AST file so that we can track 4334 // whether we need to serialize it. 4335 markIdentifierFromAST(*this, II); 4336 4337 // Associate the ID with the identifier so that the writer can reuse it. 4338 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first); 4339 SetIdentifierInfo(ID, &II); 4340 } 4341 } 4342 4343 // Setup the import locations and notify the module manager that we've 4344 // committed to these module files. 4345 for (ImportedModule &M : Loaded) { 4346 ModuleFile &F = *M.Mod; 4347 4348 ModuleMgr.moduleFileAccepted(&F); 4349 4350 // Set the import location. 4351 F.DirectImportLoc = ImportLoc; 4352 // FIXME: We assume that locations from PCH / preamble do not need 4353 // any translation. 4354 if (!M.ImportedBy) 4355 F.ImportLoc = M.ImportLoc; 4356 else 4357 F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc); 4358 } 4359 4360 if (!PP.getLangOpts().CPlusPlus || 4361 (Type != MK_ImplicitModule && Type != MK_ExplicitModule && 4362 Type != MK_PrebuiltModule)) { 4363 // Mark all of the identifiers in the identifier table as being out of date, 4364 // so that various accessors know to check the loaded modules when the 4365 // identifier is used. 4366 // 4367 // For C++ modules, we don't need information on many identifiers (just 4368 // those that provide macros or are poisoned), so we mark all of 4369 // the interesting ones via PreloadIdentifierOffsets. 4370 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(), 4371 IdEnd = PP.getIdentifierTable().end(); 4372 Id != IdEnd; ++Id) 4373 Id->second->setOutOfDate(true); 4374 } 4375 // Mark selectors as out of date. 4376 for (auto Sel : SelectorGeneration) 4377 SelectorOutOfDate[Sel.first] = true; 4378 4379 // Resolve any unresolved module exports. 4380 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) { 4381 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I]; 4382 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID); 4383 Module *ResolvedMod = getSubmodule(GlobalID); 4384 4385 switch (Unresolved.Kind) { 4386 case UnresolvedModuleRef::Conflict: 4387 if (ResolvedMod) { 4388 Module::Conflict Conflict; 4389 Conflict.Other = ResolvedMod; 4390 Conflict.Message = Unresolved.String.str(); 4391 Unresolved.Mod->Conflicts.push_back(Conflict); 4392 } 4393 continue; 4394 4395 case UnresolvedModuleRef::Import: 4396 if (ResolvedMod) 4397 Unresolved.Mod->Imports.insert(ResolvedMod); 4398 continue; 4399 4400 case UnresolvedModuleRef::Export: 4401 if (ResolvedMod || Unresolved.IsWildcard) 4402 Unresolved.Mod->Exports.push_back( 4403 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard)); 4404 continue; 4405 } 4406 } 4407 UnresolvedModuleRefs.clear(); 4408 4409 if (Imported) 4410 Imported->append(ImportedModules.begin(), 4411 ImportedModules.end()); 4412 4413 // FIXME: How do we load the 'use'd modules? They may not be submodules. 4414 // Might be unnecessary as use declarations are only used to build the 4415 // module itself. 4416 4417 if (ContextObj) 4418 InitializeContext(); 4419 4420 if (SemaObj) 4421 UpdateSema(); 4422 4423 if (DeserializationListener) 4424 DeserializationListener->ReaderInitialized(this); 4425 4426 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule(); 4427 if (PrimaryModule.OriginalSourceFileID.isValid()) { 4428 // If this AST file is a precompiled preamble, then set the 4429 // preamble file ID of the source manager to the file source file 4430 // from which the preamble was built. 4431 if (Type == MK_Preamble) { 4432 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID); 4433 } else if (Type == MK_MainFile) { 4434 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID); 4435 } 4436 } 4437 4438 // For any Objective-C class definitions we have already loaded, make sure 4439 // that we load any additional categories. 4440 if (ContextObj) { 4441 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) { 4442 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(), 4443 ObjCClassesLoaded[I], 4444 PreviousGeneration); 4445 } 4446 } 4447 4448 if (PP.getHeaderSearchInfo() 4449 .getHeaderSearchOpts() 4450 .ModulesValidateOncePerBuildSession) { 4451 // Now we are certain that the module and all modules it depends on are 4452 // up to date. Create or update timestamp files for modules that are 4453 // located in the module cache (not for PCH files that could be anywhere 4454 // in the filesystem). 4455 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) { 4456 ImportedModule &M = Loaded[I]; 4457 if (M.Mod->Kind == MK_ImplicitModule) { 4458 updateModuleTimestamp(*M.Mod); 4459 } 4460 } 4461 } 4462 4463 return Success; 4464 } 4465 4466 static ASTFileSignature readASTFileSignature(StringRef PCH); 4467 4468 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'. 4469 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) { 4470 // FIXME checking magic headers is done in other places such as 4471 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't 4472 // always done the same. Unify it all with a helper. 4473 if (!Stream.canSkipToPos(4)) 4474 return llvm::createStringError(std::errc::illegal_byte_sequence, 4475 "file too small to contain AST file magic"); 4476 for (unsigned C : {'C', 'P', 'C', 'H'}) 4477 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) { 4478 if (Res.get() != C) 4479 return llvm::createStringError( 4480 std::errc::illegal_byte_sequence, 4481 "file doesn't start with AST file magic"); 4482 } else 4483 return Res.takeError(); 4484 return llvm::Error::success(); 4485 } 4486 4487 static unsigned moduleKindForDiagnostic(ModuleKind Kind) { 4488 switch (Kind) { 4489 case MK_PCH: 4490 return 0; // PCH 4491 case MK_ImplicitModule: 4492 case MK_ExplicitModule: 4493 case MK_PrebuiltModule: 4494 return 1; // module 4495 case MK_MainFile: 4496 case MK_Preamble: 4497 return 2; // main source file 4498 } 4499 llvm_unreachable("unknown module kind"); 4500 } 4501 4502 ASTReader::ASTReadResult 4503 ASTReader::ReadASTCore(StringRef FileName, 4504 ModuleKind Type, 4505 SourceLocation ImportLoc, 4506 ModuleFile *ImportedBy, 4507 SmallVectorImpl<ImportedModule> &Loaded, 4508 off_t ExpectedSize, time_t ExpectedModTime, 4509 ASTFileSignature ExpectedSignature, 4510 unsigned ClientLoadCapabilities) { 4511 ModuleFile *M; 4512 std::string ErrorStr; 4513 ModuleManager::AddModuleResult AddResult 4514 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy, 4515 getGeneration(), ExpectedSize, ExpectedModTime, 4516 ExpectedSignature, readASTFileSignature, 4517 M, ErrorStr); 4518 4519 switch (AddResult) { 4520 case ModuleManager::AlreadyLoaded: 4521 Diag(diag::remark_module_import) 4522 << M->ModuleName << M->FileName << (ImportedBy ? true : false) 4523 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 4524 return Success; 4525 4526 case ModuleManager::NewlyLoaded: 4527 // Load module file below. 4528 break; 4529 4530 case ModuleManager::Missing: 4531 // The module file was missing; if the client can handle that, return 4532 // it. 4533 if (ClientLoadCapabilities & ARR_Missing) 4534 return Missing; 4535 4536 // Otherwise, return an error. 4537 Diag(diag::err_ast_file_not_found) 4538 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty() 4539 << ErrorStr; 4540 return Failure; 4541 4542 case ModuleManager::OutOfDate: 4543 // We couldn't load the module file because it is out-of-date. If the 4544 // client can handle out-of-date, return it. 4545 if (ClientLoadCapabilities & ARR_OutOfDate) 4546 return OutOfDate; 4547 4548 // Otherwise, return an error. 4549 Diag(diag::err_ast_file_out_of_date) 4550 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty() 4551 << ErrorStr; 4552 return Failure; 4553 } 4554 4555 assert(M && "Missing module file"); 4556 4557 bool ShouldFinalizePCM = false; 4558 auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() { 4559 auto &MC = getModuleManager().getModuleCache(); 4560 if (ShouldFinalizePCM) 4561 MC.finalizePCM(FileName); 4562 else 4563 MC.tryToDropPCM(FileName); 4564 }); 4565 ModuleFile &F = *M; 4566 BitstreamCursor &Stream = F.Stream; 4567 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer)); 4568 F.SizeInBits = F.Buffer->getBufferSize() * 8; 4569 4570 // Sniff for the signature. 4571 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4572 Diag(diag::err_ast_file_invalid) 4573 << moduleKindForDiagnostic(Type) << FileName << std::move(Err); 4574 return Failure; 4575 } 4576 4577 // This is used for compatibility with older PCH formats. 4578 bool HaveReadControlBlock = false; 4579 while (true) { 4580 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4581 if (!MaybeEntry) { 4582 Error(MaybeEntry.takeError()); 4583 return Failure; 4584 } 4585 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4586 4587 switch (Entry.Kind) { 4588 case llvm::BitstreamEntry::Error: 4589 case llvm::BitstreamEntry::Record: 4590 case llvm::BitstreamEntry::EndBlock: 4591 Error("invalid record at top-level of AST file"); 4592 return Failure; 4593 4594 case llvm::BitstreamEntry::SubBlock: 4595 break; 4596 } 4597 4598 switch (Entry.ID) { 4599 case CONTROL_BLOCK_ID: 4600 HaveReadControlBlock = true; 4601 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) { 4602 case Success: 4603 // Check that we didn't try to load a non-module AST file as a module. 4604 // 4605 // FIXME: Should we also perform the converse check? Loading a module as 4606 // a PCH file sort of works, but it's a bit wonky. 4607 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule || 4608 Type == MK_PrebuiltModule) && 4609 F.ModuleName.empty()) { 4610 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure; 4611 if (Result != OutOfDate || 4612 (ClientLoadCapabilities & ARR_OutOfDate) == 0) 4613 Diag(diag::err_module_file_not_module) << FileName; 4614 return Result; 4615 } 4616 break; 4617 4618 case Failure: return Failure; 4619 case Missing: return Missing; 4620 case OutOfDate: return OutOfDate; 4621 case VersionMismatch: return VersionMismatch; 4622 case ConfigurationMismatch: return ConfigurationMismatch; 4623 case HadErrors: return HadErrors; 4624 } 4625 break; 4626 4627 case AST_BLOCK_ID: 4628 if (!HaveReadControlBlock) { 4629 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 4630 Diag(diag::err_pch_version_too_old); 4631 return VersionMismatch; 4632 } 4633 4634 // Record that we've loaded this module. 4635 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc)); 4636 ShouldFinalizePCM = true; 4637 return Success; 4638 4639 case UNHASHED_CONTROL_BLOCK_ID: 4640 // This block is handled using look-ahead during ReadControlBlock. We 4641 // shouldn't get here! 4642 Error("malformed block record in AST file"); 4643 return Failure; 4644 4645 default: 4646 if (llvm::Error Err = Stream.SkipBlock()) { 4647 Error(std::move(Err)); 4648 return Failure; 4649 } 4650 break; 4651 } 4652 } 4653 4654 llvm_unreachable("unexpected break; expected return"); 4655 } 4656 4657 ASTReader::ASTReadResult 4658 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy, 4659 unsigned ClientLoadCapabilities) { 4660 const HeaderSearchOptions &HSOpts = 4661 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 4662 bool AllowCompatibleConfigurationMismatch = 4663 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 4664 bool DisableValidation = shouldDisableValidationForFile(F); 4665 4666 ASTReadResult Result = readUnhashedControlBlockImpl( 4667 &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch, 4668 Listener.get(), 4669 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions); 4670 4671 // If F was directly imported by another module, it's implicitly validated by 4672 // the importing module. 4673 if (DisableValidation || WasImportedBy || 4674 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 4675 return Success; 4676 4677 if (Result == Failure) { 4678 Error("malformed block record in AST file"); 4679 return Failure; 4680 } 4681 4682 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) { 4683 // If this module has already been finalized in the ModuleCache, we're stuck 4684 // with it; we can only load a single version of each module. 4685 // 4686 // This can happen when a module is imported in two contexts: in one, as a 4687 // user module; in another, as a system module (due to an import from 4688 // another module marked with the [system] flag). It usually indicates a 4689 // bug in the module map: this module should also be marked with [system]. 4690 // 4691 // If -Wno-system-headers (the default), and the first import is as a 4692 // system module, then validation will fail during the as-user import, 4693 // since -Werror flags won't have been validated. However, it's reasonable 4694 // to treat this consistently as a system module. 4695 // 4696 // If -Wsystem-headers, the PCM on disk was built with 4697 // -Wno-system-headers, and the first import is as a user module, then 4698 // validation will fail during the as-system import since the PCM on disk 4699 // doesn't guarantee that -Werror was respected. However, the -Werror 4700 // flags were checked during the initial as-user import. 4701 if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) { 4702 Diag(diag::warn_module_system_bit_conflict) << F.FileName; 4703 return Success; 4704 } 4705 } 4706 4707 return Result; 4708 } 4709 4710 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl( 4711 ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities, 4712 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener, 4713 bool ValidateDiagnosticOptions) { 4714 // Initialize a stream. 4715 BitstreamCursor Stream(StreamData); 4716 4717 // Sniff for the signature. 4718 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4719 // FIXME this drops the error on the floor. 4720 consumeError(std::move(Err)); 4721 return Failure; 4722 } 4723 4724 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 4725 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 4726 return Failure; 4727 4728 // Read all of the records in the options block. 4729 RecordData Record; 4730 ASTReadResult Result = Success; 4731 while (true) { 4732 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4733 if (!MaybeEntry) { 4734 // FIXME this drops the error on the floor. 4735 consumeError(MaybeEntry.takeError()); 4736 return Failure; 4737 } 4738 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4739 4740 switch (Entry.Kind) { 4741 case llvm::BitstreamEntry::Error: 4742 case llvm::BitstreamEntry::SubBlock: 4743 return Failure; 4744 4745 case llvm::BitstreamEntry::EndBlock: 4746 return Result; 4747 4748 case llvm::BitstreamEntry::Record: 4749 // The interesting case. 4750 break; 4751 } 4752 4753 // Read and process a record. 4754 Record.clear(); 4755 StringRef Blob; 4756 Expected<unsigned> MaybeRecordType = 4757 Stream.readRecord(Entry.ID, Record, &Blob); 4758 if (!MaybeRecordType) { 4759 // FIXME this drops the error. 4760 return Failure; 4761 } 4762 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) { 4763 case SIGNATURE: 4764 if (F) 4765 F->Signature = ASTFileSignature::create(Record.begin(), Record.end()); 4766 break; 4767 case AST_BLOCK_HASH: 4768 if (F) 4769 F->ASTBlockHash = 4770 ASTFileSignature::create(Record.begin(), Record.end()); 4771 break; 4772 case DIAGNOSTIC_OPTIONS: { 4773 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 4774 if (Listener && ValidateDiagnosticOptions && 4775 !AllowCompatibleConfigurationMismatch && 4776 ParseDiagnosticOptions(Record, Complain, *Listener)) 4777 Result = OutOfDate; // Don't return early. Read the signature. 4778 break; 4779 } 4780 case DIAG_PRAGMA_MAPPINGS: 4781 if (!F) 4782 break; 4783 if (F->PragmaDiagMappings.empty()) 4784 F->PragmaDiagMappings.swap(Record); 4785 else 4786 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(), 4787 Record.begin(), Record.end()); 4788 break; 4789 case HEADER_SEARCH_ENTRY_USAGE: 4790 if (!F) 4791 break; 4792 unsigned Count = Record[0]; 4793 const char *Byte = Blob.data(); 4794 F->SearchPathUsage = llvm::BitVector(Count, false); 4795 for (unsigned I = 0; I < Count; ++Byte) 4796 for (unsigned Bit = 0; Bit < 8 && I < Count; ++Bit, ++I) 4797 if (*Byte & (1 << Bit)) 4798 F->SearchPathUsage[I] = true; 4799 break; 4800 } 4801 } 4802 } 4803 4804 /// Parse a record and blob containing module file extension metadata. 4805 static bool parseModuleFileExtensionMetadata( 4806 const SmallVectorImpl<uint64_t> &Record, 4807 StringRef Blob, 4808 ModuleFileExtensionMetadata &Metadata) { 4809 if (Record.size() < 4) return true; 4810 4811 Metadata.MajorVersion = Record[0]; 4812 Metadata.MinorVersion = Record[1]; 4813 4814 unsigned BlockNameLen = Record[2]; 4815 unsigned UserInfoLen = Record[3]; 4816 4817 if (BlockNameLen + UserInfoLen > Blob.size()) return true; 4818 4819 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen); 4820 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen, 4821 Blob.data() + BlockNameLen + UserInfoLen); 4822 return false; 4823 } 4824 4825 llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) { 4826 BitstreamCursor &Stream = F.Stream; 4827 4828 RecordData Record; 4829 while (true) { 4830 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4831 if (!MaybeEntry) 4832 return MaybeEntry.takeError(); 4833 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4834 4835 switch (Entry.Kind) { 4836 case llvm::BitstreamEntry::SubBlock: 4837 if (llvm::Error Err = Stream.SkipBlock()) 4838 return Err; 4839 continue; 4840 case llvm::BitstreamEntry::EndBlock: 4841 return llvm::Error::success(); 4842 case llvm::BitstreamEntry::Error: 4843 return llvm::createStringError(std::errc::illegal_byte_sequence, 4844 "malformed block record in AST file"); 4845 case llvm::BitstreamEntry::Record: 4846 break; 4847 } 4848 4849 Record.clear(); 4850 StringRef Blob; 4851 Expected<unsigned> MaybeRecCode = 4852 Stream.readRecord(Entry.ID, Record, &Blob); 4853 if (!MaybeRecCode) 4854 return MaybeRecCode.takeError(); 4855 switch (MaybeRecCode.get()) { 4856 case EXTENSION_METADATA: { 4857 ModuleFileExtensionMetadata Metadata; 4858 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) 4859 return llvm::createStringError( 4860 std::errc::illegal_byte_sequence, 4861 "malformed EXTENSION_METADATA in AST file"); 4862 4863 // Find a module file extension with this block name. 4864 auto Known = ModuleFileExtensions.find(Metadata.BlockName); 4865 if (Known == ModuleFileExtensions.end()) break; 4866 4867 // Form a reader. 4868 if (auto Reader = Known->second->createExtensionReader(Metadata, *this, 4869 F, Stream)) { 4870 F.ExtensionReaders.push_back(std::move(Reader)); 4871 } 4872 4873 break; 4874 } 4875 } 4876 } 4877 4878 return llvm::Error::success(); 4879 } 4880 4881 void ASTReader::InitializeContext() { 4882 assert(ContextObj && "no context to initialize"); 4883 ASTContext &Context = *ContextObj; 4884 4885 // If there's a listener, notify them that we "read" the translation unit. 4886 if (DeserializationListener) 4887 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, 4888 Context.getTranslationUnitDecl()); 4889 4890 // FIXME: Find a better way to deal with collisions between these 4891 // built-in types. Right now, we just ignore the problem. 4892 4893 // Load the special types. 4894 if (SpecialTypes.size() >= NumSpecialTypeIDs) { 4895 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) { 4896 if (!Context.CFConstantStringTypeDecl) 4897 Context.setCFConstantStringType(GetType(String)); 4898 } 4899 4900 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) { 4901 QualType FileType = GetType(File); 4902 if (FileType.isNull()) { 4903 Error("FILE type is NULL"); 4904 return; 4905 } 4906 4907 if (!Context.FILEDecl) { 4908 if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) 4909 Context.setFILEDecl(Typedef->getDecl()); 4910 else { 4911 const TagType *Tag = FileType->getAs<TagType>(); 4912 if (!Tag) { 4913 Error("Invalid FILE type in AST file"); 4914 return; 4915 } 4916 Context.setFILEDecl(Tag->getDecl()); 4917 } 4918 } 4919 } 4920 4921 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) { 4922 QualType Jmp_bufType = GetType(Jmp_buf); 4923 if (Jmp_bufType.isNull()) { 4924 Error("jmp_buf type is NULL"); 4925 return; 4926 } 4927 4928 if (!Context.jmp_bufDecl) { 4929 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) 4930 Context.setjmp_bufDecl(Typedef->getDecl()); 4931 else { 4932 const TagType *Tag = Jmp_bufType->getAs<TagType>(); 4933 if (!Tag) { 4934 Error("Invalid jmp_buf type in AST file"); 4935 return; 4936 } 4937 Context.setjmp_bufDecl(Tag->getDecl()); 4938 } 4939 } 4940 } 4941 4942 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) { 4943 QualType Sigjmp_bufType = GetType(Sigjmp_buf); 4944 if (Sigjmp_bufType.isNull()) { 4945 Error("sigjmp_buf type is NULL"); 4946 return; 4947 } 4948 4949 if (!Context.sigjmp_bufDecl) { 4950 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) 4951 Context.setsigjmp_bufDecl(Typedef->getDecl()); 4952 else { 4953 const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); 4954 assert(Tag && "Invalid sigjmp_buf type in AST file"); 4955 Context.setsigjmp_bufDecl(Tag->getDecl()); 4956 } 4957 } 4958 } 4959 4960 if (unsigned ObjCIdRedef 4961 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) { 4962 if (Context.ObjCIdRedefinitionType.isNull()) 4963 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef); 4964 } 4965 4966 if (unsigned ObjCClassRedef 4967 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) { 4968 if (Context.ObjCClassRedefinitionType.isNull()) 4969 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef); 4970 } 4971 4972 if (unsigned ObjCSelRedef 4973 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) { 4974 if (Context.ObjCSelRedefinitionType.isNull()) 4975 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef); 4976 } 4977 4978 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) { 4979 QualType Ucontext_tType = GetType(Ucontext_t); 4980 if (Ucontext_tType.isNull()) { 4981 Error("ucontext_t type is NULL"); 4982 return; 4983 } 4984 4985 if (!Context.ucontext_tDecl) { 4986 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>()) 4987 Context.setucontext_tDecl(Typedef->getDecl()); 4988 else { 4989 const TagType *Tag = Ucontext_tType->getAs<TagType>(); 4990 assert(Tag && "Invalid ucontext_t type in AST file"); 4991 Context.setucontext_tDecl(Tag->getDecl()); 4992 } 4993 } 4994 } 4995 } 4996 4997 ReadPragmaDiagnosticMappings(Context.getDiagnostics()); 4998 4999 // If there were any CUDA special declarations, deserialize them. 5000 if (!CUDASpecialDeclRefs.empty()) { 5001 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!"); 5002 Context.setcudaConfigureCallDecl( 5003 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0]))); 5004 } 5005 5006 // Re-export any modules that were imported by a non-module AST file. 5007 // FIXME: This does not make macro-only imports visible again. 5008 for (auto &Import : ImportedModules) { 5009 if (Module *Imported = getSubmodule(Import.ID)) { 5010 makeModuleVisible(Imported, Module::AllVisible, 5011 /*ImportLoc=*/Import.ImportLoc); 5012 if (Import.ImportLoc.isValid()) 5013 PP.makeModuleVisible(Imported, Import.ImportLoc); 5014 // This updates visibility for Preprocessor only. For Sema, which can be 5015 // nullptr here, we do the same later, in UpdateSema(). 5016 } 5017 } 5018 } 5019 5020 void ASTReader::finalizeForWriting() { 5021 // Nothing to do for now. 5022 } 5023 5024 /// Reads and return the signature record from \p PCH's control block, or 5025 /// else returns 0. 5026 static ASTFileSignature readASTFileSignature(StringRef PCH) { 5027 BitstreamCursor Stream(PCH); 5028 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5029 // FIXME this drops the error on the floor. 5030 consumeError(std::move(Err)); 5031 return ASTFileSignature(); 5032 } 5033 5034 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 5035 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 5036 return ASTFileSignature(); 5037 5038 // Scan for SIGNATURE inside the diagnostic options block. 5039 ASTReader::RecordData Record; 5040 while (true) { 5041 Expected<llvm::BitstreamEntry> MaybeEntry = 5042 Stream.advanceSkippingSubblocks(); 5043 if (!MaybeEntry) { 5044 // FIXME this drops the error on the floor. 5045 consumeError(MaybeEntry.takeError()); 5046 return ASTFileSignature(); 5047 } 5048 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5049 5050 if (Entry.Kind != llvm::BitstreamEntry::Record) 5051 return ASTFileSignature(); 5052 5053 Record.clear(); 5054 StringRef Blob; 5055 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 5056 if (!MaybeRecord) { 5057 // FIXME this drops the error on the floor. 5058 consumeError(MaybeRecord.takeError()); 5059 return ASTFileSignature(); 5060 } 5061 if (SIGNATURE == MaybeRecord.get()) 5062 return ASTFileSignature::create(Record.begin(), 5063 Record.begin() + ASTFileSignature::size); 5064 } 5065 } 5066 5067 /// Retrieve the name of the original source file name 5068 /// directly from the AST file, without actually loading the AST 5069 /// file. 5070 std::string ASTReader::getOriginalSourceFile( 5071 const std::string &ASTFileName, FileManager &FileMgr, 5072 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) { 5073 // Open the AST file. 5074 auto Buffer = FileMgr.getBufferForFile(ASTFileName); 5075 if (!Buffer) { 5076 Diags.Report(diag::err_fe_unable_to_read_pch_file) 5077 << ASTFileName << Buffer.getError().message(); 5078 return std::string(); 5079 } 5080 5081 // Initialize the stream 5082 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer)); 5083 5084 // Sniff for the signature. 5085 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5086 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err); 5087 return std::string(); 5088 } 5089 5090 // Scan for the CONTROL_BLOCK_ID block. 5091 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) { 5092 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 5093 return std::string(); 5094 } 5095 5096 // Scan for ORIGINAL_FILE inside the control block. 5097 RecordData Record; 5098 while (true) { 5099 Expected<llvm::BitstreamEntry> MaybeEntry = 5100 Stream.advanceSkippingSubblocks(); 5101 if (!MaybeEntry) { 5102 // FIXME this drops errors on the floor. 5103 consumeError(MaybeEntry.takeError()); 5104 return std::string(); 5105 } 5106 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5107 5108 if (Entry.Kind == llvm::BitstreamEntry::EndBlock) 5109 return std::string(); 5110 5111 if (Entry.Kind != llvm::BitstreamEntry::Record) { 5112 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 5113 return std::string(); 5114 } 5115 5116 Record.clear(); 5117 StringRef Blob; 5118 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 5119 if (!MaybeRecord) { 5120 // FIXME this drops the errors on the floor. 5121 consumeError(MaybeRecord.takeError()); 5122 return std::string(); 5123 } 5124 if (ORIGINAL_FILE == MaybeRecord.get()) 5125 return Blob.str(); 5126 } 5127 } 5128 5129 namespace { 5130 5131 class SimplePCHValidator : public ASTReaderListener { 5132 const LangOptions &ExistingLangOpts; 5133 const TargetOptions &ExistingTargetOpts; 5134 const PreprocessorOptions &ExistingPPOpts; 5135 std::string ExistingModuleCachePath; 5136 FileManager &FileMgr; 5137 5138 public: 5139 SimplePCHValidator(const LangOptions &ExistingLangOpts, 5140 const TargetOptions &ExistingTargetOpts, 5141 const PreprocessorOptions &ExistingPPOpts, 5142 StringRef ExistingModuleCachePath, FileManager &FileMgr) 5143 : ExistingLangOpts(ExistingLangOpts), 5144 ExistingTargetOpts(ExistingTargetOpts), 5145 ExistingPPOpts(ExistingPPOpts), 5146 ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {} 5147 5148 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain, 5149 bool AllowCompatibleDifferences) override { 5150 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr, 5151 AllowCompatibleDifferences); 5152 } 5153 5154 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain, 5155 bool AllowCompatibleDifferences) override { 5156 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr, 5157 AllowCompatibleDifferences); 5158 } 5159 5160 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 5161 StringRef SpecificModuleCachePath, 5162 bool Complain) override { 5163 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5164 ExistingModuleCachePath, nullptr, 5165 ExistingLangOpts, ExistingPPOpts); 5166 } 5167 5168 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 5169 bool Complain, 5170 std::string &SuggestedPredefines) override { 5171 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr, 5172 SuggestedPredefines, ExistingLangOpts); 5173 } 5174 }; 5175 5176 } // namespace 5177 5178 bool ASTReader::readASTFileControlBlock( 5179 StringRef Filename, FileManager &FileMgr, 5180 const PCHContainerReader &PCHContainerRdr, 5181 bool FindModuleFileExtensions, 5182 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) { 5183 // Open the AST file. 5184 // FIXME: This allows use of the VFS; we do not allow use of the 5185 // VFS when actually loading a module. 5186 auto Buffer = FileMgr.getBufferForFile(Filename); 5187 if (!Buffer) { 5188 return true; 5189 } 5190 5191 // Initialize the stream 5192 StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer); 5193 BitstreamCursor Stream(Bytes); 5194 5195 // Sniff for the signature. 5196 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5197 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 5198 return true; 5199 } 5200 5201 // Scan for the CONTROL_BLOCK_ID block. 5202 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) 5203 return true; 5204 5205 bool NeedsInputFiles = Listener.needsInputFileVisitation(); 5206 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation(); 5207 bool NeedsImports = Listener.needsImportVisitation(); 5208 BitstreamCursor InputFilesCursor; 5209 5210 RecordData Record; 5211 std::string ModuleDir; 5212 bool DoneWithControlBlock = false; 5213 while (!DoneWithControlBlock) { 5214 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5215 if (!MaybeEntry) { 5216 // FIXME this drops the error on the floor. 5217 consumeError(MaybeEntry.takeError()); 5218 return true; 5219 } 5220 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5221 5222 switch (Entry.Kind) { 5223 case llvm::BitstreamEntry::SubBlock: { 5224 switch (Entry.ID) { 5225 case OPTIONS_BLOCK_ID: { 5226 std::string IgnoredSuggestedPredefines; 5227 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate, 5228 /*AllowCompatibleConfigurationMismatch*/ false, 5229 Listener, IgnoredSuggestedPredefines) != Success) 5230 return true; 5231 break; 5232 } 5233 5234 case INPUT_FILES_BLOCK_ID: 5235 InputFilesCursor = Stream; 5236 if (llvm::Error Err = Stream.SkipBlock()) { 5237 // FIXME this drops the error on the floor. 5238 consumeError(std::move(Err)); 5239 return true; 5240 } 5241 if (NeedsInputFiles && 5242 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID)) 5243 return true; 5244 break; 5245 5246 default: 5247 if (llvm::Error Err = Stream.SkipBlock()) { 5248 // FIXME this drops the error on the floor. 5249 consumeError(std::move(Err)); 5250 return true; 5251 } 5252 break; 5253 } 5254 5255 continue; 5256 } 5257 5258 case llvm::BitstreamEntry::EndBlock: 5259 DoneWithControlBlock = true; 5260 break; 5261 5262 case llvm::BitstreamEntry::Error: 5263 return true; 5264 5265 case llvm::BitstreamEntry::Record: 5266 break; 5267 } 5268 5269 if (DoneWithControlBlock) break; 5270 5271 Record.clear(); 5272 StringRef Blob; 5273 Expected<unsigned> MaybeRecCode = 5274 Stream.readRecord(Entry.ID, Record, &Blob); 5275 if (!MaybeRecCode) { 5276 // FIXME this drops the error. 5277 return Failure; 5278 } 5279 switch ((ControlRecordTypes)MaybeRecCode.get()) { 5280 case METADATA: 5281 if (Record[0] != VERSION_MAJOR) 5282 return true; 5283 if (Listener.ReadFullVersionInformation(Blob)) 5284 return true; 5285 break; 5286 case MODULE_NAME: 5287 Listener.ReadModuleName(Blob); 5288 break; 5289 case MODULE_DIRECTORY: 5290 ModuleDir = std::string(Blob); 5291 break; 5292 case MODULE_MAP_FILE: { 5293 unsigned Idx = 0; 5294 auto Path = ReadString(Record, Idx); 5295 ResolveImportedPath(Path, ModuleDir); 5296 Listener.ReadModuleMapFile(Path); 5297 break; 5298 } 5299 case INPUT_FILE_OFFSETS: { 5300 if (!NeedsInputFiles) 5301 break; 5302 5303 unsigned NumInputFiles = Record[0]; 5304 unsigned NumUserFiles = Record[1]; 5305 const llvm::support::unaligned_uint64_t *InputFileOffs = 5306 (const llvm::support::unaligned_uint64_t *)Blob.data(); 5307 for (unsigned I = 0; I != NumInputFiles; ++I) { 5308 // Go find this input file. 5309 bool isSystemFile = I >= NumUserFiles; 5310 5311 if (isSystemFile && !NeedsSystemInputFiles) 5312 break; // the rest are system input files 5313 5314 BitstreamCursor &Cursor = InputFilesCursor; 5315 SavedStreamPosition SavedPosition(Cursor); 5316 if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) { 5317 // FIXME this drops errors on the floor. 5318 consumeError(std::move(Err)); 5319 } 5320 5321 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 5322 if (!MaybeCode) { 5323 // FIXME this drops errors on the floor. 5324 consumeError(MaybeCode.takeError()); 5325 } 5326 unsigned Code = MaybeCode.get(); 5327 5328 RecordData Record; 5329 StringRef Blob; 5330 bool shouldContinue = false; 5331 Expected<unsigned> MaybeRecordType = 5332 Cursor.readRecord(Code, Record, &Blob); 5333 if (!MaybeRecordType) { 5334 // FIXME this drops errors on the floor. 5335 consumeError(MaybeRecordType.takeError()); 5336 } 5337 switch ((InputFileRecordTypes)MaybeRecordType.get()) { 5338 case INPUT_FILE_HASH: 5339 break; 5340 case INPUT_FILE: 5341 bool Overridden = static_cast<bool>(Record[3]); 5342 std::string Filename = std::string(Blob); 5343 ResolveImportedPath(Filename, ModuleDir); 5344 shouldContinue = Listener.visitInputFile( 5345 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false); 5346 break; 5347 } 5348 if (!shouldContinue) 5349 break; 5350 } 5351 break; 5352 } 5353 5354 case IMPORTS: { 5355 if (!NeedsImports) 5356 break; 5357 5358 unsigned Idx = 0, N = Record.size(); 5359 while (Idx < N) { 5360 // Read information about the AST file. 5361 Idx += 5362 1 + 1 + 1 + 1 + 5363 ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature 5364 std::string ModuleName = ReadString(Record, Idx); 5365 std::string Filename = ReadString(Record, Idx); 5366 ResolveImportedPath(Filename, ModuleDir); 5367 Listener.visitImport(ModuleName, Filename); 5368 } 5369 break; 5370 } 5371 5372 default: 5373 // No other validation to perform. 5374 break; 5375 } 5376 } 5377 5378 // Look for module file extension blocks, if requested. 5379 if (FindModuleFileExtensions) { 5380 BitstreamCursor SavedStream = Stream; 5381 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) { 5382 bool DoneWithExtensionBlock = false; 5383 while (!DoneWithExtensionBlock) { 5384 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5385 if (!MaybeEntry) { 5386 // FIXME this drops the error. 5387 return true; 5388 } 5389 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5390 5391 switch (Entry.Kind) { 5392 case llvm::BitstreamEntry::SubBlock: 5393 if (llvm::Error Err = Stream.SkipBlock()) { 5394 // FIXME this drops the error on the floor. 5395 consumeError(std::move(Err)); 5396 return true; 5397 } 5398 continue; 5399 5400 case llvm::BitstreamEntry::EndBlock: 5401 DoneWithExtensionBlock = true; 5402 continue; 5403 5404 case llvm::BitstreamEntry::Error: 5405 return true; 5406 5407 case llvm::BitstreamEntry::Record: 5408 break; 5409 } 5410 5411 Record.clear(); 5412 StringRef Blob; 5413 Expected<unsigned> MaybeRecCode = 5414 Stream.readRecord(Entry.ID, Record, &Blob); 5415 if (!MaybeRecCode) { 5416 // FIXME this drops the error. 5417 return true; 5418 } 5419 switch (MaybeRecCode.get()) { 5420 case EXTENSION_METADATA: { 5421 ModuleFileExtensionMetadata Metadata; 5422 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) 5423 return true; 5424 5425 Listener.readModuleFileExtension(Metadata); 5426 break; 5427 } 5428 } 5429 } 5430 } 5431 Stream = SavedStream; 5432 } 5433 5434 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 5435 if (readUnhashedControlBlockImpl( 5436 nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate, 5437 /*AllowCompatibleConfigurationMismatch*/ false, &Listener, 5438 ValidateDiagnosticOptions) != Success) 5439 return true; 5440 5441 return false; 5442 } 5443 5444 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr, 5445 const PCHContainerReader &PCHContainerRdr, 5446 const LangOptions &LangOpts, 5447 const TargetOptions &TargetOpts, 5448 const PreprocessorOptions &PPOpts, 5449 StringRef ExistingModuleCachePath) { 5450 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, 5451 ExistingModuleCachePath, FileMgr); 5452 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr, 5453 /*FindModuleFileExtensions=*/false, 5454 validator, 5455 /*ValidateDiagnosticOptions=*/true); 5456 } 5457 5458 llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F, 5459 unsigned ClientLoadCapabilities) { 5460 // Enter the submodule block. 5461 if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) 5462 return Err; 5463 5464 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap(); 5465 bool First = true; 5466 Module *CurrentModule = nullptr; 5467 RecordData Record; 5468 while (true) { 5469 Expected<llvm::BitstreamEntry> MaybeEntry = 5470 F.Stream.advanceSkippingSubblocks(); 5471 if (!MaybeEntry) 5472 return MaybeEntry.takeError(); 5473 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5474 5475 switch (Entry.Kind) { 5476 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 5477 case llvm::BitstreamEntry::Error: 5478 return llvm::createStringError(std::errc::illegal_byte_sequence, 5479 "malformed block record in AST file"); 5480 case llvm::BitstreamEntry::EndBlock: 5481 return llvm::Error::success(); 5482 case llvm::BitstreamEntry::Record: 5483 // The interesting case. 5484 break; 5485 } 5486 5487 // Read a record. 5488 StringRef Blob; 5489 Record.clear(); 5490 Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob); 5491 if (!MaybeKind) 5492 return MaybeKind.takeError(); 5493 unsigned Kind = MaybeKind.get(); 5494 5495 if ((Kind == SUBMODULE_METADATA) != First) 5496 return llvm::createStringError( 5497 std::errc::illegal_byte_sequence, 5498 "submodule metadata record should be at beginning of block"); 5499 First = false; 5500 5501 // Submodule information is only valid if we have a current module. 5502 // FIXME: Should we error on these cases? 5503 if (!CurrentModule && Kind != SUBMODULE_METADATA && 5504 Kind != SUBMODULE_DEFINITION) 5505 continue; 5506 5507 switch (Kind) { 5508 default: // Default behavior: ignore. 5509 break; 5510 5511 case SUBMODULE_DEFINITION: { 5512 if (Record.size() < 12) 5513 return llvm::createStringError(std::errc::illegal_byte_sequence, 5514 "malformed module definition"); 5515 5516 StringRef Name = Blob; 5517 unsigned Idx = 0; 5518 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]); 5519 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]); 5520 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++]; 5521 bool IsFramework = Record[Idx++]; 5522 bool IsExplicit = Record[Idx++]; 5523 bool IsSystem = Record[Idx++]; 5524 bool IsExternC = Record[Idx++]; 5525 bool InferSubmodules = Record[Idx++]; 5526 bool InferExplicitSubmodules = Record[Idx++]; 5527 bool InferExportWildcard = Record[Idx++]; 5528 bool ConfigMacrosExhaustive = Record[Idx++]; 5529 bool ModuleMapIsPrivate = Record[Idx++]; 5530 5531 Module *ParentModule = nullptr; 5532 if (Parent) 5533 ParentModule = getSubmodule(Parent); 5534 5535 // Retrieve this (sub)module from the module map, creating it if 5536 // necessary. 5537 CurrentModule = 5538 ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit) 5539 .first; 5540 5541 // FIXME: set the definition loc for CurrentModule, or call 5542 // ModMap.setInferredModuleAllowedBy() 5543 5544 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS; 5545 if (GlobalIndex >= SubmodulesLoaded.size() || 5546 SubmodulesLoaded[GlobalIndex]) 5547 return llvm::createStringError(std::errc::invalid_argument, 5548 "too many submodules"); 5549 5550 if (!ParentModule) { 5551 if (const FileEntry *CurFile = CurrentModule->getASTFile()) { 5552 // Don't emit module relocation error if we have -fno-validate-pch 5553 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation & 5554 DisableValidationForModuleKind::Module) && 5555 CurFile != F.File) { 5556 auto ConflictError = 5557 PartialDiagnostic(diag::err_module_file_conflict, 5558 ContextObj->DiagAllocator) 5559 << CurrentModule->getTopLevelModuleName() << CurFile->getName() 5560 << F.File->getName(); 5561 return DiagnosticError::create(CurrentImportLoc, ConflictError); 5562 } 5563 } 5564 5565 F.DidReadTopLevelSubmodule = true; 5566 CurrentModule->setASTFile(F.File); 5567 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath; 5568 } 5569 5570 CurrentModule->Kind = Kind; 5571 CurrentModule->Signature = F.Signature; 5572 CurrentModule->IsFromModuleFile = true; 5573 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem; 5574 CurrentModule->IsExternC = IsExternC; 5575 CurrentModule->InferSubmodules = InferSubmodules; 5576 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules; 5577 CurrentModule->InferExportWildcard = InferExportWildcard; 5578 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive; 5579 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate; 5580 if (DeserializationListener) 5581 DeserializationListener->ModuleRead(GlobalID, CurrentModule); 5582 5583 SubmodulesLoaded[GlobalIndex] = CurrentModule; 5584 5585 // Clear out data that will be replaced by what is in the module file. 5586 CurrentModule->LinkLibraries.clear(); 5587 CurrentModule->ConfigMacros.clear(); 5588 CurrentModule->UnresolvedConflicts.clear(); 5589 CurrentModule->Conflicts.clear(); 5590 5591 // The module is available unless it's missing a requirement; relevant 5592 // requirements will be (re-)added by SUBMODULE_REQUIRES records. 5593 // Missing headers that were present when the module was built do not 5594 // make it unavailable -- if we got this far, this must be an explicitly 5595 // imported module file. 5596 CurrentModule->Requirements.clear(); 5597 CurrentModule->MissingHeaders.clear(); 5598 CurrentModule->IsUnimportable = 5599 ParentModule && ParentModule->IsUnimportable; 5600 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable; 5601 break; 5602 } 5603 5604 case SUBMODULE_UMBRELLA_HEADER: { 5605 // FIXME: This doesn't work for framework modules as `Filename` is the 5606 // name as written in the module file and does not include 5607 // `Headers/`, so this path will never exist. 5608 std::string Filename = std::string(Blob); 5609 ResolveImportedPath(F, Filename); 5610 if (auto Umbrella = PP.getFileManager().getFile(Filename)) { 5611 if (!CurrentModule->getUmbrellaHeader()) { 5612 // FIXME: NameAsWritten 5613 ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob, ""); 5614 } 5615 // Note that it's too late at this point to return out of date if the 5616 // name from the PCM doesn't match up with the one in the module map, 5617 // but also quite unlikely since we will have already checked the 5618 // modification time and size of the module map file itself. 5619 } 5620 break; 5621 } 5622 5623 case SUBMODULE_HEADER: 5624 case SUBMODULE_EXCLUDED_HEADER: 5625 case SUBMODULE_PRIVATE_HEADER: 5626 // We lazily associate headers with their modules via the HeaderInfo table. 5627 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead 5628 // of complete filenames or remove it entirely. 5629 break; 5630 5631 case SUBMODULE_TEXTUAL_HEADER: 5632 case SUBMODULE_PRIVATE_TEXTUAL_HEADER: 5633 // FIXME: Textual headers are not marked in the HeaderInfo table. Load 5634 // them here. 5635 break; 5636 5637 case SUBMODULE_TOPHEADER: 5638 CurrentModule->addTopHeaderFilename(Blob); 5639 break; 5640 5641 case SUBMODULE_UMBRELLA_DIR: { 5642 // See comments in SUBMODULE_UMBRELLA_HEADER 5643 std::string Dirname = std::string(Blob); 5644 ResolveImportedPath(F, Dirname); 5645 if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) { 5646 if (!CurrentModule->getUmbrellaDir()) { 5647 // FIXME: NameAsWritten 5648 ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob, ""); 5649 } 5650 } 5651 break; 5652 } 5653 5654 case SUBMODULE_METADATA: { 5655 F.BaseSubmoduleID = getTotalNumSubmodules(); 5656 F.LocalNumSubmodules = Record[0]; 5657 unsigned LocalBaseSubmoduleID = Record[1]; 5658 if (F.LocalNumSubmodules > 0) { 5659 // Introduce the global -> local mapping for submodules within this 5660 // module. 5661 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F)); 5662 5663 // Introduce the local -> global mapping for submodules within this 5664 // module. 5665 F.SubmoduleRemap.insertOrReplace( 5666 std::make_pair(LocalBaseSubmoduleID, 5667 F.BaseSubmoduleID - LocalBaseSubmoduleID)); 5668 5669 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules); 5670 } 5671 break; 5672 } 5673 5674 case SUBMODULE_IMPORTS: 5675 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) { 5676 UnresolvedModuleRef Unresolved; 5677 Unresolved.File = &F; 5678 Unresolved.Mod = CurrentModule; 5679 Unresolved.ID = Record[Idx]; 5680 Unresolved.Kind = UnresolvedModuleRef::Import; 5681 Unresolved.IsWildcard = false; 5682 UnresolvedModuleRefs.push_back(Unresolved); 5683 } 5684 break; 5685 5686 case SUBMODULE_EXPORTS: 5687 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) { 5688 UnresolvedModuleRef Unresolved; 5689 Unresolved.File = &F; 5690 Unresolved.Mod = CurrentModule; 5691 Unresolved.ID = Record[Idx]; 5692 Unresolved.Kind = UnresolvedModuleRef::Export; 5693 Unresolved.IsWildcard = Record[Idx + 1]; 5694 UnresolvedModuleRefs.push_back(Unresolved); 5695 } 5696 5697 // Once we've loaded the set of exports, there's no reason to keep 5698 // the parsed, unresolved exports around. 5699 CurrentModule->UnresolvedExports.clear(); 5700 break; 5701 5702 case SUBMODULE_REQUIRES: 5703 CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(), 5704 PP.getTargetInfo()); 5705 break; 5706 5707 case SUBMODULE_LINK_LIBRARY: 5708 ModMap.resolveLinkAsDependencies(CurrentModule); 5709 CurrentModule->LinkLibraries.push_back( 5710 Module::LinkLibrary(std::string(Blob), Record[0])); 5711 break; 5712 5713 case SUBMODULE_CONFIG_MACRO: 5714 CurrentModule->ConfigMacros.push_back(Blob.str()); 5715 break; 5716 5717 case SUBMODULE_CONFLICT: { 5718 UnresolvedModuleRef Unresolved; 5719 Unresolved.File = &F; 5720 Unresolved.Mod = CurrentModule; 5721 Unresolved.ID = Record[0]; 5722 Unresolved.Kind = UnresolvedModuleRef::Conflict; 5723 Unresolved.IsWildcard = false; 5724 Unresolved.String = Blob; 5725 UnresolvedModuleRefs.push_back(Unresolved); 5726 break; 5727 } 5728 5729 case SUBMODULE_INITIALIZERS: { 5730 if (!ContextObj) 5731 break; 5732 SmallVector<uint32_t, 16> Inits; 5733 for (auto &ID : Record) 5734 Inits.push_back(getGlobalDeclID(F, ID)); 5735 ContextObj->addLazyModuleInitializers(CurrentModule, Inits); 5736 break; 5737 } 5738 5739 case SUBMODULE_EXPORT_AS: 5740 CurrentModule->ExportAsModule = Blob.str(); 5741 ModMap.addLinkAsDependency(CurrentModule); 5742 break; 5743 } 5744 } 5745 } 5746 5747 /// Parse the record that corresponds to a LangOptions data 5748 /// structure. 5749 /// 5750 /// This routine parses the language options from the AST file and then gives 5751 /// them to the AST listener if one is set. 5752 /// 5753 /// \returns true if the listener deems the file unacceptable, false otherwise. 5754 bool ASTReader::ParseLanguageOptions(const RecordData &Record, 5755 bool Complain, 5756 ASTReaderListener &Listener, 5757 bool AllowCompatibleDifferences) { 5758 LangOptions LangOpts; 5759 unsigned Idx = 0; 5760 #define LANGOPT(Name, Bits, Default, Description) \ 5761 LangOpts.Name = Record[Idx++]; 5762 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 5763 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++])); 5764 #include "clang/Basic/LangOptions.def" 5765 #define SANITIZER(NAME, ID) \ 5766 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]); 5767 #include "clang/Basic/Sanitizers.def" 5768 5769 for (unsigned N = Record[Idx++]; N; --N) 5770 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx)); 5771 5772 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++]; 5773 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx); 5774 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion); 5775 5776 LangOpts.CurrentModule = ReadString(Record, Idx); 5777 5778 // Comment options. 5779 for (unsigned N = Record[Idx++]; N; --N) { 5780 LangOpts.CommentOpts.BlockCommandNames.push_back( 5781 ReadString(Record, Idx)); 5782 } 5783 LangOpts.CommentOpts.ParseAllComments = Record[Idx++]; 5784 5785 // OpenMP offloading options. 5786 for (unsigned N = Record[Idx++]; N; --N) { 5787 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx))); 5788 } 5789 5790 LangOpts.OMPHostIRFile = ReadString(Record, Idx); 5791 5792 return Listener.ReadLanguageOptions(LangOpts, Complain, 5793 AllowCompatibleDifferences); 5794 } 5795 5796 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain, 5797 ASTReaderListener &Listener, 5798 bool AllowCompatibleDifferences) { 5799 unsigned Idx = 0; 5800 TargetOptions TargetOpts; 5801 TargetOpts.Triple = ReadString(Record, Idx); 5802 TargetOpts.CPU = ReadString(Record, Idx); 5803 TargetOpts.TuneCPU = ReadString(Record, Idx); 5804 TargetOpts.ABI = ReadString(Record, Idx); 5805 for (unsigned N = Record[Idx++]; N; --N) { 5806 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx)); 5807 } 5808 for (unsigned N = Record[Idx++]; N; --N) { 5809 TargetOpts.Features.push_back(ReadString(Record, Idx)); 5810 } 5811 5812 return Listener.ReadTargetOptions(TargetOpts, Complain, 5813 AllowCompatibleDifferences); 5814 } 5815 5816 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain, 5817 ASTReaderListener &Listener) { 5818 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions); 5819 unsigned Idx = 0; 5820 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++]; 5821 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \ 5822 DiagOpts->set##Name(static_cast<Type>(Record[Idx++])); 5823 #include "clang/Basic/DiagnosticOptions.def" 5824 5825 for (unsigned N = Record[Idx++]; N; --N) 5826 DiagOpts->Warnings.push_back(ReadString(Record, Idx)); 5827 for (unsigned N = Record[Idx++]; N; --N) 5828 DiagOpts->Remarks.push_back(ReadString(Record, Idx)); 5829 5830 return Listener.ReadDiagnosticOptions(DiagOpts, Complain); 5831 } 5832 5833 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain, 5834 ASTReaderListener &Listener) { 5835 FileSystemOptions FSOpts; 5836 unsigned Idx = 0; 5837 FSOpts.WorkingDir = ReadString(Record, Idx); 5838 return Listener.ReadFileSystemOptions(FSOpts, Complain); 5839 } 5840 5841 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record, 5842 bool Complain, 5843 ASTReaderListener &Listener) { 5844 HeaderSearchOptions HSOpts; 5845 unsigned Idx = 0; 5846 HSOpts.Sysroot = ReadString(Record, Idx); 5847 5848 // Include entries. 5849 for (unsigned N = Record[Idx++]; N; --N) { 5850 std::string Path = ReadString(Record, Idx); 5851 frontend::IncludeDirGroup Group 5852 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]); 5853 bool IsFramework = Record[Idx++]; 5854 bool IgnoreSysRoot = Record[Idx++]; 5855 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework, 5856 IgnoreSysRoot); 5857 } 5858 5859 // System header prefixes. 5860 for (unsigned N = Record[Idx++]; N; --N) { 5861 std::string Prefix = ReadString(Record, Idx); 5862 bool IsSystemHeader = Record[Idx++]; 5863 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader); 5864 } 5865 5866 HSOpts.ResourceDir = ReadString(Record, Idx); 5867 HSOpts.ModuleCachePath = ReadString(Record, Idx); 5868 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx); 5869 HSOpts.DisableModuleHash = Record[Idx++]; 5870 HSOpts.ImplicitModuleMaps = Record[Idx++]; 5871 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++]; 5872 HSOpts.EnablePrebuiltImplicitModules = Record[Idx++]; 5873 HSOpts.UseBuiltinIncludes = Record[Idx++]; 5874 HSOpts.UseStandardSystemIncludes = Record[Idx++]; 5875 HSOpts.UseStandardCXXIncludes = Record[Idx++]; 5876 HSOpts.UseLibcxx = Record[Idx++]; 5877 std::string SpecificModuleCachePath = ReadString(Record, Idx); 5878 5879 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5880 Complain); 5881 } 5882 5883 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record, 5884 bool Complain, 5885 ASTReaderListener &Listener, 5886 std::string &SuggestedPredefines) { 5887 PreprocessorOptions PPOpts; 5888 unsigned Idx = 0; 5889 5890 // Macro definitions/undefs 5891 for (unsigned N = Record[Idx++]; N; --N) { 5892 std::string Macro = ReadString(Record, Idx); 5893 bool IsUndef = Record[Idx++]; 5894 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef)); 5895 } 5896 5897 // Includes 5898 for (unsigned N = Record[Idx++]; N; --N) { 5899 PPOpts.Includes.push_back(ReadString(Record, Idx)); 5900 } 5901 5902 // Macro Includes 5903 for (unsigned N = Record[Idx++]; N; --N) { 5904 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx)); 5905 } 5906 5907 PPOpts.UsePredefines = Record[Idx++]; 5908 PPOpts.DetailedRecord = Record[Idx++]; 5909 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx); 5910 PPOpts.ObjCXXARCStandardLibrary = 5911 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]); 5912 SuggestedPredefines.clear(); 5913 return Listener.ReadPreprocessorOptions(PPOpts, Complain, 5914 SuggestedPredefines); 5915 } 5916 5917 std::pair<ModuleFile *, unsigned> 5918 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) { 5919 GlobalPreprocessedEntityMapType::iterator 5920 I = GlobalPreprocessedEntityMap.find(GlobalIndex); 5921 assert(I != GlobalPreprocessedEntityMap.end() && 5922 "Corrupted global preprocessed entity map"); 5923 ModuleFile *M = I->second; 5924 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID; 5925 return std::make_pair(M, LocalIndex); 5926 } 5927 5928 llvm::iterator_range<PreprocessingRecord::iterator> 5929 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const { 5930 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord()) 5931 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID, 5932 Mod.NumPreprocessedEntities); 5933 5934 return llvm::make_range(PreprocessingRecord::iterator(), 5935 PreprocessingRecord::iterator()); 5936 } 5937 5938 bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName, 5939 unsigned int ClientLoadCapabilities) { 5940 return ClientLoadCapabilities & ARR_OutOfDate && 5941 !getModuleManager().getModuleCache().isPCMFinal(ModuleFileName); 5942 } 5943 5944 llvm::iterator_range<ASTReader::ModuleDeclIterator> 5945 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) { 5946 return llvm::make_range( 5947 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls), 5948 ModuleDeclIterator(this, &Mod, 5949 Mod.FileSortedDecls + Mod.NumFileSortedDecls)); 5950 } 5951 5952 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) { 5953 auto I = GlobalSkippedRangeMap.find(GlobalIndex); 5954 assert(I != GlobalSkippedRangeMap.end() && 5955 "Corrupted global skipped range map"); 5956 ModuleFile *M = I->second; 5957 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID; 5958 assert(LocalIndex < M->NumPreprocessedSkippedRanges); 5959 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex]; 5960 SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()), 5961 TranslateSourceLocation(*M, RawRange.getEnd())); 5962 assert(Range.isValid()); 5963 return Range; 5964 } 5965 5966 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) { 5967 PreprocessedEntityID PPID = Index+1; 5968 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 5969 ModuleFile &M = *PPInfo.first; 5970 unsigned LocalIndex = PPInfo.second; 5971 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 5972 5973 if (!PP.getPreprocessingRecord()) { 5974 Error("no preprocessing record"); 5975 return nullptr; 5976 } 5977 5978 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor); 5979 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit( 5980 M.MacroOffsetsBase + PPOffs.BitOffset)) { 5981 Error(std::move(Err)); 5982 return nullptr; 5983 } 5984 5985 Expected<llvm::BitstreamEntry> MaybeEntry = 5986 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 5987 if (!MaybeEntry) { 5988 Error(MaybeEntry.takeError()); 5989 return nullptr; 5990 } 5991 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5992 5993 if (Entry.Kind != llvm::BitstreamEntry::Record) 5994 return nullptr; 5995 5996 // Read the record. 5997 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()), 5998 TranslateSourceLocation(M, PPOffs.getEnd())); 5999 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 6000 StringRef Blob; 6001 RecordData Record; 6002 Expected<unsigned> MaybeRecType = 6003 M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob); 6004 if (!MaybeRecType) { 6005 Error(MaybeRecType.takeError()); 6006 return nullptr; 6007 } 6008 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) { 6009 case PPD_MACRO_EXPANSION: { 6010 bool isBuiltin = Record[0]; 6011 IdentifierInfo *Name = nullptr; 6012 MacroDefinitionRecord *Def = nullptr; 6013 if (isBuiltin) 6014 Name = getLocalIdentifier(M, Record[1]); 6015 else { 6016 PreprocessedEntityID GlobalID = 6017 getGlobalPreprocessedEntityID(M, Record[1]); 6018 Def = cast<MacroDefinitionRecord>( 6019 PPRec.getLoadedPreprocessedEntity(GlobalID - 1)); 6020 } 6021 6022 MacroExpansion *ME; 6023 if (isBuiltin) 6024 ME = new (PPRec) MacroExpansion(Name, Range); 6025 else 6026 ME = new (PPRec) MacroExpansion(Def, Range); 6027 6028 return ME; 6029 } 6030 6031 case PPD_MACRO_DEFINITION: { 6032 // Decode the identifier info and then check again; if the macro is 6033 // still defined and associated with the identifier, 6034 IdentifierInfo *II = getLocalIdentifier(M, Record[0]); 6035 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range); 6036 6037 if (DeserializationListener) 6038 DeserializationListener->MacroDefinitionRead(PPID, MD); 6039 6040 return MD; 6041 } 6042 6043 case PPD_INCLUSION_DIRECTIVE: { 6044 const char *FullFileNameStart = Blob.data() + Record[0]; 6045 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]); 6046 Optional<FileEntryRef> File; 6047 if (!FullFileName.empty()) 6048 File = PP.getFileManager().getOptionalFileRef(FullFileName); 6049 6050 // FIXME: Stable encoding 6051 InclusionDirective::InclusionKind Kind 6052 = static_cast<InclusionDirective::InclusionKind>(Record[2]); 6053 InclusionDirective *ID 6054 = new (PPRec) InclusionDirective(PPRec, Kind, 6055 StringRef(Blob.data(), Record[0]), 6056 Record[1], Record[3], 6057 File, 6058 Range); 6059 return ID; 6060 } 6061 } 6062 6063 llvm_unreachable("Invalid PreprocessorDetailRecordTypes"); 6064 } 6065 6066 /// Find the next module that contains entities and return the ID 6067 /// of the first entry. 6068 /// 6069 /// \param SLocMapI points at a chunk of a module that contains no 6070 /// preprocessed entities or the entities it contains are not the ones we are 6071 /// looking for. 6072 PreprocessedEntityID ASTReader::findNextPreprocessedEntity( 6073 GlobalSLocOffsetMapType::const_iterator SLocMapI) const { 6074 ++SLocMapI; 6075 for (GlobalSLocOffsetMapType::const_iterator 6076 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) { 6077 ModuleFile &M = *SLocMapI->second; 6078 if (M.NumPreprocessedEntities) 6079 return M.BasePreprocessedEntityID; 6080 } 6081 6082 return getTotalNumPreprocessedEntities(); 6083 } 6084 6085 namespace { 6086 6087 struct PPEntityComp { 6088 const ASTReader &Reader; 6089 ModuleFile &M; 6090 6091 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {} 6092 6093 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const { 6094 SourceLocation LHS = getLoc(L); 6095 SourceLocation RHS = getLoc(R); 6096 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6097 } 6098 6099 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const { 6100 SourceLocation LHS = getLoc(L); 6101 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6102 } 6103 6104 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const { 6105 SourceLocation RHS = getLoc(R); 6106 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6107 } 6108 6109 SourceLocation getLoc(const PPEntityOffset &PPE) const { 6110 return Reader.TranslateSourceLocation(M, PPE.getBegin()); 6111 } 6112 }; 6113 6114 } // namespace 6115 6116 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc, 6117 bool EndsAfter) const { 6118 if (SourceMgr.isLocalSourceLocation(Loc)) 6119 return getTotalNumPreprocessedEntities(); 6120 6121 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find( 6122 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1); 6123 assert(SLocMapI != GlobalSLocOffsetMap.end() && 6124 "Corrupted global sloc offset map"); 6125 6126 if (SLocMapI->second->NumPreprocessedEntities == 0) 6127 return findNextPreprocessedEntity(SLocMapI); 6128 6129 ModuleFile &M = *SLocMapI->second; 6130 6131 using pp_iterator = const PPEntityOffset *; 6132 6133 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 6134 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 6135 6136 size_t Count = M.NumPreprocessedEntities; 6137 size_t Half; 6138 pp_iterator First = pp_begin; 6139 pp_iterator PPI; 6140 6141 if (EndsAfter) { 6142 PPI = std::upper_bound(pp_begin, pp_end, Loc, 6143 PPEntityComp(*this, M)); 6144 } else { 6145 // Do a binary search manually instead of using std::lower_bound because 6146 // The end locations of entities may be unordered (when a macro expansion 6147 // is inside another macro argument), but for this case it is not important 6148 // whether we get the first macro expansion or its containing macro. 6149 while (Count > 0) { 6150 Half = Count / 2; 6151 PPI = First; 6152 std::advance(PPI, Half); 6153 if (SourceMgr.isBeforeInTranslationUnit( 6154 TranslateSourceLocation(M, PPI->getEnd()), Loc)) { 6155 First = PPI; 6156 ++First; 6157 Count = Count - Half - 1; 6158 } else 6159 Count = Half; 6160 } 6161 } 6162 6163 if (PPI == pp_end) 6164 return findNextPreprocessedEntity(SLocMapI); 6165 6166 return M.BasePreprocessedEntityID + (PPI - pp_begin); 6167 } 6168 6169 /// Returns a pair of [Begin, End) indices of preallocated 6170 /// preprocessed entities that \arg Range encompasses. 6171 std::pair<unsigned, unsigned> 6172 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) { 6173 if (Range.isInvalid()) 6174 return std::make_pair(0,0); 6175 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); 6176 6177 PreprocessedEntityID BeginID = 6178 findPreprocessedEntity(Range.getBegin(), false); 6179 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true); 6180 return std::make_pair(BeginID, EndID); 6181 } 6182 6183 /// Optionally returns true or false if the preallocated preprocessed 6184 /// entity with index \arg Index came from file \arg FID. 6185 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index, 6186 FileID FID) { 6187 if (FID.isInvalid()) 6188 return false; 6189 6190 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 6191 ModuleFile &M = *PPInfo.first; 6192 unsigned LocalIndex = PPInfo.second; 6193 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 6194 6195 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin()); 6196 if (Loc.isInvalid()) 6197 return false; 6198 6199 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID)) 6200 return true; 6201 else 6202 return false; 6203 } 6204 6205 namespace { 6206 6207 /// Visitor used to search for information about a header file. 6208 class HeaderFileInfoVisitor { 6209 const FileEntry *FE; 6210 Optional<HeaderFileInfo> HFI; 6211 6212 public: 6213 explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {} 6214 6215 bool operator()(ModuleFile &M) { 6216 HeaderFileInfoLookupTable *Table 6217 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable); 6218 if (!Table) 6219 return false; 6220 6221 // Look in the on-disk hash table for an entry for this file name. 6222 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE); 6223 if (Pos == Table->end()) 6224 return false; 6225 6226 HFI = *Pos; 6227 return true; 6228 } 6229 6230 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; } 6231 }; 6232 6233 } // namespace 6234 6235 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) { 6236 HeaderFileInfoVisitor Visitor(FE); 6237 ModuleMgr.visit(Visitor); 6238 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) 6239 return *HFI; 6240 6241 return HeaderFileInfo(); 6242 } 6243 6244 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) { 6245 using DiagState = DiagnosticsEngine::DiagState; 6246 SmallVector<DiagState *, 32> DiagStates; 6247 6248 for (ModuleFile &F : ModuleMgr) { 6249 unsigned Idx = 0; 6250 auto &Record = F.PragmaDiagMappings; 6251 if (Record.empty()) 6252 continue; 6253 6254 DiagStates.clear(); 6255 6256 auto ReadDiagState = 6257 [&](const DiagState &BasedOn, SourceLocation Loc, 6258 bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * { 6259 unsigned BackrefID = Record[Idx++]; 6260 if (BackrefID != 0) 6261 return DiagStates[BackrefID - 1]; 6262 6263 // A new DiagState was created here. 6264 Diag.DiagStates.push_back(BasedOn); 6265 DiagState *NewState = &Diag.DiagStates.back(); 6266 DiagStates.push_back(NewState); 6267 unsigned Size = Record[Idx++]; 6268 assert(Idx + Size * 2 <= Record.size() && 6269 "Invalid data, not enough diag/map pairs"); 6270 while (Size--) { 6271 unsigned DiagID = Record[Idx++]; 6272 DiagnosticMapping NewMapping = 6273 DiagnosticMapping::deserialize(Record[Idx++]); 6274 if (!NewMapping.isPragma() && !IncludeNonPragmaStates) 6275 continue; 6276 6277 DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID); 6278 6279 // If this mapping was specified as a warning but the severity was 6280 // upgraded due to diagnostic settings, simulate the current diagnostic 6281 // settings (and use a warning). 6282 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) { 6283 NewMapping.setSeverity(diag::Severity::Warning); 6284 NewMapping.setUpgradedFromWarning(false); 6285 } 6286 6287 Mapping = NewMapping; 6288 } 6289 return NewState; 6290 }; 6291 6292 // Read the first state. 6293 DiagState *FirstState; 6294 if (F.Kind == MK_ImplicitModule) { 6295 // Implicitly-built modules are reused with different diagnostic 6296 // settings. Use the initial diagnostic state from Diag to simulate this 6297 // compilation's diagnostic settings. 6298 FirstState = Diag.DiagStatesByLoc.FirstDiagState; 6299 DiagStates.push_back(FirstState); 6300 6301 // Skip the initial diagnostic state from the serialized module. 6302 assert(Record[1] == 0 && 6303 "Invalid data, unexpected backref in initial state"); 6304 Idx = 3 + Record[2] * 2; 6305 assert(Idx < Record.size() && 6306 "Invalid data, not enough state change pairs in initial state"); 6307 } else if (F.isModule()) { 6308 // For an explicit module, preserve the flags from the module build 6309 // command line (-w, -Weverything, -Werror, ...) along with any explicit 6310 // -Wblah flags. 6311 unsigned Flags = Record[Idx++]; 6312 DiagState Initial; 6313 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1; 6314 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1; 6315 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1; 6316 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1; 6317 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1; 6318 Initial.ExtBehavior = (diag::Severity)Flags; 6319 FirstState = ReadDiagState(Initial, SourceLocation(), true); 6320 6321 assert(F.OriginalSourceFileID.isValid()); 6322 6323 // Set up the root buffer of the module to start with the initial 6324 // diagnostic state of the module itself, to cover files that contain no 6325 // explicit transitions (for which we did not serialize anything). 6326 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID] 6327 .StateTransitions.push_back({FirstState, 0}); 6328 } else { 6329 // For prefix ASTs, start with whatever the user configured on the 6330 // command line. 6331 Idx++; // Skip flags. 6332 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, 6333 SourceLocation(), false); 6334 } 6335 6336 // Read the state transitions. 6337 unsigned NumLocations = Record[Idx++]; 6338 while (NumLocations--) { 6339 assert(Idx < Record.size() && 6340 "Invalid data, missing pragma diagnostic states"); 6341 SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]); 6342 auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc); 6343 assert(IDAndOffset.first.isValid() && "invalid FileID for transition"); 6344 assert(IDAndOffset.second == 0 && "not a start location for a FileID"); 6345 unsigned Transitions = Record[Idx++]; 6346 6347 // Note that we don't need to set up Parent/ParentOffset here, because 6348 // we won't be changing the diagnostic state within imported FileIDs 6349 // (other than perhaps appending to the main source file, which has no 6350 // parent). 6351 auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first]; 6352 F.StateTransitions.reserve(F.StateTransitions.size() + Transitions); 6353 for (unsigned I = 0; I != Transitions; ++I) { 6354 unsigned Offset = Record[Idx++]; 6355 auto *State = 6356 ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false); 6357 F.StateTransitions.push_back({State, Offset}); 6358 } 6359 } 6360 6361 // Read the final state. 6362 assert(Idx < Record.size() && 6363 "Invalid data, missing final pragma diagnostic state"); 6364 SourceLocation CurStateLoc = 6365 ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]); 6366 auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false); 6367 6368 if (!F.isModule()) { 6369 Diag.DiagStatesByLoc.CurDiagState = CurState; 6370 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc; 6371 6372 // Preserve the property that the imaginary root file describes the 6373 // current state. 6374 FileID NullFile; 6375 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions; 6376 if (T.empty()) 6377 T.push_back({CurState, 0}); 6378 else 6379 T[0].State = CurState; 6380 } 6381 6382 // Don't try to read these mappings again. 6383 Record.clear(); 6384 } 6385 } 6386 6387 /// Get the correct cursor and offset for loading a type. 6388 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) { 6389 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index); 6390 assert(I != GlobalTypeMap.end() && "Corrupted global type map"); 6391 ModuleFile *M = I->second; 6392 return RecordLocation( 6393 M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() + 6394 M->DeclsBlockStartOffset); 6395 } 6396 6397 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) { 6398 switch (code) { 6399 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \ 6400 case TYPE_##CODE_ID: return Type::CLASS_ID; 6401 #include "clang/Serialization/TypeBitCodes.def" 6402 default: return llvm::None; 6403 } 6404 } 6405 6406 /// Read and return the type with the given index.. 6407 /// 6408 /// The index is the type ID, shifted and minus the number of predefs. This 6409 /// routine actually reads the record corresponding to the type at the given 6410 /// location. It is a helper routine for GetType, which deals with reading type 6411 /// IDs. 6412 QualType ASTReader::readTypeRecord(unsigned Index) { 6413 assert(ContextObj && "reading type with no AST context"); 6414 ASTContext &Context = *ContextObj; 6415 RecordLocation Loc = TypeCursorForIndex(Index); 6416 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 6417 6418 // Keep track of where we are in the stream, then jump back there 6419 // after reading this type. 6420 SavedStreamPosition SavedPosition(DeclsCursor); 6421 6422 ReadingKindTracker ReadingKind(Read_Type, *this); 6423 6424 // Note that we are loading a type record. 6425 Deserializing AType(this); 6426 6427 if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) { 6428 Error(std::move(Err)); 6429 return QualType(); 6430 } 6431 Expected<unsigned> RawCode = DeclsCursor.ReadCode(); 6432 if (!RawCode) { 6433 Error(RawCode.takeError()); 6434 return QualType(); 6435 } 6436 6437 ASTRecordReader Record(*this, *Loc.F); 6438 Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get()); 6439 if (!Code) { 6440 Error(Code.takeError()); 6441 return QualType(); 6442 } 6443 if (Code.get() == TYPE_EXT_QUAL) { 6444 QualType baseType = Record.readQualType(); 6445 Qualifiers quals = Record.readQualifiers(); 6446 return Context.getQualifiedType(baseType, quals); 6447 } 6448 6449 auto maybeClass = getTypeClassForCode((TypeCode) Code.get()); 6450 if (!maybeClass) { 6451 Error("Unexpected code for type"); 6452 return QualType(); 6453 } 6454 6455 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record); 6456 return TypeReader.read(*maybeClass); 6457 } 6458 6459 namespace clang { 6460 6461 class TypeLocReader : public TypeLocVisitor<TypeLocReader> { 6462 ASTRecordReader &Reader; 6463 6464 SourceLocation readSourceLocation() { 6465 return Reader.readSourceLocation(); 6466 } 6467 6468 TypeSourceInfo *GetTypeSourceInfo() { 6469 return Reader.readTypeSourceInfo(); 6470 } 6471 6472 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() { 6473 return Reader.readNestedNameSpecifierLoc(); 6474 } 6475 6476 Attr *ReadAttr() { 6477 return Reader.readAttr(); 6478 } 6479 6480 public: 6481 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {} 6482 6483 // We want compile-time assurance that we've enumerated all of 6484 // these, so unfortunately we have to declare them first, then 6485 // define them out-of-line. 6486 #define ABSTRACT_TYPELOC(CLASS, PARENT) 6487 #define TYPELOC(CLASS, PARENT) \ 6488 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 6489 #include "clang/AST/TypeLocNodes.def" 6490 6491 void VisitFunctionTypeLoc(FunctionTypeLoc); 6492 void VisitArrayTypeLoc(ArrayTypeLoc); 6493 }; 6494 6495 } // namespace clang 6496 6497 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 6498 // nothing to do 6499 } 6500 6501 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 6502 TL.setBuiltinLoc(readSourceLocation()); 6503 if (TL.needsExtraLocalData()) { 6504 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt())); 6505 TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt())); 6506 TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt())); 6507 TL.setModeAttr(Reader.readInt()); 6508 } 6509 } 6510 6511 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { 6512 TL.setNameLoc(readSourceLocation()); 6513 } 6514 6515 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { 6516 TL.setStarLoc(readSourceLocation()); 6517 } 6518 6519 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) { 6520 // nothing to do 6521 } 6522 6523 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { 6524 // nothing to do 6525 } 6526 6527 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) { 6528 TL.setExpansionLoc(readSourceLocation()); 6529 } 6530 6531 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 6532 TL.setCaretLoc(readSourceLocation()); 6533 } 6534 6535 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 6536 TL.setAmpLoc(readSourceLocation()); 6537 } 6538 6539 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 6540 TL.setAmpAmpLoc(readSourceLocation()); 6541 } 6542 6543 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 6544 TL.setStarLoc(readSourceLocation()); 6545 TL.setClassTInfo(GetTypeSourceInfo()); 6546 } 6547 6548 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { 6549 TL.setLBracketLoc(readSourceLocation()); 6550 TL.setRBracketLoc(readSourceLocation()); 6551 if (Reader.readBool()) 6552 TL.setSizeExpr(Reader.readExpr()); 6553 else 6554 TL.setSizeExpr(nullptr); 6555 } 6556 6557 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 6558 VisitArrayTypeLoc(TL); 6559 } 6560 6561 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 6562 VisitArrayTypeLoc(TL); 6563 } 6564 6565 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 6566 VisitArrayTypeLoc(TL); 6567 } 6568 6569 void TypeLocReader::VisitDependentSizedArrayTypeLoc( 6570 DependentSizedArrayTypeLoc TL) { 6571 VisitArrayTypeLoc(TL); 6572 } 6573 6574 void TypeLocReader::VisitDependentAddressSpaceTypeLoc( 6575 DependentAddressSpaceTypeLoc TL) { 6576 6577 TL.setAttrNameLoc(readSourceLocation()); 6578 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6579 TL.setAttrExprOperand(Reader.readExpr()); 6580 } 6581 6582 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( 6583 DependentSizedExtVectorTypeLoc TL) { 6584 TL.setNameLoc(readSourceLocation()); 6585 } 6586 6587 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { 6588 TL.setNameLoc(readSourceLocation()); 6589 } 6590 6591 void TypeLocReader::VisitDependentVectorTypeLoc( 6592 DependentVectorTypeLoc TL) { 6593 TL.setNameLoc(readSourceLocation()); 6594 } 6595 6596 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 6597 TL.setNameLoc(readSourceLocation()); 6598 } 6599 6600 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) { 6601 TL.setAttrNameLoc(readSourceLocation()); 6602 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6603 TL.setAttrRowOperand(Reader.readExpr()); 6604 TL.setAttrColumnOperand(Reader.readExpr()); 6605 } 6606 6607 void TypeLocReader::VisitDependentSizedMatrixTypeLoc( 6608 DependentSizedMatrixTypeLoc TL) { 6609 TL.setAttrNameLoc(readSourceLocation()); 6610 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6611 TL.setAttrRowOperand(Reader.readExpr()); 6612 TL.setAttrColumnOperand(Reader.readExpr()); 6613 } 6614 6615 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 6616 TL.setLocalRangeBegin(readSourceLocation()); 6617 TL.setLParenLoc(readSourceLocation()); 6618 TL.setRParenLoc(readSourceLocation()); 6619 TL.setExceptionSpecRange(Reader.readSourceRange()); 6620 TL.setLocalRangeEnd(readSourceLocation()); 6621 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) { 6622 TL.setParam(i, Reader.readDeclAs<ParmVarDecl>()); 6623 } 6624 } 6625 6626 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 6627 VisitFunctionTypeLoc(TL); 6628 } 6629 6630 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 6631 VisitFunctionTypeLoc(TL); 6632 } 6633 6634 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 6635 TL.setNameLoc(readSourceLocation()); 6636 } 6637 6638 void TypeLocReader::VisitUsingTypeLoc(UsingTypeLoc TL) { 6639 TL.setNameLoc(readSourceLocation()); 6640 } 6641 6642 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 6643 TL.setNameLoc(readSourceLocation()); 6644 } 6645 6646 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 6647 TL.setTypeofLoc(readSourceLocation()); 6648 TL.setLParenLoc(readSourceLocation()); 6649 TL.setRParenLoc(readSourceLocation()); 6650 } 6651 6652 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 6653 TL.setTypeofLoc(readSourceLocation()); 6654 TL.setLParenLoc(readSourceLocation()); 6655 TL.setRParenLoc(readSourceLocation()); 6656 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6657 } 6658 6659 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 6660 TL.setDecltypeLoc(readSourceLocation()); 6661 TL.setRParenLoc(readSourceLocation()); 6662 } 6663 6664 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 6665 TL.setKWLoc(readSourceLocation()); 6666 TL.setLParenLoc(readSourceLocation()); 6667 TL.setRParenLoc(readSourceLocation()); 6668 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6669 } 6670 6671 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) { 6672 TL.setNameLoc(readSourceLocation()); 6673 if (Reader.readBool()) { 6674 TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc()); 6675 TL.setTemplateKWLoc(readSourceLocation()); 6676 TL.setConceptNameLoc(readSourceLocation()); 6677 TL.setFoundDecl(Reader.readDeclAs<NamedDecl>()); 6678 TL.setLAngleLoc(readSourceLocation()); 6679 TL.setRAngleLoc(readSourceLocation()); 6680 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6681 TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo( 6682 TL.getTypePtr()->getArg(i).getKind())); 6683 } 6684 if (Reader.readBool()) 6685 TL.setRParenLoc(readSourceLocation()); 6686 } 6687 6688 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc( 6689 DeducedTemplateSpecializationTypeLoc TL) { 6690 TL.setTemplateNameLoc(readSourceLocation()); 6691 } 6692 6693 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { 6694 TL.setNameLoc(readSourceLocation()); 6695 } 6696 6697 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { 6698 TL.setNameLoc(readSourceLocation()); 6699 } 6700 6701 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 6702 TL.setAttr(ReadAttr()); 6703 } 6704 6705 void TypeLocReader::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) { 6706 // Nothing to do. 6707 } 6708 6709 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 6710 TL.setNameLoc(readSourceLocation()); 6711 } 6712 6713 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( 6714 SubstTemplateTypeParmTypeLoc TL) { 6715 TL.setNameLoc(readSourceLocation()); 6716 } 6717 6718 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc( 6719 SubstTemplateTypeParmPackTypeLoc TL) { 6720 TL.setNameLoc(readSourceLocation()); 6721 } 6722 6723 void TypeLocReader::VisitTemplateSpecializationTypeLoc( 6724 TemplateSpecializationTypeLoc TL) { 6725 TL.setTemplateKeywordLoc(readSourceLocation()); 6726 TL.setTemplateNameLoc(readSourceLocation()); 6727 TL.setLAngleLoc(readSourceLocation()); 6728 TL.setRAngleLoc(readSourceLocation()); 6729 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6730 TL.setArgLocInfo( 6731 i, 6732 Reader.readTemplateArgumentLocInfo( 6733 TL.getTypePtr()->getArg(i).getKind())); 6734 } 6735 6736 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) { 6737 TL.setLParenLoc(readSourceLocation()); 6738 TL.setRParenLoc(readSourceLocation()); 6739 } 6740 6741 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 6742 TL.setElaboratedKeywordLoc(readSourceLocation()); 6743 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6744 } 6745 6746 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 6747 TL.setNameLoc(readSourceLocation()); 6748 } 6749 6750 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 6751 TL.setElaboratedKeywordLoc(readSourceLocation()); 6752 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6753 TL.setNameLoc(readSourceLocation()); 6754 } 6755 6756 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( 6757 DependentTemplateSpecializationTypeLoc TL) { 6758 TL.setElaboratedKeywordLoc(readSourceLocation()); 6759 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6760 TL.setTemplateKeywordLoc(readSourceLocation()); 6761 TL.setTemplateNameLoc(readSourceLocation()); 6762 TL.setLAngleLoc(readSourceLocation()); 6763 TL.setRAngleLoc(readSourceLocation()); 6764 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 6765 TL.setArgLocInfo( 6766 I, 6767 Reader.readTemplateArgumentLocInfo( 6768 TL.getTypePtr()->getArg(I).getKind())); 6769 } 6770 6771 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 6772 TL.setEllipsisLoc(readSourceLocation()); 6773 } 6774 6775 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 6776 TL.setNameLoc(readSourceLocation()); 6777 } 6778 6779 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) { 6780 if (TL.getNumProtocols()) { 6781 TL.setProtocolLAngleLoc(readSourceLocation()); 6782 TL.setProtocolRAngleLoc(readSourceLocation()); 6783 } 6784 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6785 TL.setProtocolLoc(i, readSourceLocation()); 6786 } 6787 6788 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 6789 TL.setHasBaseTypeAsWritten(Reader.readBool()); 6790 TL.setTypeArgsLAngleLoc(readSourceLocation()); 6791 TL.setTypeArgsRAngleLoc(readSourceLocation()); 6792 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i) 6793 TL.setTypeArgTInfo(i, GetTypeSourceInfo()); 6794 TL.setProtocolLAngleLoc(readSourceLocation()); 6795 TL.setProtocolRAngleLoc(readSourceLocation()); 6796 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6797 TL.setProtocolLoc(i, readSourceLocation()); 6798 } 6799 6800 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 6801 TL.setStarLoc(readSourceLocation()); 6802 } 6803 6804 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) { 6805 TL.setKWLoc(readSourceLocation()); 6806 TL.setLParenLoc(readSourceLocation()); 6807 TL.setRParenLoc(readSourceLocation()); 6808 } 6809 6810 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) { 6811 TL.setKWLoc(readSourceLocation()); 6812 } 6813 6814 void TypeLocReader::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) { 6815 TL.setNameLoc(readSourceLocation()); 6816 } 6817 void TypeLocReader::VisitDependentBitIntTypeLoc( 6818 clang::DependentBitIntTypeLoc TL) { 6819 TL.setNameLoc(readSourceLocation()); 6820 } 6821 6822 6823 void ASTRecordReader::readTypeLoc(TypeLoc TL) { 6824 TypeLocReader TLR(*this); 6825 for (; !TL.isNull(); TL = TL.getNextTypeLoc()) 6826 TLR.Visit(TL); 6827 } 6828 6829 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() { 6830 QualType InfoTy = readType(); 6831 if (InfoTy.isNull()) 6832 return nullptr; 6833 6834 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy); 6835 readTypeLoc(TInfo->getTypeLoc()); 6836 return TInfo; 6837 } 6838 6839 QualType ASTReader::GetType(TypeID ID) { 6840 assert(ContextObj && "reading type with no AST context"); 6841 ASTContext &Context = *ContextObj; 6842 6843 unsigned FastQuals = ID & Qualifiers::FastMask; 6844 unsigned Index = ID >> Qualifiers::FastWidth; 6845 6846 if (Index < NUM_PREDEF_TYPE_IDS) { 6847 QualType T; 6848 switch ((PredefinedTypeIDs)Index) { 6849 case PREDEF_TYPE_NULL_ID: 6850 return QualType(); 6851 case PREDEF_TYPE_VOID_ID: 6852 T = Context.VoidTy; 6853 break; 6854 case PREDEF_TYPE_BOOL_ID: 6855 T = Context.BoolTy; 6856 break; 6857 case PREDEF_TYPE_CHAR_U_ID: 6858 case PREDEF_TYPE_CHAR_S_ID: 6859 // FIXME: Check that the signedness of CharTy is correct! 6860 T = Context.CharTy; 6861 break; 6862 case PREDEF_TYPE_UCHAR_ID: 6863 T = Context.UnsignedCharTy; 6864 break; 6865 case PREDEF_TYPE_USHORT_ID: 6866 T = Context.UnsignedShortTy; 6867 break; 6868 case PREDEF_TYPE_UINT_ID: 6869 T = Context.UnsignedIntTy; 6870 break; 6871 case PREDEF_TYPE_ULONG_ID: 6872 T = Context.UnsignedLongTy; 6873 break; 6874 case PREDEF_TYPE_ULONGLONG_ID: 6875 T = Context.UnsignedLongLongTy; 6876 break; 6877 case PREDEF_TYPE_UINT128_ID: 6878 T = Context.UnsignedInt128Ty; 6879 break; 6880 case PREDEF_TYPE_SCHAR_ID: 6881 T = Context.SignedCharTy; 6882 break; 6883 case PREDEF_TYPE_WCHAR_ID: 6884 T = Context.WCharTy; 6885 break; 6886 case PREDEF_TYPE_SHORT_ID: 6887 T = Context.ShortTy; 6888 break; 6889 case PREDEF_TYPE_INT_ID: 6890 T = Context.IntTy; 6891 break; 6892 case PREDEF_TYPE_LONG_ID: 6893 T = Context.LongTy; 6894 break; 6895 case PREDEF_TYPE_LONGLONG_ID: 6896 T = Context.LongLongTy; 6897 break; 6898 case PREDEF_TYPE_INT128_ID: 6899 T = Context.Int128Ty; 6900 break; 6901 case PREDEF_TYPE_BFLOAT16_ID: 6902 T = Context.BFloat16Ty; 6903 break; 6904 case PREDEF_TYPE_HALF_ID: 6905 T = Context.HalfTy; 6906 break; 6907 case PREDEF_TYPE_FLOAT_ID: 6908 T = Context.FloatTy; 6909 break; 6910 case PREDEF_TYPE_DOUBLE_ID: 6911 T = Context.DoubleTy; 6912 break; 6913 case PREDEF_TYPE_LONGDOUBLE_ID: 6914 T = Context.LongDoubleTy; 6915 break; 6916 case PREDEF_TYPE_SHORT_ACCUM_ID: 6917 T = Context.ShortAccumTy; 6918 break; 6919 case PREDEF_TYPE_ACCUM_ID: 6920 T = Context.AccumTy; 6921 break; 6922 case PREDEF_TYPE_LONG_ACCUM_ID: 6923 T = Context.LongAccumTy; 6924 break; 6925 case PREDEF_TYPE_USHORT_ACCUM_ID: 6926 T = Context.UnsignedShortAccumTy; 6927 break; 6928 case PREDEF_TYPE_UACCUM_ID: 6929 T = Context.UnsignedAccumTy; 6930 break; 6931 case PREDEF_TYPE_ULONG_ACCUM_ID: 6932 T = Context.UnsignedLongAccumTy; 6933 break; 6934 case PREDEF_TYPE_SHORT_FRACT_ID: 6935 T = Context.ShortFractTy; 6936 break; 6937 case PREDEF_TYPE_FRACT_ID: 6938 T = Context.FractTy; 6939 break; 6940 case PREDEF_TYPE_LONG_FRACT_ID: 6941 T = Context.LongFractTy; 6942 break; 6943 case PREDEF_TYPE_USHORT_FRACT_ID: 6944 T = Context.UnsignedShortFractTy; 6945 break; 6946 case PREDEF_TYPE_UFRACT_ID: 6947 T = Context.UnsignedFractTy; 6948 break; 6949 case PREDEF_TYPE_ULONG_FRACT_ID: 6950 T = Context.UnsignedLongFractTy; 6951 break; 6952 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID: 6953 T = Context.SatShortAccumTy; 6954 break; 6955 case PREDEF_TYPE_SAT_ACCUM_ID: 6956 T = Context.SatAccumTy; 6957 break; 6958 case PREDEF_TYPE_SAT_LONG_ACCUM_ID: 6959 T = Context.SatLongAccumTy; 6960 break; 6961 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID: 6962 T = Context.SatUnsignedShortAccumTy; 6963 break; 6964 case PREDEF_TYPE_SAT_UACCUM_ID: 6965 T = Context.SatUnsignedAccumTy; 6966 break; 6967 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID: 6968 T = Context.SatUnsignedLongAccumTy; 6969 break; 6970 case PREDEF_TYPE_SAT_SHORT_FRACT_ID: 6971 T = Context.SatShortFractTy; 6972 break; 6973 case PREDEF_TYPE_SAT_FRACT_ID: 6974 T = Context.SatFractTy; 6975 break; 6976 case PREDEF_TYPE_SAT_LONG_FRACT_ID: 6977 T = Context.SatLongFractTy; 6978 break; 6979 case PREDEF_TYPE_SAT_USHORT_FRACT_ID: 6980 T = Context.SatUnsignedShortFractTy; 6981 break; 6982 case PREDEF_TYPE_SAT_UFRACT_ID: 6983 T = Context.SatUnsignedFractTy; 6984 break; 6985 case PREDEF_TYPE_SAT_ULONG_FRACT_ID: 6986 T = Context.SatUnsignedLongFractTy; 6987 break; 6988 case PREDEF_TYPE_FLOAT16_ID: 6989 T = Context.Float16Ty; 6990 break; 6991 case PREDEF_TYPE_FLOAT128_ID: 6992 T = Context.Float128Ty; 6993 break; 6994 case PREDEF_TYPE_IBM128_ID: 6995 T = Context.Ibm128Ty; 6996 break; 6997 case PREDEF_TYPE_OVERLOAD_ID: 6998 T = Context.OverloadTy; 6999 break; 7000 case PREDEF_TYPE_BOUND_MEMBER: 7001 T = Context.BoundMemberTy; 7002 break; 7003 case PREDEF_TYPE_PSEUDO_OBJECT: 7004 T = Context.PseudoObjectTy; 7005 break; 7006 case PREDEF_TYPE_DEPENDENT_ID: 7007 T = Context.DependentTy; 7008 break; 7009 case PREDEF_TYPE_UNKNOWN_ANY: 7010 T = Context.UnknownAnyTy; 7011 break; 7012 case PREDEF_TYPE_NULLPTR_ID: 7013 T = Context.NullPtrTy; 7014 break; 7015 case PREDEF_TYPE_CHAR8_ID: 7016 T = Context.Char8Ty; 7017 break; 7018 case PREDEF_TYPE_CHAR16_ID: 7019 T = Context.Char16Ty; 7020 break; 7021 case PREDEF_TYPE_CHAR32_ID: 7022 T = Context.Char32Ty; 7023 break; 7024 case PREDEF_TYPE_OBJC_ID: 7025 T = Context.ObjCBuiltinIdTy; 7026 break; 7027 case PREDEF_TYPE_OBJC_CLASS: 7028 T = Context.ObjCBuiltinClassTy; 7029 break; 7030 case PREDEF_TYPE_OBJC_SEL: 7031 T = Context.ObjCBuiltinSelTy; 7032 break; 7033 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 7034 case PREDEF_TYPE_##Id##_ID: \ 7035 T = Context.SingletonId; \ 7036 break; 7037 #include "clang/Basic/OpenCLImageTypes.def" 7038 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ 7039 case PREDEF_TYPE_##Id##_ID: \ 7040 T = Context.Id##Ty; \ 7041 break; 7042 #include "clang/Basic/OpenCLExtensionTypes.def" 7043 case PREDEF_TYPE_SAMPLER_ID: 7044 T = Context.OCLSamplerTy; 7045 break; 7046 case PREDEF_TYPE_EVENT_ID: 7047 T = Context.OCLEventTy; 7048 break; 7049 case PREDEF_TYPE_CLK_EVENT_ID: 7050 T = Context.OCLClkEventTy; 7051 break; 7052 case PREDEF_TYPE_QUEUE_ID: 7053 T = Context.OCLQueueTy; 7054 break; 7055 case PREDEF_TYPE_RESERVE_ID_ID: 7056 T = Context.OCLReserveIDTy; 7057 break; 7058 case PREDEF_TYPE_AUTO_DEDUCT: 7059 T = Context.getAutoDeductType(); 7060 break; 7061 case PREDEF_TYPE_AUTO_RREF_DEDUCT: 7062 T = Context.getAutoRRefDeductType(); 7063 break; 7064 case PREDEF_TYPE_ARC_UNBRIDGED_CAST: 7065 T = Context.ARCUnbridgedCastTy; 7066 break; 7067 case PREDEF_TYPE_BUILTIN_FN: 7068 T = Context.BuiltinFnTy; 7069 break; 7070 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX: 7071 T = Context.IncompleteMatrixIdxTy; 7072 break; 7073 case PREDEF_TYPE_OMP_ARRAY_SECTION: 7074 T = Context.OMPArraySectionTy; 7075 break; 7076 case PREDEF_TYPE_OMP_ARRAY_SHAPING: 7077 T = Context.OMPArraySectionTy; 7078 break; 7079 case PREDEF_TYPE_OMP_ITERATOR: 7080 T = Context.OMPIteratorTy; 7081 break; 7082 #define SVE_TYPE(Name, Id, SingletonId) \ 7083 case PREDEF_TYPE_##Id##_ID: \ 7084 T = Context.SingletonId; \ 7085 break; 7086 #include "clang/Basic/AArch64SVEACLETypes.def" 7087 #define PPC_VECTOR_TYPE(Name, Id, Size) \ 7088 case PREDEF_TYPE_##Id##_ID: \ 7089 T = Context.Id##Ty; \ 7090 break; 7091 #include "clang/Basic/PPCTypes.def" 7092 #define RVV_TYPE(Name, Id, SingletonId) \ 7093 case PREDEF_TYPE_##Id##_ID: \ 7094 T = Context.SingletonId; \ 7095 break; 7096 #include "clang/Basic/RISCVVTypes.def" 7097 } 7098 7099 assert(!T.isNull() && "Unknown predefined type"); 7100 return T.withFastQualifiers(FastQuals); 7101 } 7102 7103 Index -= NUM_PREDEF_TYPE_IDS; 7104 assert(Index < TypesLoaded.size() && "Type index out-of-range"); 7105 if (TypesLoaded[Index].isNull()) { 7106 TypesLoaded[Index] = readTypeRecord(Index); 7107 if (TypesLoaded[Index].isNull()) 7108 return QualType(); 7109 7110 TypesLoaded[Index]->setFromAST(); 7111 if (DeserializationListener) 7112 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID), 7113 TypesLoaded[Index]); 7114 } 7115 7116 return TypesLoaded[Index].withFastQualifiers(FastQuals); 7117 } 7118 7119 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) { 7120 return GetType(getGlobalTypeID(F, LocalID)); 7121 } 7122 7123 serialization::TypeID 7124 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const { 7125 unsigned FastQuals = LocalID & Qualifiers::FastMask; 7126 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth; 7127 7128 if (LocalIndex < NUM_PREDEF_TYPE_IDS) 7129 return LocalID; 7130 7131 if (!F.ModuleOffsetMap.empty()) 7132 ReadModuleOffsetMap(F); 7133 7134 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7135 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS); 7136 assert(I != F.TypeRemap.end() && "Invalid index into type index remap"); 7137 7138 unsigned GlobalIndex = LocalIndex + I->second; 7139 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals; 7140 } 7141 7142 TemplateArgumentLocInfo 7143 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) { 7144 switch (Kind) { 7145 case TemplateArgument::Expression: 7146 return readExpr(); 7147 case TemplateArgument::Type: 7148 return readTypeSourceInfo(); 7149 case TemplateArgument::Template: { 7150 NestedNameSpecifierLoc QualifierLoc = 7151 readNestedNameSpecifierLoc(); 7152 SourceLocation TemplateNameLoc = readSourceLocation(); 7153 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc, 7154 TemplateNameLoc, SourceLocation()); 7155 } 7156 case TemplateArgument::TemplateExpansion: { 7157 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc(); 7158 SourceLocation TemplateNameLoc = readSourceLocation(); 7159 SourceLocation EllipsisLoc = readSourceLocation(); 7160 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc, 7161 TemplateNameLoc, EllipsisLoc); 7162 } 7163 case TemplateArgument::Null: 7164 case TemplateArgument::Integral: 7165 case TemplateArgument::Declaration: 7166 case TemplateArgument::NullPtr: 7167 case TemplateArgument::Pack: 7168 // FIXME: Is this right? 7169 return TemplateArgumentLocInfo(); 7170 } 7171 llvm_unreachable("unexpected template argument loc"); 7172 } 7173 7174 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() { 7175 TemplateArgument Arg = readTemplateArgument(); 7176 7177 if (Arg.getKind() == TemplateArgument::Expression) { 7178 if (readBool()) // bool InfoHasSameExpr. 7179 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); 7180 } 7181 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind())); 7182 } 7183 7184 const ASTTemplateArgumentListInfo * 7185 ASTRecordReader::readASTTemplateArgumentListInfo() { 7186 SourceLocation LAngleLoc = readSourceLocation(); 7187 SourceLocation RAngleLoc = readSourceLocation(); 7188 unsigned NumArgsAsWritten = readInt(); 7189 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc); 7190 for (unsigned i = 0; i != NumArgsAsWritten; ++i) 7191 TemplArgsInfo.addArgument(readTemplateArgumentLoc()); 7192 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo); 7193 } 7194 7195 Decl *ASTReader::GetExternalDecl(uint32_t ID) { 7196 return GetDecl(ID); 7197 } 7198 7199 void ASTReader::CompleteRedeclChain(const Decl *D) { 7200 if (NumCurrentElementsDeserializing) { 7201 // We arrange to not care about the complete redeclaration chain while we're 7202 // deserializing. Just remember that the AST has marked this one as complete 7203 // but that it's not actually complete yet, so we know we still need to 7204 // complete it later. 7205 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D)); 7206 return; 7207 } 7208 7209 if (!D->getDeclContext()) { 7210 assert(isa<TranslationUnitDecl>(D) && "Not a TU?"); 7211 return; 7212 } 7213 7214 const DeclContext *DC = D->getDeclContext()->getRedeclContext(); 7215 7216 // If this is a named declaration, complete it by looking it up 7217 // within its context. 7218 // 7219 // FIXME: Merging a function definition should merge 7220 // all mergeable entities within it. 7221 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) || 7222 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) { 7223 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) { 7224 if (!getContext().getLangOpts().CPlusPlus && 7225 isa<TranslationUnitDecl>(DC)) { 7226 // Outside of C++, we don't have a lookup table for the TU, so update 7227 // the identifier instead. (For C++ modules, we don't store decls 7228 // in the serialized identifier table, so we do the lookup in the TU.) 7229 auto *II = Name.getAsIdentifierInfo(); 7230 assert(II && "non-identifier name in C?"); 7231 if (II->isOutOfDate()) 7232 updateOutOfDateIdentifier(*II); 7233 } else 7234 DC->lookup(Name); 7235 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) { 7236 // Find all declarations of this kind from the relevant context. 7237 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) { 7238 auto *DC = cast<DeclContext>(DCDecl); 7239 SmallVector<Decl*, 8> Decls; 7240 FindExternalLexicalDecls( 7241 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls); 7242 } 7243 } 7244 } 7245 7246 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) 7247 CTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7248 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) 7249 VTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7250 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 7251 if (auto *Template = FD->getPrimaryTemplate()) 7252 Template->LoadLazySpecializations(); 7253 } 7254 } 7255 7256 CXXCtorInitializer ** 7257 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) { 7258 RecordLocation Loc = getLocalBitOffset(Offset); 7259 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7260 SavedStreamPosition SavedPosition(Cursor); 7261 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7262 Error(std::move(Err)); 7263 return nullptr; 7264 } 7265 ReadingKindTracker ReadingKind(Read_Decl, *this); 7266 7267 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7268 if (!MaybeCode) { 7269 Error(MaybeCode.takeError()); 7270 return nullptr; 7271 } 7272 unsigned Code = MaybeCode.get(); 7273 7274 ASTRecordReader Record(*this, *Loc.F); 7275 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7276 if (!MaybeRecCode) { 7277 Error(MaybeRecCode.takeError()); 7278 return nullptr; 7279 } 7280 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) { 7281 Error("malformed AST file: missing C++ ctor initializers"); 7282 return nullptr; 7283 } 7284 7285 return Record.readCXXCtorInitializers(); 7286 } 7287 7288 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) { 7289 assert(ContextObj && "reading base specifiers with no AST context"); 7290 ASTContext &Context = *ContextObj; 7291 7292 RecordLocation Loc = getLocalBitOffset(Offset); 7293 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7294 SavedStreamPosition SavedPosition(Cursor); 7295 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7296 Error(std::move(Err)); 7297 return nullptr; 7298 } 7299 ReadingKindTracker ReadingKind(Read_Decl, *this); 7300 7301 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7302 if (!MaybeCode) { 7303 Error(MaybeCode.takeError()); 7304 return nullptr; 7305 } 7306 unsigned Code = MaybeCode.get(); 7307 7308 ASTRecordReader Record(*this, *Loc.F); 7309 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7310 if (!MaybeRecCode) { 7311 Error(MaybeCode.takeError()); 7312 return nullptr; 7313 } 7314 unsigned RecCode = MaybeRecCode.get(); 7315 7316 if (RecCode != DECL_CXX_BASE_SPECIFIERS) { 7317 Error("malformed AST file: missing C++ base specifiers"); 7318 return nullptr; 7319 } 7320 7321 unsigned NumBases = Record.readInt(); 7322 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases); 7323 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases]; 7324 for (unsigned I = 0; I != NumBases; ++I) 7325 Bases[I] = Record.readCXXBaseSpecifier(); 7326 return Bases; 7327 } 7328 7329 serialization::DeclID 7330 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const { 7331 if (LocalID < NUM_PREDEF_DECL_IDS) 7332 return LocalID; 7333 7334 if (!F.ModuleOffsetMap.empty()) 7335 ReadModuleOffsetMap(F); 7336 7337 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7338 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS); 7339 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap"); 7340 7341 return LocalID + I->second; 7342 } 7343 7344 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID, 7345 ModuleFile &M) const { 7346 // Predefined decls aren't from any module. 7347 if (ID < NUM_PREDEF_DECL_IDS) 7348 return false; 7349 7350 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID && 7351 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls; 7352 } 7353 7354 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) { 7355 if (!D->isFromASTFile()) 7356 return nullptr; 7357 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID()); 7358 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7359 return I->second; 7360 } 7361 7362 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) { 7363 if (ID < NUM_PREDEF_DECL_IDS) 7364 return SourceLocation(); 7365 7366 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7367 7368 if (Index > DeclsLoaded.size()) { 7369 Error("declaration ID out-of-range for AST file"); 7370 return SourceLocation(); 7371 } 7372 7373 if (Decl *D = DeclsLoaded[Index]) 7374 return D->getLocation(); 7375 7376 SourceLocation Loc; 7377 DeclCursorForID(ID, Loc); 7378 return Loc; 7379 } 7380 7381 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) { 7382 switch (ID) { 7383 case PREDEF_DECL_NULL_ID: 7384 return nullptr; 7385 7386 case PREDEF_DECL_TRANSLATION_UNIT_ID: 7387 return Context.getTranslationUnitDecl(); 7388 7389 case PREDEF_DECL_OBJC_ID_ID: 7390 return Context.getObjCIdDecl(); 7391 7392 case PREDEF_DECL_OBJC_SEL_ID: 7393 return Context.getObjCSelDecl(); 7394 7395 case PREDEF_DECL_OBJC_CLASS_ID: 7396 return Context.getObjCClassDecl(); 7397 7398 case PREDEF_DECL_OBJC_PROTOCOL_ID: 7399 return Context.getObjCProtocolDecl(); 7400 7401 case PREDEF_DECL_INT_128_ID: 7402 return Context.getInt128Decl(); 7403 7404 case PREDEF_DECL_UNSIGNED_INT_128_ID: 7405 return Context.getUInt128Decl(); 7406 7407 case PREDEF_DECL_OBJC_INSTANCETYPE_ID: 7408 return Context.getObjCInstanceTypeDecl(); 7409 7410 case PREDEF_DECL_BUILTIN_VA_LIST_ID: 7411 return Context.getBuiltinVaListDecl(); 7412 7413 case PREDEF_DECL_VA_LIST_TAG: 7414 return Context.getVaListTagDecl(); 7415 7416 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID: 7417 return Context.getBuiltinMSVaListDecl(); 7418 7419 case PREDEF_DECL_BUILTIN_MS_GUID_ID: 7420 return Context.getMSGuidTagDecl(); 7421 7422 case PREDEF_DECL_EXTERN_C_CONTEXT_ID: 7423 return Context.getExternCContextDecl(); 7424 7425 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID: 7426 return Context.getMakeIntegerSeqDecl(); 7427 7428 case PREDEF_DECL_CF_CONSTANT_STRING_ID: 7429 return Context.getCFConstantStringDecl(); 7430 7431 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID: 7432 return Context.getCFConstantStringTagDecl(); 7433 7434 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID: 7435 return Context.getTypePackElementDecl(); 7436 } 7437 llvm_unreachable("PredefinedDeclIDs unknown enum value"); 7438 } 7439 7440 Decl *ASTReader::GetExistingDecl(DeclID ID) { 7441 assert(ContextObj && "reading decl with no AST context"); 7442 if (ID < NUM_PREDEF_DECL_IDS) { 7443 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID); 7444 if (D) { 7445 // Track that we have merged the declaration with ID \p ID into the 7446 // pre-existing predefined declaration \p D. 7447 auto &Merged = KeyDecls[D->getCanonicalDecl()]; 7448 if (Merged.empty()) 7449 Merged.push_back(ID); 7450 } 7451 return D; 7452 } 7453 7454 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7455 7456 if (Index >= DeclsLoaded.size()) { 7457 assert(0 && "declaration ID out-of-range for AST file"); 7458 Error("declaration ID out-of-range for AST file"); 7459 return nullptr; 7460 } 7461 7462 return DeclsLoaded[Index]; 7463 } 7464 7465 Decl *ASTReader::GetDecl(DeclID ID) { 7466 if (ID < NUM_PREDEF_DECL_IDS) 7467 return GetExistingDecl(ID); 7468 7469 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7470 7471 if (Index >= DeclsLoaded.size()) { 7472 assert(0 && "declaration ID out-of-range for AST file"); 7473 Error("declaration ID out-of-range for AST file"); 7474 return nullptr; 7475 } 7476 7477 if (!DeclsLoaded[Index]) { 7478 ReadDeclRecord(ID); 7479 if (DeserializationListener) 7480 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); 7481 } 7482 7483 return DeclsLoaded[Index]; 7484 } 7485 7486 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M, 7487 DeclID GlobalID) { 7488 if (GlobalID < NUM_PREDEF_DECL_IDS) 7489 return GlobalID; 7490 7491 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID); 7492 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7493 ModuleFile *Owner = I->second; 7494 7495 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos 7496 = M.GlobalToLocalDeclIDs.find(Owner); 7497 if (Pos == M.GlobalToLocalDeclIDs.end()) 7498 return 0; 7499 7500 return GlobalID - Owner->BaseDeclID + Pos->second; 7501 } 7502 7503 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F, 7504 const RecordData &Record, 7505 unsigned &Idx) { 7506 if (Idx >= Record.size()) { 7507 Error("Corrupted AST file"); 7508 return 0; 7509 } 7510 7511 return getGlobalDeclID(F, Record[Idx++]); 7512 } 7513 7514 /// Resolve the offset of a statement into a statement. 7515 /// 7516 /// This operation will read a new statement from the external 7517 /// source each time it is called, and is meant to be used via a 7518 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 7519 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) { 7520 // Switch case IDs are per Decl. 7521 ClearSwitchCaseIDs(); 7522 7523 // Offset here is a global offset across the entire chain. 7524 RecordLocation Loc = getLocalBitOffset(Offset); 7525 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) { 7526 Error(std::move(Err)); 7527 return nullptr; 7528 } 7529 assert(NumCurrentElementsDeserializing == 0 && 7530 "should not be called while already deserializing"); 7531 Deserializing D(this); 7532 return ReadStmtFromStream(*Loc.F); 7533 } 7534 7535 void ASTReader::FindExternalLexicalDecls( 7536 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant, 7537 SmallVectorImpl<Decl *> &Decls) { 7538 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {}; 7539 7540 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) { 7541 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries"); 7542 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) { 7543 auto K = (Decl::Kind)+LexicalDecls[I]; 7544 if (!IsKindWeWant(K)) 7545 continue; 7546 7547 auto ID = (serialization::DeclID)+LexicalDecls[I + 1]; 7548 7549 // Don't add predefined declarations to the lexical context more 7550 // than once. 7551 if (ID < NUM_PREDEF_DECL_IDS) { 7552 if (PredefsVisited[ID]) 7553 continue; 7554 7555 PredefsVisited[ID] = true; 7556 } 7557 7558 if (Decl *D = GetLocalDecl(*M, ID)) { 7559 assert(D->getKind() == K && "wrong kind for lexical decl"); 7560 if (!DC->isDeclInLexicalTraversal(D)) 7561 Decls.push_back(D); 7562 } 7563 } 7564 }; 7565 7566 if (isa<TranslationUnitDecl>(DC)) { 7567 for (auto Lexical : TULexicalDecls) 7568 Visit(Lexical.first, Lexical.second); 7569 } else { 7570 auto I = LexicalDecls.find(DC); 7571 if (I != LexicalDecls.end()) 7572 Visit(I->second.first, I->second.second); 7573 } 7574 7575 ++NumLexicalDeclContextsRead; 7576 } 7577 7578 namespace { 7579 7580 class DeclIDComp { 7581 ASTReader &Reader; 7582 ModuleFile &Mod; 7583 7584 public: 7585 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {} 7586 7587 bool operator()(LocalDeclID L, LocalDeclID R) const { 7588 SourceLocation LHS = getLocation(L); 7589 SourceLocation RHS = getLocation(R); 7590 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7591 } 7592 7593 bool operator()(SourceLocation LHS, LocalDeclID R) const { 7594 SourceLocation RHS = getLocation(R); 7595 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7596 } 7597 7598 bool operator()(LocalDeclID L, SourceLocation RHS) const { 7599 SourceLocation LHS = getLocation(L); 7600 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7601 } 7602 7603 SourceLocation getLocation(LocalDeclID ID) const { 7604 return Reader.getSourceManager().getFileLoc( 7605 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID))); 7606 } 7607 }; 7608 7609 } // namespace 7610 7611 void ASTReader::FindFileRegionDecls(FileID File, 7612 unsigned Offset, unsigned Length, 7613 SmallVectorImpl<Decl *> &Decls) { 7614 SourceManager &SM = getSourceManager(); 7615 7616 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File); 7617 if (I == FileDeclIDs.end()) 7618 return; 7619 7620 FileDeclsInfo &DInfo = I->second; 7621 if (DInfo.Decls.empty()) 7622 return; 7623 7624 SourceLocation 7625 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset); 7626 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length); 7627 7628 DeclIDComp DIDComp(*this, *DInfo.Mod); 7629 ArrayRef<serialization::LocalDeclID>::iterator BeginIt = 7630 llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp); 7631 if (BeginIt != DInfo.Decls.begin()) 7632 --BeginIt; 7633 7634 // If we are pointing at a top-level decl inside an objc container, we need 7635 // to backtrack until we find it otherwise we will fail to report that the 7636 // region overlaps with an objc container. 7637 while (BeginIt != DInfo.Decls.begin() && 7638 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt)) 7639 ->isTopLevelDeclInObjCContainer()) 7640 --BeginIt; 7641 7642 ArrayRef<serialization::LocalDeclID>::iterator EndIt = 7643 llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp); 7644 if (EndIt != DInfo.Decls.end()) 7645 ++EndIt; 7646 7647 for (ArrayRef<serialization::LocalDeclID>::iterator 7648 DIt = BeginIt; DIt != EndIt; ++DIt) 7649 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt))); 7650 } 7651 7652 bool 7653 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, 7654 DeclarationName Name) { 7655 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() && 7656 "DeclContext has no visible decls in storage"); 7657 if (!Name) 7658 return false; 7659 7660 auto It = Lookups.find(DC); 7661 if (It == Lookups.end()) 7662 return false; 7663 7664 Deserializing LookupResults(this); 7665 7666 // Load the list of declarations. 7667 SmallVector<NamedDecl *, 64> Decls; 7668 llvm::SmallPtrSet<NamedDecl *, 8> Found; 7669 for (DeclID ID : It->second.Table.find(Name)) { 7670 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7671 if (ND->getDeclName() == Name && Found.insert(ND).second) 7672 Decls.push_back(ND); 7673 } 7674 7675 ++NumVisibleDeclContextsRead; 7676 SetExternalVisibleDeclsForName(DC, Name, Decls); 7677 return !Decls.empty(); 7678 } 7679 7680 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) { 7681 if (!DC->hasExternalVisibleStorage()) 7682 return; 7683 7684 auto It = Lookups.find(DC); 7685 assert(It != Lookups.end() && 7686 "have external visible storage but no lookup tables"); 7687 7688 DeclsMap Decls; 7689 7690 for (DeclID ID : It->second.Table.findAll()) { 7691 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7692 Decls[ND->getDeclName()].push_back(ND); 7693 } 7694 7695 ++NumVisibleDeclContextsRead; 7696 7697 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) { 7698 SetExternalVisibleDeclsForName(DC, I->first, I->second); 7699 } 7700 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false); 7701 } 7702 7703 const serialization::reader::DeclContextLookupTable * 7704 ASTReader::getLoadedLookupTables(DeclContext *Primary) const { 7705 auto I = Lookups.find(Primary); 7706 return I == Lookups.end() ? nullptr : &I->second; 7707 } 7708 7709 /// Under non-PCH compilation the consumer receives the objc methods 7710 /// before receiving the implementation, and codegen depends on this. 7711 /// We simulate this by deserializing and passing to consumer the methods of the 7712 /// implementation before passing the deserialized implementation decl. 7713 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD, 7714 ASTConsumer *Consumer) { 7715 assert(ImplD && Consumer); 7716 7717 for (auto *I : ImplD->methods()) 7718 Consumer->HandleInterestingDecl(DeclGroupRef(I)); 7719 7720 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD)); 7721 } 7722 7723 void ASTReader::PassInterestingDeclToConsumer(Decl *D) { 7724 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) 7725 PassObjCImplDeclToConsumer(ImplD, Consumer); 7726 else 7727 Consumer->HandleInterestingDecl(DeclGroupRef(D)); 7728 } 7729 7730 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) { 7731 this->Consumer = Consumer; 7732 7733 if (Consumer) 7734 PassInterestingDeclsToConsumer(); 7735 7736 if (DeserializationListener) 7737 DeserializationListener->ReaderInitialized(this); 7738 } 7739 7740 void ASTReader::PrintStats() { 7741 std::fprintf(stderr, "*** AST File Statistics:\n"); 7742 7743 unsigned NumTypesLoaded = 7744 TypesLoaded.size() - llvm::count(TypesLoaded, QualType()); 7745 unsigned NumDeclsLoaded = 7746 DeclsLoaded.size() - llvm::count(DeclsLoaded, (Decl *)nullptr); 7747 unsigned NumIdentifiersLoaded = 7748 IdentifiersLoaded.size() - 7749 llvm::count(IdentifiersLoaded, (IdentifierInfo *)nullptr); 7750 unsigned NumMacrosLoaded = 7751 MacrosLoaded.size() - llvm::count(MacrosLoaded, (MacroInfo *)nullptr); 7752 unsigned NumSelectorsLoaded = 7753 SelectorsLoaded.size() - llvm::count(SelectorsLoaded, Selector()); 7754 7755 if (unsigned TotalNumSLocEntries = getTotalNumSLocs()) 7756 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", 7757 NumSLocEntriesRead, TotalNumSLocEntries, 7758 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); 7759 if (!TypesLoaded.empty()) 7760 std::fprintf(stderr, " %u/%u types read (%f%%)\n", 7761 NumTypesLoaded, (unsigned)TypesLoaded.size(), 7762 ((float)NumTypesLoaded/TypesLoaded.size() * 100)); 7763 if (!DeclsLoaded.empty()) 7764 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", 7765 NumDeclsLoaded, (unsigned)DeclsLoaded.size(), 7766 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); 7767 if (!IdentifiersLoaded.empty()) 7768 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", 7769 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), 7770 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); 7771 if (!MacrosLoaded.empty()) 7772 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7773 NumMacrosLoaded, (unsigned)MacrosLoaded.size(), 7774 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100)); 7775 if (!SelectorsLoaded.empty()) 7776 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", 7777 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), 7778 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); 7779 if (TotalNumStatements) 7780 std::fprintf(stderr, " %u/%u statements read (%f%%)\n", 7781 NumStatementsRead, TotalNumStatements, 7782 ((float)NumStatementsRead/TotalNumStatements * 100)); 7783 if (TotalNumMacros) 7784 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7785 NumMacrosRead, TotalNumMacros, 7786 ((float)NumMacrosRead/TotalNumMacros * 100)); 7787 if (TotalLexicalDeclContexts) 7788 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", 7789 NumLexicalDeclContextsRead, TotalLexicalDeclContexts, 7790 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts 7791 * 100)); 7792 if (TotalVisibleDeclContexts) 7793 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", 7794 NumVisibleDeclContextsRead, TotalVisibleDeclContexts, 7795 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts 7796 * 100)); 7797 if (TotalNumMethodPoolEntries) 7798 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", 7799 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, 7800 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries 7801 * 100)); 7802 if (NumMethodPoolLookups) 7803 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n", 7804 NumMethodPoolHits, NumMethodPoolLookups, 7805 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0)); 7806 if (NumMethodPoolTableLookups) 7807 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n", 7808 NumMethodPoolTableHits, NumMethodPoolTableLookups, 7809 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups 7810 * 100.0)); 7811 if (NumIdentifierLookupHits) 7812 std::fprintf(stderr, 7813 " %u / %u identifier table lookups succeeded (%f%%)\n", 7814 NumIdentifierLookupHits, NumIdentifierLookups, 7815 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups); 7816 7817 if (GlobalIndex) { 7818 std::fprintf(stderr, "\n"); 7819 GlobalIndex->printStats(); 7820 } 7821 7822 std::fprintf(stderr, "\n"); 7823 dump(); 7824 std::fprintf(stderr, "\n"); 7825 } 7826 7827 template<typename Key, typename ModuleFile, unsigned InitialCapacity> 7828 LLVM_DUMP_METHOD static void 7829 dumpModuleIDMap(StringRef Name, 7830 const ContinuousRangeMap<Key, ModuleFile *, 7831 InitialCapacity> &Map) { 7832 if (Map.begin() == Map.end()) 7833 return; 7834 7835 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>; 7836 7837 llvm::errs() << Name << ":\n"; 7838 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end(); 7839 I != IEnd; ++I) { 7840 llvm::errs() << " " << I->first << " -> " << I->second->FileName 7841 << "\n"; 7842 } 7843 } 7844 7845 LLVM_DUMP_METHOD void ASTReader::dump() { 7846 llvm::errs() << "*** PCH/ModuleFile Remappings:\n"; 7847 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap); 7848 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap); 7849 dumpModuleIDMap("Global type map", GlobalTypeMap); 7850 dumpModuleIDMap("Global declaration map", GlobalDeclMap); 7851 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap); 7852 dumpModuleIDMap("Global macro map", GlobalMacroMap); 7853 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap); 7854 dumpModuleIDMap("Global selector map", GlobalSelectorMap); 7855 dumpModuleIDMap("Global preprocessed entity map", 7856 GlobalPreprocessedEntityMap); 7857 7858 llvm::errs() << "\n*** PCH/Modules Loaded:"; 7859 for (ModuleFile &M : ModuleMgr) 7860 M.dump(); 7861 } 7862 7863 /// Return the amount of memory used by memory buffers, breaking down 7864 /// by heap-backed versus mmap'ed memory. 7865 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const { 7866 for (ModuleFile &I : ModuleMgr) { 7867 if (llvm::MemoryBuffer *buf = I.Buffer) { 7868 size_t bytes = buf->getBufferSize(); 7869 switch (buf->getBufferKind()) { 7870 case llvm::MemoryBuffer::MemoryBuffer_Malloc: 7871 sizes.malloc_bytes += bytes; 7872 break; 7873 case llvm::MemoryBuffer::MemoryBuffer_MMap: 7874 sizes.mmap_bytes += bytes; 7875 break; 7876 } 7877 } 7878 } 7879 } 7880 7881 void ASTReader::InitializeSema(Sema &S) { 7882 SemaObj = &S; 7883 S.addExternalSource(this); 7884 7885 // Makes sure any declarations that were deserialized "too early" 7886 // still get added to the identifier's declaration chains. 7887 for (uint64_t ID : PreloadedDeclIDs) { 7888 NamedDecl *D = cast<NamedDecl>(GetDecl(ID)); 7889 pushExternalDeclIntoScope(D, D->getDeclName()); 7890 } 7891 PreloadedDeclIDs.clear(); 7892 7893 // FIXME: What happens if these are changed by a module import? 7894 if (!FPPragmaOptions.empty()) { 7895 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS"); 7896 FPOptionsOverride NewOverrides = 7897 FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]); 7898 SemaObj->CurFPFeatures = 7899 NewOverrides.applyOverrides(SemaObj->getLangOpts()); 7900 } 7901 7902 SemaObj->OpenCLFeatures = OpenCLExtensions; 7903 7904 UpdateSema(); 7905 } 7906 7907 void ASTReader::UpdateSema() { 7908 assert(SemaObj && "no Sema to update"); 7909 7910 // Load the offsets of the declarations that Sema references. 7911 // They will be lazily deserialized when needed. 7912 if (!SemaDeclRefs.empty()) { 7913 assert(SemaDeclRefs.size() % 3 == 0); 7914 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) { 7915 if (!SemaObj->StdNamespace) 7916 SemaObj->StdNamespace = SemaDeclRefs[I]; 7917 if (!SemaObj->StdBadAlloc) 7918 SemaObj->StdBadAlloc = SemaDeclRefs[I+1]; 7919 if (!SemaObj->StdAlignValT) 7920 SemaObj->StdAlignValT = SemaDeclRefs[I+2]; 7921 } 7922 SemaDeclRefs.clear(); 7923 } 7924 7925 // Update the state of pragmas. Use the same API as if we had encountered the 7926 // pragma in the source. 7927 if(OptimizeOffPragmaLocation.isValid()) 7928 SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation); 7929 if (PragmaMSStructState != -1) 7930 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState); 7931 if (PointersToMembersPragmaLocation.isValid()) { 7932 SemaObj->ActOnPragmaMSPointersToMembers( 7933 (LangOptions::PragmaMSPointersToMembersKind) 7934 PragmaMSPointersToMembersState, 7935 PointersToMembersPragmaLocation); 7936 } 7937 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth; 7938 7939 if (PragmaAlignPackCurrentValue) { 7940 // The bottom of the stack might have a default value. It must be adjusted 7941 // to the current value to ensure that the packing state is preserved after 7942 // popping entries that were included/imported from a PCH/module. 7943 bool DropFirst = false; 7944 if (!PragmaAlignPackStack.empty() && 7945 PragmaAlignPackStack.front().Location.isInvalid()) { 7946 assert(PragmaAlignPackStack.front().Value == 7947 SemaObj->AlignPackStack.DefaultValue && 7948 "Expected a default alignment value"); 7949 SemaObj->AlignPackStack.Stack.emplace_back( 7950 PragmaAlignPackStack.front().SlotLabel, 7951 SemaObj->AlignPackStack.CurrentValue, 7952 SemaObj->AlignPackStack.CurrentPragmaLocation, 7953 PragmaAlignPackStack.front().PushLocation); 7954 DropFirst = true; 7955 } 7956 for (const auto &Entry : llvm::makeArrayRef(PragmaAlignPackStack) 7957 .drop_front(DropFirst ? 1 : 0)) { 7958 SemaObj->AlignPackStack.Stack.emplace_back( 7959 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation); 7960 } 7961 if (PragmaAlignPackCurrentLocation.isInvalid()) { 7962 assert(*PragmaAlignPackCurrentValue == 7963 SemaObj->AlignPackStack.DefaultValue && 7964 "Expected a default align and pack value"); 7965 // Keep the current values. 7966 } else { 7967 SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue; 7968 SemaObj->AlignPackStack.CurrentPragmaLocation = 7969 PragmaAlignPackCurrentLocation; 7970 } 7971 } 7972 if (FpPragmaCurrentValue) { 7973 // The bottom of the stack might have a default value. It must be adjusted 7974 // to the current value to ensure that fp-pragma state is preserved after 7975 // popping entries that were included/imported from a PCH/module. 7976 bool DropFirst = false; 7977 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) { 7978 assert(FpPragmaStack.front().Value == 7979 SemaObj->FpPragmaStack.DefaultValue && 7980 "Expected a default pragma float_control value"); 7981 SemaObj->FpPragmaStack.Stack.emplace_back( 7982 FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue, 7983 SemaObj->FpPragmaStack.CurrentPragmaLocation, 7984 FpPragmaStack.front().PushLocation); 7985 DropFirst = true; 7986 } 7987 for (const auto &Entry : 7988 llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0)) 7989 SemaObj->FpPragmaStack.Stack.emplace_back( 7990 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation); 7991 if (FpPragmaCurrentLocation.isInvalid()) { 7992 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue && 7993 "Expected a default pragma float_control value"); 7994 // Keep the current values. 7995 } else { 7996 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue; 7997 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation; 7998 } 7999 } 8000 8001 // For non-modular AST files, restore visiblity of modules. 8002 for (auto &Import : ImportedModules) { 8003 if (Import.ImportLoc.isInvalid()) 8004 continue; 8005 if (Module *Imported = getSubmodule(Import.ID)) { 8006 SemaObj->makeModuleVisible(Imported, Import.ImportLoc); 8007 } 8008 } 8009 } 8010 8011 IdentifierInfo *ASTReader::get(StringRef Name) { 8012 // Note that we are loading an identifier. 8013 Deserializing AnIdentifier(this); 8014 8015 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0, 8016 NumIdentifierLookups, 8017 NumIdentifierLookupHits); 8018 8019 // We don't need to do identifier table lookups in C++ modules (we preload 8020 // all interesting declarations, and don't need to use the scope for name 8021 // lookups). Perform the lookup in PCH files, though, since we don't build 8022 // a complete initial identifier table if we're carrying on from a PCH. 8023 if (PP.getLangOpts().CPlusPlus) { 8024 for (auto F : ModuleMgr.pch_modules()) 8025 if (Visitor(*F)) 8026 break; 8027 } else { 8028 // If there is a global index, look there first to determine which modules 8029 // provably do not have any results for this identifier. 8030 GlobalModuleIndex::HitSet Hits; 8031 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 8032 if (!loadGlobalIndex()) { 8033 if (GlobalIndex->lookupIdentifier(Name, Hits)) { 8034 HitsPtr = &Hits; 8035 } 8036 } 8037 8038 ModuleMgr.visit(Visitor, HitsPtr); 8039 } 8040 8041 IdentifierInfo *II = Visitor.getIdentifierInfo(); 8042 markIdentifierUpToDate(II); 8043 return II; 8044 } 8045 8046 namespace clang { 8047 8048 /// An identifier-lookup iterator that enumerates all of the 8049 /// identifiers stored within a set of AST files. 8050 class ASTIdentifierIterator : public IdentifierIterator { 8051 /// The AST reader whose identifiers are being enumerated. 8052 const ASTReader &Reader; 8053 8054 /// The current index into the chain of AST files stored in 8055 /// the AST reader. 8056 unsigned Index; 8057 8058 /// The current position within the identifier lookup table 8059 /// of the current AST file. 8060 ASTIdentifierLookupTable::key_iterator Current; 8061 8062 /// The end position within the identifier lookup table of 8063 /// the current AST file. 8064 ASTIdentifierLookupTable::key_iterator End; 8065 8066 /// Whether to skip any modules in the ASTReader. 8067 bool SkipModules; 8068 8069 public: 8070 explicit ASTIdentifierIterator(const ASTReader &Reader, 8071 bool SkipModules = false); 8072 8073 StringRef Next() override; 8074 }; 8075 8076 } // namespace clang 8077 8078 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader, 8079 bool SkipModules) 8080 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) { 8081 } 8082 8083 StringRef ASTIdentifierIterator::Next() { 8084 while (Current == End) { 8085 // If we have exhausted all of our AST files, we're done. 8086 if (Index == 0) 8087 return StringRef(); 8088 8089 --Index; 8090 ModuleFile &F = Reader.ModuleMgr[Index]; 8091 if (SkipModules && F.isModule()) 8092 continue; 8093 8094 ASTIdentifierLookupTable *IdTable = 8095 (ASTIdentifierLookupTable *)F.IdentifierLookupTable; 8096 Current = IdTable->key_begin(); 8097 End = IdTable->key_end(); 8098 } 8099 8100 // We have any identifiers remaining in the current AST file; return 8101 // the next one. 8102 StringRef Result = *Current; 8103 ++Current; 8104 return Result; 8105 } 8106 8107 namespace { 8108 8109 /// A utility for appending two IdentifierIterators. 8110 class ChainedIdentifierIterator : public IdentifierIterator { 8111 std::unique_ptr<IdentifierIterator> Current; 8112 std::unique_ptr<IdentifierIterator> Queued; 8113 8114 public: 8115 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First, 8116 std::unique_ptr<IdentifierIterator> Second) 8117 : Current(std::move(First)), Queued(std::move(Second)) {} 8118 8119 StringRef Next() override { 8120 if (!Current) 8121 return StringRef(); 8122 8123 StringRef result = Current->Next(); 8124 if (!result.empty()) 8125 return result; 8126 8127 // Try the queued iterator, which may itself be empty. 8128 Current.reset(); 8129 std::swap(Current, Queued); 8130 return Next(); 8131 } 8132 }; 8133 8134 } // namespace 8135 8136 IdentifierIterator *ASTReader::getIdentifiers() { 8137 if (!loadGlobalIndex()) { 8138 std::unique_ptr<IdentifierIterator> ReaderIter( 8139 new ASTIdentifierIterator(*this, /*SkipModules=*/true)); 8140 std::unique_ptr<IdentifierIterator> ModulesIter( 8141 GlobalIndex->createIdentifierIterator()); 8142 return new ChainedIdentifierIterator(std::move(ReaderIter), 8143 std::move(ModulesIter)); 8144 } 8145 8146 return new ASTIdentifierIterator(*this); 8147 } 8148 8149 namespace clang { 8150 namespace serialization { 8151 8152 class ReadMethodPoolVisitor { 8153 ASTReader &Reader; 8154 Selector Sel; 8155 unsigned PriorGeneration; 8156 unsigned InstanceBits = 0; 8157 unsigned FactoryBits = 0; 8158 bool InstanceHasMoreThanOneDecl = false; 8159 bool FactoryHasMoreThanOneDecl = false; 8160 SmallVector<ObjCMethodDecl *, 4> InstanceMethods; 8161 SmallVector<ObjCMethodDecl *, 4> FactoryMethods; 8162 8163 public: 8164 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel, 8165 unsigned PriorGeneration) 8166 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {} 8167 8168 bool operator()(ModuleFile &M) { 8169 if (!M.SelectorLookupTable) 8170 return false; 8171 8172 // If we've already searched this module file, skip it now. 8173 if (M.Generation <= PriorGeneration) 8174 return true; 8175 8176 ++Reader.NumMethodPoolTableLookups; 8177 ASTSelectorLookupTable *PoolTable 8178 = (ASTSelectorLookupTable*)M.SelectorLookupTable; 8179 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel); 8180 if (Pos == PoolTable->end()) 8181 return false; 8182 8183 ++Reader.NumMethodPoolTableHits; 8184 ++Reader.NumSelectorsRead; 8185 // FIXME: Not quite happy with the statistics here. We probably should 8186 // disable this tracking when called via LoadSelector. 8187 // Also, should entries without methods count as misses? 8188 ++Reader.NumMethodPoolEntriesRead; 8189 ASTSelectorLookupTrait::data_type Data = *Pos; 8190 if (Reader.DeserializationListener) 8191 Reader.DeserializationListener->SelectorRead(Data.ID, Sel); 8192 8193 // Append methods in the reverse order, so that later we can process them 8194 // in the order they appear in the source code by iterating through 8195 // the vector in the reverse order. 8196 InstanceMethods.append(Data.Instance.rbegin(), Data.Instance.rend()); 8197 FactoryMethods.append(Data.Factory.rbegin(), Data.Factory.rend()); 8198 InstanceBits = Data.InstanceBits; 8199 FactoryBits = Data.FactoryBits; 8200 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl; 8201 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl; 8202 return false; 8203 } 8204 8205 /// Retrieve the instance methods found by this visitor. 8206 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const { 8207 return InstanceMethods; 8208 } 8209 8210 /// Retrieve the instance methods found by this visitor. 8211 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const { 8212 return FactoryMethods; 8213 } 8214 8215 unsigned getInstanceBits() const { return InstanceBits; } 8216 unsigned getFactoryBits() const { return FactoryBits; } 8217 8218 bool instanceHasMoreThanOneDecl() const { 8219 return InstanceHasMoreThanOneDecl; 8220 } 8221 8222 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; } 8223 }; 8224 8225 } // namespace serialization 8226 } // namespace clang 8227 8228 /// Add the given set of methods to the method list. 8229 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods, 8230 ObjCMethodList &List) { 8231 for (auto I = Methods.rbegin(), E = Methods.rend(); I != E; ++I) 8232 S.addMethodToGlobalList(&List, *I); 8233 } 8234 8235 void ASTReader::ReadMethodPool(Selector Sel) { 8236 // Get the selector generation and update it to the current generation. 8237 unsigned &Generation = SelectorGeneration[Sel]; 8238 unsigned PriorGeneration = Generation; 8239 Generation = getGeneration(); 8240 SelectorOutOfDate[Sel] = false; 8241 8242 // Search for methods defined with this selector. 8243 ++NumMethodPoolLookups; 8244 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration); 8245 ModuleMgr.visit(Visitor); 8246 8247 if (Visitor.getInstanceMethods().empty() && 8248 Visitor.getFactoryMethods().empty()) 8249 return; 8250 8251 ++NumMethodPoolHits; 8252 8253 if (!getSema()) 8254 return; 8255 8256 Sema &S = *getSema(); 8257 Sema::GlobalMethodPool::iterator Pos = 8258 S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethodPool::Lists())) 8259 .first; 8260 8261 Pos->second.first.setBits(Visitor.getInstanceBits()); 8262 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl()); 8263 Pos->second.second.setBits(Visitor.getFactoryBits()); 8264 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl()); 8265 8266 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since 8267 // when building a module we keep every method individually and may need to 8268 // update hasMoreThanOneDecl as we add the methods. 8269 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first); 8270 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second); 8271 } 8272 8273 void ASTReader::updateOutOfDateSelector(Selector Sel) { 8274 if (SelectorOutOfDate[Sel]) 8275 ReadMethodPool(Sel); 8276 } 8277 8278 void ASTReader::ReadKnownNamespaces( 8279 SmallVectorImpl<NamespaceDecl *> &Namespaces) { 8280 Namespaces.clear(); 8281 8282 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) { 8283 if (NamespaceDecl *Namespace 8284 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I]))) 8285 Namespaces.push_back(Namespace); 8286 } 8287 } 8288 8289 void ASTReader::ReadUndefinedButUsed( 8290 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) { 8291 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) { 8292 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++])); 8293 SourceLocation Loc = 8294 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]); 8295 Undefined.insert(std::make_pair(D, Loc)); 8296 } 8297 } 8298 8299 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector< 8300 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> & 8301 Exprs) { 8302 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) { 8303 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++])); 8304 uint64_t Count = DelayedDeleteExprs[Idx++]; 8305 for (uint64_t C = 0; C < Count; ++C) { 8306 SourceLocation DeleteLoc = 8307 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]); 8308 const bool IsArrayForm = DelayedDeleteExprs[Idx++]; 8309 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm)); 8310 } 8311 } 8312 } 8313 8314 void ASTReader::ReadTentativeDefinitions( 8315 SmallVectorImpl<VarDecl *> &TentativeDefs) { 8316 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { 8317 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I])); 8318 if (Var) 8319 TentativeDefs.push_back(Var); 8320 } 8321 TentativeDefinitions.clear(); 8322 } 8323 8324 void ASTReader::ReadUnusedFileScopedDecls( 8325 SmallVectorImpl<const DeclaratorDecl *> &Decls) { 8326 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) { 8327 DeclaratorDecl *D 8328 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I])); 8329 if (D) 8330 Decls.push_back(D); 8331 } 8332 UnusedFileScopedDecls.clear(); 8333 } 8334 8335 void ASTReader::ReadDelegatingConstructors( 8336 SmallVectorImpl<CXXConstructorDecl *> &Decls) { 8337 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) { 8338 CXXConstructorDecl *D 8339 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I])); 8340 if (D) 8341 Decls.push_back(D); 8342 } 8343 DelegatingCtorDecls.clear(); 8344 } 8345 8346 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) { 8347 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) { 8348 TypedefNameDecl *D 8349 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])); 8350 if (D) 8351 Decls.push_back(D); 8352 } 8353 ExtVectorDecls.clear(); 8354 } 8355 8356 void ASTReader::ReadUnusedLocalTypedefNameCandidates( 8357 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) { 8358 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N; 8359 ++I) { 8360 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>( 8361 GetDecl(UnusedLocalTypedefNameCandidates[I])); 8362 if (D) 8363 Decls.insert(D); 8364 } 8365 UnusedLocalTypedefNameCandidates.clear(); 8366 } 8367 8368 void ASTReader::ReadDeclsToCheckForDeferredDiags( 8369 llvm::SmallSetVector<Decl *, 4> &Decls) { 8370 for (auto I : DeclsToCheckForDeferredDiags) { 8371 auto *D = dyn_cast_or_null<Decl>(GetDecl(I)); 8372 if (D) 8373 Decls.insert(D); 8374 } 8375 DeclsToCheckForDeferredDiags.clear(); 8376 } 8377 8378 void ASTReader::ReadReferencedSelectors( 8379 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) { 8380 if (ReferencedSelectorsData.empty()) 8381 return; 8382 8383 // If there are @selector references added them to its pool. This is for 8384 // implementation of -Wselector. 8385 unsigned int DataSize = ReferencedSelectorsData.size()-1; 8386 unsigned I = 0; 8387 while (I < DataSize) { 8388 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]); 8389 SourceLocation SelLoc 8390 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]); 8391 Sels.push_back(std::make_pair(Sel, SelLoc)); 8392 } 8393 ReferencedSelectorsData.clear(); 8394 } 8395 8396 void ASTReader::ReadWeakUndeclaredIdentifiers( 8397 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) { 8398 if (WeakUndeclaredIdentifiers.empty()) 8399 return; 8400 8401 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) { 8402 IdentifierInfo *WeakId 8403 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8404 IdentifierInfo *AliasId 8405 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8406 SourceLocation Loc = 8407 SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]); 8408 WeakInfo WI(AliasId, Loc); 8409 WeakIDs.push_back(std::make_pair(WeakId, WI)); 8410 } 8411 WeakUndeclaredIdentifiers.clear(); 8412 } 8413 8414 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) { 8415 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) { 8416 ExternalVTableUse VT; 8417 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); 8418 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]); 8419 VT.DefinitionRequired = VTableUses[Idx++]; 8420 VTables.push_back(VT); 8421 } 8422 8423 VTableUses.clear(); 8424 } 8425 8426 void ASTReader::ReadPendingInstantiations( 8427 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) { 8428 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) { 8429 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++])); 8430 SourceLocation Loc 8431 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]); 8432 8433 Pending.push_back(std::make_pair(D, Loc)); 8434 } 8435 PendingInstantiations.clear(); 8436 } 8437 8438 void ASTReader::ReadLateParsedTemplates( 8439 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>> 8440 &LPTMap) { 8441 for (auto &LPT : LateParsedTemplates) { 8442 ModuleFile *FMod = LPT.first; 8443 RecordDataImpl &LateParsed = LPT.second; 8444 for (unsigned Idx = 0, N = LateParsed.size(); Idx < N; 8445 /* In loop */) { 8446 FunctionDecl *FD = 8447 cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++])); 8448 8449 auto LT = std::make_unique<LateParsedTemplate>(); 8450 LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]); 8451 8452 ModuleFile *F = getOwningModuleFile(LT->D); 8453 assert(F && "No module"); 8454 8455 unsigned TokN = LateParsed[Idx++]; 8456 LT->Toks.reserve(TokN); 8457 for (unsigned T = 0; T < TokN; ++T) 8458 LT->Toks.push_back(ReadToken(*F, LateParsed, Idx)); 8459 8460 LPTMap.insert(std::make_pair(FD, std::move(LT))); 8461 } 8462 } 8463 8464 LateParsedTemplates.clear(); 8465 } 8466 8467 void ASTReader::LoadSelector(Selector Sel) { 8468 // It would be complicated to avoid reading the methods anyway. So don't. 8469 ReadMethodPool(Sel); 8470 } 8471 8472 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) { 8473 assert(ID && "Non-zero identifier ID required"); 8474 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); 8475 IdentifiersLoaded[ID - 1] = II; 8476 if (DeserializationListener) 8477 DeserializationListener->IdentifierRead(ID, II); 8478 } 8479 8480 /// Set the globally-visible declarations associated with the given 8481 /// identifier. 8482 /// 8483 /// If the AST reader is currently in a state where the given declaration IDs 8484 /// cannot safely be resolved, they are queued until it is safe to resolve 8485 /// them. 8486 /// 8487 /// \param II an IdentifierInfo that refers to one or more globally-visible 8488 /// declarations. 8489 /// 8490 /// \param DeclIDs the set of declaration IDs with the name @p II that are 8491 /// visible at global scope. 8492 /// 8493 /// \param Decls if non-null, this vector will be populated with the set of 8494 /// deserialized declarations. These declarations will not be pushed into 8495 /// scope. 8496 void 8497 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, 8498 const SmallVectorImpl<uint32_t> &DeclIDs, 8499 SmallVectorImpl<Decl *> *Decls) { 8500 if (NumCurrentElementsDeserializing && !Decls) { 8501 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end()); 8502 return; 8503 } 8504 8505 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { 8506 if (!SemaObj) { 8507 // Queue this declaration so that it will be added to the 8508 // translation unit scope and identifier's declaration chain 8509 // once a Sema object is known. 8510 PreloadedDeclIDs.push_back(DeclIDs[I]); 8511 continue; 8512 } 8513 8514 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); 8515 8516 // If we're simply supposed to record the declarations, do so now. 8517 if (Decls) { 8518 Decls->push_back(D); 8519 continue; 8520 } 8521 8522 // Introduce this declaration into the translation-unit scope 8523 // and add it to the declaration chain for this identifier, so 8524 // that (unqualified) name lookup will find it. 8525 pushExternalDeclIntoScope(D, II); 8526 } 8527 } 8528 8529 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) { 8530 if (ID == 0) 8531 return nullptr; 8532 8533 if (IdentifiersLoaded.empty()) { 8534 Error("no identifier table in AST file"); 8535 return nullptr; 8536 } 8537 8538 ID -= 1; 8539 if (!IdentifiersLoaded[ID]) { 8540 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1); 8541 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map"); 8542 ModuleFile *M = I->second; 8543 unsigned Index = ID - M->BaseIdentifierID; 8544 const unsigned char *Data = 8545 M->IdentifierTableData + M->IdentifierOffsets[Index]; 8546 8547 ASTIdentifierLookupTrait Trait(*this, *M); 8548 auto KeyDataLen = Trait.ReadKeyDataLength(Data); 8549 auto Key = Trait.ReadKey(Data, KeyDataLen.first); 8550 auto &II = PP.getIdentifierTable().get(Key); 8551 IdentifiersLoaded[ID] = &II; 8552 markIdentifierFromAST(*this, II); 8553 if (DeserializationListener) 8554 DeserializationListener->IdentifierRead(ID + 1, &II); 8555 } 8556 8557 return IdentifiersLoaded[ID]; 8558 } 8559 8560 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) { 8561 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID)); 8562 } 8563 8564 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) { 8565 if (LocalID < NUM_PREDEF_IDENT_IDS) 8566 return LocalID; 8567 8568 if (!M.ModuleOffsetMap.empty()) 8569 ReadModuleOffsetMap(M); 8570 8571 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8572 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS); 8573 assert(I != M.IdentifierRemap.end() 8574 && "Invalid index into identifier index remap"); 8575 8576 return LocalID + I->second; 8577 } 8578 8579 MacroInfo *ASTReader::getMacro(MacroID ID) { 8580 if (ID == 0) 8581 return nullptr; 8582 8583 if (MacrosLoaded.empty()) { 8584 Error("no macro table in AST file"); 8585 return nullptr; 8586 } 8587 8588 ID -= NUM_PREDEF_MACRO_IDS; 8589 if (!MacrosLoaded[ID]) { 8590 GlobalMacroMapType::iterator I 8591 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS); 8592 assert(I != GlobalMacroMap.end() && "Corrupted global macro map"); 8593 ModuleFile *M = I->second; 8594 unsigned Index = ID - M->BaseMacroID; 8595 MacrosLoaded[ID] = 8596 ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]); 8597 8598 if (DeserializationListener) 8599 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS, 8600 MacrosLoaded[ID]); 8601 } 8602 8603 return MacrosLoaded[ID]; 8604 } 8605 8606 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) { 8607 if (LocalID < NUM_PREDEF_MACRO_IDS) 8608 return LocalID; 8609 8610 if (!M.ModuleOffsetMap.empty()) 8611 ReadModuleOffsetMap(M); 8612 8613 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8614 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS); 8615 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap"); 8616 8617 return LocalID + I->second; 8618 } 8619 8620 serialization::SubmoduleID 8621 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) { 8622 if (LocalID < NUM_PREDEF_SUBMODULE_IDS) 8623 return LocalID; 8624 8625 if (!M.ModuleOffsetMap.empty()) 8626 ReadModuleOffsetMap(M); 8627 8628 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8629 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS); 8630 assert(I != M.SubmoduleRemap.end() 8631 && "Invalid index into submodule index remap"); 8632 8633 return LocalID + I->second; 8634 } 8635 8636 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) { 8637 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) { 8638 assert(GlobalID == 0 && "Unhandled global submodule ID"); 8639 return nullptr; 8640 } 8641 8642 if (GlobalID > SubmodulesLoaded.size()) { 8643 Error("submodule ID out of range in AST file"); 8644 return nullptr; 8645 } 8646 8647 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS]; 8648 } 8649 8650 Module *ASTReader::getModule(unsigned ID) { 8651 return getSubmodule(ID); 8652 } 8653 8654 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) { 8655 if (ID & 1) { 8656 // It's a module, look it up by submodule ID. 8657 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1)); 8658 return I == GlobalSubmoduleMap.end() ? nullptr : I->second; 8659 } else { 8660 // It's a prefix (preamble, PCH, ...). Look it up by index. 8661 unsigned IndexFromEnd = ID >> 1; 8662 assert(IndexFromEnd && "got reference to unknown module file"); 8663 return getModuleManager().pch_modules().end()[-IndexFromEnd]; 8664 } 8665 } 8666 8667 unsigned ASTReader::getModuleFileID(ModuleFile *F) { 8668 if (!F) 8669 return 1; 8670 8671 // For a file representing a module, use the submodule ID of the top-level 8672 // module as the file ID. For any other kind of file, the number of such 8673 // files loaded beforehand will be the same on reload. 8674 // FIXME: Is this true even if we have an explicit module file and a PCH? 8675 if (F->isModule()) 8676 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1; 8677 8678 auto PCHModules = getModuleManager().pch_modules(); 8679 auto I = llvm::find(PCHModules, F); 8680 assert(I != PCHModules.end() && "emitting reference to unknown file"); 8681 return (I - PCHModules.end()) << 1; 8682 } 8683 8684 llvm::Optional<ASTSourceDescriptor> 8685 ASTReader::getSourceDescriptor(unsigned ID) { 8686 if (Module *M = getSubmodule(ID)) 8687 return ASTSourceDescriptor(*M); 8688 8689 // If there is only a single PCH, return it instead. 8690 // Chained PCH are not supported. 8691 const auto &PCHChain = ModuleMgr.pch_modules(); 8692 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) { 8693 ModuleFile &MF = ModuleMgr.getPrimaryModule(); 8694 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName); 8695 StringRef FileName = llvm::sys::path::filename(MF.FileName); 8696 return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName, 8697 MF.Signature); 8698 } 8699 return None; 8700 } 8701 8702 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) { 8703 auto I = DefinitionSource.find(FD); 8704 if (I == DefinitionSource.end()) 8705 return EK_ReplyHazy; 8706 return I->second ? EK_Never : EK_Always; 8707 } 8708 8709 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) { 8710 return DecodeSelector(getGlobalSelectorID(M, LocalID)); 8711 } 8712 8713 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) { 8714 if (ID == 0) 8715 return Selector(); 8716 8717 if (ID > SelectorsLoaded.size()) { 8718 Error("selector ID out of range in AST file"); 8719 return Selector(); 8720 } 8721 8722 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) { 8723 // Load this selector from the selector table. 8724 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID); 8725 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map"); 8726 ModuleFile &M = *I->second; 8727 ASTSelectorLookupTrait Trait(*this, M); 8728 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS; 8729 SelectorsLoaded[ID - 1] = 8730 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0); 8731 if (DeserializationListener) 8732 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 8733 } 8734 8735 return SelectorsLoaded[ID - 1]; 8736 } 8737 8738 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) { 8739 return DecodeSelector(ID); 8740 } 8741 8742 uint32_t ASTReader::GetNumExternalSelectors() { 8743 // ID 0 (the null selector) is considered an external selector. 8744 return getTotalNumSelectors() + 1; 8745 } 8746 8747 serialization::SelectorID 8748 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const { 8749 if (LocalID < NUM_PREDEF_SELECTOR_IDS) 8750 return LocalID; 8751 8752 if (!M.ModuleOffsetMap.empty()) 8753 ReadModuleOffsetMap(M); 8754 8755 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8756 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS); 8757 assert(I != M.SelectorRemap.end() 8758 && "Invalid index into selector index remap"); 8759 8760 return LocalID + I->second; 8761 } 8762 8763 DeclarationNameLoc 8764 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) { 8765 switch (Name.getNameKind()) { 8766 case DeclarationName::CXXConstructorName: 8767 case DeclarationName::CXXDestructorName: 8768 case DeclarationName::CXXConversionFunctionName: 8769 return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo()); 8770 8771 case DeclarationName::CXXOperatorName: 8772 return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange()); 8773 8774 case DeclarationName::CXXLiteralOperatorName: 8775 return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc( 8776 readSourceLocation()); 8777 8778 case DeclarationName::Identifier: 8779 case DeclarationName::ObjCZeroArgSelector: 8780 case DeclarationName::ObjCOneArgSelector: 8781 case DeclarationName::ObjCMultiArgSelector: 8782 case DeclarationName::CXXUsingDirective: 8783 case DeclarationName::CXXDeductionGuideName: 8784 break; 8785 } 8786 return DeclarationNameLoc(); 8787 } 8788 8789 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() { 8790 DeclarationNameInfo NameInfo; 8791 NameInfo.setName(readDeclarationName()); 8792 NameInfo.setLoc(readSourceLocation()); 8793 NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName())); 8794 return NameInfo; 8795 } 8796 8797 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) { 8798 Info.QualifierLoc = readNestedNameSpecifierLoc(); 8799 unsigned NumTPLists = readInt(); 8800 Info.NumTemplParamLists = NumTPLists; 8801 if (NumTPLists) { 8802 Info.TemplParamLists = 8803 new (getContext()) TemplateParameterList *[NumTPLists]; 8804 for (unsigned i = 0; i != NumTPLists; ++i) 8805 Info.TemplParamLists[i] = readTemplateParameterList(); 8806 } 8807 } 8808 8809 TemplateParameterList * 8810 ASTRecordReader::readTemplateParameterList() { 8811 SourceLocation TemplateLoc = readSourceLocation(); 8812 SourceLocation LAngleLoc = readSourceLocation(); 8813 SourceLocation RAngleLoc = readSourceLocation(); 8814 8815 unsigned NumParams = readInt(); 8816 SmallVector<NamedDecl *, 16> Params; 8817 Params.reserve(NumParams); 8818 while (NumParams--) 8819 Params.push_back(readDeclAs<NamedDecl>()); 8820 8821 bool HasRequiresClause = readBool(); 8822 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr; 8823 8824 TemplateParameterList *TemplateParams = TemplateParameterList::Create( 8825 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause); 8826 return TemplateParams; 8827 } 8828 8829 void ASTRecordReader::readTemplateArgumentList( 8830 SmallVectorImpl<TemplateArgument> &TemplArgs, 8831 bool Canonicalize) { 8832 unsigned NumTemplateArgs = readInt(); 8833 TemplArgs.reserve(NumTemplateArgs); 8834 while (NumTemplateArgs--) 8835 TemplArgs.push_back(readTemplateArgument(Canonicalize)); 8836 } 8837 8838 /// Read a UnresolvedSet structure. 8839 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) { 8840 unsigned NumDecls = readInt(); 8841 Set.reserve(getContext(), NumDecls); 8842 while (NumDecls--) { 8843 DeclID ID = readDeclID(); 8844 AccessSpecifier AS = (AccessSpecifier) readInt(); 8845 Set.addLazyDecl(getContext(), ID, AS); 8846 } 8847 } 8848 8849 CXXBaseSpecifier 8850 ASTRecordReader::readCXXBaseSpecifier() { 8851 bool isVirtual = readBool(); 8852 bool isBaseOfClass = readBool(); 8853 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt()); 8854 bool inheritConstructors = readBool(); 8855 TypeSourceInfo *TInfo = readTypeSourceInfo(); 8856 SourceRange Range = readSourceRange(); 8857 SourceLocation EllipsisLoc = readSourceLocation(); 8858 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo, 8859 EllipsisLoc); 8860 Result.setInheritConstructors(inheritConstructors); 8861 return Result; 8862 } 8863 8864 CXXCtorInitializer ** 8865 ASTRecordReader::readCXXCtorInitializers() { 8866 ASTContext &Context = getContext(); 8867 unsigned NumInitializers = readInt(); 8868 assert(NumInitializers && "wrote ctor initializers but have no inits"); 8869 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers]; 8870 for (unsigned i = 0; i != NumInitializers; ++i) { 8871 TypeSourceInfo *TInfo = nullptr; 8872 bool IsBaseVirtual = false; 8873 FieldDecl *Member = nullptr; 8874 IndirectFieldDecl *IndirectMember = nullptr; 8875 8876 CtorInitializerType Type = (CtorInitializerType) readInt(); 8877 switch (Type) { 8878 case CTOR_INITIALIZER_BASE: 8879 TInfo = readTypeSourceInfo(); 8880 IsBaseVirtual = readBool(); 8881 break; 8882 8883 case CTOR_INITIALIZER_DELEGATING: 8884 TInfo = readTypeSourceInfo(); 8885 break; 8886 8887 case CTOR_INITIALIZER_MEMBER: 8888 Member = readDeclAs<FieldDecl>(); 8889 break; 8890 8891 case CTOR_INITIALIZER_INDIRECT_MEMBER: 8892 IndirectMember = readDeclAs<IndirectFieldDecl>(); 8893 break; 8894 } 8895 8896 SourceLocation MemberOrEllipsisLoc = readSourceLocation(); 8897 Expr *Init = readExpr(); 8898 SourceLocation LParenLoc = readSourceLocation(); 8899 SourceLocation RParenLoc = readSourceLocation(); 8900 8901 CXXCtorInitializer *BOMInit; 8902 if (Type == CTOR_INITIALIZER_BASE) 8903 BOMInit = new (Context) 8904 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init, 8905 RParenLoc, MemberOrEllipsisLoc); 8906 else if (Type == CTOR_INITIALIZER_DELEGATING) 8907 BOMInit = new (Context) 8908 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc); 8909 else if (Member) 8910 BOMInit = new (Context) 8911 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc, 8912 Init, RParenLoc); 8913 else 8914 BOMInit = new (Context) 8915 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc, 8916 LParenLoc, Init, RParenLoc); 8917 8918 if (/*IsWritten*/readBool()) { 8919 unsigned SourceOrder = readInt(); 8920 BOMInit->setSourceOrder(SourceOrder); 8921 } 8922 8923 CtorInitializers[i] = BOMInit; 8924 } 8925 8926 return CtorInitializers; 8927 } 8928 8929 NestedNameSpecifierLoc 8930 ASTRecordReader::readNestedNameSpecifierLoc() { 8931 ASTContext &Context = getContext(); 8932 unsigned N = readInt(); 8933 NestedNameSpecifierLocBuilder Builder; 8934 for (unsigned I = 0; I != N; ++I) { 8935 auto Kind = readNestedNameSpecifierKind(); 8936 switch (Kind) { 8937 case NestedNameSpecifier::Identifier: { 8938 IdentifierInfo *II = readIdentifier(); 8939 SourceRange Range = readSourceRange(); 8940 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd()); 8941 break; 8942 } 8943 8944 case NestedNameSpecifier::Namespace: { 8945 NamespaceDecl *NS = readDeclAs<NamespaceDecl>(); 8946 SourceRange Range = readSourceRange(); 8947 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd()); 8948 break; 8949 } 8950 8951 case NestedNameSpecifier::NamespaceAlias: { 8952 NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>(); 8953 SourceRange Range = readSourceRange(); 8954 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd()); 8955 break; 8956 } 8957 8958 case NestedNameSpecifier::TypeSpec: 8959 case NestedNameSpecifier::TypeSpecWithTemplate: { 8960 bool Template = readBool(); 8961 TypeSourceInfo *T = readTypeSourceInfo(); 8962 if (!T) 8963 return NestedNameSpecifierLoc(); 8964 SourceLocation ColonColonLoc = readSourceLocation(); 8965 8966 // FIXME: 'template' keyword location not saved anywhere, so we fake it. 8967 Builder.Extend(Context, 8968 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(), 8969 T->getTypeLoc(), ColonColonLoc); 8970 break; 8971 } 8972 8973 case NestedNameSpecifier::Global: { 8974 SourceLocation ColonColonLoc = readSourceLocation(); 8975 Builder.MakeGlobal(Context, ColonColonLoc); 8976 break; 8977 } 8978 8979 case NestedNameSpecifier::Super: { 8980 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>(); 8981 SourceRange Range = readSourceRange(); 8982 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd()); 8983 break; 8984 } 8985 } 8986 } 8987 8988 return Builder.getWithLocInContext(Context); 8989 } 8990 8991 SourceRange 8992 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record, 8993 unsigned &Idx) { 8994 SourceLocation beg = ReadSourceLocation(F, Record, Idx); 8995 SourceLocation end = ReadSourceLocation(F, Record, Idx); 8996 return SourceRange(beg, end); 8997 } 8998 8999 /// Read a floating-point value 9000 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) { 9001 return llvm::APFloat(Sem, readAPInt()); 9002 } 9003 9004 // Read a string 9005 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) { 9006 unsigned Len = Record[Idx++]; 9007 std::string Result(Record.data() + Idx, Record.data() + Idx + Len); 9008 Idx += Len; 9009 return Result; 9010 } 9011 9012 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record, 9013 unsigned &Idx) { 9014 std::string Filename = ReadString(Record, Idx); 9015 ResolveImportedPath(F, Filename); 9016 return Filename; 9017 } 9018 9019 std::string ASTReader::ReadPath(StringRef BaseDirectory, 9020 const RecordData &Record, unsigned &Idx) { 9021 std::string Filename = ReadString(Record, Idx); 9022 if (!BaseDirectory.empty()) 9023 ResolveImportedPath(Filename, BaseDirectory); 9024 return Filename; 9025 } 9026 9027 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record, 9028 unsigned &Idx) { 9029 unsigned Major = Record[Idx++]; 9030 unsigned Minor = Record[Idx++]; 9031 unsigned Subminor = Record[Idx++]; 9032 if (Minor == 0) 9033 return VersionTuple(Major); 9034 if (Subminor == 0) 9035 return VersionTuple(Major, Minor - 1); 9036 return VersionTuple(Major, Minor - 1, Subminor - 1); 9037 } 9038 9039 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F, 9040 const RecordData &Record, 9041 unsigned &Idx) { 9042 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx); 9043 return CXXTemporary::Create(getContext(), Decl); 9044 } 9045 9046 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const { 9047 return Diag(CurrentImportLoc, DiagID); 9048 } 9049 9050 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const { 9051 return Diags.Report(Loc, DiagID); 9052 } 9053 9054 /// Retrieve the identifier table associated with the 9055 /// preprocessor. 9056 IdentifierTable &ASTReader::getIdentifierTable() { 9057 return PP.getIdentifierTable(); 9058 } 9059 9060 /// Record that the given ID maps to the given switch-case 9061 /// statement. 9062 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { 9063 assert((*CurrSwitchCaseStmts)[ID] == nullptr && 9064 "Already have a SwitchCase with this ID"); 9065 (*CurrSwitchCaseStmts)[ID] = SC; 9066 } 9067 9068 /// Retrieve the switch-case statement with the given ID. 9069 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) { 9070 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID"); 9071 return (*CurrSwitchCaseStmts)[ID]; 9072 } 9073 9074 void ASTReader::ClearSwitchCaseIDs() { 9075 CurrSwitchCaseStmts->clear(); 9076 } 9077 9078 void ASTReader::ReadComments() { 9079 ASTContext &Context = getContext(); 9080 std::vector<RawComment *> Comments; 9081 for (SmallVectorImpl<std::pair<BitstreamCursor, 9082 serialization::ModuleFile *>>::iterator 9083 I = CommentsCursors.begin(), 9084 E = CommentsCursors.end(); 9085 I != E; ++I) { 9086 Comments.clear(); 9087 BitstreamCursor &Cursor = I->first; 9088 serialization::ModuleFile &F = *I->second; 9089 SavedStreamPosition SavedPosition(Cursor); 9090 9091 RecordData Record; 9092 while (true) { 9093 Expected<llvm::BitstreamEntry> MaybeEntry = 9094 Cursor.advanceSkippingSubblocks( 9095 BitstreamCursor::AF_DontPopBlockAtEnd); 9096 if (!MaybeEntry) { 9097 Error(MaybeEntry.takeError()); 9098 return; 9099 } 9100 llvm::BitstreamEntry Entry = MaybeEntry.get(); 9101 9102 switch (Entry.Kind) { 9103 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 9104 case llvm::BitstreamEntry::Error: 9105 Error("malformed block record in AST file"); 9106 return; 9107 case llvm::BitstreamEntry::EndBlock: 9108 goto NextCursor; 9109 case llvm::BitstreamEntry::Record: 9110 // The interesting case. 9111 break; 9112 } 9113 9114 // Read a record. 9115 Record.clear(); 9116 Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record); 9117 if (!MaybeComment) { 9118 Error(MaybeComment.takeError()); 9119 return; 9120 } 9121 switch ((CommentRecordTypes)MaybeComment.get()) { 9122 case COMMENTS_RAW_COMMENT: { 9123 unsigned Idx = 0; 9124 SourceRange SR = ReadSourceRange(F, Record, Idx); 9125 RawComment::CommentKind Kind = 9126 (RawComment::CommentKind) Record[Idx++]; 9127 bool IsTrailingComment = Record[Idx++]; 9128 bool IsAlmostTrailingComment = Record[Idx++]; 9129 Comments.push_back(new (Context) RawComment( 9130 SR, Kind, IsTrailingComment, IsAlmostTrailingComment)); 9131 break; 9132 } 9133 } 9134 } 9135 NextCursor: 9136 llvm::DenseMap<FileID, std::map<unsigned, RawComment *>> 9137 FileToOffsetToComment; 9138 for (RawComment *C : Comments) { 9139 SourceLocation CommentLoc = C->getBeginLoc(); 9140 if (CommentLoc.isValid()) { 9141 std::pair<FileID, unsigned> Loc = 9142 SourceMgr.getDecomposedLoc(CommentLoc); 9143 if (Loc.first.isValid()) 9144 Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C); 9145 } 9146 } 9147 } 9148 } 9149 9150 void ASTReader::visitInputFiles(serialization::ModuleFile &MF, 9151 bool IncludeSystem, bool Complain, 9152 llvm::function_ref<void(const serialization::InputFile &IF, 9153 bool isSystem)> Visitor) { 9154 unsigned NumUserInputs = MF.NumUserInputFiles; 9155 unsigned NumInputs = MF.InputFilesLoaded.size(); 9156 assert(NumUserInputs <= NumInputs); 9157 unsigned N = IncludeSystem ? NumInputs : NumUserInputs; 9158 for (unsigned I = 0; I < N; ++I) { 9159 bool IsSystem = I >= NumUserInputs; 9160 InputFile IF = getInputFile(MF, I+1, Complain); 9161 Visitor(IF, IsSystem); 9162 } 9163 } 9164 9165 void ASTReader::visitTopLevelModuleMaps( 9166 serialization::ModuleFile &MF, 9167 llvm::function_ref<void(const FileEntry *FE)> Visitor) { 9168 unsigned NumInputs = MF.InputFilesLoaded.size(); 9169 for (unsigned I = 0; I < NumInputs; ++I) { 9170 InputFileInfo IFI = readInputFileInfo(MF, I + 1); 9171 if (IFI.TopLevelModuleMap) 9172 // FIXME: This unnecessarily re-reads the InputFileInfo. 9173 if (auto FE = getInputFile(MF, I + 1).getFile()) 9174 Visitor(FE); 9175 } 9176 } 9177 9178 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) { 9179 // If we know the owning module, use it. 9180 if (Module *M = D->getImportedOwningModule()) 9181 return M->getFullModuleName(); 9182 9183 // Otherwise, use the name of the top-level module the decl is within. 9184 if (ModuleFile *M = getOwningModuleFile(D)) 9185 return M->ModuleName; 9186 9187 // Not from a module. 9188 return {}; 9189 } 9190 9191 void ASTReader::finishPendingActions() { 9192 while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() || 9193 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() || 9194 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() || 9195 !PendingUpdateRecords.empty()) { 9196 // If any identifiers with corresponding top-level declarations have 9197 // been loaded, load those declarations now. 9198 using TopLevelDeclsMap = 9199 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>; 9200 TopLevelDeclsMap TopLevelDecls; 9201 9202 while (!PendingIdentifierInfos.empty()) { 9203 IdentifierInfo *II = PendingIdentifierInfos.back().first; 9204 SmallVector<uint32_t, 4> DeclIDs = 9205 std::move(PendingIdentifierInfos.back().second); 9206 PendingIdentifierInfos.pop_back(); 9207 9208 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]); 9209 } 9210 9211 // Load each function type that we deferred loading because it was a 9212 // deduced type that might refer to a local type declared within itself. 9213 for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) { 9214 auto *FD = PendingFunctionTypes[I].first; 9215 FD->setType(GetType(PendingFunctionTypes[I].second)); 9216 9217 // If we gave a function a deduced return type, remember that we need to 9218 // propagate that along the redeclaration chain. 9219 auto *DT = FD->getReturnType()->getContainedDeducedType(); 9220 if (DT && DT->isDeduced()) 9221 PendingDeducedTypeUpdates.insert( 9222 {FD->getCanonicalDecl(), FD->getReturnType()}); 9223 } 9224 PendingFunctionTypes.clear(); 9225 9226 // For each decl chain that we wanted to complete while deserializing, mark 9227 // it as "still needs to be completed". 9228 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) { 9229 markIncompleteDeclChain(PendingIncompleteDeclChains[I]); 9230 } 9231 PendingIncompleteDeclChains.clear(); 9232 9233 // Load pending declaration chains. 9234 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) 9235 loadPendingDeclChain(PendingDeclChains[I].first, 9236 PendingDeclChains[I].second); 9237 PendingDeclChains.clear(); 9238 9239 // Make the most recent of the top-level declarations visible. 9240 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(), 9241 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) { 9242 IdentifierInfo *II = TLD->first; 9243 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) { 9244 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II); 9245 } 9246 } 9247 9248 // Load any pending macro definitions. 9249 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) { 9250 IdentifierInfo *II = PendingMacroIDs.begin()[I].first; 9251 SmallVector<PendingMacroInfo, 2> GlobalIDs; 9252 GlobalIDs.swap(PendingMacroIDs.begin()[I].second); 9253 // Initialize the macro history from chained-PCHs ahead of module imports. 9254 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9255 ++IDIdx) { 9256 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9257 if (!Info.M->isModule()) 9258 resolvePendingMacro(II, Info); 9259 } 9260 // Handle module imports. 9261 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9262 ++IDIdx) { 9263 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9264 if (Info.M->isModule()) 9265 resolvePendingMacro(II, Info); 9266 } 9267 } 9268 PendingMacroIDs.clear(); 9269 9270 // Wire up the DeclContexts for Decls that we delayed setting until 9271 // recursive loading is completed. 9272 while (!PendingDeclContextInfos.empty()) { 9273 PendingDeclContextInfo Info = PendingDeclContextInfos.front(); 9274 PendingDeclContextInfos.pop_front(); 9275 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC)); 9276 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC)); 9277 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext()); 9278 } 9279 9280 // Perform any pending declaration updates. 9281 while (!PendingUpdateRecords.empty()) { 9282 auto Update = PendingUpdateRecords.pop_back_val(); 9283 ReadingKindTracker ReadingKind(Read_Decl, *this); 9284 loadDeclUpdateRecords(Update); 9285 } 9286 } 9287 9288 // At this point, all update records for loaded decls are in place, so any 9289 // fake class definitions should have become real. 9290 assert(PendingFakeDefinitionData.empty() && 9291 "faked up a class definition but never saw the real one"); 9292 9293 // If we deserialized any C++ or Objective-C class definitions, any 9294 // Objective-C protocol definitions, or any redeclarable templates, make sure 9295 // that all redeclarations point to the definitions. Note that this can only 9296 // happen now, after the redeclaration chains have been fully wired. 9297 for (Decl *D : PendingDefinitions) { 9298 if (TagDecl *TD = dyn_cast<TagDecl>(D)) { 9299 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) { 9300 // Make sure that the TagType points at the definition. 9301 const_cast<TagType*>(TagT)->decl = TD; 9302 } 9303 9304 if (auto RD = dyn_cast<CXXRecordDecl>(D)) { 9305 for (auto *R = getMostRecentExistingDecl(RD); R; 9306 R = R->getPreviousDecl()) { 9307 assert((R == D) == 9308 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() && 9309 "declaration thinks it's the definition but it isn't"); 9310 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData; 9311 } 9312 } 9313 9314 continue; 9315 } 9316 9317 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) { 9318 // Make sure that the ObjCInterfaceType points at the definition. 9319 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl)) 9320 ->Decl = ID; 9321 9322 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl()) 9323 cast<ObjCInterfaceDecl>(R)->Data = ID->Data; 9324 9325 continue; 9326 } 9327 9328 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) { 9329 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl()) 9330 cast<ObjCProtocolDecl>(R)->Data = PD->Data; 9331 9332 continue; 9333 } 9334 9335 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl(); 9336 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl()) 9337 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common; 9338 } 9339 PendingDefinitions.clear(); 9340 9341 // Load the bodies of any functions or methods we've encountered. We do 9342 // this now (delayed) so that we can be sure that the declaration chains 9343 // have been fully wired up (hasBody relies on this). 9344 // FIXME: We shouldn't require complete redeclaration chains here. 9345 for (PendingBodiesMap::iterator PB = PendingBodies.begin(), 9346 PBEnd = PendingBodies.end(); 9347 PB != PBEnd; ++PB) { 9348 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) { 9349 // For a function defined inline within a class template, force the 9350 // canonical definition to be the one inside the canonical definition of 9351 // the template. This ensures that we instantiate from a correct view 9352 // of the template. 9353 // 9354 // Sadly we can't do this more generally: we can't be sure that all 9355 // copies of an arbitrary class definition will have the same members 9356 // defined (eg, some member functions may not be instantiated, and some 9357 // special members may or may not have been implicitly defined). 9358 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent())) 9359 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition()) 9360 continue; 9361 9362 // FIXME: Check for =delete/=default? 9363 // FIXME: Complain about ODR violations here? 9364 const FunctionDecl *Defn = nullptr; 9365 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) { 9366 FD->setLazyBody(PB->second); 9367 } else { 9368 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn); 9369 mergeDefinitionVisibility(NonConstDefn, FD); 9370 9371 if (!FD->isLateTemplateParsed() && 9372 !NonConstDefn->isLateTemplateParsed() && 9373 FD->getODRHash() != NonConstDefn->getODRHash()) { 9374 if (!isa<CXXMethodDecl>(FD)) { 9375 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9376 } else if (FD->getLexicalParent()->isFileContext() && 9377 NonConstDefn->getLexicalParent()->isFileContext()) { 9378 // Only diagnose out-of-line method definitions. If they are 9379 // in class definitions, then an error will be generated when 9380 // processing the class bodies. 9381 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9382 } 9383 } 9384 } 9385 continue; 9386 } 9387 9388 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first); 9389 if (!getContext().getLangOpts().Modules || !MD->hasBody()) 9390 MD->setLazyBody(PB->second); 9391 } 9392 PendingBodies.clear(); 9393 9394 // Do some cleanup. 9395 for (auto *ND : PendingMergedDefinitionsToDeduplicate) 9396 getContext().deduplicateMergedDefinitonsFor(ND); 9397 PendingMergedDefinitionsToDeduplicate.clear(); 9398 } 9399 9400 void ASTReader::diagnoseOdrViolations() { 9401 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() && 9402 PendingFunctionOdrMergeFailures.empty() && 9403 PendingEnumOdrMergeFailures.empty()) 9404 return; 9405 9406 // Trigger the import of the full definition of each class that had any 9407 // odr-merging problems, so we can produce better diagnostics for them. 9408 // These updates may in turn find and diagnose some ODR failures, so take 9409 // ownership of the set first. 9410 auto OdrMergeFailures = std::move(PendingOdrMergeFailures); 9411 PendingOdrMergeFailures.clear(); 9412 for (auto &Merge : OdrMergeFailures) { 9413 Merge.first->buildLookup(); 9414 Merge.first->decls_begin(); 9415 Merge.first->bases_begin(); 9416 Merge.first->vbases_begin(); 9417 for (auto &RecordPair : Merge.second) { 9418 auto *RD = RecordPair.first; 9419 RD->decls_begin(); 9420 RD->bases_begin(); 9421 RD->vbases_begin(); 9422 } 9423 } 9424 9425 // Trigger the import of functions. 9426 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures); 9427 PendingFunctionOdrMergeFailures.clear(); 9428 for (auto &Merge : FunctionOdrMergeFailures) { 9429 Merge.first->buildLookup(); 9430 Merge.first->decls_begin(); 9431 Merge.first->getBody(); 9432 for (auto &FD : Merge.second) { 9433 FD->buildLookup(); 9434 FD->decls_begin(); 9435 FD->getBody(); 9436 } 9437 } 9438 9439 // Trigger the import of enums. 9440 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures); 9441 PendingEnumOdrMergeFailures.clear(); 9442 for (auto &Merge : EnumOdrMergeFailures) { 9443 Merge.first->decls_begin(); 9444 for (auto &Enum : Merge.second) { 9445 Enum->decls_begin(); 9446 } 9447 } 9448 9449 // For each declaration from a merged context, check that the canonical 9450 // definition of that context also contains a declaration of the same 9451 // entity. 9452 // 9453 // Caution: this loop does things that might invalidate iterators into 9454 // PendingOdrMergeChecks. Don't turn this into a range-based for loop! 9455 while (!PendingOdrMergeChecks.empty()) { 9456 NamedDecl *D = PendingOdrMergeChecks.pop_back_val(); 9457 9458 // FIXME: Skip over implicit declarations for now. This matters for things 9459 // like implicitly-declared special member functions. This isn't entirely 9460 // correct; we can end up with multiple unmerged declarations of the same 9461 // implicit entity. 9462 if (D->isImplicit()) 9463 continue; 9464 9465 DeclContext *CanonDef = D->getDeclContext(); 9466 9467 bool Found = false; 9468 const Decl *DCanon = D->getCanonicalDecl(); 9469 9470 for (auto RI : D->redecls()) { 9471 if (RI->getLexicalDeclContext() == CanonDef) { 9472 Found = true; 9473 break; 9474 } 9475 } 9476 if (Found) 9477 continue; 9478 9479 // Quick check failed, time to do the slow thing. Note, we can't just 9480 // look up the name of D in CanonDef here, because the member that is 9481 // in CanonDef might not be found by name lookup (it might have been 9482 // replaced by a more recent declaration in the lookup table), and we 9483 // can't necessarily find it in the redeclaration chain because it might 9484 // be merely mergeable, not redeclarable. 9485 llvm::SmallVector<const NamedDecl*, 4> Candidates; 9486 for (auto *CanonMember : CanonDef->decls()) { 9487 if (CanonMember->getCanonicalDecl() == DCanon) { 9488 // This can happen if the declaration is merely mergeable and not 9489 // actually redeclarable (we looked for redeclarations earlier). 9490 // 9491 // FIXME: We should be able to detect this more efficiently, without 9492 // pulling in all of the members of CanonDef. 9493 Found = true; 9494 break; 9495 } 9496 if (auto *ND = dyn_cast<NamedDecl>(CanonMember)) 9497 if (ND->getDeclName() == D->getDeclName()) 9498 Candidates.push_back(ND); 9499 } 9500 9501 if (!Found) { 9502 // The AST doesn't like TagDecls becoming invalid after they've been 9503 // completed. We only really need to mark FieldDecls as invalid here. 9504 if (!isa<TagDecl>(D)) 9505 D->setInvalidDecl(); 9506 9507 // Ensure we don't accidentally recursively enter deserialization while 9508 // we're producing our diagnostic. 9509 Deserializing RecursionGuard(this); 9510 9511 std::string CanonDefModule = 9512 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef)); 9513 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl) 9514 << D << getOwningModuleNameForDiagnostic(D) 9515 << CanonDef << CanonDefModule.empty() << CanonDefModule; 9516 9517 if (Candidates.empty()) 9518 Diag(cast<Decl>(CanonDef)->getLocation(), 9519 diag::note_module_odr_violation_no_possible_decls) << D; 9520 else { 9521 for (unsigned I = 0, N = Candidates.size(); I != N; ++I) 9522 Diag(Candidates[I]->getLocation(), 9523 diag::note_module_odr_violation_possible_decl) 9524 << Candidates[I]; 9525 } 9526 9527 DiagnosedOdrMergeFailures.insert(CanonDef); 9528 } 9529 } 9530 9531 if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() && 9532 EnumOdrMergeFailures.empty()) 9533 return; 9534 9535 // Ensure we don't accidentally recursively enter deserialization while 9536 // we're producing our diagnostics. 9537 Deserializing RecursionGuard(this); 9538 9539 // Common code for hashing helpers. 9540 ODRHash Hash; 9541 auto ComputeQualTypeODRHash = [&Hash](QualType Ty) { 9542 Hash.clear(); 9543 Hash.AddQualType(Ty); 9544 return Hash.CalculateHash(); 9545 }; 9546 9547 auto ComputeODRHash = [&Hash](const Stmt *S) { 9548 assert(S); 9549 Hash.clear(); 9550 Hash.AddStmt(S); 9551 return Hash.CalculateHash(); 9552 }; 9553 9554 auto ComputeSubDeclODRHash = [&Hash](const Decl *D) { 9555 assert(D); 9556 Hash.clear(); 9557 Hash.AddSubDecl(D); 9558 return Hash.CalculateHash(); 9559 }; 9560 9561 auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) { 9562 Hash.clear(); 9563 Hash.AddTemplateArgument(TA); 9564 return Hash.CalculateHash(); 9565 }; 9566 9567 auto ComputeTemplateParameterListODRHash = 9568 [&Hash](const TemplateParameterList *TPL) { 9569 assert(TPL); 9570 Hash.clear(); 9571 Hash.AddTemplateParameterList(TPL); 9572 return Hash.CalculateHash(); 9573 }; 9574 9575 // Used with err_module_odr_violation_mismatch_decl and 9576 // note_module_odr_violation_mismatch_decl 9577 // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed 9578 enum ODRMismatchDecl { 9579 EndOfClass, 9580 PublicSpecifer, 9581 PrivateSpecifer, 9582 ProtectedSpecifer, 9583 StaticAssert, 9584 Field, 9585 CXXMethod, 9586 TypeAlias, 9587 TypeDef, 9588 Var, 9589 Friend, 9590 FunctionTemplate, 9591 Other 9592 }; 9593 9594 // Used with err_module_odr_violation_mismatch_decl_diff and 9595 // note_module_odr_violation_mismatch_decl_diff 9596 enum ODRMismatchDeclDifference { 9597 StaticAssertCondition, 9598 StaticAssertMessage, 9599 StaticAssertOnlyMessage, 9600 FieldName, 9601 FieldTypeName, 9602 FieldSingleBitField, 9603 FieldDifferentWidthBitField, 9604 FieldSingleMutable, 9605 FieldSingleInitializer, 9606 FieldDifferentInitializers, 9607 MethodName, 9608 MethodDeleted, 9609 MethodDefaulted, 9610 MethodVirtual, 9611 MethodStatic, 9612 MethodVolatile, 9613 MethodConst, 9614 MethodInline, 9615 MethodNumberParameters, 9616 MethodParameterType, 9617 MethodParameterName, 9618 MethodParameterSingleDefaultArgument, 9619 MethodParameterDifferentDefaultArgument, 9620 MethodNoTemplateArguments, 9621 MethodDifferentNumberTemplateArguments, 9622 MethodDifferentTemplateArgument, 9623 MethodSingleBody, 9624 MethodDifferentBody, 9625 TypedefName, 9626 TypedefType, 9627 VarName, 9628 VarType, 9629 VarSingleInitializer, 9630 VarDifferentInitializer, 9631 VarConstexpr, 9632 FriendTypeFunction, 9633 FriendType, 9634 FriendFunction, 9635 FunctionTemplateDifferentNumberParameters, 9636 FunctionTemplateParameterDifferentKind, 9637 FunctionTemplateParameterName, 9638 FunctionTemplateParameterSingleDefaultArgument, 9639 FunctionTemplateParameterDifferentDefaultArgument, 9640 FunctionTemplateParameterDifferentType, 9641 FunctionTemplatePackParameter, 9642 }; 9643 9644 // These lambdas have the common portions of the ODR diagnostics. This 9645 // has the same return as Diag(), so addition parameters can be passed 9646 // in with operator<< 9647 auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule, 9648 SourceLocation Loc, SourceRange Range, 9649 ODRMismatchDeclDifference DiffType) { 9650 return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff) 9651 << FirstRecord << FirstModule.empty() << FirstModule << Range 9652 << DiffType; 9653 }; 9654 auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc, 9655 SourceRange Range, ODRMismatchDeclDifference DiffType) { 9656 return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff) 9657 << SecondModule << Range << DiffType; 9658 }; 9659 9660 auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote, 9661 &ComputeQualTypeODRHash, &ComputeODRHash]( 9662 NamedDecl *FirstRecord, StringRef FirstModule, 9663 StringRef SecondModule, FieldDecl *FirstField, 9664 FieldDecl *SecondField) { 9665 IdentifierInfo *FirstII = FirstField->getIdentifier(); 9666 IdentifierInfo *SecondII = SecondField->getIdentifier(); 9667 if (FirstII->getName() != SecondII->getName()) { 9668 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9669 FirstField->getSourceRange(), FieldName) 9670 << FirstII; 9671 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9672 SecondField->getSourceRange(), FieldName) 9673 << SecondII; 9674 9675 return true; 9676 } 9677 9678 assert(getContext().hasSameType(FirstField->getType(), 9679 SecondField->getType())); 9680 9681 QualType FirstType = FirstField->getType(); 9682 QualType SecondType = SecondField->getType(); 9683 if (ComputeQualTypeODRHash(FirstType) != 9684 ComputeQualTypeODRHash(SecondType)) { 9685 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9686 FirstField->getSourceRange(), FieldTypeName) 9687 << FirstII << FirstType; 9688 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9689 SecondField->getSourceRange(), FieldTypeName) 9690 << SecondII << SecondType; 9691 9692 return true; 9693 } 9694 9695 const bool IsFirstBitField = FirstField->isBitField(); 9696 const bool IsSecondBitField = SecondField->isBitField(); 9697 if (IsFirstBitField != IsSecondBitField) { 9698 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9699 FirstField->getSourceRange(), FieldSingleBitField) 9700 << FirstII << IsFirstBitField; 9701 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9702 SecondField->getSourceRange(), FieldSingleBitField) 9703 << SecondII << IsSecondBitField; 9704 return true; 9705 } 9706 9707 if (IsFirstBitField && IsSecondBitField) { 9708 unsigned FirstBitWidthHash = 9709 ComputeODRHash(FirstField->getBitWidth()); 9710 unsigned SecondBitWidthHash = 9711 ComputeODRHash(SecondField->getBitWidth()); 9712 if (FirstBitWidthHash != SecondBitWidthHash) { 9713 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9714 FirstField->getSourceRange(), 9715 FieldDifferentWidthBitField) 9716 << FirstII << FirstField->getBitWidth()->getSourceRange(); 9717 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9718 SecondField->getSourceRange(), 9719 FieldDifferentWidthBitField) 9720 << SecondII << SecondField->getBitWidth()->getSourceRange(); 9721 return true; 9722 } 9723 } 9724 9725 if (!PP.getLangOpts().CPlusPlus) 9726 return false; 9727 9728 const bool IsFirstMutable = FirstField->isMutable(); 9729 const bool IsSecondMutable = SecondField->isMutable(); 9730 if (IsFirstMutable != IsSecondMutable) { 9731 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9732 FirstField->getSourceRange(), FieldSingleMutable) 9733 << FirstII << IsFirstMutable; 9734 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9735 SecondField->getSourceRange(), FieldSingleMutable) 9736 << SecondII << IsSecondMutable; 9737 return true; 9738 } 9739 9740 const Expr *FirstInitializer = FirstField->getInClassInitializer(); 9741 const Expr *SecondInitializer = SecondField->getInClassInitializer(); 9742 if ((!FirstInitializer && SecondInitializer) || 9743 (FirstInitializer && !SecondInitializer)) { 9744 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9745 FirstField->getSourceRange(), FieldSingleInitializer) 9746 << FirstII << (FirstInitializer != nullptr); 9747 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9748 SecondField->getSourceRange(), FieldSingleInitializer) 9749 << SecondII << (SecondInitializer != nullptr); 9750 return true; 9751 } 9752 9753 if (FirstInitializer && SecondInitializer) { 9754 unsigned FirstInitHash = ComputeODRHash(FirstInitializer); 9755 unsigned SecondInitHash = ComputeODRHash(SecondInitializer); 9756 if (FirstInitHash != SecondInitHash) { 9757 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9758 FirstField->getSourceRange(), 9759 FieldDifferentInitializers) 9760 << FirstII << FirstInitializer->getSourceRange(); 9761 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9762 SecondField->getSourceRange(), 9763 FieldDifferentInitializers) 9764 << SecondII << SecondInitializer->getSourceRange(); 9765 return true; 9766 } 9767 } 9768 9769 return false; 9770 }; 9771 9772 auto ODRDiagTypeDefOrAlias = 9773 [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash]( 9774 NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule, 9775 TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD, 9776 bool IsTypeAlias) { 9777 auto FirstName = FirstTD->getDeclName(); 9778 auto SecondName = SecondTD->getDeclName(); 9779 if (FirstName != SecondName) { 9780 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(), 9781 FirstTD->getSourceRange(), TypedefName) 9782 << IsTypeAlias << FirstName; 9783 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(), 9784 SecondTD->getSourceRange(), TypedefName) 9785 << IsTypeAlias << SecondName; 9786 return true; 9787 } 9788 9789 QualType FirstType = FirstTD->getUnderlyingType(); 9790 QualType SecondType = SecondTD->getUnderlyingType(); 9791 if (ComputeQualTypeODRHash(FirstType) != 9792 ComputeQualTypeODRHash(SecondType)) { 9793 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(), 9794 FirstTD->getSourceRange(), TypedefType) 9795 << IsTypeAlias << FirstName << FirstType; 9796 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(), 9797 SecondTD->getSourceRange(), TypedefType) 9798 << IsTypeAlias << SecondName << SecondType; 9799 return true; 9800 } 9801 9802 return false; 9803 }; 9804 9805 auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote, 9806 &ComputeQualTypeODRHash, &ComputeODRHash, 9807 this](NamedDecl *FirstRecord, StringRef FirstModule, 9808 StringRef SecondModule, VarDecl *FirstVD, 9809 VarDecl *SecondVD) { 9810 auto FirstName = FirstVD->getDeclName(); 9811 auto SecondName = SecondVD->getDeclName(); 9812 if (FirstName != SecondName) { 9813 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9814 FirstVD->getSourceRange(), VarName) 9815 << FirstName; 9816 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9817 SecondVD->getSourceRange(), VarName) 9818 << SecondName; 9819 return true; 9820 } 9821 9822 QualType FirstType = FirstVD->getType(); 9823 QualType SecondType = SecondVD->getType(); 9824 if (ComputeQualTypeODRHash(FirstType) != 9825 ComputeQualTypeODRHash(SecondType)) { 9826 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9827 FirstVD->getSourceRange(), VarType) 9828 << FirstName << FirstType; 9829 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9830 SecondVD->getSourceRange(), VarType) 9831 << SecondName << SecondType; 9832 return true; 9833 } 9834 9835 if (!PP.getLangOpts().CPlusPlus) 9836 return false; 9837 9838 const Expr *FirstInit = FirstVD->getInit(); 9839 const Expr *SecondInit = SecondVD->getInit(); 9840 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 9841 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9842 FirstVD->getSourceRange(), VarSingleInitializer) 9843 << FirstName << (FirstInit == nullptr) 9844 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 9845 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9846 SecondVD->getSourceRange(), VarSingleInitializer) 9847 << SecondName << (SecondInit == nullptr) 9848 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 9849 return true; 9850 } 9851 9852 if (FirstInit && SecondInit && 9853 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 9854 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9855 FirstVD->getSourceRange(), VarDifferentInitializer) 9856 << FirstName << FirstInit->getSourceRange(); 9857 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9858 SecondVD->getSourceRange(), VarDifferentInitializer) 9859 << SecondName << SecondInit->getSourceRange(); 9860 return true; 9861 } 9862 9863 const bool FirstIsConstexpr = FirstVD->isConstexpr(); 9864 const bool SecondIsConstexpr = SecondVD->isConstexpr(); 9865 if (FirstIsConstexpr != SecondIsConstexpr) { 9866 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9867 FirstVD->getSourceRange(), VarConstexpr) 9868 << FirstName << FirstIsConstexpr; 9869 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9870 SecondVD->getSourceRange(), VarConstexpr) 9871 << SecondName << SecondIsConstexpr; 9872 return true; 9873 } 9874 return false; 9875 }; 9876 9877 auto DifferenceSelector = [](Decl *D) { 9878 assert(D && "valid Decl required"); 9879 switch (D->getKind()) { 9880 default: 9881 return Other; 9882 case Decl::AccessSpec: 9883 switch (D->getAccess()) { 9884 case AS_public: 9885 return PublicSpecifer; 9886 case AS_private: 9887 return PrivateSpecifer; 9888 case AS_protected: 9889 return ProtectedSpecifer; 9890 case AS_none: 9891 break; 9892 } 9893 llvm_unreachable("Invalid access specifier"); 9894 case Decl::StaticAssert: 9895 return StaticAssert; 9896 case Decl::Field: 9897 return Field; 9898 case Decl::CXXMethod: 9899 case Decl::CXXConstructor: 9900 case Decl::CXXDestructor: 9901 return CXXMethod; 9902 case Decl::TypeAlias: 9903 return TypeAlias; 9904 case Decl::Typedef: 9905 return TypeDef; 9906 case Decl::Var: 9907 return Var; 9908 case Decl::Friend: 9909 return Friend; 9910 case Decl::FunctionTemplate: 9911 return FunctionTemplate; 9912 } 9913 }; 9914 9915 using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>; 9916 auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes, 9917 RecordDecl *Record, 9918 const DeclContext *DC) { 9919 for (auto *D : Record->decls()) { 9920 if (!ODRHash::isDeclToBeProcessed(D, DC)) 9921 continue; 9922 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 9923 } 9924 }; 9925 9926 struct DiffResult { 9927 Decl *FirstDecl = nullptr, *SecondDecl = nullptr; 9928 ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other; 9929 }; 9930 9931 // If there is a diagnoseable difference, FirstDiffType and 9932 // SecondDiffType will not be Other and FirstDecl and SecondDecl will be 9933 // filled in if not EndOfClass. 9934 auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes, 9935 DeclHashes &SecondHashes) { 9936 DiffResult DR; 9937 auto FirstIt = FirstHashes.begin(); 9938 auto SecondIt = SecondHashes.begin(); 9939 while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) { 9940 if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() && 9941 FirstIt->second == SecondIt->second) { 9942 ++FirstIt; 9943 ++SecondIt; 9944 continue; 9945 } 9946 9947 DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first; 9948 DR.SecondDecl = 9949 SecondIt == SecondHashes.end() ? nullptr : SecondIt->first; 9950 9951 DR.FirstDiffType = 9952 DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass; 9953 DR.SecondDiffType = 9954 DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass; 9955 return DR; 9956 } 9957 return DR; 9958 }; 9959 9960 // Use this to diagnose that an unexpected Decl was encountered 9961 // or no difference was detected. This causes a generic error 9962 // message to be emitted. 9963 auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord, 9964 StringRef FirstModule, 9965 NamedDecl *SecondRecord, 9966 StringRef SecondModule) { 9967 Diag(FirstRecord->getLocation(), 9968 diag::err_module_odr_violation_different_definitions) 9969 << FirstRecord << FirstModule.empty() << FirstModule; 9970 9971 if (DR.FirstDecl) { 9972 Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference) 9973 << FirstRecord << DR.FirstDecl->getSourceRange(); 9974 } 9975 9976 Diag(SecondRecord->getLocation(), 9977 diag::note_module_odr_violation_different_definitions) 9978 << SecondModule; 9979 9980 if (DR.SecondDecl) { 9981 Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference) 9982 << DR.SecondDecl->getSourceRange(); 9983 } 9984 }; 9985 9986 auto DiagnoseODRMismatch = 9987 [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule, 9988 NamedDecl *SecondRecord, StringRef SecondModule) { 9989 SourceLocation FirstLoc; 9990 SourceRange FirstRange; 9991 auto *FirstTag = dyn_cast<TagDecl>(FirstRecord); 9992 if (DR.FirstDiffType == EndOfClass && FirstTag) { 9993 FirstLoc = FirstTag->getBraceRange().getEnd(); 9994 } else { 9995 FirstLoc = DR.FirstDecl->getLocation(); 9996 FirstRange = DR.FirstDecl->getSourceRange(); 9997 } 9998 Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl) 9999 << FirstRecord << FirstModule.empty() << FirstModule << FirstRange 10000 << DR.FirstDiffType; 10001 10002 SourceLocation SecondLoc; 10003 SourceRange SecondRange; 10004 auto *SecondTag = dyn_cast<TagDecl>(SecondRecord); 10005 if (DR.SecondDiffType == EndOfClass && SecondTag) { 10006 SecondLoc = SecondTag->getBraceRange().getEnd(); 10007 } else { 10008 SecondLoc = DR.SecondDecl->getLocation(); 10009 SecondRange = DR.SecondDecl->getSourceRange(); 10010 } 10011 Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl) 10012 << SecondModule << SecondRange << DR.SecondDiffType; 10013 }; 10014 10015 // Issue any pending ODR-failure diagnostics. 10016 for (auto &Merge : OdrMergeFailures) { 10017 // If we've already pointed out a specific problem with this class, don't 10018 // bother issuing a general "something's different" diagnostic. 10019 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 10020 continue; 10021 10022 bool Diagnosed = false; 10023 CXXRecordDecl *FirstRecord = Merge.first; 10024 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord); 10025 for (auto &RecordPair : Merge.second) { 10026 CXXRecordDecl *SecondRecord = RecordPair.first; 10027 // Multiple different declarations got merged together; tell the user 10028 // where they came from. 10029 if (FirstRecord == SecondRecord) 10030 continue; 10031 10032 std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord); 10033 10034 auto *FirstDD = FirstRecord->DefinitionData; 10035 auto *SecondDD = RecordPair.second; 10036 10037 assert(FirstDD && SecondDD && "Definitions without DefinitionData"); 10038 10039 // Diagnostics from DefinitionData are emitted here. 10040 if (FirstDD != SecondDD) { 10041 enum ODRDefinitionDataDifference { 10042 NumBases, 10043 NumVBases, 10044 BaseType, 10045 BaseVirtual, 10046 BaseAccess, 10047 }; 10048 auto ODRDiagBaseError = [FirstRecord, &FirstModule, 10049 this](SourceLocation Loc, SourceRange Range, 10050 ODRDefinitionDataDifference DiffType) { 10051 return Diag(Loc, diag::err_module_odr_violation_definition_data) 10052 << FirstRecord << FirstModule.empty() << FirstModule << Range 10053 << DiffType; 10054 }; 10055 auto ODRDiagBaseNote = [&SecondModule, 10056 this](SourceLocation Loc, SourceRange Range, 10057 ODRDefinitionDataDifference DiffType) { 10058 return Diag(Loc, diag::note_module_odr_violation_definition_data) 10059 << SecondModule << Range << DiffType; 10060 }; 10061 10062 unsigned FirstNumBases = FirstDD->NumBases; 10063 unsigned FirstNumVBases = FirstDD->NumVBases; 10064 unsigned SecondNumBases = SecondDD->NumBases; 10065 unsigned SecondNumVBases = SecondDD->NumVBases; 10066 10067 auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) { 10068 unsigned NumBases = DD->NumBases; 10069 if (NumBases == 0) return SourceRange(); 10070 auto bases = DD->bases(); 10071 return SourceRange(bases[0].getBeginLoc(), 10072 bases[NumBases - 1].getEndLoc()); 10073 }; 10074 10075 if (FirstNumBases != SecondNumBases) { 10076 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10077 NumBases) 10078 << FirstNumBases; 10079 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10080 NumBases) 10081 << SecondNumBases; 10082 Diagnosed = true; 10083 break; 10084 } 10085 10086 if (FirstNumVBases != SecondNumVBases) { 10087 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10088 NumVBases) 10089 << FirstNumVBases; 10090 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10091 NumVBases) 10092 << SecondNumVBases; 10093 Diagnosed = true; 10094 break; 10095 } 10096 10097 auto FirstBases = FirstDD->bases(); 10098 auto SecondBases = SecondDD->bases(); 10099 unsigned i = 0; 10100 for (i = 0; i < FirstNumBases; ++i) { 10101 auto FirstBase = FirstBases[i]; 10102 auto SecondBase = SecondBases[i]; 10103 if (ComputeQualTypeODRHash(FirstBase.getType()) != 10104 ComputeQualTypeODRHash(SecondBase.getType())) { 10105 ODRDiagBaseError(FirstRecord->getLocation(), 10106 FirstBase.getSourceRange(), BaseType) 10107 << (i + 1) << FirstBase.getType(); 10108 ODRDiagBaseNote(SecondRecord->getLocation(), 10109 SecondBase.getSourceRange(), BaseType) 10110 << (i + 1) << SecondBase.getType(); 10111 break; 10112 } 10113 10114 if (FirstBase.isVirtual() != SecondBase.isVirtual()) { 10115 ODRDiagBaseError(FirstRecord->getLocation(), 10116 FirstBase.getSourceRange(), BaseVirtual) 10117 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType(); 10118 ODRDiagBaseNote(SecondRecord->getLocation(), 10119 SecondBase.getSourceRange(), BaseVirtual) 10120 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType(); 10121 break; 10122 } 10123 10124 if (FirstBase.getAccessSpecifierAsWritten() != 10125 SecondBase.getAccessSpecifierAsWritten()) { 10126 ODRDiagBaseError(FirstRecord->getLocation(), 10127 FirstBase.getSourceRange(), BaseAccess) 10128 << (i + 1) << FirstBase.getType() 10129 << (int)FirstBase.getAccessSpecifierAsWritten(); 10130 ODRDiagBaseNote(SecondRecord->getLocation(), 10131 SecondBase.getSourceRange(), BaseAccess) 10132 << (i + 1) << SecondBase.getType() 10133 << (int)SecondBase.getAccessSpecifierAsWritten(); 10134 break; 10135 } 10136 } 10137 10138 if (i != FirstNumBases) { 10139 Diagnosed = true; 10140 break; 10141 } 10142 } 10143 10144 const ClassTemplateDecl *FirstTemplate = 10145 FirstRecord->getDescribedClassTemplate(); 10146 const ClassTemplateDecl *SecondTemplate = 10147 SecondRecord->getDescribedClassTemplate(); 10148 10149 assert(!FirstTemplate == !SecondTemplate && 10150 "Both pointers should be null or non-null"); 10151 10152 if (FirstTemplate && SecondTemplate) { 10153 DeclHashes FirstTemplateHashes; 10154 DeclHashes SecondTemplateHashes; 10155 10156 auto PopulateTemplateParameterHashs = 10157 [&ComputeSubDeclODRHash](DeclHashes &Hashes, 10158 const ClassTemplateDecl *TD) { 10159 for (auto *D : TD->getTemplateParameters()->asArray()) { 10160 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 10161 } 10162 }; 10163 10164 PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate); 10165 PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate); 10166 10167 assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() && 10168 "Number of template parameters should be equal."); 10169 10170 auto FirstIt = FirstTemplateHashes.begin(); 10171 auto FirstEnd = FirstTemplateHashes.end(); 10172 auto SecondIt = SecondTemplateHashes.begin(); 10173 for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) { 10174 if (FirstIt->second == SecondIt->second) 10175 continue; 10176 10177 const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first); 10178 const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first); 10179 10180 assert(FirstDecl->getKind() == SecondDecl->getKind() && 10181 "Parameter Decl's should be the same kind."); 10182 10183 enum ODRTemplateDifference { 10184 ParamEmptyName, 10185 ParamName, 10186 ParamSingleDefaultArgument, 10187 ParamDifferentDefaultArgument, 10188 }; 10189 10190 auto hasDefaultArg = [](const NamedDecl *D) { 10191 if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(D)) 10192 return TTP->hasDefaultArgument() && 10193 !TTP->defaultArgumentWasInherited(); 10194 if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) 10195 return NTTP->hasDefaultArgument() && 10196 !NTTP->defaultArgumentWasInherited(); 10197 auto *TTP = cast<TemplateTemplateParmDecl>(D); 10198 return TTP->hasDefaultArgument() && 10199 !TTP->defaultArgumentWasInherited(); 10200 }; 10201 bool hasFirstArg = hasDefaultArg(FirstDecl); 10202 bool hasSecondArg = hasDefaultArg(SecondDecl); 10203 10204 ODRTemplateDifference ErrDiffType; 10205 ODRTemplateDifference NoteDiffType; 10206 10207 DeclarationName FirstName = FirstDecl->getDeclName(); 10208 DeclarationName SecondName = SecondDecl->getDeclName(); 10209 10210 if (FirstName != SecondName) { 10211 bool FirstNameEmpty = 10212 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo(); 10213 bool SecondNameEmpty = SecondName.isIdentifier() && 10214 !SecondName.getAsIdentifierInfo(); 10215 ErrDiffType = FirstNameEmpty ? ParamEmptyName : ParamName; 10216 NoteDiffType = SecondNameEmpty ? ParamEmptyName : ParamName; 10217 } else if (hasFirstArg == hasSecondArg) 10218 ErrDiffType = NoteDiffType = ParamDifferentDefaultArgument; 10219 else 10220 ErrDiffType = NoteDiffType = ParamSingleDefaultArgument; 10221 10222 Diag(FirstDecl->getLocation(), 10223 diag::err_module_odr_violation_template_parameter) 10224 << FirstRecord << FirstModule.empty() << FirstModule 10225 << FirstDecl->getSourceRange() << ErrDiffType << hasFirstArg 10226 << FirstName; 10227 Diag(SecondDecl->getLocation(), 10228 diag::note_module_odr_violation_template_parameter) 10229 << SecondModule << SecondDecl->getSourceRange() << NoteDiffType 10230 << hasSecondArg << SecondName; 10231 break; 10232 } 10233 10234 if (FirstIt != FirstEnd) { 10235 Diagnosed = true; 10236 break; 10237 } 10238 } 10239 10240 DeclHashes FirstHashes; 10241 DeclHashes SecondHashes; 10242 const DeclContext *DC = FirstRecord; 10243 PopulateHashes(FirstHashes, FirstRecord, DC); 10244 PopulateHashes(SecondHashes, SecondRecord, DC); 10245 10246 auto DR = FindTypeDiffs(FirstHashes, SecondHashes); 10247 ODRMismatchDecl FirstDiffType = DR.FirstDiffType; 10248 ODRMismatchDecl SecondDiffType = DR.SecondDiffType; 10249 Decl *FirstDecl = DR.FirstDecl; 10250 Decl *SecondDecl = DR.SecondDecl; 10251 10252 if (FirstDiffType == Other || SecondDiffType == Other) { 10253 DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord, 10254 SecondModule); 10255 Diagnosed = true; 10256 break; 10257 } 10258 10259 if (FirstDiffType != SecondDiffType) { 10260 DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord, 10261 SecondModule); 10262 Diagnosed = true; 10263 break; 10264 } 10265 10266 assert(FirstDiffType == SecondDiffType); 10267 10268 switch (FirstDiffType) { 10269 case Other: 10270 case EndOfClass: 10271 case PublicSpecifer: 10272 case PrivateSpecifer: 10273 case ProtectedSpecifer: 10274 llvm_unreachable("Invalid diff type"); 10275 10276 case StaticAssert: { 10277 StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl); 10278 StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl); 10279 10280 Expr *FirstExpr = FirstSA->getAssertExpr(); 10281 Expr *SecondExpr = SecondSA->getAssertExpr(); 10282 unsigned FirstODRHash = ComputeODRHash(FirstExpr); 10283 unsigned SecondODRHash = ComputeODRHash(SecondExpr); 10284 if (FirstODRHash != SecondODRHash) { 10285 ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(), 10286 FirstExpr->getSourceRange(), StaticAssertCondition); 10287 ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(), 10288 SecondExpr->getSourceRange(), StaticAssertCondition); 10289 Diagnosed = true; 10290 break; 10291 } 10292 10293 StringLiteral *FirstStr = FirstSA->getMessage(); 10294 StringLiteral *SecondStr = SecondSA->getMessage(); 10295 assert((FirstStr || SecondStr) && "Both messages cannot be empty"); 10296 if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) { 10297 SourceLocation FirstLoc, SecondLoc; 10298 SourceRange FirstRange, SecondRange; 10299 if (FirstStr) { 10300 FirstLoc = FirstStr->getBeginLoc(); 10301 FirstRange = FirstStr->getSourceRange(); 10302 } else { 10303 FirstLoc = FirstSA->getBeginLoc(); 10304 FirstRange = FirstSA->getSourceRange(); 10305 } 10306 if (SecondStr) { 10307 SecondLoc = SecondStr->getBeginLoc(); 10308 SecondRange = SecondStr->getSourceRange(); 10309 } else { 10310 SecondLoc = SecondSA->getBeginLoc(); 10311 SecondRange = SecondSA->getSourceRange(); 10312 } 10313 ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange, 10314 StaticAssertOnlyMessage) 10315 << (FirstStr == nullptr); 10316 ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange, 10317 StaticAssertOnlyMessage) 10318 << (SecondStr == nullptr); 10319 Diagnosed = true; 10320 break; 10321 } 10322 10323 if (FirstStr && SecondStr && 10324 FirstStr->getString() != SecondStr->getString()) { 10325 ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(), 10326 FirstStr->getSourceRange(), StaticAssertMessage); 10327 ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(), 10328 SecondStr->getSourceRange(), StaticAssertMessage); 10329 Diagnosed = true; 10330 break; 10331 } 10332 break; 10333 } 10334 case Field: { 10335 Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule, 10336 cast<FieldDecl>(FirstDecl), 10337 cast<FieldDecl>(SecondDecl)); 10338 break; 10339 } 10340 case CXXMethod: { 10341 enum { 10342 DiagMethod, 10343 DiagConstructor, 10344 DiagDestructor, 10345 } FirstMethodType, 10346 SecondMethodType; 10347 auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) { 10348 if (isa<CXXConstructorDecl>(D)) return DiagConstructor; 10349 if (isa<CXXDestructorDecl>(D)) return DiagDestructor; 10350 return DiagMethod; 10351 }; 10352 const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl); 10353 const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl); 10354 FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod); 10355 SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod); 10356 auto FirstName = FirstMethod->getDeclName(); 10357 auto SecondName = SecondMethod->getDeclName(); 10358 if (FirstMethodType != SecondMethodType || FirstName != SecondName) { 10359 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10360 FirstMethod->getSourceRange(), MethodName) 10361 << FirstMethodType << FirstName; 10362 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10363 SecondMethod->getSourceRange(), MethodName) 10364 << SecondMethodType << SecondName; 10365 10366 Diagnosed = true; 10367 break; 10368 } 10369 10370 const bool FirstDeleted = FirstMethod->isDeletedAsWritten(); 10371 const bool SecondDeleted = SecondMethod->isDeletedAsWritten(); 10372 if (FirstDeleted != SecondDeleted) { 10373 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10374 FirstMethod->getSourceRange(), MethodDeleted) 10375 << FirstMethodType << FirstName << FirstDeleted; 10376 10377 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10378 SecondMethod->getSourceRange(), MethodDeleted) 10379 << SecondMethodType << SecondName << SecondDeleted; 10380 Diagnosed = true; 10381 break; 10382 } 10383 10384 const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted(); 10385 const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted(); 10386 if (FirstDefaulted != SecondDefaulted) { 10387 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10388 FirstMethod->getSourceRange(), MethodDefaulted) 10389 << FirstMethodType << FirstName << FirstDefaulted; 10390 10391 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10392 SecondMethod->getSourceRange(), MethodDefaulted) 10393 << SecondMethodType << SecondName << SecondDefaulted; 10394 Diagnosed = true; 10395 break; 10396 } 10397 10398 const bool FirstVirtual = FirstMethod->isVirtualAsWritten(); 10399 const bool SecondVirtual = SecondMethod->isVirtualAsWritten(); 10400 const bool FirstPure = FirstMethod->isPure(); 10401 const bool SecondPure = SecondMethod->isPure(); 10402 if ((FirstVirtual || SecondVirtual) && 10403 (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) { 10404 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10405 FirstMethod->getSourceRange(), MethodVirtual) 10406 << FirstMethodType << FirstName << FirstPure << FirstVirtual; 10407 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10408 SecondMethod->getSourceRange(), MethodVirtual) 10409 << SecondMethodType << SecondName << SecondPure << SecondVirtual; 10410 Diagnosed = true; 10411 break; 10412 } 10413 10414 // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging, 10415 // FirstDecl is the canonical Decl of SecondDecl, so the storage 10416 // class needs to be checked instead. 10417 const auto FirstStorage = FirstMethod->getStorageClass(); 10418 const auto SecondStorage = SecondMethod->getStorageClass(); 10419 const bool FirstStatic = FirstStorage == SC_Static; 10420 const bool SecondStatic = SecondStorage == SC_Static; 10421 if (FirstStatic != SecondStatic) { 10422 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10423 FirstMethod->getSourceRange(), MethodStatic) 10424 << FirstMethodType << FirstName << FirstStatic; 10425 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10426 SecondMethod->getSourceRange(), MethodStatic) 10427 << SecondMethodType << SecondName << SecondStatic; 10428 Diagnosed = true; 10429 break; 10430 } 10431 10432 const bool FirstVolatile = FirstMethod->isVolatile(); 10433 const bool SecondVolatile = SecondMethod->isVolatile(); 10434 if (FirstVolatile != SecondVolatile) { 10435 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10436 FirstMethod->getSourceRange(), MethodVolatile) 10437 << FirstMethodType << FirstName << FirstVolatile; 10438 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10439 SecondMethod->getSourceRange(), MethodVolatile) 10440 << SecondMethodType << SecondName << SecondVolatile; 10441 Diagnosed = true; 10442 break; 10443 } 10444 10445 const bool FirstConst = FirstMethod->isConst(); 10446 const bool SecondConst = SecondMethod->isConst(); 10447 if (FirstConst != SecondConst) { 10448 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10449 FirstMethod->getSourceRange(), MethodConst) 10450 << FirstMethodType << FirstName << FirstConst; 10451 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10452 SecondMethod->getSourceRange(), MethodConst) 10453 << SecondMethodType << SecondName << SecondConst; 10454 Diagnosed = true; 10455 break; 10456 } 10457 10458 const bool FirstInline = FirstMethod->isInlineSpecified(); 10459 const bool SecondInline = SecondMethod->isInlineSpecified(); 10460 if (FirstInline != SecondInline) { 10461 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10462 FirstMethod->getSourceRange(), MethodInline) 10463 << FirstMethodType << FirstName << FirstInline; 10464 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10465 SecondMethod->getSourceRange(), MethodInline) 10466 << SecondMethodType << SecondName << SecondInline; 10467 Diagnosed = true; 10468 break; 10469 } 10470 10471 const unsigned FirstNumParameters = FirstMethod->param_size(); 10472 const unsigned SecondNumParameters = SecondMethod->param_size(); 10473 if (FirstNumParameters != SecondNumParameters) { 10474 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10475 FirstMethod->getSourceRange(), 10476 MethodNumberParameters) 10477 << FirstMethodType << FirstName << FirstNumParameters; 10478 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10479 SecondMethod->getSourceRange(), 10480 MethodNumberParameters) 10481 << SecondMethodType << SecondName << SecondNumParameters; 10482 Diagnosed = true; 10483 break; 10484 } 10485 10486 // Need this status boolean to know when break out of the switch. 10487 bool ParameterMismatch = false; 10488 for (unsigned I = 0; I < FirstNumParameters; ++I) { 10489 const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I); 10490 const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I); 10491 10492 QualType FirstParamType = FirstParam->getType(); 10493 QualType SecondParamType = SecondParam->getType(); 10494 if (FirstParamType != SecondParamType && 10495 ComputeQualTypeODRHash(FirstParamType) != 10496 ComputeQualTypeODRHash(SecondParamType)) { 10497 if (const DecayedType *ParamDecayedType = 10498 FirstParamType->getAs<DecayedType>()) { 10499 ODRDiagDeclError( 10500 FirstRecord, FirstModule, FirstMethod->getLocation(), 10501 FirstMethod->getSourceRange(), MethodParameterType) 10502 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10503 << true << ParamDecayedType->getOriginalType(); 10504 } else { 10505 ODRDiagDeclError( 10506 FirstRecord, FirstModule, FirstMethod->getLocation(), 10507 FirstMethod->getSourceRange(), MethodParameterType) 10508 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10509 << false; 10510 } 10511 10512 if (const DecayedType *ParamDecayedType = 10513 SecondParamType->getAs<DecayedType>()) { 10514 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10515 SecondMethod->getSourceRange(), 10516 MethodParameterType) 10517 << SecondMethodType << SecondName << (I + 1) 10518 << SecondParamType << true 10519 << ParamDecayedType->getOriginalType(); 10520 } else { 10521 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10522 SecondMethod->getSourceRange(), 10523 MethodParameterType) 10524 << SecondMethodType << SecondName << (I + 1) 10525 << SecondParamType << false; 10526 } 10527 ParameterMismatch = true; 10528 break; 10529 } 10530 10531 DeclarationName FirstParamName = FirstParam->getDeclName(); 10532 DeclarationName SecondParamName = SecondParam->getDeclName(); 10533 if (FirstParamName != SecondParamName) { 10534 ODRDiagDeclError(FirstRecord, FirstModule, 10535 FirstMethod->getLocation(), 10536 FirstMethod->getSourceRange(), MethodParameterName) 10537 << FirstMethodType << FirstName << (I + 1) << FirstParamName; 10538 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10539 SecondMethod->getSourceRange(), MethodParameterName) 10540 << SecondMethodType << SecondName << (I + 1) << SecondParamName; 10541 ParameterMismatch = true; 10542 break; 10543 } 10544 10545 const Expr *FirstInit = FirstParam->getInit(); 10546 const Expr *SecondInit = SecondParam->getInit(); 10547 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 10548 ODRDiagDeclError(FirstRecord, FirstModule, 10549 FirstMethod->getLocation(), 10550 FirstMethod->getSourceRange(), 10551 MethodParameterSingleDefaultArgument) 10552 << FirstMethodType << FirstName << (I + 1) 10553 << (FirstInit == nullptr) 10554 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 10555 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10556 SecondMethod->getSourceRange(), 10557 MethodParameterSingleDefaultArgument) 10558 << SecondMethodType << SecondName << (I + 1) 10559 << (SecondInit == nullptr) 10560 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 10561 ParameterMismatch = true; 10562 break; 10563 } 10564 10565 if (FirstInit && SecondInit && 10566 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 10567 ODRDiagDeclError(FirstRecord, FirstModule, 10568 FirstMethod->getLocation(), 10569 FirstMethod->getSourceRange(), 10570 MethodParameterDifferentDefaultArgument) 10571 << FirstMethodType << FirstName << (I + 1) 10572 << FirstInit->getSourceRange(); 10573 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10574 SecondMethod->getSourceRange(), 10575 MethodParameterDifferentDefaultArgument) 10576 << SecondMethodType << SecondName << (I + 1) 10577 << SecondInit->getSourceRange(); 10578 ParameterMismatch = true; 10579 break; 10580 10581 } 10582 } 10583 10584 if (ParameterMismatch) { 10585 Diagnosed = true; 10586 break; 10587 } 10588 10589 const auto *FirstTemplateArgs = 10590 FirstMethod->getTemplateSpecializationArgs(); 10591 const auto *SecondTemplateArgs = 10592 SecondMethod->getTemplateSpecializationArgs(); 10593 10594 if ((FirstTemplateArgs && !SecondTemplateArgs) || 10595 (!FirstTemplateArgs && SecondTemplateArgs)) { 10596 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10597 FirstMethod->getSourceRange(), 10598 MethodNoTemplateArguments) 10599 << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr); 10600 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10601 SecondMethod->getSourceRange(), 10602 MethodNoTemplateArguments) 10603 << SecondMethodType << SecondName 10604 << (SecondTemplateArgs != nullptr); 10605 10606 Diagnosed = true; 10607 break; 10608 } 10609 10610 if (FirstTemplateArgs && SecondTemplateArgs) { 10611 // Remove pack expansions from argument list. 10612 auto ExpandTemplateArgumentList = 10613 [](const TemplateArgumentList *TAL) { 10614 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList; 10615 for (const TemplateArgument &TA : TAL->asArray()) { 10616 if (TA.getKind() != TemplateArgument::Pack) { 10617 ExpandedList.push_back(&TA); 10618 continue; 10619 } 10620 llvm::append_range(ExpandedList, llvm::make_pointer_range( 10621 TA.getPackAsArray())); 10622 } 10623 return ExpandedList; 10624 }; 10625 llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList = 10626 ExpandTemplateArgumentList(FirstTemplateArgs); 10627 llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList = 10628 ExpandTemplateArgumentList(SecondTemplateArgs); 10629 10630 if (FirstExpandedList.size() != SecondExpandedList.size()) { 10631 ODRDiagDeclError(FirstRecord, FirstModule, 10632 FirstMethod->getLocation(), 10633 FirstMethod->getSourceRange(), 10634 MethodDifferentNumberTemplateArguments) 10635 << FirstMethodType << FirstName 10636 << (unsigned)FirstExpandedList.size(); 10637 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10638 SecondMethod->getSourceRange(), 10639 MethodDifferentNumberTemplateArguments) 10640 << SecondMethodType << SecondName 10641 << (unsigned)SecondExpandedList.size(); 10642 10643 Diagnosed = true; 10644 break; 10645 } 10646 10647 bool TemplateArgumentMismatch = false; 10648 for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) { 10649 const TemplateArgument &FirstTA = *FirstExpandedList[i], 10650 &SecondTA = *SecondExpandedList[i]; 10651 if (ComputeTemplateArgumentODRHash(FirstTA) == 10652 ComputeTemplateArgumentODRHash(SecondTA)) { 10653 continue; 10654 } 10655 10656 ODRDiagDeclError( 10657 FirstRecord, FirstModule, FirstMethod->getLocation(), 10658 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument) 10659 << FirstMethodType << FirstName << FirstTA << i + 1; 10660 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10661 SecondMethod->getSourceRange(), 10662 MethodDifferentTemplateArgument) 10663 << SecondMethodType << SecondName << SecondTA << i + 1; 10664 10665 TemplateArgumentMismatch = true; 10666 break; 10667 } 10668 10669 if (TemplateArgumentMismatch) { 10670 Diagnosed = true; 10671 break; 10672 } 10673 } 10674 10675 // Compute the hash of the method as if it has no body. 10676 auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) { 10677 Hash.clear(); 10678 Hash.AddFunctionDecl(D, true /*SkipBody*/); 10679 return Hash.CalculateHash(); 10680 }; 10681 10682 // Compare the hash generated to the hash stored. A difference means 10683 // that a body was present in the original source. Due to merging, 10684 // the stardard way of detecting a body will not work. 10685 const bool HasFirstBody = 10686 ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash(); 10687 const bool HasSecondBody = 10688 ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash(); 10689 10690 if (HasFirstBody != HasSecondBody) { 10691 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10692 FirstMethod->getSourceRange(), MethodSingleBody) 10693 << FirstMethodType << FirstName << HasFirstBody; 10694 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10695 SecondMethod->getSourceRange(), MethodSingleBody) 10696 << SecondMethodType << SecondName << HasSecondBody; 10697 Diagnosed = true; 10698 break; 10699 } 10700 10701 if (HasFirstBody && HasSecondBody) { 10702 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10703 FirstMethod->getSourceRange(), MethodDifferentBody) 10704 << FirstMethodType << FirstName; 10705 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10706 SecondMethod->getSourceRange(), MethodDifferentBody) 10707 << SecondMethodType << SecondName; 10708 Diagnosed = true; 10709 break; 10710 } 10711 10712 break; 10713 } 10714 case TypeAlias: 10715 case TypeDef: { 10716 Diagnosed = ODRDiagTypeDefOrAlias( 10717 FirstRecord, FirstModule, SecondModule, 10718 cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl), 10719 FirstDiffType == TypeAlias); 10720 break; 10721 } 10722 case Var: { 10723 Diagnosed = 10724 ODRDiagVar(FirstRecord, FirstModule, SecondModule, 10725 cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl)); 10726 break; 10727 } 10728 case Friend: { 10729 FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl); 10730 FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl); 10731 10732 NamedDecl *FirstND = FirstFriend->getFriendDecl(); 10733 NamedDecl *SecondND = SecondFriend->getFriendDecl(); 10734 10735 TypeSourceInfo *FirstTSI = FirstFriend->getFriendType(); 10736 TypeSourceInfo *SecondTSI = SecondFriend->getFriendType(); 10737 10738 if (FirstND && SecondND) { 10739 ODRDiagDeclError(FirstRecord, FirstModule, 10740 FirstFriend->getFriendLoc(), 10741 FirstFriend->getSourceRange(), FriendFunction) 10742 << FirstND; 10743 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10744 SecondFriend->getSourceRange(), FriendFunction) 10745 << SecondND; 10746 10747 Diagnosed = true; 10748 break; 10749 } 10750 10751 if (FirstTSI && SecondTSI) { 10752 QualType FirstFriendType = FirstTSI->getType(); 10753 QualType SecondFriendType = SecondTSI->getType(); 10754 assert(ComputeQualTypeODRHash(FirstFriendType) != 10755 ComputeQualTypeODRHash(SecondFriendType)); 10756 ODRDiagDeclError(FirstRecord, FirstModule, 10757 FirstFriend->getFriendLoc(), 10758 FirstFriend->getSourceRange(), FriendType) 10759 << FirstFriendType; 10760 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10761 SecondFriend->getSourceRange(), FriendType) 10762 << SecondFriendType; 10763 Diagnosed = true; 10764 break; 10765 } 10766 10767 ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(), 10768 FirstFriend->getSourceRange(), FriendTypeFunction) 10769 << (FirstTSI == nullptr); 10770 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10771 SecondFriend->getSourceRange(), FriendTypeFunction) 10772 << (SecondTSI == nullptr); 10773 10774 Diagnosed = true; 10775 break; 10776 } 10777 case FunctionTemplate: { 10778 FunctionTemplateDecl *FirstTemplate = 10779 cast<FunctionTemplateDecl>(FirstDecl); 10780 FunctionTemplateDecl *SecondTemplate = 10781 cast<FunctionTemplateDecl>(SecondDecl); 10782 10783 TemplateParameterList *FirstTPL = 10784 FirstTemplate->getTemplateParameters(); 10785 TemplateParameterList *SecondTPL = 10786 SecondTemplate->getTemplateParameters(); 10787 10788 if (FirstTPL->size() != SecondTPL->size()) { 10789 ODRDiagDeclError(FirstRecord, FirstModule, 10790 FirstTemplate->getLocation(), 10791 FirstTemplate->getSourceRange(), 10792 FunctionTemplateDifferentNumberParameters) 10793 << FirstTemplate << FirstTPL->size(); 10794 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10795 SecondTemplate->getSourceRange(), 10796 FunctionTemplateDifferentNumberParameters) 10797 << SecondTemplate << SecondTPL->size(); 10798 10799 Diagnosed = true; 10800 break; 10801 } 10802 10803 bool ParameterMismatch = false; 10804 for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) { 10805 NamedDecl *FirstParam = FirstTPL->getParam(i); 10806 NamedDecl *SecondParam = SecondTPL->getParam(i); 10807 10808 if (FirstParam->getKind() != SecondParam->getKind()) { 10809 enum { 10810 TemplateTypeParameter, 10811 NonTypeTemplateParameter, 10812 TemplateTemplateParameter, 10813 }; 10814 auto GetParamType = [](NamedDecl *D) { 10815 switch (D->getKind()) { 10816 default: 10817 llvm_unreachable("Unexpected template parameter type"); 10818 case Decl::TemplateTypeParm: 10819 return TemplateTypeParameter; 10820 case Decl::NonTypeTemplateParm: 10821 return NonTypeTemplateParameter; 10822 case Decl::TemplateTemplateParm: 10823 return TemplateTemplateParameter; 10824 } 10825 }; 10826 10827 ODRDiagDeclError(FirstRecord, FirstModule, 10828 FirstTemplate->getLocation(), 10829 FirstTemplate->getSourceRange(), 10830 FunctionTemplateParameterDifferentKind) 10831 << FirstTemplate << (i + 1) << GetParamType(FirstParam); 10832 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10833 SecondTemplate->getSourceRange(), 10834 FunctionTemplateParameterDifferentKind) 10835 << SecondTemplate << (i + 1) << GetParamType(SecondParam); 10836 10837 ParameterMismatch = true; 10838 break; 10839 } 10840 10841 if (FirstParam->getName() != SecondParam->getName()) { 10842 ODRDiagDeclError( 10843 FirstRecord, FirstModule, FirstTemplate->getLocation(), 10844 FirstTemplate->getSourceRange(), FunctionTemplateParameterName) 10845 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier() 10846 << FirstParam; 10847 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10848 SecondTemplate->getSourceRange(), 10849 FunctionTemplateParameterName) 10850 << SecondTemplate << (i + 1) 10851 << (bool)SecondParam->getIdentifier() << SecondParam; 10852 ParameterMismatch = true; 10853 break; 10854 } 10855 10856 if (isa<TemplateTypeParmDecl>(FirstParam) && 10857 isa<TemplateTypeParmDecl>(SecondParam)) { 10858 TemplateTypeParmDecl *FirstTTPD = 10859 cast<TemplateTypeParmDecl>(FirstParam); 10860 TemplateTypeParmDecl *SecondTTPD = 10861 cast<TemplateTypeParmDecl>(SecondParam); 10862 bool HasFirstDefaultArgument = 10863 FirstTTPD->hasDefaultArgument() && 10864 !FirstTTPD->defaultArgumentWasInherited(); 10865 bool HasSecondDefaultArgument = 10866 SecondTTPD->hasDefaultArgument() && 10867 !SecondTTPD->defaultArgumentWasInherited(); 10868 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10869 ODRDiagDeclError(FirstRecord, FirstModule, 10870 FirstTemplate->getLocation(), 10871 FirstTemplate->getSourceRange(), 10872 FunctionTemplateParameterSingleDefaultArgument) 10873 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 10874 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10875 SecondTemplate->getSourceRange(), 10876 FunctionTemplateParameterSingleDefaultArgument) 10877 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 10878 ParameterMismatch = true; 10879 break; 10880 } 10881 10882 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 10883 QualType FirstType = FirstTTPD->getDefaultArgument(); 10884 QualType SecondType = SecondTTPD->getDefaultArgument(); 10885 if (ComputeQualTypeODRHash(FirstType) != 10886 ComputeQualTypeODRHash(SecondType)) { 10887 ODRDiagDeclError( 10888 FirstRecord, FirstModule, FirstTemplate->getLocation(), 10889 FirstTemplate->getSourceRange(), 10890 FunctionTemplateParameterDifferentDefaultArgument) 10891 << FirstTemplate << (i + 1) << FirstType; 10892 ODRDiagDeclNote( 10893 SecondModule, SecondTemplate->getLocation(), 10894 SecondTemplate->getSourceRange(), 10895 FunctionTemplateParameterDifferentDefaultArgument) 10896 << SecondTemplate << (i + 1) << SecondType; 10897 ParameterMismatch = true; 10898 break; 10899 } 10900 } 10901 10902 if (FirstTTPD->isParameterPack() != 10903 SecondTTPD->isParameterPack()) { 10904 ODRDiagDeclError(FirstRecord, FirstModule, 10905 FirstTemplate->getLocation(), 10906 FirstTemplate->getSourceRange(), 10907 FunctionTemplatePackParameter) 10908 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 10909 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10910 SecondTemplate->getSourceRange(), 10911 FunctionTemplatePackParameter) 10912 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 10913 ParameterMismatch = true; 10914 break; 10915 } 10916 } 10917 10918 if (isa<TemplateTemplateParmDecl>(FirstParam) && 10919 isa<TemplateTemplateParmDecl>(SecondParam)) { 10920 TemplateTemplateParmDecl *FirstTTPD = 10921 cast<TemplateTemplateParmDecl>(FirstParam); 10922 TemplateTemplateParmDecl *SecondTTPD = 10923 cast<TemplateTemplateParmDecl>(SecondParam); 10924 10925 TemplateParameterList *FirstTPL = 10926 FirstTTPD->getTemplateParameters(); 10927 TemplateParameterList *SecondTPL = 10928 SecondTTPD->getTemplateParameters(); 10929 10930 if (ComputeTemplateParameterListODRHash(FirstTPL) != 10931 ComputeTemplateParameterListODRHash(SecondTPL)) { 10932 ODRDiagDeclError(FirstRecord, FirstModule, 10933 FirstTemplate->getLocation(), 10934 FirstTemplate->getSourceRange(), 10935 FunctionTemplateParameterDifferentType) 10936 << FirstTemplate << (i + 1); 10937 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10938 SecondTemplate->getSourceRange(), 10939 FunctionTemplateParameterDifferentType) 10940 << SecondTemplate << (i + 1); 10941 ParameterMismatch = true; 10942 break; 10943 } 10944 10945 bool HasFirstDefaultArgument = 10946 FirstTTPD->hasDefaultArgument() && 10947 !FirstTTPD->defaultArgumentWasInherited(); 10948 bool HasSecondDefaultArgument = 10949 SecondTTPD->hasDefaultArgument() && 10950 !SecondTTPD->defaultArgumentWasInherited(); 10951 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10952 ODRDiagDeclError(FirstRecord, FirstModule, 10953 FirstTemplate->getLocation(), 10954 FirstTemplate->getSourceRange(), 10955 FunctionTemplateParameterSingleDefaultArgument) 10956 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 10957 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10958 SecondTemplate->getSourceRange(), 10959 FunctionTemplateParameterSingleDefaultArgument) 10960 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 10961 ParameterMismatch = true; 10962 break; 10963 } 10964 10965 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 10966 TemplateArgument FirstTA = 10967 FirstTTPD->getDefaultArgument().getArgument(); 10968 TemplateArgument SecondTA = 10969 SecondTTPD->getDefaultArgument().getArgument(); 10970 if (ComputeTemplateArgumentODRHash(FirstTA) != 10971 ComputeTemplateArgumentODRHash(SecondTA)) { 10972 ODRDiagDeclError( 10973 FirstRecord, FirstModule, FirstTemplate->getLocation(), 10974 FirstTemplate->getSourceRange(), 10975 FunctionTemplateParameterDifferentDefaultArgument) 10976 << FirstTemplate << (i + 1) << FirstTA; 10977 ODRDiagDeclNote( 10978 SecondModule, SecondTemplate->getLocation(), 10979 SecondTemplate->getSourceRange(), 10980 FunctionTemplateParameterDifferentDefaultArgument) 10981 << SecondTemplate << (i + 1) << SecondTA; 10982 ParameterMismatch = true; 10983 break; 10984 } 10985 } 10986 10987 if (FirstTTPD->isParameterPack() != 10988 SecondTTPD->isParameterPack()) { 10989 ODRDiagDeclError(FirstRecord, FirstModule, 10990 FirstTemplate->getLocation(), 10991 FirstTemplate->getSourceRange(), 10992 FunctionTemplatePackParameter) 10993 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 10994 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10995 SecondTemplate->getSourceRange(), 10996 FunctionTemplatePackParameter) 10997 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 10998 ParameterMismatch = true; 10999 break; 11000 } 11001 } 11002 11003 if (isa<NonTypeTemplateParmDecl>(FirstParam) && 11004 isa<NonTypeTemplateParmDecl>(SecondParam)) { 11005 NonTypeTemplateParmDecl *FirstNTTPD = 11006 cast<NonTypeTemplateParmDecl>(FirstParam); 11007 NonTypeTemplateParmDecl *SecondNTTPD = 11008 cast<NonTypeTemplateParmDecl>(SecondParam); 11009 11010 QualType FirstType = FirstNTTPD->getType(); 11011 QualType SecondType = SecondNTTPD->getType(); 11012 if (ComputeQualTypeODRHash(FirstType) != 11013 ComputeQualTypeODRHash(SecondType)) { 11014 ODRDiagDeclError(FirstRecord, FirstModule, 11015 FirstTemplate->getLocation(), 11016 FirstTemplate->getSourceRange(), 11017 FunctionTemplateParameterDifferentType) 11018 << FirstTemplate << (i + 1); 11019 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11020 SecondTemplate->getSourceRange(), 11021 FunctionTemplateParameterDifferentType) 11022 << SecondTemplate << (i + 1); 11023 ParameterMismatch = true; 11024 break; 11025 } 11026 11027 bool HasFirstDefaultArgument = 11028 FirstNTTPD->hasDefaultArgument() && 11029 !FirstNTTPD->defaultArgumentWasInherited(); 11030 bool HasSecondDefaultArgument = 11031 SecondNTTPD->hasDefaultArgument() && 11032 !SecondNTTPD->defaultArgumentWasInherited(); 11033 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11034 ODRDiagDeclError(FirstRecord, FirstModule, 11035 FirstTemplate->getLocation(), 11036 FirstTemplate->getSourceRange(), 11037 FunctionTemplateParameterSingleDefaultArgument) 11038 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11039 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11040 SecondTemplate->getSourceRange(), 11041 FunctionTemplateParameterSingleDefaultArgument) 11042 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11043 ParameterMismatch = true; 11044 break; 11045 } 11046 11047 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11048 Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument(); 11049 Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument(); 11050 if (ComputeODRHash(FirstDefaultArgument) != 11051 ComputeODRHash(SecondDefaultArgument)) { 11052 ODRDiagDeclError( 11053 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11054 FirstTemplate->getSourceRange(), 11055 FunctionTemplateParameterDifferentDefaultArgument) 11056 << FirstTemplate << (i + 1) << FirstDefaultArgument; 11057 ODRDiagDeclNote( 11058 SecondModule, SecondTemplate->getLocation(), 11059 SecondTemplate->getSourceRange(), 11060 FunctionTemplateParameterDifferentDefaultArgument) 11061 << SecondTemplate << (i + 1) << SecondDefaultArgument; 11062 ParameterMismatch = true; 11063 break; 11064 } 11065 } 11066 11067 if (FirstNTTPD->isParameterPack() != 11068 SecondNTTPD->isParameterPack()) { 11069 ODRDiagDeclError(FirstRecord, FirstModule, 11070 FirstTemplate->getLocation(), 11071 FirstTemplate->getSourceRange(), 11072 FunctionTemplatePackParameter) 11073 << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack(); 11074 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11075 SecondTemplate->getSourceRange(), 11076 FunctionTemplatePackParameter) 11077 << SecondTemplate << (i + 1) 11078 << SecondNTTPD->isParameterPack(); 11079 ParameterMismatch = true; 11080 break; 11081 } 11082 } 11083 } 11084 11085 if (ParameterMismatch) { 11086 Diagnosed = true; 11087 break; 11088 } 11089 11090 break; 11091 } 11092 } 11093 11094 if (Diagnosed) 11095 continue; 11096 11097 Diag(FirstDecl->getLocation(), 11098 diag::err_module_odr_violation_mismatch_decl_unknown) 11099 << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType 11100 << FirstDecl->getSourceRange(); 11101 Diag(SecondDecl->getLocation(), 11102 diag::note_module_odr_violation_mismatch_decl_unknown) 11103 << SecondModule << FirstDiffType << SecondDecl->getSourceRange(); 11104 Diagnosed = true; 11105 } 11106 11107 if (!Diagnosed) { 11108 // All definitions are updates to the same declaration. This happens if a 11109 // module instantiates the declaration of a class template specialization 11110 // and two or more other modules instantiate its definition. 11111 // 11112 // FIXME: Indicate which modules had instantiations of this definition. 11113 // FIXME: How can this even happen? 11114 Diag(Merge.first->getLocation(), 11115 diag::err_module_odr_violation_different_instantiations) 11116 << Merge.first; 11117 } 11118 } 11119 11120 // Issue ODR failures diagnostics for functions. 11121 for (auto &Merge : FunctionOdrMergeFailures) { 11122 enum ODRFunctionDifference { 11123 ReturnType, 11124 ParameterName, 11125 ParameterType, 11126 ParameterSingleDefaultArgument, 11127 ParameterDifferentDefaultArgument, 11128 FunctionBody, 11129 }; 11130 11131 FunctionDecl *FirstFunction = Merge.first; 11132 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction); 11133 11134 bool Diagnosed = false; 11135 for (auto &SecondFunction : Merge.second) { 11136 11137 if (FirstFunction == SecondFunction) 11138 continue; 11139 11140 std::string SecondModule = 11141 getOwningModuleNameForDiagnostic(SecondFunction); 11142 11143 auto ODRDiagError = [FirstFunction, &FirstModule, 11144 this](SourceLocation Loc, SourceRange Range, 11145 ODRFunctionDifference DiffType) { 11146 return Diag(Loc, diag::err_module_odr_violation_function) 11147 << FirstFunction << FirstModule.empty() << FirstModule << Range 11148 << DiffType; 11149 }; 11150 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11151 SourceRange Range, 11152 ODRFunctionDifference DiffType) { 11153 return Diag(Loc, diag::note_module_odr_violation_function) 11154 << SecondModule << Range << DiffType; 11155 }; 11156 11157 if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) != 11158 ComputeQualTypeODRHash(SecondFunction->getReturnType())) { 11159 ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(), 11160 FirstFunction->getReturnTypeSourceRange(), ReturnType) 11161 << FirstFunction->getReturnType(); 11162 ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(), 11163 SecondFunction->getReturnTypeSourceRange(), ReturnType) 11164 << SecondFunction->getReturnType(); 11165 Diagnosed = true; 11166 break; 11167 } 11168 11169 assert(FirstFunction->param_size() == SecondFunction->param_size() && 11170 "Merged functions with different number of parameters"); 11171 11172 auto ParamSize = FirstFunction->param_size(); 11173 bool ParameterMismatch = false; 11174 for (unsigned I = 0; I < ParamSize; ++I) { 11175 auto *FirstParam = FirstFunction->getParamDecl(I); 11176 auto *SecondParam = SecondFunction->getParamDecl(I); 11177 11178 assert(getContext().hasSameType(FirstParam->getType(), 11179 SecondParam->getType()) && 11180 "Merged function has different parameter types."); 11181 11182 if (FirstParam->getDeclName() != SecondParam->getDeclName()) { 11183 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11184 ParameterName) 11185 << I + 1 << FirstParam->getDeclName(); 11186 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11187 ParameterName) 11188 << I + 1 << SecondParam->getDeclName(); 11189 ParameterMismatch = true; 11190 break; 11191 }; 11192 11193 QualType FirstParamType = FirstParam->getType(); 11194 QualType SecondParamType = SecondParam->getType(); 11195 if (FirstParamType != SecondParamType && 11196 ComputeQualTypeODRHash(FirstParamType) != 11197 ComputeQualTypeODRHash(SecondParamType)) { 11198 if (const DecayedType *ParamDecayedType = 11199 FirstParamType->getAs<DecayedType>()) { 11200 ODRDiagError(FirstParam->getLocation(), 11201 FirstParam->getSourceRange(), ParameterType) 11202 << (I + 1) << FirstParamType << true 11203 << ParamDecayedType->getOriginalType(); 11204 } else { 11205 ODRDiagError(FirstParam->getLocation(), 11206 FirstParam->getSourceRange(), ParameterType) 11207 << (I + 1) << FirstParamType << false; 11208 } 11209 11210 if (const DecayedType *ParamDecayedType = 11211 SecondParamType->getAs<DecayedType>()) { 11212 ODRDiagNote(SecondParam->getLocation(), 11213 SecondParam->getSourceRange(), ParameterType) 11214 << (I + 1) << SecondParamType << true 11215 << ParamDecayedType->getOriginalType(); 11216 } else { 11217 ODRDiagNote(SecondParam->getLocation(), 11218 SecondParam->getSourceRange(), ParameterType) 11219 << (I + 1) << SecondParamType << false; 11220 } 11221 ParameterMismatch = true; 11222 break; 11223 } 11224 11225 const Expr *FirstInit = FirstParam->getInit(); 11226 const Expr *SecondInit = SecondParam->getInit(); 11227 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 11228 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11229 ParameterSingleDefaultArgument) 11230 << (I + 1) << (FirstInit == nullptr) 11231 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 11232 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11233 ParameterSingleDefaultArgument) 11234 << (I + 1) << (SecondInit == nullptr) 11235 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 11236 ParameterMismatch = true; 11237 break; 11238 } 11239 11240 if (FirstInit && SecondInit && 11241 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11242 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11243 ParameterDifferentDefaultArgument) 11244 << (I + 1) << FirstInit->getSourceRange(); 11245 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11246 ParameterDifferentDefaultArgument) 11247 << (I + 1) << SecondInit->getSourceRange(); 11248 ParameterMismatch = true; 11249 break; 11250 } 11251 11252 assert(ComputeSubDeclODRHash(FirstParam) == 11253 ComputeSubDeclODRHash(SecondParam) && 11254 "Undiagnosed parameter difference."); 11255 } 11256 11257 if (ParameterMismatch) { 11258 Diagnosed = true; 11259 break; 11260 } 11261 11262 // If no error has been generated before now, assume the problem is in 11263 // the body and generate a message. 11264 ODRDiagError(FirstFunction->getLocation(), 11265 FirstFunction->getSourceRange(), FunctionBody); 11266 ODRDiagNote(SecondFunction->getLocation(), 11267 SecondFunction->getSourceRange(), FunctionBody); 11268 Diagnosed = true; 11269 break; 11270 } 11271 (void)Diagnosed; 11272 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11273 } 11274 11275 // Issue ODR failures diagnostics for enums. 11276 for (auto &Merge : EnumOdrMergeFailures) { 11277 enum ODREnumDifference { 11278 SingleScopedEnum, 11279 EnumTagKeywordMismatch, 11280 SingleSpecifiedType, 11281 DifferentSpecifiedTypes, 11282 DifferentNumberEnumConstants, 11283 EnumConstantName, 11284 EnumConstantSingleInitilizer, 11285 EnumConstantDifferentInitilizer, 11286 }; 11287 11288 // If we've already pointed out a specific problem with this enum, don't 11289 // bother issuing a general "something's different" diagnostic. 11290 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 11291 continue; 11292 11293 EnumDecl *FirstEnum = Merge.first; 11294 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum); 11295 11296 using DeclHashes = 11297 llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>; 11298 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum]( 11299 DeclHashes &Hashes, EnumDecl *Enum) { 11300 for (auto *D : Enum->decls()) { 11301 // Due to decl merging, the first EnumDecl is the parent of 11302 // Decls in both records. 11303 if (!ODRHash::isDeclToBeProcessed(D, FirstEnum)) 11304 continue; 11305 assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind"); 11306 Hashes.emplace_back(cast<EnumConstantDecl>(D), 11307 ComputeSubDeclODRHash(D)); 11308 } 11309 }; 11310 DeclHashes FirstHashes; 11311 PopulateHashes(FirstHashes, FirstEnum); 11312 bool Diagnosed = false; 11313 for (auto &SecondEnum : Merge.second) { 11314 11315 if (FirstEnum == SecondEnum) 11316 continue; 11317 11318 std::string SecondModule = 11319 getOwningModuleNameForDiagnostic(SecondEnum); 11320 11321 auto ODRDiagError = [FirstEnum, &FirstModule, 11322 this](SourceLocation Loc, SourceRange Range, 11323 ODREnumDifference DiffType) { 11324 return Diag(Loc, diag::err_module_odr_violation_enum) 11325 << FirstEnum << FirstModule.empty() << FirstModule << Range 11326 << DiffType; 11327 }; 11328 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11329 SourceRange Range, 11330 ODREnumDifference DiffType) { 11331 return Diag(Loc, diag::note_module_odr_violation_enum) 11332 << SecondModule << Range << DiffType; 11333 }; 11334 11335 if (FirstEnum->isScoped() != SecondEnum->isScoped()) { 11336 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11337 SingleScopedEnum) 11338 << FirstEnum->isScoped(); 11339 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11340 SingleScopedEnum) 11341 << SecondEnum->isScoped(); 11342 Diagnosed = true; 11343 continue; 11344 } 11345 11346 if (FirstEnum->isScoped() && SecondEnum->isScoped()) { 11347 if (FirstEnum->isScopedUsingClassTag() != 11348 SecondEnum->isScopedUsingClassTag()) { 11349 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11350 EnumTagKeywordMismatch) 11351 << FirstEnum->isScopedUsingClassTag(); 11352 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11353 EnumTagKeywordMismatch) 11354 << SecondEnum->isScopedUsingClassTag(); 11355 Diagnosed = true; 11356 continue; 11357 } 11358 } 11359 11360 QualType FirstUnderlyingType = 11361 FirstEnum->getIntegerTypeSourceInfo() 11362 ? FirstEnum->getIntegerTypeSourceInfo()->getType() 11363 : QualType(); 11364 QualType SecondUnderlyingType = 11365 SecondEnum->getIntegerTypeSourceInfo() 11366 ? SecondEnum->getIntegerTypeSourceInfo()->getType() 11367 : QualType(); 11368 if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) { 11369 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11370 SingleSpecifiedType) 11371 << !FirstUnderlyingType.isNull(); 11372 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11373 SingleSpecifiedType) 11374 << !SecondUnderlyingType.isNull(); 11375 Diagnosed = true; 11376 continue; 11377 } 11378 11379 if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) { 11380 if (ComputeQualTypeODRHash(FirstUnderlyingType) != 11381 ComputeQualTypeODRHash(SecondUnderlyingType)) { 11382 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11383 DifferentSpecifiedTypes) 11384 << FirstUnderlyingType; 11385 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11386 DifferentSpecifiedTypes) 11387 << SecondUnderlyingType; 11388 Diagnosed = true; 11389 continue; 11390 } 11391 } 11392 11393 DeclHashes SecondHashes; 11394 PopulateHashes(SecondHashes, SecondEnum); 11395 11396 if (FirstHashes.size() != SecondHashes.size()) { 11397 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11398 DifferentNumberEnumConstants) 11399 << (int)FirstHashes.size(); 11400 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11401 DifferentNumberEnumConstants) 11402 << (int)SecondHashes.size(); 11403 Diagnosed = true; 11404 continue; 11405 } 11406 11407 for (unsigned I = 0; I < FirstHashes.size(); ++I) { 11408 if (FirstHashes[I].second == SecondHashes[I].second) 11409 continue; 11410 const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first; 11411 const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first; 11412 11413 if (FirstEnumConstant->getDeclName() != 11414 SecondEnumConstant->getDeclName()) { 11415 11416 ODRDiagError(FirstEnumConstant->getLocation(), 11417 FirstEnumConstant->getSourceRange(), EnumConstantName) 11418 << I + 1 << FirstEnumConstant; 11419 ODRDiagNote(SecondEnumConstant->getLocation(), 11420 SecondEnumConstant->getSourceRange(), EnumConstantName) 11421 << I + 1 << SecondEnumConstant; 11422 Diagnosed = true; 11423 break; 11424 } 11425 11426 const Expr *FirstInit = FirstEnumConstant->getInitExpr(); 11427 const Expr *SecondInit = SecondEnumConstant->getInitExpr(); 11428 if (!FirstInit && !SecondInit) 11429 continue; 11430 11431 if (!FirstInit || !SecondInit) { 11432 ODRDiagError(FirstEnumConstant->getLocation(), 11433 FirstEnumConstant->getSourceRange(), 11434 EnumConstantSingleInitilizer) 11435 << I + 1 << FirstEnumConstant << (FirstInit != nullptr); 11436 ODRDiagNote(SecondEnumConstant->getLocation(), 11437 SecondEnumConstant->getSourceRange(), 11438 EnumConstantSingleInitilizer) 11439 << I + 1 << SecondEnumConstant << (SecondInit != nullptr); 11440 Diagnosed = true; 11441 break; 11442 } 11443 11444 if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11445 ODRDiagError(FirstEnumConstant->getLocation(), 11446 FirstEnumConstant->getSourceRange(), 11447 EnumConstantDifferentInitilizer) 11448 << I + 1 << FirstEnumConstant; 11449 ODRDiagNote(SecondEnumConstant->getLocation(), 11450 SecondEnumConstant->getSourceRange(), 11451 EnumConstantDifferentInitilizer) 11452 << I + 1 << SecondEnumConstant; 11453 Diagnosed = true; 11454 break; 11455 } 11456 } 11457 } 11458 11459 (void)Diagnosed; 11460 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11461 } 11462 } 11463 11464 void ASTReader::StartedDeserializing() { 11465 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get()) 11466 ReadTimer->startTimer(); 11467 } 11468 11469 void ASTReader::FinishedDeserializing() { 11470 assert(NumCurrentElementsDeserializing && 11471 "FinishedDeserializing not paired with StartedDeserializing"); 11472 if (NumCurrentElementsDeserializing == 1) { 11473 // We decrease NumCurrentElementsDeserializing only after pending actions 11474 // are finished, to avoid recursively re-calling finishPendingActions(). 11475 finishPendingActions(); 11476 } 11477 --NumCurrentElementsDeserializing; 11478 11479 if (NumCurrentElementsDeserializing == 0) { 11480 // Propagate exception specification and deduced type updates along 11481 // redeclaration chains. 11482 // 11483 // We do this now rather than in finishPendingActions because we want to 11484 // be able to walk the complete redeclaration chains of the updated decls. 11485 while (!PendingExceptionSpecUpdates.empty() || 11486 !PendingDeducedTypeUpdates.empty()) { 11487 auto ESUpdates = std::move(PendingExceptionSpecUpdates); 11488 PendingExceptionSpecUpdates.clear(); 11489 for (auto Update : ESUpdates) { 11490 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11491 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>(); 11492 auto ESI = FPT->getExtProtoInfo().ExceptionSpec; 11493 if (auto *Listener = getContext().getASTMutationListener()) 11494 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second)); 11495 for (auto *Redecl : Update.second->redecls()) 11496 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI); 11497 } 11498 11499 auto DTUpdates = std::move(PendingDeducedTypeUpdates); 11500 PendingDeducedTypeUpdates.clear(); 11501 for (auto Update : DTUpdates) { 11502 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11503 // FIXME: If the return type is already deduced, check that it matches. 11504 getContext().adjustDeducedFunctionResultType(Update.first, 11505 Update.second); 11506 } 11507 } 11508 11509 if (ReadTimer) 11510 ReadTimer->stopTimer(); 11511 11512 diagnoseOdrViolations(); 11513 11514 // We are not in recursive loading, so it's safe to pass the "interesting" 11515 // decls to the consumer. 11516 if (Consumer) 11517 PassInterestingDeclsToConsumer(); 11518 } 11519 } 11520 11521 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) { 11522 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) { 11523 // Remove any fake results before adding any real ones. 11524 auto It = PendingFakeLookupResults.find(II); 11525 if (It != PendingFakeLookupResults.end()) { 11526 for (auto *ND : It->second) 11527 SemaObj->IdResolver.RemoveDecl(ND); 11528 // FIXME: this works around module+PCH performance issue. 11529 // Rather than erase the result from the map, which is O(n), just clear 11530 // the vector of NamedDecls. 11531 It->second.clear(); 11532 } 11533 } 11534 11535 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) { 11536 SemaObj->TUScope->AddDecl(D); 11537 } else if (SemaObj->TUScope) { 11538 // Adding the decl to IdResolver may have failed because it was already in 11539 // (even though it was not added in scope). If it is already in, make sure 11540 // it gets in the scope as well. 11541 if (std::find(SemaObj->IdResolver.begin(Name), 11542 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end()) 11543 SemaObj->TUScope->AddDecl(D); 11544 } 11545 } 11546 11547 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache, 11548 ASTContext *Context, 11549 const PCHContainerReader &PCHContainerRdr, 11550 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, 11551 StringRef isysroot, 11552 DisableValidationForModuleKind DisableValidationKind, 11553 bool AllowASTWithCompilerErrors, 11554 bool AllowConfigurationMismatch, bool ValidateSystemInputs, 11555 bool ValidateASTInputFilesContent, bool UseGlobalIndex, 11556 std::unique_ptr<llvm::Timer> ReadTimer) 11557 : Listener(bool(DisableValidationKind &DisableValidationForModuleKind::PCH) 11558 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP)) 11559 : cast<ASTReaderListener>(new PCHValidator(PP, *this))), 11560 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), 11561 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP), 11562 ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache, 11563 PCHContainerRdr, PP.getHeaderSearchInfo()), 11564 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot), 11565 DisableValidationKind(DisableValidationKind), 11566 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors), 11567 AllowConfigurationMismatch(AllowConfigurationMismatch), 11568 ValidateSystemInputs(ValidateSystemInputs), 11569 ValidateASTInputFilesContent(ValidateASTInputFilesContent), 11570 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) { 11571 SourceMgr.setExternalSLocEntrySource(this); 11572 11573 for (const auto &Ext : Extensions) { 11574 auto BlockName = Ext->getExtensionMetadata().BlockName; 11575 auto Known = ModuleFileExtensions.find(BlockName); 11576 if (Known != ModuleFileExtensions.end()) { 11577 Diags.Report(diag::warn_duplicate_module_file_extension) 11578 << BlockName; 11579 continue; 11580 } 11581 11582 ModuleFileExtensions.insert({BlockName, Ext}); 11583 } 11584 } 11585 11586 ASTReader::~ASTReader() { 11587 if (OwnsDeserializationListener) 11588 delete DeserializationListener; 11589 } 11590 11591 IdentifierResolver &ASTReader::getIdResolver() { 11592 return SemaObj ? SemaObj->IdResolver : DummyIdResolver; 11593 } 11594 11595 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor, 11596 unsigned AbbrevID) { 11597 Idx = 0; 11598 Record.clear(); 11599 return Cursor.readRecord(AbbrevID, Record); 11600 } 11601 //===----------------------------------------------------------------------===// 11602 //// OMPClauseReader implementation 11603 ////===----------------------------------------------------------------------===// 11604 11605 // This has to be in namespace clang because it's friended by all 11606 // of the OMP clauses. 11607 namespace clang { 11608 11609 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> { 11610 ASTRecordReader &Record; 11611 ASTContext &Context; 11612 11613 public: 11614 OMPClauseReader(ASTRecordReader &Record) 11615 : Record(Record), Context(Record.getContext()) {} 11616 #define GEN_CLANG_CLAUSE_CLASS 11617 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C); 11618 #include "llvm/Frontend/OpenMP/OMP.inc" 11619 OMPClause *readClause(); 11620 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C); 11621 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C); 11622 }; 11623 11624 } // end namespace clang 11625 11626 OMPClause *ASTRecordReader::readOMPClause() { 11627 return OMPClauseReader(*this).readClause(); 11628 } 11629 11630 OMPClause *OMPClauseReader::readClause() { 11631 OMPClause *C = nullptr; 11632 switch (llvm::omp::Clause(Record.readInt())) { 11633 case llvm::omp::OMPC_if: 11634 C = new (Context) OMPIfClause(); 11635 break; 11636 case llvm::omp::OMPC_final: 11637 C = new (Context) OMPFinalClause(); 11638 break; 11639 case llvm::omp::OMPC_num_threads: 11640 C = new (Context) OMPNumThreadsClause(); 11641 break; 11642 case llvm::omp::OMPC_safelen: 11643 C = new (Context) OMPSafelenClause(); 11644 break; 11645 case llvm::omp::OMPC_simdlen: 11646 C = new (Context) OMPSimdlenClause(); 11647 break; 11648 case llvm::omp::OMPC_sizes: { 11649 unsigned NumSizes = Record.readInt(); 11650 C = OMPSizesClause::CreateEmpty(Context, NumSizes); 11651 break; 11652 } 11653 case llvm::omp::OMPC_full: 11654 C = OMPFullClause::CreateEmpty(Context); 11655 break; 11656 case llvm::omp::OMPC_partial: 11657 C = OMPPartialClause::CreateEmpty(Context); 11658 break; 11659 case llvm::omp::OMPC_allocator: 11660 C = new (Context) OMPAllocatorClause(); 11661 break; 11662 case llvm::omp::OMPC_collapse: 11663 C = new (Context) OMPCollapseClause(); 11664 break; 11665 case llvm::omp::OMPC_default: 11666 C = new (Context) OMPDefaultClause(); 11667 break; 11668 case llvm::omp::OMPC_proc_bind: 11669 C = new (Context) OMPProcBindClause(); 11670 break; 11671 case llvm::omp::OMPC_schedule: 11672 C = new (Context) OMPScheduleClause(); 11673 break; 11674 case llvm::omp::OMPC_ordered: 11675 C = OMPOrderedClause::CreateEmpty(Context, Record.readInt()); 11676 break; 11677 case llvm::omp::OMPC_nowait: 11678 C = new (Context) OMPNowaitClause(); 11679 break; 11680 case llvm::omp::OMPC_untied: 11681 C = new (Context) OMPUntiedClause(); 11682 break; 11683 case llvm::omp::OMPC_mergeable: 11684 C = new (Context) OMPMergeableClause(); 11685 break; 11686 case llvm::omp::OMPC_read: 11687 C = new (Context) OMPReadClause(); 11688 break; 11689 case llvm::omp::OMPC_write: 11690 C = new (Context) OMPWriteClause(); 11691 break; 11692 case llvm::omp::OMPC_update: 11693 C = OMPUpdateClause::CreateEmpty(Context, Record.readInt()); 11694 break; 11695 case llvm::omp::OMPC_capture: 11696 C = new (Context) OMPCaptureClause(); 11697 break; 11698 case llvm::omp::OMPC_compare: 11699 C = new (Context) OMPCompareClause(); 11700 break; 11701 case llvm::omp::OMPC_seq_cst: 11702 C = new (Context) OMPSeqCstClause(); 11703 break; 11704 case llvm::omp::OMPC_acq_rel: 11705 C = new (Context) OMPAcqRelClause(); 11706 break; 11707 case llvm::omp::OMPC_acquire: 11708 C = new (Context) OMPAcquireClause(); 11709 break; 11710 case llvm::omp::OMPC_release: 11711 C = new (Context) OMPReleaseClause(); 11712 break; 11713 case llvm::omp::OMPC_relaxed: 11714 C = new (Context) OMPRelaxedClause(); 11715 break; 11716 case llvm::omp::OMPC_threads: 11717 C = new (Context) OMPThreadsClause(); 11718 break; 11719 case llvm::omp::OMPC_simd: 11720 C = new (Context) OMPSIMDClause(); 11721 break; 11722 case llvm::omp::OMPC_nogroup: 11723 C = new (Context) OMPNogroupClause(); 11724 break; 11725 case llvm::omp::OMPC_unified_address: 11726 C = new (Context) OMPUnifiedAddressClause(); 11727 break; 11728 case llvm::omp::OMPC_unified_shared_memory: 11729 C = new (Context) OMPUnifiedSharedMemoryClause(); 11730 break; 11731 case llvm::omp::OMPC_reverse_offload: 11732 C = new (Context) OMPReverseOffloadClause(); 11733 break; 11734 case llvm::omp::OMPC_dynamic_allocators: 11735 C = new (Context) OMPDynamicAllocatorsClause(); 11736 break; 11737 case llvm::omp::OMPC_atomic_default_mem_order: 11738 C = new (Context) OMPAtomicDefaultMemOrderClause(); 11739 break; 11740 case llvm::omp::OMPC_private: 11741 C = OMPPrivateClause::CreateEmpty(Context, Record.readInt()); 11742 break; 11743 case llvm::omp::OMPC_firstprivate: 11744 C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt()); 11745 break; 11746 case llvm::omp::OMPC_lastprivate: 11747 C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt()); 11748 break; 11749 case llvm::omp::OMPC_shared: 11750 C = OMPSharedClause::CreateEmpty(Context, Record.readInt()); 11751 break; 11752 case llvm::omp::OMPC_reduction: { 11753 unsigned N = Record.readInt(); 11754 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>(); 11755 C = OMPReductionClause::CreateEmpty(Context, N, Modifier); 11756 break; 11757 } 11758 case llvm::omp::OMPC_task_reduction: 11759 C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt()); 11760 break; 11761 case llvm::omp::OMPC_in_reduction: 11762 C = OMPInReductionClause::CreateEmpty(Context, Record.readInt()); 11763 break; 11764 case llvm::omp::OMPC_linear: 11765 C = OMPLinearClause::CreateEmpty(Context, Record.readInt()); 11766 break; 11767 case llvm::omp::OMPC_aligned: 11768 C = OMPAlignedClause::CreateEmpty(Context, Record.readInt()); 11769 break; 11770 case llvm::omp::OMPC_copyin: 11771 C = OMPCopyinClause::CreateEmpty(Context, Record.readInt()); 11772 break; 11773 case llvm::omp::OMPC_copyprivate: 11774 C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt()); 11775 break; 11776 case llvm::omp::OMPC_flush: 11777 C = OMPFlushClause::CreateEmpty(Context, Record.readInt()); 11778 break; 11779 case llvm::omp::OMPC_depobj: 11780 C = OMPDepobjClause::CreateEmpty(Context); 11781 break; 11782 case llvm::omp::OMPC_depend: { 11783 unsigned NumVars = Record.readInt(); 11784 unsigned NumLoops = Record.readInt(); 11785 C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops); 11786 break; 11787 } 11788 case llvm::omp::OMPC_device: 11789 C = new (Context) OMPDeviceClause(); 11790 break; 11791 case llvm::omp::OMPC_map: { 11792 OMPMappableExprListSizeTy Sizes; 11793 Sizes.NumVars = Record.readInt(); 11794 Sizes.NumUniqueDeclarations = Record.readInt(); 11795 Sizes.NumComponentLists = Record.readInt(); 11796 Sizes.NumComponents = Record.readInt(); 11797 C = OMPMapClause::CreateEmpty(Context, Sizes); 11798 break; 11799 } 11800 case llvm::omp::OMPC_num_teams: 11801 C = new (Context) OMPNumTeamsClause(); 11802 break; 11803 case llvm::omp::OMPC_thread_limit: 11804 C = new (Context) OMPThreadLimitClause(); 11805 break; 11806 case llvm::omp::OMPC_priority: 11807 C = new (Context) OMPPriorityClause(); 11808 break; 11809 case llvm::omp::OMPC_grainsize: 11810 C = new (Context) OMPGrainsizeClause(); 11811 break; 11812 case llvm::omp::OMPC_num_tasks: 11813 C = new (Context) OMPNumTasksClause(); 11814 break; 11815 case llvm::omp::OMPC_hint: 11816 C = new (Context) OMPHintClause(); 11817 break; 11818 case llvm::omp::OMPC_dist_schedule: 11819 C = new (Context) OMPDistScheduleClause(); 11820 break; 11821 case llvm::omp::OMPC_defaultmap: 11822 C = new (Context) OMPDefaultmapClause(); 11823 break; 11824 case llvm::omp::OMPC_to: { 11825 OMPMappableExprListSizeTy Sizes; 11826 Sizes.NumVars = Record.readInt(); 11827 Sizes.NumUniqueDeclarations = Record.readInt(); 11828 Sizes.NumComponentLists = Record.readInt(); 11829 Sizes.NumComponents = Record.readInt(); 11830 C = OMPToClause::CreateEmpty(Context, Sizes); 11831 break; 11832 } 11833 case llvm::omp::OMPC_from: { 11834 OMPMappableExprListSizeTy Sizes; 11835 Sizes.NumVars = Record.readInt(); 11836 Sizes.NumUniqueDeclarations = Record.readInt(); 11837 Sizes.NumComponentLists = Record.readInt(); 11838 Sizes.NumComponents = Record.readInt(); 11839 C = OMPFromClause::CreateEmpty(Context, Sizes); 11840 break; 11841 } 11842 case llvm::omp::OMPC_use_device_ptr: { 11843 OMPMappableExprListSizeTy Sizes; 11844 Sizes.NumVars = Record.readInt(); 11845 Sizes.NumUniqueDeclarations = Record.readInt(); 11846 Sizes.NumComponentLists = Record.readInt(); 11847 Sizes.NumComponents = Record.readInt(); 11848 C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes); 11849 break; 11850 } 11851 case llvm::omp::OMPC_use_device_addr: { 11852 OMPMappableExprListSizeTy Sizes; 11853 Sizes.NumVars = Record.readInt(); 11854 Sizes.NumUniqueDeclarations = Record.readInt(); 11855 Sizes.NumComponentLists = Record.readInt(); 11856 Sizes.NumComponents = Record.readInt(); 11857 C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes); 11858 break; 11859 } 11860 case llvm::omp::OMPC_is_device_ptr: { 11861 OMPMappableExprListSizeTy Sizes; 11862 Sizes.NumVars = Record.readInt(); 11863 Sizes.NumUniqueDeclarations = Record.readInt(); 11864 Sizes.NumComponentLists = Record.readInt(); 11865 Sizes.NumComponents = Record.readInt(); 11866 C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes); 11867 break; 11868 } 11869 case llvm::omp::OMPC_has_device_addr: { 11870 OMPMappableExprListSizeTy Sizes; 11871 Sizes.NumVars = Record.readInt(); 11872 Sizes.NumUniqueDeclarations = Record.readInt(); 11873 Sizes.NumComponentLists = Record.readInt(); 11874 Sizes.NumComponents = Record.readInt(); 11875 C = OMPHasDeviceAddrClause::CreateEmpty(Context, Sizes); 11876 break; 11877 } 11878 case llvm::omp::OMPC_allocate: 11879 C = OMPAllocateClause::CreateEmpty(Context, Record.readInt()); 11880 break; 11881 case llvm::omp::OMPC_nontemporal: 11882 C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt()); 11883 break; 11884 case llvm::omp::OMPC_inclusive: 11885 C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt()); 11886 break; 11887 case llvm::omp::OMPC_exclusive: 11888 C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt()); 11889 break; 11890 case llvm::omp::OMPC_order: 11891 C = new (Context) OMPOrderClause(); 11892 break; 11893 case llvm::omp::OMPC_init: 11894 C = OMPInitClause::CreateEmpty(Context, Record.readInt()); 11895 break; 11896 case llvm::omp::OMPC_use: 11897 C = new (Context) OMPUseClause(); 11898 break; 11899 case llvm::omp::OMPC_destroy: 11900 C = new (Context) OMPDestroyClause(); 11901 break; 11902 case llvm::omp::OMPC_novariants: 11903 C = new (Context) OMPNovariantsClause(); 11904 break; 11905 case llvm::omp::OMPC_nocontext: 11906 C = new (Context) OMPNocontextClause(); 11907 break; 11908 case llvm::omp::OMPC_detach: 11909 C = new (Context) OMPDetachClause(); 11910 break; 11911 case llvm::omp::OMPC_uses_allocators: 11912 C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt()); 11913 break; 11914 case llvm::omp::OMPC_affinity: 11915 C = OMPAffinityClause::CreateEmpty(Context, Record.readInt()); 11916 break; 11917 case llvm::omp::OMPC_filter: 11918 C = new (Context) OMPFilterClause(); 11919 break; 11920 case llvm::omp::OMPC_bind: 11921 C = OMPBindClause::CreateEmpty(Context); 11922 break; 11923 case llvm::omp::OMPC_align: 11924 C = new (Context) OMPAlignClause(); 11925 break; 11926 #define OMP_CLAUSE_NO_CLASS(Enum, Str) \ 11927 case llvm::omp::Enum: \ 11928 break; 11929 #include "llvm/Frontend/OpenMP/OMPKinds.def" 11930 default: 11931 break; 11932 } 11933 assert(C && "Unknown OMPClause type"); 11934 11935 Visit(C); 11936 C->setLocStart(Record.readSourceLocation()); 11937 C->setLocEnd(Record.readSourceLocation()); 11938 11939 return C; 11940 } 11941 11942 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) { 11943 C->setPreInitStmt(Record.readSubStmt(), 11944 static_cast<OpenMPDirectiveKind>(Record.readInt())); 11945 } 11946 11947 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) { 11948 VisitOMPClauseWithPreInit(C); 11949 C->setPostUpdateExpr(Record.readSubExpr()); 11950 } 11951 11952 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) { 11953 VisitOMPClauseWithPreInit(C); 11954 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt())); 11955 C->setNameModifierLoc(Record.readSourceLocation()); 11956 C->setColonLoc(Record.readSourceLocation()); 11957 C->setCondition(Record.readSubExpr()); 11958 C->setLParenLoc(Record.readSourceLocation()); 11959 } 11960 11961 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) { 11962 VisitOMPClauseWithPreInit(C); 11963 C->setCondition(Record.readSubExpr()); 11964 C->setLParenLoc(Record.readSourceLocation()); 11965 } 11966 11967 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) { 11968 VisitOMPClauseWithPreInit(C); 11969 C->setNumThreads(Record.readSubExpr()); 11970 C->setLParenLoc(Record.readSourceLocation()); 11971 } 11972 11973 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) { 11974 C->setSafelen(Record.readSubExpr()); 11975 C->setLParenLoc(Record.readSourceLocation()); 11976 } 11977 11978 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) { 11979 C->setSimdlen(Record.readSubExpr()); 11980 C->setLParenLoc(Record.readSourceLocation()); 11981 } 11982 11983 void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) { 11984 for (Expr *&E : C->getSizesRefs()) 11985 E = Record.readSubExpr(); 11986 C->setLParenLoc(Record.readSourceLocation()); 11987 } 11988 11989 void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {} 11990 11991 void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) { 11992 C->setFactor(Record.readSubExpr()); 11993 C->setLParenLoc(Record.readSourceLocation()); 11994 } 11995 11996 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) { 11997 C->setAllocator(Record.readExpr()); 11998 C->setLParenLoc(Record.readSourceLocation()); 11999 } 12000 12001 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) { 12002 C->setNumForLoops(Record.readSubExpr()); 12003 C->setLParenLoc(Record.readSourceLocation()); 12004 } 12005 12006 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) { 12007 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt())); 12008 C->setLParenLoc(Record.readSourceLocation()); 12009 C->setDefaultKindKwLoc(Record.readSourceLocation()); 12010 } 12011 12012 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) { 12013 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt())); 12014 C->setLParenLoc(Record.readSourceLocation()); 12015 C->setProcBindKindKwLoc(Record.readSourceLocation()); 12016 } 12017 12018 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) { 12019 VisitOMPClauseWithPreInit(C); 12020 C->setScheduleKind( 12021 static_cast<OpenMPScheduleClauseKind>(Record.readInt())); 12022 C->setFirstScheduleModifier( 12023 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 12024 C->setSecondScheduleModifier( 12025 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 12026 C->setChunkSize(Record.readSubExpr()); 12027 C->setLParenLoc(Record.readSourceLocation()); 12028 C->setFirstScheduleModifierLoc(Record.readSourceLocation()); 12029 C->setSecondScheduleModifierLoc(Record.readSourceLocation()); 12030 C->setScheduleKindLoc(Record.readSourceLocation()); 12031 C->setCommaLoc(Record.readSourceLocation()); 12032 } 12033 12034 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) { 12035 C->setNumForLoops(Record.readSubExpr()); 12036 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 12037 C->setLoopNumIterations(I, Record.readSubExpr()); 12038 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 12039 C->setLoopCounter(I, Record.readSubExpr()); 12040 C->setLParenLoc(Record.readSourceLocation()); 12041 } 12042 12043 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) { 12044 C->setEventHandler(Record.readSubExpr()); 12045 C->setLParenLoc(Record.readSourceLocation()); 12046 } 12047 12048 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {} 12049 12050 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {} 12051 12052 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {} 12053 12054 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {} 12055 12056 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {} 12057 12058 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) { 12059 if (C->isExtended()) { 12060 C->setLParenLoc(Record.readSourceLocation()); 12061 C->setArgumentLoc(Record.readSourceLocation()); 12062 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>()); 12063 } 12064 } 12065 12066 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {} 12067 12068 void OMPClauseReader::VisitOMPCompareClause(OMPCompareClause *) {} 12069 12070 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {} 12071 12072 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {} 12073 12074 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {} 12075 12076 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {} 12077 12078 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {} 12079 12080 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {} 12081 12082 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {} 12083 12084 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {} 12085 12086 void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) { 12087 unsigned NumVars = C->varlist_size(); 12088 SmallVector<Expr *, 16> Vars; 12089 Vars.reserve(NumVars); 12090 for (unsigned I = 0; I != NumVars; ++I) 12091 Vars.push_back(Record.readSubExpr()); 12092 C->setVarRefs(Vars); 12093 C->setIsTarget(Record.readBool()); 12094 C->setIsTargetSync(Record.readBool()); 12095 C->setLParenLoc(Record.readSourceLocation()); 12096 C->setVarLoc(Record.readSourceLocation()); 12097 } 12098 12099 void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) { 12100 C->setInteropVar(Record.readSubExpr()); 12101 C->setLParenLoc(Record.readSourceLocation()); 12102 C->setVarLoc(Record.readSourceLocation()); 12103 } 12104 12105 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) { 12106 C->setInteropVar(Record.readSubExpr()); 12107 C->setLParenLoc(Record.readSourceLocation()); 12108 C->setVarLoc(Record.readSourceLocation()); 12109 } 12110 12111 void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) { 12112 VisitOMPClauseWithPreInit(C); 12113 C->setCondition(Record.readSubExpr()); 12114 C->setLParenLoc(Record.readSourceLocation()); 12115 } 12116 12117 void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) { 12118 VisitOMPClauseWithPreInit(C); 12119 C->setCondition(Record.readSubExpr()); 12120 C->setLParenLoc(Record.readSourceLocation()); 12121 } 12122 12123 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {} 12124 12125 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause( 12126 OMPUnifiedSharedMemoryClause *) {} 12127 12128 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {} 12129 12130 void 12131 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) { 12132 } 12133 12134 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause( 12135 OMPAtomicDefaultMemOrderClause *C) { 12136 C->setAtomicDefaultMemOrderKind( 12137 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt())); 12138 C->setLParenLoc(Record.readSourceLocation()); 12139 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation()); 12140 } 12141 12142 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) { 12143 C->setLParenLoc(Record.readSourceLocation()); 12144 unsigned NumVars = C->varlist_size(); 12145 SmallVector<Expr *, 16> Vars; 12146 Vars.reserve(NumVars); 12147 for (unsigned i = 0; i != NumVars; ++i) 12148 Vars.push_back(Record.readSubExpr()); 12149 C->setVarRefs(Vars); 12150 Vars.clear(); 12151 for (unsigned i = 0; i != NumVars; ++i) 12152 Vars.push_back(Record.readSubExpr()); 12153 C->setPrivateCopies(Vars); 12154 } 12155 12156 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) { 12157 VisitOMPClauseWithPreInit(C); 12158 C->setLParenLoc(Record.readSourceLocation()); 12159 unsigned NumVars = C->varlist_size(); 12160 SmallVector<Expr *, 16> Vars; 12161 Vars.reserve(NumVars); 12162 for (unsigned i = 0; i != NumVars; ++i) 12163 Vars.push_back(Record.readSubExpr()); 12164 C->setVarRefs(Vars); 12165 Vars.clear(); 12166 for (unsigned i = 0; i != NumVars; ++i) 12167 Vars.push_back(Record.readSubExpr()); 12168 C->setPrivateCopies(Vars); 12169 Vars.clear(); 12170 for (unsigned i = 0; i != NumVars; ++i) 12171 Vars.push_back(Record.readSubExpr()); 12172 C->setInits(Vars); 12173 } 12174 12175 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) { 12176 VisitOMPClauseWithPostUpdate(C); 12177 C->setLParenLoc(Record.readSourceLocation()); 12178 C->setKind(Record.readEnum<OpenMPLastprivateModifier>()); 12179 C->setKindLoc(Record.readSourceLocation()); 12180 C->setColonLoc(Record.readSourceLocation()); 12181 unsigned NumVars = C->varlist_size(); 12182 SmallVector<Expr *, 16> Vars; 12183 Vars.reserve(NumVars); 12184 for (unsigned i = 0; i != NumVars; ++i) 12185 Vars.push_back(Record.readSubExpr()); 12186 C->setVarRefs(Vars); 12187 Vars.clear(); 12188 for (unsigned i = 0; i != NumVars; ++i) 12189 Vars.push_back(Record.readSubExpr()); 12190 C->setPrivateCopies(Vars); 12191 Vars.clear(); 12192 for (unsigned i = 0; i != NumVars; ++i) 12193 Vars.push_back(Record.readSubExpr()); 12194 C->setSourceExprs(Vars); 12195 Vars.clear(); 12196 for (unsigned i = 0; i != NumVars; ++i) 12197 Vars.push_back(Record.readSubExpr()); 12198 C->setDestinationExprs(Vars); 12199 Vars.clear(); 12200 for (unsigned i = 0; i != NumVars; ++i) 12201 Vars.push_back(Record.readSubExpr()); 12202 C->setAssignmentOps(Vars); 12203 } 12204 12205 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) { 12206 C->setLParenLoc(Record.readSourceLocation()); 12207 unsigned NumVars = C->varlist_size(); 12208 SmallVector<Expr *, 16> Vars; 12209 Vars.reserve(NumVars); 12210 for (unsigned i = 0; i != NumVars; ++i) 12211 Vars.push_back(Record.readSubExpr()); 12212 C->setVarRefs(Vars); 12213 } 12214 12215 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) { 12216 VisitOMPClauseWithPostUpdate(C); 12217 C->setLParenLoc(Record.readSourceLocation()); 12218 C->setModifierLoc(Record.readSourceLocation()); 12219 C->setColonLoc(Record.readSourceLocation()); 12220 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12221 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12222 C->setQualifierLoc(NNSL); 12223 C->setNameInfo(DNI); 12224 12225 unsigned NumVars = C->varlist_size(); 12226 SmallVector<Expr *, 16> Vars; 12227 Vars.reserve(NumVars); 12228 for (unsigned i = 0; i != NumVars; ++i) 12229 Vars.push_back(Record.readSubExpr()); 12230 C->setVarRefs(Vars); 12231 Vars.clear(); 12232 for (unsigned i = 0; i != NumVars; ++i) 12233 Vars.push_back(Record.readSubExpr()); 12234 C->setPrivates(Vars); 12235 Vars.clear(); 12236 for (unsigned i = 0; i != NumVars; ++i) 12237 Vars.push_back(Record.readSubExpr()); 12238 C->setLHSExprs(Vars); 12239 Vars.clear(); 12240 for (unsigned i = 0; i != NumVars; ++i) 12241 Vars.push_back(Record.readSubExpr()); 12242 C->setRHSExprs(Vars); 12243 Vars.clear(); 12244 for (unsigned i = 0; i != NumVars; ++i) 12245 Vars.push_back(Record.readSubExpr()); 12246 C->setReductionOps(Vars); 12247 if (C->getModifier() == OMPC_REDUCTION_inscan) { 12248 Vars.clear(); 12249 for (unsigned i = 0; i != NumVars; ++i) 12250 Vars.push_back(Record.readSubExpr()); 12251 C->setInscanCopyOps(Vars); 12252 Vars.clear(); 12253 for (unsigned i = 0; i != NumVars; ++i) 12254 Vars.push_back(Record.readSubExpr()); 12255 C->setInscanCopyArrayTemps(Vars); 12256 Vars.clear(); 12257 for (unsigned i = 0; i != NumVars; ++i) 12258 Vars.push_back(Record.readSubExpr()); 12259 C->setInscanCopyArrayElems(Vars); 12260 } 12261 } 12262 12263 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) { 12264 VisitOMPClauseWithPostUpdate(C); 12265 C->setLParenLoc(Record.readSourceLocation()); 12266 C->setColonLoc(Record.readSourceLocation()); 12267 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12268 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12269 C->setQualifierLoc(NNSL); 12270 C->setNameInfo(DNI); 12271 12272 unsigned NumVars = C->varlist_size(); 12273 SmallVector<Expr *, 16> Vars; 12274 Vars.reserve(NumVars); 12275 for (unsigned I = 0; I != NumVars; ++I) 12276 Vars.push_back(Record.readSubExpr()); 12277 C->setVarRefs(Vars); 12278 Vars.clear(); 12279 for (unsigned I = 0; I != NumVars; ++I) 12280 Vars.push_back(Record.readSubExpr()); 12281 C->setPrivates(Vars); 12282 Vars.clear(); 12283 for (unsigned I = 0; I != NumVars; ++I) 12284 Vars.push_back(Record.readSubExpr()); 12285 C->setLHSExprs(Vars); 12286 Vars.clear(); 12287 for (unsigned I = 0; I != NumVars; ++I) 12288 Vars.push_back(Record.readSubExpr()); 12289 C->setRHSExprs(Vars); 12290 Vars.clear(); 12291 for (unsigned I = 0; I != NumVars; ++I) 12292 Vars.push_back(Record.readSubExpr()); 12293 C->setReductionOps(Vars); 12294 } 12295 12296 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) { 12297 VisitOMPClauseWithPostUpdate(C); 12298 C->setLParenLoc(Record.readSourceLocation()); 12299 C->setColonLoc(Record.readSourceLocation()); 12300 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12301 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12302 C->setQualifierLoc(NNSL); 12303 C->setNameInfo(DNI); 12304 12305 unsigned NumVars = C->varlist_size(); 12306 SmallVector<Expr *, 16> Vars; 12307 Vars.reserve(NumVars); 12308 for (unsigned I = 0; I != NumVars; ++I) 12309 Vars.push_back(Record.readSubExpr()); 12310 C->setVarRefs(Vars); 12311 Vars.clear(); 12312 for (unsigned I = 0; I != NumVars; ++I) 12313 Vars.push_back(Record.readSubExpr()); 12314 C->setPrivates(Vars); 12315 Vars.clear(); 12316 for (unsigned I = 0; I != NumVars; ++I) 12317 Vars.push_back(Record.readSubExpr()); 12318 C->setLHSExprs(Vars); 12319 Vars.clear(); 12320 for (unsigned I = 0; I != NumVars; ++I) 12321 Vars.push_back(Record.readSubExpr()); 12322 C->setRHSExprs(Vars); 12323 Vars.clear(); 12324 for (unsigned I = 0; I != NumVars; ++I) 12325 Vars.push_back(Record.readSubExpr()); 12326 C->setReductionOps(Vars); 12327 Vars.clear(); 12328 for (unsigned I = 0; I != NumVars; ++I) 12329 Vars.push_back(Record.readSubExpr()); 12330 C->setTaskgroupDescriptors(Vars); 12331 } 12332 12333 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) { 12334 VisitOMPClauseWithPostUpdate(C); 12335 C->setLParenLoc(Record.readSourceLocation()); 12336 C->setColonLoc(Record.readSourceLocation()); 12337 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt())); 12338 C->setModifierLoc(Record.readSourceLocation()); 12339 unsigned NumVars = C->varlist_size(); 12340 SmallVector<Expr *, 16> Vars; 12341 Vars.reserve(NumVars); 12342 for (unsigned i = 0; i != NumVars; ++i) 12343 Vars.push_back(Record.readSubExpr()); 12344 C->setVarRefs(Vars); 12345 Vars.clear(); 12346 for (unsigned i = 0; i != NumVars; ++i) 12347 Vars.push_back(Record.readSubExpr()); 12348 C->setPrivates(Vars); 12349 Vars.clear(); 12350 for (unsigned i = 0; i != NumVars; ++i) 12351 Vars.push_back(Record.readSubExpr()); 12352 C->setInits(Vars); 12353 Vars.clear(); 12354 for (unsigned i = 0; i != NumVars; ++i) 12355 Vars.push_back(Record.readSubExpr()); 12356 C->setUpdates(Vars); 12357 Vars.clear(); 12358 for (unsigned i = 0; i != NumVars; ++i) 12359 Vars.push_back(Record.readSubExpr()); 12360 C->setFinals(Vars); 12361 C->setStep(Record.readSubExpr()); 12362 C->setCalcStep(Record.readSubExpr()); 12363 Vars.clear(); 12364 for (unsigned I = 0; I != NumVars + 1; ++I) 12365 Vars.push_back(Record.readSubExpr()); 12366 C->setUsedExprs(Vars); 12367 } 12368 12369 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) { 12370 C->setLParenLoc(Record.readSourceLocation()); 12371 C->setColonLoc(Record.readSourceLocation()); 12372 unsigned NumVars = C->varlist_size(); 12373 SmallVector<Expr *, 16> Vars; 12374 Vars.reserve(NumVars); 12375 for (unsigned i = 0; i != NumVars; ++i) 12376 Vars.push_back(Record.readSubExpr()); 12377 C->setVarRefs(Vars); 12378 C->setAlignment(Record.readSubExpr()); 12379 } 12380 12381 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) { 12382 C->setLParenLoc(Record.readSourceLocation()); 12383 unsigned NumVars = C->varlist_size(); 12384 SmallVector<Expr *, 16> Exprs; 12385 Exprs.reserve(NumVars); 12386 for (unsigned i = 0; i != NumVars; ++i) 12387 Exprs.push_back(Record.readSubExpr()); 12388 C->setVarRefs(Exprs); 12389 Exprs.clear(); 12390 for (unsigned i = 0; i != NumVars; ++i) 12391 Exprs.push_back(Record.readSubExpr()); 12392 C->setSourceExprs(Exprs); 12393 Exprs.clear(); 12394 for (unsigned i = 0; i != NumVars; ++i) 12395 Exprs.push_back(Record.readSubExpr()); 12396 C->setDestinationExprs(Exprs); 12397 Exprs.clear(); 12398 for (unsigned i = 0; i != NumVars; ++i) 12399 Exprs.push_back(Record.readSubExpr()); 12400 C->setAssignmentOps(Exprs); 12401 } 12402 12403 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) { 12404 C->setLParenLoc(Record.readSourceLocation()); 12405 unsigned NumVars = C->varlist_size(); 12406 SmallVector<Expr *, 16> Exprs; 12407 Exprs.reserve(NumVars); 12408 for (unsigned i = 0; i != NumVars; ++i) 12409 Exprs.push_back(Record.readSubExpr()); 12410 C->setVarRefs(Exprs); 12411 Exprs.clear(); 12412 for (unsigned i = 0; i != NumVars; ++i) 12413 Exprs.push_back(Record.readSubExpr()); 12414 C->setSourceExprs(Exprs); 12415 Exprs.clear(); 12416 for (unsigned i = 0; i != NumVars; ++i) 12417 Exprs.push_back(Record.readSubExpr()); 12418 C->setDestinationExprs(Exprs); 12419 Exprs.clear(); 12420 for (unsigned i = 0; i != NumVars; ++i) 12421 Exprs.push_back(Record.readSubExpr()); 12422 C->setAssignmentOps(Exprs); 12423 } 12424 12425 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) { 12426 C->setLParenLoc(Record.readSourceLocation()); 12427 unsigned NumVars = C->varlist_size(); 12428 SmallVector<Expr *, 16> Vars; 12429 Vars.reserve(NumVars); 12430 for (unsigned i = 0; i != NumVars; ++i) 12431 Vars.push_back(Record.readSubExpr()); 12432 C->setVarRefs(Vars); 12433 } 12434 12435 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) { 12436 C->setDepobj(Record.readSubExpr()); 12437 C->setLParenLoc(Record.readSourceLocation()); 12438 } 12439 12440 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) { 12441 C->setLParenLoc(Record.readSourceLocation()); 12442 C->setModifier(Record.readSubExpr()); 12443 C->setDependencyKind( 12444 static_cast<OpenMPDependClauseKind>(Record.readInt())); 12445 C->setDependencyLoc(Record.readSourceLocation()); 12446 C->setColonLoc(Record.readSourceLocation()); 12447 unsigned NumVars = C->varlist_size(); 12448 SmallVector<Expr *, 16> Vars; 12449 Vars.reserve(NumVars); 12450 for (unsigned I = 0; I != NumVars; ++I) 12451 Vars.push_back(Record.readSubExpr()); 12452 C->setVarRefs(Vars); 12453 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I) 12454 C->setLoopData(I, Record.readSubExpr()); 12455 } 12456 12457 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) { 12458 VisitOMPClauseWithPreInit(C); 12459 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>()); 12460 C->setDevice(Record.readSubExpr()); 12461 C->setModifierLoc(Record.readSourceLocation()); 12462 C->setLParenLoc(Record.readSourceLocation()); 12463 } 12464 12465 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) { 12466 C->setLParenLoc(Record.readSourceLocation()); 12467 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) { 12468 C->setMapTypeModifier( 12469 I, static_cast<OpenMPMapModifierKind>(Record.readInt())); 12470 C->setMapTypeModifierLoc(I, Record.readSourceLocation()); 12471 } 12472 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12473 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12474 C->setMapType( 12475 static_cast<OpenMPMapClauseKind>(Record.readInt())); 12476 C->setMapLoc(Record.readSourceLocation()); 12477 C->setColonLoc(Record.readSourceLocation()); 12478 auto NumVars = C->varlist_size(); 12479 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12480 auto TotalLists = C->getTotalComponentListNum(); 12481 auto TotalComponents = C->getTotalComponentsNum(); 12482 12483 SmallVector<Expr *, 16> Vars; 12484 Vars.reserve(NumVars); 12485 for (unsigned i = 0; i != NumVars; ++i) 12486 Vars.push_back(Record.readExpr()); 12487 C->setVarRefs(Vars); 12488 12489 SmallVector<Expr *, 16> UDMappers; 12490 UDMappers.reserve(NumVars); 12491 for (unsigned I = 0; I < NumVars; ++I) 12492 UDMappers.push_back(Record.readExpr()); 12493 C->setUDMapperRefs(UDMappers); 12494 12495 SmallVector<ValueDecl *, 16> Decls; 12496 Decls.reserve(UniqueDecls); 12497 for (unsigned i = 0; i < UniqueDecls; ++i) 12498 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12499 C->setUniqueDecls(Decls); 12500 12501 SmallVector<unsigned, 16> ListsPerDecl; 12502 ListsPerDecl.reserve(UniqueDecls); 12503 for (unsigned i = 0; i < UniqueDecls; ++i) 12504 ListsPerDecl.push_back(Record.readInt()); 12505 C->setDeclNumLists(ListsPerDecl); 12506 12507 SmallVector<unsigned, 32> ListSizes; 12508 ListSizes.reserve(TotalLists); 12509 for (unsigned i = 0; i < TotalLists; ++i) 12510 ListSizes.push_back(Record.readInt()); 12511 C->setComponentListSizes(ListSizes); 12512 12513 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12514 Components.reserve(TotalComponents); 12515 for (unsigned i = 0; i < TotalComponents; ++i) { 12516 Expr *AssociatedExprPr = Record.readExpr(); 12517 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12518 Components.emplace_back(AssociatedExprPr, AssociatedDecl, 12519 /*IsNonContiguous=*/false); 12520 } 12521 C->setComponents(Components, ListSizes); 12522 } 12523 12524 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) { 12525 C->setLParenLoc(Record.readSourceLocation()); 12526 C->setColonLoc(Record.readSourceLocation()); 12527 C->setAllocator(Record.readSubExpr()); 12528 unsigned NumVars = C->varlist_size(); 12529 SmallVector<Expr *, 16> Vars; 12530 Vars.reserve(NumVars); 12531 for (unsigned i = 0; i != NumVars; ++i) 12532 Vars.push_back(Record.readSubExpr()); 12533 C->setVarRefs(Vars); 12534 } 12535 12536 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) { 12537 VisitOMPClauseWithPreInit(C); 12538 C->setNumTeams(Record.readSubExpr()); 12539 C->setLParenLoc(Record.readSourceLocation()); 12540 } 12541 12542 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) { 12543 VisitOMPClauseWithPreInit(C); 12544 C->setThreadLimit(Record.readSubExpr()); 12545 C->setLParenLoc(Record.readSourceLocation()); 12546 } 12547 12548 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) { 12549 VisitOMPClauseWithPreInit(C); 12550 C->setPriority(Record.readSubExpr()); 12551 C->setLParenLoc(Record.readSourceLocation()); 12552 } 12553 12554 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) { 12555 VisitOMPClauseWithPreInit(C); 12556 C->setGrainsize(Record.readSubExpr()); 12557 C->setLParenLoc(Record.readSourceLocation()); 12558 } 12559 12560 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) { 12561 VisitOMPClauseWithPreInit(C); 12562 C->setNumTasks(Record.readSubExpr()); 12563 C->setLParenLoc(Record.readSourceLocation()); 12564 } 12565 12566 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) { 12567 C->setHint(Record.readSubExpr()); 12568 C->setLParenLoc(Record.readSourceLocation()); 12569 } 12570 12571 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) { 12572 VisitOMPClauseWithPreInit(C); 12573 C->setDistScheduleKind( 12574 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt())); 12575 C->setChunkSize(Record.readSubExpr()); 12576 C->setLParenLoc(Record.readSourceLocation()); 12577 C->setDistScheduleKindLoc(Record.readSourceLocation()); 12578 C->setCommaLoc(Record.readSourceLocation()); 12579 } 12580 12581 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) { 12582 C->setDefaultmapKind( 12583 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt())); 12584 C->setDefaultmapModifier( 12585 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt())); 12586 C->setLParenLoc(Record.readSourceLocation()); 12587 C->setDefaultmapModifierLoc(Record.readSourceLocation()); 12588 C->setDefaultmapKindLoc(Record.readSourceLocation()); 12589 } 12590 12591 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) { 12592 C->setLParenLoc(Record.readSourceLocation()); 12593 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) { 12594 C->setMotionModifier( 12595 I, static_cast<OpenMPMotionModifierKind>(Record.readInt())); 12596 C->setMotionModifierLoc(I, Record.readSourceLocation()); 12597 } 12598 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12599 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12600 C->setColonLoc(Record.readSourceLocation()); 12601 auto NumVars = C->varlist_size(); 12602 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12603 auto TotalLists = C->getTotalComponentListNum(); 12604 auto TotalComponents = C->getTotalComponentsNum(); 12605 12606 SmallVector<Expr *, 16> Vars; 12607 Vars.reserve(NumVars); 12608 for (unsigned i = 0; i != NumVars; ++i) 12609 Vars.push_back(Record.readSubExpr()); 12610 C->setVarRefs(Vars); 12611 12612 SmallVector<Expr *, 16> UDMappers; 12613 UDMappers.reserve(NumVars); 12614 for (unsigned I = 0; I < NumVars; ++I) 12615 UDMappers.push_back(Record.readSubExpr()); 12616 C->setUDMapperRefs(UDMappers); 12617 12618 SmallVector<ValueDecl *, 16> Decls; 12619 Decls.reserve(UniqueDecls); 12620 for (unsigned i = 0; i < UniqueDecls; ++i) 12621 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12622 C->setUniqueDecls(Decls); 12623 12624 SmallVector<unsigned, 16> ListsPerDecl; 12625 ListsPerDecl.reserve(UniqueDecls); 12626 for (unsigned i = 0; i < UniqueDecls; ++i) 12627 ListsPerDecl.push_back(Record.readInt()); 12628 C->setDeclNumLists(ListsPerDecl); 12629 12630 SmallVector<unsigned, 32> ListSizes; 12631 ListSizes.reserve(TotalLists); 12632 for (unsigned i = 0; i < TotalLists; ++i) 12633 ListSizes.push_back(Record.readInt()); 12634 C->setComponentListSizes(ListSizes); 12635 12636 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12637 Components.reserve(TotalComponents); 12638 for (unsigned i = 0; i < TotalComponents; ++i) { 12639 Expr *AssociatedExprPr = Record.readSubExpr(); 12640 bool IsNonContiguous = Record.readBool(); 12641 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12642 Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous); 12643 } 12644 C->setComponents(Components, ListSizes); 12645 } 12646 12647 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) { 12648 C->setLParenLoc(Record.readSourceLocation()); 12649 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) { 12650 C->setMotionModifier( 12651 I, static_cast<OpenMPMotionModifierKind>(Record.readInt())); 12652 C->setMotionModifierLoc(I, Record.readSourceLocation()); 12653 } 12654 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12655 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12656 C->setColonLoc(Record.readSourceLocation()); 12657 auto NumVars = C->varlist_size(); 12658 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12659 auto TotalLists = C->getTotalComponentListNum(); 12660 auto TotalComponents = C->getTotalComponentsNum(); 12661 12662 SmallVector<Expr *, 16> Vars; 12663 Vars.reserve(NumVars); 12664 for (unsigned i = 0; i != NumVars; ++i) 12665 Vars.push_back(Record.readSubExpr()); 12666 C->setVarRefs(Vars); 12667 12668 SmallVector<Expr *, 16> UDMappers; 12669 UDMappers.reserve(NumVars); 12670 for (unsigned I = 0; I < NumVars; ++I) 12671 UDMappers.push_back(Record.readSubExpr()); 12672 C->setUDMapperRefs(UDMappers); 12673 12674 SmallVector<ValueDecl *, 16> Decls; 12675 Decls.reserve(UniqueDecls); 12676 for (unsigned i = 0; i < UniqueDecls; ++i) 12677 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12678 C->setUniqueDecls(Decls); 12679 12680 SmallVector<unsigned, 16> ListsPerDecl; 12681 ListsPerDecl.reserve(UniqueDecls); 12682 for (unsigned i = 0; i < UniqueDecls; ++i) 12683 ListsPerDecl.push_back(Record.readInt()); 12684 C->setDeclNumLists(ListsPerDecl); 12685 12686 SmallVector<unsigned, 32> ListSizes; 12687 ListSizes.reserve(TotalLists); 12688 for (unsigned i = 0; i < TotalLists; ++i) 12689 ListSizes.push_back(Record.readInt()); 12690 C->setComponentListSizes(ListSizes); 12691 12692 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12693 Components.reserve(TotalComponents); 12694 for (unsigned i = 0; i < TotalComponents; ++i) { 12695 Expr *AssociatedExprPr = Record.readSubExpr(); 12696 bool IsNonContiguous = Record.readBool(); 12697 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12698 Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous); 12699 } 12700 C->setComponents(Components, ListSizes); 12701 } 12702 12703 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) { 12704 C->setLParenLoc(Record.readSourceLocation()); 12705 auto NumVars = C->varlist_size(); 12706 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12707 auto TotalLists = C->getTotalComponentListNum(); 12708 auto TotalComponents = C->getTotalComponentsNum(); 12709 12710 SmallVector<Expr *, 16> Vars; 12711 Vars.reserve(NumVars); 12712 for (unsigned i = 0; i != NumVars; ++i) 12713 Vars.push_back(Record.readSubExpr()); 12714 C->setVarRefs(Vars); 12715 Vars.clear(); 12716 for (unsigned i = 0; i != NumVars; ++i) 12717 Vars.push_back(Record.readSubExpr()); 12718 C->setPrivateCopies(Vars); 12719 Vars.clear(); 12720 for (unsigned i = 0; i != NumVars; ++i) 12721 Vars.push_back(Record.readSubExpr()); 12722 C->setInits(Vars); 12723 12724 SmallVector<ValueDecl *, 16> Decls; 12725 Decls.reserve(UniqueDecls); 12726 for (unsigned i = 0; i < UniqueDecls; ++i) 12727 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12728 C->setUniqueDecls(Decls); 12729 12730 SmallVector<unsigned, 16> ListsPerDecl; 12731 ListsPerDecl.reserve(UniqueDecls); 12732 for (unsigned i = 0; i < UniqueDecls; ++i) 12733 ListsPerDecl.push_back(Record.readInt()); 12734 C->setDeclNumLists(ListsPerDecl); 12735 12736 SmallVector<unsigned, 32> ListSizes; 12737 ListSizes.reserve(TotalLists); 12738 for (unsigned i = 0; i < TotalLists; ++i) 12739 ListSizes.push_back(Record.readInt()); 12740 C->setComponentListSizes(ListSizes); 12741 12742 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12743 Components.reserve(TotalComponents); 12744 for (unsigned i = 0; i < TotalComponents; ++i) { 12745 auto *AssociatedExprPr = Record.readSubExpr(); 12746 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12747 Components.emplace_back(AssociatedExprPr, AssociatedDecl, 12748 /*IsNonContiguous=*/false); 12749 } 12750 C->setComponents(Components, ListSizes); 12751 } 12752 12753 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) { 12754 C->setLParenLoc(Record.readSourceLocation()); 12755 auto NumVars = C->varlist_size(); 12756 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12757 auto TotalLists = C->getTotalComponentListNum(); 12758 auto TotalComponents = C->getTotalComponentsNum(); 12759 12760 SmallVector<Expr *, 16> Vars; 12761 Vars.reserve(NumVars); 12762 for (unsigned i = 0; i != NumVars; ++i) 12763 Vars.push_back(Record.readSubExpr()); 12764 C->setVarRefs(Vars); 12765 12766 SmallVector<ValueDecl *, 16> Decls; 12767 Decls.reserve(UniqueDecls); 12768 for (unsigned i = 0; i < UniqueDecls; ++i) 12769 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12770 C->setUniqueDecls(Decls); 12771 12772 SmallVector<unsigned, 16> ListsPerDecl; 12773 ListsPerDecl.reserve(UniqueDecls); 12774 for (unsigned i = 0; i < UniqueDecls; ++i) 12775 ListsPerDecl.push_back(Record.readInt()); 12776 C->setDeclNumLists(ListsPerDecl); 12777 12778 SmallVector<unsigned, 32> ListSizes; 12779 ListSizes.reserve(TotalLists); 12780 for (unsigned i = 0; i < TotalLists; ++i) 12781 ListSizes.push_back(Record.readInt()); 12782 C->setComponentListSizes(ListSizes); 12783 12784 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12785 Components.reserve(TotalComponents); 12786 for (unsigned i = 0; i < TotalComponents; ++i) { 12787 Expr *AssociatedExpr = Record.readSubExpr(); 12788 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12789 Components.emplace_back(AssociatedExpr, AssociatedDecl, 12790 /*IsNonContiguous*/ false); 12791 } 12792 C->setComponents(Components, ListSizes); 12793 } 12794 12795 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) { 12796 C->setLParenLoc(Record.readSourceLocation()); 12797 auto NumVars = C->varlist_size(); 12798 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12799 auto TotalLists = C->getTotalComponentListNum(); 12800 auto TotalComponents = C->getTotalComponentsNum(); 12801 12802 SmallVector<Expr *, 16> Vars; 12803 Vars.reserve(NumVars); 12804 for (unsigned i = 0; i != NumVars; ++i) 12805 Vars.push_back(Record.readSubExpr()); 12806 C->setVarRefs(Vars); 12807 Vars.clear(); 12808 12809 SmallVector<ValueDecl *, 16> Decls; 12810 Decls.reserve(UniqueDecls); 12811 for (unsigned i = 0; i < UniqueDecls; ++i) 12812 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12813 C->setUniqueDecls(Decls); 12814 12815 SmallVector<unsigned, 16> ListsPerDecl; 12816 ListsPerDecl.reserve(UniqueDecls); 12817 for (unsigned i = 0; i < UniqueDecls; ++i) 12818 ListsPerDecl.push_back(Record.readInt()); 12819 C->setDeclNumLists(ListsPerDecl); 12820 12821 SmallVector<unsigned, 32> ListSizes; 12822 ListSizes.reserve(TotalLists); 12823 for (unsigned i = 0; i < TotalLists; ++i) 12824 ListSizes.push_back(Record.readInt()); 12825 C->setComponentListSizes(ListSizes); 12826 12827 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12828 Components.reserve(TotalComponents); 12829 for (unsigned i = 0; i < TotalComponents; ++i) { 12830 Expr *AssociatedExpr = Record.readSubExpr(); 12831 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12832 Components.emplace_back(AssociatedExpr, AssociatedDecl, 12833 /*IsNonContiguous=*/false); 12834 } 12835 C->setComponents(Components, ListSizes); 12836 } 12837 12838 void OMPClauseReader::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *C) { 12839 C->setLParenLoc(Record.readSourceLocation()); 12840 auto NumVars = C->varlist_size(); 12841 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12842 auto TotalLists = C->getTotalComponentListNum(); 12843 auto TotalComponents = C->getTotalComponentsNum(); 12844 12845 SmallVector<Expr *, 16> Vars; 12846 Vars.reserve(NumVars); 12847 for (unsigned I = 0; I != NumVars; ++I) 12848 Vars.push_back(Record.readSubExpr()); 12849 C->setVarRefs(Vars); 12850 Vars.clear(); 12851 12852 SmallVector<ValueDecl *, 16> Decls; 12853 Decls.reserve(UniqueDecls); 12854 for (unsigned I = 0; I < UniqueDecls; ++I) 12855 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12856 C->setUniqueDecls(Decls); 12857 12858 SmallVector<unsigned, 16> ListsPerDecl; 12859 ListsPerDecl.reserve(UniqueDecls); 12860 for (unsigned I = 0; I < UniqueDecls; ++I) 12861 ListsPerDecl.push_back(Record.readInt()); 12862 C->setDeclNumLists(ListsPerDecl); 12863 12864 SmallVector<unsigned, 32> ListSizes; 12865 ListSizes.reserve(TotalLists); 12866 for (unsigned i = 0; i < TotalLists; ++i) 12867 ListSizes.push_back(Record.readInt()); 12868 C->setComponentListSizes(ListSizes); 12869 12870 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12871 Components.reserve(TotalComponents); 12872 for (unsigned I = 0; I < TotalComponents; ++I) { 12873 Expr *AssociatedExpr = Record.readSubExpr(); 12874 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12875 Components.emplace_back(AssociatedExpr, AssociatedDecl, 12876 /*IsNonContiguous=*/false); 12877 } 12878 C->setComponents(Components, ListSizes); 12879 } 12880 12881 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) { 12882 C->setLParenLoc(Record.readSourceLocation()); 12883 unsigned NumVars = C->varlist_size(); 12884 SmallVector<Expr *, 16> Vars; 12885 Vars.reserve(NumVars); 12886 for (unsigned i = 0; i != NumVars; ++i) 12887 Vars.push_back(Record.readSubExpr()); 12888 C->setVarRefs(Vars); 12889 Vars.clear(); 12890 Vars.reserve(NumVars); 12891 for (unsigned i = 0; i != NumVars; ++i) 12892 Vars.push_back(Record.readSubExpr()); 12893 C->setPrivateRefs(Vars); 12894 } 12895 12896 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) { 12897 C->setLParenLoc(Record.readSourceLocation()); 12898 unsigned NumVars = C->varlist_size(); 12899 SmallVector<Expr *, 16> Vars; 12900 Vars.reserve(NumVars); 12901 for (unsigned i = 0; i != NumVars; ++i) 12902 Vars.push_back(Record.readSubExpr()); 12903 C->setVarRefs(Vars); 12904 } 12905 12906 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) { 12907 C->setLParenLoc(Record.readSourceLocation()); 12908 unsigned NumVars = C->varlist_size(); 12909 SmallVector<Expr *, 16> Vars; 12910 Vars.reserve(NumVars); 12911 for (unsigned i = 0; i != NumVars; ++i) 12912 Vars.push_back(Record.readSubExpr()); 12913 C->setVarRefs(Vars); 12914 } 12915 12916 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) { 12917 C->setLParenLoc(Record.readSourceLocation()); 12918 unsigned NumOfAllocators = C->getNumberOfAllocators(); 12919 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data; 12920 Data.reserve(NumOfAllocators); 12921 for (unsigned I = 0; I != NumOfAllocators; ++I) { 12922 OMPUsesAllocatorsClause::Data &D = Data.emplace_back(); 12923 D.Allocator = Record.readSubExpr(); 12924 D.AllocatorTraits = Record.readSubExpr(); 12925 D.LParenLoc = Record.readSourceLocation(); 12926 D.RParenLoc = Record.readSourceLocation(); 12927 } 12928 C->setAllocatorsData(Data); 12929 } 12930 12931 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) { 12932 C->setLParenLoc(Record.readSourceLocation()); 12933 C->setModifier(Record.readSubExpr()); 12934 C->setColonLoc(Record.readSourceLocation()); 12935 unsigned NumOfLocators = C->varlist_size(); 12936 SmallVector<Expr *, 4> Locators; 12937 Locators.reserve(NumOfLocators); 12938 for (unsigned I = 0; I != NumOfLocators; ++I) 12939 Locators.push_back(Record.readSubExpr()); 12940 C->setVarRefs(Locators); 12941 } 12942 12943 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) { 12944 C->setKind(Record.readEnum<OpenMPOrderClauseKind>()); 12945 C->setLParenLoc(Record.readSourceLocation()); 12946 C->setKindKwLoc(Record.readSourceLocation()); 12947 } 12948 12949 void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) { 12950 VisitOMPClauseWithPreInit(C); 12951 C->setThreadID(Record.readSubExpr()); 12952 C->setLParenLoc(Record.readSourceLocation()); 12953 } 12954 12955 void OMPClauseReader::VisitOMPBindClause(OMPBindClause *C) { 12956 C->setBindKind(Record.readEnum<OpenMPBindClauseKind>()); 12957 C->setLParenLoc(Record.readSourceLocation()); 12958 C->setBindKindLoc(Record.readSourceLocation()); 12959 } 12960 12961 void OMPClauseReader::VisitOMPAlignClause(OMPAlignClause *C) { 12962 C->setAlignment(Record.readExpr()); 12963 C->setLParenLoc(Record.readSourceLocation()); 12964 } 12965 12966 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() { 12967 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo(); 12968 TI.Sets.resize(readUInt32()); 12969 for (auto &Set : TI.Sets) { 12970 Set.Kind = readEnum<llvm::omp::TraitSet>(); 12971 Set.Selectors.resize(readUInt32()); 12972 for (auto &Selector : Set.Selectors) { 12973 Selector.Kind = readEnum<llvm::omp::TraitSelector>(); 12974 Selector.ScoreOrCondition = nullptr; 12975 if (readBool()) 12976 Selector.ScoreOrCondition = readExprRef(); 12977 Selector.Properties.resize(readUInt32()); 12978 for (auto &Property : Selector.Properties) 12979 Property.Kind = readEnum<llvm::omp::TraitProperty>(); 12980 } 12981 } 12982 return &TI; 12983 } 12984 12985 void ASTRecordReader::readOMPChildren(OMPChildren *Data) { 12986 if (!Data) 12987 return; 12988 if (Reader->ReadingKind == ASTReader::Read_Stmt) { 12989 // Skip NumClauses, NumChildren and HasAssociatedStmt fields. 12990 skipInts(3); 12991 } 12992 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses()); 12993 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I) 12994 Clauses[I] = readOMPClause(); 12995 Data->setClauses(Clauses); 12996 if (Data->hasAssociatedStmt()) 12997 Data->setAssociatedStmt(readStmt()); 12998 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I) 12999 Data->getChildren()[I] = readStmt(); 13000 } 13001