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