1 //===- ASTReader.cpp - AST File Reader ------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the ASTReader class, which reads AST files. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "clang/Basic/OpenMPKinds.h" 14 #include "clang/Serialization/ASTRecordReader.h" 15 #include "ASTCommon.h" 16 #include "ASTReaderInternals.h" 17 #include "clang/AST/AbstractTypeReader.h" 18 #include "clang/AST/ASTConsumer.h" 19 #include "clang/AST/ASTContext.h" 20 #include "clang/AST/ASTMutationListener.h" 21 #include "clang/AST/ASTUnresolvedSet.h" 22 #include "clang/AST/Decl.h" 23 #include "clang/AST/DeclBase.h" 24 #include "clang/AST/DeclCXX.h" 25 #include "clang/AST/DeclFriend.h" 26 #include "clang/AST/DeclGroup.h" 27 #include "clang/AST/DeclObjC.h" 28 #include "clang/AST/DeclTemplate.h" 29 #include "clang/AST/DeclarationName.h" 30 #include "clang/AST/Expr.h" 31 #include "clang/AST/ExprCXX.h" 32 #include "clang/AST/ExternalASTSource.h" 33 #include "clang/AST/NestedNameSpecifier.h" 34 #include "clang/AST/OpenMPClause.h" 35 #include "clang/AST/ODRHash.h" 36 #include "clang/AST/RawCommentList.h" 37 #include "clang/AST/TemplateBase.h" 38 #include "clang/AST/TemplateName.h" 39 #include "clang/AST/Type.h" 40 #include "clang/AST/TypeLoc.h" 41 #include "clang/AST/TypeLocVisitor.h" 42 #include "clang/AST/UnresolvedSet.h" 43 #include "clang/Basic/CommentOptions.h" 44 #include "clang/Basic/Diagnostic.h" 45 #include "clang/Basic/DiagnosticOptions.h" 46 #include "clang/Basic/ExceptionSpecificationType.h" 47 #include "clang/Basic/FileManager.h" 48 #include "clang/Basic/FileSystemOptions.h" 49 #include "clang/Basic/IdentifierTable.h" 50 #include "clang/Basic/LLVM.h" 51 #include "clang/Basic/LangOptions.h" 52 #include "clang/Basic/Module.h" 53 #include "clang/Basic/ObjCRuntime.h" 54 #include "clang/Basic/OperatorKinds.h" 55 #include "clang/Basic/PragmaKinds.h" 56 #include "clang/Basic/Sanitizers.h" 57 #include "clang/Basic/SourceLocation.h" 58 #include "clang/Basic/SourceManager.h" 59 #include "clang/Basic/SourceManagerInternals.h" 60 #include "clang/Basic/Specifiers.h" 61 #include "clang/Basic/TargetInfo.h" 62 #include "clang/Basic/TargetOptions.h" 63 #include "clang/Basic/TokenKinds.h" 64 #include "clang/Basic/Version.h" 65 #include "clang/Lex/HeaderSearch.h" 66 #include "clang/Lex/HeaderSearchOptions.h" 67 #include "clang/Lex/MacroInfo.h" 68 #include "clang/Lex/ModuleMap.h" 69 #include "clang/Lex/PreprocessingRecord.h" 70 #include "clang/Lex/Preprocessor.h" 71 #include "clang/Lex/PreprocessorOptions.h" 72 #include "clang/Lex/Token.h" 73 #include "clang/Sema/ObjCMethodList.h" 74 #include "clang/Sema/Scope.h" 75 #include "clang/Sema/Sema.h" 76 #include "clang/Sema/Weak.h" 77 #include "clang/Serialization/ASTBitCodes.h" 78 #include "clang/Serialization/ASTDeserializationListener.h" 79 #include "clang/Serialization/ContinuousRangeMap.h" 80 #include "clang/Serialization/GlobalModuleIndex.h" 81 #include "clang/Serialization/InMemoryModuleCache.h" 82 #include "clang/Serialization/ModuleFile.h" 83 #include "clang/Serialization/ModuleFileExtension.h" 84 #include "clang/Serialization/ModuleManager.h" 85 #include "clang/Serialization/PCHContainerOperations.h" 86 #include "clang/Serialization/SerializationDiagnostic.h" 87 #include "llvm/ADT/APFloat.h" 88 #include "llvm/ADT/APInt.h" 89 #include "llvm/ADT/APSInt.h" 90 #include "llvm/ADT/ArrayRef.h" 91 #include "llvm/ADT/DenseMap.h" 92 #include "llvm/ADT/FloatingPointMode.h" 93 #include "llvm/ADT/FoldingSet.h" 94 #include "llvm/ADT/Hashing.h" 95 #include "llvm/ADT/IntrusiveRefCntPtr.h" 96 #include "llvm/ADT/None.h" 97 #include "llvm/ADT/Optional.h" 98 #include "llvm/ADT/STLExtras.h" 99 #include "llvm/ADT/ScopeExit.h" 100 #include "llvm/ADT/SmallPtrSet.h" 101 #include "llvm/ADT/SmallString.h" 102 #include "llvm/ADT/SmallVector.h" 103 #include "llvm/ADT/StringExtras.h" 104 #include "llvm/ADT/StringMap.h" 105 #include "llvm/ADT/StringRef.h" 106 #include "llvm/ADT/Triple.h" 107 #include "llvm/ADT/iterator_range.h" 108 #include "llvm/Bitstream/BitstreamReader.h" 109 #include "llvm/Support/Casting.h" 110 #include "llvm/Support/Compiler.h" 111 #include "llvm/Support/Compression.h" 112 #include "llvm/Support/DJB.h" 113 #include "llvm/Support/Endian.h" 114 #include "llvm/Support/Error.h" 115 #include "llvm/Support/ErrorHandling.h" 116 #include "llvm/Support/FileSystem.h" 117 #include "llvm/Support/MemoryBuffer.h" 118 #include "llvm/Support/Path.h" 119 #include "llvm/Support/SaveAndRestore.h" 120 #include "llvm/Support/Timer.h" 121 #include "llvm/Support/VersionTuple.h" 122 #include "llvm/Support/raw_ostream.h" 123 #include <algorithm> 124 #include <cassert> 125 #include <cstddef> 126 #include <cstdint> 127 #include <cstdio> 128 #include <ctime> 129 #include <iterator> 130 #include <limits> 131 #include <map> 132 #include <memory> 133 #include <string> 134 #include <system_error> 135 #include <tuple> 136 #include <utility> 137 #include <vector> 138 139 using namespace clang; 140 using namespace clang::serialization; 141 using namespace clang::serialization::reader; 142 using llvm::BitstreamCursor; 143 using llvm::RoundingMode; 144 145 //===----------------------------------------------------------------------===// 146 // ChainedASTReaderListener implementation 147 //===----------------------------------------------------------------------===// 148 149 bool 150 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) { 151 return First->ReadFullVersionInformation(FullVersion) || 152 Second->ReadFullVersionInformation(FullVersion); 153 } 154 155 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) { 156 First->ReadModuleName(ModuleName); 157 Second->ReadModuleName(ModuleName); 158 } 159 160 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) { 161 First->ReadModuleMapFile(ModuleMapPath); 162 Second->ReadModuleMapFile(ModuleMapPath); 163 } 164 165 bool 166 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts, 167 bool Complain, 168 bool AllowCompatibleDifferences) { 169 return First->ReadLanguageOptions(LangOpts, Complain, 170 AllowCompatibleDifferences) || 171 Second->ReadLanguageOptions(LangOpts, Complain, 172 AllowCompatibleDifferences); 173 } 174 175 bool ChainedASTReaderListener::ReadTargetOptions( 176 const TargetOptions &TargetOpts, bool Complain, 177 bool AllowCompatibleDifferences) { 178 return First->ReadTargetOptions(TargetOpts, Complain, 179 AllowCompatibleDifferences) || 180 Second->ReadTargetOptions(TargetOpts, Complain, 181 AllowCompatibleDifferences); 182 } 183 184 bool ChainedASTReaderListener::ReadDiagnosticOptions( 185 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 186 return First->ReadDiagnosticOptions(DiagOpts, Complain) || 187 Second->ReadDiagnosticOptions(DiagOpts, Complain); 188 } 189 190 bool 191 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts, 192 bool Complain) { 193 return First->ReadFileSystemOptions(FSOpts, Complain) || 194 Second->ReadFileSystemOptions(FSOpts, Complain); 195 } 196 197 bool ChainedASTReaderListener::ReadHeaderSearchOptions( 198 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath, 199 bool Complain) { 200 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 201 Complain) || 202 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 203 Complain); 204 } 205 206 bool ChainedASTReaderListener::ReadPreprocessorOptions( 207 const PreprocessorOptions &PPOpts, bool Complain, 208 std::string &SuggestedPredefines) { 209 return First->ReadPreprocessorOptions(PPOpts, Complain, 210 SuggestedPredefines) || 211 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines); 212 } 213 214 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M, 215 unsigned Value) { 216 First->ReadCounter(M, Value); 217 Second->ReadCounter(M, Value); 218 } 219 220 bool ChainedASTReaderListener::needsInputFileVisitation() { 221 return First->needsInputFileVisitation() || 222 Second->needsInputFileVisitation(); 223 } 224 225 bool ChainedASTReaderListener::needsSystemInputFileVisitation() { 226 return First->needsSystemInputFileVisitation() || 227 Second->needsSystemInputFileVisitation(); 228 } 229 230 void ChainedASTReaderListener::visitModuleFile(StringRef Filename, 231 ModuleKind Kind) { 232 First->visitModuleFile(Filename, Kind); 233 Second->visitModuleFile(Filename, Kind); 234 } 235 236 bool ChainedASTReaderListener::visitInputFile(StringRef Filename, 237 bool isSystem, 238 bool isOverridden, 239 bool isExplicitModule) { 240 bool Continue = false; 241 if (First->needsInputFileVisitation() && 242 (!isSystem || First->needsSystemInputFileVisitation())) 243 Continue |= First->visitInputFile(Filename, isSystem, isOverridden, 244 isExplicitModule); 245 if (Second->needsInputFileVisitation() && 246 (!isSystem || Second->needsSystemInputFileVisitation())) 247 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden, 248 isExplicitModule); 249 return Continue; 250 } 251 252 void ChainedASTReaderListener::readModuleFileExtension( 253 const ModuleFileExtensionMetadata &Metadata) { 254 First->readModuleFileExtension(Metadata); 255 Second->readModuleFileExtension(Metadata); 256 } 257 258 //===----------------------------------------------------------------------===// 259 // PCH validator implementation 260 //===----------------------------------------------------------------------===// 261 262 ASTReaderListener::~ASTReaderListener() = default; 263 264 /// Compare the given set of language options against an existing set of 265 /// language options. 266 /// 267 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 268 /// \param AllowCompatibleDifferences If true, differences between compatible 269 /// language options will be permitted. 270 /// 271 /// \returns true if the languagae options mis-match, false otherwise. 272 static bool checkLanguageOptions(const LangOptions &LangOpts, 273 const LangOptions &ExistingLangOpts, 274 DiagnosticsEngine *Diags, 275 bool AllowCompatibleDifferences = true) { 276 #define LANGOPT(Name, Bits, Default, Description) \ 277 if (ExistingLangOpts.Name != LangOpts.Name) { \ 278 if (Diags) \ 279 Diags->Report(diag::err_pch_langopt_mismatch) \ 280 << Description << LangOpts.Name << ExistingLangOpts.Name; \ 281 return true; \ 282 } 283 284 #define VALUE_LANGOPT(Name, Bits, Default, Description) \ 285 if (ExistingLangOpts.Name != LangOpts.Name) { \ 286 if (Diags) \ 287 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 288 << Description; \ 289 return true; \ 290 } 291 292 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 293 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \ 294 if (Diags) \ 295 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 296 << Description; \ 297 return true; \ 298 } 299 300 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \ 301 if (!AllowCompatibleDifferences) \ 302 LANGOPT(Name, Bits, Default, Description) 303 304 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \ 305 if (!AllowCompatibleDifferences) \ 306 ENUM_LANGOPT(Name, Bits, Default, Description) 307 308 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \ 309 if (!AllowCompatibleDifferences) \ 310 VALUE_LANGOPT(Name, Bits, Default, Description) 311 312 #define BENIGN_LANGOPT(Name, Bits, Default, Description) 313 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) 314 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description) 315 #include "clang/Basic/LangOptions.def" 316 317 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) { 318 if (Diags) 319 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features"; 320 return true; 321 } 322 323 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) { 324 if (Diags) 325 Diags->Report(diag::err_pch_langopt_value_mismatch) 326 << "target Objective-C runtime"; 327 return true; 328 } 329 330 if (ExistingLangOpts.CommentOpts.BlockCommandNames != 331 LangOpts.CommentOpts.BlockCommandNames) { 332 if (Diags) 333 Diags->Report(diag::err_pch_langopt_value_mismatch) 334 << "block command names"; 335 return true; 336 } 337 338 // Sanitizer feature mismatches are treated as compatible differences. If 339 // compatible differences aren't allowed, we still only want to check for 340 // mismatches of non-modular sanitizers (the only ones which can affect AST 341 // generation). 342 if (!AllowCompatibleDifferences) { 343 SanitizerMask ModularSanitizers = getPPTransparentSanitizers(); 344 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize; 345 SanitizerSet ImportedSanitizers = LangOpts.Sanitize; 346 ExistingSanitizers.clear(ModularSanitizers); 347 ImportedSanitizers.clear(ModularSanitizers); 348 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) { 349 const std::string Flag = "-fsanitize="; 350 if (Diags) { 351 #define SANITIZER(NAME, ID) \ 352 { \ 353 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \ 354 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \ 355 if (InExistingModule != InImportedModule) \ 356 Diags->Report(diag::err_pch_targetopt_feature_mismatch) \ 357 << InExistingModule << (Flag + NAME); \ 358 } 359 #include "clang/Basic/Sanitizers.def" 360 } 361 return true; 362 } 363 } 364 365 return false; 366 } 367 368 /// Compare the given set of target options against an existing set of 369 /// target options. 370 /// 371 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 372 /// 373 /// \returns true if the target options mis-match, false otherwise. 374 static bool checkTargetOptions(const TargetOptions &TargetOpts, 375 const TargetOptions &ExistingTargetOpts, 376 DiagnosticsEngine *Diags, 377 bool AllowCompatibleDifferences = true) { 378 #define CHECK_TARGET_OPT(Field, Name) \ 379 if (TargetOpts.Field != ExistingTargetOpts.Field) { \ 380 if (Diags) \ 381 Diags->Report(diag::err_pch_targetopt_mismatch) \ 382 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \ 383 return true; \ 384 } 385 386 // The triple and ABI must match exactly. 387 CHECK_TARGET_OPT(Triple, "target"); 388 CHECK_TARGET_OPT(ABI, "target ABI"); 389 390 // We can tolerate different CPUs in many cases, notably when one CPU 391 // supports a strict superset of another. When allowing compatible 392 // differences skip this check. 393 if (!AllowCompatibleDifferences) 394 CHECK_TARGET_OPT(CPU, "target CPU"); 395 396 #undef CHECK_TARGET_OPT 397 398 // Compare feature sets. 399 SmallVector<StringRef, 4> ExistingFeatures( 400 ExistingTargetOpts.FeaturesAsWritten.begin(), 401 ExistingTargetOpts.FeaturesAsWritten.end()); 402 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(), 403 TargetOpts.FeaturesAsWritten.end()); 404 llvm::sort(ExistingFeatures); 405 llvm::sort(ReadFeatures); 406 407 // We compute the set difference in both directions explicitly so that we can 408 // diagnose the differences differently. 409 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures; 410 std::set_difference( 411 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(), 412 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures)); 413 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(), 414 ExistingFeatures.begin(), ExistingFeatures.end(), 415 std::back_inserter(UnmatchedReadFeatures)); 416 417 // If we are allowing compatible differences and the read feature set is 418 // a strict subset of the existing feature set, there is nothing to diagnose. 419 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty()) 420 return false; 421 422 if (Diags) { 423 for (StringRef Feature : UnmatchedReadFeatures) 424 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 425 << /* is-existing-feature */ false << Feature; 426 for (StringRef Feature : UnmatchedExistingFeatures) 427 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 428 << /* is-existing-feature */ true << Feature; 429 } 430 431 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty(); 432 } 433 434 bool 435 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts, 436 bool Complain, 437 bool AllowCompatibleDifferences) { 438 const LangOptions &ExistingLangOpts = PP.getLangOpts(); 439 return checkLanguageOptions(LangOpts, ExistingLangOpts, 440 Complain ? &Reader.Diags : nullptr, 441 AllowCompatibleDifferences); 442 } 443 444 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts, 445 bool Complain, 446 bool AllowCompatibleDifferences) { 447 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts(); 448 return checkTargetOptions(TargetOpts, ExistingTargetOpts, 449 Complain ? &Reader.Diags : nullptr, 450 AllowCompatibleDifferences); 451 } 452 453 namespace { 454 455 using MacroDefinitionsMap = 456 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>; 457 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>; 458 459 } // namespace 460 461 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags, 462 DiagnosticsEngine &Diags, 463 bool Complain) { 464 using Level = DiagnosticsEngine::Level; 465 466 // Check current mappings for new -Werror mappings, and the stored mappings 467 // for cases that were explicitly mapped to *not* be errors that are now 468 // errors because of options like -Werror. 469 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags }; 470 471 for (DiagnosticsEngine *MappingSource : MappingSources) { 472 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) { 473 diag::kind DiagID = DiagIDMappingPair.first; 474 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation()); 475 if (CurLevel < DiagnosticsEngine::Error) 476 continue; // not significant 477 Level StoredLevel = 478 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation()); 479 if (StoredLevel < DiagnosticsEngine::Error) { 480 if (Complain) 481 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" + 482 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str(); 483 return true; 484 } 485 } 486 } 487 488 return false; 489 } 490 491 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) { 492 diag::Severity Ext = Diags.getExtensionHandlingBehavior(); 493 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors()) 494 return true; 495 return Ext >= diag::Severity::Error; 496 } 497 498 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags, 499 DiagnosticsEngine &Diags, 500 bool IsSystem, bool Complain) { 501 // Top-level options 502 if (IsSystem) { 503 if (Diags.getSuppressSystemWarnings()) 504 return false; 505 // If -Wsystem-headers was not enabled before, be conservative 506 if (StoredDiags.getSuppressSystemWarnings()) { 507 if (Complain) 508 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers"; 509 return true; 510 } 511 } 512 513 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) { 514 if (Complain) 515 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror"; 516 return true; 517 } 518 519 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() && 520 !StoredDiags.getEnableAllWarnings()) { 521 if (Complain) 522 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror"; 523 return true; 524 } 525 526 if (isExtHandlingFromDiagsError(Diags) && 527 !isExtHandlingFromDiagsError(StoredDiags)) { 528 if (Complain) 529 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors"; 530 return true; 531 } 532 533 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain); 534 } 535 536 /// Return the top import module if it is implicit, nullptr otherwise. 537 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr, 538 Preprocessor &PP) { 539 // If the original import came from a file explicitly generated by the user, 540 // don't check the diagnostic mappings. 541 // FIXME: currently this is approximated by checking whether this is not a 542 // module import of an implicitly-loaded module file. 543 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in 544 // the transitive closure of its imports, since unrelated modules cannot be 545 // imported until after this module finishes validation. 546 ModuleFile *TopImport = &*ModuleMgr.rbegin(); 547 while (!TopImport->ImportedBy.empty()) 548 TopImport = TopImport->ImportedBy[0]; 549 if (TopImport->Kind != MK_ImplicitModule) 550 return nullptr; 551 552 StringRef ModuleName = TopImport->ModuleName; 553 assert(!ModuleName.empty() && "diagnostic options read before module name"); 554 555 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName); 556 assert(M && "missing module"); 557 return M; 558 } 559 560 bool PCHValidator::ReadDiagnosticOptions( 561 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 562 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics(); 563 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs()); 564 IntrusiveRefCntPtr<DiagnosticsEngine> Diags( 565 new DiagnosticsEngine(DiagIDs, DiagOpts.get())); 566 // This should never fail, because we would have processed these options 567 // before writing them to an ASTFile. 568 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false); 569 570 ModuleManager &ModuleMgr = Reader.getModuleManager(); 571 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then"); 572 573 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP); 574 if (!TopM) 575 return false; 576 577 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that 578 // contains the union of their flags. 579 return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem, 580 Complain); 581 } 582 583 /// Collect the macro definitions provided by the given preprocessor 584 /// options. 585 static void 586 collectMacroDefinitions(const PreprocessorOptions &PPOpts, 587 MacroDefinitionsMap &Macros, 588 SmallVectorImpl<StringRef> *MacroNames = nullptr) { 589 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) { 590 StringRef Macro = PPOpts.Macros[I].first; 591 bool IsUndef = PPOpts.Macros[I].second; 592 593 std::pair<StringRef, StringRef> MacroPair = Macro.split('='); 594 StringRef MacroName = MacroPair.first; 595 StringRef MacroBody = MacroPair.second; 596 597 // For an #undef'd macro, we only care about the name. 598 if (IsUndef) { 599 if (MacroNames && !Macros.count(MacroName)) 600 MacroNames->push_back(MacroName); 601 602 Macros[MacroName] = std::make_pair("", true); 603 continue; 604 } 605 606 // For a #define'd macro, figure out the actual definition. 607 if (MacroName.size() == Macro.size()) 608 MacroBody = "1"; 609 else { 610 // Note: GCC drops anything following an end-of-line character. 611 StringRef::size_type End = MacroBody.find_first_of("\n\r"); 612 MacroBody = MacroBody.substr(0, End); 613 } 614 615 if (MacroNames && !Macros.count(MacroName)) 616 MacroNames->push_back(MacroName); 617 Macros[MacroName] = std::make_pair(MacroBody, false); 618 } 619 } 620 621 /// Check the preprocessor options deserialized from the control block 622 /// against the preprocessor options in an existing preprocessor. 623 /// 624 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 625 /// \param Validate If true, validate preprocessor options. If false, allow 626 /// macros defined by \p ExistingPPOpts to override those defined by 627 /// \p PPOpts in SuggestedPredefines. 628 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts, 629 const PreprocessorOptions &ExistingPPOpts, 630 DiagnosticsEngine *Diags, 631 FileManager &FileMgr, 632 std::string &SuggestedPredefines, 633 const LangOptions &LangOpts, 634 bool Validate = true) { 635 // Check macro definitions. 636 MacroDefinitionsMap ASTFileMacros; 637 collectMacroDefinitions(PPOpts, ASTFileMacros); 638 MacroDefinitionsMap ExistingMacros; 639 SmallVector<StringRef, 4> ExistingMacroNames; 640 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames); 641 642 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) { 643 // Dig out the macro definition in the existing preprocessor options. 644 StringRef MacroName = ExistingMacroNames[I]; 645 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName]; 646 647 // Check whether we know anything about this macro name or not. 648 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known = 649 ASTFileMacros.find(MacroName); 650 if (!Validate || Known == ASTFileMacros.end()) { 651 // FIXME: Check whether this identifier was referenced anywhere in the 652 // AST file. If so, we should reject the AST file. Unfortunately, this 653 // information isn't in the control block. What shall we do about it? 654 655 if (Existing.second) { 656 SuggestedPredefines += "#undef "; 657 SuggestedPredefines += MacroName.str(); 658 SuggestedPredefines += '\n'; 659 } else { 660 SuggestedPredefines += "#define "; 661 SuggestedPredefines += MacroName.str(); 662 SuggestedPredefines += ' '; 663 SuggestedPredefines += Existing.first.str(); 664 SuggestedPredefines += '\n'; 665 } 666 continue; 667 } 668 669 // If the macro was defined in one but undef'd in the other, we have a 670 // conflict. 671 if (Existing.second != Known->second.second) { 672 if (Diags) { 673 Diags->Report(diag::err_pch_macro_def_undef) 674 << MacroName << Known->second.second; 675 } 676 return true; 677 } 678 679 // If the macro was #undef'd in both, or if the macro bodies are identical, 680 // it's fine. 681 if (Existing.second || Existing.first == Known->second.first) 682 continue; 683 684 // The macro bodies differ; complain. 685 if (Diags) { 686 Diags->Report(diag::err_pch_macro_def_conflict) 687 << MacroName << Known->second.first << Existing.first; 688 } 689 return true; 690 } 691 692 // Check whether we're using predefines. 693 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) { 694 if (Diags) { 695 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines; 696 } 697 return true; 698 } 699 700 // Detailed record is important since it is used for the module cache hash. 701 if (LangOpts.Modules && 702 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) { 703 if (Diags) { 704 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord; 705 } 706 return true; 707 } 708 709 // Compute the #include and #include_macros lines we need. 710 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) { 711 StringRef File = ExistingPPOpts.Includes[I]; 712 713 if (!ExistingPPOpts.ImplicitPCHInclude.empty() && 714 !ExistingPPOpts.PCHThroughHeader.empty()) { 715 // In case the through header is an include, we must add all the includes 716 // to the predefines so the start point can be determined. 717 SuggestedPredefines += "#include \""; 718 SuggestedPredefines += File; 719 SuggestedPredefines += "\"\n"; 720 continue; 721 } 722 723 if (File == ExistingPPOpts.ImplicitPCHInclude) 724 continue; 725 726 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File) 727 != PPOpts.Includes.end()) 728 continue; 729 730 SuggestedPredefines += "#include \""; 731 SuggestedPredefines += File; 732 SuggestedPredefines += "\"\n"; 733 } 734 735 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) { 736 StringRef File = ExistingPPOpts.MacroIncludes[I]; 737 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(), 738 File) 739 != PPOpts.MacroIncludes.end()) 740 continue; 741 742 SuggestedPredefines += "#__include_macros \""; 743 SuggestedPredefines += File; 744 SuggestedPredefines += "\"\n##\n"; 745 } 746 747 return false; 748 } 749 750 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 751 bool Complain, 752 std::string &SuggestedPredefines) { 753 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts(); 754 755 return checkPreprocessorOptions(PPOpts, ExistingPPOpts, 756 Complain? &Reader.Diags : nullptr, 757 PP.getFileManager(), 758 SuggestedPredefines, 759 PP.getLangOpts()); 760 } 761 762 bool SimpleASTReaderListener::ReadPreprocessorOptions( 763 const PreprocessorOptions &PPOpts, 764 bool Complain, 765 std::string &SuggestedPredefines) { 766 return checkPreprocessorOptions(PPOpts, 767 PP.getPreprocessorOpts(), 768 nullptr, 769 PP.getFileManager(), 770 SuggestedPredefines, 771 PP.getLangOpts(), 772 false); 773 } 774 775 /// Check the header search options deserialized from the control block 776 /// against the header search options in an existing preprocessor. 777 /// 778 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 779 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 780 StringRef SpecificModuleCachePath, 781 StringRef ExistingModuleCachePath, 782 DiagnosticsEngine *Diags, 783 const LangOptions &LangOpts) { 784 if (LangOpts.Modules) { 785 if (SpecificModuleCachePath != ExistingModuleCachePath) { 786 if (Diags) 787 Diags->Report(diag::err_pch_modulecache_mismatch) 788 << SpecificModuleCachePath << ExistingModuleCachePath; 789 return true; 790 } 791 } 792 793 return false; 794 } 795 796 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 797 StringRef SpecificModuleCachePath, 798 bool Complain) { 799 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 800 PP.getHeaderSearchInfo().getModuleCachePath(), 801 Complain ? &Reader.Diags : nullptr, 802 PP.getLangOpts()); 803 } 804 805 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) { 806 PP.setCounterValue(Value); 807 } 808 809 //===----------------------------------------------------------------------===// 810 // AST reader implementation 811 //===----------------------------------------------------------------------===// 812 813 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener, 814 bool TakeOwnership) { 815 DeserializationListener = Listener; 816 OwnsDeserializationListener = TakeOwnership; 817 } 818 819 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) { 820 return serialization::ComputeHash(Sel); 821 } 822 823 std::pair<unsigned, unsigned> 824 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 825 using namespace llvm::support; 826 827 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 828 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 829 return std::make_pair(KeyLen, DataLen); 830 } 831 832 ASTSelectorLookupTrait::internal_key_type 833 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) { 834 using namespace llvm::support; 835 836 SelectorTable &SelTable = Reader.getContext().Selectors; 837 unsigned N = endian::readNext<uint16_t, little, unaligned>(d); 838 IdentifierInfo *FirstII = Reader.getLocalIdentifier( 839 F, endian::readNext<uint32_t, little, unaligned>(d)); 840 if (N == 0) 841 return SelTable.getNullarySelector(FirstII); 842 else if (N == 1) 843 return SelTable.getUnarySelector(FirstII); 844 845 SmallVector<IdentifierInfo *, 16> Args; 846 Args.push_back(FirstII); 847 for (unsigned I = 1; I != N; ++I) 848 Args.push_back(Reader.getLocalIdentifier( 849 F, endian::readNext<uint32_t, little, unaligned>(d))); 850 851 return SelTable.getSelector(N, Args.data()); 852 } 853 854 ASTSelectorLookupTrait::data_type 855 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d, 856 unsigned DataLen) { 857 using namespace llvm::support; 858 859 data_type Result; 860 861 Result.ID = Reader.getGlobalSelectorID( 862 F, endian::readNext<uint32_t, little, unaligned>(d)); 863 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d); 864 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d); 865 Result.InstanceBits = FullInstanceBits & 0x3; 866 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1; 867 Result.FactoryBits = FullFactoryBits & 0x3; 868 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1; 869 unsigned NumInstanceMethods = FullInstanceBits >> 3; 870 unsigned NumFactoryMethods = FullFactoryBits >> 3; 871 872 // Load instance methods 873 for (unsigned I = 0; I != NumInstanceMethods; ++I) { 874 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 875 F, endian::readNext<uint32_t, little, unaligned>(d))) 876 Result.Instance.push_back(Method); 877 } 878 879 // Load factory methods 880 for (unsigned I = 0; I != NumFactoryMethods; ++I) { 881 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 882 F, endian::readNext<uint32_t, little, unaligned>(d))) 883 Result.Factory.push_back(Method); 884 } 885 886 return Result; 887 } 888 889 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) { 890 return llvm::djbHash(a); 891 } 892 893 std::pair<unsigned, unsigned> 894 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) { 895 using namespace llvm::support; 896 897 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 898 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 899 return std::make_pair(KeyLen, DataLen); 900 } 901 902 ASTIdentifierLookupTraitBase::internal_key_type 903 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) { 904 assert(n >= 2 && d[n-1] == '\0'); 905 return StringRef((const char*) d, n-1); 906 } 907 908 /// Whether the given identifier is "interesting". 909 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II, 910 bool IsModule) { 911 return II.hadMacroDefinition() || 912 II.isPoisoned() || 913 (IsModule ? II.hasRevertedBuiltin() : II.getObjCOrBuiltinID()) || 914 II.hasRevertedTokenIDToIdentifier() || 915 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) && 916 II.getFETokenInfo()); 917 } 918 919 static bool readBit(unsigned &Bits) { 920 bool Value = Bits & 0x1; 921 Bits >>= 1; 922 return Value; 923 } 924 925 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) { 926 using namespace llvm::support; 927 928 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 929 return Reader.getGlobalIdentifierID(F, RawID >> 1); 930 } 931 932 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) { 933 if (!II.isFromAST()) { 934 II.setIsFromAST(); 935 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr; 936 if (isInterestingIdentifier(Reader, II, IsModule)) 937 II.setChangedSinceDeserialization(); 938 } 939 } 940 941 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k, 942 const unsigned char* d, 943 unsigned DataLen) { 944 using namespace llvm::support; 945 946 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 947 bool IsInteresting = RawID & 0x01; 948 949 // Wipe out the "is interesting" bit. 950 RawID = RawID >> 1; 951 952 // Build the IdentifierInfo and link the identifier ID with it. 953 IdentifierInfo *II = KnownII; 954 if (!II) { 955 II = &Reader.getIdentifierTable().getOwn(k); 956 KnownII = II; 957 } 958 markIdentifierFromAST(Reader, *II); 959 Reader.markIdentifierUpToDate(II); 960 961 IdentID ID = Reader.getGlobalIdentifierID(F, RawID); 962 if (!IsInteresting) { 963 // For uninteresting identifiers, there's nothing else to do. Just notify 964 // the reader that we've finished loading this identifier. 965 Reader.SetIdentifierInfo(ID, II); 966 return II; 967 } 968 969 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d); 970 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d); 971 bool CPlusPlusOperatorKeyword = readBit(Bits); 972 bool HasRevertedTokenIDToIdentifier = readBit(Bits); 973 bool HasRevertedBuiltin = readBit(Bits); 974 bool Poisoned = readBit(Bits); 975 bool ExtensionToken = readBit(Bits); 976 bool HadMacroDefinition = readBit(Bits); 977 978 assert(Bits == 0 && "Extra bits in the identifier?"); 979 DataLen -= 8; 980 981 // Set or check the various bits in the IdentifierInfo structure. 982 // Token IDs are read-only. 983 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier) 984 II->revertTokenIDToIdentifier(); 985 if (!F.isModule()) 986 II->setObjCOrBuiltinID(ObjCOrBuiltinID); 987 else if (HasRevertedBuiltin && II->getBuiltinID()) { 988 II->revertBuiltin(); 989 assert((II->hasRevertedBuiltin() || 990 II->getObjCOrBuiltinID() == ObjCOrBuiltinID) && 991 "Incorrect ObjC keyword or builtin ID"); 992 } 993 assert(II->isExtensionToken() == ExtensionToken && 994 "Incorrect extension token flag"); 995 (void)ExtensionToken; 996 if (Poisoned) 997 II->setIsPoisoned(true); 998 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword && 999 "Incorrect C++ operator keyword flag"); 1000 (void)CPlusPlusOperatorKeyword; 1001 1002 // If this identifier is a macro, deserialize the macro 1003 // definition. 1004 if (HadMacroDefinition) { 1005 uint32_t MacroDirectivesOffset = 1006 endian::readNext<uint32_t, little, unaligned>(d); 1007 DataLen -= 4; 1008 1009 Reader.addPendingMacro(II, &F, MacroDirectivesOffset); 1010 } 1011 1012 Reader.SetIdentifierInfo(ID, II); 1013 1014 // Read all of the declarations visible at global scope with this 1015 // name. 1016 if (DataLen > 0) { 1017 SmallVector<uint32_t, 4> DeclIDs; 1018 for (; DataLen > 0; DataLen -= 4) 1019 DeclIDs.push_back(Reader.getGlobalDeclID( 1020 F, endian::readNext<uint32_t, little, unaligned>(d))); 1021 Reader.SetGloballyVisibleDecls(II, DeclIDs); 1022 } 1023 1024 return II; 1025 } 1026 1027 DeclarationNameKey::DeclarationNameKey(DeclarationName Name) 1028 : Kind(Name.getNameKind()) { 1029 switch (Kind) { 1030 case DeclarationName::Identifier: 1031 Data = (uint64_t)Name.getAsIdentifierInfo(); 1032 break; 1033 case DeclarationName::ObjCZeroArgSelector: 1034 case DeclarationName::ObjCOneArgSelector: 1035 case DeclarationName::ObjCMultiArgSelector: 1036 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr(); 1037 break; 1038 case DeclarationName::CXXOperatorName: 1039 Data = Name.getCXXOverloadedOperator(); 1040 break; 1041 case DeclarationName::CXXLiteralOperatorName: 1042 Data = (uint64_t)Name.getCXXLiteralIdentifier(); 1043 break; 1044 case DeclarationName::CXXDeductionGuideName: 1045 Data = (uint64_t)Name.getCXXDeductionGuideTemplate() 1046 ->getDeclName().getAsIdentifierInfo(); 1047 break; 1048 case DeclarationName::CXXConstructorName: 1049 case DeclarationName::CXXDestructorName: 1050 case DeclarationName::CXXConversionFunctionName: 1051 case DeclarationName::CXXUsingDirective: 1052 Data = 0; 1053 break; 1054 } 1055 } 1056 1057 unsigned DeclarationNameKey::getHash() const { 1058 llvm::FoldingSetNodeID ID; 1059 ID.AddInteger(Kind); 1060 1061 switch (Kind) { 1062 case DeclarationName::Identifier: 1063 case DeclarationName::CXXLiteralOperatorName: 1064 case DeclarationName::CXXDeductionGuideName: 1065 ID.AddString(((IdentifierInfo*)Data)->getName()); 1066 break; 1067 case DeclarationName::ObjCZeroArgSelector: 1068 case DeclarationName::ObjCOneArgSelector: 1069 case DeclarationName::ObjCMultiArgSelector: 1070 ID.AddInteger(serialization::ComputeHash(Selector(Data))); 1071 break; 1072 case DeclarationName::CXXOperatorName: 1073 ID.AddInteger((OverloadedOperatorKind)Data); 1074 break; 1075 case DeclarationName::CXXConstructorName: 1076 case DeclarationName::CXXDestructorName: 1077 case DeclarationName::CXXConversionFunctionName: 1078 case DeclarationName::CXXUsingDirective: 1079 break; 1080 } 1081 1082 return ID.ComputeHash(); 1083 } 1084 1085 ModuleFile * 1086 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) { 1087 using namespace llvm::support; 1088 1089 uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d); 1090 return Reader.getLocalModuleFile(F, ModuleFileID); 1091 } 1092 1093 std::pair<unsigned, unsigned> 1094 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) { 1095 using namespace llvm::support; 1096 1097 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 1098 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 1099 return std::make_pair(KeyLen, DataLen); 1100 } 1101 1102 ASTDeclContextNameLookupTrait::internal_key_type 1103 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) { 1104 using namespace llvm::support; 1105 1106 auto Kind = (DeclarationName::NameKind)*d++; 1107 uint64_t Data; 1108 switch (Kind) { 1109 case DeclarationName::Identifier: 1110 case DeclarationName::CXXLiteralOperatorName: 1111 case DeclarationName::CXXDeductionGuideName: 1112 Data = (uint64_t)Reader.getLocalIdentifier( 1113 F, endian::readNext<uint32_t, little, unaligned>(d)); 1114 break; 1115 case DeclarationName::ObjCZeroArgSelector: 1116 case DeclarationName::ObjCOneArgSelector: 1117 case DeclarationName::ObjCMultiArgSelector: 1118 Data = 1119 (uint64_t)Reader.getLocalSelector( 1120 F, endian::readNext<uint32_t, little, unaligned>( 1121 d)).getAsOpaquePtr(); 1122 break; 1123 case DeclarationName::CXXOperatorName: 1124 Data = *d++; // OverloadedOperatorKind 1125 break; 1126 case DeclarationName::CXXConstructorName: 1127 case DeclarationName::CXXDestructorName: 1128 case DeclarationName::CXXConversionFunctionName: 1129 case DeclarationName::CXXUsingDirective: 1130 Data = 0; 1131 break; 1132 } 1133 1134 return DeclarationNameKey(Kind, Data); 1135 } 1136 1137 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type, 1138 const unsigned char *d, 1139 unsigned DataLen, 1140 data_type_builder &Val) { 1141 using namespace llvm::support; 1142 1143 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) { 1144 uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d); 1145 Val.insert(Reader.getGlobalDeclID(F, LocalID)); 1146 } 1147 } 1148 1149 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M, 1150 BitstreamCursor &Cursor, 1151 uint64_t Offset, 1152 DeclContext *DC) { 1153 assert(Offset != 0); 1154 1155 SavedStreamPosition SavedPosition(Cursor); 1156 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1157 Error(std::move(Err)); 1158 return true; 1159 } 1160 1161 RecordData Record; 1162 StringRef Blob; 1163 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1164 if (!MaybeCode) { 1165 Error(MaybeCode.takeError()); 1166 return true; 1167 } 1168 unsigned Code = MaybeCode.get(); 1169 1170 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1171 if (!MaybeRecCode) { 1172 Error(MaybeRecCode.takeError()); 1173 return true; 1174 } 1175 unsigned RecCode = MaybeRecCode.get(); 1176 if (RecCode != DECL_CONTEXT_LEXICAL) { 1177 Error("Expected lexical block"); 1178 return true; 1179 } 1180 1181 assert(!isa<TranslationUnitDecl>(DC) && 1182 "expected a TU_UPDATE_LEXICAL record for TU"); 1183 // If we are handling a C++ class template instantiation, we can see multiple 1184 // lexical updates for the same record. It's important that we select only one 1185 // of them, so that field numbering works properly. Just pick the first one we 1186 // see. 1187 auto &Lex = LexicalDecls[DC]; 1188 if (!Lex.first) { 1189 Lex = std::make_pair( 1190 &M, llvm::makeArrayRef( 1191 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 1192 Blob.data()), 1193 Blob.size() / 4)); 1194 } 1195 DC->setHasExternalLexicalStorage(true); 1196 return false; 1197 } 1198 1199 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M, 1200 BitstreamCursor &Cursor, 1201 uint64_t Offset, 1202 DeclID ID) { 1203 assert(Offset != 0); 1204 1205 SavedStreamPosition SavedPosition(Cursor); 1206 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1207 Error(std::move(Err)); 1208 return true; 1209 } 1210 1211 RecordData Record; 1212 StringRef Blob; 1213 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1214 if (!MaybeCode) { 1215 Error(MaybeCode.takeError()); 1216 return true; 1217 } 1218 unsigned Code = MaybeCode.get(); 1219 1220 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1221 if (!MaybeRecCode) { 1222 Error(MaybeRecCode.takeError()); 1223 return true; 1224 } 1225 unsigned RecCode = MaybeRecCode.get(); 1226 if (RecCode != DECL_CONTEXT_VISIBLE) { 1227 Error("Expected visible lookup table block"); 1228 return true; 1229 } 1230 1231 // We can't safely determine the primary context yet, so delay attaching the 1232 // lookup table until we're done with recursive deserialization. 1233 auto *Data = (const unsigned char*)Blob.data(); 1234 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data}); 1235 return false; 1236 } 1237 1238 void ASTReader::Error(StringRef Msg) const { 1239 Error(diag::err_fe_pch_malformed, Msg); 1240 if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() && 1241 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) { 1242 Diag(diag::note_module_cache_path) 1243 << PP.getHeaderSearchInfo().getModuleCachePath(); 1244 } 1245 } 1246 1247 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2, 1248 StringRef Arg3) const { 1249 if (Diags.isDiagnosticInFlight()) 1250 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3); 1251 else 1252 Diag(DiagID) << Arg1 << Arg2 << Arg3; 1253 } 1254 1255 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2, 1256 unsigned Select) const { 1257 if (!Diags.isDiagnosticInFlight()) 1258 Diag(DiagID) << Arg1 << Arg2 << Select; 1259 } 1260 1261 void ASTReader::Error(llvm::Error &&Err) const { 1262 Error(toString(std::move(Err))); 1263 } 1264 1265 //===----------------------------------------------------------------------===// 1266 // Source Manager Deserialization 1267 //===----------------------------------------------------------------------===// 1268 1269 /// Read the line table in the source manager block. 1270 /// \returns true if there was an error. 1271 bool ASTReader::ParseLineTable(ModuleFile &F, 1272 const RecordData &Record) { 1273 unsigned Idx = 0; 1274 LineTableInfo &LineTable = SourceMgr.getLineTable(); 1275 1276 // Parse the file names 1277 std::map<int, int> FileIDs; 1278 FileIDs[-1] = -1; // For unspecified filenames. 1279 for (unsigned I = 0; Record[Idx]; ++I) { 1280 // Extract the file name 1281 auto Filename = ReadPath(F, Record, Idx); 1282 FileIDs[I] = LineTable.getLineTableFilenameID(Filename); 1283 } 1284 ++Idx; 1285 1286 // Parse the line entries 1287 std::vector<LineEntry> Entries; 1288 while (Idx < Record.size()) { 1289 int FID = Record[Idx++]; 1290 assert(FID >= 0 && "Serialized line entries for non-local file."); 1291 // Remap FileID from 1-based old view. 1292 FID += F.SLocEntryBaseID - 1; 1293 1294 // Extract the line entries 1295 unsigned NumEntries = Record[Idx++]; 1296 assert(NumEntries && "no line entries for file ID"); 1297 Entries.clear(); 1298 Entries.reserve(NumEntries); 1299 for (unsigned I = 0; I != NumEntries; ++I) { 1300 unsigned FileOffset = Record[Idx++]; 1301 unsigned LineNo = Record[Idx++]; 1302 int FilenameID = FileIDs[Record[Idx++]]; 1303 SrcMgr::CharacteristicKind FileKind 1304 = (SrcMgr::CharacteristicKind)Record[Idx++]; 1305 unsigned IncludeOffset = Record[Idx++]; 1306 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, 1307 FileKind, IncludeOffset)); 1308 } 1309 LineTable.AddEntry(FileID::get(FID), Entries); 1310 } 1311 1312 return false; 1313 } 1314 1315 /// Read a source manager block 1316 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) { 1317 using namespace SrcMgr; 1318 1319 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor; 1320 1321 // Set the source-location entry cursor to the current position in 1322 // the stream. This cursor will be used to read the contents of the 1323 // source manager block initially, and then lazily read 1324 // source-location entries as needed. 1325 SLocEntryCursor = F.Stream; 1326 1327 // The stream itself is going to skip over the source manager block. 1328 if (llvm::Error Err = F.Stream.SkipBlock()) { 1329 Error(std::move(Err)); 1330 return true; 1331 } 1332 1333 // Enter the source manager block. 1334 if (llvm::Error Err = 1335 SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) { 1336 Error(std::move(Err)); 1337 return true; 1338 } 1339 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo(); 1340 1341 RecordData Record; 1342 while (true) { 1343 Expected<llvm::BitstreamEntry> MaybeE = 1344 SLocEntryCursor.advanceSkippingSubblocks(); 1345 if (!MaybeE) { 1346 Error(MaybeE.takeError()); 1347 return true; 1348 } 1349 llvm::BitstreamEntry E = MaybeE.get(); 1350 1351 switch (E.Kind) { 1352 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1353 case llvm::BitstreamEntry::Error: 1354 Error("malformed block record in AST file"); 1355 return true; 1356 case llvm::BitstreamEntry::EndBlock: 1357 return false; 1358 case llvm::BitstreamEntry::Record: 1359 // The interesting case. 1360 break; 1361 } 1362 1363 // Read a record. 1364 Record.clear(); 1365 StringRef Blob; 1366 Expected<unsigned> MaybeRecord = 1367 SLocEntryCursor.readRecord(E.ID, Record, &Blob); 1368 if (!MaybeRecord) { 1369 Error(MaybeRecord.takeError()); 1370 return true; 1371 } 1372 switch (MaybeRecord.get()) { 1373 default: // Default behavior: ignore. 1374 break; 1375 1376 case SM_SLOC_FILE_ENTRY: 1377 case SM_SLOC_BUFFER_ENTRY: 1378 case SM_SLOC_EXPANSION_ENTRY: 1379 // Once we hit one of the source location entries, we're done. 1380 return false; 1381 } 1382 } 1383 } 1384 1385 /// If a header file is not found at the path that we expect it to be 1386 /// and the PCH file was moved from its original location, try to resolve the 1387 /// file by assuming that header+PCH were moved together and the header is in 1388 /// the same place relative to the PCH. 1389 static std::string 1390 resolveFileRelativeToOriginalDir(const std::string &Filename, 1391 const std::string &OriginalDir, 1392 const std::string &CurrDir) { 1393 assert(OriginalDir != CurrDir && 1394 "No point trying to resolve the file if the PCH dir didn't change"); 1395 1396 using namespace llvm::sys; 1397 1398 SmallString<128> filePath(Filename); 1399 fs::make_absolute(filePath); 1400 assert(path::is_absolute(OriginalDir)); 1401 SmallString<128> currPCHPath(CurrDir); 1402 1403 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)), 1404 fileDirE = path::end(path::parent_path(filePath)); 1405 path::const_iterator origDirI = path::begin(OriginalDir), 1406 origDirE = path::end(OriginalDir); 1407 // Skip the common path components from filePath and OriginalDir. 1408 while (fileDirI != fileDirE && origDirI != origDirE && 1409 *fileDirI == *origDirI) { 1410 ++fileDirI; 1411 ++origDirI; 1412 } 1413 for (; origDirI != origDirE; ++origDirI) 1414 path::append(currPCHPath, ".."); 1415 path::append(currPCHPath, fileDirI, fileDirE); 1416 path::append(currPCHPath, path::filename(Filename)); 1417 return std::string(currPCHPath.str()); 1418 } 1419 1420 bool ASTReader::ReadSLocEntry(int ID) { 1421 if (ID == 0) 1422 return false; 1423 1424 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1425 Error("source location entry ID out-of-range for AST file"); 1426 return true; 1427 } 1428 1429 // Local helper to read the (possibly-compressed) buffer data following the 1430 // entry record. 1431 auto ReadBuffer = [this]( 1432 BitstreamCursor &SLocEntryCursor, 1433 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> { 1434 RecordData Record; 1435 StringRef Blob; 1436 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode(); 1437 if (!MaybeCode) { 1438 Error(MaybeCode.takeError()); 1439 return nullptr; 1440 } 1441 unsigned Code = MaybeCode.get(); 1442 1443 Expected<unsigned> MaybeRecCode = 1444 SLocEntryCursor.readRecord(Code, Record, &Blob); 1445 if (!MaybeRecCode) { 1446 Error(MaybeRecCode.takeError()); 1447 return nullptr; 1448 } 1449 unsigned RecCode = MaybeRecCode.get(); 1450 1451 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) { 1452 if (!llvm::zlib::isAvailable()) { 1453 Error("zlib is not available"); 1454 return nullptr; 1455 } 1456 SmallString<0> Uncompressed; 1457 if (llvm::Error E = 1458 llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) { 1459 Error("could not decompress embedded file contents: " + 1460 llvm::toString(std::move(E))); 1461 return nullptr; 1462 } 1463 return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name); 1464 } else if (RecCode == SM_SLOC_BUFFER_BLOB) { 1465 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true); 1466 } else { 1467 Error("AST record has invalid code"); 1468 return nullptr; 1469 } 1470 }; 1471 1472 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second; 1473 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit( 1474 F->SLocEntryOffsetsBase + 1475 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) { 1476 Error(std::move(Err)); 1477 return true; 1478 } 1479 1480 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor; 1481 unsigned BaseOffset = F->SLocEntryBaseOffset; 1482 1483 ++NumSLocEntriesRead; 1484 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance(); 1485 if (!MaybeEntry) { 1486 Error(MaybeEntry.takeError()); 1487 return true; 1488 } 1489 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1490 1491 if (Entry.Kind != llvm::BitstreamEntry::Record) { 1492 Error("incorrectly-formatted source location entry in AST file"); 1493 return true; 1494 } 1495 1496 RecordData Record; 1497 StringRef Blob; 1498 Expected<unsigned> MaybeSLOC = 1499 SLocEntryCursor.readRecord(Entry.ID, Record, &Blob); 1500 if (!MaybeSLOC) { 1501 Error(MaybeSLOC.takeError()); 1502 return true; 1503 } 1504 switch (MaybeSLOC.get()) { 1505 default: 1506 Error("incorrectly-formatted source location entry in AST file"); 1507 return true; 1508 1509 case SM_SLOC_FILE_ENTRY: { 1510 // We will detect whether a file changed and return 'Failure' for it, but 1511 // we will also try to fail gracefully by setting up the SLocEntry. 1512 unsigned InputID = Record[4]; 1513 InputFile IF = getInputFile(*F, InputID); 1514 const FileEntry *File = IF.getFile(); 1515 bool OverriddenBuffer = IF.isOverridden(); 1516 1517 // Note that we only check if a File was returned. If it was out-of-date 1518 // we have complained but we will continue creating a FileID to recover 1519 // gracefully. 1520 if (!File) 1521 return true; 1522 1523 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1524 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) { 1525 // This is the module's main file. 1526 IncludeLoc = getImportLocation(F); 1527 } 1528 SrcMgr::CharacteristicKind 1529 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1530 // FIXME: The FileID should be created from the FileEntryRef. 1531 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter, 1532 ID, BaseOffset + Record[0]); 1533 SrcMgr::FileInfo &FileInfo = 1534 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()); 1535 FileInfo.NumCreatedFIDs = Record[5]; 1536 if (Record[3]) 1537 FileInfo.setHasLineDirectives(); 1538 1539 unsigned NumFileDecls = Record[7]; 1540 if (NumFileDecls && ContextObj) { 1541 const DeclID *FirstDecl = F->FileSortedDecls + Record[6]; 1542 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?"); 1543 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl, 1544 NumFileDecls)); 1545 } 1546 1547 const SrcMgr::ContentCache *ContentCache 1548 = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter)); 1549 if (OverriddenBuffer && !ContentCache->BufferOverridden && 1550 ContentCache->ContentsEntry == ContentCache->OrigEntry && 1551 !ContentCache->getRawBuffer()) { 1552 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName()); 1553 if (!Buffer) 1554 return true; 1555 SourceMgr.overrideFileContents(File, std::move(Buffer)); 1556 } 1557 1558 break; 1559 } 1560 1561 case SM_SLOC_BUFFER_ENTRY: { 1562 const char *Name = Blob.data(); 1563 unsigned Offset = Record[0]; 1564 SrcMgr::CharacteristicKind 1565 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1566 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1567 if (IncludeLoc.isInvalid() && F->isModule()) { 1568 IncludeLoc = getImportLocation(F); 1569 } 1570 1571 auto Buffer = ReadBuffer(SLocEntryCursor, Name); 1572 if (!Buffer) 1573 return true; 1574 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID, 1575 BaseOffset + Offset, IncludeLoc); 1576 break; 1577 } 1578 1579 case SM_SLOC_EXPANSION_ENTRY: { 1580 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]); 1581 SourceMgr.createExpansionLoc(SpellingLoc, 1582 ReadSourceLocation(*F, Record[2]), 1583 ReadSourceLocation(*F, Record[3]), 1584 Record[5], 1585 Record[4], 1586 ID, 1587 BaseOffset + Record[0]); 1588 break; 1589 } 1590 } 1591 1592 return false; 1593 } 1594 1595 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) { 1596 if (ID == 0) 1597 return std::make_pair(SourceLocation(), ""); 1598 1599 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1600 Error("source location entry ID out-of-range for AST file"); 1601 return std::make_pair(SourceLocation(), ""); 1602 } 1603 1604 // Find which module file this entry lands in. 1605 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second; 1606 if (!M->isModule()) 1607 return std::make_pair(SourceLocation(), ""); 1608 1609 // FIXME: Can we map this down to a particular submodule? That would be 1610 // ideal. 1611 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName)); 1612 } 1613 1614 /// Find the location where the module F is imported. 1615 SourceLocation ASTReader::getImportLocation(ModuleFile *F) { 1616 if (F->ImportLoc.isValid()) 1617 return F->ImportLoc; 1618 1619 // Otherwise we have a PCH. It's considered to be "imported" at the first 1620 // location of its includer. 1621 if (F->ImportedBy.empty() || !F->ImportedBy[0]) { 1622 // Main file is the importer. 1623 assert(SourceMgr.getMainFileID().isValid() && "missing main file"); 1624 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); 1625 } 1626 return F->ImportedBy[0]->FirstLoc; 1627 } 1628 1629 /// Enter a subblock of the specified BlockID with the specified cursor. Read 1630 /// the abbreviations that are at the top of the block and then leave the cursor 1631 /// pointing into the block. 1632 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID, 1633 uint64_t *StartOfBlockOffset) { 1634 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) { 1635 // FIXME this drops errors on the floor. 1636 consumeError(std::move(Err)); 1637 return true; 1638 } 1639 1640 if (StartOfBlockOffset) 1641 *StartOfBlockOffset = Cursor.GetCurrentBitNo(); 1642 1643 while (true) { 1644 uint64_t Offset = Cursor.GetCurrentBitNo(); 1645 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1646 if (!MaybeCode) { 1647 // FIXME this drops errors on the floor. 1648 consumeError(MaybeCode.takeError()); 1649 return true; 1650 } 1651 unsigned Code = MaybeCode.get(); 1652 1653 // We expect all abbrevs to be at the start of the block. 1654 if (Code != llvm::bitc::DEFINE_ABBREV) { 1655 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1656 // FIXME this drops errors on the floor. 1657 consumeError(std::move(Err)); 1658 return true; 1659 } 1660 return false; 1661 } 1662 if (llvm::Error Err = Cursor.ReadAbbrevRecord()) { 1663 // FIXME this drops errors on the floor. 1664 consumeError(std::move(Err)); 1665 return true; 1666 } 1667 } 1668 } 1669 1670 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record, 1671 unsigned &Idx) { 1672 Token Tok; 1673 Tok.startToken(); 1674 Tok.setLocation(ReadSourceLocation(F, Record, Idx)); 1675 Tok.setLength(Record[Idx++]); 1676 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++])) 1677 Tok.setIdentifierInfo(II); 1678 Tok.setKind((tok::TokenKind)Record[Idx++]); 1679 Tok.setFlag((Token::TokenFlags)Record[Idx++]); 1680 return Tok; 1681 } 1682 1683 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) { 1684 BitstreamCursor &Stream = F.MacroCursor; 1685 1686 // Keep track of where we are in the stream, then jump back there 1687 // after reading this macro. 1688 SavedStreamPosition SavedPosition(Stream); 1689 1690 if (llvm::Error Err = Stream.JumpToBit(Offset)) { 1691 // FIXME this drops errors on the floor. 1692 consumeError(std::move(Err)); 1693 return nullptr; 1694 } 1695 RecordData Record; 1696 SmallVector<IdentifierInfo*, 16> MacroParams; 1697 MacroInfo *Macro = nullptr; 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 1753 if (RecType == PP_MACRO_FUNCTION_LIKE) { 1754 // Decode function-like macro info. 1755 bool isC99VarArgs = Record[NextIndex++]; 1756 bool isGNUVarArgs = Record[NextIndex++]; 1757 bool hasCommaPasting = Record[NextIndex++]; 1758 MacroParams.clear(); 1759 unsigned NumArgs = Record[NextIndex++]; 1760 for (unsigned i = 0; i != NumArgs; ++i) 1761 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++])); 1762 1763 // Install function-like macro info. 1764 MI->setIsFunctionLike(); 1765 if (isC99VarArgs) MI->setIsC99Varargs(); 1766 if (isGNUVarArgs) MI->setIsGNUVarargs(); 1767 if (hasCommaPasting) MI->setHasCommaPasting(); 1768 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator()); 1769 } 1770 1771 // Remember that we saw this macro last so that we add the tokens that 1772 // form its body to it. 1773 Macro = MI; 1774 1775 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() && 1776 Record[NextIndex]) { 1777 // We have a macro definition. Register the association 1778 PreprocessedEntityID 1779 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]); 1780 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 1781 PreprocessingRecord::PPEntityID PPID = 1782 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true); 1783 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>( 1784 PPRec.getPreprocessedEntity(PPID)); 1785 if (PPDef) 1786 PPRec.RegisterMacroDefinition(Macro, PPDef); 1787 } 1788 1789 ++NumMacrosRead; 1790 break; 1791 } 1792 1793 case PP_TOKEN: { 1794 // If we see a TOKEN before a PP_MACRO_*, then the file is 1795 // erroneous, just pretend we didn't see this. 1796 if (!Macro) break; 1797 1798 unsigned Idx = 0; 1799 Token Tok = ReadToken(F, Record, Idx); 1800 Macro->AddTokenToBody(Tok); 1801 break; 1802 } 1803 } 1804 } 1805 } 1806 1807 PreprocessedEntityID 1808 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, 1809 unsigned LocalID) const { 1810 if (!M.ModuleOffsetMap.empty()) 1811 ReadModuleOffsetMap(M); 1812 1813 ContinuousRangeMap<uint32_t, int, 2>::const_iterator 1814 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS); 1815 assert(I != M.PreprocessedEntityRemap.end() 1816 && "Invalid index into preprocessed entity index remap"); 1817 1818 return LocalID + I->second; 1819 } 1820 1821 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) { 1822 return llvm::hash_combine(ikey.Size, ikey.ModTime); 1823 } 1824 1825 HeaderFileInfoTrait::internal_key_type 1826 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) { 1827 internal_key_type ikey = {FE->getSize(), 1828 M.HasTimestamps ? FE->getModificationTime() : 0, 1829 FE->getName(), /*Imported*/ false}; 1830 return ikey; 1831 } 1832 1833 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) { 1834 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime)) 1835 return false; 1836 1837 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename) 1838 return true; 1839 1840 // Determine whether the actual files are equivalent. 1841 FileManager &FileMgr = Reader.getFileManager(); 1842 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* { 1843 if (!Key.Imported) { 1844 if (auto File = FileMgr.getFile(Key.Filename)) 1845 return *File; 1846 return nullptr; 1847 } 1848 1849 std::string Resolved = std::string(Key.Filename); 1850 Reader.ResolveImportedPath(M, Resolved); 1851 if (auto File = FileMgr.getFile(Resolved)) 1852 return *File; 1853 return nullptr; 1854 }; 1855 1856 const FileEntry *FEA = GetFile(a); 1857 const FileEntry *FEB = GetFile(b); 1858 return FEA && FEA == FEB; 1859 } 1860 1861 std::pair<unsigned, unsigned> 1862 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) { 1863 using namespace llvm::support; 1864 1865 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d); 1866 unsigned DataLen = (unsigned) *d++; 1867 return std::make_pair(KeyLen, DataLen); 1868 } 1869 1870 HeaderFileInfoTrait::internal_key_type 1871 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) { 1872 using namespace llvm::support; 1873 1874 internal_key_type ikey; 1875 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d)); 1876 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d)); 1877 ikey.Filename = (const char *)d; 1878 ikey.Imported = true; 1879 return ikey; 1880 } 1881 1882 HeaderFileInfoTrait::data_type 1883 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d, 1884 unsigned DataLen) { 1885 using namespace llvm::support; 1886 1887 const unsigned char *End = d + DataLen; 1888 HeaderFileInfo HFI; 1889 unsigned Flags = *d++; 1890 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp. 1891 HFI.isImport |= (Flags >> 5) & 0x01; 1892 HFI.isPragmaOnce |= (Flags >> 4) & 0x01; 1893 HFI.DirInfo = (Flags >> 1) & 0x07; 1894 HFI.IndexHeaderMapHeader = Flags & 0x01; 1895 // FIXME: Find a better way to handle this. Maybe just store a 1896 // "has been included" flag? 1897 HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d), 1898 HFI.NumIncludes); 1899 HFI.ControllingMacroID = Reader.getGlobalIdentifierID( 1900 M, endian::readNext<uint32_t, little, unaligned>(d)); 1901 if (unsigned FrameworkOffset = 1902 endian::readNext<uint32_t, little, unaligned>(d)) { 1903 // The framework offset is 1 greater than the actual offset, 1904 // since 0 is used as an indicator for "no framework name". 1905 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1); 1906 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName); 1907 } 1908 1909 assert((End - d) % 4 == 0 && 1910 "Wrong data length in HeaderFileInfo deserialization"); 1911 while (d != End) { 1912 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d); 1913 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3); 1914 LocalSMID >>= 2; 1915 1916 // This header is part of a module. Associate it with the module to enable 1917 // implicit module import. 1918 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID); 1919 Module *Mod = Reader.getSubmodule(GlobalSMID); 1920 FileManager &FileMgr = Reader.getFileManager(); 1921 ModuleMap &ModMap = 1922 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap(); 1923 1924 std::string Filename = std::string(key.Filename); 1925 if (key.Imported) 1926 Reader.ResolveImportedPath(M, Filename); 1927 // FIXME: This is not always the right filename-as-written, but we're not 1928 // going to use this information to rebuild the module, so it doesn't make 1929 // a lot of difference. 1930 Module::Header H = {std::string(key.Filename), *FileMgr.getFile(Filename)}; 1931 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true); 1932 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader); 1933 } 1934 1935 // This HeaderFileInfo was externally loaded. 1936 HFI.External = true; 1937 HFI.IsValid = true; 1938 return HFI; 1939 } 1940 1941 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M, 1942 uint32_t MacroDirectivesOffset) { 1943 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard"); 1944 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset)); 1945 } 1946 1947 void ASTReader::ReadDefinedMacros() { 1948 // Note that we are loading defined macros. 1949 Deserializing Macros(this); 1950 1951 for (ModuleFile &I : llvm::reverse(ModuleMgr)) { 1952 BitstreamCursor &MacroCursor = I.MacroCursor; 1953 1954 // If there was no preprocessor block, skip this file. 1955 if (MacroCursor.getBitcodeBytes().empty()) 1956 continue; 1957 1958 BitstreamCursor Cursor = MacroCursor; 1959 if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) { 1960 Error(std::move(Err)); 1961 return; 1962 } 1963 1964 RecordData Record; 1965 while (true) { 1966 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks(); 1967 if (!MaybeE) { 1968 Error(MaybeE.takeError()); 1969 return; 1970 } 1971 llvm::BitstreamEntry E = MaybeE.get(); 1972 1973 switch (E.Kind) { 1974 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1975 case llvm::BitstreamEntry::Error: 1976 Error("malformed block record in AST file"); 1977 return; 1978 case llvm::BitstreamEntry::EndBlock: 1979 goto NextCursor; 1980 1981 case llvm::BitstreamEntry::Record: { 1982 Record.clear(); 1983 Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record); 1984 if (!MaybeRecord) { 1985 Error(MaybeRecord.takeError()); 1986 return; 1987 } 1988 switch (MaybeRecord.get()) { 1989 default: // Default behavior: ignore. 1990 break; 1991 1992 case PP_MACRO_OBJECT_LIKE: 1993 case PP_MACRO_FUNCTION_LIKE: { 1994 IdentifierInfo *II = getLocalIdentifier(I, Record[0]); 1995 if (II->isOutOfDate()) 1996 updateOutOfDateIdentifier(*II); 1997 break; 1998 } 1999 2000 case PP_TOKEN: 2001 // Ignore tokens. 2002 break; 2003 } 2004 break; 2005 } 2006 } 2007 } 2008 NextCursor: ; 2009 } 2010 } 2011 2012 namespace { 2013 2014 /// Visitor class used to look up identifirs in an AST file. 2015 class IdentifierLookupVisitor { 2016 StringRef Name; 2017 unsigned NameHash; 2018 unsigned PriorGeneration; 2019 unsigned &NumIdentifierLookups; 2020 unsigned &NumIdentifierLookupHits; 2021 IdentifierInfo *Found = nullptr; 2022 2023 public: 2024 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration, 2025 unsigned &NumIdentifierLookups, 2026 unsigned &NumIdentifierLookupHits) 2027 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)), 2028 PriorGeneration(PriorGeneration), 2029 NumIdentifierLookups(NumIdentifierLookups), 2030 NumIdentifierLookupHits(NumIdentifierLookupHits) {} 2031 2032 bool operator()(ModuleFile &M) { 2033 // If we've already searched this module file, skip it now. 2034 if (M.Generation <= PriorGeneration) 2035 return true; 2036 2037 ASTIdentifierLookupTable *IdTable 2038 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable; 2039 if (!IdTable) 2040 return false; 2041 2042 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M, 2043 Found); 2044 ++NumIdentifierLookups; 2045 ASTIdentifierLookupTable::iterator Pos = 2046 IdTable->find_hashed(Name, NameHash, &Trait); 2047 if (Pos == IdTable->end()) 2048 return false; 2049 2050 // Dereferencing the iterator has the effect of building the 2051 // IdentifierInfo node and populating it with the various 2052 // declarations it needs. 2053 ++NumIdentifierLookupHits; 2054 Found = *Pos; 2055 return true; 2056 } 2057 2058 // Retrieve the identifier info found within the module 2059 // files. 2060 IdentifierInfo *getIdentifierInfo() const { return Found; } 2061 }; 2062 2063 } // namespace 2064 2065 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) { 2066 // Note that we are loading an identifier. 2067 Deserializing AnIdentifier(this); 2068 2069 unsigned PriorGeneration = 0; 2070 if (getContext().getLangOpts().Modules) 2071 PriorGeneration = IdentifierGeneration[&II]; 2072 2073 // If there is a global index, look there first to determine which modules 2074 // provably do not have any results for this identifier. 2075 GlobalModuleIndex::HitSet Hits; 2076 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 2077 if (!loadGlobalIndex()) { 2078 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) { 2079 HitsPtr = &Hits; 2080 } 2081 } 2082 2083 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration, 2084 NumIdentifierLookups, 2085 NumIdentifierLookupHits); 2086 ModuleMgr.visit(Visitor, HitsPtr); 2087 markIdentifierUpToDate(&II); 2088 } 2089 2090 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) { 2091 if (!II) 2092 return; 2093 2094 II->setOutOfDate(false); 2095 2096 // Update the generation for this identifier. 2097 if (getContext().getLangOpts().Modules) 2098 IdentifierGeneration[II] = getGeneration(); 2099 } 2100 2101 void ASTReader::resolvePendingMacro(IdentifierInfo *II, 2102 const PendingMacroInfo &PMInfo) { 2103 ModuleFile &M = *PMInfo.M; 2104 2105 BitstreamCursor &Cursor = M.MacroCursor; 2106 SavedStreamPosition SavedPosition(Cursor); 2107 if (llvm::Error Err = 2108 Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) { 2109 Error(std::move(Err)); 2110 return; 2111 } 2112 2113 struct ModuleMacroRecord { 2114 SubmoduleID SubModID; 2115 MacroInfo *MI; 2116 SmallVector<SubmoduleID, 8> Overrides; 2117 }; 2118 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros; 2119 2120 // We expect to see a sequence of PP_MODULE_MACRO records listing exported 2121 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete 2122 // macro histroy. 2123 RecordData Record; 2124 while (true) { 2125 Expected<llvm::BitstreamEntry> MaybeEntry = 2126 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 2127 if (!MaybeEntry) { 2128 Error(MaybeEntry.takeError()); 2129 return; 2130 } 2131 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2132 2133 if (Entry.Kind != llvm::BitstreamEntry::Record) { 2134 Error("malformed block record in AST file"); 2135 return; 2136 } 2137 2138 Record.clear(); 2139 Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record); 2140 if (!MaybePP) { 2141 Error(MaybePP.takeError()); 2142 return; 2143 } 2144 switch ((PreprocessorRecordTypes)MaybePP.get()) { 2145 case PP_MACRO_DIRECTIVE_HISTORY: 2146 break; 2147 2148 case PP_MODULE_MACRO: { 2149 ModuleMacros.push_back(ModuleMacroRecord()); 2150 auto &Info = ModuleMacros.back(); 2151 Info.SubModID = getGlobalSubmoduleID(M, Record[0]); 2152 Info.MI = getMacro(getGlobalMacroID(M, Record[1])); 2153 for (int I = 2, N = Record.size(); I != N; ++I) 2154 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I])); 2155 continue; 2156 } 2157 2158 default: 2159 Error("malformed block record in AST file"); 2160 return; 2161 } 2162 2163 // We found the macro directive history; that's the last record 2164 // for this macro. 2165 break; 2166 } 2167 2168 // Module macros are listed in reverse dependency order. 2169 { 2170 std::reverse(ModuleMacros.begin(), ModuleMacros.end()); 2171 llvm::SmallVector<ModuleMacro*, 8> Overrides; 2172 for (auto &MMR : ModuleMacros) { 2173 Overrides.clear(); 2174 for (unsigned ModID : MMR.Overrides) { 2175 Module *Mod = getSubmodule(ModID); 2176 auto *Macro = PP.getModuleMacro(Mod, II); 2177 assert(Macro && "missing definition for overridden macro"); 2178 Overrides.push_back(Macro); 2179 } 2180 2181 bool Inserted = false; 2182 Module *Owner = getSubmodule(MMR.SubModID); 2183 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted); 2184 } 2185 } 2186 2187 // Don't read the directive history for a module; we don't have anywhere 2188 // to put it. 2189 if (M.isModule()) 2190 return; 2191 2192 // Deserialize the macro directives history in reverse source-order. 2193 MacroDirective *Latest = nullptr, *Earliest = nullptr; 2194 unsigned Idx = 0, N = Record.size(); 2195 while (Idx < N) { 2196 MacroDirective *MD = nullptr; 2197 SourceLocation Loc = ReadSourceLocation(M, Record, Idx); 2198 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++]; 2199 switch (K) { 2200 case MacroDirective::MD_Define: { 2201 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++])); 2202 MD = PP.AllocateDefMacroDirective(MI, Loc); 2203 break; 2204 } 2205 case MacroDirective::MD_Undefine: 2206 MD = PP.AllocateUndefMacroDirective(Loc); 2207 break; 2208 case MacroDirective::MD_Visibility: 2209 bool isPublic = Record[Idx++]; 2210 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic); 2211 break; 2212 } 2213 2214 if (!Latest) 2215 Latest = MD; 2216 if (Earliest) 2217 Earliest->setPrevious(MD); 2218 Earliest = MD; 2219 } 2220 2221 if (Latest) 2222 PP.setLoadedMacroDirective(II, Earliest, Latest); 2223 } 2224 2225 ASTReader::InputFileInfo 2226 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) { 2227 // Go find this input file. 2228 BitstreamCursor &Cursor = F.InputFilesCursor; 2229 SavedStreamPosition SavedPosition(Cursor); 2230 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2231 // FIXME this drops errors on the floor. 2232 consumeError(std::move(Err)); 2233 } 2234 2235 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 2236 if (!MaybeCode) { 2237 // FIXME this drops errors on the floor. 2238 consumeError(MaybeCode.takeError()); 2239 } 2240 unsigned Code = MaybeCode.get(); 2241 RecordData Record; 2242 StringRef Blob; 2243 2244 if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob)) 2245 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE && 2246 "invalid record type for input file"); 2247 else { 2248 // FIXME this drops errors on the floor. 2249 consumeError(Maybe.takeError()); 2250 } 2251 2252 assert(Record[0] == ID && "Bogus stored ID or offset"); 2253 InputFileInfo R; 2254 R.StoredSize = static_cast<off_t>(Record[1]); 2255 R.StoredTime = static_cast<time_t>(Record[2]); 2256 R.Overridden = static_cast<bool>(Record[3]); 2257 R.Transient = static_cast<bool>(Record[4]); 2258 R.TopLevelModuleMap = static_cast<bool>(Record[5]); 2259 R.Filename = std::string(Blob); 2260 ResolveImportedPath(F, R.Filename); 2261 2262 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 2263 if (!MaybeEntry) // FIXME this drops errors on the floor. 2264 consumeError(MaybeEntry.takeError()); 2265 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2266 assert(Entry.Kind == llvm::BitstreamEntry::Record && 2267 "expected record type for input file hash"); 2268 2269 Record.clear(); 2270 if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record)) 2271 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH && 2272 "invalid record type for input file hash"); 2273 else { 2274 // FIXME this drops errors on the floor. 2275 consumeError(Maybe.takeError()); 2276 } 2277 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) | 2278 static_cast<uint64_t>(Record[0]); 2279 return R; 2280 } 2281 2282 static unsigned moduleKindForDiagnostic(ModuleKind Kind); 2283 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) { 2284 // If this ID is bogus, just return an empty input file. 2285 if (ID == 0 || ID > F.InputFilesLoaded.size()) 2286 return InputFile(); 2287 2288 // If we've already loaded this input file, return it. 2289 if (F.InputFilesLoaded[ID-1].getFile()) 2290 return F.InputFilesLoaded[ID-1]; 2291 2292 if (F.InputFilesLoaded[ID-1].isNotFound()) 2293 return InputFile(); 2294 2295 // Go find this input file. 2296 BitstreamCursor &Cursor = F.InputFilesCursor; 2297 SavedStreamPosition SavedPosition(Cursor); 2298 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2299 // FIXME this drops errors on the floor. 2300 consumeError(std::move(Err)); 2301 } 2302 2303 InputFileInfo FI = readInputFileInfo(F, ID); 2304 off_t StoredSize = FI.StoredSize; 2305 time_t StoredTime = FI.StoredTime; 2306 bool Overridden = FI.Overridden; 2307 bool Transient = FI.Transient; 2308 StringRef Filename = FI.Filename; 2309 uint64_t StoredContentHash = FI.ContentHash; 2310 2311 const FileEntry *File = nullptr; 2312 if (auto FE = FileMgr.getFile(Filename, /*OpenFile=*/false)) 2313 File = *FE; 2314 2315 // If we didn't find the file, resolve it relative to the 2316 // original directory from which this AST file was created. 2317 if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() && 2318 F.OriginalDir != F.BaseDirectory) { 2319 std::string Resolved = resolveFileRelativeToOriginalDir( 2320 std::string(Filename), F.OriginalDir, F.BaseDirectory); 2321 if (!Resolved.empty()) 2322 if (auto FE = FileMgr.getFile(Resolved)) 2323 File = *FE; 2324 } 2325 2326 // For an overridden file, create a virtual file with the stored 2327 // size/timestamp. 2328 if ((Overridden || Transient) && File == nullptr) 2329 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime); 2330 2331 if (File == nullptr) { 2332 if (Complain) { 2333 std::string ErrorStr = "could not find file '"; 2334 ErrorStr += Filename; 2335 ErrorStr += "' referenced by AST file '"; 2336 ErrorStr += F.FileName; 2337 ErrorStr += "'"; 2338 Error(ErrorStr); 2339 } 2340 // Record that we didn't find the file. 2341 F.InputFilesLoaded[ID-1] = InputFile::getNotFound(); 2342 return InputFile(); 2343 } 2344 2345 // Check if there was a request to override the contents of the file 2346 // that was part of the precompiled header. Overriding such a file 2347 // can lead to problems when lexing using the source locations from the 2348 // PCH. 2349 SourceManager &SM = getSourceManager(); 2350 // FIXME: Reject if the overrides are different. 2351 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) { 2352 if (Complain) 2353 Error(diag::err_fe_pch_file_overridden, Filename); 2354 2355 // After emitting the diagnostic, bypass the overriding file to recover 2356 // (this creates a separate FileEntry). 2357 File = SM.bypassFileContentsOverride(*File); 2358 if (!File) { 2359 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound(); 2360 return InputFile(); 2361 } 2362 } 2363 2364 enum ModificationType { 2365 Size, 2366 ModTime, 2367 Content, 2368 None, 2369 }; 2370 auto HasInputFileChanged = [&]() { 2371 if (StoredSize != File->getSize()) 2372 return ModificationType::Size; 2373 if (!DisableValidation && StoredTime && 2374 StoredTime != File->getModificationTime()) { 2375 // In case the modification time changes but not the content, 2376 // accept the cached file as legit. 2377 if (ValidateASTInputFilesContent && 2378 StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) { 2379 auto MemBuffOrError = FileMgr.getBufferForFile(File); 2380 if (!MemBuffOrError) { 2381 if (!Complain) 2382 return ModificationType::ModTime; 2383 std::string ErrorStr = "could not get buffer for file '"; 2384 ErrorStr += File->getName(); 2385 ErrorStr += "'"; 2386 Error(ErrorStr); 2387 return ModificationType::ModTime; 2388 } 2389 2390 auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer()); 2391 if (StoredContentHash == static_cast<uint64_t>(ContentHash)) 2392 return ModificationType::None; 2393 return ModificationType::Content; 2394 } 2395 return ModificationType::ModTime; 2396 } 2397 return ModificationType::None; 2398 }; 2399 2400 bool IsOutOfDate = false; 2401 auto FileChange = HasInputFileChanged(); 2402 // For an overridden file, there is nothing to validate. 2403 if (!Overridden && FileChange != ModificationType::None) { 2404 if (Complain) { 2405 // Build a list of the PCH imports that got us here (in reverse). 2406 SmallVector<ModuleFile *, 4> ImportStack(1, &F); 2407 while (!ImportStack.back()->ImportedBy.empty()) 2408 ImportStack.push_back(ImportStack.back()->ImportedBy[0]); 2409 2410 // The top-level PCH is stale. 2411 StringRef TopLevelPCHName(ImportStack.back()->FileName); 2412 unsigned DiagnosticKind = 2413 moduleKindForDiagnostic(ImportStack.back()->Kind); 2414 if (DiagnosticKind == 0) 2415 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName, 2416 (unsigned)FileChange); 2417 else if (DiagnosticKind == 1) 2418 Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName, 2419 (unsigned)FileChange); 2420 else 2421 Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName, 2422 (unsigned)FileChange); 2423 2424 // Print the import stack. 2425 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) { 2426 Diag(diag::note_pch_required_by) 2427 << Filename << ImportStack[0]->FileName; 2428 for (unsigned I = 1; I < ImportStack.size(); ++I) 2429 Diag(diag::note_pch_required_by) 2430 << ImportStack[I-1]->FileName << ImportStack[I]->FileName; 2431 } 2432 2433 if (!Diags.isDiagnosticInFlight()) 2434 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName; 2435 } 2436 2437 IsOutOfDate = true; 2438 } 2439 // FIXME: If the file is overridden and we've already opened it, 2440 // issue an error (or split it into a separate FileEntry). 2441 2442 InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate); 2443 2444 // Note that we've loaded this input file. 2445 F.InputFilesLoaded[ID-1] = IF; 2446 return IF; 2447 } 2448 2449 /// If we are loading a relocatable PCH or module file, and the filename 2450 /// is not an absolute path, add the system or module root to the beginning of 2451 /// the file name. 2452 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) { 2453 // Resolve relative to the base directory, if we have one. 2454 if (!M.BaseDirectory.empty()) 2455 return ResolveImportedPath(Filename, M.BaseDirectory); 2456 } 2457 2458 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) { 2459 if (Filename.empty() || llvm::sys::path::is_absolute(Filename)) 2460 return; 2461 2462 SmallString<128> Buffer; 2463 llvm::sys::path::append(Buffer, Prefix, Filename); 2464 Filename.assign(Buffer.begin(), Buffer.end()); 2465 } 2466 2467 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) { 2468 switch (ARR) { 2469 case ASTReader::Failure: return true; 2470 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing); 2471 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate); 2472 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch); 2473 case ASTReader::ConfigurationMismatch: 2474 return !(Caps & ASTReader::ARR_ConfigurationMismatch); 2475 case ASTReader::HadErrors: return true; 2476 case ASTReader::Success: return false; 2477 } 2478 2479 llvm_unreachable("unknown ASTReadResult"); 2480 } 2481 2482 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock( 2483 BitstreamCursor &Stream, unsigned ClientLoadCapabilities, 2484 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener, 2485 std::string &SuggestedPredefines) { 2486 if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) { 2487 // FIXME this drops errors on the floor. 2488 consumeError(std::move(Err)); 2489 return Failure; 2490 } 2491 2492 // Read all of the records in the options block. 2493 RecordData Record; 2494 ASTReadResult Result = Success; 2495 while (true) { 2496 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2497 if (!MaybeEntry) { 2498 // FIXME this drops errors on the floor. 2499 consumeError(MaybeEntry.takeError()); 2500 return Failure; 2501 } 2502 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2503 2504 switch (Entry.Kind) { 2505 case llvm::BitstreamEntry::Error: 2506 case llvm::BitstreamEntry::SubBlock: 2507 return Failure; 2508 2509 case llvm::BitstreamEntry::EndBlock: 2510 return Result; 2511 2512 case llvm::BitstreamEntry::Record: 2513 // The interesting case. 2514 break; 2515 } 2516 2517 // Read and process a record. 2518 Record.clear(); 2519 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record); 2520 if (!MaybeRecordType) { 2521 // FIXME this drops errors on the floor. 2522 consumeError(MaybeRecordType.takeError()); 2523 return Failure; 2524 } 2525 switch ((OptionsRecordTypes)MaybeRecordType.get()) { 2526 case LANGUAGE_OPTIONS: { 2527 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2528 if (ParseLanguageOptions(Record, Complain, Listener, 2529 AllowCompatibleConfigurationMismatch)) 2530 Result = ConfigurationMismatch; 2531 break; 2532 } 2533 2534 case TARGET_OPTIONS: { 2535 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2536 if (ParseTargetOptions(Record, Complain, Listener, 2537 AllowCompatibleConfigurationMismatch)) 2538 Result = ConfigurationMismatch; 2539 break; 2540 } 2541 2542 case FILE_SYSTEM_OPTIONS: { 2543 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2544 if (!AllowCompatibleConfigurationMismatch && 2545 ParseFileSystemOptions(Record, Complain, Listener)) 2546 Result = ConfigurationMismatch; 2547 break; 2548 } 2549 2550 case HEADER_SEARCH_OPTIONS: { 2551 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2552 if (!AllowCompatibleConfigurationMismatch && 2553 ParseHeaderSearchOptions(Record, Complain, Listener)) 2554 Result = ConfigurationMismatch; 2555 break; 2556 } 2557 2558 case PREPROCESSOR_OPTIONS: 2559 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2560 if (!AllowCompatibleConfigurationMismatch && 2561 ParsePreprocessorOptions(Record, Complain, Listener, 2562 SuggestedPredefines)) 2563 Result = ConfigurationMismatch; 2564 break; 2565 } 2566 } 2567 } 2568 2569 ASTReader::ASTReadResult 2570 ASTReader::ReadControlBlock(ModuleFile &F, 2571 SmallVectorImpl<ImportedModule> &Loaded, 2572 const ModuleFile *ImportedBy, 2573 unsigned ClientLoadCapabilities) { 2574 BitstreamCursor &Stream = F.Stream; 2575 2576 if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) { 2577 Error(std::move(Err)); 2578 return Failure; 2579 } 2580 2581 // Lambda to read the unhashed control block the first time it's called. 2582 // 2583 // For PCM files, the unhashed control block cannot be read until after the 2584 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still 2585 // need to look ahead before reading the IMPORTS record. For consistency, 2586 // this block is always read somehow (see BitstreamEntry::EndBlock). 2587 bool HasReadUnhashedControlBlock = false; 2588 auto readUnhashedControlBlockOnce = [&]() { 2589 if (!HasReadUnhashedControlBlock) { 2590 HasReadUnhashedControlBlock = true; 2591 if (ASTReadResult Result = 2592 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities)) 2593 return Result; 2594 } 2595 return Success; 2596 }; 2597 2598 // Read all of the records and blocks in the control block. 2599 RecordData Record; 2600 unsigned NumInputs = 0; 2601 unsigned NumUserInputs = 0; 2602 StringRef BaseDirectoryAsWritten; 2603 while (true) { 2604 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2605 if (!MaybeEntry) { 2606 Error(MaybeEntry.takeError()); 2607 return Failure; 2608 } 2609 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2610 2611 switch (Entry.Kind) { 2612 case llvm::BitstreamEntry::Error: 2613 Error("malformed block record in AST file"); 2614 return Failure; 2615 case llvm::BitstreamEntry::EndBlock: { 2616 // Validate the module before returning. This call catches an AST with 2617 // no module name and no imports. 2618 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2619 return Result; 2620 2621 // Validate input files. 2622 const HeaderSearchOptions &HSOpts = 2623 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 2624 2625 // All user input files reside at the index range [0, NumUserInputs), and 2626 // system input files reside at [NumUserInputs, NumInputs). For explicitly 2627 // loaded module files, ignore missing inputs. 2628 if (!DisableValidation && F.Kind != MK_ExplicitModule && 2629 F.Kind != MK_PrebuiltModule) { 2630 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 2631 2632 // If we are reading a module, we will create a verification timestamp, 2633 // so we verify all input files. Otherwise, verify only user input 2634 // files. 2635 2636 unsigned N = NumUserInputs; 2637 if (ValidateSystemInputs || 2638 (HSOpts.ModulesValidateOncePerBuildSession && 2639 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp && 2640 F.Kind == MK_ImplicitModule)) 2641 N = NumInputs; 2642 2643 for (unsigned I = 0; I < N; ++I) { 2644 InputFile IF = getInputFile(F, I+1, Complain); 2645 if (!IF.getFile() || IF.isOutOfDate()) 2646 return OutOfDate; 2647 } 2648 } 2649 2650 if (Listener) 2651 Listener->visitModuleFile(F.FileName, F.Kind); 2652 2653 if (Listener && Listener->needsInputFileVisitation()) { 2654 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs 2655 : NumUserInputs; 2656 for (unsigned I = 0; I < N; ++I) { 2657 bool IsSystem = I >= NumUserInputs; 2658 InputFileInfo FI = readInputFileInfo(F, I+1); 2659 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden, 2660 F.Kind == MK_ExplicitModule || 2661 F.Kind == MK_PrebuiltModule); 2662 } 2663 } 2664 2665 return Success; 2666 } 2667 2668 case llvm::BitstreamEntry::SubBlock: 2669 switch (Entry.ID) { 2670 case INPUT_FILES_BLOCK_ID: 2671 F.InputFilesCursor = Stream; 2672 if (llvm::Error Err = Stream.SkipBlock()) { 2673 Error(std::move(Err)); 2674 return Failure; 2675 } 2676 if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) { 2677 Error("malformed block record in AST file"); 2678 return Failure; 2679 } 2680 continue; 2681 2682 case OPTIONS_BLOCK_ID: 2683 // If we're reading the first module for this group, check its options 2684 // are compatible with ours. For modules it imports, no further checking 2685 // is required, because we checked them when we built it. 2686 if (Listener && !ImportedBy) { 2687 // Should we allow the configuration of the module file to differ from 2688 // the configuration of the current translation unit in a compatible 2689 // way? 2690 // 2691 // FIXME: Allow this for files explicitly specified with -include-pch. 2692 bool AllowCompatibleConfigurationMismatch = 2693 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 2694 2695 ASTReadResult Result = 2696 ReadOptionsBlock(Stream, ClientLoadCapabilities, 2697 AllowCompatibleConfigurationMismatch, *Listener, 2698 SuggestedPredefines); 2699 if (Result == Failure) { 2700 Error("malformed block record in AST file"); 2701 return Result; 2702 } 2703 2704 if (DisableValidation || 2705 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 2706 Result = Success; 2707 2708 // If we can't load the module, exit early since we likely 2709 // will rebuild the module anyway. The stream may be in the 2710 // middle of a block. 2711 if (Result != Success) 2712 return Result; 2713 } else if (llvm::Error Err = Stream.SkipBlock()) { 2714 Error(std::move(Err)); 2715 return Failure; 2716 } 2717 continue; 2718 2719 default: 2720 if (llvm::Error Err = Stream.SkipBlock()) { 2721 Error(std::move(Err)); 2722 return Failure; 2723 } 2724 continue; 2725 } 2726 2727 case llvm::BitstreamEntry::Record: 2728 // The interesting case. 2729 break; 2730 } 2731 2732 // Read and process a record. 2733 Record.clear(); 2734 StringRef Blob; 2735 Expected<unsigned> MaybeRecordType = 2736 Stream.readRecord(Entry.ID, Record, &Blob); 2737 if (!MaybeRecordType) { 2738 Error(MaybeRecordType.takeError()); 2739 return Failure; 2740 } 2741 switch ((ControlRecordTypes)MaybeRecordType.get()) { 2742 case METADATA: { 2743 if (Record[0] != VERSION_MAJOR && !DisableValidation) { 2744 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2745 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old 2746 : diag::err_pch_version_too_new); 2747 return VersionMismatch; 2748 } 2749 2750 bool hasErrors = Record[6]; 2751 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) { 2752 Diag(diag::err_pch_with_compiler_errors); 2753 return HadErrors; 2754 } 2755 if (hasErrors) { 2756 Diags.ErrorOccurred = true; 2757 Diags.UncompilableErrorOccurred = true; 2758 Diags.UnrecoverableErrorOccurred = true; 2759 } 2760 2761 F.RelocatablePCH = Record[4]; 2762 // Relative paths in a relocatable PCH are relative to our sysroot. 2763 if (F.RelocatablePCH) 2764 F.BaseDirectory = isysroot.empty() ? "/" : isysroot; 2765 2766 F.HasTimestamps = Record[5]; 2767 2768 const std::string &CurBranch = getClangFullRepositoryVersion(); 2769 StringRef ASTBranch = Blob; 2770 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) { 2771 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2772 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch; 2773 return VersionMismatch; 2774 } 2775 break; 2776 } 2777 2778 case IMPORTS: { 2779 // Validate the AST before processing any imports (otherwise, untangling 2780 // them can be error-prone and expensive). A module will have a name and 2781 // will already have been validated, but this catches the PCH case. 2782 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2783 return Result; 2784 2785 // Load each of the imported PCH files. 2786 unsigned Idx = 0, N = Record.size(); 2787 while (Idx < N) { 2788 // Read information about the AST file. 2789 ModuleKind ImportedKind = (ModuleKind)Record[Idx++]; 2790 // The import location will be the local one for now; we will adjust 2791 // all import locations of module imports after the global source 2792 // location info are setup, in ReadAST. 2793 SourceLocation ImportLoc = 2794 ReadUntranslatedSourceLocation(Record[Idx++]); 2795 off_t StoredSize = (off_t)Record[Idx++]; 2796 time_t StoredModTime = (time_t)Record[Idx++]; 2797 auto FirstSignatureByte = Record.begin() + Idx; 2798 ASTFileSignature StoredSignature = ASTFileSignature::create( 2799 FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size); 2800 Idx += ASTFileSignature::size; 2801 2802 std::string ImportedName = ReadString(Record, Idx); 2803 std::string ImportedFile; 2804 2805 // For prebuilt and explicit modules first consult the file map for 2806 // an override. Note that here we don't search prebuilt module 2807 // directories, only the explicit name to file mappings. Also, we will 2808 // still verify the size/signature making sure it is essentially the 2809 // same file but perhaps in a different location. 2810 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule) 2811 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName( 2812 ImportedName, /*FileMapOnly*/ true); 2813 2814 if (ImportedFile.empty()) 2815 // Use BaseDirectoryAsWritten to ensure we use the same path in the 2816 // ModuleCache as when writing. 2817 ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx); 2818 else 2819 SkipPath(Record, Idx); 2820 2821 // If our client can't cope with us being out of date, we can't cope with 2822 // our dependency being missing. 2823 unsigned Capabilities = ClientLoadCapabilities; 2824 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 2825 Capabilities &= ~ARR_Missing; 2826 2827 // Load the AST file. 2828 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, 2829 Loaded, StoredSize, StoredModTime, 2830 StoredSignature, Capabilities); 2831 2832 // If we diagnosed a problem, produce a backtrace. 2833 if (isDiagnosedResult(Result, Capabilities)) 2834 Diag(diag::note_module_file_imported_by) 2835 << F.FileName << !F.ModuleName.empty() << F.ModuleName; 2836 2837 switch (Result) { 2838 case Failure: return Failure; 2839 // If we have to ignore the dependency, we'll have to ignore this too. 2840 case Missing: 2841 case OutOfDate: return OutOfDate; 2842 case VersionMismatch: return VersionMismatch; 2843 case ConfigurationMismatch: return ConfigurationMismatch; 2844 case HadErrors: return HadErrors; 2845 case Success: break; 2846 } 2847 } 2848 break; 2849 } 2850 2851 case ORIGINAL_FILE: 2852 F.OriginalSourceFileID = FileID::get(Record[0]); 2853 F.ActualOriginalSourceFileName = std::string(Blob); 2854 F.OriginalSourceFileName = F.ActualOriginalSourceFileName; 2855 ResolveImportedPath(F, F.OriginalSourceFileName); 2856 break; 2857 2858 case ORIGINAL_FILE_ID: 2859 F.OriginalSourceFileID = FileID::get(Record[0]); 2860 break; 2861 2862 case ORIGINAL_PCH_DIR: 2863 F.OriginalDir = std::string(Blob); 2864 break; 2865 2866 case MODULE_NAME: 2867 F.ModuleName = std::string(Blob); 2868 Diag(diag::remark_module_import) 2869 << F.ModuleName << F.FileName << (ImportedBy ? true : false) 2870 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 2871 if (Listener) 2872 Listener->ReadModuleName(F.ModuleName); 2873 2874 // Validate the AST as soon as we have a name so we can exit early on 2875 // failure. 2876 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2877 return Result; 2878 2879 break; 2880 2881 case MODULE_DIRECTORY: { 2882 // Save the BaseDirectory as written in the PCM for computing the module 2883 // filename for the ModuleCache. 2884 BaseDirectoryAsWritten = Blob; 2885 assert(!F.ModuleName.empty() && 2886 "MODULE_DIRECTORY found before MODULE_NAME"); 2887 // If we've already loaded a module map file covering this module, we may 2888 // have a better path for it (relative to the current build). 2889 Module *M = PP.getHeaderSearchInfo().lookupModule( 2890 F.ModuleName, /*AllowSearch*/ true, 2891 /*AllowExtraModuleMapSearch*/ true); 2892 if (M && M->Directory) { 2893 // If we're implicitly loading a module, the base directory can't 2894 // change between the build and use. 2895 // Don't emit module relocation error if we have -fno-validate-pch 2896 if (!PP.getPreprocessorOpts().DisablePCHValidation && 2897 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) { 2898 auto BuildDir = PP.getFileManager().getDirectory(Blob); 2899 if (!BuildDir || *BuildDir != M->Directory) { 2900 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 2901 Diag(diag::err_imported_module_relocated) 2902 << F.ModuleName << Blob << M->Directory->getName(); 2903 return OutOfDate; 2904 } 2905 } 2906 F.BaseDirectory = std::string(M->Directory->getName()); 2907 } else { 2908 F.BaseDirectory = std::string(Blob); 2909 } 2910 break; 2911 } 2912 2913 case MODULE_MAP_FILE: 2914 if (ASTReadResult Result = 2915 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities)) 2916 return Result; 2917 break; 2918 2919 case INPUT_FILE_OFFSETS: 2920 NumInputs = Record[0]; 2921 NumUserInputs = Record[1]; 2922 F.InputFileOffsets = 2923 (const llvm::support::unaligned_uint64_t *)Blob.data(); 2924 F.InputFilesLoaded.resize(NumInputs); 2925 F.NumUserInputFiles = NumUserInputs; 2926 break; 2927 } 2928 } 2929 } 2930 2931 ASTReader::ASTReadResult 2932 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) { 2933 BitstreamCursor &Stream = F.Stream; 2934 2935 if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) { 2936 Error(std::move(Err)); 2937 return Failure; 2938 } 2939 F.ASTBlockStartOffset = Stream.GetCurrentBitNo(); 2940 2941 // Read all of the records and blocks for the AST file. 2942 RecordData Record; 2943 while (true) { 2944 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2945 if (!MaybeEntry) { 2946 Error(MaybeEntry.takeError()); 2947 return Failure; 2948 } 2949 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2950 2951 switch (Entry.Kind) { 2952 case llvm::BitstreamEntry::Error: 2953 Error("error at end of module block in AST file"); 2954 return Failure; 2955 case llvm::BitstreamEntry::EndBlock: 2956 // Outside of C++, we do not store a lookup map for the translation unit. 2957 // Instead, mark it as needing a lookup map to be built if this module 2958 // contains any declarations lexically within it (which it always does!). 2959 // This usually has no cost, since we very rarely need the lookup map for 2960 // the translation unit outside C++. 2961 if (ASTContext *Ctx = ContextObj) { 2962 DeclContext *DC = Ctx->getTranslationUnitDecl(); 2963 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus) 2964 DC->setMustBuildLookupTable(); 2965 } 2966 2967 return Success; 2968 case llvm::BitstreamEntry::SubBlock: 2969 switch (Entry.ID) { 2970 case DECLTYPES_BLOCK_ID: 2971 // We lazily load the decls block, but we want to set up the 2972 // DeclsCursor cursor to point into it. Clone our current bitcode 2973 // cursor to it, enter the block and read the abbrevs in that block. 2974 // With the main cursor, we just skip over it. 2975 F.DeclsCursor = Stream; 2976 if (llvm::Error Err = Stream.SkipBlock()) { 2977 Error(std::move(Err)); 2978 return Failure; 2979 } 2980 if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID, 2981 &F.DeclsBlockStartOffset)) { 2982 Error("malformed block record in AST file"); 2983 return Failure; 2984 } 2985 break; 2986 2987 case PREPROCESSOR_BLOCK_ID: 2988 F.MacroCursor = Stream; 2989 if (!PP.getExternalSource()) 2990 PP.setExternalSource(this); 2991 2992 if (llvm::Error Err = Stream.SkipBlock()) { 2993 Error(std::move(Err)); 2994 return Failure; 2995 } 2996 if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) { 2997 Error("malformed block record in AST file"); 2998 return Failure; 2999 } 3000 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo(); 3001 break; 3002 3003 case PREPROCESSOR_DETAIL_BLOCK_ID: 3004 F.PreprocessorDetailCursor = Stream; 3005 3006 if (llvm::Error Err = Stream.SkipBlock()) { 3007 Error(std::move(Err)); 3008 return Failure; 3009 } 3010 if (ReadBlockAbbrevs(F.PreprocessorDetailCursor, 3011 PREPROCESSOR_DETAIL_BLOCK_ID)) { 3012 Error("malformed preprocessor detail record in AST file"); 3013 return Failure; 3014 } 3015 F.PreprocessorDetailStartOffset 3016 = F.PreprocessorDetailCursor.GetCurrentBitNo(); 3017 3018 if (!PP.getPreprocessingRecord()) 3019 PP.createPreprocessingRecord(); 3020 if (!PP.getPreprocessingRecord()->getExternalSource()) 3021 PP.getPreprocessingRecord()->SetExternalSource(*this); 3022 break; 3023 3024 case SOURCE_MANAGER_BLOCK_ID: 3025 if (ReadSourceManagerBlock(F)) 3026 return Failure; 3027 break; 3028 3029 case SUBMODULE_BLOCK_ID: 3030 if (ASTReadResult Result = 3031 ReadSubmoduleBlock(F, ClientLoadCapabilities)) 3032 return Result; 3033 break; 3034 3035 case COMMENTS_BLOCK_ID: { 3036 BitstreamCursor C = Stream; 3037 3038 if (llvm::Error Err = Stream.SkipBlock()) { 3039 Error(std::move(Err)); 3040 return Failure; 3041 } 3042 if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) { 3043 Error("malformed comments block in AST file"); 3044 return Failure; 3045 } 3046 CommentsCursors.push_back(std::make_pair(C, &F)); 3047 break; 3048 } 3049 3050 default: 3051 if (llvm::Error Err = Stream.SkipBlock()) { 3052 Error(std::move(Err)); 3053 return Failure; 3054 } 3055 break; 3056 } 3057 continue; 3058 3059 case llvm::BitstreamEntry::Record: 3060 // The interesting case. 3061 break; 3062 } 3063 3064 // Read and process a record. 3065 Record.clear(); 3066 StringRef Blob; 3067 Expected<unsigned> MaybeRecordType = 3068 Stream.readRecord(Entry.ID, Record, &Blob); 3069 if (!MaybeRecordType) { 3070 Error(MaybeRecordType.takeError()); 3071 return Failure; 3072 } 3073 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get(); 3074 3075 // If we're not loading an AST context, we don't care about most records. 3076 if (!ContextObj) { 3077 switch (RecordType) { 3078 case IDENTIFIER_TABLE: 3079 case IDENTIFIER_OFFSET: 3080 case INTERESTING_IDENTIFIERS: 3081 case STATISTICS: 3082 case PP_CONDITIONAL_STACK: 3083 case PP_COUNTER_VALUE: 3084 case SOURCE_LOCATION_OFFSETS: 3085 case MODULE_OFFSET_MAP: 3086 case SOURCE_MANAGER_LINE_TABLE: 3087 case SOURCE_LOCATION_PRELOADS: 3088 case PPD_ENTITIES_OFFSETS: 3089 case HEADER_SEARCH_TABLE: 3090 case IMPORTED_MODULES: 3091 case MACRO_OFFSET: 3092 break; 3093 default: 3094 continue; 3095 } 3096 } 3097 3098 switch (RecordType) { 3099 default: // Default behavior: ignore. 3100 break; 3101 3102 case TYPE_OFFSET: { 3103 if (F.LocalNumTypes != 0) { 3104 Error("duplicate TYPE_OFFSET record in AST file"); 3105 return Failure; 3106 } 3107 F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data()); 3108 F.LocalNumTypes = Record[0]; 3109 unsigned LocalBaseTypeIndex = Record[1]; 3110 F.BaseTypeIndex = getTotalNumTypes(); 3111 3112 if (F.LocalNumTypes > 0) { 3113 // Introduce the global -> local mapping for types within this module. 3114 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F)); 3115 3116 // Introduce the local -> global mapping for types within this module. 3117 F.TypeRemap.insertOrReplace( 3118 std::make_pair(LocalBaseTypeIndex, 3119 F.BaseTypeIndex - LocalBaseTypeIndex)); 3120 3121 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes); 3122 } 3123 break; 3124 } 3125 3126 case DECL_OFFSET: { 3127 if (F.LocalNumDecls != 0) { 3128 Error("duplicate DECL_OFFSET record in AST file"); 3129 return Failure; 3130 } 3131 F.DeclOffsets = (const DeclOffset *)Blob.data(); 3132 F.LocalNumDecls = Record[0]; 3133 unsigned LocalBaseDeclID = Record[1]; 3134 F.BaseDeclID = getTotalNumDecls(); 3135 3136 if (F.LocalNumDecls > 0) { 3137 // Introduce the global -> local mapping for declarations within this 3138 // module. 3139 GlobalDeclMap.insert( 3140 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F)); 3141 3142 // Introduce the local -> global mapping for declarations within this 3143 // module. 3144 F.DeclRemap.insertOrReplace( 3145 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID)); 3146 3147 // Introduce the global -> local mapping for declarations within this 3148 // module. 3149 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID; 3150 3151 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls); 3152 } 3153 break; 3154 } 3155 3156 case TU_UPDATE_LEXICAL: { 3157 DeclContext *TU = ContextObj->getTranslationUnitDecl(); 3158 LexicalContents Contents( 3159 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 3160 Blob.data()), 3161 static_cast<unsigned int>(Blob.size() / 4)); 3162 TULexicalDecls.push_back(std::make_pair(&F, Contents)); 3163 TU->setHasExternalLexicalStorage(true); 3164 break; 3165 } 3166 3167 case UPDATE_VISIBLE: { 3168 unsigned Idx = 0; 3169 serialization::DeclID ID = ReadDeclID(F, Record, Idx); 3170 auto *Data = (const unsigned char*)Blob.data(); 3171 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data}); 3172 // If we've already loaded the decl, perform the updates when we finish 3173 // loading this block. 3174 if (Decl *D = GetExistingDecl(ID)) 3175 PendingUpdateRecords.push_back( 3176 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3177 break; 3178 } 3179 3180 case IDENTIFIER_TABLE: 3181 F.IdentifierTableData = Blob.data(); 3182 if (Record[0]) { 3183 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create( 3184 (const unsigned char *)F.IdentifierTableData + Record[0], 3185 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t), 3186 (const unsigned char *)F.IdentifierTableData, 3187 ASTIdentifierLookupTrait(*this, F)); 3188 3189 PP.getIdentifierTable().setExternalIdentifierLookup(this); 3190 } 3191 break; 3192 3193 case IDENTIFIER_OFFSET: { 3194 if (F.LocalNumIdentifiers != 0) { 3195 Error("duplicate IDENTIFIER_OFFSET record in AST file"); 3196 return Failure; 3197 } 3198 F.IdentifierOffsets = (const uint32_t *)Blob.data(); 3199 F.LocalNumIdentifiers = Record[0]; 3200 unsigned LocalBaseIdentifierID = Record[1]; 3201 F.BaseIdentifierID = getTotalNumIdentifiers(); 3202 3203 if (F.LocalNumIdentifiers > 0) { 3204 // Introduce the global -> local mapping for identifiers within this 3205 // module. 3206 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1, 3207 &F)); 3208 3209 // Introduce the local -> global mapping for identifiers within this 3210 // module. 3211 F.IdentifierRemap.insertOrReplace( 3212 std::make_pair(LocalBaseIdentifierID, 3213 F.BaseIdentifierID - LocalBaseIdentifierID)); 3214 3215 IdentifiersLoaded.resize(IdentifiersLoaded.size() 3216 + F.LocalNumIdentifiers); 3217 } 3218 break; 3219 } 3220 3221 case INTERESTING_IDENTIFIERS: 3222 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end()); 3223 break; 3224 3225 case EAGERLY_DESERIALIZED_DECLS: 3226 // FIXME: Skip reading this record if our ASTConsumer doesn't care 3227 // about "interesting" decls (for instance, if we're building a module). 3228 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3229 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3230 break; 3231 3232 case MODULAR_CODEGEN_DECLS: 3233 // FIXME: Skip reading this record if our ASTConsumer doesn't care about 3234 // them (ie: if we're not codegenerating this module). 3235 if (F.Kind == MK_MainFile || 3236 getContext().getLangOpts().BuildingPCHWithObjectFile) 3237 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3238 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3239 break; 3240 3241 case SPECIAL_TYPES: 3242 if (SpecialTypes.empty()) { 3243 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3244 SpecialTypes.push_back(getGlobalTypeID(F, Record[I])); 3245 break; 3246 } 3247 3248 if (SpecialTypes.size() != Record.size()) { 3249 Error("invalid special-types record"); 3250 return Failure; 3251 } 3252 3253 for (unsigned I = 0, N = Record.size(); I != N; ++I) { 3254 serialization::TypeID ID = getGlobalTypeID(F, Record[I]); 3255 if (!SpecialTypes[I]) 3256 SpecialTypes[I] = ID; 3257 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate 3258 // merge step? 3259 } 3260 break; 3261 3262 case STATISTICS: 3263 TotalNumStatements += Record[0]; 3264 TotalNumMacros += Record[1]; 3265 TotalLexicalDeclContexts += Record[2]; 3266 TotalVisibleDeclContexts += Record[3]; 3267 break; 3268 3269 case UNUSED_FILESCOPED_DECLS: 3270 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3271 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I])); 3272 break; 3273 3274 case DELEGATING_CTORS: 3275 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3276 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I])); 3277 break; 3278 3279 case WEAK_UNDECLARED_IDENTIFIERS: 3280 if (Record.size() % 4 != 0) { 3281 Error("invalid weak identifiers record"); 3282 return Failure; 3283 } 3284 3285 // FIXME: Ignore weak undeclared identifiers from non-original PCH 3286 // files. This isn't the way to do it :) 3287 WeakUndeclaredIdentifiers.clear(); 3288 3289 // Translate the weak, undeclared identifiers into global IDs. 3290 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) { 3291 WeakUndeclaredIdentifiers.push_back( 3292 getGlobalIdentifierID(F, Record[I++])); 3293 WeakUndeclaredIdentifiers.push_back( 3294 getGlobalIdentifierID(F, Record[I++])); 3295 WeakUndeclaredIdentifiers.push_back( 3296 ReadSourceLocation(F, Record, I).getRawEncoding()); 3297 WeakUndeclaredIdentifiers.push_back(Record[I++]); 3298 } 3299 break; 3300 3301 case SELECTOR_OFFSETS: { 3302 F.SelectorOffsets = (const uint32_t *)Blob.data(); 3303 F.LocalNumSelectors = Record[0]; 3304 unsigned LocalBaseSelectorID = Record[1]; 3305 F.BaseSelectorID = getTotalNumSelectors(); 3306 3307 if (F.LocalNumSelectors > 0) { 3308 // Introduce the global -> local mapping for selectors within this 3309 // module. 3310 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F)); 3311 3312 // Introduce the local -> global mapping for selectors within this 3313 // module. 3314 F.SelectorRemap.insertOrReplace( 3315 std::make_pair(LocalBaseSelectorID, 3316 F.BaseSelectorID - LocalBaseSelectorID)); 3317 3318 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors); 3319 } 3320 break; 3321 } 3322 3323 case METHOD_POOL: 3324 F.SelectorLookupTableData = (const unsigned char *)Blob.data(); 3325 if (Record[0]) 3326 F.SelectorLookupTable 3327 = ASTSelectorLookupTable::Create( 3328 F.SelectorLookupTableData + Record[0], 3329 F.SelectorLookupTableData, 3330 ASTSelectorLookupTrait(*this, F)); 3331 TotalNumMethodPoolEntries += Record[1]; 3332 break; 3333 3334 case REFERENCED_SELECTOR_POOL: 3335 if (!Record.empty()) { 3336 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) { 3337 ReferencedSelectorsData.push_back(getGlobalSelectorID(F, 3338 Record[Idx++])); 3339 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx). 3340 getRawEncoding()); 3341 } 3342 } 3343 break; 3344 3345 case PP_CONDITIONAL_STACK: 3346 if (!Record.empty()) { 3347 unsigned Idx = 0, End = Record.size() - 1; 3348 bool ReachedEOFWhileSkipping = Record[Idx++]; 3349 llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo; 3350 if (ReachedEOFWhileSkipping) { 3351 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx); 3352 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx); 3353 bool FoundNonSkipPortion = Record[Idx++]; 3354 bool FoundElse = Record[Idx++]; 3355 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx); 3356 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion, 3357 FoundElse, ElseLoc); 3358 } 3359 SmallVector<PPConditionalInfo, 4> ConditionalStack; 3360 while (Idx < End) { 3361 auto Loc = ReadSourceLocation(F, Record, Idx); 3362 bool WasSkipping = Record[Idx++]; 3363 bool FoundNonSkip = Record[Idx++]; 3364 bool FoundElse = Record[Idx++]; 3365 ConditionalStack.push_back( 3366 {Loc, WasSkipping, FoundNonSkip, FoundElse}); 3367 } 3368 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo); 3369 } 3370 break; 3371 3372 case PP_COUNTER_VALUE: 3373 if (!Record.empty() && Listener) 3374 Listener->ReadCounter(F, Record[0]); 3375 break; 3376 3377 case FILE_SORTED_DECLS: 3378 F.FileSortedDecls = (const DeclID *)Blob.data(); 3379 F.NumFileSortedDecls = Record[0]; 3380 break; 3381 3382 case SOURCE_LOCATION_OFFSETS: { 3383 F.SLocEntryOffsets = (const uint32_t *)Blob.data(); 3384 F.LocalNumSLocEntries = Record[0]; 3385 unsigned SLocSpaceSize = Record[1]; 3386 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset; 3387 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) = 3388 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, 3389 SLocSpaceSize); 3390 if (!F.SLocEntryBaseID) { 3391 Error("ran out of source locations"); 3392 break; 3393 } 3394 // Make our entry in the range map. BaseID is negative and growing, so 3395 // we invert it. Because we invert it, though, we need the other end of 3396 // the range. 3397 unsigned RangeStart = 3398 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1; 3399 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F)); 3400 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset); 3401 3402 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing. 3403 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0); 3404 GlobalSLocOffsetMap.insert( 3405 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset 3406 - SLocSpaceSize,&F)); 3407 3408 // Initialize the remapping table. 3409 // Invalid stays invalid. 3410 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0)); 3411 // This module. Base was 2 when being compiled. 3412 F.SLocRemap.insertOrReplace(std::make_pair(2U, 3413 static_cast<int>(F.SLocEntryBaseOffset - 2))); 3414 3415 TotalNumSLocEntries += F.LocalNumSLocEntries; 3416 break; 3417 } 3418 3419 case MODULE_OFFSET_MAP: 3420 F.ModuleOffsetMap = Blob; 3421 break; 3422 3423 case SOURCE_MANAGER_LINE_TABLE: 3424 if (ParseLineTable(F, Record)) { 3425 Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file"); 3426 return Failure; 3427 } 3428 break; 3429 3430 case SOURCE_LOCATION_PRELOADS: { 3431 // Need to transform from the local view (1-based IDs) to the global view, 3432 // which is based off F.SLocEntryBaseID. 3433 if (!F.PreloadSLocEntries.empty()) { 3434 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file"); 3435 return Failure; 3436 } 3437 3438 F.PreloadSLocEntries.swap(Record); 3439 break; 3440 } 3441 3442 case EXT_VECTOR_DECLS: 3443 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3444 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I])); 3445 break; 3446 3447 case VTABLE_USES: 3448 if (Record.size() % 3 != 0) { 3449 Error("Invalid VTABLE_USES record"); 3450 return Failure; 3451 } 3452 3453 // Later tables overwrite earlier ones. 3454 // FIXME: Modules will have some trouble with this. This is clearly not 3455 // the right way to do this. 3456 VTableUses.clear(); 3457 3458 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) { 3459 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++])); 3460 VTableUses.push_back( 3461 ReadSourceLocation(F, Record, Idx).getRawEncoding()); 3462 VTableUses.push_back(Record[Idx++]); 3463 } 3464 break; 3465 3466 case PENDING_IMPLICIT_INSTANTIATIONS: 3467 if (PendingInstantiations.size() % 2 != 0) { 3468 Error("Invalid existing PendingInstantiations"); 3469 return Failure; 3470 } 3471 3472 if (Record.size() % 2 != 0) { 3473 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block"); 3474 return Failure; 3475 } 3476 3477 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3478 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++])); 3479 PendingInstantiations.push_back( 3480 ReadSourceLocation(F, Record, I).getRawEncoding()); 3481 } 3482 break; 3483 3484 case SEMA_DECL_REFS: 3485 if (Record.size() != 3) { 3486 Error("Invalid SEMA_DECL_REFS block"); 3487 return Failure; 3488 } 3489 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3490 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3491 break; 3492 3493 case PPD_ENTITIES_OFFSETS: { 3494 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data(); 3495 assert(Blob.size() % sizeof(PPEntityOffset) == 0); 3496 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset); 3497 3498 unsigned LocalBasePreprocessedEntityID = Record[0]; 3499 3500 unsigned StartingID; 3501 if (!PP.getPreprocessingRecord()) 3502 PP.createPreprocessingRecord(); 3503 if (!PP.getPreprocessingRecord()->getExternalSource()) 3504 PP.getPreprocessingRecord()->SetExternalSource(*this); 3505 StartingID 3506 = PP.getPreprocessingRecord() 3507 ->allocateLoadedEntities(F.NumPreprocessedEntities); 3508 F.BasePreprocessedEntityID = StartingID; 3509 3510 if (F.NumPreprocessedEntities > 0) { 3511 // Introduce the global -> local mapping for preprocessed entities in 3512 // this module. 3513 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F)); 3514 3515 // Introduce the local -> global mapping for preprocessed entities in 3516 // this module. 3517 F.PreprocessedEntityRemap.insertOrReplace( 3518 std::make_pair(LocalBasePreprocessedEntityID, 3519 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID)); 3520 } 3521 3522 break; 3523 } 3524 3525 case PPD_SKIPPED_RANGES: { 3526 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data(); 3527 assert(Blob.size() % sizeof(PPSkippedRange) == 0); 3528 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange); 3529 3530 if (!PP.getPreprocessingRecord()) 3531 PP.createPreprocessingRecord(); 3532 if (!PP.getPreprocessingRecord()->getExternalSource()) 3533 PP.getPreprocessingRecord()->SetExternalSource(*this); 3534 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord() 3535 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges); 3536 3537 if (F.NumPreprocessedSkippedRanges > 0) 3538 GlobalSkippedRangeMap.insert( 3539 std::make_pair(F.BasePreprocessedSkippedRangeID, &F)); 3540 break; 3541 } 3542 3543 case DECL_UPDATE_OFFSETS: 3544 if (Record.size() % 2 != 0) { 3545 Error("invalid DECL_UPDATE_OFFSETS block in AST file"); 3546 return Failure; 3547 } 3548 for (unsigned I = 0, N = Record.size(); I != N; I += 2) { 3549 GlobalDeclID ID = getGlobalDeclID(F, Record[I]); 3550 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1])); 3551 3552 // If we've already loaded the decl, perform the updates when we finish 3553 // loading this block. 3554 if (Decl *D = GetExistingDecl(ID)) 3555 PendingUpdateRecords.push_back( 3556 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3557 } 3558 break; 3559 3560 case OBJC_CATEGORIES_MAP: 3561 if (F.LocalNumObjCCategoriesInMap != 0) { 3562 Error("duplicate OBJC_CATEGORIES_MAP record in AST file"); 3563 return Failure; 3564 } 3565 3566 F.LocalNumObjCCategoriesInMap = Record[0]; 3567 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data(); 3568 break; 3569 3570 case OBJC_CATEGORIES: 3571 F.ObjCCategories.swap(Record); 3572 break; 3573 3574 case CUDA_SPECIAL_DECL_REFS: 3575 // Later tables overwrite earlier ones. 3576 // FIXME: Modules will have trouble with this. 3577 CUDASpecialDeclRefs.clear(); 3578 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3579 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3580 break; 3581 3582 case HEADER_SEARCH_TABLE: 3583 F.HeaderFileInfoTableData = Blob.data(); 3584 F.LocalNumHeaderFileInfos = Record[1]; 3585 if (Record[0]) { 3586 F.HeaderFileInfoTable 3587 = HeaderFileInfoLookupTable::Create( 3588 (const unsigned char *)F.HeaderFileInfoTableData + Record[0], 3589 (const unsigned char *)F.HeaderFileInfoTableData, 3590 HeaderFileInfoTrait(*this, F, 3591 &PP.getHeaderSearchInfo(), 3592 Blob.data() + Record[2])); 3593 3594 PP.getHeaderSearchInfo().SetExternalSource(this); 3595 if (!PP.getHeaderSearchInfo().getExternalLookup()) 3596 PP.getHeaderSearchInfo().SetExternalLookup(this); 3597 } 3598 break; 3599 3600 case FP_PRAGMA_OPTIONS: 3601 // Later tables overwrite earlier ones. 3602 FPPragmaOptions.swap(Record); 3603 break; 3604 3605 case OPENCL_EXTENSIONS: 3606 for (unsigned I = 0, E = Record.size(); I != E; ) { 3607 auto Name = ReadString(Record, I); 3608 auto &Opt = OpenCLExtensions.OptMap[Name]; 3609 Opt.Supported = Record[I++] != 0; 3610 Opt.Enabled = Record[I++] != 0; 3611 Opt.Avail = Record[I++]; 3612 Opt.Core = Record[I++]; 3613 } 3614 break; 3615 3616 case OPENCL_EXTENSION_TYPES: 3617 for (unsigned I = 0, E = Record.size(); I != E;) { 3618 auto TypeID = static_cast<::TypeID>(Record[I++]); 3619 auto *Type = GetType(TypeID).getTypePtr(); 3620 auto NumExt = static_cast<unsigned>(Record[I++]); 3621 for (unsigned II = 0; II != NumExt; ++II) { 3622 auto Ext = ReadString(Record, I); 3623 OpenCLTypeExtMap[Type].insert(Ext); 3624 } 3625 } 3626 break; 3627 3628 case OPENCL_EXTENSION_DECLS: 3629 for (unsigned I = 0, E = Record.size(); I != E;) { 3630 auto DeclID = static_cast<::DeclID>(Record[I++]); 3631 auto *Decl = GetDecl(DeclID); 3632 auto NumExt = static_cast<unsigned>(Record[I++]); 3633 for (unsigned II = 0; II != NumExt; ++II) { 3634 auto Ext = ReadString(Record, I); 3635 OpenCLDeclExtMap[Decl].insert(Ext); 3636 } 3637 } 3638 break; 3639 3640 case TENTATIVE_DEFINITIONS: 3641 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3642 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I])); 3643 break; 3644 3645 case KNOWN_NAMESPACES: 3646 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3647 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I])); 3648 break; 3649 3650 case UNDEFINED_BUT_USED: 3651 if (UndefinedButUsed.size() % 2 != 0) { 3652 Error("Invalid existing UndefinedButUsed"); 3653 return Failure; 3654 } 3655 3656 if (Record.size() % 2 != 0) { 3657 Error("invalid undefined-but-used record"); 3658 return Failure; 3659 } 3660 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3661 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++])); 3662 UndefinedButUsed.push_back( 3663 ReadSourceLocation(F, Record, I).getRawEncoding()); 3664 } 3665 break; 3666 3667 case DELETE_EXPRS_TO_ANALYZE: 3668 for (unsigned I = 0, N = Record.size(); I != N;) { 3669 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++])); 3670 const uint64_t Count = Record[I++]; 3671 DelayedDeleteExprs.push_back(Count); 3672 for (uint64_t C = 0; C < Count; ++C) { 3673 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding()); 3674 bool IsArrayForm = Record[I++] == 1; 3675 DelayedDeleteExprs.push_back(IsArrayForm); 3676 } 3677 } 3678 break; 3679 3680 case IMPORTED_MODULES: 3681 if (!F.isModule()) { 3682 // If we aren't loading a module (which has its own exports), make 3683 // all of the imported modules visible. 3684 // FIXME: Deal with macros-only imports. 3685 for (unsigned I = 0, N = Record.size(); I != N; /**/) { 3686 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]); 3687 SourceLocation Loc = ReadSourceLocation(F, Record, I); 3688 if (GlobalID) { 3689 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc)); 3690 if (DeserializationListener) 3691 DeserializationListener->ModuleImportRead(GlobalID, Loc); 3692 } 3693 } 3694 } 3695 break; 3696 3697 case MACRO_OFFSET: { 3698 if (F.LocalNumMacros != 0) { 3699 Error("duplicate MACRO_OFFSET record in AST file"); 3700 return Failure; 3701 } 3702 F.MacroOffsets = (const uint32_t *)Blob.data(); 3703 F.LocalNumMacros = Record[0]; 3704 unsigned LocalBaseMacroID = Record[1]; 3705 F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset; 3706 F.BaseMacroID = getTotalNumMacros(); 3707 3708 if (F.LocalNumMacros > 0) { 3709 // Introduce the global -> local mapping for macros within this module. 3710 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F)); 3711 3712 // Introduce the local -> global mapping for macros within this module. 3713 F.MacroRemap.insertOrReplace( 3714 std::make_pair(LocalBaseMacroID, 3715 F.BaseMacroID - LocalBaseMacroID)); 3716 3717 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros); 3718 } 3719 break; 3720 } 3721 3722 case LATE_PARSED_TEMPLATE: 3723 LateParsedTemplates.append(Record.begin(), Record.end()); 3724 break; 3725 3726 case OPTIMIZE_PRAGMA_OPTIONS: 3727 if (Record.size() != 1) { 3728 Error("invalid pragma optimize record"); 3729 return Failure; 3730 } 3731 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]); 3732 break; 3733 3734 case MSSTRUCT_PRAGMA_OPTIONS: 3735 if (Record.size() != 1) { 3736 Error("invalid pragma ms_struct record"); 3737 return Failure; 3738 } 3739 PragmaMSStructState = Record[0]; 3740 break; 3741 3742 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS: 3743 if (Record.size() != 2) { 3744 Error("invalid pragma ms_struct record"); 3745 return Failure; 3746 } 3747 PragmaMSPointersToMembersState = Record[0]; 3748 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]); 3749 break; 3750 3751 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES: 3752 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3753 UnusedLocalTypedefNameCandidates.push_back( 3754 getGlobalDeclID(F, Record[I])); 3755 break; 3756 3757 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH: 3758 if (Record.size() != 1) { 3759 Error("invalid cuda pragma options record"); 3760 return Failure; 3761 } 3762 ForceCUDAHostDeviceDepth = Record[0]; 3763 break; 3764 3765 case PACK_PRAGMA_OPTIONS: { 3766 if (Record.size() < 3) { 3767 Error("invalid pragma pack record"); 3768 return Failure; 3769 } 3770 PragmaPackCurrentValue = Record[0]; 3771 PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]); 3772 unsigned NumStackEntries = Record[2]; 3773 unsigned Idx = 3; 3774 // Reset the stack when importing a new module. 3775 PragmaPackStack.clear(); 3776 for (unsigned I = 0; I < NumStackEntries; ++I) { 3777 PragmaPackStackEntry Entry; 3778 Entry.Value = Record[Idx++]; 3779 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3780 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3781 PragmaPackStrings.push_back(ReadString(Record, Idx)); 3782 Entry.SlotLabel = PragmaPackStrings.back(); 3783 PragmaPackStack.push_back(Entry); 3784 } 3785 break; 3786 } 3787 3788 case FLOAT_CONTROL_PRAGMA_OPTIONS: { 3789 if (Record.size() < 3) { 3790 Error("invalid pragma pack record"); 3791 return Failure; 3792 } 3793 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]); 3794 FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]); 3795 unsigned NumStackEntries = Record[2]; 3796 unsigned Idx = 3; 3797 // Reset the stack when importing a new module. 3798 FpPragmaStack.clear(); 3799 for (unsigned I = 0; I < NumStackEntries; ++I) { 3800 FpPragmaStackEntry Entry; 3801 Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]); 3802 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3803 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3804 FpPragmaStrings.push_back(ReadString(Record, Idx)); 3805 Entry.SlotLabel = FpPragmaStrings.back(); 3806 FpPragmaStack.push_back(Entry); 3807 } 3808 break; 3809 } 3810 3811 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS: 3812 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3813 DeclsToCheckForDeferredDiags.push_back(getGlobalDeclID(F, Record[I])); 3814 break; 3815 } 3816 } 3817 } 3818 3819 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const { 3820 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read"); 3821 3822 // Additional remapping information. 3823 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data(); 3824 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size(); 3825 F.ModuleOffsetMap = StringRef(); 3826 3827 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders. 3828 if (F.SLocRemap.find(0) == F.SLocRemap.end()) { 3829 F.SLocRemap.insert(std::make_pair(0U, 0)); 3830 F.SLocRemap.insert(std::make_pair(2U, 1)); 3831 } 3832 3833 // Continuous range maps we may be updating in our module. 3834 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder; 3835 RemapBuilder SLocRemap(F.SLocRemap); 3836 RemapBuilder IdentifierRemap(F.IdentifierRemap); 3837 RemapBuilder MacroRemap(F.MacroRemap); 3838 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap); 3839 RemapBuilder SubmoduleRemap(F.SubmoduleRemap); 3840 RemapBuilder SelectorRemap(F.SelectorRemap); 3841 RemapBuilder DeclRemap(F.DeclRemap); 3842 RemapBuilder TypeRemap(F.TypeRemap); 3843 3844 while (Data < DataEnd) { 3845 // FIXME: Looking up dependency modules by filename is horrible. Let's 3846 // start fixing this with prebuilt, explicit and implicit modules and see 3847 // how it goes... 3848 using namespace llvm::support; 3849 ModuleKind Kind = static_cast<ModuleKind>( 3850 endian::readNext<uint8_t, little, unaligned>(Data)); 3851 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data); 3852 StringRef Name = StringRef((const char*)Data, Len); 3853 Data += Len; 3854 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule || 3855 Kind == MK_ImplicitModule 3856 ? ModuleMgr.lookupByModuleName(Name) 3857 : ModuleMgr.lookupByFileName(Name)); 3858 if (!OM) { 3859 std::string Msg = 3860 "SourceLocation remap refers to unknown module, cannot find "; 3861 Msg.append(std::string(Name)); 3862 Error(Msg); 3863 return; 3864 } 3865 3866 uint32_t SLocOffset = 3867 endian::readNext<uint32_t, little, unaligned>(Data); 3868 uint32_t IdentifierIDOffset = 3869 endian::readNext<uint32_t, little, unaligned>(Data); 3870 uint32_t MacroIDOffset = 3871 endian::readNext<uint32_t, little, unaligned>(Data); 3872 uint32_t PreprocessedEntityIDOffset = 3873 endian::readNext<uint32_t, little, unaligned>(Data); 3874 uint32_t SubmoduleIDOffset = 3875 endian::readNext<uint32_t, little, unaligned>(Data); 3876 uint32_t SelectorIDOffset = 3877 endian::readNext<uint32_t, little, unaligned>(Data); 3878 uint32_t DeclIDOffset = 3879 endian::readNext<uint32_t, little, unaligned>(Data); 3880 uint32_t TypeIndexOffset = 3881 endian::readNext<uint32_t, little, unaligned>(Data); 3882 3883 uint32_t None = std::numeric_limits<uint32_t>::max(); 3884 3885 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset, 3886 RemapBuilder &Remap) { 3887 if (Offset != None) 3888 Remap.insert(std::make_pair(Offset, 3889 static_cast<int>(BaseOffset - Offset))); 3890 }; 3891 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap); 3892 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap); 3893 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap); 3894 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID, 3895 PreprocessedEntityRemap); 3896 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap); 3897 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap); 3898 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap); 3899 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap); 3900 3901 // Global -> local mappings. 3902 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset; 3903 } 3904 } 3905 3906 ASTReader::ASTReadResult 3907 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F, 3908 const ModuleFile *ImportedBy, 3909 unsigned ClientLoadCapabilities) { 3910 unsigned Idx = 0; 3911 F.ModuleMapPath = ReadPath(F, Record, Idx); 3912 3913 // Try to resolve ModuleName in the current header search context and 3914 // verify that it is found in the same module map file as we saved. If the 3915 // top-level AST file is a main file, skip this check because there is no 3916 // usable header search context. 3917 assert(!F.ModuleName.empty() && 3918 "MODULE_NAME should come before MODULE_MAP_FILE"); 3919 if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) { 3920 // An implicitly-loaded module file should have its module listed in some 3921 // module map file that we've already loaded. 3922 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName); 3923 auto &Map = PP.getHeaderSearchInfo().getModuleMap(); 3924 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr; 3925 // Don't emit module relocation error if we have -fno-validate-pch 3926 if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) { 3927 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) { 3928 if (auto *ASTFE = M ? M->getASTFile() : nullptr) { 3929 // This module was defined by an imported (explicit) module. 3930 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName 3931 << ASTFE->getName(); 3932 } else { 3933 // This module was built with a different module map. 3934 Diag(diag::err_imported_module_not_found) 3935 << F.ModuleName << F.FileName 3936 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath 3937 << !ImportedBy; 3938 // In case it was imported by a PCH, there's a chance the user is 3939 // just missing to include the search path to the directory containing 3940 // the modulemap. 3941 if (ImportedBy && ImportedBy->Kind == MK_PCH) 3942 Diag(diag::note_imported_by_pch_module_not_found) 3943 << llvm::sys::path::parent_path(F.ModuleMapPath); 3944 } 3945 } 3946 return OutOfDate; 3947 } 3948 3949 assert(M->Name == F.ModuleName && "found module with different name"); 3950 3951 // Check the primary module map file. 3952 auto StoredModMap = FileMgr.getFile(F.ModuleMapPath); 3953 if (!StoredModMap || *StoredModMap != ModMap) { 3954 assert(ModMap && "found module is missing module map file"); 3955 assert((ImportedBy || F.Kind == MK_ImplicitModule) && 3956 "top-level import should be verified"); 3957 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy; 3958 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3959 Diag(diag::err_imported_module_modmap_changed) 3960 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName) 3961 << ModMap->getName() << F.ModuleMapPath << NotImported; 3962 return OutOfDate; 3963 } 3964 3965 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps; 3966 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) { 3967 // FIXME: we should use input files rather than storing names. 3968 std::string Filename = ReadPath(F, Record, Idx); 3969 auto F = FileMgr.getFile(Filename, false, false); 3970 if (!F) { 3971 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3972 Error("could not find file '" + Filename +"' referenced by AST file"); 3973 return OutOfDate; 3974 } 3975 AdditionalStoredMaps.insert(*F); 3976 } 3977 3978 // Check any additional module map files (e.g. module.private.modulemap) 3979 // that are not in the pcm. 3980 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) { 3981 for (const FileEntry *ModMap : *AdditionalModuleMaps) { 3982 // Remove files that match 3983 // Note: SmallPtrSet::erase is really remove 3984 if (!AdditionalStoredMaps.erase(ModMap)) { 3985 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3986 Diag(diag::err_module_different_modmap) 3987 << F.ModuleName << /*new*/0 << ModMap->getName(); 3988 return OutOfDate; 3989 } 3990 } 3991 } 3992 3993 // Check any additional module map files that are in the pcm, but not 3994 // found in header search. Cases that match are already removed. 3995 for (const FileEntry *ModMap : AdditionalStoredMaps) { 3996 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3997 Diag(diag::err_module_different_modmap) 3998 << F.ModuleName << /*not new*/1 << ModMap->getName(); 3999 return OutOfDate; 4000 } 4001 } 4002 4003 if (Listener) 4004 Listener->ReadModuleMapFile(F.ModuleMapPath); 4005 return Success; 4006 } 4007 4008 /// Move the given method to the back of the global list of methods. 4009 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) { 4010 // Find the entry for this selector in the method pool. 4011 Sema::GlobalMethodPool::iterator Known 4012 = S.MethodPool.find(Method->getSelector()); 4013 if (Known == S.MethodPool.end()) 4014 return; 4015 4016 // Retrieve the appropriate method list. 4017 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first 4018 : Known->second.second; 4019 bool Found = false; 4020 for (ObjCMethodList *List = &Start; List; List = List->getNext()) { 4021 if (!Found) { 4022 if (List->getMethod() == Method) { 4023 Found = true; 4024 } else { 4025 // Keep searching. 4026 continue; 4027 } 4028 } 4029 4030 if (List->getNext()) 4031 List->setMethod(List->getNext()->getMethod()); 4032 else 4033 List->setMethod(Method); 4034 } 4035 } 4036 4037 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) { 4038 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?"); 4039 for (Decl *D : Names) { 4040 bool wasHidden = !D->isUnconditionallyVisible(); 4041 D->setVisibleDespiteOwningModule(); 4042 4043 if (wasHidden && SemaObj) { 4044 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) { 4045 moveMethodToBackOfGlobalList(*SemaObj, Method); 4046 } 4047 } 4048 } 4049 } 4050 4051 void ASTReader::makeModuleVisible(Module *Mod, 4052 Module::NameVisibilityKind NameVisibility, 4053 SourceLocation ImportLoc) { 4054 llvm::SmallPtrSet<Module *, 4> Visited; 4055 SmallVector<Module *, 4> Stack; 4056 Stack.push_back(Mod); 4057 while (!Stack.empty()) { 4058 Mod = Stack.pop_back_val(); 4059 4060 if (NameVisibility <= Mod->NameVisibility) { 4061 // This module already has this level of visibility (or greater), so 4062 // there is nothing more to do. 4063 continue; 4064 } 4065 4066 if (Mod->isUnimportable()) { 4067 // Modules that aren't importable cannot be made visible. 4068 continue; 4069 } 4070 4071 // Update the module's name visibility. 4072 Mod->NameVisibility = NameVisibility; 4073 4074 // If we've already deserialized any names from this module, 4075 // mark them as visible. 4076 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod); 4077 if (Hidden != HiddenNamesMap.end()) { 4078 auto HiddenNames = std::move(*Hidden); 4079 HiddenNamesMap.erase(Hidden); 4080 makeNamesVisible(HiddenNames.second, HiddenNames.first); 4081 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() && 4082 "making names visible added hidden names"); 4083 } 4084 4085 // Push any exported modules onto the stack to be marked as visible. 4086 SmallVector<Module *, 16> Exports; 4087 Mod->getExportedModules(Exports); 4088 for (SmallVectorImpl<Module *>::iterator 4089 I = Exports.begin(), E = Exports.end(); I != E; ++I) { 4090 Module *Exported = *I; 4091 if (Visited.insert(Exported).second) 4092 Stack.push_back(Exported); 4093 } 4094 } 4095 } 4096 4097 /// We've merged the definition \p MergedDef into the existing definition 4098 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made 4099 /// visible. 4100 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def, 4101 NamedDecl *MergedDef) { 4102 if (!Def->isUnconditionallyVisible()) { 4103 // If MergedDef is visible or becomes visible, make the definition visible. 4104 if (MergedDef->isUnconditionallyVisible()) 4105 Def->setVisibleDespiteOwningModule(); 4106 else { 4107 getContext().mergeDefinitionIntoModule( 4108 Def, MergedDef->getImportedOwningModule(), 4109 /*NotifyListeners*/ false); 4110 PendingMergedDefinitionsToDeduplicate.insert(Def); 4111 } 4112 } 4113 } 4114 4115 bool ASTReader::loadGlobalIndex() { 4116 if (GlobalIndex) 4117 return false; 4118 4119 if (TriedLoadingGlobalIndex || !UseGlobalIndex || 4120 !PP.getLangOpts().Modules) 4121 return true; 4122 4123 // Try to load the global index. 4124 TriedLoadingGlobalIndex = true; 4125 StringRef ModuleCachePath 4126 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath(); 4127 std::pair<GlobalModuleIndex *, llvm::Error> Result = 4128 GlobalModuleIndex::readIndex(ModuleCachePath); 4129 if (llvm::Error Err = std::move(Result.second)) { 4130 assert(!Result.first); 4131 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 4132 return true; 4133 } 4134 4135 GlobalIndex.reset(Result.first); 4136 ModuleMgr.setGlobalIndex(GlobalIndex.get()); 4137 return false; 4138 } 4139 4140 bool ASTReader::isGlobalIndexUnavailable() const { 4141 return PP.getLangOpts().Modules && UseGlobalIndex && 4142 !hasGlobalIndex() && TriedLoadingGlobalIndex; 4143 } 4144 4145 static void updateModuleTimestamp(ModuleFile &MF) { 4146 // Overwrite the timestamp file contents so that file's mtime changes. 4147 std::string TimestampFilename = MF.getTimestampFilename(); 4148 std::error_code EC; 4149 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text); 4150 if (EC) 4151 return; 4152 OS << "Timestamp file\n"; 4153 OS.close(); 4154 OS.clear_error(); // Avoid triggering a fatal error. 4155 } 4156 4157 /// Given a cursor at the start of an AST file, scan ahead and drop the 4158 /// cursor into the start of the given block ID, returning false on success and 4159 /// true on failure. 4160 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) { 4161 while (true) { 4162 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 4163 if (!MaybeEntry) { 4164 // FIXME this drops errors on the floor. 4165 consumeError(MaybeEntry.takeError()); 4166 return true; 4167 } 4168 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4169 4170 switch (Entry.Kind) { 4171 case llvm::BitstreamEntry::Error: 4172 case llvm::BitstreamEntry::EndBlock: 4173 return true; 4174 4175 case llvm::BitstreamEntry::Record: 4176 // Ignore top-level records. 4177 if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID)) 4178 break; 4179 else { 4180 // FIXME this drops errors on the floor. 4181 consumeError(Skipped.takeError()); 4182 return true; 4183 } 4184 4185 case llvm::BitstreamEntry::SubBlock: 4186 if (Entry.ID == BlockID) { 4187 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) { 4188 // FIXME this drops the error on the floor. 4189 consumeError(std::move(Err)); 4190 return true; 4191 } 4192 // Found it! 4193 return false; 4194 } 4195 4196 if (llvm::Error Err = Cursor.SkipBlock()) { 4197 // FIXME this drops the error on the floor. 4198 consumeError(std::move(Err)); 4199 return true; 4200 } 4201 } 4202 } 4203 } 4204 4205 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, 4206 ModuleKind Type, 4207 SourceLocation ImportLoc, 4208 unsigned ClientLoadCapabilities, 4209 SmallVectorImpl<ImportedSubmodule> *Imported) { 4210 llvm::SaveAndRestore<SourceLocation> 4211 SetCurImportLocRAII(CurrentImportLoc, ImportLoc); 4212 4213 // Defer any pending actions until we get to the end of reading the AST file. 4214 Deserializing AnASTFile(this); 4215 4216 // Bump the generation number. 4217 unsigned PreviousGeneration = 0; 4218 if (ContextObj) 4219 PreviousGeneration = incrementGeneration(*ContextObj); 4220 4221 unsigned NumModules = ModuleMgr.size(); 4222 auto removeModulesAndReturn = [&](ASTReadResult ReadResult) { 4223 assert(ReadResult && "expected to return error"); 4224 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, 4225 PP.getLangOpts().Modules 4226 ? &PP.getHeaderSearchInfo().getModuleMap() 4227 : nullptr); 4228 4229 // If we find that any modules are unusable, the global index is going 4230 // to be out-of-date. Just remove it. 4231 GlobalIndex.reset(); 4232 ModuleMgr.setGlobalIndex(nullptr); 4233 return ReadResult; 4234 }; 4235 4236 SmallVector<ImportedModule, 4> Loaded; 4237 switch (ASTReadResult ReadResult = 4238 ReadASTCore(FileName, Type, ImportLoc, 4239 /*ImportedBy=*/nullptr, Loaded, 0, 0, 4240 ASTFileSignature(), ClientLoadCapabilities)) { 4241 case Failure: 4242 case Missing: 4243 case OutOfDate: 4244 case VersionMismatch: 4245 case ConfigurationMismatch: 4246 case HadErrors: 4247 return removeModulesAndReturn(ReadResult); 4248 case Success: 4249 break; 4250 } 4251 4252 // Here comes stuff that we only do once the entire chain is loaded. 4253 4254 // Load the AST blocks of all of the modules that we loaded. We can still 4255 // hit errors parsing the ASTs at this point. 4256 for (ImportedModule &M : Loaded) { 4257 ModuleFile &F = *M.Mod; 4258 4259 // Read the AST block. 4260 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities)) 4261 return removeModulesAndReturn(Result); 4262 4263 // The AST block should always have a definition for the main module. 4264 if (F.isModule() && !F.DidReadTopLevelSubmodule) { 4265 Error(diag::err_module_file_missing_top_level_submodule, F.FileName); 4266 return removeModulesAndReturn(Failure); 4267 } 4268 4269 // Read the extension blocks. 4270 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) { 4271 if (ASTReadResult Result = ReadExtensionBlock(F)) 4272 return removeModulesAndReturn(Result); 4273 } 4274 4275 // Once read, set the ModuleFile bit base offset and update the size in 4276 // bits of all files we've seen. 4277 F.GlobalBitOffset = TotalModulesSizeInBits; 4278 TotalModulesSizeInBits += F.SizeInBits; 4279 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F)); 4280 } 4281 4282 // Preload source locations and interesting indentifiers. 4283 for (ImportedModule &M : Loaded) { 4284 ModuleFile &F = *M.Mod; 4285 4286 // Preload SLocEntries. 4287 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) { 4288 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID; 4289 // Load it through the SourceManager and don't call ReadSLocEntry() 4290 // directly because the entry may have already been loaded in which case 4291 // calling ReadSLocEntry() directly would trigger an assertion in 4292 // SourceManager. 4293 SourceMgr.getLoadedSLocEntryByID(Index); 4294 } 4295 4296 // Map the original source file ID into the ID space of the current 4297 // compilation. 4298 if (F.OriginalSourceFileID.isValid()) { 4299 F.OriginalSourceFileID = FileID::get( 4300 F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1); 4301 } 4302 4303 // Preload all the pending interesting identifiers by marking them out of 4304 // date. 4305 for (auto Offset : F.PreloadIdentifierOffsets) { 4306 const unsigned char *Data = reinterpret_cast<const unsigned char *>( 4307 F.IdentifierTableData + Offset); 4308 4309 ASTIdentifierLookupTrait Trait(*this, F); 4310 auto KeyDataLen = Trait.ReadKeyDataLength(Data); 4311 auto Key = Trait.ReadKey(Data, KeyDataLen.first); 4312 auto &II = PP.getIdentifierTable().getOwn(Key); 4313 II.setOutOfDate(true); 4314 4315 // Mark this identifier as being from an AST file so that we can track 4316 // whether we need to serialize it. 4317 markIdentifierFromAST(*this, II); 4318 4319 // Associate the ID with the identifier so that the writer can reuse it. 4320 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first); 4321 SetIdentifierInfo(ID, &II); 4322 } 4323 } 4324 4325 // Setup the import locations and notify the module manager that we've 4326 // committed to these module files. 4327 for (ImportedModule &M : Loaded) { 4328 ModuleFile &F = *M.Mod; 4329 4330 ModuleMgr.moduleFileAccepted(&F); 4331 4332 // Set the import location. 4333 F.DirectImportLoc = ImportLoc; 4334 // FIXME: We assume that locations from PCH / preamble do not need 4335 // any translation. 4336 if (!M.ImportedBy) 4337 F.ImportLoc = M.ImportLoc; 4338 else 4339 F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc); 4340 } 4341 4342 if (!PP.getLangOpts().CPlusPlus || 4343 (Type != MK_ImplicitModule && Type != MK_ExplicitModule && 4344 Type != MK_PrebuiltModule)) { 4345 // Mark all of the identifiers in the identifier table as being out of date, 4346 // so that various accessors know to check the loaded modules when the 4347 // identifier is used. 4348 // 4349 // For C++ modules, we don't need information on many identifiers (just 4350 // those that provide macros or are poisoned), so we mark all of 4351 // the interesting ones via PreloadIdentifierOffsets. 4352 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(), 4353 IdEnd = PP.getIdentifierTable().end(); 4354 Id != IdEnd; ++Id) 4355 Id->second->setOutOfDate(true); 4356 } 4357 // Mark selectors as out of date. 4358 for (auto Sel : SelectorGeneration) 4359 SelectorOutOfDate[Sel.first] = true; 4360 4361 // Resolve any unresolved module exports. 4362 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) { 4363 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I]; 4364 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID); 4365 Module *ResolvedMod = getSubmodule(GlobalID); 4366 4367 switch (Unresolved.Kind) { 4368 case UnresolvedModuleRef::Conflict: 4369 if (ResolvedMod) { 4370 Module::Conflict Conflict; 4371 Conflict.Other = ResolvedMod; 4372 Conflict.Message = Unresolved.String.str(); 4373 Unresolved.Mod->Conflicts.push_back(Conflict); 4374 } 4375 continue; 4376 4377 case UnresolvedModuleRef::Import: 4378 if (ResolvedMod) 4379 Unresolved.Mod->Imports.insert(ResolvedMod); 4380 continue; 4381 4382 case UnresolvedModuleRef::Export: 4383 if (ResolvedMod || Unresolved.IsWildcard) 4384 Unresolved.Mod->Exports.push_back( 4385 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard)); 4386 continue; 4387 } 4388 } 4389 UnresolvedModuleRefs.clear(); 4390 4391 if (Imported) 4392 Imported->append(ImportedModules.begin(), 4393 ImportedModules.end()); 4394 4395 // FIXME: How do we load the 'use'd modules? They may not be submodules. 4396 // Might be unnecessary as use declarations are only used to build the 4397 // module itself. 4398 4399 if (ContextObj) 4400 InitializeContext(); 4401 4402 if (SemaObj) 4403 UpdateSema(); 4404 4405 if (DeserializationListener) 4406 DeserializationListener->ReaderInitialized(this); 4407 4408 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule(); 4409 if (PrimaryModule.OriginalSourceFileID.isValid()) { 4410 // If this AST file is a precompiled preamble, then set the 4411 // preamble file ID of the source manager to the file source file 4412 // from which the preamble was built. 4413 if (Type == MK_Preamble) { 4414 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID); 4415 } else if (Type == MK_MainFile) { 4416 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID); 4417 } 4418 } 4419 4420 // For any Objective-C class definitions we have already loaded, make sure 4421 // that we load any additional categories. 4422 if (ContextObj) { 4423 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) { 4424 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(), 4425 ObjCClassesLoaded[I], 4426 PreviousGeneration); 4427 } 4428 } 4429 4430 if (PP.getHeaderSearchInfo() 4431 .getHeaderSearchOpts() 4432 .ModulesValidateOncePerBuildSession) { 4433 // Now we are certain that the module and all modules it depends on are 4434 // up to date. Create or update timestamp files for modules that are 4435 // located in the module cache (not for PCH files that could be anywhere 4436 // in the filesystem). 4437 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) { 4438 ImportedModule &M = Loaded[I]; 4439 if (M.Mod->Kind == MK_ImplicitModule) { 4440 updateModuleTimestamp(*M.Mod); 4441 } 4442 } 4443 } 4444 4445 return Success; 4446 } 4447 4448 static ASTFileSignature readASTFileSignature(StringRef PCH); 4449 4450 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'. 4451 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) { 4452 // FIXME checking magic headers is done in other places such as 4453 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't 4454 // always done the same. Unify it all with a helper. 4455 if (!Stream.canSkipToPos(4)) 4456 return llvm::createStringError(std::errc::illegal_byte_sequence, 4457 "file too small to contain AST file magic"); 4458 for (unsigned C : {'C', 'P', 'C', 'H'}) 4459 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) { 4460 if (Res.get() != C) 4461 return llvm::createStringError( 4462 std::errc::illegal_byte_sequence, 4463 "file doesn't start with AST file magic"); 4464 } else 4465 return Res.takeError(); 4466 return llvm::Error::success(); 4467 } 4468 4469 static unsigned moduleKindForDiagnostic(ModuleKind Kind) { 4470 switch (Kind) { 4471 case MK_PCH: 4472 return 0; // PCH 4473 case MK_ImplicitModule: 4474 case MK_ExplicitModule: 4475 case MK_PrebuiltModule: 4476 return 1; // module 4477 case MK_MainFile: 4478 case MK_Preamble: 4479 return 2; // main source file 4480 } 4481 llvm_unreachable("unknown module kind"); 4482 } 4483 4484 ASTReader::ASTReadResult 4485 ASTReader::ReadASTCore(StringRef FileName, 4486 ModuleKind Type, 4487 SourceLocation ImportLoc, 4488 ModuleFile *ImportedBy, 4489 SmallVectorImpl<ImportedModule> &Loaded, 4490 off_t ExpectedSize, time_t ExpectedModTime, 4491 ASTFileSignature ExpectedSignature, 4492 unsigned ClientLoadCapabilities) { 4493 ModuleFile *M; 4494 std::string ErrorStr; 4495 ModuleManager::AddModuleResult AddResult 4496 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy, 4497 getGeneration(), ExpectedSize, ExpectedModTime, 4498 ExpectedSignature, readASTFileSignature, 4499 M, ErrorStr); 4500 4501 switch (AddResult) { 4502 case ModuleManager::AlreadyLoaded: 4503 Diag(diag::remark_module_import) 4504 << M->ModuleName << M->FileName << (ImportedBy ? true : false) 4505 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 4506 return Success; 4507 4508 case ModuleManager::NewlyLoaded: 4509 // Load module file below. 4510 break; 4511 4512 case ModuleManager::Missing: 4513 // The module file was missing; if the client can handle that, return 4514 // it. 4515 if (ClientLoadCapabilities & ARR_Missing) 4516 return Missing; 4517 4518 // Otherwise, return an error. 4519 Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type) 4520 << FileName << !ErrorStr.empty() 4521 << ErrorStr; 4522 return Failure; 4523 4524 case ModuleManager::OutOfDate: 4525 // We couldn't load the module file because it is out-of-date. If the 4526 // client can handle out-of-date, return it. 4527 if (ClientLoadCapabilities & ARR_OutOfDate) 4528 return OutOfDate; 4529 4530 // Otherwise, return an error. 4531 Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type) 4532 << FileName << !ErrorStr.empty() 4533 << ErrorStr; 4534 return Failure; 4535 } 4536 4537 assert(M && "Missing module file"); 4538 4539 bool ShouldFinalizePCM = false; 4540 auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() { 4541 auto &MC = getModuleManager().getModuleCache(); 4542 if (ShouldFinalizePCM) 4543 MC.finalizePCM(FileName); 4544 else 4545 MC.tryToDropPCM(FileName); 4546 }); 4547 ModuleFile &F = *M; 4548 BitstreamCursor &Stream = F.Stream; 4549 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer)); 4550 F.SizeInBits = F.Buffer->getBufferSize() * 8; 4551 4552 // Sniff for the signature. 4553 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4554 Diag(diag::err_module_file_invalid) 4555 << moduleKindForDiagnostic(Type) << FileName << std::move(Err); 4556 return Failure; 4557 } 4558 4559 // This is used for compatibility with older PCH formats. 4560 bool HaveReadControlBlock = false; 4561 while (true) { 4562 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4563 if (!MaybeEntry) { 4564 Error(MaybeEntry.takeError()); 4565 return Failure; 4566 } 4567 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4568 4569 switch (Entry.Kind) { 4570 case llvm::BitstreamEntry::Error: 4571 case llvm::BitstreamEntry::Record: 4572 case llvm::BitstreamEntry::EndBlock: 4573 Error("invalid record at top-level of AST file"); 4574 return Failure; 4575 4576 case llvm::BitstreamEntry::SubBlock: 4577 break; 4578 } 4579 4580 switch (Entry.ID) { 4581 case CONTROL_BLOCK_ID: 4582 HaveReadControlBlock = true; 4583 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) { 4584 case Success: 4585 // Check that we didn't try to load a non-module AST file as a module. 4586 // 4587 // FIXME: Should we also perform the converse check? Loading a module as 4588 // a PCH file sort of works, but it's a bit wonky. 4589 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule || 4590 Type == MK_PrebuiltModule) && 4591 F.ModuleName.empty()) { 4592 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure; 4593 if (Result != OutOfDate || 4594 (ClientLoadCapabilities & ARR_OutOfDate) == 0) 4595 Diag(diag::err_module_file_not_module) << FileName; 4596 return Result; 4597 } 4598 break; 4599 4600 case Failure: return Failure; 4601 case Missing: return Missing; 4602 case OutOfDate: return OutOfDate; 4603 case VersionMismatch: return VersionMismatch; 4604 case ConfigurationMismatch: return ConfigurationMismatch; 4605 case HadErrors: return HadErrors; 4606 } 4607 break; 4608 4609 case AST_BLOCK_ID: 4610 if (!HaveReadControlBlock) { 4611 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 4612 Diag(diag::err_pch_version_too_old); 4613 return VersionMismatch; 4614 } 4615 4616 // Record that we've loaded this module. 4617 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc)); 4618 ShouldFinalizePCM = true; 4619 return Success; 4620 4621 case UNHASHED_CONTROL_BLOCK_ID: 4622 // This block is handled using look-ahead during ReadControlBlock. We 4623 // shouldn't get here! 4624 Error("malformed block record in AST file"); 4625 return Failure; 4626 4627 default: 4628 if (llvm::Error Err = Stream.SkipBlock()) { 4629 Error(std::move(Err)); 4630 return Failure; 4631 } 4632 break; 4633 } 4634 } 4635 4636 llvm_unreachable("unexpected break; expected return"); 4637 } 4638 4639 ASTReader::ASTReadResult 4640 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy, 4641 unsigned ClientLoadCapabilities) { 4642 const HeaderSearchOptions &HSOpts = 4643 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 4644 bool AllowCompatibleConfigurationMismatch = 4645 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 4646 4647 ASTReadResult Result = readUnhashedControlBlockImpl( 4648 &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch, 4649 Listener.get(), 4650 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions); 4651 4652 // If F was directly imported by another module, it's implicitly validated by 4653 // the importing module. 4654 if (DisableValidation || WasImportedBy || 4655 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 4656 return Success; 4657 4658 if (Result == Failure) { 4659 Error("malformed block record in AST file"); 4660 return Failure; 4661 } 4662 4663 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) { 4664 // If this module has already been finalized in the ModuleCache, we're stuck 4665 // with it; we can only load a single version of each module. 4666 // 4667 // This can happen when a module is imported in two contexts: in one, as a 4668 // user module; in another, as a system module (due to an import from 4669 // another module marked with the [system] flag). It usually indicates a 4670 // bug in the module map: this module should also be marked with [system]. 4671 // 4672 // If -Wno-system-headers (the default), and the first import is as a 4673 // system module, then validation will fail during the as-user import, 4674 // since -Werror flags won't have been validated. However, it's reasonable 4675 // to treat this consistently as a system module. 4676 // 4677 // If -Wsystem-headers, the PCM on disk was built with 4678 // -Wno-system-headers, and the first import is as a user module, then 4679 // validation will fail during the as-system import since the PCM on disk 4680 // doesn't guarantee that -Werror was respected. However, the -Werror 4681 // flags were checked during the initial as-user import. 4682 if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) { 4683 Diag(diag::warn_module_system_bit_conflict) << F.FileName; 4684 return Success; 4685 } 4686 } 4687 4688 return Result; 4689 } 4690 4691 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl( 4692 ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities, 4693 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener, 4694 bool ValidateDiagnosticOptions) { 4695 // Initialize a stream. 4696 BitstreamCursor Stream(StreamData); 4697 4698 // Sniff for the signature. 4699 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4700 // FIXME this drops the error on the floor. 4701 consumeError(std::move(Err)); 4702 return Failure; 4703 } 4704 4705 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 4706 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 4707 return Failure; 4708 4709 // Read all of the records in the options block. 4710 RecordData Record; 4711 ASTReadResult Result = Success; 4712 while (true) { 4713 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4714 if (!MaybeEntry) { 4715 // FIXME this drops the error on the floor. 4716 consumeError(MaybeEntry.takeError()); 4717 return Failure; 4718 } 4719 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4720 4721 switch (Entry.Kind) { 4722 case llvm::BitstreamEntry::Error: 4723 case llvm::BitstreamEntry::SubBlock: 4724 return Failure; 4725 4726 case llvm::BitstreamEntry::EndBlock: 4727 return Result; 4728 4729 case llvm::BitstreamEntry::Record: 4730 // The interesting case. 4731 break; 4732 } 4733 4734 // Read and process a record. 4735 Record.clear(); 4736 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record); 4737 if (!MaybeRecordType) { 4738 // FIXME this drops the error. 4739 return Failure; 4740 } 4741 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) { 4742 case SIGNATURE: 4743 if (F) 4744 F->Signature = ASTFileSignature::create(Record.begin(), Record.end()); 4745 break; 4746 case AST_BLOCK_HASH: 4747 if (F) 4748 F->ASTBlockHash = 4749 ASTFileSignature::create(Record.begin(), Record.end()); 4750 break; 4751 case DIAGNOSTIC_OPTIONS: { 4752 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 4753 if (Listener && ValidateDiagnosticOptions && 4754 !AllowCompatibleConfigurationMismatch && 4755 ParseDiagnosticOptions(Record, Complain, *Listener)) 4756 Result = OutOfDate; // Don't return early. Read the signature. 4757 break; 4758 } 4759 case DIAG_PRAGMA_MAPPINGS: 4760 if (!F) 4761 break; 4762 if (F->PragmaDiagMappings.empty()) 4763 F->PragmaDiagMappings.swap(Record); 4764 else 4765 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(), 4766 Record.begin(), Record.end()); 4767 break; 4768 } 4769 } 4770 } 4771 4772 /// Parse a record and blob containing module file extension metadata. 4773 static bool parseModuleFileExtensionMetadata( 4774 const SmallVectorImpl<uint64_t> &Record, 4775 StringRef Blob, 4776 ModuleFileExtensionMetadata &Metadata) { 4777 if (Record.size() < 4) return true; 4778 4779 Metadata.MajorVersion = Record[0]; 4780 Metadata.MinorVersion = Record[1]; 4781 4782 unsigned BlockNameLen = Record[2]; 4783 unsigned UserInfoLen = Record[3]; 4784 4785 if (BlockNameLen + UserInfoLen > Blob.size()) return true; 4786 4787 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen); 4788 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen, 4789 Blob.data() + BlockNameLen + UserInfoLen); 4790 return false; 4791 } 4792 4793 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) { 4794 BitstreamCursor &Stream = F.Stream; 4795 4796 RecordData Record; 4797 while (true) { 4798 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4799 if (!MaybeEntry) { 4800 Error(MaybeEntry.takeError()); 4801 return Failure; 4802 } 4803 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4804 4805 switch (Entry.Kind) { 4806 case llvm::BitstreamEntry::SubBlock: 4807 if (llvm::Error Err = Stream.SkipBlock()) { 4808 Error(std::move(Err)); 4809 return Failure; 4810 } 4811 continue; 4812 4813 case llvm::BitstreamEntry::EndBlock: 4814 return Success; 4815 4816 case llvm::BitstreamEntry::Error: 4817 return HadErrors; 4818 4819 case llvm::BitstreamEntry::Record: 4820 break; 4821 } 4822 4823 Record.clear(); 4824 StringRef Blob; 4825 Expected<unsigned> MaybeRecCode = 4826 Stream.readRecord(Entry.ID, Record, &Blob); 4827 if (!MaybeRecCode) { 4828 Error(MaybeRecCode.takeError()); 4829 return Failure; 4830 } 4831 switch (MaybeRecCode.get()) { 4832 case EXTENSION_METADATA: { 4833 ModuleFileExtensionMetadata Metadata; 4834 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) { 4835 Error("malformed EXTENSION_METADATA in AST file"); 4836 return Failure; 4837 } 4838 4839 // Find a module file extension with this block name. 4840 auto Known = ModuleFileExtensions.find(Metadata.BlockName); 4841 if (Known == ModuleFileExtensions.end()) break; 4842 4843 // Form a reader. 4844 if (auto Reader = Known->second->createExtensionReader(Metadata, *this, 4845 F, Stream)) { 4846 F.ExtensionReaders.push_back(std::move(Reader)); 4847 } 4848 4849 break; 4850 } 4851 } 4852 } 4853 4854 return Success; 4855 } 4856 4857 void ASTReader::InitializeContext() { 4858 assert(ContextObj && "no context to initialize"); 4859 ASTContext &Context = *ContextObj; 4860 4861 // If there's a listener, notify them that we "read" the translation unit. 4862 if (DeserializationListener) 4863 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, 4864 Context.getTranslationUnitDecl()); 4865 4866 // FIXME: Find a better way to deal with collisions between these 4867 // built-in types. Right now, we just ignore the problem. 4868 4869 // Load the special types. 4870 if (SpecialTypes.size() >= NumSpecialTypeIDs) { 4871 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) { 4872 if (!Context.CFConstantStringTypeDecl) 4873 Context.setCFConstantStringType(GetType(String)); 4874 } 4875 4876 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) { 4877 QualType FileType = GetType(File); 4878 if (FileType.isNull()) { 4879 Error("FILE type is NULL"); 4880 return; 4881 } 4882 4883 if (!Context.FILEDecl) { 4884 if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) 4885 Context.setFILEDecl(Typedef->getDecl()); 4886 else { 4887 const TagType *Tag = FileType->getAs<TagType>(); 4888 if (!Tag) { 4889 Error("Invalid FILE type in AST file"); 4890 return; 4891 } 4892 Context.setFILEDecl(Tag->getDecl()); 4893 } 4894 } 4895 } 4896 4897 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) { 4898 QualType Jmp_bufType = GetType(Jmp_buf); 4899 if (Jmp_bufType.isNull()) { 4900 Error("jmp_buf type is NULL"); 4901 return; 4902 } 4903 4904 if (!Context.jmp_bufDecl) { 4905 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) 4906 Context.setjmp_bufDecl(Typedef->getDecl()); 4907 else { 4908 const TagType *Tag = Jmp_bufType->getAs<TagType>(); 4909 if (!Tag) { 4910 Error("Invalid jmp_buf type in AST file"); 4911 return; 4912 } 4913 Context.setjmp_bufDecl(Tag->getDecl()); 4914 } 4915 } 4916 } 4917 4918 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) { 4919 QualType Sigjmp_bufType = GetType(Sigjmp_buf); 4920 if (Sigjmp_bufType.isNull()) { 4921 Error("sigjmp_buf type is NULL"); 4922 return; 4923 } 4924 4925 if (!Context.sigjmp_bufDecl) { 4926 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) 4927 Context.setsigjmp_bufDecl(Typedef->getDecl()); 4928 else { 4929 const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); 4930 assert(Tag && "Invalid sigjmp_buf type in AST file"); 4931 Context.setsigjmp_bufDecl(Tag->getDecl()); 4932 } 4933 } 4934 } 4935 4936 if (unsigned ObjCIdRedef 4937 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) { 4938 if (Context.ObjCIdRedefinitionType.isNull()) 4939 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef); 4940 } 4941 4942 if (unsigned ObjCClassRedef 4943 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) { 4944 if (Context.ObjCClassRedefinitionType.isNull()) 4945 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef); 4946 } 4947 4948 if (unsigned ObjCSelRedef 4949 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) { 4950 if (Context.ObjCSelRedefinitionType.isNull()) 4951 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef); 4952 } 4953 4954 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) { 4955 QualType Ucontext_tType = GetType(Ucontext_t); 4956 if (Ucontext_tType.isNull()) { 4957 Error("ucontext_t type is NULL"); 4958 return; 4959 } 4960 4961 if (!Context.ucontext_tDecl) { 4962 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>()) 4963 Context.setucontext_tDecl(Typedef->getDecl()); 4964 else { 4965 const TagType *Tag = Ucontext_tType->getAs<TagType>(); 4966 assert(Tag && "Invalid ucontext_t type in AST file"); 4967 Context.setucontext_tDecl(Tag->getDecl()); 4968 } 4969 } 4970 } 4971 } 4972 4973 ReadPragmaDiagnosticMappings(Context.getDiagnostics()); 4974 4975 // If there were any CUDA special declarations, deserialize them. 4976 if (!CUDASpecialDeclRefs.empty()) { 4977 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!"); 4978 Context.setcudaConfigureCallDecl( 4979 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0]))); 4980 } 4981 4982 // Re-export any modules that were imported by a non-module AST file. 4983 // FIXME: This does not make macro-only imports visible again. 4984 for (auto &Import : ImportedModules) { 4985 if (Module *Imported = getSubmodule(Import.ID)) { 4986 makeModuleVisible(Imported, Module::AllVisible, 4987 /*ImportLoc=*/Import.ImportLoc); 4988 if (Import.ImportLoc.isValid()) 4989 PP.makeModuleVisible(Imported, Import.ImportLoc); 4990 // This updates visibility for Preprocessor only. For Sema, which can be 4991 // nullptr here, we do the same later, in UpdateSema(). 4992 } 4993 } 4994 } 4995 4996 void ASTReader::finalizeForWriting() { 4997 // Nothing to do for now. 4998 } 4999 5000 /// Reads and return the signature record from \p PCH's control block, or 5001 /// else returns 0. 5002 static ASTFileSignature readASTFileSignature(StringRef PCH) { 5003 BitstreamCursor Stream(PCH); 5004 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5005 // FIXME this drops the error on the floor. 5006 consumeError(std::move(Err)); 5007 return ASTFileSignature(); 5008 } 5009 5010 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 5011 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 5012 return ASTFileSignature(); 5013 5014 // Scan for SIGNATURE inside the diagnostic options block. 5015 ASTReader::RecordData Record; 5016 while (true) { 5017 Expected<llvm::BitstreamEntry> MaybeEntry = 5018 Stream.advanceSkippingSubblocks(); 5019 if (!MaybeEntry) { 5020 // FIXME this drops the error on the floor. 5021 consumeError(MaybeEntry.takeError()); 5022 return ASTFileSignature(); 5023 } 5024 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5025 5026 if (Entry.Kind != llvm::BitstreamEntry::Record) 5027 return ASTFileSignature(); 5028 5029 Record.clear(); 5030 StringRef Blob; 5031 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 5032 if (!MaybeRecord) { 5033 // FIXME this drops the error on the floor. 5034 consumeError(MaybeRecord.takeError()); 5035 return ASTFileSignature(); 5036 } 5037 if (SIGNATURE == MaybeRecord.get()) 5038 return ASTFileSignature::create(Record.begin(), 5039 Record.begin() + ASTFileSignature::size); 5040 } 5041 } 5042 5043 /// Retrieve the name of the original source file name 5044 /// directly from the AST file, without actually loading the AST 5045 /// file. 5046 std::string ASTReader::getOriginalSourceFile( 5047 const std::string &ASTFileName, FileManager &FileMgr, 5048 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) { 5049 // Open the AST file. 5050 auto Buffer = FileMgr.getBufferForFile(ASTFileName); 5051 if (!Buffer) { 5052 Diags.Report(diag::err_fe_unable_to_read_pch_file) 5053 << ASTFileName << Buffer.getError().message(); 5054 return std::string(); 5055 } 5056 5057 // Initialize the stream 5058 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer)); 5059 5060 // Sniff for the signature. 5061 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5062 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err); 5063 return std::string(); 5064 } 5065 5066 // Scan for the CONTROL_BLOCK_ID block. 5067 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) { 5068 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 5069 return std::string(); 5070 } 5071 5072 // Scan for ORIGINAL_FILE inside the control block. 5073 RecordData Record; 5074 while (true) { 5075 Expected<llvm::BitstreamEntry> MaybeEntry = 5076 Stream.advanceSkippingSubblocks(); 5077 if (!MaybeEntry) { 5078 // FIXME this drops errors on the floor. 5079 consumeError(MaybeEntry.takeError()); 5080 return std::string(); 5081 } 5082 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5083 5084 if (Entry.Kind == llvm::BitstreamEntry::EndBlock) 5085 return std::string(); 5086 5087 if (Entry.Kind != llvm::BitstreamEntry::Record) { 5088 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 5089 return std::string(); 5090 } 5091 5092 Record.clear(); 5093 StringRef Blob; 5094 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 5095 if (!MaybeRecord) { 5096 // FIXME this drops the errors on the floor. 5097 consumeError(MaybeRecord.takeError()); 5098 return std::string(); 5099 } 5100 if (ORIGINAL_FILE == MaybeRecord.get()) 5101 return Blob.str(); 5102 } 5103 } 5104 5105 namespace { 5106 5107 class SimplePCHValidator : public ASTReaderListener { 5108 const LangOptions &ExistingLangOpts; 5109 const TargetOptions &ExistingTargetOpts; 5110 const PreprocessorOptions &ExistingPPOpts; 5111 std::string ExistingModuleCachePath; 5112 FileManager &FileMgr; 5113 5114 public: 5115 SimplePCHValidator(const LangOptions &ExistingLangOpts, 5116 const TargetOptions &ExistingTargetOpts, 5117 const PreprocessorOptions &ExistingPPOpts, 5118 StringRef ExistingModuleCachePath, FileManager &FileMgr) 5119 : ExistingLangOpts(ExistingLangOpts), 5120 ExistingTargetOpts(ExistingTargetOpts), 5121 ExistingPPOpts(ExistingPPOpts), 5122 ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {} 5123 5124 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain, 5125 bool AllowCompatibleDifferences) override { 5126 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr, 5127 AllowCompatibleDifferences); 5128 } 5129 5130 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain, 5131 bool AllowCompatibleDifferences) override { 5132 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr, 5133 AllowCompatibleDifferences); 5134 } 5135 5136 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 5137 StringRef SpecificModuleCachePath, 5138 bool Complain) override { 5139 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5140 ExistingModuleCachePath, 5141 nullptr, ExistingLangOpts); 5142 } 5143 5144 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 5145 bool Complain, 5146 std::string &SuggestedPredefines) override { 5147 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr, 5148 SuggestedPredefines, ExistingLangOpts); 5149 } 5150 }; 5151 5152 } // namespace 5153 5154 bool ASTReader::readASTFileControlBlock( 5155 StringRef Filename, FileManager &FileMgr, 5156 const PCHContainerReader &PCHContainerRdr, 5157 bool FindModuleFileExtensions, 5158 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) { 5159 // Open the AST file. 5160 // FIXME: This allows use of the VFS; we do not allow use of the 5161 // VFS when actually loading a module. 5162 auto Buffer = FileMgr.getBufferForFile(Filename); 5163 if (!Buffer) { 5164 return true; 5165 } 5166 5167 // Initialize the stream 5168 StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer); 5169 BitstreamCursor Stream(Bytes); 5170 5171 // Sniff for the signature. 5172 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5173 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 5174 return true; 5175 } 5176 5177 // Scan for the CONTROL_BLOCK_ID block. 5178 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) 5179 return true; 5180 5181 bool NeedsInputFiles = Listener.needsInputFileVisitation(); 5182 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation(); 5183 bool NeedsImports = Listener.needsImportVisitation(); 5184 BitstreamCursor InputFilesCursor; 5185 5186 RecordData Record; 5187 std::string ModuleDir; 5188 bool DoneWithControlBlock = false; 5189 while (!DoneWithControlBlock) { 5190 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5191 if (!MaybeEntry) { 5192 // FIXME this drops the error on the floor. 5193 consumeError(MaybeEntry.takeError()); 5194 return true; 5195 } 5196 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5197 5198 switch (Entry.Kind) { 5199 case llvm::BitstreamEntry::SubBlock: { 5200 switch (Entry.ID) { 5201 case OPTIONS_BLOCK_ID: { 5202 std::string IgnoredSuggestedPredefines; 5203 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate, 5204 /*AllowCompatibleConfigurationMismatch*/ false, 5205 Listener, IgnoredSuggestedPredefines) != Success) 5206 return true; 5207 break; 5208 } 5209 5210 case INPUT_FILES_BLOCK_ID: 5211 InputFilesCursor = Stream; 5212 if (llvm::Error Err = Stream.SkipBlock()) { 5213 // FIXME this drops the error on the floor. 5214 consumeError(std::move(Err)); 5215 return true; 5216 } 5217 if (NeedsInputFiles && 5218 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID)) 5219 return true; 5220 break; 5221 5222 default: 5223 if (llvm::Error Err = Stream.SkipBlock()) { 5224 // FIXME this drops the error on the floor. 5225 consumeError(std::move(Err)); 5226 return true; 5227 } 5228 break; 5229 } 5230 5231 continue; 5232 } 5233 5234 case llvm::BitstreamEntry::EndBlock: 5235 DoneWithControlBlock = true; 5236 break; 5237 5238 case llvm::BitstreamEntry::Error: 5239 return true; 5240 5241 case llvm::BitstreamEntry::Record: 5242 break; 5243 } 5244 5245 if (DoneWithControlBlock) break; 5246 5247 Record.clear(); 5248 StringRef Blob; 5249 Expected<unsigned> MaybeRecCode = 5250 Stream.readRecord(Entry.ID, Record, &Blob); 5251 if (!MaybeRecCode) { 5252 // FIXME this drops the error. 5253 return Failure; 5254 } 5255 switch ((ControlRecordTypes)MaybeRecCode.get()) { 5256 case METADATA: 5257 if (Record[0] != VERSION_MAJOR) 5258 return true; 5259 if (Listener.ReadFullVersionInformation(Blob)) 5260 return true; 5261 break; 5262 case MODULE_NAME: 5263 Listener.ReadModuleName(Blob); 5264 break; 5265 case MODULE_DIRECTORY: 5266 ModuleDir = std::string(Blob); 5267 break; 5268 case MODULE_MAP_FILE: { 5269 unsigned Idx = 0; 5270 auto Path = ReadString(Record, Idx); 5271 ResolveImportedPath(Path, ModuleDir); 5272 Listener.ReadModuleMapFile(Path); 5273 break; 5274 } 5275 case INPUT_FILE_OFFSETS: { 5276 if (!NeedsInputFiles) 5277 break; 5278 5279 unsigned NumInputFiles = Record[0]; 5280 unsigned NumUserFiles = Record[1]; 5281 const llvm::support::unaligned_uint64_t *InputFileOffs = 5282 (const llvm::support::unaligned_uint64_t *)Blob.data(); 5283 for (unsigned I = 0; I != NumInputFiles; ++I) { 5284 // Go find this input file. 5285 bool isSystemFile = I >= NumUserFiles; 5286 5287 if (isSystemFile && !NeedsSystemInputFiles) 5288 break; // the rest are system input files 5289 5290 BitstreamCursor &Cursor = InputFilesCursor; 5291 SavedStreamPosition SavedPosition(Cursor); 5292 if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) { 5293 // FIXME this drops errors on the floor. 5294 consumeError(std::move(Err)); 5295 } 5296 5297 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 5298 if (!MaybeCode) { 5299 // FIXME this drops errors on the floor. 5300 consumeError(MaybeCode.takeError()); 5301 } 5302 unsigned Code = MaybeCode.get(); 5303 5304 RecordData Record; 5305 StringRef Blob; 5306 bool shouldContinue = false; 5307 Expected<unsigned> MaybeRecordType = 5308 Cursor.readRecord(Code, Record, &Blob); 5309 if (!MaybeRecordType) { 5310 // FIXME this drops errors on the floor. 5311 consumeError(MaybeRecordType.takeError()); 5312 } 5313 switch ((InputFileRecordTypes)MaybeRecordType.get()) { 5314 case INPUT_FILE_HASH: 5315 break; 5316 case INPUT_FILE: 5317 bool Overridden = static_cast<bool>(Record[3]); 5318 std::string Filename = std::string(Blob); 5319 ResolveImportedPath(Filename, ModuleDir); 5320 shouldContinue = Listener.visitInputFile( 5321 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false); 5322 break; 5323 } 5324 if (!shouldContinue) 5325 break; 5326 } 5327 break; 5328 } 5329 5330 case IMPORTS: { 5331 if (!NeedsImports) 5332 break; 5333 5334 unsigned Idx = 0, N = Record.size(); 5335 while (Idx < N) { 5336 // Read information about the AST file. 5337 Idx += 5338 1 + 1 + 1 + 1 + 5339 ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature 5340 std::string ModuleName = ReadString(Record, Idx); 5341 std::string Filename = ReadString(Record, Idx); 5342 ResolveImportedPath(Filename, ModuleDir); 5343 Listener.visitImport(ModuleName, Filename); 5344 } 5345 break; 5346 } 5347 5348 default: 5349 // No other validation to perform. 5350 break; 5351 } 5352 } 5353 5354 // Look for module file extension blocks, if requested. 5355 if (FindModuleFileExtensions) { 5356 BitstreamCursor SavedStream = Stream; 5357 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) { 5358 bool DoneWithExtensionBlock = false; 5359 while (!DoneWithExtensionBlock) { 5360 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5361 if (!MaybeEntry) { 5362 // FIXME this drops the error. 5363 return true; 5364 } 5365 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5366 5367 switch (Entry.Kind) { 5368 case llvm::BitstreamEntry::SubBlock: 5369 if (llvm::Error Err = Stream.SkipBlock()) { 5370 // FIXME this drops the error on the floor. 5371 consumeError(std::move(Err)); 5372 return true; 5373 } 5374 continue; 5375 5376 case llvm::BitstreamEntry::EndBlock: 5377 DoneWithExtensionBlock = true; 5378 continue; 5379 5380 case llvm::BitstreamEntry::Error: 5381 return true; 5382 5383 case llvm::BitstreamEntry::Record: 5384 break; 5385 } 5386 5387 Record.clear(); 5388 StringRef Blob; 5389 Expected<unsigned> MaybeRecCode = 5390 Stream.readRecord(Entry.ID, Record, &Blob); 5391 if (!MaybeRecCode) { 5392 // FIXME this drops the error. 5393 return true; 5394 } 5395 switch (MaybeRecCode.get()) { 5396 case EXTENSION_METADATA: { 5397 ModuleFileExtensionMetadata Metadata; 5398 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) 5399 return true; 5400 5401 Listener.readModuleFileExtension(Metadata); 5402 break; 5403 } 5404 } 5405 } 5406 } 5407 Stream = SavedStream; 5408 } 5409 5410 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 5411 if (readUnhashedControlBlockImpl( 5412 nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate, 5413 /*AllowCompatibleConfigurationMismatch*/ false, &Listener, 5414 ValidateDiagnosticOptions) != Success) 5415 return true; 5416 5417 return false; 5418 } 5419 5420 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr, 5421 const PCHContainerReader &PCHContainerRdr, 5422 const LangOptions &LangOpts, 5423 const TargetOptions &TargetOpts, 5424 const PreprocessorOptions &PPOpts, 5425 StringRef ExistingModuleCachePath) { 5426 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, 5427 ExistingModuleCachePath, FileMgr); 5428 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr, 5429 /*FindModuleFileExtensions=*/false, 5430 validator, 5431 /*ValidateDiagnosticOptions=*/true); 5432 } 5433 5434 ASTReader::ASTReadResult 5435 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) { 5436 // Enter the submodule block. 5437 if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) { 5438 Error(std::move(Err)); 5439 return Failure; 5440 } 5441 5442 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap(); 5443 bool First = true; 5444 Module *CurrentModule = nullptr; 5445 RecordData Record; 5446 while (true) { 5447 Expected<llvm::BitstreamEntry> MaybeEntry = 5448 F.Stream.advanceSkippingSubblocks(); 5449 if (!MaybeEntry) { 5450 Error(MaybeEntry.takeError()); 5451 return Failure; 5452 } 5453 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5454 5455 switch (Entry.Kind) { 5456 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 5457 case llvm::BitstreamEntry::Error: 5458 Error("malformed block record in AST file"); 5459 return Failure; 5460 case llvm::BitstreamEntry::EndBlock: 5461 return Success; 5462 case llvm::BitstreamEntry::Record: 5463 // The interesting case. 5464 break; 5465 } 5466 5467 // Read a record. 5468 StringRef Blob; 5469 Record.clear(); 5470 Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob); 5471 if (!MaybeKind) { 5472 Error(MaybeKind.takeError()); 5473 return Failure; 5474 } 5475 unsigned Kind = MaybeKind.get(); 5476 5477 if ((Kind == SUBMODULE_METADATA) != First) { 5478 Error("submodule metadata record should be at beginning of block"); 5479 return Failure; 5480 } 5481 First = false; 5482 5483 // Submodule information is only valid if we have a current module. 5484 // FIXME: Should we error on these cases? 5485 if (!CurrentModule && Kind != SUBMODULE_METADATA && 5486 Kind != SUBMODULE_DEFINITION) 5487 continue; 5488 5489 switch (Kind) { 5490 default: // Default behavior: ignore. 5491 break; 5492 5493 case SUBMODULE_DEFINITION: { 5494 if (Record.size() < 12) { 5495 Error("malformed module definition"); 5496 return Failure; 5497 } 5498 5499 StringRef Name = Blob; 5500 unsigned Idx = 0; 5501 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]); 5502 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]); 5503 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++]; 5504 bool IsFramework = Record[Idx++]; 5505 bool IsExplicit = Record[Idx++]; 5506 bool IsSystem = Record[Idx++]; 5507 bool IsExternC = Record[Idx++]; 5508 bool InferSubmodules = Record[Idx++]; 5509 bool InferExplicitSubmodules = Record[Idx++]; 5510 bool InferExportWildcard = Record[Idx++]; 5511 bool ConfigMacrosExhaustive = Record[Idx++]; 5512 bool ModuleMapIsPrivate = Record[Idx++]; 5513 5514 Module *ParentModule = nullptr; 5515 if (Parent) 5516 ParentModule = getSubmodule(Parent); 5517 5518 // Retrieve this (sub)module from the module map, creating it if 5519 // necessary. 5520 CurrentModule = 5521 ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit) 5522 .first; 5523 5524 // FIXME: set the definition loc for CurrentModule, or call 5525 // ModMap.setInferredModuleAllowedBy() 5526 5527 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS; 5528 if (GlobalIndex >= SubmodulesLoaded.size() || 5529 SubmodulesLoaded[GlobalIndex]) { 5530 Error("too many submodules"); 5531 return Failure; 5532 } 5533 5534 if (!ParentModule) { 5535 if (const FileEntry *CurFile = CurrentModule->getASTFile()) { 5536 // Don't emit module relocation error if we have -fno-validate-pch 5537 if (!PP.getPreprocessorOpts().DisablePCHValidation && 5538 CurFile != F.File) { 5539 Error(diag::err_module_file_conflict, 5540 CurrentModule->getTopLevelModuleName(), CurFile->getName(), 5541 F.File->getName()); 5542 return Failure; 5543 } 5544 } 5545 5546 F.DidReadTopLevelSubmodule = true; 5547 CurrentModule->setASTFile(F.File); 5548 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath; 5549 } 5550 5551 CurrentModule->Kind = Kind; 5552 CurrentModule->Signature = F.Signature; 5553 CurrentModule->IsFromModuleFile = true; 5554 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem; 5555 CurrentModule->IsExternC = IsExternC; 5556 CurrentModule->InferSubmodules = InferSubmodules; 5557 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules; 5558 CurrentModule->InferExportWildcard = InferExportWildcard; 5559 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive; 5560 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate; 5561 if (DeserializationListener) 5562 DeserializationListener->ModuleRead(GlobalID, CurrentModule); 5563 5564 SubmodulesLoaded[GlobalIndex] = CurrentModule; 5565 5566 // Clear out data that will be replaced by what is in the module file. 5567 CurrentModule->LinkLibraries.clear(); 5568 CurrentModule->ConfigMacros.clear(); 5569 CurrentModule->UnresolvedConflicts.clear(); 5570 CurrentModule->Conflicts.clear(); 5571 5572 // The module is available unless it's missing a requirement; relevant 5573 // requirements will be (re-)added by SUBMODULE_REQUIRES records. 5574 // Missing headers that were present when the module was built do not 5575 // make it unavailable -- if we got this far, this must be an explicitly 5576 // imported module file. 5577 CurrentModule->Requirements.clear(); 5578 CurrentModule->MissingHeaders.clear(); 5579 CurrentModule->IsUnimportable = 5580 ParentModule && ParentModule->IsUnimportable; 5581 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable; 5582 break; 5583 } 5584 5585 case SUBMODULE_UMBRELLA_HEADER: { 5586 std::string Filename = std::string(Blob); 5587 ResolveImportedPath(F, Filename); 5588 if (auto Umbrella = PP.getFileManager().getFile(Filename)) { 5589 if (!CurrentModule->getUmbrellaHeader()) 5590 ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob); 5591 else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) { 5592 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 5593 Error("mismatched umbrella headers in submodule"); 5594 return OutOfDate; 5595 } 5596 } 5597 break; 5598 } 5599 5600 case SUBMODULE_HEADER: 5601 case SUBMODULE_EXCLUDED_HEADER: 5602 case SUBMODULE_PRIVATE_HEADER: 5603 // We lazily associate headers with their modules via the HeaderInfo table. 5604 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead 5605 // of complete filenames or remove it entirely. 5606 break; 5607 5608 case SUBMODULE_TEXTUAL_HEADER: 5609 case SUBMODULE_PRIVATE_TEXTUAL_HEADER: 5610 // FIXME: Textual headers are not marked in the HeaderInfo table. Load 5611 // them here. 5612 break; 5613 5614 case SUBMODULE_TOPHEADER: 5615 CurrentModule->addTopHeaderFilename(Blob); 5616 break; 5617 5618 case SUBMODULE_UMBRELLA_DIR: { 5619 std::string Dirname = std::string(Blob); 5620 ResolveImportedPath(F, Dirname); 5621 if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) { 5622 if (!CurrentModule->getUmbrellaDir()) 5623 ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob); 5624 else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) { 5625 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 5626 Error("mismatched umbrella directories in submodule"); 5627 return OutOfDate; 5628 } 5629 } 5630 break; 5631 } 5632 5633 case SUBMODULE_METADATA: { 5634 F.BaseSubmoduleID = getTotalNumSubmodules(); 5635 F.LocalNumSubmodules = Record[0]; 5636 unsigned LocalBaseSubmoduleID = Record[1]; 5637 if (F.LocalNumSubmodules > 0) { 5638 // Introduce the global -> local mapping for submodules within this 5639 // module. 5640 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F)); 5641 5642 // Introduce the local -> global mapping for submodules within this 5643 // module. 5644 F.SubmoduleRemap.insertOrReplace( 5645 std::make_pair(LocalBaseSubmoduleID, 5646 F.BaseSubmoduleID - LocalBaseSubmoduleID)); 5647 5648 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules); 5649 } 5650 break; 5651 } 5652 5653 case SUBMODULE_IMPORTS: 5654 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) { 5655 UnresolvedModuleRef Unresolved; 5656 Unresolved.File = &F; 5657 Unresolved.Mod = CurrentModule; 5658 Unresolved.ID = Record[Idx]; 5659 Unresolved.Kind = UnresolvedModuleRef::Import; 5660 Unresolved.IsWildcard = false; 5661 UnresolvedModuleRefs.push_back(Unresolved); 5662 } 5663 break; 5664 5665 case SUBMODULE_EXPORTS: 5666 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) { 5667 UnresolvedModuleRef Unresolved; 5668 Unresolved.File = &F; 5669 Unresolved.Mod = CurrentModule; 5670 Unresolved.ID = Record[Idx]; 5671 Unresolved.Kind = UnresolvedModuleRef::Export; 5672 Unresolved.IsWildcard = Record[Idx + 1]; 5673 UnresolvedModuleRefs.push_back(Unresolved); 5674 } 5675 5676 // Once we've loaded the set of exports, there's no reason to keep 5677 // the parsed, unresolved exports around. 5678 CurrentModule->UnresolvedExports.clear(); 5679 break; 5680 5681 case SUBMODULE_REQUIRES: 5682 CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(), 5683 PP.getTargetInfo()); 5684 break; 5685 5686 case SUBMODULE_LINK_LIBRARY: 5687 ModMap.resolveLinkAsDependencies(CurrentModule); 5688 CurrentModule->LinkLibraries.push_back( 5689 Module::LinkLibrary(std::string(Blob), Record[0])); 5690 break; 5691 5692 case SUBMODULE_CONFIG_MACRO: 5693 CurrentModule->ConfigMacros.push_back(Blob.str()); 5694 break; 5695 5696 case SUBMODULE_CONFLICT: { 5697 UnresolvedModuleRef Unresolved; 5698 Unresolved.File = &F; 5699 Unresolved.Mod = CurrentModule; 5700 Unresolved.ID = Record[0]; 5701 Unresolved.Kind = UnresolvedModuleRef::Conflict; 5702 Unresolved.IsWildcard = false; 5703 Unresolved.String = Blob; 5704 UnresolvedModuleRefs.push_back(Unresolved); 5705 break; 5706 } 5707 5708 case SUBMODULE_INITIALIZERS: { 5709 if (!ContextObj) 5710 break; 5711 SmallVector<uint32_t, 16> Inits; 5712 for (auto &ID : Record) 5713 Inits.push_back(getGlobalDeclID(F, ID)); 5714 ContextObj->addLazyModuleInitializers(CurrentModule, Inits); 5715 break; 5716 } 5717 5718 case SUBMODULE_EXPORT_AS: 5719 CurrentModule->ExportAsModule = Blob.str(); 5720 ModMap.addLinkAsDependency(CurrentModule); 5721 break; 5722 } 5723 } 5724 } 5725 5726 /// Parse the record that corresponds to a LangOptions data 5727 /// structure. 5728 /// 5729 /// This routine parses the language options from the AST file and then gives 5730 /// them to the AST listener if one is set. 5731 /// 5732 /// \returns true if the listener deems the file unacceptable, false otherwise. 5733 bool ASTReader::ParseLanguageOptions(const RecordData &Record, 5734 bool Complain, 5735 ASTReaderListener &Listener, 5736 bool AllowCompatibleDifferences) { 5737 LangOptions LangOpts; 5738 unsigned Idx = 0; 5739 #define LANGOPT(Name, Bits, Default, Description) \ 5740 LangOpts.Name = Record[Idx++]; 5741 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 5742 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++])); 5743 #include "clang/Basic/LangOptions.def" 5744 #define SANITIZER(NAME, ID) \ 5745 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]); 5746 #include "clang/Basic/Sanitizers.def" 5747 5748 for (unsigned N = Record[Idx++]; N; --N) 5749 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx)); 5750 5751 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++]; 5752 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx); 5753 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion); 5754 5755 LangOpts.CurrentModule = ReadString(Record, Idx); 5756 5757 // Comment options. 5758 for (unsigned N = Record[Idx++]; N; --N) { 5759 LangOpts.CommentOpts.BlockCommandNames.push_back( 5760 ReadString(Record, Idx)); 5761 } 5762 LangOpts.CommentOpts.ParseAllComments = Record[Idx++]; 5763 5764 // OpenMP offloading options. 5765 for (unsigned N = Record[Idx++]; N; --N) { 5766 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx))); 5767 } 5768 5769 LangOpts.OMPHostIRFile = ReadString(Record, Idx); 5770 5771 return Listener.ReadLanguageOptions(LangOpts, Complain, 5772 AllowCompatibleDifferences); 5773 } 5774 5775 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain, 5776 ASTReaderListener &Listener, 5777 bool AllowCompatibleDifferences) { 5778 unsigned Idx = 0; 5779 TargetOptions TargetOpts; 5780 TargetOpts.Triple = ReadString(Record, Idx); 5781 TargetOpts.CPU = ReadString(Record, Idx); 5782 TargetOpts.ABI = ReadString(Record, Idx); 5783 for (unsigned N = Record[Idx++]; N; --N) { 5784 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx)); 5785 } 5786 for (unsigned N = Record[Idx++]; N; --N) { 5787 TargetOpts.Features.push_back(ReadString(Record, Idx)); 5788 } 5789 5790 return Listener.ReadTargetOptions(TargetOpts, Complain, 5791 AllowCompatibleDifferences); 5792 } 5793 5794 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain, 5795 ASTReaderListener &Listener) { 5796 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions); 5797 unsigned Idx = 0; 5798 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++]; 5799 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \ 5800 DiagOpts->set##Name(static_cast<Type>(Record[Idx++])); 5801 #include "clang/Basic/DiagnosticOptions.def" 5802 5803 for (unsigned N = Record[Idx++]; N; --N) 5804 DiagOpts->Warnings.push_back(ReadString(Record, Idx)); 5805 for (unsigned N = Record[Idx++]; N; --N) 5806 DiagOpts->Remarks.push_back(ReadString(Record, Idx)); 5807 5808 return Listener.ReadDiagnosticOptions(DiagOpts, Complain); 5809 } 5810 5811 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain, 5812 ASTReaderListener &Listener) { 5813 FileSystemOptions FSOpts; 5814 unsigned Idx = 0; 5815 FSOpts.WorkingDir = ReadString(Record, Idx); 5816 return Listener.ReadFileSystemOptions(FSOpts, Complain); 5817 } 5818 5819 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record, 5820 bool Complain, 5821 ASTReaderListener &Listener) { 5822 HeaderSearchOptions HSOpts; 5823 unsigned Idx = 0; 5824 HSOpts.Sysroot = ReadString(Record, Idx); 5825 5826 // Include entries. 5827 for (unsigned N = Record[Idx++]; N; --N) { 5828 std::string Path = ReadString(Record, Idx); 5829 frontend::IncludeDirGroup Group 5830 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]); 5831 bool IsFramework = Record[Idx++]; 5832 bool IgnoreSysRoot = Record[Idx++]; 5833 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework, 5834 IgnoreSysRoot); 5835 } 5836 5837 // System header prefixes. 5838 for (unsigned N = Record[Idx++]; N; --N) { 5839 std::string Prefix = ReadString(Record, Idx); 5840 bool IsSystemHeader = Record[Idx++]; 5841 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader); 5842 } 5843 5844 HSOpts.ResourceDir = ReadString(Record, Idx); 5845 HSOpts.ModuleCachePath = ReadString(Record, Idx); 5846 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx); 5847 HSOpts.DisableModuleHash = Record[Idx++]; 5848 HSOpts.ImplicitModuleMaps = Record[Idx++]; 5849 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++]; 5850 HSOpts.UseBuiltinIncludes = Record[Idx++]; 5851 HSOpts.UseStandardSystemIncludes = Record[Idx++]; 5852 HSOpts.UseStandardCXXIncludes = Record[Idx++]; 5853 HSOpts.UseLibcxx = Record[Idx++]; 5854 std::string SpecificModuleCachePath = ReadString(Record, Idx); 5855 5856 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5857 Complain); 5858 } 5859 5860 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record, 5861 bool Complain, 5862 ASTReaderListener &Listener, 5863 std::string &SuggestedPredefines) { 5864 PreprocessorOptions PPOpts; 5865 unsigned Idx = 0; 5866 5867 // Macro definitions/undefs 5868 for (unsigned N = Record[Idx++]; N; --N) { 5869 std::string Macro = ReadString(Record, Idx); 5870 bool IsUndef = Record[Idx++]; 5871 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef)); 5872 } 5873 5874 // Includes 5875 for (unsigned N = Record[Idx++]; N; --N) { 5876 PPOpts.Includes.push_back(ReadString(Record, Idx)); 5877 } 5878 5879 // Macro Includes 5880 for (unsigned N = Record[Idx++]; N; --N) { 5881 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx)); 5882 } 5883 5884 PPOpts.UsePredefines = Record[Idx++]; 5885 PPOpts.DetailedRecord = Record[Idx++]; 5886 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx); 5887 PPOpts.ObjCXXARCStandardLibrary = 5888 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]); 5889 SuggestedPredefines.clear(); 5890 return Listener.ReadPreprocessorOptions(PPOpts, Complain, 5891 SuggestedPredefines); 5892 } 5893 5894 std::pair<ModuleFile *, unsigned> 5895 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) { 5896 GlobalPreprocessedEntityMapType::iterator 5897 I = GlobalPreprocessedEntityMap.find(GlobalIndex); 5898 assert(I != GlobalPreprocessedEntityMap.end() && 5899 "Corrupted global preprocessed entity map"); 5900 ModuleFile *M = I->second; 5901 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID; 5902 return std::make_pair(M, LocalIndex); 5903 } 5904 5905 llvm::iterator_range<PreprocessingRecord::iterator> 5906 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const { 5907 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord()) 5908 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID, 5909 Mod.NumPreprocessedEntities); 5910 5911 return llvm::make_range(PreprocessingRecord::iterator(), 5912 PreprocessingRecord::iterator()); 5913 } 5914 5915 llvm::iterator_range<ASTReader::ModuleDeclIterator> 5916 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) { 5917 return llvm::make_range( 5918 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls), 5919 ModuleDeclIterator(this, &Mod, 5920 Mod.FileSortedDecls + Mod.NumFileSortedDecls)); 5921 } 5922 5923 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) { 5924 auto I = GlobalSkippedRangeMap.find(GlobalIndex); 5925 assert(I != GlobalSkippedRangeMap.end() && 5926 "Corrupted global skipped range map"); 5927 ModuleFile *M = I->second; 5928 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID; 5929 assert(LocalIndex < M->NumPreprocessedSkippedRanges); 5930 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex]; 5931 SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()), 5932 TranslateSourceLocation(*M, RawRange.getEnd())); 5933 assert(Range.isValid()); 5934 return Range; 5935 } 5936 5937 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) { 5938 PreprocessedEntityID PPID = Index+1; 5939 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 5940 ModuleFile &M = *PPInfo.first; 5941 unsigned LocalIndex = PPInfo.second; 5942 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 5943 5944 if (!PP.getPreprocessingRecord()) { 5945 Error("no preprocessing record"); 5946 return nullptr; 5947 } 5948 5949 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor); 5950 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit( 5951 M.MacroOffsetsBase + PPOffs.BitOffset)) { 5952 Error(std::move(Err)); 5953 return nullptr; 5954 } 5955 5956 Expected<llvm::BitstreamEntry> MaybeEntry = 5957 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 5958 if (!MaybeEntry) { 5959 Error(MaybeEntry.takeError()); 5960 return nullptr; 5961 } 5962 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5963 5964 if (Entry.Kind != llvm::BitstreamEntry::Record) 5965 return nullptr; 5966 5967 // Read the record. 5968 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()), 5969 TranslateSourceLocation(M, PPOffs.getEnd())); 5970 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 5971 StringRef Blob; 5972 RecordData Record; 5973 Expected<unsigned> MaybeRecType = 5974 M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob); 5975 if (!MaybeRecType) { 5976 Error(MaybeRecType.takeError()); 5977 return nullptr; 5978 } 5979 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) { 5980 case PPD_MACRO_EXPANSION: { 5981 bool isBuiltin = Record[0]; 5982 IdentifierInfo *Name = nullptr; 5983 MacroDefinitionRecord *Def = nullptr; 5984 if (isBuiltin) 5985 Name = getLocalIdentifier(M, Record[1]); 5986 else { 5987 PreprocessedEntityID GlobalID = 5988 getGlobalPreprocessedEntityID(M, Record[1]); 5989 Def = cast<MacroDefinitionRecord>( 5990 PPRec.getLoadedPreprocessedEntity(GlobalID - 1)); 5991 } 5992 5993 MacroExpansion *ME; 5994 if (isBuiltin) 5995 ME = new (PPRec) MacroExpansion(Name, Range); 5996 else 5997 ME = new (PPRec) MacroExpansion(Def, Range); 5998 5999 return ME; 6000 } 6001 6002 case PPD_MACRO_DEFINITION: { 6003 // Decode the identifier info and then check again; if the macro is 6004 // still defined and associated with the identifier, 6005 IdentifierInfo *II = getLocalIdentifier(M, Record[0]); 6006 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range); 6007 6008 if (DeserializationListener) 6009 DeserializationListener->MacroDefinitionRead(PPID, MD); 6010 6011 return MD; 6012 } 6013 6014 case PPD_INCLUSION_DIRECTIVE: { 6015 const char *FullFileNameStart = Blob.data() + Record[0]; 6016 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]); 6017 const FileEntry *File = nullptr; 6018 if (!FullFileName.empty()) 6019 if (auto FE = PP.getFileManager().getFile(FullFileName)) 6020 File = *FE; 6021 6022 // FIXME: Stable encoding 6023 InclusionDirective::InclusionKind Kind 6024 = static_cast<InclusionDirective::InclusionKind>(Record[2]); 6025 InclusionDirective *ID 6026 = new (PPRec) InclusionDirective(PPRec, Kind, 6027 StringRef(Blob.data(), Record[0]), 6028 Record[1], Record[3], 6029 File, 6030 Range); 6031 return ID; 6032 } 6033 } 6034 6035 llvm_unreachable("Invalid PreprocessorDetailRecordTypes"); 6036 } 6037 6038 /// Find the next module that contains entities and return the ID 6039 /// of the first entry. 6040 /// 6041 /// \param SLocMapI points at a chunk of a module that contains no 6042 /// preprocessed entities or the entities it contains are not the ones we are 6043 /// looking for. 6044 PreprocessedEntityID ASTReader::findNextPreprocessedEntity( 6045 GlobalSLocOffsetMapType::const_iterator SLocMapI) const { 6046 ++SLocMapI; 6047 for (GlobalSLocOffsetMapType::const_iterator 6048 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) { 6049 ModuleFile &M = *SLocMapI->second; 6050 if (M.NumPreprocessedEntities) 6051 return M.BasePreprocessedEntityID; 6052 } 6053 6054 return getTotalNumPreprocessedEntities(); 6055 } 6056 6057 namespace { 6058 6059 struct PPEntityComp { 6060 const ASTReader &Reader; 6061 ModuleFile &M; 6062 6063 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {} 6064 6065 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const { 6066 SourceLocation LHS = getLoc(L); 6067 SourceLocation RHS = getLoc(R); 6068 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6069 } 6070 6071 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const { 6072 SourceLocation LHS = getLoc(L); 6073 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6074 } 6075 6076 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const { 6077 SourceLocation RHS = getLoc(R); 6078 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6079 } 6080 6081 SourceLocation getLoc(const PPEntityOffset &PPE) const { 6082 return Reader.TranslateSourceLocation(M, PPE.getBegin()); 6083 } 6084 }; 6085 6086 } // namespace 6087 6088 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc, 6089 bool EndsAfter) const { 6090 if (SourceMgr.isLocalSourceLocation(Loc)) 6091 return getTotalNumPreprocessedEntities(); 6092 6093 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find( 6094 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1); 6095 assert(SLocMapI != GlobalSLocOffsetMap.end() && 6096 "Corrupted global sloc offset map"); 6097 6098 if (SLocMapI->second->NumPreprocessedEntities == 0) 6099 return findNextPreprocessedEntity(SLocMapI); 6100 6101 ModuleFile &M = *SLocMapI->second; 6102 6103 using pp_iterator = const PPEntityOffset *; 6104 6105 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 6106 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 6107 6108 size_t Count = M.NumPreprocessedEntities; 6109 size_t Half; 6110 pp_iterator First = pp_begin; 6111 pp_iterator PPI; 6112 6113 if (EndsAfter) { 6114 PPI = std::upper_bound(pp_begin, pp_end, Loc, 6115 PPEntityComp(*this, M)); 6116 } else { 6117 // Do a binary search manually instead of using std::lower_bound because 6118 // The end locations of entities may be unordered (when a macro expansion 6119 // is inside another macro argument), but for this case it is not important 6120 // whether we get the first macro expansion or its containing macro. 6121 while (Count > 0) { 6122 Half = Count / 2; 6123 PPI = First; 6124 std::advance(PPI, Half); 6125 if (SourceMgr.isBeforeInTranslationUnit( 6126 TranslateSourceLocation(M, PPI->getEnd()), Loc)) { 6127 First = PPI; 6128 ++First; 6129 Count = Count - Half - 1; 6130 } else 6131 Count = Half; 6132 } 6133 } 6134 6135 if (PPI == pp_end) 6136 return findNextPreprocessedEntity(SLocMapI); 6137 6138 return M.BasePreprocessedEntityID + (PPI - pp_begin); 6139 } 6140 6141 /// Returns a pair of [Begin, End) indices of preallocated 6142 /// preprocessed entities that \arg Range encompasses. 6143 std::pair<unsigned, unsigned> 6144 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) { 6145 if (Range.isInvalid()) 6146 return std::make_pair(0,0); 6147 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); 6148 6149 PreprocessedEntityID BeginID = 6150 findPreprocessedEntity(Range.getBegin(), false); 6151 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true); 6152 return std::make_pair(BeginID, EndID); 6153 } 6154 6155 /// Optionally returns true or false if the preallocated preprocessed 6156 /// entity with index \arg Index came from file \arg FID. 6157 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index, 6158 FileID FID) { 6159 if (FID.isInvalid()) 6160 return false; 6161 6162 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 6163 ModuleFile &M = *PPInfo.first; 6164 unsigned LocalIndex = PPInfo.second; 6165 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 6166 6167 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin()); 6168 if (Loc.isInvalid()) 6169 return false; 6170 6171 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID)) 6172 return true; 6173 else 6174 return false; 6175 } 6176 6177 namespace { 6178 6179 /// Visitor used to search for information about a header file. 6180 class HeaderFileInfoVisitor { 6181 const FileEntry *FE; 6182 Optional<HeaderFileInfo> HFI; 6183 6184 public: 6185 explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {} 6186 6187 bool operator()(ModuleFile &M) { 6188 HeaderFileInfoLookupTable *Table 6189 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable); 6190 if (!Table) 6191 return false; 6192 6193 // Look in the on-disk hash table for an entry for this file name. 6194 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE); 6195 if (Pos == Table->end()) 6196 return false; 6197 6198 HFI = *Pos; 6199 return true; 6200 } 6201 6202 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; } 6203 }; 6204 6205 } // namespace 6206 6207 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) { 6208 HeaderFileInfoVisitor Visitor(FE); 6209 ModuleMgr.visit(Visitor); 6210 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) 6211 return *HFI; 6212 6213 return HeaderFileInfo(); 6214 } 6215 6216 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) { 6217 using DiagState = DiagnosticsEngine::DiagState; 6218 SmallVector<DiagState *, 32> DiagStates; 6219 6220 for (ModuleFile &F : ModuleMgr) { 6221 unsigned Idx = 0; 6222 auto &Record = F.PragmaDiagMappings; 6223 if (Record.empty()) 6224 continue; 6225 6226 DiagStates.clear(); 6227 6228 auto ReadDiagState = 6229 [&](const DiagState &BasedOn, SourceLocation Loc, 6230 bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * { 6231 unsigned BackrefID = Record[Idx++]; 6232 if (BackrefID != 0) 6233 return DiagStates[BackrefID - 1]; 6234 6235 // A new DiagState was created here. 6236 Diag.DiagStates.push_back(BasedOn); 6237 DiagState *NewState = &Diag.DiagStates.back(); 6238 DiagStates.push_back(NewState); 6239 unsigned Size = Record[Idx++]; 6240 assert(Idx + Size * 2 <= Record.size() && 6241 "Invalid data, not enough diag/map pairs"); 6242 while (Size--) { 6243 unsigned DiagID = Record[Idx++]; 6244 DiagnosticMapping NewMapping = 6245 DiagnosticMapping::deserialize(Record[Idx++]); 6246 if (!NewMapping.isPragma() && !IncludeNonPragmaStates) 6247 continue; 6248 6249 DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID); 6250 6251 // If this mapping was specified as a warning but the severity was 6252 // upgraded due to diagnostic settings, simulate the current diagnostic 6253 // settings (and use a warning). 6254 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) { 6255 NewMapping.setSeverity(diag::Severity::Warning); 6256 NewMapping.setUpgradedFromWarning(false); 6257 } 6258 6259 Mapping = NewMapping; 6260 } 6261 return NewState; 6262 }; 6263 6264 // Read the first state. 6265 DiagState *FirstState; 6266 if (F.Kind == MK_ImplicitModule) { 6267 // Implicitly-built modules are reused with different diagnostic 6268 // settings. Use the initial diagnostic state from Diag to simulate this 6269 // compilation's diagnostic settings. 6270 FirstState = Diag.DiagStatesByLoc.FirstDiagState; 6271 DiagStates.push_back(FirstState); 6272 6273 // Skip the initial diagnostic state from the serialized module. 6274 assert(Record[1] == 0 && 6275 "Invalid data, unexpected backref in initial state"); 6276 Idx = 3 + Record[2] * 2; 6277 assert(Idx < Record.size() && 6278 "Invalid data, not enough state change pairs in initial state"); 6279 } else if (F.isModule()) { 6280 // For an explicit module, preserve the flags from the module build 6281 // command line (-w, -Weverything, -Werror, ...) along with any explicit 6282 // -Wblah flags. 6283 unsigned Flags = Record[Idx++]; 6284 DiagState Initial; 6285 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1; 6286 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1; 6287 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1; 6288 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1; 6289 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1; 6290 Initial.ExtBehavior = (diag::Severity)Flags; 6291 FirstState = ReadDiagState(Initial, SourceLocation(), true); 6292 6293 assert(F.OriginalSourceFileID.isValid()); 6294 6295 // Set up the root buffer of the module to start with the initial 6296 // diagnostic state of the module itself, to cover files that contain no 6297 // explicit transitions (for which we did not serialize anything). 6298 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID] 6299 .StateTransitions.push_back({FirstState, 0}); 6300 } else { 6301 // For prefix ASTs, start with whatever the user configured on the 6302 // command line. 6303 Idx++; // Skip flags. 6304 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, 6305 SourceLocation(), false); 6306 } 6307 6308 // Read the state transitions. 6309 unsigned NumLocations = Record[Idx++]; 6310 while (NumLocations--) { 6311 assert(Idx < Record.size() && 6312 "Invalid data, missing pragma diagnostic states"); 6313 SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]); 6314 auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc); 6315 assert(IDAndOffset.first.isValid() && "invalid FileID for transition"); 6316 assert(IDAndOffset.second == 0 && "not a start location for a FileID"); 6317 unsigned Transitions = Record[Idx++]; 6318 6319 // Note that we don't need to set up Parent/ParentOffset here, because 6320 // we won't be changing the diagnostic state within imported FileIDs 6321 // (other than perhaps appending to the main source file, which has no 6322 // parent). 6323 auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first]; 6324 F.StateTransitions.reserve(F.StateTransitions.size() + Transitions); 6325 for (unsigned I = 0; I != Transitions; ++I) { 6326 unsigned Offset = Record[Idx++]; 6327 auto *State = 6328 ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false); 6329 F.StateTransitions.push_back({State, Offset}); 6330 } 6331 } 6332 6333 // Read the final state. 6334 assert(Idx < Record.size() && 6335 "Invalid data, missing final pragma diagnostic state"); 6336 SourceLocation CurStateLoc = 6337 ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]); 6338 auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false); 6339 6340 if (!F.isModule()) { 6341 Diag.DiagStatesByLoc.CurDiagState = CurState; 6342 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc; 6343 6344 // Preserve the property that the imaginary root file describes the 6345 // current state. 6346 FileID NullFile; 6347 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions; 6348 if (T.empty()) 6349 T.push_back({CurState, 0}); 6350 else 6351 T[0].State = CurState; 6352 } 6353 6354 // Don't try to read these mappings again. 6355 Record.clear(); 6356 } 6357 } 6358 6359 /// Get the correct cursor and offset for loading a type. 6360 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) { 6361 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index); 6362 assert(I != GlobalTypeMap.end() && "Corrupted global type map"); 6363 ModuleFile *M = I->second; 6364 return RecordLocation( 6365 M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() + 6366 M->DeclsBlockStartOffset); 6367 } 6368 6369 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) { 6370 switch (code) { 6371 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \ 6372 case TYPE_##CODE_ID: return Type::CLASS_ID; 6373 #include "clang/Serialization/TypeBitCodes.def" 6374 default: return llvm::None; 6375 } 6376 } 6377 6378 /// Read and return the type with the given index.. 6379 /// 6380 /// The index is the type ID, shifted and minus the number of predefs. This 6381 /// routine actually reads the record corresponding to the type at the given 6382 /// location. It is a helper routine for GetType, which deals with reading type 6383 /// IDs. 6384 QualType ASTReader::readTypeRecord(unsigned Index) { 6385 assert(ContextObj && "reading type with no AST context"); 6386 ASTContext &Context = *ContextObj; 6387 RecordLocation Loc = TypeCursorForIndex(Index); 6388 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 6389 6390 // Keep track of where we are in the stream, then jump back there 6391 // after reading this type. 6392 SavedStreamPosition SavedPosition(DeclsCursor); 6393 6394 ReadingKindTracker ReadingKind(Read_Type, *this); 6395 6396 // Note that we are loading a type record. 6397 Deserializing AType(this); 6398 6399 if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) { 6400 Error(std::move(Err)); 6401 return QualType(); 6402 } 6403 Expected<unsigned> RawCode = DeclsCursor.ReadCode(); 6404 if (!RawCode) { 6405 Error(RawCode.takeError()); 6406 return QualType(); 6407 } 6408 6409 ASTRecordReader Record(*this, *Loc.F); 6410 Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get()); 6411 if (!Code) { 6412 Error(Code.takeError()); 6413 return QualType(); 6414 } 6415 if (Code.get() == TYPE_EXT_QUAL) { 6416 QualType baseType = Record.readQualType(); 6417 Qualifiers quals = Record.readQualifiers(); 6418 return Context.getQualifiedType(baseType, quals); 6419 } 6420 6421 auto maybeClass = getTypeClassForCode((TypeCode) Code.get()); 6422 if (!maybeClass) { 6423 Error("Unexpected code for type"); 6424 return QualType(); 6425 } 6426 6427 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record); 6428 return TypeReader.read(*maybeClass); 6429 } 6430 6431 namespace clang { 6432 6433 class TypeLocReader : public TypeLocVisitor<TypeLocReader> { 6434 ASTRecordReader &Reader; 6435 6436 SourceLocation readSourceLocation() { 6437 return Reader.readSourceLocation(); 6438 } 6439 6440 TypeSourceInfo *GetTypeSourceInfo() { 6441 return Reader.readTypeSourceInfo(); 6442 } 6443 6444 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() { 6445 return Reader.readNestedNameSpecifierLoc(); 6446 } 6447 6448 Attr *ReadAttr() { 6449 return Reader.readAttr(); 6450 } 6451 6452 public: 6453 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {} 6454 6455 // We want compile-time assurance that we've enumerated all of 6456 // these, so unfortunately we have to declare them first, then 6457 // define them out-of-line. 6458 #define ABSTRACT_TYPELOC(CLASS, PARENT) 6459 #define TYPELOC(CLASS, PARENT) \ 6460 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 6461 #include "clang/AST/TypeLocNodes.def" 6462 6463 void VisitFunctionTypeLoc(FunctionTypeLoc); 6464 void VisitArrayTypeLoc(ArrayTypeLoc); 6465 }; 6466 6467 } // namespace clang 6468 6469 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 6470 // nothing to do 6471 } 6472 6473 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 6474 TL.setBuiltinLoc(readSourceLocation()); 6475 if (TL.needsExtraLocalData()) { 6476 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt())); 6477 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Reader.readInt())); 6478 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Reader.readInt())); 6479 TL.setModeAttr(Reader.readInt()); 6480 } 6481 } 6482 6483 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { 6484 TL.setNameLoc(readSourceLocation()); 6485 } 6486 6487 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { 6488 TL.setStarLoc(readSourceLocation()); 6489 } 6490 6491 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) { 6492 // nothing to do 6493 } 6494 6495 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { 6496 // nothing to do 6497 } 6498 6499 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) { 6500 TL.setExpansionLoc(readSourceLocation()); 6501 } 6502 6503 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 6504 TL.setCaretLoc(readSourceLocation()); 6505 } 6506 6507 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 6508 TL.setAmpLoc(readSourceLocation()); 6509 } 6510 6511 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 6512 TL.setAmpAmpLoc(readSourceLocation()); 6513 } 6514 6515 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 6516 TL.setStarLoc(readSourceLocation()); 6517 TL.setClassTInfo(GetTypeSourceInfo()); 6518 } 6519 6520 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { 6521 TL.setLBracketLoc(readSourceLocation()); 6522 TL.setRBracketLoc(readSourceLocation()); 6523 if (Reader.readBool()) 6524 TL.setSizeExpr(Reader.readExpr()); 6525 else 6526 TL.setSizeExpr(nullptr); 6527 } 6528 6529 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 6530 VisitArrayTypeLoc(TL); 6531 } 6532 6533 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 6534 VisitArrayTypeLoc(TL); 6535 } 6536 6537 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 6538 VisitArrayTypeLoc(TL); 6539 } 6540 6541 void TypeLocReader::VisitDependentSizedArrayTypeLoc( 6542 DependentSizedArrayTypeLoc TL) { 6543 VisitArrayTypeLoc(TL); 6544 } 6545 6546 void TypeLocReader::VisitDependentAddressSpaceTypeLoc( 6547 DependentAddressSpaceTypeLoc TL) { 6548 6549 TL.setAttrNameLoc(readSourceLocation()); 6550 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6551 TL.setAttrExprOperand(Reader.readExpr()); 6552 } 6553 6554 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( 6555 DependentSizedExtVectorTypeLoc TL) { 6556 TL.setNameLoc(readSourceLocation()); 6557 } 6558 6559 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { 6560 TL.setNameLoc(readSourceLocation()); 6561 } 6562 6563 void TypeLocReader::VisitDependentVectorTypeLoc( 6564 DependentVectorTypeLoc TL) { 6565 TL.setNameLoc(readSourceLocation()); 6566 } 6567 6568 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 6569 TL.setNameLoc(readSourceLocation()); 6570 } 6571 6572 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) { 6573 TL.setAttrNameLoc(readSourceLocation()); 6574 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6575 TL.setAttrRowOperand(Reader.readExpr()); 6576 TL.setAttrColumnOperand(Reader.readExpr()); 6577 } 6578 6579 void TypeLocReader::VisitDependentSizedMatrixTypeLoc( 6580 DependentSizedMatrixTypeLoc TL) { 6581 TL.setAttrNameLoc(readSourceLocation()); 6582 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6583 TL.setAttrRowOperand(Reader.readExpr()); 6584 TL.setAttrColumnOperand(Reader.readExpr()); 6585 } 6586 6587 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 6588 TL.setLocalRangeBegin(readSourceLocation()); 6589 TL.setLParenLoc(readSourceLocation()); 6590 TL.setRParenLoc(readSourceLocation()); 6591 TL.setExceptionSpecRange(Reader.readSourceRange()); 6592 TL.setLocalRangeEnd(readSourceLocation()); 6593 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) { 6594 TL.setParam(i, Reader.readDeclAs<ParmVarDecl>()); 6595 } 6596 } 6597 6598 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 6599 VisitFunctionTypeLoc(TL); 6600 } 6601 6602 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 6603 VisitFunctionTypeLoc(TL); 6604 } 6605 6606 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 6607 TL.setNameLoc(readSourceLocation()); 6608 } 6609 6610 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 6611 TL.setNameLoc(readSourceLocation()); 6612 } 6613 6614 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 6615 TL.setTypeofLoc(readSourceLocation()); 6616 TL.setLParenLoc(readSourceLocation()); 6617 TL.setRParenLoc(readSourceLocation()); 6618 } 6619 6620 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 6621 TL.setTypeofLoc(readSourceLocation()); 6622 TL.setLParenLoc(readSourceLocation()); 6623 TL.setRParenLoc(readSourceLocation()); 6624 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6625 } 6626 6627 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 6628 TL.setNameLoc(readSourceLocation()); 6629 } 6630 6631 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 6632 TL.setKWLoc(readSourceLocation()); 6633 TL.setLParenLoc(readSourceLocation()); 6634 TL.setRParenLoc(readSourceLocation()); 6635 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6636 } 6637 6638 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) { 6639 TL.setNameLoc(readSourceLocation()); 6640 if (Reader.readBool()) { 6641 TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc()); 6642 TL.setTemplateKWLoc(readSourceLocation()); 6643 TL.setConceptNameLoc(readSourceLocation()); 6644 TL.setFoundDecl(Reader.readDeclAs<NamedDecl>()); 6645 TL.setLAngleLoc(readSourceLocation()); 6646 TL.setRAngleLoc(readSourceLocation()); 6647 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6648 TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo( 6649 TL.getTypePtr()->getArg(i).getKind())); 6650 } 6651 } 6652 6653 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc( 6654 DeducedTemplateSpecializationTypeLoc TL) { 6655 TL.setTemplateNameLoc(readSourceLocation()); 6656 } 6657 6658 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { 6659 TL.setNameLoc(readSourceLocation()); 6660 } 6661 6662 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { 6663 TL.setNameLoc(readSourceLocation()); 6664 } 6665 6666 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 6667 TL.setAttr(ReadAttr()); 6668 } 6669 6670 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 6671 TL.setNameLoc(readSourceLocation()); 6672 } 6673 6674 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( 6675 SubstTemplateTypeParmTypeLoc TL) { 6676 TL.setNameLoc(readSourceLocation()); 6677 } 6678 6679 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc( 6680 SubstTemplateTypeParmPackTypeLoc TL) { 6681 TL.setNameLoc(readSourceLocation()); 6682 } 6683 6684 void TypeLocReader::VisitTemplateSpecializationTypeLoc( 6685 TemplateSpecializationTypeLoc TL) { 6686 TL.setTemplateKeywordLoc(readSourceLocation()); 6687 TL.setTemplateNameLoc(readSourceLocation()); 6688 TL.setLAngleLoc(readSourceLocation()); 6689 TL.setRAngleLoc(readSourceLocation()); 6690 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6691 TL.setArgLocInfo( 6692 i, 6693 Reader.readTemplateArgumentLocInfo( 6694 TL.getTypePtr()->getArg(i).getKind())); 6695 } 6696 6697 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) { 6698 TL.setLParenLoc(readSourceLocation()); 6699 TL.setRParenLoc(readSourceLocation()); 6700 } 6701 6702 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 6703 TL.setElaboratedKeywordLoc(readSourceLocation()); 6704 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6705 } 6706 6707 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 6708 TL.setNameLoc(readSourceLocation()); 6709 } 6710 6711 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 6712 TL.setElaboratedKeywordLoc(readSourceLocation()); 6713 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6714 TL.setNameLoc(readSourceLocation()); 6715 } 6716 6717 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( 6718 DependentTemplateSpecializationTypeLoc TL) { 6719 TL.setElaboratedKeywordLoc(readSourceLocation()); 6720 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6721 TL.setTemplateKeywordLoc(readSourceLocation()); 6722 TL.setTemplateNameLoc(readSourceLocation()); 6723 TL.setLAngleLoc(readSourceLocation()); 6724 TL.setRAngleLoc(readSourceLocation()); 6725 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 6726 TL.setArgLocInfo( 6727 I, 6728 Reader.readTemplateArgumentLocInfo( 6729 TL.getTypePtr()->getArg(I).getKind())); 6730 } 6731 6732 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 6733 TL.setEllipsisLoc(readSourceLocation()); 6734 } 6735 6736 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 6737 TL.setNameLoc(readSourceLocation()); 6738 } 6739 6740 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) { 6741 if (TL.getNumProtocols()) { 6742 TL.setProtocolLAngleLoc(readSourceLocation()); 6743 TL.setProtocolRAngleLoc(readSourceLocation()); 6744 } 6745 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6746 TL.setProtocolLoc(i, readSourceLocation()); 6747 } 6748 6749 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 6750 TL.setHasBaseTypeAsWritten(Reader.readBool()); 6751 TL.setTypeArgsLAngleLoc(readSourceLocation()); 6752 TL.setTypeArgsRAngleLoc(readSourceLocation()); 6753 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i) 6754 TL.setTypeArgTInfo(i, GetTypeSourceInfo()); 6755 TL.setProtocolLAngleLoc(readSourceLocation()); 6756 TL.setProtocolRAngleLoc(readSourceLocation()); 6757 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6758 TL.setProtocolLoc(i, readSourceLocation()); 6759 } 6760 6761 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 6762 TL.setStarLoc(readSourceLocation()); 6763 } 6764 6765 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) { 6766 TL.setKWLoc(readSourceLocation()); 6767 TL.setLParenLoc(readSourceLocation()); 6768 TL.setRParenLoc(readSourceLocation()); 6769 } 6770 6771 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) { 6772 TL.setKWLoc(readSourceLocation()); 6773 } 6774 6775 void TypeLocReader::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) { 6776 TL.setNameLoc(readSourceLocation()); 6777 } 6778 void TypeLocReader::VisitDependentExtIntTypeLoc( 6779 clang::DependentExtIntTypeLoc TL) { 6780 TL.setNameLoc(readSourceLocation()); 6781 } 6782 6783 6784 void ASTRecordReader::readTypeLoc(TypeLoc TL) { 6785 TypeLocReader TLR(*this); 6786 for (; !TL.isNull(); TL = TL.getNextTypeLoc()) 6787 TLR.Visit(TL); 6788 } 6789 6790 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() { 6791 QualType InfoTy = readType(); 6792 if (InfoTy.isNull()) 6793 return nullptr; 6794 6795 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy); 6796 readTypeLoc(TInfo->getTypeLoc()); 6797 return TInfo; 6798 } 6799 6800 QualType ASTReader::GetType(TypeID ID) { 6801 assert(ContextObj && "reading type with no AST context"); 6802 ASTContext &Context = *ContextObj; 6803 6804 unsigned FastQuals = ID & Qualifiers::FastMask; 6805 unsigned Index = ID >> Qualifiers::FastWidth; 6806 6807 if (Index < NUM_PREDEF_TYPE_IDS) { 6808 QualType T; 6809 switch ((PredefinedTypeIDs)Index) { 6810 case PREDEF_TYPE_NULL_ID: 6811 return QualType(); 6812 case PREDEF_TYPE_VOID_ID: 6813 T = Context.VoidTy; 6814 break; 6815 case PREDEF_TYPE_BOOL_ID: 6816 T = Context.BoolTy; 6817 break; 6818 case PREDEF_TYPE_CHAR_U_ID: 6819 case PREDEF_TYPE_CHAR_S_ID: 6820 // FIXME: Check that the signedness of CharTy is correct! 6821 T = Context.CharTy; 6822 break; 6823 case PREDEF_TYPE_UCHAR_ID: 6824 T = Context.UnsignedCharTy; 6825 break; 6826 case PREDEF_TYPE_USHORT_ID: 6827 T = Context.UnsignedShortTy; 6828 break; 6829 case PREDEF_TYPE_UINT_ID: 6830 T = Context.UnsignedIntTy; 6831 break; 6832 case PREDEF_TYPE_ULONG_ID: 6833 T = Context.UnsignedLongTy; 6834 break; 6835 case PREDEF_TYPE_ULONGLONG_ID: 6836 T = Context.UnsignedLongLongTy; 6837 break; 6838 case PREDEF_TYPE_UINT128_ID: 6839 T = Context.UnsignedInt128Ty; 6840 break; 6841 case PREDEF_TYPE_SCHAR_ID: 6842 T = Context.SignedCharTy; 6843 break; 6844 case PREDEF_TYPE_WCHAR_ID: 6845 T = Context.WCharTy; 6846 break; 6847 case PREDEF_TYPE_SHORT_ID: 6848 T = Context.ShortTy; 6849 break; 6850 case PREDEF_TYPE_INT_ID: 6851 T = Context.IntTy; 6852 break; 6853 case PREDEF_TYPE_LONG_ID: 6854 T = Context.LongTy; 6855 break; 6856 case PREDEF_TYPE_LONGLONG_ID: 6857 T = Context.LongLongTy; 6858 break; 6859 case PREDEF_TYPE_INT128_ID: 6860 T = Context.Int128Ty; 6861 break; 6862 case PREDEF_TYPE_BFLOAT16_ID: 6863 T = Context.BFloat16Ty; 6864 break; 6865 case PREDEF_TYPE_HALF_ID: 6866 T = Context.HalfTy; 6867 break; 6868 case PREDEF_TYPE_FLOAT_ID: 6869 T = Context.FloatTy; 6870 break; 6871 case PREDEF_TYPE_DOUBLE_ID: 6872 T = Context.DoubleTy; 6873 break; 6874 case PREDEF_TYPE_LONGDOUBLE_ID: 6875 T = Context.LongDoubleTy; 6876 break; 6877 case PREDEF_TYPE_SHORT_ACCUM_ID: 6878 T = Context.ShortAccumTy; 6879 break; 6880 case PREDEF_TYPE_ACCUM_ID: 6881 T = Context.AccumTy; 6882 break; 6883 case PREDEF_TYPE_LONG_ACCUM_ID: 6884 T = Context.LongAccumTy; 6885 break; 6886 case PREDEF_TYPE_USHORT_ACCUM_ID: 6887 T = Context.UnsignedShortAccumTy; 6888 break; 6889 case PREDEF_TYPE_UACCUM_ID: 6890 T = Context.UnsignedAccumTy; 6891 break; 6892 case PREDEF_TYPE_ULONG_ACCUM_ID: 6893 T = Context.UnsignedLongAccumTy; 6894 break; 6895 case PREDEF_TYPE_SHORT_FRACT_ID: 6896 T = Context.ShortFractTy; 6897 break; 6898 case PREDEF_TYPE_FRACT_ID: 6899 T = Context.FractTy; 6900 break; 6901 case PREDEF_TYPE_LONG_FRACT_ID: 6902 T = Context.LongFractTy; 6903 break; 6904 case PREDEF_TYPE_USHORT_FRACT_ID: 6905 T = Context.UnsignedShortFractTy; 6906 break; 6907 case PREDEF_TYPE_UFRACT_ID: 6908 T = Context.UnsignedFractTy; 6909 break; 6910 case PREDEF_TYPE_ULONG_FRACT_ID: 6911 T = Context.UnsignedLongFractTy; 6912 break; 6913 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID: 6914 T = Context.SatShortAccumTy; 6915 break; 6916 case PREDEF_TYPE_SAT_ACCUM_ID: 6917 T = Context.SatAccumTy; 6918 break; 6919 case PREDEF_TYPE_SAT_LONG_ACCUM_ID: 6920 T = Context.SatLongAccumTy; 6921 break; 6922 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID: 6923 T = Context.SatUnsignedShortAccumTy; 6924 break; 6925 case PREDEF_TYPE_SAT_UACCUM_ID: 6926 T = Context.SatUnsignedAccumTy; 6927 break; 6928 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID: 6929 T = Context.SatUnsignedLongAccumTy; 6930 break; 6931 case PREDEF_TYPE_SAT_SHORT_FRACT_ID: 6932 T = Context.SatShortFractTy; 6933 break; 6934 case PREDEF_TYPE_SAT_FRACT_ID: 6935 T = Context.SatFractTy; 6936 break; 6937 case PREDEF_TYPE_SAT_LONG_FRACT_ID: 6938 T = Context.SatLongFractTy; 6939 break; 6940 case PREDEF_TYPE_SAT_USHORT_FRACT_ID: 6941 T = Context.SatUnsignedShortFractTy; 6942 break; 6943 case PREDEF_TYPE_SAT_UFRACT_ID: 6944 T = Context.SatUnsignedFractTy; 6945 break; 6946 case PREDEF_TYPE_SAT_ULONG_FRACT_ID: 6947 T = Context.SatUnsignedLongFractTy; 6948 break; 6949 case PREDEF_TYPE_FLOAT16_ID: 6950 T = Context.Float16Ty; 6951 break; 6952 case PREDEF_TYPE_FLOAT128_ID: 6953 T = Context.Float128Ty; 6954 break; 6955 case PREDEF_TYPE_OVERLOAD_ID: 6956 T = Context.OverloadTy; 6957 break; 6958 case PREDEF_TYPE_BOUND_MEMBER: 6959 T = Context.BoundMemberTy; 6960 break; 6961 case PREDEF_TYPE_PSEUDO_OBJECT: 6962 T = Context.PseudoObjectTy; 6963 break; 6964 case PREDEF_TYPE_DEPENDENT_ID: 6965 T = Context.DependentTy; 6966 break; 6967 case PREDEF_TYPE_UNKNOWN_ANY: 6968 T = Context.UnknownAnyTy; 6969 break; 6970 case PREDEF_TYPE_NULLPTR_ID: 6971 T = Context.NullPtrTy; 6972 break; 6973 case PREDEF_TYPE_CHAR8_ID: 6974 T = Context.Char8Ty; 6975 break; 6976 case PREDEF_TYPE_CHAR16_ID: 6977 T = Context.Char16Ty; 6978 break; 6979 case PREDEF_TYPE_CHAR32_ID: 6980 T = Context.Char32Ty; 6981 break; 6982 case PREDEF_TYPE_OBJC_ID: 6983 T = Context.ObjCBuiltinIdTy; 6984 break; 6985 case PREDEF_TYPE_OBJC_CLASS: 6986 T = Context.ObjCBuiltinClassTy; 6987 break; 6988 case PREDEF_TYPE_OBJC_SEL: 6989 T = Context.ObjCBuiltinSelTy; 6990 break; 6991 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 6992 case PREDEF_TYPE_##Id##_ID: \ 6993 T = Context.SingletonId; \ 6994 break; 6995 #include "clang/Basic/OpenCLImageTypes.def" 6996 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ 6997 case PREDEF_TYPE_##Id##_ID: \ 6998 T = Context.Id##Ty; \ 6999 break; 7000 #include "clang/Basic/OpenCLExtensionTypes.def" 7001 case PREDEF_TYPE_SAMPLER_ID: 7002 T = Context.OCLSamplerTy; 7003 break; 7004 case PREDEF_TYPE_EVENT_ID: 7005 T = Context.OCLEventTy; 7006 break; 7007 case PREDEF_TYPE_CLK_EVENT_ID: 7008 T = Context.OCLClkEventTy; 7009 break; 7010 case PREDEF_TYPE_QUEUE_ID: 7011 T = Context.OCLQueueTy; 7012 break; 7013 case PREDEF_TYPE_RESERVE_ID_ID: 7014 T = Context.OCLReserveIDTy; 7015 break; 7016 case PREDEF_TYPE_AUTO_DEDUCT: 7017 T = Context.getAutoDeductType(); 7018 break; 7019 case PREDEF_TYPE_AUTO_RREF_DEDUCT: 7020 T = Context.getAutoRRefDeductType(); 7021 break; 7022 case PREDEF_TYPE_ARC_UNBRIDGED_CAST: 7023 T = Context.ARCUnbridgedCastTy; 7024 break; 7025 case PREDEF_TYPE_BUILTIN_FN: 7026 T = Context.BuiltinFnTy; 7027 break; 7028 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX: 7029 T = Context.IncompleteMatrixIdxTy; 7030 break; 7031 case PREDEF_TYPE_OMP_ARRAY_SECTION: 7032 T = Context.OMPArraySectionTy; 7033 break; 7034 case PREDEF_TYPE_OMP_ARRAY_SHAPING: 7035 T = Context.OMPArraySectionTy; 7036 break; 7037 case PREDEF_TYPE_OMP_ITERATOR: 7038 T = Context.OMPIteratorTy; 7039 break; 7040 #define SVE_TYPE(Name, Id, SingletonId) \ 7041 case PREDEF_TYPE_##Id##_ID: \ 7042 T = Context.SingletonId; \ 7043 break; 7044 #include "clang/Basic/AArch64SVEACLETypes.def" 7045 } 7046 7047 assert(!T.isNull() && "Unknown predefined type"); 7048 return T.withFastQualifiers(FastQuals); 7049 } 7050 7051 Index -= NUM_PREDEF_TYPE_IDS; 7052 assert(Index < TypesLoaded.size() && "Type index out-of-range"); 7053 if (TypesLoaded[Index].isNull()) { 7054 TypesLoaded[Index] = readTypeRecord(Index); 7055 if (TypesLoaded[Index].isNull()) 7056 return QualType(); 7057 7058 TypesLoaded[Index]->setFromAST(); 7059 if (DeserializationListener) 7060 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID), 7061 TypesLoaded[Index]); 7062 } 7063 7064 return TypesLoaded[Index].withFastQualifiers(FastQuals); 7065 } 7066 7067 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) { 7068 return GetType(getGlobalTypeID(F, LocalID)); 7069 } 7070 7071 serialization::TypeID 7072 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const { 7073 unsigned FastQuals = LocalID & Qualifiers::FastMask; 7074 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth; 7075 7076 if (LocalIndex < NUM_PREDEF_TYPE_IDS) 7077 return LocalID; 7078 7079 if (!F.ModuleOffsetMap.empty()) 7080 ReadModuleOffsetMap(F); 7081 7082 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7083 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS); 7084 assert(I != F.TypeRemap.end() && "Invalid index into type index remap"); 7085 7086 unsigned GlobalIndex = LocalIndex + I->second; 7087 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals; 7088 } 7089 7090 TemplateArgumentLocInfo 7091 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) { 7092 switch (Kind) { 7093 case TemplateArgument::Expression: 7094 return readExpr(); 7095 case TemplateArgument::Type: 7096 return readTypeSourceInfo(); 7097 case TemplateArgument::Template: { 7098 NestedNameSpecifierLoc QualifierLoc = 7099 readNestedNameSpecifierLoc(); 7100 SourceLocation TemplateNameLoc = readSourceLocation(); 7101 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 7102 SourceLocation()); 7103 } 7104 case TemplateArgument::TemplateExpansion: { 7105 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc(); 7106 SourceLocation TemplateNameLoc = readSourceLocation(); 7107 SourceLocation EllipsisLoc = readSourceLocation(); 7108 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 7109 EllipsisLoc); 7110 } 7111 case TemplateArgument::Null: 7112 case TemplateArgument::Integral: 7113 case TemplateArgument::Declaration: 7114 case TemplateArgument::NullPtr: 7115 case TemplateArgument::Pack: 7116 // FIXME: Is this right? 7117 return TemplateArgumentLocInfo(); 7118 } 7119 llvm_unreachable("unexpected template argument loc"); 7120 } 7121 7122 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() { 7123 TemplateArgument Arg = readTemplateArgument(); 7124 7125 if (Arg.getKind() == TemplateArgument::Expression) { 7126 if (readBool()) // bool InfoHasSameExpr. 7127 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); 7128 } 7129 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind())); 7130 } 7131 7132 const ASTTemplateArgumentListInfo * 7133 ASTRecordReader::readASTTemplateArgumentListInfo() { 7134 SourceLocation LAngleLoc = readSourceLocation(); 7135 SourceLocation RAngleLoc = readSourceLocation(); 7136 unsigned NumArgsAsWritten = readInt(); 7137 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc); 7138 for (unsigned i = 0; i != NumArgsAsWritten; ++i) 7139 TemplArgsInfo.addArgument(readTemplateArgumentLoc()); 7140 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo); 7141 } 7142 7143 Decl *ASTReader::GetExternalDecl(uint32_t ID) { 7144 return GetDecl(ID); 7145 } 7146 7147 void ASTReader::CompleteRedeclChain(const Decl *D) { 7148 if (NumCurrentElementsDeserializing) { 7149 // We arrange to not care about the complete redeclaration chain while we're 7150 // deserializing. Just remember that the AST has marked this one as complete 7151 // but that it's not actually complete yet, so we know we still need to 7152 // complete it later. 7153 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D)); 7154 return; 7155 } 7156 7157 const DeclContext *DC = D->getDeclContext()->getRedeclContext(); 7158 7159 // If this is a named declaration, complete it by looking it up 7160 // within its context. 7161 // 7162 // FIXME: Merging a function definition should merge 7163 // all mergeable entities within it. 7164 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) || 7165 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) { 7166 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) { 7167 if (!getContext().getLangOpts().CPlusPlus && 7168 isa<TranslationUnitDecl>(DC)) { 7169 // Outside of C++, we don't have a lookup table for the TU, so update 7170 // the identifier instead. (For C++ modules, we don't store decls 7171 // in the serialized identifier table, so we do the lookup in the TU.) 7172 auto *II = Name.getAsIdentifierInfo(); 7173 assert(II && "non-identifier name in C?"); 7174 if (II->isOutOfDate()) 7175 updateOutOfDateIdentifier(*II); 7176 } else 7177 DC->lookup(Name); 7178 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) { 7179 // Find all declarations of this kind from the relevant context. 7180 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) { 7181 auto *DC = cast<DeclContext>(DCDecl); 7182 SmallVector<Decl*, 8> Decls; 7183 FindExternalLexicalDecls( 7184 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls); 7185 } 7186 } 7187 } 7188 7189 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) 7190 CTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7191 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) 7192 VTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7193 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 7194 if (auto *Template = FD->getPrimaryTemplate()) 7195 Template->LoadLazySpecializations(); 7196 } 7197 } 7198 7199 CXXCtorInitializer ** 7200 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) { 7201 RecordLocation Loc = getLocalBitOffset(Offset); 7202 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7203 SavedStreamPosition SavedPosition(Cursor); 7204 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7205 Error(std::move(Err)); 7206 return nullptr; 7207 } 7208 ReadingKindTracker ReadingKind(Read_Decl, *this); 7209 7210 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7211 if (!MaybeCode) { 7212 Error(MaybeCode.takeError()); 7213 return nullptr; 7214 } 7215 unsigned Code = MaybeCode.get(); 7216 7217 ASTRecordReader Record(*this, *Loc.F); 7218 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7219 if (!MaybeRecCode) { 7220 Error(MaybeRecCode.takeError()); 7221 return nullptr; 7222 } 7223 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) { 7224 Error("malformed AST file: missing C++ ctor initializers"); 7225 return nullptr; 7226 } 7227 7228 return Record.readCXXCtorInitializers(); 7229 } 7230 7231 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) { 7232 assert(ContextObj && "reading base specifiers with no AST context"); 7233 ASTContext &Context = *ContextObj; 7234 7235 RecordLocation Loc = getLocalBitOffset(Offset); 7236 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7237 SavedStreamPosition SavedPosition(Cursor); 7238 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7239 Error(std::move(Err)); 7240 return nullptr; 7241 } 7242 ReadingKindTracker ReadingKind(Read_Decl, *this); 7243 7244 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7245 if (!MaybeCode) { 7246 Error(MaybeCode.takeError()); 7247 return nullptr; 7248 } 7249 unsigned Code = MaybeCode.get(); 7250 7251 ASTRecordReader Record(*this, *Loc.F); 7252 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7253 if (!MaybeRecCode) { 7254 Error(MaybeCode.takeError()); 7255 return nullptr; 7256 } 7257 unsigned RecCode = MaybeRecCode.get(); 7258 7259 if (RecCode != DECL_CXX_BASE_SPECIFIERS) { 7260 Error("malformed AST file: missing C++ base specifiers"); 7261 return nullptr; 7262 } 7263 7264 unsigned NumBases = Record.readInt(); 7265 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases); 7266 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases]; 7267 for (unsigned I = 0; I != NumBases; ++I) 7268 Bases[I] = Record.readCXXBaseSpecifier(); 7269 return Bases; 7270 } 7271 7272 serialization::DeclID 7273 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const { 7274 if (LocalID < NUM_PREDEF_DECL_IDS) 7275 return LocalID; 7276 7277 if (!F.ModuleOffsetMap.empty()) 7278 ReadModuleOffsetMap(F); 7279 7280 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7281 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS); 7282 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap"); 7283 7284 return LocalID + I->second; 7285 } 7286 7287 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID, 7288 ModuleFile &M) const { 7289 // Predefined decls aren't from any module. 7290 if (ID < NUM_PREDEF_DECL_IDS) 7291 return false; 7292 7293 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID && 7294 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls; 7295 } 7296 7297 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) { 7298 if (!D->isFromASTFile()) 7299 return nullptr; 7300 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID()); 7301 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7302 return I->second; 7303 } 7304 7305 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) { 7306 if (ID < NUM_PREDEF_DECL_IDS) 7307 return SourceLocation(); 7308 7309 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7310 7311 if (Index > DeclsLoaded.size()) { 7312 Error("declaration ID out-of-range for AST file"); 7313 return SourceLocation(); 7314 } 7315 7316 if (Decl *D = DeclsLoaded[Index]) 7317 return D->getLocation(); 7318 7319 SourceLocation Loc; 7320 DeclCursorForID(ID, Loc); 7321 return Loc; 7322 } 7323 7324 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) { 7325 switch (ID) { 7326 case PREDEF_DECL_NULL_ID: 7327 return nullptr; 7328 7329 case PREDEF_DECL_TRANSLATION_UNIT_ID: 7330 return Context.getTranslationUnitDecl(); 7331 7332 case PREDEF_DECL_OBJC_ID_ID: 7333 return Context.getObjCIdDecl(); 7334 7335 case PREDEF_DECL_OBJC_SEL_ID: 7336 return Context.getObjCSelDecl(); 7337 7338 case PREDEF_DECL_OBJC_CLASS_ID: 7339 return Context.getObjCClassDecl(); 7340 7341 case PREDEF_DECL_OBJC_PROTOCOL_ID: 7342 return Context.getObjCProtocolDecl(); 7343 7344 case PREDEF_DECL_INT_128_ID: 7345 return Context.getInt128Decl(); 7346 7347 case PREDEF_DECL_UNSIGNED_INT_128_ID: 7348 return Context.getUInt128Decl(); 7349 7350 case PREDEF_DECL_OBJC_INSTANCETYPE_ID: 7351 return Context.getObjCInstanceTypeDecl(); 7352 7353 case PREDEF_DECL_BUILTIN_VA_LIST_ID: 7354 return Context.getBuiltinVaListDecl(); 7355 7356 case PREDEF_DECL_VA_LIST_TAG: 7357 return Context.getVaListTagDecl(); 7358 7359 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID: 7360 return Context.getBuiltinMSVaListDecl(); 7361 7362 case PREDEF_DECL_BUILTIN_MS_GUID_ID: 7363 return Context.getMSGuidTagDecl(); 7364 7365 case PREDEF_DECL_EXTERN_C_CONTEXT_ID: 7366 return Context.getExternCContextDecl(); 7367 7368 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID: 7369 return Context.getMakeIntegerSeqDecl(); 7370 7371 case PREDEF_DECL_CF_CONSTANT_STRING_ID: 7372 return Context.getCFConstantStringDecl(); 7373 7374 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID: 7375 return Context.getCFConstantStringTagDecl(); 7376 7377 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID: 7378 return Context.getTypePackElementDecl(); 7379 } 7380 llvm_unreachable("PredefinedDeclIDs unknown enum value"); 7381 } 7382 7383 Decl *ASTReader::GetExistingDecl(DeclID ID) { 7384 assert(ContextObj && "reading decl with no AST context"); 7385 if (ID < NUM_PREDEF_DECL_IDS) { 7386 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID); 7387 if (D) { 7388 // Track that we have merged the declaration with ID \p ID into the 7389 // pre-existing predefined declaration \p D. 7390 auto &Merged = KeyDecls[D->getCanonicalDecl()]; 7391 if (Merged.empty()) 7392 Merged.push_back(ID); 7393 } 7394 return D; 7395 } 7396 7397 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7398 7399 if (Index >= DeclsLoaded.size()) { 7400 assert(0 && "declaration ID out-of-range for AST file"); 7401 Error("declaration ID out-of-range for AST file"); 7402 return nullptr; 7403 } 7404 7405 return DeclsLoaded[Index]; 7406 } 7407 7408 Decl *ASTReader::GetDecl(DeclID ID) { 7409 if (ID < NUM_PREDEF_DECL_IDS) 7410 return GetExistingDecl(ID); 7411 7412 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7413 7414 if (Index >= DeclsLoaded.size()) { 7415 assert(0 && "declaration ID out-of-range for AST file"); 7416 Error("declaration ID out-of-range for AST file"); 7417 return nullptr; 7418 } 7419 7420 if (!DeclsLoaded[Index]) { 7421 ReadDeclRecord(ID); 7422 if (DeserializationListener) 7423 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); 7424 } 7425 7426 return DeclsLoaded[Index]; 7427 } 7428 7429 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M, 7430 DeclID GlobalID) { 7431 if (GlobalID < NUM_PREDEF_DECL_IDS) 7432 return GlobalID; 7433 7434 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID); 7435 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7436 ModuleFile *Owner = I->second; 7437 7438 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos 7439 = M.GlobalToLocalDeclIDs.find(Owner); 7440 if (Pos == M.GlobalToLocalDeclIDs.end()) 7441 return 0; 7442 7443 return GlobalID - Owner->BaseDeclID + Pos->second; 7444 } 7445 7446 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F, 7447 const RecordData &Record, 7448 unsigned &Idx) { 7449 if (Idx >= Record.size()) { 7450 Error("Corrupted AST file"); 7451 return 0; 7452 } 7453 7454 return getGlobalDeclID(F, Record[Idx++]); 7455 } 7456 7457 /// Resolve the offset of a statement into a statement. 7458 /// 7459 /// This operation will read a new statement from the external 7460 /// source each time it is called, and is meant to be used via a 7461 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 7462 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) { 7463 // Switch case IDs are per Decl. 7464 ClearSwitchCaseIDs(); 7465 7466 // Offset here is a global offset across the entire chain. 7467 RecordLocation Loc = getLocalBitOffset(Offset); 7468 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) { 7469 Error(std::move(Err)); 7470 return nullptr; 7471 } 7472 assert(NumCurrentElementsDeserializing == 0 && 7473 "should not be called while already deserializing"); 7474 Deserializing D(this); 7475 return ReadStmtFromStream(*Loc.F); 7476 } 7477 7478 void ASTReader::FindExternalLexicalDecls( 7479 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant, 7480 SmallVectorImpl<Decl *> &Decls) { 7481 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {}; 7482 7483 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) { 7484 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries"); 7485 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) { 7486 auto K = (Decl::Kind)+LexicalDecls[I]; 7487 if (!IsKindWeWant(K)) 7488 continue; 7489 7490 auto ID = (serialization::DeclID)+LexicalDecls[I + 1]; 7491 7492 // Don't add predefined declarations to the lexical context more 7493 // than once. 7494 if (ID < NUM_PREDEF_DECL_IDS) { 7495 if (PredefsVisited[ID]) 7496 continue; 7497 7498 PredefsVisited[ID] = true; 7499 } 7500 7501 if (Decl *D = GetLocalDecl(*M, ID)) { 7502 assert(D->getKind() == K && "wrong kind for lexical decl"); 7503 if (!DC->isDeclInLexicalTraversal(D)) 7504 Decls.push_back(D); 7505 } 7506 } 7507 }; 7508 7509 if (isa<TranslationUnitDecl>(DC)) { 7510 for (auto Lexical : TULexicalDecls) 7511 Visit(Lexical.first, Lexical.second); 7512 } else { 7513 auto I = LexicalDecls.find(DC); 7514 if (I != LexicalDecls.end()) 7515 Visit(I->second.first, I->second.second); 7516 } 7517 7518 ++NumLexicalDeclContextsRead; 7519 } 7520 7521 namespace { 7522 7523 class DeclIDComp { 7524 ASTReader &Reader; 7525 ModuleFile &Mod; 7526 7527 public: 7528 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {} 7529 7530 bool operator()(LocalDeclID L, LocalDeclID R) const { 7531 SourceLocation LHS = getLocation(L); 7532 SourceLocation RHS = getLocation(R); 7533 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7534 } 7535 7536 bool operator()(SourceLocation LHS, LocalDeclID R) const { 7537 SourceLocation RHS = getLocation(R); 7538 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7539 } 7540 7541 bool operator()(LocalDeclID L, SourceLocation RHS) const { 7542 SourceLocation LHS = getLocation(L); 7543 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7544 } 7545 7546 SourceLocation getLocation(LocalDeclID ID) const { 7547 return Reader.getSourceManager().getFileLoc( 7548 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID))); 7549 } 7550 }; 7551 7552 } // namespace 7553 7554 void ASTReader::FindFileRegionDecls(FileID File, 7555 unsigned Offset, unsigned Length, 7556 SmallVectorImpl<Decl *> &Decls) { 7557 SourceManager &SM = getSourceManager(); 7558 7559 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File); 7560 if (I == FileDeclIDs.end()) 7561 return; 7562 7563 FileDeclsInfo &DInfo = I->second; 7564 if (DInfo.Decls.empty()) 7565 return; 7566 7567 SourceLocation 7568 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset); 7569 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length); 7570 7571 DeclIDComp DIDComp(*this, *DInfo.Mod); 7572 ArrayRef<serialization::LocalDeclID>::iterator BeginIt = 7573 llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp); 7574 if (BeginIt != DInfo.Decls.begin()) 7575 --BeginIt; 7576 7577 // If we are pointing at a top-level decl inside an objc container, we need 7578 // to backtrack until we find it otherwise we will fail to report that the 7579 // region overlaps with an objc container. 7580 while (BeginIt != DInfo.Decls.begin() && 7581 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt)) 7582 ->isTopLevelDeclInObjCContainer()) 7583 --BeginIt; 7584 7585 ArrayRef<serialization::LocalDeclID>::iterator EndIt = 7586 llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp); 7587 if (EndIt != DInfo.Decls.end()) 7588 ++EndIt; 7589 7590 for (ArrayRef<serialization::LocalDeclID>::iterator 7591 DIt = BeginIt; DIt != EndIt; ++DIt) 7592 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt))); 7593 } 7594 7595 bool 7596 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, 7597 DeclarationName Name) { 7598 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() && 7599 "DeclContext has no visible decls in storage"); 7600 if (!Name) 7601 return false; 7602 7603 auto It = Lookups.find(DC); 7604 if (It == Lookups.end()) 7605 return false; 7606 7607 Deserializing LookupResults(this); 7608 7609 // Load the list of declarations. 7610 SmallVector<NamedDecl *, 64> Decls; 7611 for (DeclID ID : It->second.Table.find(Name)) { 7612 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7613 if (ND->getDeclName() == Name) 7614 Decls.push_back(ND); 7615 } 7616 7617 ++NumVisibleDeclContextsRead; 7618 SetExternalVisibleDeclsForName(DC, Name, Decls); 7619 return !Decls.empty(); 7620 } 7621 7622 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) { 7623 if (!DC->hasExternalVisibleStorage()) 7624 return; 7625 7626 auto It = Lookups.find(DC); 7627 assert(It != Lookups.end() && 7628 "have external visible storage but no lookup tables"); 7629 7630 DeclsMap Decls; 7631 7632 for (DeclID ID : It->second.Table.findAll()) { 7633 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7634 Decls[ND->getDeclName()].push_back(ND); 7635 } 7636 7637 ++NumVisibleDeclContextsRead; 7638 7639 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) { 7640 SetExternalVisibleDeclsForName(DC, I->first, I->second); 7641 } 7642 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false); 7643 } 7644 7645 const serialization::reader::DeclContextLookupTable * 7646 ASTReader::getLoadedLookupTables(DeclContext *Primary) const { 7647 auto I = Lookups.find(Primary); 7648 return I == Lookups.end() ? nullptr : &I->second; 7649 } 7650 7651 /// Under non-PCH compilation the consumer receives the objc methods 7652 /// before receiving the implementation, and codegen depends on this. 7653 /// We simulate this by deserializing and passing to consumer the methods of the 7654 /// implementation before passing the deserialized implementation decl. 7655 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD, 7656 ASTConsumer *Consumer) { 7657 assert(ImplD && Consumer); 7658 7659 for (auto *I : ImplD->methods()) 7660 Consumer->HandleInterestingDecl(DeclGroupRef(I)); 7661 7662 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD)); 7663 } 7664 7665 void ASTReader::PassInterestingDeclToConsumer(Decl *D) { 7666 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) 7667 PassObjCImplDeclToConsumer(ImplD, Consumer); 7668 else 7669 Consumer->HandleInterestingDecl(DeclGroupRef(D)); 7670 } 7671 7672 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) { 7673 this->Consumer = Consumer; 7674 7675 if (Consumer) 7676 PassInterestingDeclsToConsumer(); 7677 7678 if (DeserializationListener) 7679 DeserializationListener->ReaderInitialized(this); 7680 } 7681 7682 void ASTReader::PrintStats() { 7683 std::fprintf(stderr, "*** AST File Statistics:\n"); 7684 7685 unsigned NumTypesLoaded 7686 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), 7687 QualType()); 7688 unsigned NumDeclsLoaded 7689 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), 7690 (Decl *)nullptr); 7691 unsigned NumIdentifiersLoaded 7692 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), 7693 IdentifiersLoaded.end(), 7694 (IdentifierInfo *)nullptr); 7695 unsigned NumMacrosLoaded 7696 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(), 7697 MacrosLoaded.end(), 7698 (MacroInfo *)nullptr); 7699 unsigned NumSelectorsLoaded 7700 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), 7701 SelectorsLoaded.end(), 7702 Selector()); 7703 7704 if (unsigned TotalNumSLocEntries = getTotalNumSLocs()) 7705 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", 7706 NumSLocEntriesRead, TotalNumSLocEntries, 7707 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); 7708 if (!TypesLoaded.empty()) 7709 std::fprintf(stderr, " %u/%u types read (%f%%)\n", 7710 NumTypesLoaded, (unsigned)TypesLoaded.size(), 7711 ((float)NumTypesLoaded/TypesLoaded.size() * 100)); 7712 if (!DeclsLoaded.empty()) 7713 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", 7714 NumDeclsLoaded, (unsigned)DeclsLoaded.size(), 7715 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); 7716 if (!IdentifiersLoaded.empty()) 7717 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", 7718 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), 7719 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); 7720 if (!MacrosLoaded.empty()) 7721 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7722 NumMacrosLoaded, (unsigned)MacrosLoaded.size(), 7723 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100)); 7724 if (!SelectorsLoaded.empty()) 7725 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", 7726 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), 7727 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); 7728 if (TotalNumStatements) 7729 std::fprintf(stderr, " %u/%u statements read (%f%%)\n", 7730 NumStatementsRead, TotalNumStatements, 7731 ((float)NumStatementsRead/TotalNumStatements * 100)); 7732 if (TotalNumMacros) 7733 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7734 NumMacrosRead, TotalNumMacros, 7735 ((float)NumMacrosRead/TotalNumMacros * 100)); 7736 if (TotalLexicalDeclContexts) 7737 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", 7738 NumLexicalDeclContextsRead, TotalLexicalDeclContexts, 7739 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts 7740 * 100)); 7741 if (TotalVisibleDeclContexts) 7742 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", 7743 NumVisibleDeclContextsRead, TotalVisibleDeclContexts, 7744 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts 7745 * 100)); 7746 if (TotalNumMethodPoolEntries) 7747 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", 7748 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, 7749 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries 7750 * 100)); 7751 if (NumMethodPoolLookups) 7752 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n", 7753 NumMethodPoolHits, NumMethodPoolLookups, 7754 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0)); 7755 if (NumMethodPoolTableLookups) 7756 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n", 7757 NumMethodPoolTableHits, NumMethodPoolTableLookups, 7758 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups 7759 * 100.0)); 7760 if (NumIdentifierLookupHits) 7761 std::fprintf(stderr, 7762 " %u / %u identifier table lookups succeeded (%f%%)\n", 7763 NumIdentifierLookupHits, NumIdentifierLookups, 7764 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups); 7765 7766 if (GlobalIndex) { 7767 std::fprintf(stderr, "\n"); 7768 GlobalIndex->printStats(); 7769 } 7770 7771 std::fprintf(stderr, "\n"); 7772 dump(); 7773 std::fprintf(stderr, "\n"); 7774 } 7775 7776 template<typename Key, typename ModuleFile, unsigned InitialCapacity> 7777 LLVM_DUMP_METHOD static void 7778 dumpModuleIDMap(StringRef Name, 7779 const ContinuousRangeMap<Key, ModuleFile *, 7780 InitialCapacity> &Map) { 7781 if (Map.begin() == Map.end()) 7782 return; 7783 7784 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>; 7785 7786 llvm::errs() << Name << ":\n"; 7787 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end(); 7788 I != IEnd; ++I) { 7789 llvm::errs() << " " << I->first << " -> " << I->second->FileName 7790 << "\n"; 7791 } 7792 } 7793 7794 LLVM_DUMP_METHOD void ASTReader::dump() { 7795 llvm::errs() << "*** PCH/ModuleFile Remappings:\n"; 7796 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap); 7797 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap); 7798 dumpModuleIDMap("Global type map", GlobalTypeMap); 7799 dumpModuleIDMap("Global declaration map", GlobalDeclMap); 7800 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap); 7801 dumpModuleIDMap("Global macro map", GlobalMacroMap); 7802 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap); 7803 dumpModuleIDMap("Global selector map", GlobalSelectorMap); 7804 dumpModuleIDMap("Global preprocessed entity map", 7805 GlobalPreprocessedEntityMap); 7806 7807 llvm::errs() << "\n*** PCH/Modules Loaded:"; 7808 for (ModuleFile &M : ModuleMgr) 7809 M.dump(); 7810 } 7811 7812 /// Return the amount of memory used by memory buffers, breaking down 7813 /// by heap-backed versus mmap'ed memory. 7814 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const { 7815 for (ModuleFile &I : ModuleMgr) { 7816 if (llvm::MemoryBuffer *buf = I.Buffer) { 7817 size_t bytes = buf->getBufferSize(); 7818 switch (buf->getBufferKind()) { 7819 case llvm::MemoryBuffer::MemoryBuffer_Malloc: 7820 sizes.malloc_bytes += bytes; 7821 break; 7822 case llvm::MemoryBuffer::MemoryBuffer_MMap: 7823 sizes.mmap_bytes += bytes; 7824 break; 7825 } 7826 } 7827 } 7828 } 7829 7830 void ASTReader::InitializeSema(Sema &S) { 7831 SemaObj = &S; 7832 S.addExternalSource(this); 7833 7834 // Makes sure any declarations that were deserialized "too early" 7835 // still get added to the identifier's declaration chains. 7836 for (uint64_t ID : PreloadedDeclIDs) { 7837 NamedDecl *D = cast<NamedDecl>(GetDecl(ID)); 7838 pushExternalDeclIntoScope(D, D->getDeclName()); 7839 } 7840 PreloadedDeclIDs.clear(); 7841 7842 // FIXME: What happens if these are changed by a module import? 7843 if (!FPPragmaOptions.empty()) { 7844 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS"); 7845 FPOptionsOverride NewOverrides = 7846 FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]); 7847 SemaObj->CurFPFeatures = 7848 NewOverrides.applyOverrides(SemaObj->getLangOpts()); 7849 } 7850 7851 SemaObj->OpenCLFeatures.copy(OpenCLExtensions); 7852 SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap; 7853 SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap; 7854 7855 UpdateSema(); 7856 } 7857 7858 void ASTReader::UpdateSema() { 7859 assert(SemaObj && "no Sema to update"); 7860 7861 // Load the offsets of the declarations that Sema references. 7862 // They will be lazily deserialized when needed. 7863 if (!SemaDeclRefs.empty()) { 7864 assert(SemaDeclRefs.size() % 3 == 0); 7865 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) { 7866 if (!SemaObj->StdNamespace) 7867 SemaObj->StdNamespace = SemaDeclRefs[I]; 7868 if (!SemaObj->StdBadAlloc) 7869 SemaObj->StdBadAlloc = SemaDeclRefs[I+1]; 7870 if (!SemaObj->StdAlignValT) 7871 SemaObj->StdAlignValT = SemaDeclRefs[I+2]; 7872 } 7873 SemaDeclRefs.clear(); 7874 } 7875 7876 // Update the state of pragmas. Use the same API as if we had encountered the 7877 // pragma in the source. 7878 if(OptimizeOffPragmaLocation.isValid()) 7879 SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation); 7880 if (PragmaMSStructState != -1) 7881 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState); 7882 if (PointersToMembersPragmaLocation.isValid()) { 7883 SemaObj->ActOnPragmaMSPointersToMembers( 7884 (LangOptions::PragmaMSPointersToMembersKind) 7885 PragmaMSPointersToMembersState, 7886 PointersToMembersPragmaLocation); 7887 } 7888 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth; 7889 7890 if (PragmaPackCurrentValue) { 7891 // The bottom of the stack might have a default value. It must be adjusted 7892 // to the current value to ensure that the packing state is preserved after 7893 // popping entries that were included/imported from a PCH/module. 7894 bool DropFirst = false; 7895 if (!PragmaPackStack.empty() && 7896 PragmaPackStack.front().Location.isInvalid()) { 7897 assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue && 7898 "Expected a default alignment value"); 7899 SemaObj->PackStack.Stack.emplace_back( 7900 PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue, 7901 SemaObj->PackStack.CurrentPragmaLocation, 7902 PragmaPackStack.front().PushLocation); 7903 DropFirst = true; 7904 } 7905 for (const auto &Entry : 7906 llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0)) 7907 SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value, 7908 Entry.Location, Entry.PushLocation); 7909 if (PragmaPackCurrentLocation.isInvalid()) { 7910 assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue && 7911 "Expected a default alignment value"); 7912 // Keep the current values. 7913 } else { 7914 SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue; 7915 SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation; 7916 } 7917 } 7918 if (FpPragmaCurrentValue) { 7919 // The bottom of the stack might have a default value. It must be adjusted 7920 // to the current value to ensure that fp-pragma state is preserved after 7921 // popping entries that were included/imported from a PCH/module. 7922 bool DropFirst = false; 7923 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) { 7924 assert(FpPragmaStack.front().Value == 7925 SemaObj->FpPragmaStack.DefaultValue && 7926 "Expected a default pragma float_control value"); 7927 SemaObj->FpPragmaStack.Stack.emplace_back( 7928 FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue, 7929 SemaObj->FpPragmaStack.CurrentPragmaLocation, 7930 FpPragmaStack.front().PushLocation); 7931 DropFirst = true; 7932 } 7933 for (const auto &Entry : 7934 llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0)) 7935 SemaObj->FpPragmaStack.Stack.emplace_back( 7936 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation); 7937 if (FpPragmaCurrentLocation.isInvalid()) { 7938 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue && 7939 "Expected a default pragma float_control value"); 7940 // Keep the current values. 7941 } else { 7942 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue; 7943 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation; 7944 } 7945 } 7946 7947 // For non-modular AST files, restore visiblity of modules. 7948 for (auto &Import : ImportedModules) { 7949 if (Import.ImportLoc.isInvalid()) 7950 continue; 7951 if (Module *Imported = getSubmodule(Import.ID)) { 7952 SemaObj->makeModuleVisible(Imported, Import.ImportLoc); 7953 } 7954 } 7955 } 7956 7957 IdentifierInfo *ASTReader::get(StringRef Name) { 7958 // Note that we are loading an identifier. 7959 Deserializing AnIdentifier(this); 7960 7961 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0, 7962 NumIdentifierLookups, 7963 NumIdentifierLookupHits); 7964 7965 // We don't need to do identifier table lookups in C++ modules (we preload 7966 // all interesting declarations, and don't need to use the scope for name 7967 // lookups). Perform the lookup in PCH files, though, since we don't build 7968 // a complete initial identifier table if we're carrying on from a PCH. 7969 if (PP.getLangOpts().CPlusPlus) { 7970 for (auto F : ModuleMgr.pch_modules()) 7971 if (Visitor(*F)) 7972 break; 7973 } else { 7974 // If there is a global index, look there first to determine which modules 7975 // provably do not have any results for this identifier. 7976 GlobalModuleIndex::HitSet Hits; 7977 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 7978 if (!loadGlobalIndex()) { 7979 if (GlobalIndex->lookupIdentifier(Name, Hits)) { 7980 HitsPtr = &Hits; 7981 } 7982 } 7983 7984 ModuleMgr.visit(Visitor, HitsPtr); 7985 } 7986 7987 IdentifierInfo *II = Visitor.getIdentifierInfo(); 7988 markIdentifierUpToDate(II); 7989 return II; 7990 } 7991 7992 namespace clang { 7993 7994 /// An identifier-lookup iterator that enumerates all of the 7995 /// identifiers stored within a set of AST files. 7996 class ASTIdentifierIterator : public IdentifierIterator { 7997 /// The AST reader whose identifiers are being enumerated. 7998 const ASTReader &Reader; 7999 8000 /// The current index into the chain of AST files stored in 8001 /// the AST reader. 8002 unsigned Index; 8003 8004 /// The current position within the identifier lookup table 8005 /// of the current AST file. 8006 ASTIdentifierLookupTable::key_iterator Current; 8007 8008 /// The end position within the identifier lookup table of 8009 /// the current AST file. 8010 ASTIdentifierLookupTable::key_iterator End; 8011 8012 /// Whether to skip any modules in the ASTReader. 8013 bool SkipModules; 8014 8015 public: 8016 explicit ASTIdentifierIterator(const ASTReader &Reader, 8017 bool SkipModules = false); 8018 8019 StringRef Next() override; 8020 }; 8021 8022 } // namespace clang 8023 8024 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader, 8025 bool SkipModules) 8026 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) { 8027 } 8028 8029 StringRef ASTIdentifierIterator::Next() { 8030 while (Current == End) { 8031 // If we have exhausted all of our AST files, we're done. 8032 if (Index == 0) 8033 return StringRef(); 8034 8035 --Index; 8036 ModuleFile &F = Reader.ModuleMgr[Index]; 8037 if (SkipModules && F.isModule()) 8038 continue; 8039 8040 ASTIdentifierLookupTable *IdTable = 8041 (ASTIdentifierLookupTable *)F.IdentifierLookupTable; 8042 Current = IdTable->key_begin(); 8043 End = IdTable->key_end(); 8044 } 8045 8046 // We have any identifiers remaining in the current AST file; return 8047 // the next one. 8048 StringRef Result = *Current; 8049 ++Current; 8050 return Result; 8051 } 8052 8053 namespace { 8054 8055 /// A utility for appending two IdentifierIterators. 8056 class ChainedIdentifierIterator : public IdentifierIterator { 8057 std::unique_ptr<IdentifierIterator> Current; 8058 std::unique_ptr<IdentifierIterator> Queued; 8059 8060 public: 8061 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First, 8062 std::unique_ptr<IdentifierIterator> Second) 8063 : Current(std::move(First)), Queued(std::move(Second)) {} 8064 8065 StringRef Next() override { 8066 if (!Current) 8067 return StringRef(); 8068 8069 StringRef result = Current->Next(); 8070 if (!result.empty()) 8071 return result; 8072 8073 // Try the queued iterator, which may itself be empty. 8074 Current.reset(); 8075 std::swap(Current, Queued); 8076 return Next(); 8077 } 8078 }; 8079 8080 } // namespace 8081 8082 IdentifierIterator *ASTReader::getIdentifiers() { 8083 if (!loadGlobalIndex()) { 8084 std::unique_ptr<IdentifierIterator> ReaderIter( 8085 new ASTIdentifierIterator(*this, /*SkipModules=*/true)); 8086 std::unique_ptr<IdentifierIterator> ModulesIter( 8087 GlobalIndex->createIdentifierIterator()); 8088 return new ChainedIdentifierIterator(std::move(ReaderIter), 8089 std::move(ModulesIter)); 8090 } 8091 8092 return new ASTIdentifierIterator(*this); 8093 } 8094 8095 namespace clang { 8096 namespace serialization { 8097 8098 class ReadMethodPoolVisitor { 8099 ASTReader &Reader; 8100 Selector Sel; 8101 unsigned PriorGeneration; 8102 unsigned InstanceBits = 0; 8103 unsigned FactoryBits = 0; 8104 bool InstanceHasMoreThanOneDecl = false; 8105 bool FactoryHasMoreThanOneDecl = false; 8106 SmallVector<ObjCMethodDecl *, 4> InstanceMethods; 8107 SmallVector<ObjCMethodDecl *, 4> FactoryMethods; 8108 8109 public: 8110 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel, 8111 unsigned PriorGeneration) 8112 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {} 8113 8114 bool operator()(ModuleFile &M) { 8115 if (!M.SelectorLookupTable) 8116 return false; 8117 8118 // If we've already searched this module file, skip it now. 8119 if (M.Generation <= PriorGeneration) 8120 return true; 8121 8122 ++Reader.NumMethodPoolTableLookups; 8123 ASTSelectorLookupTable *PoolTable 8124 = (ASTSelectorLookupTable*)M.SelectorLookupTable; 8125 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel); 8126 if (Pos == PoolTable->end()) 8127 return false; 8128 8129 ++Reader.NumMethodPoolTableHits; 8130 ++Reader.NumSelectorsRead; 8131 // FIXME: Not quite happy with the statistics here. We probably should 8132 // disable this tracking when called via LoadSelector. 8133 // Also, should entries without methods count as misses? 8134 ++Reader.NumMethodPoolEntriesRead; 8135 ASTSelectorLookupTrait::data_type Data = *Pos; 8136 if (Reader.DeserializationListener) 8137 Reader.DeserializationListener->SelectorRead(Data.ID, Sel); 8138 8139 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end()); 8140 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end()); 8141 InstanceBits = Data.InstanceBits; 8142 FactoryBits = Data.FactoryBits; 8143 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl; 8144 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl; 8145 return true; 8146 } 8147 8148 /// Retrieve the instance methods found by this visitor. 8149 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const { 8150 return InstanceMethods; 8151 } 8152 8153 /// Retrieve the instance methods found by this visitor. 8154 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const { 8155 return FactoryMethods; 8156 } 8157 8158 unsigned getInstanceBits() const { return InstanceBits; } 8159 unsigned getFactoryBits() const { return FactoryBits; } 8160 8161 bool instanceHasMoreThanOneDecl() const { 8162 return InstanceHasMoreThanOneDecl; 8163 } 8164 8165 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; } 8166 }; 8167 8168 } // namespace serialization 8169 } // namespace clang 8170 8171 /// Add the given set of methods to the method list. 8172 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods, 8173 ObjCMethodList &List) { 8174 for (unsigned I = 0, N = Methods.size(); I != N; ++I) { 8175 S.addMethodToGlobalList(&List, Methods[I]); 8176 } 8177 } 8178 8179 void ASTReader::ReadMethodPool(Selector Sel) { 8180 // Get the selector generation and update it to the current generation. 8181 unsigned &Generation = SelectorGeneration[Sel]; 8182 unsigned PriorGeneration = Generation; 8183 Generation = getGeneration(); 8184 SelectorOutOfDate[Sel] = false; 8185 8186 // Search for methods defined with this selector. 8187 ++NumMethodPoolLookups; 8188 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration); 8189 ModuleMgr.visit(Visitor); 8190 8191 if (Visitor.getInstanceMethods().empty() && 8192 Visitor.getFactoryMethods().empty()) 8193 return; 8194 8195 ++NumMethodPoolHits; 8196 8197 if (!getSema()) 8198 return; 8199 8200 Sema &S = *getSema(); 8201 Sema::GlobalMethodPool::iterator Pos 8202 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first; 8203 8204 Pos->second.first.setBits(Visitor.getInstanceBits()); 8205 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl()); 8206 Pos->second.second.setBits(Visitor.getFactoryBits()); 8207 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl()); 8208 8209 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since 8210 // when building a module we keep every method individually and may need to 8211 // update hasMoreThanOneDecl as we add the methods. 8212 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first); 8213 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second); 8214 } 8215 8216 void ASTReader::updateOutOfDateSelector(Selector Sel) { 8217 if (SelectorOutOfDate[Sel]) 8218 ReadMethodPool(Sel); 8219 } 8220 8221 void ASTReader::ReadKnownNamespaces( 8222 SmallVectorImpl<NamespaceDecl *> &Namespaces) { 8223 Namespaces.clear(); 8224 8225 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) { 8226 if (NamespaceDecl *Namespace 8227 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I]))) 8228 Namespaces.push_back(Namespace); 8229 } 8230 } 8231 8232 void ASTReader::ReadUndefinedButUsed( 8233 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) { 8234 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) { 8235 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++])); 8236 SourceLocation Loc = 8237 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]); 8238 Undefined.insert(std::make_pair(D, Loc)); 8239 } 8240 } 8241 8242 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector< 8243 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> & 8244 Exprs) { 8245 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) { 8246 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++])); 8247 uint64_t Count = DelayedDeleteExprs[Idx++]; 8248 for (uint64_t C = 0; C < Count; ++C) { 8249 SourceLocation DeleteLoc = 8250 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]); 8251 const bool IsArrayForm = DelayedDeleteExprs[Idx++]; 8252 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm)); 8253 } 8254 } 8255 } 8256 8257 void ASTReader::ReadTentativeDefinitions( 8258 SmallVectorImpl<VarDecl *> &TentativeDefs) { 8259 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { 8260 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I])); 8261 if (Var) 8262 TentativeDefs.push_back(Var); 8263 } 8264 TentativeDefinitions.clear(); 8265 } 8266 8267 void ASTReader::ReadUnusedFileScopedDecls( 8268 SmallVectorImpl<const DeclaratorDecl *> &Decls) { 8269 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) { 8270 DeclaratorDecl *D 8271 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I])); 8272 if (D) 8273 Decls.push_back(D); 8274 } 8275 UnusedFileScopedDecls.clear(); 8276 } 8277 8278 void ASTReader::ReadDelegatingConstructors( 8279 SmallVectorImpl<CXXConstructorDecl *> &Decls) { 8280 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) { 8281 CXXConstructorDecl *D 8282 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I])); 8283 if (D) 8284 Decls.push_back(D); 8285 } 8286 DelegatingCtorDecls.clear(); 8287 } 8288 8289 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) { 8290 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) { 8291 TypedefNameDecl *D 8292 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])); 8293 if (D) 8294 Decls.push_back(D); 8295 } 8296 ExtVectorDecls.clear(); 8297 } 8298 8299 void ASTReader::ReadUnusedLocalTypedefNameCandidates( 8300 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) { 8301 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N; 8302 ++I) { 8303 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>( 8304 GetDecl(UnusedLocalTypedefNameCandidates[I])); 8305 if (D) 8306 Decls.insert(D); 8307 } 8308 UnusedLocalTypedefNameCandidates.clear(); 8309 } 8310 8311 void ASTReader::ReadDeclsToCheckForDeferredDiags( 8312 llvm::SmallVector<Decl *, 4> &Decls) { 8313 for (unsigned I = 0, N = DeclsToCheckForDeferredDiags.size(); I != N; 8314 ++I) { 8315 auto *D = dyn_cast_or_null<Decl>( 8316 GetDecl(DeclsToCheckForDeferredDiags[I])); 8317 if (D) 8318 Decls.push_back(D); 8319 } 8320 DeclsToCheckForDeferredDiags.clear(); 8321 } 8322 8323 8324 void ASTReader::ReadReferencedSelectors( 8325 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) { 8326 if (ReferencedSelectorsData.empty()) 8327 return; 8328 8329 // If there are @selector references added them to its pool. This is for 8330 // implementation of -Wselector. 8331 unsigned int DataSize = ReferencedSelectorsData.size()-1; 8332 unsigned I = 0; 8333 while (I < DataSize) { 8334 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]); 8335 SourceLocation SelLoc 8336 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]); 8337 Sels.push_back(std::make_pair(Sel, SelLoc)); 8338 } 8339 ReferencedSelectorsData.clear(); 8340 } 8341 8342 void ASTReader::ReadWeakUndeclaredIdentifiers( 8343 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) { 8344 if (WeakUndeclaredIdentifiers.empty()) 8345 return; 8346 8347 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) { 8348 IdentifierInfo *WeakId 8349 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8350 IdentifierInfo *AliasId 8351 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8352 SourceLocation Loc 8353 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]); 8354 bool Used = WeakUndeclaredIdentifiers[I++]; 8355 WeakInfo WI(AliasId, Loc); 8356 WI.setUsed(Used); 8357 WeakIDs.push_back(std::make_pair(WeakId, WI)); 8358 } 8359 WeakUndeclaredIdentifiers.clear(); 8360 } 8361 8362 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) { 8363 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) { 8364 ExternalVTableUse VT; 8365 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); 8366 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]); 8367 VT.DefinitionRequired = VTableUses[Idx++]; 8368 VTables.push_back(VT); 8369 } 8370 8371 VTableUses.clear(); 8372 } 8373 8374 void ASTReader::ReadPendingInstantiations( 8375 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) { 8376 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) { 8377 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++])); 8378 SourceLocation Loc 8379 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]); 8380 8381 Pending.push_back(std::make_pair(D, Loc)); 8382 } 8383 PendingInstantiations.clear(); 8384 } 8385 8386 void ASTReader::ReadLateParsedTemplates( 8387 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>> 8388 &LPTMap) { 8389 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N; 8390 /* In loop */) { 8391 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++])); 8392 8393 auto LT = std::make_unique<LateParsedTemplate>(); 8394 LT->D = GetDecl(LateParsedTemplates[Idx++]); 8395 8396 ModuleFile *F = getOwningModuleFile(LT->D); 8397 assert(F && "No module"); 8398 8399 unsigned TokN = LateParsedTemplates[Idx++]; 8400 LT->Toks.reserve(TokN); 8401 for (unsigned T = 0; T < TokN; ++T) 8402 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx)); 8403 8404 LPTMap.insert(std::make_pair(FD, std::move(LT))); 8405 } 8406 8407 LateParsedTemplates.clear(); 8408 } 8409 8410 void ASTReader::LoadSelector(Selector Sel) { 8411 // It would be complicated to avoid reading the methods anyway. So don't. 8412 ReadMethodPool(Sel); 8413 } 8414 8415 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) { 8416 assert(ID && "Non-zero identifier ID required"); 8417 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); 8418 IdentifiersLoaded[ID - 1] = II; 8419 if (DeserializationListener) 8420 DeserializationListener->IdentifierRead(ID, II); 8421 } 8422 8423 /// Set the globally-visible declarations associated with the given 8424 /// identifier. 8425 /// 8426 /// If the AST reader is currently in a state where the given declaration IDs 8427 /// cannot safely be resolved, they are queued until it is safe to resolve 8428 /// them. 8429 /// 8430 /// \param II an IdentifierInfo that refers to one or more globally-visible 8431 /// declarations. 8432 /// 8433 /// \param DeclIDs the set of declaration IDs with the name @p II that are 8434 /// visible at global scope. 8435 /// 8436 /// \param Decls if non-null, this vector will be populated with the set of 8437 /// deserialized declarations. These declarations will not be pushed into 8438 /// scope. 8439 void 8440 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, 8441 const SmallVectorImpl<uint32_t> &DeclIDs, 8442 SmallVectorImpl<Decl *> *Decls) { 8443 if (NumCurrentElementsDeserializing && !Decls) { 8444 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end()); 8445 return; 8446 } 8447 8448 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { 8449 if (!SemaObj) { 8450 // Queue this declaration so that it will be added to the 8451 // translation unit scope and identifier's declaration chain 8452 // once a Sema object is known. 8453 PreloadedDeclIDs.push_back(DeclIDs[I]); 8454 continue; 8455 } 8456 8457 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); 8458 8459 // If we're simply supposed to record the declarations, do so now. 8460 if (Decls) { 8461 Decls->push_back(D); 8462 continue; 8463 } 8464 8465 // Introduce this declaration into the translation-unit scope 8466 // and add it to the declaration chain for this identifier, so 8467 // that (unqualified) name lookup will find it. 8468 pushExternalDeclIntoScope(D, II); 8469 } 8470 } 8471 8472 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) { 8473 if (ID == 0) 8474 return nullptr; 8475 8476 if (IdentifiersLoaded.empty()) { 8477 Error("no identifier table in AST file"); 8478 return nullptr; 8479 } 8480 8481 ID -= 1; 8482 if (!IdentifiersLoaded[ID]) { 8483 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1); 8484 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map"); 8485 ModuleFile *M = I->second; 8486 unsigned Index = ID - M->BaseIdentifierID; 8487 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index]; 8488 8489 // All of the strings in the AST file are preceded by a 16-bit length. 8490 // Extract that 16-bit length to avoid having to execute strlen(). 8491 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as 8492 // unsigned integers. This is important to avoid integer overflow when 8493 // we cast them to 'unsigned'. 8494 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2; 8495 unsigned StrLen = (((unsigned) StrLenPtr[0]) 8496 | (((unsigned) StrLenPtr[1]) << 8)) - 1; 8497 auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen)); 8498 IdentifiersLoaded[ID] = &II; 8499 markIdentifierFromAST(*this, II); 8500 if (DeserializationListener) 8501 DeserializationListener->IdentifierRead(ID + 1, &II); 8502 } 8503 8504 return IdentifiersLoaded[ID]; 8505 } 8506 8507 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) { 8508 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID)); 8509 } 8510 8511 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) { 8512 if (LocalID < NUM_PREDEF_IDENT_IDS) 8513 return LocalID; 8514 8515 if (!M.ModuleOffsetMap.empty()) 8516 ReadModuleOffsetMap(M); 8517 8518 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8519 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS); 8520 assert(I != M.IdentifierRemap.end() 8521 && "Invalid index into identifier index remap"); 8522 8523 return LocalID + I->second; 8524 } 8525 8526 MacroInfo *ASTReader::getMacro(MacroID ID) { 8527 if (ID == 0) 8528 return nullptr; 8529 8530 if (MacrosLoaded.empty()) { 8531 Error("no macro table in AST file"); 8532 return nullptr; 8533 } 8534 8535 ID -= NUM_PREDEF_MACRO_IDS; 8536 if (!MacrosLoaded[ID]) { 8537 GlobalMacroMapType::iterator I 8538 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS); 8539 assert(I != GlobalMacroMap.end() && "Corrupted global macro map"); 8540 ModuleFile *M = I->second; 8541 unsigned Index = ID - M->BaseMacroID; 8542 MacrosLoaded[ID] = 8543 ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]); 8544 8545 if (DeserializationListener) 8546 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS, 8547 MacrosLoaded[ID]); 8548 } 8549 8550 return MacrosLoaded[ID]; 8551 } 8552 8553 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) { 8554 if (LocalID < NUM_PREDEF_MACRO_IDS) 8555 return LocalID; 8556 8557 if (!M.ModuleOffsetMap.empty()) 8558 ReadModuleOffsetMap(M); 8559 8560 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8561 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS); 8562 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap"); 8563 8564 return LocalID + I->second; 8565 } 8566 8567 serialization::SubmoduleID 8568 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) { 8569 if (LocalID < NUM_PREDEF_SUBMODULE_IDS) 8570 return LocalID; 8571 8572 if (!M.ModuleOffsetMap.empty()) 8573 ReadModuleOffsetMap(M); 8574 8575 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8576 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS); 8577 assert(I != M.SubmoduleRemap.end() 8578 && "Invalid index into submodule index remap"); 8579 8580 return LocalID + I->second; 8581 } 8582 8583 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) { 8584 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) { 8585 assert(GlobalID == 0 && "Unhandled global submodule ID"); 8586 return nullptr; 8587 } 8588 8589 if (GlobalID > SubmodulesLoaded.size()) { 8590 Error("submodule ID out of range in AST file"); 8591 return nullptr; 8592 } 8593 8594 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS]; 8595 } 8596 8597 Module *ASTReader::getModule(unsigned ID) { 8598 return getSubmodule(ID); 8599 } 8600 8601 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) { 8602 if (ID & 1) { 8603 // It's a module, look it up by submodule ID. 8604 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1)); 8605 return I == GlobalSubmoduleMap.end() ? nullptr : I->second; 8606 } else { 8607 // It's a prefix (preamble, PCH, ...). Look it up by index. 8608 unsigned IndexFromEnd = ID >> 1; 8609 assert(IndexFromEnd && "got reference to unknown module file"); 8610 return getModuleManager().pch_modules().end()[-IndexFromEnd]; 8611 } 8612 } 8613 8614 unsigned ASTReader::getModuleFileID(ModuleFile *F) { 8615 if (!F) 8616 return 1; 8617 8618 // For a file representing a module, use the submodule ID of the top-level 8619 // module as the file ID. For any other kind of file, the number of such 8620 // files loaded beforehand will be the same on reload. 8621 // FIXME: Is this true even if we have an explicit module file and a PCH? 8622 if (F->isModule()) 8623 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1; 8624 8625 auto PCHModules = getModuleManager().pch_modules(); 8626 auto I = llvm::find(PCHModules, F); 8627 assert(I != PCHModules.end() && "emitting reference to unknown file"); 8628 return (I - PCHModules.end()) << 1; 8629 } 8630 8631 llvm::Optional<ASTSourceDescriptor> 8632 ASTReader::getSourceDescriptor(unsigned ID) { 8633 if (Module *M = getSubmodule(ID)) 8634 return ASTSourceDescriptor(*M); 8635 8636 // If there is only a single PCH, return it instead. 8637 // Chained PCH are not supported. 8638 const auto &PCHChain = ModuleMgr.pch_modules(); 8639 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) { 8640 ModuleFile &MF = ModuleMgr.getPrimaryModule(); 8641 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName); 8642 StringRef FileName = llvm::sys::path::filename(MF.FileName); 8643 return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName, 8644 MF.Signature); 8645 } 8646 return None; 8647 } 8648 8649 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) { 8650 auto I = DefinitionSource.find(FD); 8651 if (I == DefinitionSource.end()) 8652 return EK_ReplyHazy; 8653 return I->second ? EK_Never : EK_Always; 8654 } 8655 8656 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) { 8657 return DecodeSelector(getGlobalSelectorID(M, LocalID)); 8658 } 8659 8660 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) { 8661 if (ID == 0) 8662 return Selector(); 8663 8664 if (ID > SelectorsLoaded.size()) { 8665 Error("selector ID out of range in AST file"); 8666 return Selector(); 8667 } 8668 8669 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) { 8670 // Load this selector from the selector table. 8671 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID); 8672 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map"); 8673 ModuleFile &M = *I->second; 8674 ASTSelectorLookupTrait Trait(*this, M); 8675 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS; 8676 SelectorsLoaded[ID - 1] = 8677 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0); 8678 if (DeserializationListener) 8679 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 8680 } 8681 8682 return SelectorsLoaded[ID - 1]; 8683 } 8684 8685 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) { 8686 return DecodeSelector(ID); 8687 } 8688 8689 uint32_t ASTReader::GetNumExternalSelectors() { 8690 // ID 0 (the null selector) is considered an external selector. 8691 return getTotalNumSelectors() + 1; 8692 } 8693 8694 serialization::SelectorID 8695 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const { 8696 if (LocalID < NUM_PREDEF_SELECTOR_IDS) 8697 return LocalID; 8698 8699 if (!M.ModuleOffsetMap.empty()) 8700 ReadModuleOffsetMap(M); 8701 8702 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8703 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS); 8704 assert(I != M.SelectorRemap.end() 8705 && "Invalid index into selector index remap"); 8706 8707 return LocalID + I->second; 8708 } 8709 8710 DeclarationNameLoc 8711 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) { 8712 DeclarationNameLoc DNLoc; 8713 switch (Name.getNameKind()) { 8714 case DeclarationName::CXXConstructorName: 8715 case DeclarationName::CXXDestructorName: 8716 case DeclarationName::CXXConversionFunctionName: 8717 DNLoc.NamedType.TInfo = readTypeSourceInfo(); 8718 break; 8719 8720 case DeclarationName::CXXOperatorName: 8721 DNLoc.CXXOperatorName.BeginOpNameLoc 8722 = readSourceLocation().getRawEncoding(); 8723 DNLoc.CXXOperatorName.EndOpNameLoc 8724 = readSourceLocation().getRawEncoding(); 8725 break; 8726 8727 case DeclarationName::CXXLiteralOperatorName: 8728 DNLoc.CXXLiteralOperatorName.OpNameLoc 8729 = readSourceLocation().getRawEncoding(); 8730 break; 8731 8732 case DeclarationName::Identifier: 8733 case DeclarationName::ObjCZeroArgSelector: 8734 case DeclarationName::ObjCOneArgSelector: 8735 case DeclarationName::ObjCMultiArgSelector: 8736 case DeclarationName::CXXUsingDirective: 8737 case DeclarationName::CXXDeductionGuideName: 8738 break; 8739 } 8740 return DNLoc; 8741 } 8742 8743 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() { 8744 DeclarationNameInfo NameInfo; 8745 NameInfo.setName(readDeclarationName()); 8746 NameInfo.setLoc(readSourceLocation()); 8747 NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName())); 8748 return NameInfo; 8749 } 8750 8751 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) { 8752 Info.QualifierLoc = readNestedNameSpecifierLoc(); 8753 unsigned NumTPLists = readInt(); 8754 Info.NumTemplParamLists = NumTPLists; 8755 if (NumTPLists) { 8756 Info.TemplParamLists = 8757 new (getContext()) TemplateParameterList *[NumTPLists]; 8758 for (unsigned i = 0; i != NumTPLists; ++i) 8759 Info.TemplParamLists[i] = readTemplateParameterList(); 8760 } 8761 } 8762 8763 TemplateParameterList * 8764 ASTRecordReader::readTemplateParameterList() { 8765 SourceLocation TemplateLoc = readSourceLocation(); 8766 SourceLocation LAngleLoc = readSourceLocation(); 8767 SourceLocation RAngleLoc = readSourceLocation(); 8768 8769 unsigned NumParams = readInt(); 8770 SmallVector<NamedDecl *, 16> Params; 8771 Params.reserve(NumParams); 8772 while (NumParams--) 8773 Params.push_back(readDeclAs<NamedDecl>()); 8774 8775 bool HasRequiresClause = readBool(); 8776 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr; 8777 8778 TemplateParameterList *TemplateParams = TemplateParameterList::Create( 8779 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause); 8780 return TemplateParams; 8781 } 8782 8783 void ASTRecordReader::readTemplateArgumentList( 8784 SmallVectorImpl<TemplateArgument> &TemplArgs, 8785 bool Canonicalize) { 8786 unsigned NumTemplateArgs = readInt(); 8787 TemplArgs.reserve(NumTemplateArgs); 8788 while (NumTemplateArgs--) 8789 TemplArgs.push_back(readTemplateArgument(Canonicalize)); 8790 } 8791 8792 /// Read a UnresolvedSet structure. 8793 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) { 8794 unsigned NumDecls = readInt(); 8795 Set.reserve(getContext(), NumDecls); 8796 while (NumDecls--) { 8797 DeclID ID = readDeclID(); 8798 AccessSpecifier AS = (AccessSpecifier) readInt(); 8799 Set.addLazyDecl(getContext(), ID, AS); 8800 } 8801 } 8802 8803 CXXBaseSpecifier 8804 ASTRecordReader::readCXXBaseSpecifier() { 8805 bool isVirtual = readBool(); 8806 bool isBaseOfClass = readBool(); 8807 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt()); 8808 bool inheritConstructors = readBool(); 8809 TypeSourceInfo *TInfo = readTypeSourceInfo(); 8810 SourceRange Range = readSourceRange(); 8811 SourceLocation EllipsisLoc = readSourceLocation(); 8812 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo, 8813 EllipsisLoc); 8814 Result.setInheritConstructors(inheritConstructors); 8815 return Result; 8816 } 8817 8818 CXXCtorInitializer ** 8819 ASTRecordReader::readCXXCtorInitializers() { 8820 ASTContext &Context = getContext(); 8821 unsigned NumInitializers = readInt(); 8822 assert(NumInitializers && "wrote ctor initializers but have no inits"); 8823 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers]; 8824 for (unsigned i = 0; i != NumInitializers; ++i) { 8825 TypeSourceInfo *TInfo = nullptr; 8826 bool IsBaseVirtual = false; 8827 FieldDecl *Member = nullptr; 8828 IndirectFieldDecl *IndirectMember = nullptr; 8829 8830 CtorInitializerType Type = (CtorInitializerType) readInt(); 8831 switch (Type) { 8832 case CTOR_INITIALIZER_BASE: 8833 TInfo = readTypeSourceInfo(); 8834 IsBaseVirtual = readBool(); 8835 break; 8836 8837 case CTOR_INITIALIZER_DELEGATING: 8838 TInfo = readTypeSourceInfo(); 8839 break; 8840 8841 case CTOR_INITIALIZER_MEMBER: 8842 Member = readDeclAs<FieldDecl>(); 8843 break; 8844 8845 case CTOR_INITIALIZER_INDIRECT_MEMBER: 8846 IndirectMember = readDeclAs<IndirectFieldDecl>(); 8847 break; 8848 } 8849 8850 SourceLocation MemberOrEllipsisLoc = readSourceLocation(); 8851 Expr *Init = readExpr(); 8852 SourceLocation LParenLoc = readSourceLocation(); 8853 SourceLocation RParenLoc = readSourceLocation(); 8854 8855 CXXCtorInitializer *BOMInit; 8856 if (Type == CTOR_INITIALIZER_BASE) 8857 BOMInit = new (Context) 8858 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init, 8859 RParenLoc, MemberOrEllipsisLoc); 8860 else if (Type == CTOR_INITIALIZER_DELEGATING) 8861 BOMInit = new (Context) 8862 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc); 8863 else if (Member) 8864 BOMInit = new (Context) 8865 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc, 8866 Init, RParenLoc); 8867 else 8868 BOMInit = new (Context) 8869 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc, 8870 LParenLoc, Init, RParenLoc); 8871 8872 if (/*IsWritten*/readBool()) { 8873 unsigned SourceOrder = readInt(); 8874 BOMInit->setSourceOrder(SourceOrder); 8875 } 8876 8877 CtorInitializers[i] = BOMInit; 8878 } 8879 8880 return CtorInitializers; 8881 } 8882 8883 NestedNameSpecifierLoc 8884 ASTRecordReader::readNestedNameSpecifierLoc() { 8885 ASTContext &Context = getContext(); 8886 unsigned N = readInt(); 8887 NestedNameSpecifierLocBuilder Builder; 8888 for (unsigned I = 0; I != N; ++I) { 8889 auto Kind = readNestedNameSpecifierKind(); 8890 switch (Kind) { 8891 case NestedNameSpecifier::Identifier: { 8892 IdentifierInfo *II = readIdentifier(); 8893 SourceRange Range = readSourceRange(); 8894 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd()); 8895 break; 8896 } 8897 8898 case NestedNameSpecifier::Namespace: { 8899 NamespaceDecl *NS = readDeclAs<NamespaceDecl>(); 8900 SourceRange Range = readSourceRange(); 8901 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd()); 8902 break; 8903 } 8904 8905 case NestedNameSpecifier::NamespaceAlias: { 8906 NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>(); 8907 SourceRange Range = readSourceRange(); 8908 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd()); 8909 break; 8910 } 8911 8912 case NestedNameSpecifier::TypeSpec: 8913 case NestedNameSpecifier::TypeSpecWithTemplate: { 8914 bool Template = readBool(); 8915 TypeSourceInfo *T = readTypeSourceInfo(); 8916 if (!T) 8917 return NestedNameSpecifierLoc(); 8918 SourceLocation ColonColonLoc = readSourceLocation(); 8919 8920 // FIXME: 'template' keyword location not saved anywhere, so we fake it. 8921 Builder.Extend(Context, 8922 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(), 8923 T->getTypeLoc(), ColonColonLoc); 8924 break; 8925 } 8926 8927 case NestedNameSpecifier::Global: { 8928 SourceLocation ColonColonLoc = readSourceLocation(); 8929 Builder.MakeGlobal(Context, ColonColonLoc); 8930 break; 8931 } 8932 8933 case NestedNameSpecifier::Super: { 8934 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>(); 8935 SourceRange Range = readSourceRange(); 8936 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd()); 8937 break; 8938 } 8939 } 8940 } 8941 8942 return Builder.getWithLocInContext(Context); 8943 } 8944 8945 SourceRange 8946 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record, 8947 unsigned &Idx) { 8948 SourceLocation beg = ReadSourceLocation(F, Record, Idx); 8949 SourceLocation end = ReadSourceLocation(F, Record, Idx); 8950 return SourceRange(beg, end); 8951 } 8952 8953 static llvm::FixedPointSemantics 8954 ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record, 8955 unsigned &Idx) { 8956 unsigned Width = Record[Idx++]; 8957 unsigned Scale = Record[Idx++]; 8958 uint64_t Tmp = Record[Idx++]; 8959 bool IsSigned = Tmp & 0x1; 8960 bool IsSaturated = Tmp & 0x2; 8961 bool HasUnsignedPadding = Tmp & 0x4; 8962 return llvm::FixedPointSemantics(Width, Scale, IsSigned, IsSaturated, 8963 HasUnsignedPadding); 8964 } 8965 8966 static const llvm::fltSemantics & 8967 readAPFloatSemantics(ASTRecordReader &reader) { 8968 return llvm::APFloatBase::EnumToSemantics( 8969 static_cast<llvm::APFloatBase::Semantics>(reader.readInt())); 8970 } 8971 8972 APValue ASTRecordReader::readAPValue() { 8973 unsigned Kind = readInt(); 8974 switch ((APValue::ValueKind) Kind) { 8975 case APValue::None: 8976 return APValue(); 8977 case APValue::Indeterminate: 8978 return APValue::IndeterminateValue(); 8979 case APValue::Int: 8980 return APValue(readAPSInt()); 8981 case APValue::Float: { 8982 const llvm::fltSemantics &FloatSema = readAPFloatSemantics(*this); 8983 return APValue(readAPFloat(FloatSema)); 8984 } 8985 case APValue::FixedPoint: { 8986 llvm::FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx); 8987 return APValue(llvm::APFixedPoint(readAPInt(), FPSema)); 8988 } 8989 case APValue::ComplexInt: { 8990 llvm::APSInt First = readAPSInt(); 8991 return APValue(std::move(First), readAPSInt()); 8992 } 8993 case APValue::ComplexFloat: { 8994 const llvm::fltSemantics &FloatSema1 = readAPFloatSemantics(*this); 8995 llvm::APFloat First = readAPFloat(FloatSema1); 8996 const llvm::fltSemantics &FloatSema2 = readAPFloatSemantics(*this); 8997 return APValue(std::move(First), readAPFloat(FloatSema2)); 8998 } 8999 case APValue::LValue: 9000 case APValue::Vector: 9001 case APValue::Array: 9002 case APValue::Struct: 9003 case APValue::Union: 9004 case APValue::MemberPointer: 9005 case APValue::AddrLabelDiff: 9006 // TODO : Handle all these APValue::ValueKind. 9007 return APValue(); 9008 } 9009 llvm_unreachable("Invalid APValue::ValueKind"); 9010 } 9011 9012 /// Read a floating-point value 9013 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) { 9014 return llvm::APFloat(Sem, readAPInt()); 9015 } 9016 9017 // Read a string 9018 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) { 9019 unsigned Len = Record[Idx++]; 9020 std::string Result(Record.data() + Idx, Record.data() + Idx + Len); 9021 Idx += Len; 9022 return Result; 9023 } 9024 9025 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record, 9026 unsigned &Idx) { 9027 std::string Filename = ReadString(Record, Idx); 9028 ResolveImportedPath(F, Filename); 9029 return Filename; 9030 } 9031 9032 std::string ASTReader::ReadPath(StringRef BaseDirectory, 9033 const RecordData &Record, unsigned &Idx) { 9034 std::string Filename = ReadString(Record, Idx); 9035 if (!BaseDirectory.empty()) 9036 ResolveImportedPath(Filename, BaseDirectory); 9037 return Filename; 9038 } 9039 9040 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record, 9041 unsigned &Idx) { 9042 unsigned Major = Record[Idx++]; 9043 unsigned Minor = Record[Idx++]; 9044 unsigned Subminor = Record[Idx++]; 9045 if (Minor == 0) 9046 return VersionTuple(Major); 9047 if (Subminor == 0) 9048 return VersionTuple(Major, Minor - 1); 9049 return VersionTuple(Major, Minor - 1, Subminor - 1); 9050 } 9051 9052 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F, 9053 const RecordData &Record, 9054 unsigned &Idx) { 9055 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx); 9056 return CXXTemporary::Create(getContext(), Decl); 9057 } 9058 9059 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const { 9060 return Diag(CurrentImportLoc, DiagID); 9061 } 9062 9063 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const { 9064 return Diags.Report(Loc, DiagID); 9065 } 9066 9067 /// Retrieve the identifier table associated with the 9068 /// preprocessor. 9069 IdentifierTable &ASTReader::getIdentifierTable() { 9070 return PP.getIdentifierTable(); 9071 } 9072 9073 /// Record that the given ID maps to the given switch-case 9074 /// statement. 9075 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { 9076 assert((*CurrSwitchCaseStmts)[ID] == nullptr && 9077 "Already have a SwitchCase with this ID"); 9078 (*CurrSwitchCaseStmts)[ID] = SC; 9079 } 9080 9081 /// Retrieve the switch-case statement with the given ID. 9082 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) { 9083 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID"); 9084 return (*CurrSwitchCaseStmts)[ID]; 9085 } 9086 9087 void ASTReader::ClearSwitchCaseIDs() { 9088 CurrSwitchCaseStmts->clear(); 9089 } 9090 9091 void ASTReader::ReadComments() { 9092 ASTContext &Context = getContext(); 9093 std::vector<RawComment *> Comments; 9094 for (SmallVectorImpl<std::pair<BitstreamCursor, 9095 serialization::ModuleFile *>>::iterator 9096 I = CommentsCursors.begin(), 9097 E = CommentsCursors.end(); 9098 I != E; ++I) { 9099 Comments.clear(); 9100 BitstreamCursor &Cursor = I->first; 9101 serialization::ModuleFile &F = *I->second; 9102 SavedStreamPosition SavedPosition(Cursor); 9103 9104 RecordData Record; 9105 while (true) { 9106 Expected<llvm::BitstreamEntry> MaybeEntry = 9107 Cursor.advanceSkippingSubblocks( 9108 BitstreamCursor::AF_DontPopBlockAtEnd); 9109 if (!MaybeEntry) { 9110 Error(MaybeEntry.takeError()); 9111 return; 9112 } 9113 llvm::BitstreamEntry Entry = MaybeEntry.get(); 9114 9115 switch (Entry.Kind) { 9116 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 9117 case llvm::BitstreamEntry::Error: 9118 Error("malformed block record in AST file"); 9119 return; 9120 case llvm::BitstreamEntry::EndBlock: 9121 goto NextCursor; 9122 case llvm::BitstreamEntry::Record: 9123 // The interesting case. 9124 break; 9125 } 9126 9127 // Read a record. 9128 Record.clear(); 9129 Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record); 9130 if (!MaybeComment) { 9131 Error(MaybeComment.takeError()); 9132 return; 9133 } 9134 switch ((CommentRecordTypes)MaybeComment.get()) { 9135 case COMMENTS_RAW_COMMENT: { 9136 unsigned Idx = 0; 9137 SourceRange SR = ReadSourceRange(F, Record, Idx); 9138 RawComment::CommentKind Kind = 9139 (RawComment::CommentKind) Record[Idx++]; 9140 bool IsTrailingComment = Record[Idx++]; 9141 bool IsAlmostTrailingComment = Record[Idx++]; 9142 Comments.push_back(new (Context) RawComment( 9143 SR, Kind, IsTrailingComment, IsAlmostTrailingComment)); 9144 break; 9145 } 9146 } 9147 } 9148 NextCursor: 9149 llvm::DenseMap<FileID, std::map<unsigned, RawComment *>> 9150 FileToOffsetToComment; 9151 for (RawComment *C : Comments) { 9152 SourceLocation CommentLoc = C->getBeginLoc(); 9153 if (CommentLoc.isValid()) { 9154 std::pair<FileID, unsigned> Loc = 9155 SourceMgr.getDecomposedLoc(CommentLoc); 9156 if (Loc.first.isValid()) 9157 Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C); 9158 } 9159 } 9160 } 9161 } 9162 9163 void ASTReader::visitInputFiles(serialization::ModuleFile &MF, 9164 bool IncludeSystem, bool Complain, 9165 llvm::function_ref<void(const serialization::InputFile &IF, 9166 bool isSystem)> Visitor) { 9167 unsigned NumUserInputs = MF.NumUserInputFiles; 9168 unsigned NumInputs = MF.InputFilesLoaded.size(); 9169 assert(NumUserInputs <= NumInputs); 9170 unsigned N = IncludeSystem ? NumInputs : NumUserInputs; 9171 for (unsigned I = 0; I < N; ++I) { 9172 bool IsSystem = I >= NumUserInputs; 9173 InputFile IF = getInputFile(MF, I+1, Complain); 9174 Visitor(IF, IsSystem); 9175 } 9176 } 9177 9178 void ASTReader::visitTopLevelModuleMaps( 9179 serialization::ModuleFile &MF, 9180 llvm::function_ref<void(const FileEntry *FE)> Visitor) { 9181 unsigned NumInputs = MF.InputFilesLoaded.size(); 9182 for (unsigned I = 0; I < NumInputs; ++I) { 9183 InputFileInfo IFI = readInputFileInfo(MF, I + 1); 9184 if (IFI.TopLevelModuleMap) 9185 // FIXME: This unnecessarily re-reads the InputFileInfo. 9186 if (auto *FE = getInputFile(MF, I + 1).getFile()) 9187 Visitor(FE); 9188 } 9189 } 9190 9191 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) { 9192 // If we know the owning module, use it. 9193 if (Module *M = D->getImportedOwningModule()) 9194 return M->getFullModuleName(); 9195 9196 // Otherwise, use the name of the top-level module the decl is within. 9197 if (ModuleFile *M = getOwningModuleFile(D)) 9198 return M->ModuleName; 9199 9200 // Not from a module. 9201 return {}; 9202 } 9203 9204 void ASTReader::finishPendingActions() { 9205 while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() || 9206 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() || 9207 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() || 9208 !PendingUpdateRecords.empty()) { 9209 // If any identifiers with corresponding top-level declarations have 9210 // been loaded, load those declarations now. 9211 using TopLevelDeclsMap = 9212 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>; 9213 TopLevelDeclsMap TopLevelDecls; 9214 9215 while (!PendingIdentifierInfos.empty()) { 9216 IdentifierInfo *II = PendingIdentifierInfos.back().first; 9217 SmallVector<uint32_t, 4> DeclIDs = 9218 std::move(PendingIdentifierInfos.back().second); 9219 PendingIdentifierInfos.pop_back(); 9220 9221 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]); 9222 } 9223 9224 // Load each function type that we deferred loading because it was a 9225 // deduced type that might refer to a local type declared within itself. 9226 for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) { 9227 auto *FD = PendingFunctionTypes[I].first; 9228 FD->setType(GetType(PendingFunctionTypes[I].second)); 9229 9230 // If we gave a function a deduced return type, remember that we need to 9231 // propagate that along the redeclaration chain. 9232 auto *DT = FD->getReturnType()->getContainedDeducedType(); 9233 if (DT && DT->isDeduced()) 9234 PendingDeducedTypeUpdates.insert( 9235 {FD->getCanonicalDecl(), FD->getReturnType()}); 9236 } 9237 PendingFunctionTypes.clear(); 9238 9239 // For each decl chain that we wanted to complete while deserializing, mark 9240 // it as "still needs to be completed". 9241 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) { 9242 markIncompleteDeclChain(PendingIncompleteDeclChains[I]); 9243 } 9244 PendingIncompleteDeclChains.clear(); 9245 9246 // Load pending declaration chains. 9247 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) 9248 loadPendingDeclChain(PendingDeclChains[I].first, 9249 PendingDeclChains[I].second); 9250 PendingDeclChains.clear(); 9251 9252 // Make the most recent of the top-level declarations visible. 9253 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(), 9254 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) { 9255 IdentifierInfo *II = TLD->first; 9256 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) { 9257 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II); 9258 } 9259 } 9260 9261 // Load any pending macro definitions. 9262 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) { 9263 IdentifierInfo *II = PendingMacroIDs.begin()[I].first; 9264 SmallVector<PendingMacroInfo, 2> GlobalIDs; 9265 GlobalIDs.swap(PendingMacroIDs.begin()[I].second); 9266 // Initialize the macro history from chained-PCHs ahead of module imports. 9267 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9268 ++IDIdx) { 9269 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9270 if (!Info.M->isModule()) 9271 resolvePendingMacro(II, Info); 9272 } 9273 // Handle module imports. 9274 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9275 ++IDIdx) { 9276 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9277 if (Info.M->isModule()) 9278 resolvePendingMacro(II, Info); 9279 } 9280 } 9281 PendingMacroIDs.clear(); 9282 9283 // Wire up the DeclContexts for Decls that we delayed setting until 9284 // recursive loading is completed. 9285 while (!PendingDeclContextInfos.empty()) { 9286 PendingDeclContextInfo Info = PendingDeclContextInfos.front(); 9287 PendingDeclContextInfos.pop_front(); 9288 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC)); 9289 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC)); 9290 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext()); 9291 } 9292 9293 // Perform any pending declaration updates. 9294 while (!PendingUpdateRecords.empty()) { 9295 auto Update = PendingUpdateRecords.pop_back_val(); 9296 ReadingKindTracker ReadingKind(Read_Decl, *this); 9297 loadDeclUpdateRecords(Update); 9298 } 9299 } 9300 9301 // At this point, all update records for loaded decls are in place, so any 9302 // fake class definitions should have become real. 9303 assert(PendingFakeDefinitionData.empty() && 9304 "faked up a class definition but never saw the real one"); 9305 9306 // If we deserialized any C++ or Objective-C class definitions, any 9307 // Objective-C protocol definitions, or any redeclarable templates, make sure 9308 // that all redeclarations point to the definitions. Note that this can only 9309 // happen now, after the redeclaration chains have been fully wired. 9310 for (Decl *D : PendingDefinitions) { 9311 if (TagDecl *TD = dyn_cast<TagDecl>(D)) { 9312 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) { 9313 // Make sure that the TagType points at the definition. 9314 const_cast<TagType*>(TagT)->decl = TD; 9315 } 9316 9317 if (auto RD = dyn_cast<CXXRecordDecl>(D)) { 9318 for (auto *R = getMostRecentExistingDecl(RD); R; 9319 R = R->getPreviousDecl()) { 9320 assert((R == D) == 9321 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() && 9322 "declaration thinks it's the definition but it isn't"); 9323 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData; 9324 } 9325 } 9326 9327 continue; 9328 } 9329 9330 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) { 9331 // Make sure that the ObjCInterfaceType points at the definition. 9332 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl)) 9333 ->Decl = ID; 9334 9335 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl()) 9336 cast<ObjCInterfaceDecl>(R)->Data = ID->Data; 9337 9338 continue; 9339 } 9340 9341 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) { 9342 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl()) 9343 cast<ObjCProtocolDecl>(R)->Data = PD->Data; 9344 9345 continue; 9346 } 9347 9348 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl(); 9349 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl()) 9350 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common; 9351 } 9352 PendingDefinitions.clear(); 9353 9354 // Load the bodies of any functions or methods we've encountered. We do 9355 // this now (delayed) so that we can be sure that the declaration chains 9356 // have been fully wired up (hasBody relies on this). 9357 // FIXME: We shouldn't require complete redeclaration chains here. 9358 for (PendingBodiesMap::iterator PB = PendingBodies.begin(), 9359 PBEnd = PendingBodies.end(); 9360 PB != PBEnd; ++PB) { 9361 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) { 9362 // For a function defined inline within a class template, force the 9363 // canonical definition to be the one inside the canonical definition of 9364 // the template. This ensures that we instantiate from a correct view 9365 // of the template. 9366 // 9367 // Sadly we can't do this more generally: we can't be sure that all 9368 // copies of an arbitrary class definition will have the same members 9369 // defined (eg, some member functions may not be instantiated, and some 9370 // special members may or may not have been implicitly defined). 9371 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent())) 9372 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition()) 9373 continue; 9374 9375 // FIXME: Check for =delete/=default? 9376 // FIXME: Complain about ODR violations here? 9377 const FunctionDecl *Defn = nullptr; 9378 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) { 9379 FD->setLazyBody(PB->second); 9380 } else { 9381 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn); 9382 mergeDefinitionVisibility(NonConstDefn, FD); 9383 9384 if (!FD->isLateTemplateParsed() && 9385 !NonConstDefn->isLateTemplateParsed() && 9386 FD->getODRHash() != NonConstDefn->getODRHash()) { 9387 if (!isa<CXXMethodDecl>(FD)) { 9388 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9389 } else if (FD->getLexicalParent()->isFileContext() && 9390 NonConstDefn->getLexicalParent()->isFileContext()) { 9391 // Only diagnose out-of-line method definitions. If they are 9392 // in class definitions, then an error will be generated when 9393 // processing the class bodies. 9394 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9395 } 9396 } 9397 } 9398 continue; 9399 } 9400 9401 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first); 9402 if (!getContext().getLangOpts().Modules || !MD->hasBody()) 9403 MD->setLazyBody(PB->second); 9404 } 9405 PendingBodies.clear(); 9406 9407 // Do some cleanup. 9408 for (auto *ND : PendingMergedDefinitionsToDeduplicate) 9409 getContext().deduplicateMergedDefinitonsFor(ND); 9410 PendingMergedDefinitionsToDeduplicate.clear(); 9411 } 9412 9413 void ASTReader::diagnoseOdrViolations() { 9414 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() && 9415 PendingFunctionOdrMergeFailures.empty() && 9416 PendingEnumOdrMergeFailures.empty()) 9417 return; 9418 9419 // Trigger the import of the full definition of each class that had any 9420 // odr-merging problems, so we can produce better diagnostics for them. 9421 // These updates may in turn find and diagnose some ODR failures, so take 9422 // ownership of the set first. 9423 auto OdrMergeFailures = std::move(PendingOdrMergeFailures); 9424 PendingOdrMergeFailures.clear(); 9425 for (auto &Merge : OdrMergeFailures) { 9426 Merge.first->buildLookup(); 9427 Merge.first->decls_begin(); 9428 Merge.first->bases_begin(); 9429 Merge.first->vbases_begin(); 9430 for (auto &RecordPair : Merge.second) { 9431 auto *RD = RecordPair.first; 9432 RD->decls_begin(); 9433 RD->bases_begin(); 9434 RD->vbases_begin(); 9435 } 9436 } 9437 9438 // Trigger the import of functions. 9439 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures); 9440 PendingFunctionOdrMergeFailures.clear(); 9441 for (auto &Merge : FunctionOdrMergeFailures) { 9442 Merge.first->buildLookup(); 9443 Merge.first->decls_begin(); 9444 Merge.first->getBody(); 9445 for (auto &FD : Merge.second) { 9446 FD->buildLookup(); 9447 FD->decls_begin(); 9448 FD->getBody(); 9449 } 9450 } 9451 9452 // Trigger the import of enums. 9453 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures); 9454 PendingEnumOdrMergeFailures.clear(); 9455 for (auto &Merge : EnumOdrMergeFailures) { 9456 Merge.first->decls_begin(); 9457 for (auto &Enum : Merge.second) { 9458 Enum->decls_begin(); 9459 } 9460 } 9461 9462 // For each declaration from a merged context, check that the canonical 9463 // definition of that context also contains a declaration of the same 9464 // entity. 9465 // 9466 // Caution: this loop does things that might invalidate iterators into 9467 // PendingOdrMergeChecks. Don't turn this into a range-based for loop! 9468 while (!PendingOdrMergeChecks.empty()) { 9469 NamedDecl *D = PendingOdrMergeChecks.pop_back_val(); 9470 9471 // FIXME: Skip over implicit declarations for now. This matters for things 9472 // like implicitly-declared special member functions. This isn't entirely 9473 // correct; we can end up with multiple unmerged declarations of the same 9474 // implicit entity. 9475 if (D->isImplicit()) 9476 continue; 9477 9478 DeclContext *CanonDef = D->getDeclContext(); 9479 9480 bool Found = false; 9481 const Decl *DCanon = D->getCanonicalDecl(); 9482 9483 for (auto RI : D->redecls()) { 9484 if (RI->getLexicalDeclContext() == CanonDef) { 9485 Found = true; 9486 break; 9487 } 9488 } 9489 if (Found) 9490 continue; 9491 9492 // Quick check failed, time to do the slow thing. Note, we can't just 9493 // look up the name of D in CanonDef here, because the member that is 9494 // in CanonDef might not be found by name lookup (it might have been 9495 // replaced by a more recent declaration in the lookup table), and we 9496 // can't necessarily find it in the redeclaration chain because it might 9497 // be merely mergeable, not redeclarable. 9498 llvm::SmallVector<const NamedDecl*, 4> Candidates; 9499 for (auto *CanonMember : CanonDef->decls()) { 9500 if (CanonMember->getCanonicalDecl() == DCanon) { 9501 // This can happen if the declaration is merely mergeable and not 9502 // actually redeclarable (we looked for redeclarations earlier). 9503 // 9504 // FIXME: We should be able to detect this more efficiently, without 9505 // pulling in all of the members of CanonDef. 9506 Found = true; 9507 break; 9508 } 9509 if (auto *ND = dyn_cast<NamedDecl>(CanonMember)) 9510 if (ND->getDeclName() == D->getDeclName()) 9511 Candidates.push_back(ND); 9512 } 9513 9514 if (!Found) { 9515 // The AST doesn't like TagDecls becoming invalid after they've been 9516 // completed. We only really need to mark FieldDecls as invalid here. 9517 if (!isa<TagDecl>(D)) 9518 D->setInvalidDecl(); 9519 9520 // Ensure we don't accidentally recursively enter deserialization while 9521 // we're producing our diagnostic. 9522 Deserializing RecursionGuard(this); 9523 9524 std::string CanonDefModule = 9525 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef)); 9526 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl) 9527 << D << getOwningModuleNameForDiagnostic(D) 9528 << CanonDef << CanonDefModule.empty() << CanonDefModule; 9529 9530 if (Candidates.empty()) 9531 Diag(cast<Decl>(CanonDef)->getLocation(), 9532 diag::note_module_odr_violation_no_possible_decls) << D; 9533 else { 9534 for (unsigned I = 0, N = Candidates.size(); I != N; ++I) 9535 Diag(Candidates[I]->getLocation(), 9536 diag::note_module_odr_violation_possible_decl) 9537 << Candidates[I]; 9538 } 9539 9540 DiagnosedOdrMergeFailures.insert(CanonDef); 9541 } 9542 } 9543 9544 if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() && 9545 EnumOdrMergeFailures.empty()) 9546 return; 9547 9548 // Ensure we don't accidentally recursively enter deserialization while 9549 // we're producing our diagnostics. 9550 Deserializing RecursionGuard(this); 9551 9552 // Common code for hashing helpers. 9553 ODRHash Hash; 9554 auto ComputeQualTypeODRHash = [&Hash](QualType Ty) { 9555 Hash.clear(); 9556 Hash.AddQualType(Ty); 9557 return Hash.CalculateHash(); 9558 }; 9559 9560 auto ComputeODRHash = [&Hash](const Stmt *S) { 9561 assert(S); 9562 Hash.clear(); 9563 Hash.AddStmt(S); 9564 return Hash.CalculateHash(); 9565 }; 9566 9567 auto ComputeSubDeclODRHash = [&Hash](const Decl *D) { 9568 assert(D); 9569 Hash.clear(); 9570 Hash.AddSubDecl(D); 9571 return Hash.CalculateHash(); 9572 }; 9573 9574 auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) { 9575 Hash.clear(); 9576 Hash.AddTemplateArgument(TA); 9577 return Hash.CalculateHash(); 9578 }; 9579 9580 auto ComputeTemplateParameterListODRHash = 9581 [&Hash](const TemplateParameterList *TPL) { 9582 assert(TPL); 9583 Hash.clear(); 9584 Hash.AddTemplateParameterList(TPL); 9585 return Hash.CalculateHash(); 9586 }; 9587 9588 // Used with err_module_odr_violation_mismatch_decl and 9589 // note_module_odr_violation_mismatch_decl 9590 // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed 9591 enum ODRMismatchDecl { 9592 EndOfClass, 9593 PublicSpecifer, 9594 PrivateSpecifer, 9595 ProtectedSpecifer, 9596 StaticAssert, 9597 Field, 9598 CXXMethod, 9599 TypeAlias, 9600 TypeDef, 9601 Var, 9602 Friend, 9603 FunctionTemplate, 9604 Other 9605 }; 9606 9607 // Used with err_module_odr_violation_mismatch_decl_diff and 9608 // note_module_odr_violation_mismatch_decl_diff 9609 enum ODRMismatchDeclDifference { 9610 StaticAssertCondition, 9611 StaticAssertMessage, 9612 StaticAssertOnlyMessage, 9613 FieldName, 9614 FieldTypeName, 9615 FieldSingleBitField, 9616 FieldDifferentWidthBitField, 9617 FieldSingleMutable, 9618 FieldSingleInitializer, 9619 FieldDifferentInitializers, 9620 MethodName, 9621 MethodDeleted, 9622 MethodDefaulted, 9623 MethodVirtual, 9624 MethodStatic, 9625 MethodVolatile, 9626 MethodConst, 9627 MethodInline, 9628 MethodNumberParameters, 9629 MethodParameterType, 9630 MethodParameterName, 9631 MethodParameterSingleDefaultArgument, 9632 MethodParameterDifferentDefaultArgument, 9633 MethodNoTemplateArguments, 9634 MethodDifferentNumberTemplateArguments, 9635 MethodDifferentTemplateArgument, 9636 MethodSingleBody, 9637 MethodDifferentBody, 9638 TypedefName, 9639 TypedefType, 9640 VarName, 9641 VarType, 9642 VarSingleInitializer, 9643 VarDifferentInitializer, 9644 VarConstexpr, 9645 FriendTypeFunction, 9646 FriendType, 9647 FriendFunction, 9648 FunctionTemplateDifferentNumberParameters, 9649 FunctionTemplateParameterDifferentKind, 9650 FunctionTemplateParameterName, 9651 FunctionTemplateParameterSingleDefaultArgument, 9652 FunctionTemplateParameterDifferentDefaultArgument, 9653 FunctionTemplateParameterDifferentType, 9654 FunctionTemplatePackParameter, 9655 }; 9656 9657 // These lambdas have the common portions of the ODR diagnostics. This 9658 // has the same return as Diag(), so addition parameters can be passed 9659 // in with operator<< 9660 auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule, 9661 SourceLocation Loc, SourceRange Range, 9662 ODRMismatchDeclDifference DiffType) { 9663 return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff) 9664 << FirstRecord << FirstModule.empty() << FirstModule << Range 9665 << DiffType; 9666 }; 9667 auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc, 9668 SourceRange Range, ODRMismatchDeclDifference DiffType) { 9669 return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff) 9670 << SecondModule << Range << DiffType; 9671 }; 9672 9673 auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote, 9674 &ComputeQualTypeODRHash, &ComputeODRHash]( 9675 NamedDecl *FirstRecord, StringRef FirstModule, 9676 StringRef SecondModule, FieldDecl *FirstField, 9677 FieldDecl *SecondField) { 9678 IdentifierInfo *FirstII = FirstField->getIdentifier(); 9679 IdentifierInfo *SecondII = SecondField->getIdentifier(); 9680 if (FirstII->getName() != SecondII->getName()) { 9681 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9682 FirstField->getSourceRange(), FieldName) 9683 << FirstII; 9684 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9685 SecondField->getSourceRange(), FieldName) 9686 << SecondII; 9687 9688 return true; 9689 } 9690 9691 assert(getContext().hasSameType(FirstField->getType(), 9692 SecondField->getType())); 9693 9694 QualType FirstType = FirstField->getType(); 9695 QualType SecondType = SecondField->getType(); 9696 if (ComputeQualTypeODRHash(FirstType) != 9697 ComputeQualTypeODRHash(SecondType)) { 9698 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9699 FirstField->getSourceRange(), FieldTypeName) 9700 << FirstII << FirstType; 9701 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9702 SecondField->getSourceRange(), FieldTypeName) 9703 << SecondII << SecondType; 9704 9705 return true; 9706 } 9707 9708 const bool IsFirstBitField = FirstField->isBitField(); 9709 const bool IsSecondBitField = SecondField->isBitField(); 9710 if (IsFirstBitField != IsSecondBitField) { 9711 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9712 FirstField->getSourceRange(), FieldSingleBitField) 9713 << FirstII << IsFirstBitField; 9714 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9715 SecondField->getSourceRange(), FieldSingleBitField) 9716 << SecondII << IsSecondBitField; 9717 return true; 9718 } 9719 9720 if (IsFirstBitField && IsSecondBitField) { 9721 unsigned FirstBitWidthHash = 9722 ComputeODRHash(FirstField->getBitWidth()); 9723 unsigned SecondBitWidthHash = 9724 ComputeODRHash(SecondField->getBitWidth()); 9725 if (FirstBitWidthHash != SecondBitWidthHash) { 9726 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9727 FirstField->getSourceRange(), 9728 FieldDifferentWidthBitField) 9729 << FirstII << FirstField->getBitWidth()->getSourceRange(); 9730 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9731 SecondField->getSourceRange(), 9732 FieldDifferentWidthBitField) 9733 << SecondII << SecondField->getBitWidth()->getSourceRange(); 9734 return true; 9735 } 9736 } 9737 9738 if (!PP.getLangOpts().CPlusPlus) 9739 return false; 9740 9741 const bool IsFirstMutable = FirstField->isMutable(); 9742 const bool IsSecondMutable = SecondField->isMutable(); 9743 if (IsFirstMutable != IsSecondMutable) { 9744 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9745 FirstField->getSourceRange(), FieldSingleMutable) 9746 << FirstII << IsFirstMutable; 9747 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9748 SecondField->getSourceRange(), FieldSingleMutable) 9749 << SecondII << IsSecondMutable; 9750 return true; 9751 } 9752 9753 const Expr *FirstInitializer = FirstField->getInClassInitializer(); 9754 const Expr *SecondInitializer = SecondField->getInClassInitializer(); 9755 if ((!FirstInitializer && SecondInitializer) || 9756 (FirstInitializer && !SecondInitializer)) { 9757 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9758 FirstField->getSourceRange(), FieldSingleInitializer) 9759 << FirstII << (FirstInitializer != nullptr); 9760 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9761 SecondField->getSourceRange(), FieldSingleInitializer) 9762 << SecondII << (SecondInitializer != nullptr); 9763 return true; 9764 } 9765 9766 if (FirstInitializer && SecondInitializer) { 9767 unsigned FirstInitHash = ComputeODRHash(FirstInitializer); 9768 unsigned SecondInitHash = ComputeODRHash(SecondInitializer); 9769 if (FirstInitHash != SecondInitHash) { 9770 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9771 FirstField->getSourceRange(), 9772 FieldDifferentInitializers) 9773 << FirstII << FirstInitializer->getSourceRange(); 9774 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9775 SecondField->getSourceRange(), 9776 FieldDifferentInitializers) 9777 << SecondII << SecondInitializer->getSourceRange(); 9778 return true; 9779 } 9780 } 9781 9782 return false; 9783 }; 9784 9785 auto ODRDiagTypeDefOrAlias = 9786 [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash]( 9787 NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule, 9788 TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD, 9789 bool IsTypeAlias) { 9790 auto FirstName = FirstTD->getDeclName(); 9791 auto SecondName = SecondTD->getDeclName(); 9792 if (FirstName != SecondName) { 9793 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(), 9794 FirstTD->getSourceRange(), TypedefName) 9795 << IsTypeAlias << FirstName; 9796 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(), 9797 SecondTD->getSourceRange(), TypedefName) 9798 << IsTypeAlias << SecondName; 9799 return true; 9800 } 9801 9802 QualType FirstType = FirstTD->getUnderlyingType(); 9803 QualType SecondType = SecondTD->getUnderlyingType(); 9804 if (ComputeQualTypeODRHash(FirstType) != 9805 ComputeQualTypeODRHash(SecondType)) { 9806 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(), 9807 FirstTD->getSourceRange(), TypedefType) 9808 << IsTypeAlias << FirstName << FirstType; 9809 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(), 9810 SecondTD->getSourceRange(), TypedefType) 9811 << IsTypeAlias << SecondName << SecondType; 9812 return true; 9813 } 9814 9815 return false; 9816 }; 9817 9818 auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote, 9819 &ComputeQualTypeODRHash, &ComputeODRHash, 9820 this](NamedDecl *FirstRecord, StringRef FirstModule, 9821 StringRef SecondModule, VarDecl *FirstVD, 9822 VarDecl *SecondVD) { 9823 auto FirstName = FirstVD->getDeclName(); 9824 auto SecondName = SecondVD->getDeclName(); 9825 if (FirstName != SecondName) { 9826 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9827 FirstVD->getSourceRange(), VarName) 9828 << FirstName; 9829 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9830 SecondVD->getSourceRange(), VarName) 9831 << SecondName; 9832 return true; 9833 } 9834 9835 QualType FirstType = FirstVD->getType(); 9836 QualType SecondType = SecondVD->getType(); 9837 if (ComputeQualTypeODRHash(FirstType) != 9838 ComputeQualTypeODRHash(SecondType)) { 9839 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9840 FirstVD->getSourceRange(), VarType) 9841 << FirstName << FirstType; 9842 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9843 SecondVD->getSourceRange(), VarType) 9844 << SecondName << SecondType; 9845 return true; 9846 } 9847 9848 if (!PP.getLangOpts().CPlusPlus) 9849 return false; 9850 9851 const Expr *FirstInit = FirstVD->getInit(); 9852 const Expr *SecondInit = SecondVD->getInit(); 9853 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 9854 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9855 FirstVD->getSourceRange(), VarSingleInitializer) 9856 << FirstName << (FirstInit == nullptr) 9857 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 9858 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9859 SecondVD->getSourceRange(), VarSingleInitializer) 9860 << SecondName << (SecondInit == nullptr) 9861 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 9862 return true; 9863 } 9864 9865 if (FirstInit && SecondInit && 9866 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 9867 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9868 FirstVD->getSourceRange(), VarDifferentInitializer) 9869 << FirstName << FirstInit->getSourceRange(); 9870 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9871 SecondVD->getSourceRange(), VarDifferentInitializer) 9872 << SecondName << SecondInit->getSourceRange(); 9873 return true; 9874 } 9875 9876 const bool FirstIsConstexpr = FirstVD->isConstexpr(); 9877 const bool SecondIsConstexpr = SecondVD->isConstexpr(); 9878 if (FirstIsConstexpr != SecondIsConstexpr) { 9879 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9880 FirstVD->getSourceRange(), VarConstexpr) 9881 << FirstName << FirstIsConstexpr; 9882 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9883 SecondVD->getSourceRange(), VarConstexpr) 9884 << SecondName << SecondIsConstexpr; 9885 return true; 9886 } 9887 return false; 9888 }; 9889 9890 auto DifferenceSelector = [](Decl *D) { 9891 assert(D && "valid Decl required"); 9892 switch (D->getKind()) { 9893 default: 9894 return Other; 9895 case Decl::AccessSpec: 9896 switch (D->getAccess()) { 9897 case AS_public: 9898 return PublicSpecifer; 9899 case AS_private: 9900 return PrivateSpecifer; 9901 case AS_protected: 9902 return ProtectedSpecifer; 9903 case AS_none: 9904 break; 9905 } 9906 llvm_unreachable("Invalid access specifier"); 9907 case Decl::StaticAssert: 9908 return StaticAssert; 9909 case Decl::Field: 9910 return Field; 9911 case Decl::CXXMethod: 9912 case Decl::CXXConstructor: 9913 case Decl::CXXDestructor: 9914 return CXXMethod; 9915 case Decl::TypeAlias: 9916 return TypeAlias; 9917 case Decl::Typedef: 9918 return TypeDef; 9919 case Decl::Var: 9920 return Var; 9921 case Decl::Friend: 9922 return Friend; 9923 case Decl::FunctionTemplate: 9924 return FunctionTemplate; 9925 } 9926 }; 9927 9928 using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>; 9929 auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes, 9930 RecordDecl *Record, 9931 const DeclContext *DC) { 9932 for (auto *D : Record->decls()) { 9933 if (!ODRHash::isDeclToBeProcessed(D, DC)) 9934 continue; 9935 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 9936 } 9937 }; 9938 9939 struct DiffResult { 9940 Decl *FirstDecl = nullptr, *SecondDecl = nullptr; 9941 ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other; 9942 }; 9943 9944 // If there is a diagnoseable difference, FirstDiffType and 9945 // SecondDiffType will not be Other and FirstDecl and SecondDecl will be 9946 // filled in if not EndOfClass. 9947 auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes, 9948 DeclHashes &SecondHashes) { 9949 DiffResult DR; 9950 auto FirstIt = FirstHashes.begin(); 9951 auto SecondIt = SecondHashes.begin(); 9952 while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) { 9953 if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() && 9954 FirstIt->second == SecondIt->second) { 9955 ++FirstIt; 9956 ++SecondIt; 9957 continue; 9958 } 9959 9960 DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first; 9961 DR.SecondDecl = 9962 SecondIt == SecondHashes.end() ? nullptr : SecondIt->first; 9963 9964 DR.FirstDiffType = 9965 DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass; 9966 DR.SecondDiffType = 9967 DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass; 9968 return DR; 9969 } 9970 return DR; 9971 }; 9972 9973 // Use this to diagnose that an unexpected Decl was encountered 9974 // or no difference was detected. This causes a generic error 9975 // message to be emitted. 9976 auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord, 9977 StringRef FirstModule, 9978 NamedDecl *SecondRecord, 9979 StringRef SecondModule) { 9980 Diag(FirstRecord->getLocation(), 9981 diag::err_module_odr_violation_different_definitions) 9982 << FirstRecord << FirstModule.empty() << FirstModule; 9983 9984 if (DR.FirstDecl) { 9985 Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference) 9986 << FirstRecord << DR.FirstDecl->getSourceRange(); 9987 } 9988 9989 Diag(SecondRecord->getLocation(), 9990 diag::note_module_odr_violation_different_definitions) 9991 << SecondModule; 9992 9993 if (DR.SecondDecl) { 9994 Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference) 9995 << DR.SecondDecl->getSourceRange(); 9996 } 9997 }; 9998 9999 auto DiagnoseODRMismatch = 10000 [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule, 10001 NamedDecl *SecondRecord, StringRef SecondModule) { 10002 SourceLocation FirstLoc; 10003 SourceRange FirstRange; 10004 auto *FirstTag = dyn_cast<TagDecl>(FirstRecord); 10005 if (DR.FirstDiffType == EndOfClass && FirstTag) { 10006 FirstLoc = FirstTag->getBraceRange().getEnd(); 10007 } else { 10008 FirstLoc = DR.FirstDecl->getLocation(); 10009 FirstRange = DR.FirstDecl->getSourceRange(); 10010 } 10011 Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl) 10012 << FirstRecord << FirstModule.empty() << FirstModule << FirstRange 10013 << DR.FirstDiffType; 10014 10015 SourceLocation SecondLoc; 10016 SourceRange SecondRange; 10017 auto *SecondTag = dyn_cast<TagDecl>(SecondRecord); 10018 if (DR.SecondDiffType == EndOfClass && SecondTag) { 10019 SecondLoc = SecondTag->getBraceRange().getEnd(); 10020 } else { 10021 SecondLoc = DR.SecondDecl->getLocation(); 10022 SecondRange = DR.SecondDecl->getSourceRange(); 10023 } 10024 Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl) 10025 << SecondModule << SecondRange << DR.SecondDiffType; 10026 }; 10027 10028 // Issue any pending ODR-failure diagnostics. 10029 for (auto &Merge : OdrMergeFailures) { 10030 // If we've already pointed out a specific problem with this class, don't 10031 // bother issuing a general "something's different" diagnostic. 10032 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 10033 continue; 10034 10035 bool Diagnosed = false; 10036 CXXRecordDecl *FirstRecord = Merge.first; 10037 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord); 10038 for (auto &RecordPair : Merge.second) { 10039 CXXRecordDecl *SecondRecord = RecordPair.first; 10040 // Multiple different declarations got merged together; tell the user 10041 // where they came from. 10042 if (FirstRecord == SecondRecord) 10043 continue; 10044 10045 std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord); 10046 10047 auto *FirstDD = FirstRecord->DefinitionData; 10048 auto *SecondDD = RecordPair.second; 10049 10050 assert(FirstDD && SecondDD && "Definitions without DefinitionData"); 10051 10052 // Diagnostics from DefinitionData are emitted here. 10053 if (FirstDD != SecondDD) { 10054 enum ODRDefinitionDataDifference { 10055 NumBases, 10056 NumVBases, 10057 BaseType, 10058 BaseVirtual, 10059 BaseAccess, 10060 }; 10061 auto ODRDiagBaseError = [FirstRecord, &FirstModule, 10062 this](SourceLocation Loc, SourceRange Range, 10063 ODRDefinitionDataDifference DiffType) { 10064 return Diag(Loc, diag::err_module_odr_violation_definition_data) 10065 << FirstRecord << FirstModule.empty() << FirstModule << Range 10066 << DiffType; 10067 }; 10068 auto ODRDiagBaseNote = [&SecondModule, 10069 this](SourceLocation Loc, SourceRange Range, 10070 ODRDefinitionDataDifference DiffType) { 10071 return Diag(Loc, diag::note_module_odr_violation_definition_data) 10072 << SecondModule << Range << DiffType; 10073 }; 10074 10075 unsigned FirstNumBases = FirstDD->NumBases; 10076 unsigned FirstNumVBases = FirstDD->NumVBases; 10077 unsigned SecondNumBases = SecondDD->NumBases; 10078 unsigned SecondNumVBases = SecondDD->NumVBases; 10079 10080 auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) { 10081 unsigned NumBases = DD->NumBases; 10082 if (NumBases == 0) return SourceRange(); 10083 auto bases = DD->bases(); 10084 return SourceRange(bases[0].getBeginLoc(), 10085 bases[NumBases - 1].getEndLoc()); 10086 }; 10087 10088 if (FirstNumBases != SecondNumBases) { 10089 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10090 NumBases) 10091 << FirstNumBases; 10092 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10093 NumBases) 10094 << SecondNumBases; 10095 Diagnosed = true; 10096 break; 10097 } 10098 10099 if (FirstNumVBases != SecondNumVBases) { 10100 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10101 NumVBases) 10102 << FirstNumVBases; 10103 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10104 NumVBases) 10105 << SecondNumVBases; 10106 Diagnosed = true; 10107 break; 10108 } 10109 10110 auto FirstBases = FirstDD->bases(); 10111 auto SecondBases = SecondDD->bases(); 10112 unsigned i = 0; 10113 for (i = 0; i < FirstNumBases; ++i) { 10114 auto FirstBase = FirstBases[i]; 10115 auto SecondBase = SecondBases[i]; 10116 if (ComputeQualTypeODRHash(FirstBase.getType()) != 10117 ComputeQualTypeODRHash(SecondBase.getType())) { 10118 ODRDiagBaseError(FirstRecord->getLocation(), 10119 FirstBase.getSourceRange(), BaseType) 10120 << (i + 1) << FirstBase.getType(); 10121 ODRDiagBaseNote(SecondRecord->getLocation(), 10122 SecondBase.getSourceRange(), BaseType) 10123 << (i + 1) << SecondBase.getType(); 10124 break; 10125 } 10126 10127 if (FirstBase.isVirtual() != SecondBase.isVirtual()) { 10128 ODRDiagBaseError(FirstRecord->getLocation(), 10129 FirstBase.getSourceRange(), BaseVirtual) 10130 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType(); 10131 ODRDiagBaseNote(SecondRecord->getLocation(), 10132 SecondBase.getSourceRange(), BaseVirtual) 10133 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType(); 10134 break; 10135 } 10136 10137 if (FirstBase.getAccessSpecifierAsWritten() != 10138 SecondBase.getAccessSpecifierAsWritten()) { 10139 ODRDiagBaseError(FirstRecord->getLocation(), 10140 FirstBase.getSourceRange(), BaseAccess) 10141 << (i + 1) << FirstBase.getType() 10142 << (int)FirstBase.getAccessSpecifierAsWritten(); 10143 ODRDiagBaseNote(SecondRecord->getLocation(), 10144 SecondBase.getSourceRange(), BaseAccess) 10145 << (i + 1) << SecondBase.getType() 10146 << (int)SecondBase.getAccessSpecifierAsWritten(); 10147 break; 10148 } 10149 } 10150 10151 if (i != FirstNumBases) { 10152 Diagnosed = true; 10153 break; 10154 } 10155 } 10156 10157 const ClassTemplateDecl *FirstTemplate = 10158 FirstRecord->getDescribedClassTemplate(); 10159 const ClassTemplateDecl *SecondTemplate = 10160 SecondRecord->getDescribedClassTemplate(); 10161 10162 assert(!FirstTemplate == !SecondTemplate && 10163 "Both pointers should be null or non-null"); 10164 10165 enum ODRTemplateDifference { 10166 ParamEmptyName, 10167 ParamName, 10168 ParamSingleDefaultArgument, 10169 ParamDifferentDefaultArgument, 10170 }; 10171 10172 if (FirstTemplate && SecondTemplate) { 10173 DeclHashes FirstTemplateHashes; 10174 DeclHashes SecondTemplateHashes; 10175 10176 auto PopulateTemplateParameterHashs = 10177 [&ComputeSubDeclODRHash](DeclHashes &Hashes, 10178 const ClassTemplateDecl *TD) { 10179 for (auto *D : TD->getTemplateParameters()->asArray()) { 10180 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 10181 } 10182 }; 10183 10184 PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate); 10185 PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate); 10186 10187 assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() && 10188 "Number of template parameters should be equal."); 10189 10190 auto FirstIt = FirstTemplateHashes.begin(); 10191 auto FirstEnd = FirstTemplateHashes.end(); 10192 auto SecondIt = SecondTemplateHashes.begin(); 10193 for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) { 10194 if (FirstIt->second == SecondIt->second) 10195 continue; 10196 10197 auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this]( 10198 SourceLocation Loc, SourceRange Range, 10199 ODRTemplateDifference DiffType) { 10200 return Diag(Loc, diag::err_module_odr_violation_template_parameter) 10201 << FirstRecord << FirstModule.empty() << FirstModule << Range 10202 << DiffType; 10203 }; 10204 auto ODRDiagTemplateNote = [&SecondModule, this]( 10205 SourceLocation Loc, SourceRange Range, 10206 ODRTemplateDifference DiffType) { 10207 return Diag(Loc, diag::note_module_odr_violation_template_parameter) 10208 << SecondModule << Range << DiffType; 10209 }; 10210 10211 const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first); 10212 const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first); 10213 10214 assert(FirstDecl->getKind() == SecondDecl->getKind() && 10215 "Parameter Decl's should be the same kind."); 10216 10217 DeclarationName FirstName = FirstDecl->getDeclName(); 10218 DeclarationName SecondName = SecondDecl->getDeclName(); 10219 10220 if (FirstName != SecondName) { 10221 const bool FirstNameEmpty = 10222 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo(); 10223 const bool SecondNameEmpty = 10224 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo(); 10225 assert((!FirstNameEmpty || !SecondNameEmpty) && 10226 "Both template parameters cannot be unnamed."); 10227 ODRDiagTemplateError(FirstDecl->getLocation(), 10228 FirstDecl->getSourceRange(), 10229 FirstNameEmpty ? ParamEmptyName : ParamName) 10230 << FirstName; 10231 ODRDiagTemplateNote(SecondDecl->getLocation(), 10232 SecondDecl->getSourceRange(), 10233 SecondNameEmpty ? ParamEmptyName : ParamName) 10234 << SecondName; 10235 break; 10236 } 10237 10238 switch (FirstDecl->getKind()) { 10239 default: 10240 llvm_unreachable("Invalid template parameter type."); 10241 case Decl::TemplateTypeParm: { 10242 const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl); 10243 const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl); 10244 const bool HasFirstDefaultArgument = 10245 FirstParam->hasDefaultArgument() && 10246 !FirstParam->defaultArgumentWasInherited(); 10247 const bool HasSecondDefaultArgument = 10248 SecondParam->hasDefaultArgument() && 10249 !SecondParam->defaultArgumentWasInherited(); 10250 10251 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10252 ODRDiagTemplateError(FirstDecl->getLocation(), 10253 FirstDecl->getSourceRange(), 10254 ParamSingleDefaultArgument) 10255 << HasFirstDefaultArgument; 10256 ODRDiagTemplateNote(SecondDecl->getLocation(), 10257 SecondDecl->getSourceRange(), 10258 ParamSingleDefaultArgument) 10259 << HasSecondDefaultArgument; 10260 break; 10261 } 10262 10263 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10264 "Expecting default arguments."); 10265 10266 ODRDiagTemplateError(FirstDecl->getLocation(), 10267 FirstDecl->getSourceRange(), 10268 ParamDifferentDefaultArgument); 10269 ODRDiagTemplateNote(SecondDecl->getLocation(), 10270 SecondDecl->getSourceRange(), 10271 ParamDifferentDefaultArgument); 10272 10273 break; 10274 } 10275 case Decl::NonTypeTemplateParm: { 10276 const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl); 10277 const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl); 10278 const bool HasFirstDefaultArgument = 10279 FirstParam->hasDefaultArgument() && 10280 !FirstParam->defaultArgumentWasInherited(); 10281 const bool HasSecondDefaultArgument = 10282 SecondParam->hasDefaultArgument() && 10283 !SecondParam->defaultArgumentWasInherited(); 10284 10285 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10286 ODRDiagTemplateError(FirstDecl->getLocation(), 10287 FirstDecl->getSourceRange(), 10288 ParamSingleDefaultArgument) 10289 << HasFirstDefaultArgument; 10290 ODRDiagTemplateNote(SecondDecl->getLocation(), 10291 SecondDecl->getSourceRange(), 10292 ParamSingleDefaultArgument) 10293 << HasSecondDefaultArgument; 10294 break; 10295 } 10296 10297 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10298 "Expecting default arguments."); 10299 10300 ODRDiagTemplateError(FirstDecl->getLocation(), 10301 FirstDecl->getSourceRange(), 10302 ParamDifferentDefaultArgument); 10303 ODRDiagTemplateNote(SecondDecl->getLocation(), 10304 SecondDecl->getSourceRange(), 10305 ParamDifferentDefaultArgument); 10306 10307 break; 10308 } 10309 case Decl::TemplateTemplateParm: { 10310 const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl); 10311 const auto *SecondParam = 10312 cast<TemplateTemplateParmDecl>(SecondDecl); 10313 const bool HasFirstDefaultArgument = 10314 FirstParam->hasDefaultArgument() && 10315 !FirstParam->defaultArgumentWasInherited(); 10316 const bool HasSecondDefaultArgument = 10317 SecondParam->hasDefaultArgument() && 10318 !SecondParam->defaultArgumentWasInherited(); 10319 10320 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10321 ODRDiagTemplateError(FirstDecl->getLocation(), 10322 FirstDecl->getSourceRange(), 10323 ParamSingleDefaultArgument) 10324 << HasFirstDefaultArgument; 10325 ODRDiagTemplateNote(SecondDecl->getLocation(), 10326 SecondDecl->getSourceRange(), 10327 ParamSingleDefaultArgument) 10328 << HasSecondDefaultArgument; 10329 break; 10330 } 10331 10332 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10333 "Expecting default arguments."); 10334 10335 ODRDiagTemplateError(FirstDecl->getLocation(), 10336 FirstDecl->getSourceRange(), 10337 ParamDifferentDefaultArgument); 10338 ODRDiagTemplateNote(SecondDecl->getLocation(), 10339 SecondDecl->getSourceRange(), 10340 ParamDifferentDefaultArgument); 10341 10342 break; 10343 } 10344 } 10345 10346 break; 10347 } 10348 10349 if (FirstIt != FirstEnd) { 10350 Diagnosed = true; 10351 break; 10352 } 10353 } 10354 10355 DeclHashes FirstHashes; 10356 DeclHashes SecondHashes; 10357 const DeclContext *DC = FirstRecord; 10358 PopulateHashes(FirstHashes, FirstRecord, DC); 10359 PopulateHashes(SecondHashes, SecondRecord, DC); 10360 10361 auto DR = FindTypeDiffs(FirstHashes, SecondHashes); 10362 ODRMismatchDecl FirstDiffType = DR.FirstDiffType; 10363 ODRMismatchDecl SecondDiffType = DR.SecondDiffType; 10364 Decl *FirstDecl = DR.FirstDecl; 10365 Decl *SecondDecl = DR.SecondDecl; 10366 10367 if (FirstDiffType == Other || SecondDiffType == Other) { 10368 DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord, 10369 SecondModule); 10370 Diagnosed = true; 10371 break; 10372 } 10373 10374 if (FirstDiffType != SecondDiffType) { 10375 DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord, 10376 SecondModule); 10377 Diagnosed = true; 10378 break; 10379 } 10380 10381 assert(FirstDiffType == SecondDiffType); 10382 10383 switch (FirstDiffType) { 10384 case Other: 10385 case EndOfClass: 10386 case PublicSpecifer: 10387 case PrivateSpecifer: 10388 case ProtectedSpecifer: 10389 llvm_unreachable("Invalid diff type"); 10390 10391 case StaticAssert: { 10392 StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl); 10393 StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl); 10394 10395 Expr *FirstExpr = FirstSA->getAssertExpr(); 10396 Expr *SecondExpr = SecondSA->getAssertExpr(); 10397 unsigned FirstODRHash = ComputeODRHash(FirstExpr); 10398 unsigned SecondODRHash = ComputeODRHash(SecondExpr); 10399 if (FirstODRHash != SecondODRHash) { 10400 ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(), 10401 FirstExpr->getSourceRange(), StaticAssertCondition); 10402 ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(), 10403 SecondExpr->getSourceRange(), StaticAssertCondition); 10404 Diagnosed = true; 10405 break; 10406 } 10407 10408 StringLiteral *FirstStr = FirstSA->getMessage(); 10409 StringLiteral *SecondStr = SecondSA->getMessage(); 10410 assert((FirstStr || SecondStr) && "Both messages cannot be empty"); 10411 if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) { 10412 SourceLocation FirstLoc, SecondLoc; 10413 SourceRange FirstRange, SecondRange; 10414 if (FirstStr) { 10415 FirstLoc = FirstStr->getBeginLoc(); 10416 FirstRange = FirstStr->getSourceRange(); 10417 } else { 10418 FirstLoc = FirstSA->getBeginLoc(); 10419 FirstRange = FirstSA->getSourceRange(); 10420 } 10421 if (SecondStr) { 10422 SecondLoc = SecondStr->getBeginLoc(); 10423 SecondRange = SecondStr->getSourceRange(); 10424 } else { 10425 SecondLoc = SecondSA->getBeginLoc(); 10426 SecondRange = SecondSA->getSourceRange(); 10427 } 10428 ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange, 10429 StaticAssertOnlyMessage) 10430 << (FirstStr == nullptr); 10431 ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange, 10432 StaticAssertOnlyMessage) 10433 << (SecondStr == nullptr); 10434 Diagnosed = true; 10435 break; 10436 } 10437 10438 if (FirstStr && SecondStr && 10439 FirstStr->getString() != SecondStr->getString()) { 10440 ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(), 10441 FirstStr->getSourceRange(), StaticAssertMessage); 10442 ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(), 10443 SecondStr->getSourceRange(), StaticAssertMessage); 10444 Diagnosed = true; 10445 break; 10446 } 10447 break; 10448 } 10449 case Field: { 10450 Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule, 10451 cast<FieldDecl>(FirstDecl), 10452 cast<FieldDecl>(SecondDecl)); 10453 break; 10454 } 10455 case CXXMethod: { 10456 enum { 10457 DiagMethod, 10458 DiagConstructor, 10459 DiagDestructor, 10460 } FirstMethodType, 10461 SecondMethodType; 10462 auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) { 10463 if (isa<CXXConstructorDecl>(D)) return DiagConstructor; 10464 if (isa<CXXDestructorDecl>(D)) return DiagDestructor; 10465 return DiagMethod; 10466 }; 10467 const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl); 10468 const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl); 10469 FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod); 10470 SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod); 10471 auto FirstName = FirstMethod->getDeclName(); 10472 auto SecondName = SecondMethod->getDeclName(); 10473 if (FirstMethodType != SecondMethodType || FirstName != SecondName) { 10474 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10475 FirstMethod->getSourceRange(), MethodName) 10476 << FirstMethodType << FirstName; 10477 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10478 SecondMethod->getSourceRange(), MethodName) 10479 << SecondMethodType << SecondName; 10480 10481 Diagnosed = true; 10482 break; 10483 } 10484 10485 const bool FirstDeleted = FirstMethod->isDeletedAsWritten(); 10486 const bool SecondDeleted = SecondMethod->isDeletedAsWritten(); 10487 if (FirstDeleted != SecondDeleted) { 10488 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10489 FirstMethod->getSourceRange(), MethodDeleted) 10490 << FirstMethodType << FirstName << FirstDeleted; 10491 10492 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10493 SecondMethod->getSourceRange(), MethodDeleted) 10494 << SecondMethodType << SecondName << SecondDeleted; 10495 Diagnosed = true; 10496 break; 10497 } 10498 10499 const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted(); 10500 const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted(); 10501 if (FirstDefaulted != SecondDefaulted) { 10502 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10503 FirstMethod->getSourceRange(), MethodDefaulted) 10504 << FirstMethodType << FirstName << FirstDefaulted; 10505 10506 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10507 SecondMethod->getSourceRange(), MethodDefaulted) 10508 << SecondMethodType << SecondName << SecondDefaulted; 10509 Diagnosed = true; 10510 break; 10511 } 10512 10513 const bool FirstVirtual = FirstMethod->isVirtualAsWritten(); 10514 const bool SecondVirtual = SecondMethod->isVirtualAsWritten(); 10515 const bool FirstPure = FirstMethod->isPure(); 10516 const bool SecondPure = SecondMethod->isPure(); 10517 if ((FirstVirtual || SecondVirtual) && 10518 (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) { 10519 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10520 FirstMethod->getSourceRange(), MethodVirtual) 10521 << FirstMethodType << FirstName << FirstPure << FirstVirtual; 10522 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10523 SecondMethod->getSourceRange(), MethodVirtual) 10524 << SecondMethodType << SecondName << SecondPure << SecondVirtual; 10525 Diagnosed = true; 10526 break; 10527 } 10528 10529 // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging, 10530 // FirstDecl is the canonical Decl of SecondDecl, so the storage 10531 // class needs to be checked instead. 10532 const auto FirstStorage = FirstMethod->getStorageClass(); 10533 const auto SecondStorage = SecondMethod->getStorageClass(); 10534 const bool FirstStatic = FirstStorage == SC_Static; 10535 const bool SecondStatic = SecondStorage == SC_Static; 10536 if (FirstStatic != SecondStatic) { 10537 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10538 FirstMethod->getSourceRange(), MethodStatic) 10539 << FirstMethodType << FirstName << FirstStatic; 10540 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10541 SecondMethod->getSourceRange(), MethodStatic) 10542 << SecondMethodType << SecondName << SecondStatic; 10543 Diagnosed = true; 10544 break; 10545 } 10546 10547 const bool FirstVolatile = FirstMethod->isVolatile(); 10548 const bool SecondVolatile = SecondMethod->isVolatile(); 10549 if (FirstVolatile != SecondVolatile) { 10550 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10551 FirstMethod->getSourceRange(), MethodVolatile) 10552 << FirstMethodType << FirstName << FirstVolatile; 10553 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10554 SecondMethod->getSourceRange(), MethodVolatile) 10555 << SecondMethodType << SecondName << SecondVolatile; 10556 Diagnosed = true; 10557 break; 10558 } 10559 10560 const bool FirstConst = FirstMethod->isConst(); 10561 const bool SecondConst = SecondMethod->isConst(); 10562 if (FirstConst != SecondConst) { 10563 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10564 FirstMethod->getSourceRange(), MethodConst) 10565 << FirstMethodType << FirstName << FirstConst; 10566 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10567 SecondMethod->getSourceRange(), MethodConst) 10568 << SecondMethodType << SecondName << SecondConst; 10569 Diagnosed = true; 10570 break; 10571 } 10572 10573 const bool FirstInline = FirstMethod->isInlineSpecified(); 10574 const bool SecondInline = SecondMethod->isInlineSpecified(); 10575 if (FirstInline != SecondInline) { 10576 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10577 FirstMethod->getSourceRange(), MethodInline) 10578 << FirstMethodType << FirstName << FirstInline; 10579 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10580 SecondMethod->getSourceRange(), MethodInline) 10581 << SecondMethodType << SecondName << SecondInline; 10582 Diagnosed = true; 10583 break; 10584 } 10585 10586 const unsigned FirstNumParameters = FirstMethod->param_size(); 10587 const unsigned SecondNumParameters = SecondMethod->param_size(); 10588 if (FirstNumParameters != SecondNumParameters) { 10589 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10590 FirstMethod->getSourceRange(), 10591 MethodNumberParameters) 10592 << FirstMethodType << FirstName << FirstNumParameters; 10593 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10594 SecondMethod->getSourceRange(), 10595 MethodNumberParameters) 10596 << SecondMethodType << SecondName << SecondNumParameters; 10597 Diagnosed = true; 10598 break; 10599 } 10600 10601 // Need this status boolean to know when break out of the switch. 10602 bool ParameterMismatch = false; 10603 for (unsigned I = 0; I < FirstNumParameters; ++I) { 10604 const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I); 10605 const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I); 10606 10607 QualType FirstParamType = FirstParam->getType(); 10608 QualType SecondParamType = SecondParam->getType(); 10609 if (FirstParamType != SecondParamType && 10610 ComputeQualTypeODRHash(FirstParamType) != 10611 ComputeQualTypeODRHash(SecondParamType)) { 10612 if (const DecayedType *ParamDecayedType = 10613 FirstParamType->getAs<DecayedType>()) { 10614 ODRDiagDeclError( 10615 FirstRecord, FirstModule, FirstMethod->getLocation(), 10616 FirstMethod->getSourceRange(), MethodParameterType) 10617 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10618 << true << ParamDecayedType->getOriginalType(); 10619 } else { 10620 ODRDiagDeclError( 10621 FirstRecord, FirstModule, FirstMethod->getLocation(), 10622 FirstMethod->getSourceRange(), MethodParameterType) 10623 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10624 << false; 10625 } 10626 10627 if (const DecayedType *ParamDecayedType = 10628 SecondParamType->getAs<DecayedType>()) { 10629 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10630 SecondMethod->getSourceRange(), 10631 MethodParameterType) 10632 << SecondMethodType << SecondName << (I + 1) 10633 << SecondParamType << true 10634 << ParamDecayedType->getOriginalType(); 10635 } else { 10636 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10637 SecondMethod->getSourceRange(), 10638 MethodParameterType) 10639 << SecondMethodType << SecondName << (I + 1) 10640 << SecondParamType << false; 10641 } 10642 ParameterMismatch = true; 10643 break; 10644 } 10645 10646 DeclarationName FirstParamName = FirstParam->getDeclName(); 10647 DeclarationName SecondParamName = SecondParam->getDeclName(); 10648 if (FirstParamName != SecondParamName) { 10649 ODRDiagDeclError(FirstRecord, FirstModule, 10650 FirstMethod->getLocation(), 10651 FirstMethod->getSourceRange(), MethodParameterName) 10652 << FirstMethodType << FirstName << (I + 1) << FirstParamName; 10653 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10654 SecondMethod->getSourceRange(), MethodParameterName) 10655 << SecondMethodType << SecondName << (I + 1) << SecondParamName; 10656 ParameterMismatch = true; 10657 break; 10658 } 10659 10660 const Expr *FirstInit = FirstParam->getInit(); 10661 const Expr *SecondInit = SecondParam->getInit(); 10662 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 10663 ODRDiagDeclError(FirstRecord, FirstModule, 10664 FirstMethod->getLocation(), 10665 FirstMethod->getSourceRange(), 10666 MethodParameterSingleDefaultArgument) 10667 << FirstMethodType << FirstName << (I + 1) 10668 << (FirstInit == nullptr) 10669 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 10670 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10671 SecondMethod->getSourceRange(), 10672 MethodParameterSingleDefaultArgument) 10673 << SecondMethodType << SecondName << (I + 1) 10674 << (SecondInit == nullptr) 10675 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 10676 ParameterMismatch = true; 10677 break; 10678 } 10679 10680 if (FirstInit && SecondInit && 10681 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 10682 ODRDiagDeclError(FirstRecord, FirstModule, 10683 FirstMethod->getLocation(), 10684 FirstMethod->getSourceRange(), 10685 MethodParameterDifferentDefaultArgument) 10686 << FirstMethodType << FirstName << (I + 1) 10687 << FirstInit->getSourceRange(); 10688 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10689 SecondMethod->getSourceRange(), 10690 MethodParameterDifferentDefaultArgument) 10691 << SecondMethodType << SecondName << (I + 1) 10692 << SecondInit->getSourceRange(); 10693 ParameterMismatch = true; 10694 break; 10695 10696 } 10697 } 10698 10699 if (ParameterMismatch) { 10700 Diagnosed = true; 10701 break; 10702 } 10703 10704 const auto *FirstTemplateArgs = 10705 FirstMethod->getTemplateSpecializationArgs(); 10706 const auto *SecondTemplateArgs = 10707 SecondMethod->getTemplateSpecializationArgs(); 10708 10709 if ((FirstTemplateArgs && !SecondTemplateArgs) || 10710 (!FirstTemplateArgs && SecondTemplateArgs)) { 10711 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10712 FirstMethod->getSourceRange(), 10713 MethodNoTemplateArguments) 10714 << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr); 10715 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10716 SecondMethod->getSourceRange(), 10717 MethodNoTemplateArguments) 10718 << SecondMethodType << SecondName 10719 << (SecondTemplateArgs != nullptr); 10720 10721 Diagnosed = true; 10722 break; 10723 } 10724 10725 if (FirstTemplateArgs && SecondTemplateArgs) { 10726 // Remove pack expansions from argument list. 10727 auto ExpandTemplateArgumentList = 10728 [](const TemplateArgumentList *TAL) { 10729 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList; 10730 for (const TemplateArgument &TA : TAL->asArray()) { 10731 if (TA.getKind() != TemplateArgument::Pack) { 10732 ExpandedList.push_back(&TA); 10733 continue; 10734 } 10735 for (const TemplateArgument &PackTA : TA.getPackAsArray()) { 10736 ExpandedList.push_back(&PackTA); 10737 } 10738 } 10739 return ExpandedList; 10740 }; 10741 llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList = 10742 ExpandTemplateArgumentList(FirstTemplateArgs); 10743 llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList = 10744 ExpandTemplateArgumentList(SecondTemplateArgs); 10745 10746 if (FirstExpandedList.size() != SecondExpandedList.size()) { 10747 ODRDiagDeclError(FirstRecord, FirstModule, 10748 FirstMethod->getLocation(), 10749 FirstMethod->getSourceRange(), 10750 MethodDifferentNumberTemplateArguments) 10751 << FirstMethodType << FirstName 10752 << (unsigned)FirstExpandedList.size(); 10753 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10754 SecondMethod->getSourceRange(), 10755 MethodDifferentNumberTemplateArguments) 10756 << SecondMethodType << SecondName 10757 << (unsigned)SecondExpandedList.size(); 10758 10759 Diagnosed = true; 10760 break; 10761 } 10762 10763 bool TemplateArgumentMismatch = false; 10764 for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) { 10765 const TemplateArgument &FirstTA = *FirstExpandedList[i], 10766 &SecondTA = *SecondExpandedList[i]; 10767 if (ComputeTemplateArgumentODRHash(FirstTA) == 10768 ComputeTemplateArgumentODRHash(SecondTA)) { 10769 continue; 10770 } 10771 10772 ODRDiagDeclError( 10773 FirstRecord, FirstModule, FirstMethod->getLocation(), 10774 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument) 10775 << FirstMethodType << FirstName << FirstTA << i + 1; 10776 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10777 SecondMethod->getSourceRange(), 10778 MethodDifferentTemplateArgument) 10779 << SecondMethodType << SecondName << SecondTA << i + 1; 10780 10781 TemplateArgumentMismatch = true; 10782 break; 10783 } 10784 10785 if (TemplateArgumentMismatch) { 10786 Diagnosed = true; 10787 break; 10788 } 10789 } 10790 10791 // Compute the hash of the method as if it has no body. 10792 auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) { 10793 Hash.clear(); 10794 Hash.AddFunctionDecl(D, true /*SkipBody*/); 10795 return Hash.CalculateHash(); 10796 }; 10797 10798 // Compare the hash generated to the hash stored. A difference means 10799 // that a body was present in the original source. Due to merging, 10800 // the stardard way of detecting a body will not work. 10801 const bool HasFirstBody = 10802 ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash(); 10803 const bool HasSecondBody = 10804 ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash(); 10805 10806 if (HasFirstBody != HasSecondBody) { 10807 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10808 FirstMethod->getSourceRange(), MethodSingleBody) 10809 << FirstMethodType << FirstName << HasFirstBody; 10810 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10811 SecondMethod->getSourceRange(), MethodSingleBody) 10812 << SecondMethodType << SecondName << HasSecondBody; 10813 Diagnosed = true; 10814 break; 10815 } 10816 10817 if (HasFirstBody && HasSecondBody) { 10818 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10819 FirstMethod->getSourceRange(), MethodDifferentBody) 10820 << FirstMethodType << FirstName; 10821 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10822 SecondMethod->getSourceRange(), MethodDifferentBody) 10823 << SecondMethodType << SecondName; 10824 Diagnosed = true; 10825 break; 10826 } 10827 10828 break; 10829 } 10830 case TypeAlias: 10831 case TypeDef: { 10832 Diagnosed = ODRDiagTypeDefOrAlias( 10833 FirstRecord, FirstModule, SecondModule, 10834 cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl), 10835 FirstDiffType == TypeAlias); 10836 break; 10837 } 10838 case Var: { 10839 Diagnosed = 10840 ODRDiagVar(FirstRecord, FirstModule, SecondModule, 10841 cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl)); 10842 break; 10843 } 10844 case Friend: { 10845 FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl); 10846 FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl); 10847 10848 NamedDecl *FirstND = FirstFriend->getFriendDecl(); 10849 NamedDecl *SecondND = SecondFriend->getFriendDecl(); 10850 10851 TypeSourceInfo *FirstTSI = FirstFriend->getFriendType(); 10852 TypeSourceInfo *SecondTSI = SecondFriend->getFriendType(); 10853 10854 if (FirstND && SecondND) { 10855 ODRDiagDeclError(FirstRecord, FirstModule, 10856 FirstFriend->getFriendLoc(), 10857 FirstFriend->getSourceRange(), FriendFunction) 10858 << FirstND; 10859 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10860 SecondFriend->getSourceRange(), FriendFunction) 10861 << SecondND; 10862 10863 Diagnosed = true; 10864 break; 10865 } 10866 10867 if (FirstTSI && SecondTSI) { 10868 QualType FirstFriendType = FirstTSI->getType(); 10869 QualType SecondFriendType = SecondTSI->getType(); 10870 assert(ComputeQualTypeODRHash(FirstFriendType) != 10871 ComputeQualTypeODRHash(SecondFriendType)); 10872 ODRDiagDeclError(FirstRecord, FirstModule, 10873 FirstFriend->getFriendLoc(), 10874 FirstFriend->getSourceRange(), FriendType) 10875 << FirstFriendType; 10876 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10877 SecondFriend->getSourceRange(), FriendType) 10878 << SecondFriendType; 10879 Diagnosed = true; 10880 break; 10881 } 10882 10883 ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(), 10884 FirstFriend->getSourceRange(), FriendTypeFunction) 10885 << (FirstTSI == nullptr); 10886 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10887 SecondFriend->getSourceRange(), FriendTypeFunction) 10888 << (SecondTSI == nullptr); 10889 10890 Diagnosed = true; 10891 break; 10892 } 10893 case FunctionTemplate: { 10894 FunctionTemplateDecl *FirstTemplate = 10895 cast<FunctionTemplateDecl>(FirstDecl); 10896 FunctionTemplateDecl *SecondTemplate = 10897 cast<FunctionTemplateDecl>(SecondDecl); 10898 10899 TemplateParameterList *FirstTPL = 10900 FirstTemplate->getTemplateParameters(); 10901 TemplateParameterList *SecondTPL = 10902 SecondTemplate->getTemplateParameters(); 10903 10904 if (FirstTPL->size() != SecondTPL->size()) { 10905 ODRDiagDeclError(FirstRecord, FirstModule, 10906 FirstTemplate->getLocation(), 10907 FirstTemplate->getSourceRange(), 10908 FunctionTemplateDifferentNumberParameters) 10909 << FirstTemplate << FirstTPL->size(); 10910 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10911 SecondTemplate->getSourceRange(), 10912 FunctionTemplateDifferentNumberParameters) 10913 << SecondTemplate << SecondTPL->size(); 10914 10915 Diagnosed = true; 10916 break; 10917 } 10918 10919 bool ParameterMismatch = false; 10920 for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) { 10921 NamedDecl *FirstParam = FirstTPL->getParam(i); 10922 NamedDecl *SecondParam = SecondTPL->getParam(i); 10923 10924 if (FirstParam->getKind() != SecondParam->getKind()) { 10925 enum { 10926 TemplateTypeParameter, 10927 NonTypeTemplateParameter, 10928 TemplateTemplateParameter, 10929 }; 10930 auto GetParamType = [](NamedDecl *D) { 10931 switch (D->getKind()) { 10932 default: 10933 llvm_unreachable("Unexpected template parameter type"); 10934 case Decl::TemplateTypeParm: 10935 return TemplateTypeParameter; 10936 case Decl::NonTypeTemplateParm: 10937 return NonTypeTemplateParameter; 10938 case Decl::TemplateTemplateParm: 10939 return TemplateTemplateParameter; 10940 } 10941 }; 10942 10943 ODRDiagDeclError(FirstRecord, FirstModule, 10944 FirstTemplate->getLocation(), 10945 FirstTemplate->getSourceRange(), 10946 FunctionTemplateParameterDifferentKind) 10947 << FirstTemplate << (i + 1) << GetParamType(FirstParam); 10948 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10949 SecondTemplate->getSourceRange(), 10950 FunctionTemplateParameterDifferentKind) 10951 << SecondTemplate << (i + 1) << GetParamType(SecondParam); 10952 10953 ParameterMismatch = true; 10954 break; 10955 } 10956 10957 if (FirstParam->getName() != SecondParam->getName()) { 10958 ODRDiagDeclError( 10959 FirstRecord, FirstModule, FirstTemplate->getLocation(), 10960 FirstTemplate->getSourceRange(), FunctionTemplateParameterName) 10961 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier() 10962 << FirstParam; 10963 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10964 SecondTemplate->getSourceRange(), 10965 FunctionTemplateParameterName) 10966 << SecondTemplate << (i + 1) 10967 << (bool)SecondParam->getIdentifier() << SecondParam; 10968 ParameterMismatch = true; 10969 break; 10970 } 10971 10972 if (isa<TemplateTypeParmDecl>(FirstParam) && 10973 isa<TemplateTypeParmDecl>(SecondParam)) { 10974 TemplateTypeParmDecl *FirstTTPD = 10975 cast<TemplateTypeParmDecl>(FirstParam); 10976 TemplateTypeParmDecl *SecondTTPD = 10977 cast<TemplateTypeParmDecl>(SecondParam); 10978 bool HasFirstDefaultArgument = 10979 FirstTTPD->hasDefaultArgument() && 10980 !FirstTTPD->defaultArgumentWasInherited(); 10981 bool HasSecondDefaultArgument = 10982 SecondTTPD->hasDefaultArgument() && 10983 !SecondTTPD->defaultArgumentWasInherited(); 10984 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10985 ODRDiagDeclError(FirstRecord, FirstModule, 10986 FirstTemplate->getLocation(), 10987 FirstTemplate->getSourceRange(), 10988 FunctionTemplateParameterSingleDefaultArgument) 10989 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 10990 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10991 SecondTemplate->getSourceRange(), 10992 FunctionTemplateParameterSingleDefaultArgument) 10993 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 10994 ParameterMismatch = true; 10995 break; 10996 } 10997 10998 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 10999 QualType FirstType = FirstTTPD->getDefaultArgument(); 11000 QualType SecondType = SecondTTPD->getDefaultArgument(); 11001 if (ComputeQualTypeODRHash(FirstType) != 11002 ComputeQualTypeODRHash(SecondType)) { 11003 ODRDiagDeclError( 11004 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11005 FirstTemplate->getSourceRange(), 11006 FunctionTemplateParameterDifferentDefaultArgument) 11007 << FirstTemplate << (i + 1) << FirstType; 11008 ODRDiagDeclNote( 11009 SecondModule, SecondTemplate->getLocation(), 11010 SecondTemplate->getSourceRange(), 11011 FunctionTemplateParameterDifferentDefaultArgument) 11012 << SecondTemplate << (i + 1) << SecondType; 11013 ParameterMismatch = true; 11014 break; 11015 } 11016 } 11017 11018 if (FirstTTPD->isParameterPack() != 11019 SecondTTPD->isParameterPack()) { 11020 ODRDiagDeclError(FirstRecord, FirstModule, 11021 FirstTemplate->getLocation(), 11022 FirstTemplate->getSourceRange(), 11023 FunctionTemplatePackParameter) 11024 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 11025 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11026 SecondTemplate->getSourceRange(), 11027 FunctionTemplatePackParameter) 11028 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 11029 ParameterMismatch = true; 11030 break; 11031 } 11032 } 11033 11034 if (isa<TemplateTemplateParmDecl>(FirstParam) && 11035 isa<TemplateTemplateParmDecl>(SecondParam)) { 11036 TemplateTemplateParmDecl *FirstTTPD = 11037 cast<TemplateTemplateParmDecl>(FirstParam); 11038 TemplateTemplateParmDecl *SecondTTPD = 11039 cast<TemplateTemplateParmDecl>(SecondParam); 11040 11041 TemplateParameterList *FirstTPL = 11042 FirstTTPD->getTemplateParameters(); 11043 TemplateParameterList *SecondTPL = 11044 SecondTTPD->getTemplateParameters(); 11045 11046 if (ComputeTemplateParameterListODRHash(FirstTPL) != 11047 ComputeTemplateParameterListODRHash(SecondTPL)) { 11048 ODRDiagDeclError(FirstRecord, FirstModule, 11049 FirstTemplate->getLocation(), 11050 FirstTemplate->getSourceRange(), 11051 FunctionTemplateParameterDifferentType) 11052 << FirstTemplate << (i + 1); 11053 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11054 SecondTemplate->getSourceRange(), 11055 FunctionTemplateParameterDifferentType) 11056 << SecondTemplate << (i + 1); 11057 ParameterMismatch = true; 11058 break; 11059 } 11060 11061 bool HasFirstDefaultArgument = 11062 FirstTTPD->hasDefaultArgument() && 11063 !FirstTTPD->defaultArgumentWasInherited(); 11064 bool HasSecondDefaultArgument = 11065 SecondTTPD->hasDefaultArgument() && 11066 !SecondTTPD->defaultArgumentWasInherited(); 11067 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11068 ODRDiagDeclError(FirstRecord, FirstModule, 11069 FirstTemplate->getLocation(), 11070 FirstTemplate->getSourceRange(), 11071 FunctionTemplateParameterSingleDefaultArgument) 11072 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11073 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11074 SecondTemplate->getSourceRange(), 11075 FunctionTemplateParameterSingleDefaultArgument) 11076 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11077 ParameterMismatch = true; 11078 break; 11079 } 11080 11081 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11082 TemplateArgument FirstTA = 11083 FirstTTPD->getDefaultArgument().getArgument(); 11084 TemplateArgument SecondTA = 11085 SecondTTPD->getDefaultArgument().getArgument(); 11086 if (ComputeTemplateArgumentODRHash(FirstTA) != 11087 ComputeTemplateArgumentODRHash(SecondTA)) { 11088 ODRDiagDeclError( 11089 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11090 FirstTemplate->getSourceRange(), 11091 FunctionTemplateParameterDifferentDefaultArgument) 11092 << FirstTemplate << (i + 1) << FirstTA; 11093 ODRDiagDeclNote( 11094 SecondModule, SecondTemplate->getLocation(), 11095 SecondTemplate->getSourceRange(), 11096 FunctionTemplateParameterDifferentDefaultArgument) 11097 << SecondTemplate << (i + 1) << SecondTA; 11098 ParameterMismatch = true; 11099 break; 11100 } 11101 } 11102 11103 if (FirstTTPD->isParameterPack() != 11104 SecondTTPD->isParameterPack()) { 11105 ODRDiagDeclError(FirstRecord, FirstModule, 11106 FirstTemplate->getLocation(), 11107 FirstTemplate->getSourceRange(), 11108 FunctionTemplatePackParameter) 11109 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 11110 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11111 SecondTemplate->getSourceRange(), 11112 FunctionTemplatePackParameter) 11113 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 11114 ParameterMismatch = true; 11115 break; 11116 } 11117 } 11118 11119 if (isa<NonTypeTemplateParmDecl>(FirstParam) && 11120 isa<NonTypeTemplateParmDecl>(SecondParam)) { 11121 NonTypeTemplateParmDecl *FirstNTTPD = 11122 cast<NonTypeTemplateParmDecl>(FirstParam); 11123 NonTypeTemplateParmDecl *SecondNTTPD = 11124 cast<NonTypeTemplateParmDecl>(SecondParam); 11125 11126 QualType FirstType = FirstNTTPD->getType(); 11127 QualType SecondType = SecondNTTPD->getType(); 11128 if (ComputeQualTypeODRHash(FirstType) != 11129 ComputeQualTypeODRHash(SecondType)) { 11130 ODRDiagDeclError(FirstRecord, FirstModule, 11131 FirstTemplate->getLocation(), 11132 FirstTemplate->getSourceRange(), 11133 FunctionTemplateParameterDifferentType) 11134 << FirstTemplate << (i + 1); 11135 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11136 SecondTemplate->getSourceRange(), 11137 FunctionTemplateParameterDifferentType) 11138 << SecondTemplate << (i + 1); 11139 ParameterMismatch = true; 11140 break; 11141 } 11142 11143 bool HasFirstDefaultArgument = 11144 FirstNTTPD->hasDefaultArgument() && 11145 !FirstNTTPD->defaultArgumentWasInherited(); 11146 bool HasSecondDefaultArgument = 11147 SecondNTTPD->hasDefaultArgument() && 11148 !SecondNTTPD->defaultArgumentWasInherited(); 11149 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11150 ODRDiagDeclError(FirstRecord, FirstModule, 11151 FirstTemplate->getLocation(), 11152 FirstTemplate->getSourceRange(), 11153 FunctionTemplateParameterSingleDefaultArgument) 11154 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11155 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11156 SecondTemplate->getSourceRange(), 11157 FunctionTemplateParameterSingleDefaultArgument) 11158 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11159 ParameterMismatch = true; 11160 break; 11161 } 11162 11163 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11164 Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument(); 11165 Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument(); 11166 if (ComputeODRHash(FirstDefaultArgument) != 11167 ComputeODRHash(SecondDefaultArgument)) { 11168 ODRDiagDeclError( 11169 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11170 FirstTemplate->getSourceRange(), 11171 FunctionTemplateParameterDifferentDefaultArgument) 11172 << FirstTemplate << (i + 1) << FirstDefaultArgument; 11173 ODRDiagDeclNote( 11174 SecondModule, SecondTemplate->getLocation(), 11175 SecondTemplate->getSourceRange(), 11176 FunctionTemplateParameterDifferentDefaultArgument) 11177 << SecondTemplate << (i + 1) << SecondDefaultArgument; 11178 ParameterMismatch = true; 11179 break; 11180 } 11181 } 11182 11183 if (FirstNTTPD->isParameterPack() != 11184 SecondNTTPD->isParameterPack()) { 11185 ODRDiagDeclError(FirstRecord, FirstModule, 11186 FirstTemplate->getLocation(), 11187 FirstTemplate->getSourceRange(), 11188 FunctionTemplatePackParameter) 11189 << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack(); 11190 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11191 SecondTemplate->getSourceRange(), 11192 FunctionTemplatePackParameter) 11193 << SecondTemplate << (i + 1) 11194 << SecondNTTPD->isParameterPack(); 11195 ParameterMismatch = true; 11196 break; 11197 } 11198 } 11199 } 11200 11201 if (ParameterMismatch) { 11202 Diagnosed = true; 11203 break; 11204 } 11205 11206 break; 11207 } 11208 } 11209 11210 if (Diagnosed) 11211 continue; 11212 11213 Diag(FirstDecl->getLocation(), 11214 diag::err_module_odr_violation_mismatch_decl_unknown) 11215 << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType 11216 << FirstDecl->getSourceRange(); 11217 Diag(SecondDecl->getLocation(), 11218 diag::note_module_odr_violation_mismatch_decl_unknown) 11219 << SecondModule << FirstDiffType << SecondDecl->getSourceRange(); 11220 Diagnosed = true; 11221 } 11222 11223 if (!Diagnosed) { 11224 // All definitions are updates to the same declaration. This happens if a 11225 // module instantiates the declaration of a class template specialization 11226 // and two or more other modules instantiate its definition. 11227 // 11228 // FIXME: Indicate which modules had instantiations of this definition. 11229 // FIXME: How can this even happen? 11230 Diag(Merge.first->getLocation(), 11231 diag::err_module_odr_violation_different_instantiations) 11232 << Merge.first; 11233 } 11234 } 11235 11236 // Issue ODR failures diagnostics for functions. 11237 for (auto &Merge : FunctionOdrMergeFailures) { 11238 enum ODRFunctionDifference { 11239 ReturnType, 11240 ParameterName, 11241 ParameterType, 11242 ParameterSingleDefaultArgument, 11243 ParameterDifferentDefaultArgument, 11244 FunctionBody, 11245 }; 11246 11247 FunctionDecl *FirstFunction = Merge.first; 11248 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction); 11249 11250 bool Diagnosed = false; 11251 for (auto &SecondFunction : Merge.second) { 11252 11253 if (FirstFunction == SecondFunction) 11254 continue; 11255 11256 std::string SecondModule = 11257 getOwningModuleNameForDiagnostic(SecondFunction); 11258 11259 auto ODRDiagError = [FirstFunction, &FirstModule, 11260 this](SourceLocation Loc, SourceRange Range, 11261 ODRFunctionDifference DiffType) { 11262 return Diag(Loc, diag::err_module_odr_violation_function) 11263 << FirstFunction << FirstModule.empty() << FirstModule << Range 11264 << DiffType; 11265 }; 11266 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11267 SourceRange Range, 11268 ODRFunctionDifference DiffType) { 11269 return Diag(Loc, diag::note_module_odr_violation_function) 11270 << SecondModule << Range << DiffType; 11271 }; 11272 11273 if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) != 11274 ComputeQualTypeODRHash(SecondFunction->getReturnType())) { 11275 ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(), 11276 FirstFunction->getReturnTypeSourceRange(), ReturnType) 11277 << FirstFunction->getReturnType(); 11278 ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(), 11279 SecondFunction->getReturnTypeSourceRange(), ReturnType) 11280 << SecondFunction->getReturnType(); 11281 Diagnosed = true; 11282 break; 11283 } 11284 11285 assert(FirstFunction->param_size() == SecondFunction->param_size() && 11286 "Merged functions with different number of parameters"); 11287 11288 auto ParamSize = FirstFunction->param_size(); 11289 bool ParameterMismatch = false; 11290 for (unsigned I = 0; I < ParamSize; ++I) { 11291 auto *FirstParam = FirstFunction->getParamDecl(I); 11292 auto *SecondParam = SecondFunction->getParamDecl(I); 11293 11294 assert(getContext().hasSameType(FirstParam->getType(), 11295 SecondParam->getType()) && 11296 "Merged function has different parameter types."); 11297 11298 if (FirstParam->getDeclName() != SecondParam->getDeclName()) { 11299 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11300 ParameterName) 11301 << I + 1 << FirstParam->getDeclName(); 11302 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11303 ParameterName) 11304 << I + 1 << SecondParam->getDeclName(); 11305 ParameterMismatch = true; 11306 break; 11307 }; 11308 11309 QualType FirstParamType = FirstParam->getType(); 11310 QualType SecondParamType = SecondParam->getType(); 11311 if (FirstParamType != SecondParamType && 11312 ComputeQualTypeODRHash(FirstParamType) != 11313 ComputeQualTypeODRHash(SecondParamType)) { 11314 if (const DecayedType *ParamDecayedType = 11315 FirstParamType->getAs<DecayedType>()) { 11316 ODRDiagError(FirstParam->getLocation(), 11317 FirstParam->getSourceRange(), ParameterType) 11318 << (I + 1) << FirstParamType << true 11319 << ParamDecayedType->getOriginalType(); 11320 } else { 11321 ODRDiagError(FirstParam->getLocation(), 11322 FirstParam->getSourceRange(), ParameterType) 11323 << (I + 1) << FirstParamType << false; 11324 } 11325 11326 if (const DecayedType *ParamDecayedType = 11327 SecondParamType->getAs<DecayedType>()) { 11328 ODRDiagNote(SecondParam->getLocation(), 11329 SecondParam->getSourceRange(), ParameterType) 11330 << (I + 1) << SecondParamType << true 11331 << ParamDecayedType->getOriginalType(); 11332 } else { 11333 ODRDiagNote(SecondParam->getLocation(), 11334 SecondParam->getSourceRange(), ParameterType) 11335 << (I + 1) << SecondParamType << false; 11336 } 11337 ParameterMismatch = true; 11338 break; 11339 } 11340 11341 const Expr *FirstInit = FirstParam->getInit(); 11342 const Expr *SecondInit = SecondParam->getInit(); 11343 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 11344 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11345 ParameterSingleDefaultArgument) 11346 << (I + 1) << (FirstInit == nullptr) 11347 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 11348 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11349 ParameterSingleDefaultArgument) 11350 << (I + 1) << (SecondInit == nullptr) 11351 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 11352 ParameterMismatch = true; 11353 break; 11354 } 11355 11356 if (FirstInit && SecondInit && 11357 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11358 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11359 ParameterDifferentDefaultArgument) 11360 << (I + 1) << FirstInit->getSourceRange(); 11361 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11362 ParameterDifferentDefaultArgument) 11363 << (I + 1) << SecondInit->getSourceRange(); 11364 ParameterMismatch = true; 11365 break; 11366 } 11367 11368 assert(ComputeSubDeclODRHash(FirstParam) == 11369 ComputeSubDeclODRHash(SecondParam) && 11370 "Undiagnosed parameter difference."); 11371 } 11372 11373 if (ParameterMismatch) { 11374 Diagnosed = true; 11375 break; 11376 } 11377 11378 // If no error has been generated before now, assume the problem is in 11379 // the body and generate a message. 11380 ODRDiagError(FirstFunction->getLocation(), 11381 FirstFunction->getSourceRange(), FunctionBody); 11382 ODRDiagNote(SecondFunction->getLocation(), 11383 SecondFunction->getSourceRange(), FunctionBody); 11384 Diagnosed = true; 11385 break; 11386 } 11387 (void)Diagnosed; 11388 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11389 } 11390 11391 // Issue ODR failures diagnostics for enums. 11392 for (auto &Merge : EnumOdrMergeFailures) { 11393 enum ODREnumDifference { 11394 SingleScopedEnum, 11395 EnumTagKeywordMismatch, 11396 SingleSpecifiedType, 11397 DifferentSpecifiedTypes, 11398 DifferentNumberEnumConstants, 11399 EnumConstantName, 11400 EnumConstantSingleInitilizer, 11401 EnumConstantDifferentInitilizer, 11402 }; 11403 11404 // If we've already pointed out a specific problem with this enum, don't 11405 // bother issuing a general "something's different" diagnostic. 11406 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 11407 continue; 11408 11409 EnumDecl *FirstEnum = Merge.first; 11410 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum); 11411 11412 using DeclHashes = 11413 llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>; 11414 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum]( 11415 DeclHashes &Hashes, EnumDecl *Enum) { 11416 for (auto *D : Enum->decls()) { 11417 // Due to decl merging, the first EnumDecl is the parent of 11418 // Decls in both records. 11419 if (!ODRHash::isDeclToBeProcessed(D, FirstEnum)) 11420 continue; 11421 assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind"); 11422 Hashes.emplace_back(cast<EnumConstantDecl>(D), 11423 ComputeSubDeclODRHash(D)); 11424 } 11425 }; 11426 DeclHashes FirstHashes; 11427 PopulateHashes(FirstHashes, FirstEnum); 11428 bool Diagnosed = false; 11429 for (auto &SecondEnum : Merge.second) { 11430 11431 if (FirstEnum == SecondEnum) 11432 continue; 11433 11434 std::string SecondModule = 11435 getOwningModuleNameForDiagnostic(SecondEnum); 11436 11437 auto ODRDiagError = [FirstEnum, &FirstModule, 11438 this](SourceLocation Loc, SourceRange Range, 11439 ODREnumDifference DiffType) { 11440 return Diag(Loc, diag::err_module_odr_violation_enum) 11441 << FirstEnum << FirstModule.empty() << FirstModule << Range 11442 << DiffType; 11443 }; 11444 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11445 SourceRange Range, 11446 ODREnumDifference DiffType) { 11447 return Diag(Loc, diag::note_module_odr_violation_enum) 11448 << SecondModule << Range << DiffType; 11449 }; 11450 11451 if (FirstEnum->isScoped() != SecondEnum->isScoped()) { 11452 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11453 SingleScopedEnum) 11454 << FirstEnum->isScoped(); 11455 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11456 SingleScopedEnum) 11457 << SecondEnum->isScoped(); 11458 Diagnosed = true; 11459 continue; 11460 } 11461 11462 if (FirstEnum->isScoped() && SecondEnum->isScoped()) { 11463 if (FirstEnum->isScopedUsingClassTag() != 11464 SecondEnum->isScopedUsingClassTag()) { 11465 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11466 EnumTagKeywordMismatch) 11467 << FirstEnum->isScopedUsingClassTag(); 11468 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11469 EnumTagKeywordMismatch) 11470 << SecondEnum->isScopedUsingClassTag(); 11471 Diagnosed = true; 11472 continue; 11473 } 11474 } 11475 11476 QualType FirstUnderlyingType = 11477 FirstEnum->getIntegerTypeSourceInfo() 11478 ? FirstEnum->getIntegerTypeSourceInfo()->getType() 11479 : QualType(); 11480 QualType SecondUnderlyingType = 11481 SecondEnum->getIntegerTypeSourceInfo() 11482 ? SecondEnum->getIntegerTypeSourceInfo()->getType() 11483 : QualType(); 11484 if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) { 11485 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11486 SingleSpecifiedType) 11487 << !FirstUnderlyingType.isNull(); 11488 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11489 SingleSpecifiedType) 11490 << !SecondUnderlyingType.isNull(); 11491 Diagnosed = true; 11492 continue; 11493 } 11494 11495 if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) { 11496 if (ComputeQualTypeODRHash(FirstUnderlyingType) != 11497 ComputeQualTypeODRHash(SecondUnderlyingType)) { 11498 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11499 DifferentSpecifiedTypes) 11500 << FirstUnderlyingType; 11501 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11502 DifferentSpecifiedTypes) 11503 << SecondUnderlyingType; 11504 Diagnosed = true; 11505 continue; 11506 } 11507 } 11508 11509 DeclHashes SecondHashes; 11510 PopulateHashes(SecondHashes, SecondEnum); 11511 11512 if (FirstHashes.size() != SecondHashes.size()) { 11513 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11514 DifferentNumberEnumConstants) 11515 << (int)FirstHashes.size(); 11516 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11517 DifferentNumberEnumConstants) 11518 << (int)SecondHashes.size(); 11519 Diagnosed = true; 11520 continue; 11521 } 11522 11523 for (unsigned I = 0; I < FirstHashes.size(); ++I) { 11524 if (FirstHashes[I].second == SecondHashes[I].second) 11525 continue; 11526 const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first; 11527 const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first; 11528 11529 if (FirstEnumConstant->getDeclName() != 11530 SecondEnumConstant->getDeclName()) { 11531 11532 ODRDiagError(FirstEnumConstant->getLocation(), 11533 FirstEnumConstant->getSourceRange(), EnumConstantName) 11534 << I + 1 << FirstEnumConstant; 11535 ODRDiagNote(SecondEnumConstant->getLocation(), 11536 SecondEnumConstant->getSourceRange(), EnumConstantName) 11537 << I + 1 << SecondEnumConstant; 11538 Diagnosed = true; 11539 break; 11540 } 11541 11542 const Expr *FirstInit = FirstEnumConstant->getInitExpr(); 11543 const Expr *SecondInit = SecondEnumConstant->getInitExpr(); 11544 if (!FirstInit && !SecondInit) 11545 continue; 11546 11547 if (!FirstInit || !SecondInit) { 11548 ODRDiagError(FirstEnumConstant->getLocation(), 11549 FirstEnumConstant->getSourceRange(), 11550 EnumConstantSingleInitilizer) 11551 << I + 1 << FirstEnumConstant << (FirstInit != nullptr); 11552 ODRDiagNote(SecondEnumConstant->getLocation(), 11553 SecondEnumConstant->getSourceRange(), 11554 EnumConstantSingleInitilizer) 11555 << I + 1 << SecondEnumConstant << (SecondInit != nullptr); 11556 Diagnosed = true; 11557 break; 11558 } 11559 11560 if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11561 ODRDiagError(FirstEnumConstant->getLocation(), 11562 FirstEnumConstant->getSourceRange(), 11563 EnumConstantDifferentInitilizer) 11564 << I + 1 << FirstEnumConstant; 11565 ODRDiagNote(SecondEnumConstant->getLocation(), 11566 SecondEnumConstant->getSourceRange(), 11567 EnumConstantDifferentInitilizer) 11568 << I + 1 << SecondEnumConstant; 11569 Diagnosed = true; 11570 break; 11571 } 11572 } 11573 } 11574 11575 (void)Diagnosed; 11576 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11577 } 11578 } 11579 11580 void ASTReader::StartedDeserializing() { 11581 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get()) 11582 ReadTimer->startTimer(); 11583 } 11584 11585 void ASTReader::FinishedDeserializing() { 11586 assert(NumCurrentElementsDeserializing && 11587 "FinishedDeserializing not paired with StartedDeserializing"); 11588 if (NumCurrentElementsDeserializing == 1) { 11589 // We decrease NumCurrentElementsDeserializing only after pending actions 11590 // are finished, to avoid recursively re-calling finishPendingActions(). 11591 finishPendingActions(); 11592 } 11593 --NumCurrentElementsDeserializing; 11594 11595 if (NumCurrentElementsDeserializing == 0) { 11596 // Propagate exception specification and deduced type updates along 11597 // redeclaration chains. 11598 // 11599 // We do this now rather than in finishPendingActions because we want to 11600 // be able to walk the complete redeclaration chains of the updated decls. 11601 while (!PendingExceptionSpecUpdates.empty() || 11602 !PendingDeducedTypeUpdates.empty()) { 11603 auto ESUpdates = std::move(PendingExceptionSpecUpdates); 11604 PendingExceptionSpecUpdates.clear(); 11605 for (auto Update : ESUpdates) { 11606 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11607 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>(); 11608 auto ESI = FPT->getExtProtoInfo().ExceptionSpec; 11609 if (auto *Listener = getContext().getASTMutationListener()) 11610 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second)); 11611 for (auto *Redecl : Update.second->redecls()) 11612 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI); 11613 } 11614 11615 auto DTUpdates = std::move(PendingDeducedTypeUpdates); 11616 PendingDeducedTypeUpdates.clear(); 11617 for (auto Update : DTUpdates) { 11618 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11619 // FIXME: If the return type is already deduced, check that it matches. 11620 getContext().adjustDeducedFunctionResultType(Update.first, 11621 Update.second); 11622 } 11623 } 11624 11625 if (ReadTimer) 11626 ReadTimer->stopTimer(); 11627 11628 diagnoseOdrViolations(); 11629 11630 // We are not in recursive loading, so it's safe to pass the "interesting" 11631 // decls to the consumer. 11632 if (Consumer) 11633 PassInterestingDeclsToConsumer(); 11634 } 11635 } 11636 11637 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) { 11638 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) { 11639 // Remove any fake results before adding any real ones. 11640 auto It = PendingFakeLookupResults.find(II); 11641 if (It != PendingFakeLookupResults.end()) { 11642 for (auto *ND : It->second) 11643 SemaObj->IdResolver.RemoveDecl(ND); 11644 // FIXME: this works around module+PCH performance issue. 11645 // Rather than erase the result from the map, which is O(n), just clear 11646 // the vector of NamedDecls. 11647 It->second.clear(); 11648 } 11649 } 11650 11651 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) { 11652 SemaObj->TUScope->AddDecl(D); 11653 } else if (SemaObj->TUScope) { 11654 // Adding the decl to IdResolver may have failed because it was already in 11655 // (even though it was not added in scope). If it is already in, make sure 11656 // it gets in the scope as well. 11657 if (std::find(SemaObj->IdResolver.begin(Name), 11658 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end()) 11659 SemaObj->TUScope->AddDecl(D); 11660 } 11661 } 11662 11663 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache, 11664 ASTContext *Context, 11665 const PCHContainerReader &PCHContainerRdr, 11666 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, 11667 StringRef isysroot, bool DisableValidation, 11668 bool AllowASTWithCompilerErrors, 11669 bool AllowConfigurationMismatch, bool ValidateSystemInputs, 11670 bool ValidateASTInputFilesContent, bool UseGlobalIndex, 11671 std::unique_ptr<llvm::Timer> ReadTimer) 11672 : Listener(DisableValidation 11673 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP)) 11674 : cast<ASTReaderListener>(new PCHValidator(PP, *this))), 11675 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), 11676 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP), 11677 ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache, 11678 PCHContainerRdr, PP.getHeaderSearchInfo()), 11679 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot), 11680 DisableValidation(DisableValidation), 11681 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors), 11682 AllowConfigurationMismatch(AllowConfigurationMismatch), 11683 ValidateSystemInputs(ValidateSystemInputs), 11684 ValidateASTInputFilesContent(ValidateASTInputFilesContent), 11685 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) { 11686 SourceMgr.setExternalSLocEntrySource(this); 11687 11688 for (const auto &Ext : Extensions) { 11689 auto BlockName = Ext->getExtensionMetadata().BlockName; 11690 auto Known = ModuleFileExtensions.find(BlockName); 11691 if (Known != ModuleFileExtensions.end()) { 11692 Diags.Report(diag::warn_duplicate_module_file_extension) 11693 << BlockName; 11694 continue; 11695 } 11696 11697 ModuleFileExtensions.insert({BlockName, Ext}); 11698 } 11699 } 11700 11701 ASTReader::~ASTReader() { 11702 if (OwnsDeserializationListener) 11703 delete DeserializationListener; 11704 } 11705 11706 IdentifierResolver &ASTReader::getIdResolver() { 11707 return SemaObj ? SemaObj->IdResolver : DummyIdResolver; 11708 } 11709 11710 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor, 11711 unsigned AbbrevID) { 11712 Idx = 0; 11713 Record.clear(); 11714 return Cursor.readRecord(AbbrevID, Record); 11715 } 11716 //===----------------------------------------------------------------------===// 11717 //// OMPClauseReader implementation 11718 ////===----------------------------------------------------------------------===// 11719 11720 // This has to be in namespace clang because it's friended by all 11721 // of the OMP clauses. 11722 namespace clang { 11723 11724 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> { 11725 ASTRecordReader &Record; 11726 ASTContext &Context; 11727 11728 public: 11729 OMPClauseReader(ASTRecordReader &Record) 11730 : Record(Record), Context(Record.getContext()) {} 11731 11732 #define OMP_CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C); 11733 #include "llvm/Frontend/OpenMP/OMPKinds.def" 11734 OMPClause *readClause(); 11735 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C); 11736 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C); 11737 }; 11738 11739 } // end namespace clang 11740 11741 OMPClause *ASTRecordReader::readOMPClause() { 11742 return OMPClauseReader(*this).readClause(); 11743 } 11744 11745 OMPClause *OMPClauseReader::readClause() { 11746 OMPClause *C = nullptr; 11747 switch (llvm::omp::Clause(Record.readInt())) { 11748 case llvm::omp::OMPC_if: 11749 C = new (Context) OMPIfClause(); 11750 break; 11751 case llvm::omp::OMPC_final: 11752 C = new (Context) OMPFinalClause(); 11753 break; 11754 case llvm::omp::OMPC_num_threads: 11755 C = new (Context) OMPNumThreadsClause(); 11756 break; 11757 case llvm::omp::OMPC_safelen: 11758 C = new (Context) OMPSafelenClause(); 11759 break; 11760 case llvm::omp::OMPC_simdlen: 11761 C = new (Context) OMPSimdlenClause(); 11762 break; 11763 case llvm::omp::OMPC_allocator: 11764 C = new (Context) OMPAllocatorClause(); 11765 break; 11766 case llvm::omp::OMPC_collapse: 11767 C = new (Context) OMPCollapseClause(); 11768 break; 11769 case llvm::omp::OMPC_default: 11770 C = new (Context) OMPDefaultClause(); 11771 break; 11772 case llvm::omp::OMPC_proc_bind: 11773 C = new (Context) OMPProcBindClause(); 11774 break; 11775 case llvm::omp::OMPC_schedule: 11776 C = new (Context) OMPScheduleClause(); 11777 break; 11778 case llvm::omp::OMPC_ordered: 11779 C = OMPOrderedClause::CreateEmpty(Context, Record.readInt()); 11780 break; 11781 case llvm::omp::OMPC_nowait: 11782 C = new (Context) OMPNowaitClause(); 11783 break; 11784 case llvm::omp::OMPC_untied: 11785 C = new (Context) OMPUntiedClause(); 11786 break; 11787 case llvm::omp::OMPC_mergeable: 11788 C = new (Context) OMPMergeableClause(); 11789 break; 11790 case llvm::omp::OMPC_read: 11791 C = new (Context) OMPReadClause(); 11792 break; 11793 case llvm::omp::OMPC_write: 11794 C = new (Context) OMPWriteClause(); 11795 break; 11796 case llvm::omp::OMPC_update: 11797 C = OMPUpdateClause::CreateEmpty(Context, Record.readInt()); 11798 break; 11799 case llvm::omp::OMPC_capture: 11800 C = new (Context) OMPCaptureClause(); 11801 break; 11802 case llvm::omp::OMPC_seq_cst: 11803 C = new (Context) OMPSeqCstClause(); 11804 break; 11805 case llvm::omp::OMPC_acq_rel: 11806 C = new (Context) OMPAcqRelClause(); 11807 break; 11808 case llvm::omp::OMPC_acquire: 11809 C = new (Context) OMPAcquireClause(); 11810 break; 11811 case llvm::omp::OMPC_release: 11812 C = new (Context) OMPReleaseClause(); 11813 break; 11814 case llvm::omp::OMPC_relaxed: 11815 C = new (Context) OMPRelaxedClause(); 11816 break; 11817 case llvm::omp::OMPC_threads: 11818 C = new (Context) OMPThreadsClause(); 11819 break; 11820 case llvm::omp::OMPC_simd: 11821 C = new (Context) OMPSIMDClause(); 11822 break; 11823 case llvm::omp::OMPC_nogroup: 11824 C = new (Context) OMPNogroupClause(); 11825 break; 11826 case llvm::omp::OMPC_unified_address: 11827 C = new (Context) OMPUnifiedAddressClause(); 11828 break; 11829 case llvm::omp::OMPC_unified_shared_memory: 11830 C = new (Context) OMPUnifiedSharedMemoryClause(); 11831 break; 11832 case llvm::omp::OMPC_reverse_offload: 11833 C = new (Context) OMPReverseOffloadClause(); 11834 break; 11835 case llvm::omp::OMPC_dynamic_allocators: 11836 C = new (Context) OMPDynamicAllocatorsClause(); 11837 break; 11838 case llvm::omp::OMPC_atomic_default_mem_order: 11839 C = new (Context) OMPAtomicDefaultMemOrderClause(); 11840 break; 11841 case llvm::omp::OMPC_private: 11842 C = OMPPrivateClause::CreateEmpty(Context, Record.readInt()); 11843 break; 11844 case llvm::omp::OMPC_firstprivate: 11845 C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt()); 11846 break; 11847 case llvm::omp::OMPC_lastprivate: 11848 C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt()); 11849 break; 11850 case llvm::omp::OMPC_shared: 11851 C = OMPSharedClause::CreateEmpty(Context, Record.readInt()); 11852 break; 11853 case llvm::omp::OMPC_reduction: { 11854 unsigned N = Record.readInt(); 11855 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>(); 11856 C = OMPReductionClause::CreateEmpty(Context, N, Modifier); 11857 break; 11858 } 11859 case llvm::omp::OMPC_task_reduction: 11860 C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt()); 11861 break; 11862 case llvm::omp::OMPC_in_reduction: 11863 C = OMPInReductionClause::CreateEmpty(Context, Record.readInt()); 11864 break; 11865 case llvm::omp::OMPC_linear: 11866 C = OMPLinearClause::CreateEmpty(Context, Record.readInt()); 11867 break; 11868 case llvm::omp::OMPC_aligned: 11869 C = OMPAlignedClause::CreateEmpty(Context, Record.readInt()); 11870 break; 11871 case llvm::omp::OMPC_copyin: 11872 C = OMPCopyinClause::CreateEmpty(Context, Record.readInt()); 11873 break; 11874 case llvm::omp::OMPC_copyprivate: 11875 C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt()); 11876 break; 11877 case llvm::omp::OMPC_flush: 11878 C = OMPFlushClause::CreateEmpty(Context, Record.readInt()); 11879 break; 11880 case llvm::omp::OMPC_depobj: 11881 C = OMPDepobjClause::CreateEmpty(Context); 11882 break; 11883 case llvm::omp::OMPC_depend: { 11884 unsigned NumVars = Record.readInt(); 11885 unsigned NumLoops = Record.readInt(); 11886 C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops); 11887 break; 11888 } 11889 case llvm::omp::OMPC_device: 11890 C = new (Context) OMPDeviceClause(); 11891 break; 11892 case llvm::omp::OMPC_map: { 11893 OMPMappableExprListSizeTy Sizes; 11894 Sizes.NumVars = Record.readInt(); 11895 Sizes.NumUniqueDeclarations = Record.readInt(); 11896 Sizes.NumComponentLists = Record.readInt(); 11897 Sizes.NumComponents = Record.readInt(); 11898 C = OMPMapClause::CreateEmpty(Context, Sizes); 11899 break; 11900 } 11901 case llvm::omp::OMPC_num_teams: 11902 C = new (Context) OMPNumTeamsClause(); 11903 break; 11904 case llvm::omp::OMPC_thread_limit: 11905 C = new (Context) OMPThreadLimitClause(); 11906 break; 11907 case llvm::omp::OMPC_priority: 11908 C = new (Context) OMPPriorityClause(); 11909 break; 11910 case llvm::omp::OMPC_grainsize: 11911 C = new (Context) OMPGrainsizeClause(); 11912 break; 11913 case llvm::omp::OMPC_num_tasks: 11914 C = new (Context) OMPNumTasksClause(); 11915 break; 11916 case llvm::omp::OMPC_hint: 11917 C = new (Context) OMPHintClause(); 11918 break; 11919 case llvm::omp::OMPC_dist_schedule: 11920 C = new (Context) OMPDistScheduleClause(); 11921 break; 11922 case llvm::omp::OMPC_defaultmap: 11923 C = new (Context) OMPDefaultmapClause(); 11924 break; 11925 case llvm::omp::OMPC_to: { 11926 OMPMappableExprListSizeTy Sizes; 11927 Sizes.NumVars = Record.readInt(); 11928 Sizes.NumUniqueDeclarations = Record.readInt(); 11929 Sizes.NumComponentLists = Record.readInt(); 11930 Sizes.NumComponents = Record.readInt(); 11931 C = OMPToClause::CreateEmpty(Context, Sizes); 11932 break; 11933 } 11934 case llvm::omp::OMPC_from: { 11935 OMPMappableExprListSizeTy Sizes; 11936 Sizes.NumVars = Record.readInt(); 11937 Sizes.NumUniqueDeclarations = Record.readInt(); 11938 Sizes.NumComponentLists = Record.readInt(); 11939 Sizes.NumComponents = Record.readInt(); 11940 C = OMPFromClause::CreateEmpty(Context, Sizes); 11941 break; 11942 } 11943 case llvm::omp::OMPC_use_device_ptr: { 11944 OMPMappableExprListSizeTy Sizes; 11945 Sizes.NumVars = Record.readInt(); 11946 Sizes.NumUniqueDeclarations = Record.readInt(); 11947 Sizes.NumComponentLists = Record.readInt(); 11948 Sizes.NumComponents = Record.readInt(); 11949 C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes); 11950 break; 11951 } 11952 case llvm::omp::OMPC_use_device_addr: { 11953 OMPMappableExprListSizeTy Sizes; 11954 Sizes.NumVars = Record.readInt(); 11955 Sizes.NumUniqueDeclarations = Record.readInt(); 11956 Sizes.NumComponentLists = Record.readInt(); 11957 Sizes.NumComponents = Record.readInt(); 11958 C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes); 11959 break; 11960 } 11961 case llvm::omp::OMPC_is_device_ptr: { 11962 OMPMappableExprListSizeTy Sizes; 11963 Sizes.NumVars = Record.readInt(); 11964 Sizes.NumUniqueDeclarations = Record.readInt(); 11965 Sizes.NumComponentLists = Record.readInt(); 11966 Sizes.NumComponents = Record.readInt(); 11967 C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes); 11968 break; 11969 } 11970 case llvm::omp::OMPC_allocate: 11971 C = OMPAllocateClause::CreateEmpty(Context, Record.readInt()); 11972 break; 11973 case llvm::omp::OMPC_nontemporal: 11974 C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt()); 11975 break; 11976 case llvm::omp::OMPC_inclusive: 11977 C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt()); 11978 break; 11979 case llvm::omp::OMPC_exclusive: 11980 C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt()); 11981 break; 11982 case llvm::omp::OMPC_order: 11983 C = new (Context) OMPOrderClause(); 11984 break; 11985 case llvm::omp::OMPC_destroy: 11986 C = new (Context) OMPDestroyClause(); 11987 break; 11988 case llvm::omp::OMPC_detach: 11989 C = new (Context) OMPDetachClause(); 11990 break; 11991 case llvm::omp::OMPC_uses_allocators: 11992 C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt()); 11993 break; 11994 case llvm::omp::OMPC_affinity: 11995 C = OMPAffinityClause::CreateEmpty(Context, Record.readInt()); 11996 break; 11997 #define OMP_CLAUSE_NO_CLASS(Enum, Str) \ 11998 case llvm::omp::Enum: \ 11999 break; 12000 #include "llvm/Frontend/OpenMP/OMPKinds.def" 12001 default: 12002 break; 12003 } 12004 assert(C && "Unknown OMPClause type"); 12005 12006 Visit(C); 12007 C->setLocStart(Record.readSourceLocation()); 12008 C->setLocEnd(Record.readSourceLocation()); 12009 12010 return C; 12011 } 12012 12013 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) { 12014 C->setPreInitStmt(Record.readSubStmt(), 12015 static_cast<OpenMPDirectiveKind>(Record.readInt())); 12016 } 12017 12018 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) { 12019 VisitOMPClauseWithPreInit(C); 12020 C->setPostUpdateExpr(Record.readSubExpr()); 12021 } 12022 12023 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) { 12024 VisitOMPClauseWithPreInit(C); 12025 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt())); 12026 C->setNameModifierLoc(Record.readSourceLocation()); 12027 C->setColonLoc(Record.readSourceLocation()); 12028 C->setCondition(Record.readSubExpr()); 12029 C->setLParenLoc(Record.readSourceLocation()); 12030 } 12031 12032 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) { 12033 VisitOMPClauseWithPreInit(C); 12034 C->setCondition(Record.readSubExpr()); 12035 C->setLParenLoc(Record.readSourceLocation()); 12036 } 12037 12038 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) { 12039 VisitOMPClauseWithPreInit(C); 12040 C->setNumThreads(Record.readSubExpr()); 12041 C->setLParenLoc(Record.readSourceLocation()); 12042 } 12043 12044 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) { 12045 C->setSafelen(Record.readSubExpr()); 12046 C->setLParenLoc(Record.readSourceLocation()); 12047 } 12048 12049 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) { 12050 C->setSimdlen(Record.readSubExpr()); 12051 C->setLParenLoc(Record.readSourceLocation()); 12052 } 12053 12054 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) { 12055 C->setAllocator(Record.readExpr()); 12056 C->setLParenLoc(Record.readSourceLocation()); 12057 } 12058 12059 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) { 12060 C->setNumForLoops(Record.readSubExpr()); 12061 C->setLParenLoc(Record.readSourceLocation()); 12062 } 12063 12064 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) { 12065 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt())); 12066 C->setLParenLoc(Record.readSourceLocation()); 12067 C->setDefaultKindKwLoc(Record.readSourceLocation()); 12068 } 12069 12070 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) { 12071 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt())); 12072 C->setLParenLoc(Record.readSourceLocation()); 12073 C->setProcBindKindKwLoc(Record.readSourceLocation()); 12074 } 12075 12076 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) { 12077 VisitOMPClauseWithPreInit(C); 12078 C->setScheduleKind( 12079 static_cast<OpenMPScheduleClauseKind>(Record.readInt())); 12080 C->setFirstScheduleModifier( 12081 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 12082 C->setSecondScheduleModifier( 12083 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 12084 C->setChunkSize(Record.readSubExpr()); 12085 C->setLParenLoc(Record.readSourceLocation()); 12086 C->setFirstScheduleModifierLoc(Record.readSourceLocation()); 12087 C->setSecondScheduleModifierLoc(Record.readSourceLocation()); 12088 C->setScheduleKindLoc(Record.readSourceLocation()); 12089 C->setCommaLoc(Record.readSourceLocation()); 12090 } 12091 12092 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) { 12093 C->setNumForLoops(Record.readSubExpr()); 12094 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 12095 C->setLoopNumIterations(I, Record.readSubExpr()); 12096 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 12097 C->setLoopCounter(I, Record.readSubExpr()); 12098 C->setLParenLoc(Record.readSourceLocation()); 12099 } 12100 12101 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) { 12102 C->setEventHandler(Record.readSubExpr()); 12103 C->setLParenLoc(Record.readSourceLocation()); 12104 } 12105 12106 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {} 12107 12108 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {} 12109 12110 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {} 12111 12112 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {} 12113 12114 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {} 12115 12116 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) { 12117 if (C->isExtended()) { 12118 C->setLParenLoc(Record.readSourceLocation()); 12119 C->setArgumentLoc(Record.readSourceLocation()); 12120 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>()); 12121 } 12122 } 12123 12124 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {} 12125 12126 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {} 12127 12128 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {} 12129 12130 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {} 12131 12132 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {} 12133 12134 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {} 12135 12136 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {} 12137 12138 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {} 12139 12140 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {} 12141 12142 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *) {} 12143 12144 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {} 12145 12146 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause( 12147 OMPUnifiedSharedMemoryClause *) {} 12148 12149 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {} 12150 12151 void 12152 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) { 12153 } 12154 12155 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause( 12156 OMPAtomicDefaultMemOrderClause *C) { 12157 C->setAtomicDefaultMemOrderKind( 12158 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt())); 12159 C->setLParenLoc(Record.readSourceLocation()); 12160 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation()); 12161 } 12162 12163 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) { 12164 C->setLParenLoc(Record.readSourceLocation()); 12165 unsigned NumVars = C->varlist_size(); 12166 SmallVector<Expr *, 16> Vars; 12167 Vars.reserve(NumVars); 12168 for (unsigned i = 0; i != NumVars; ++i) 12169 Vars.push_back(Record.readSubExpr()); 12170 C->setVarRefs(Vars); 12171 Vars.clear(); 12172 for (unsigned i = 0; i != NumVars; ++i) 12173 Vars.push_back(Record.readSubExpr()); 12174 C->setPrivateCopies(Vars); 12175 } 12176 12177 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) { 12178 VisitOMPClauseWithPreInit(C); 12179 C->setLParenLoc(Record.readSourceLocation()); 12180 unsigned NumVars = C->varlist_size(); 12181 SmallVector<Expr *, 16> Vars; 12182 Vars.reserve(NumVars); 12183 for (unsigned i = 0; i != NumVars; ++i) 12184 Vars.push_back(Record.readSubExpr()); 12185 C->setVarRefs(Vars); 12186 Vars.clear(); 12187 for (unsigned i = 0; i != NumVars; ++i) 12188 Vars.push_back(Record.readSubExpr()); 12189 C->setPrivateCopies(Vars); 12190 Vars.clear(); 12191 for (unsigned i = 0; i != NumVars; ++i) 12192 Vars.push_back(Record.readSubExpr()); 12193 C->setInits(Vars); 12194 } 12195 12196 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) { 12197 VisitOMPClauseWithPostUpdate(C); 12198 C->setLParenLoc(Record.readSourceLocation()); 12199 C->setKind(Record.readEnum<OpenMPLastprivateModifier>()); 12200 C->setKindLoc(Record.readSourceLocation()); 12201 C->setColonLoc(Record.readSourceLocation()); 12202 unsigned NumVars = C->varlist_size(); 12203 SmallVector<Expr *, 16> Vars; 12204 Vars.reserve(NumVars); 12205 for (unsigned i = 0; i != NumVars; ++i) 12206 Vars.push_back(Record.readSubExpr()); 12207 C->setVarRefs(Vars); 12208 Vars.clear(); 12209 for (unsigned i = 0; i != NumVars; ++i) 12210 Vars.push_back(Record.readSubExpr()); 12211 C->setPrivateCopies(Vars); 12212 Vars.clear(); 12213 for (unsigned i = 0; i != NumVars; ++i) 12214 Vars.push_back(Record.readSubExpr()); 12215 C->setSourceExprs(Vars); 12216 Vars.clear(); 12217 for (unsigned i = 0; i != NumVars; ++i) 12218 Vars.push_back(Record.readSubExpr()); 12219 C->setDestinationExprs(Vars); 12220 Vars.clear(); 12221 for (unsigned i = 0; i != NumVars; ++i) 12222 Vars.push_back(Record.readSubExpr()); 12223 C->setAssignmentOps(Vars); 12224 } 12225 12226 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) { 12227 C->setLParenLoc(Record.readSourceLocation()); 12228 unsigned NumVars = C->varlist_size(); 12229 SmallVector<Expr *, 16> Vars; 12230 Vars.reserve(NumVars); 12231 for (unsigned i = 0; i != NumVars; ++i) 12232 Vars.push_back(Record.readSubExpr()); 12233 C->setVarRefs(Vars); 12234 } 12235 12236 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) { 12237 VisitOMPClauseWithPostUpdate(C); 12238 C->setLParenLoc(Record.readSourceLocation()); 12239 C->setModifierLoc(Record.readSourceLocation()); 12240 C->setColonLoc(Record.readSourceLocation()); 12241 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12242 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12243 C->setQualifierLoc(NNSL); 12244 C->setNameInfo(DNI); 12245 12246 unsigned NumVars = C->varlist_size(); 12247 SmallVector<Expr *, 16> Vars; 12248 Vars.reserve(NumVars); 12249 for (unsigned i = 0; i != NumVars; ++i) 12250 Vars.push_back(Record.readSubExpr()); 12251 C->setVarRefs(Vars); 12252 Vars.clear(); 12253 for (unsigned i = 0; i != NumVars; ++i) 12254 Vars.push_back(Record.readSubExpr()); 12255 C->setPrivates(Vars); 12256 Vars.clear(); 12257 for (unsigned i = 0; i != NumVars; ++i) 12258 Vars.push_back(Record.readSubExpr()); 12259 C->setLHSExprs(Vars); 12260 Vars.clear(); 12261 for (unsigned i = 0; i != NumVars; ++i) 12262 Vars.push_back(Record.readSubExpr()); 12263 C->setRHSExprs(Vars); 12264 Vars.clear(); 12265 for (unsigned i = 0; i != NumVars; ++i) 12266 Vars.push_back(Record.readSubExpr()); 12267 C->setReductionOps(Vars); 12268 if (C->getModifier() == OMPC_REDUCTION_inscan) { 12269 Vars.clear(); 12270 for (unsigned i = 0; i != NumVars; ++i) 12271 Vars.push_back(Record.readSubExpr()); 12272 C->setInscanCopyOps(Vars); 12273 Vars.clear(); 12274 for (unsigned i = 0; i != NumVars; ++i) 12275 Vars.push_back(Record.readSubExpr()); 12276 C->setInscanCopyArrayTemps(Vars); 12277 Vars.clear(); 12278 for (unsigned i = 0; i != NumVars; ++i) 12279 Vars.push_back(Record.readSubExpr()); 12280 C->setInscanCopyArrayElems(Vars); 12281 } 12282 } 12283 12284 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) { 12285 VisitOMPClauseWithPostUpdate(C); 12286 C->setLParenLoc(Record.readSourceLocation()); 12287 C->setColonLoc(Record.readSourceLocation()); 12288 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12289 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12290 C->setQualifierLoc(NNSL); 12291 C->setNameInfo(DNI); 12292 12293 unsigned NumVars = C->varlist_size(); 12294 SmallVector<Expr *, 16> Vars; 12295 Vars.reserve(NumVars); 12296 for (unsigned I = 0; I != NumVars; ++I) 12297 Vars.push_back(Record.readSubExpr()); 12298 C->setVarRefs(Vars); 12299 Vars.clear(); 12300 for (unsigned I = 0; I != NumVars; ++I) 12301 Vars.push_back(Record.readSubExpr()); 12302 C->setPrivates(Vars); 12303 Vars.clear(); 12304 for (unsigned I = 0; I != NumVars; ++I) 12305 Vars.push_back(Record.readSubExpr()); 12306 C->setLHSExprs(Vars); 12307 Vars.clear(); 12308 for (unsigned I = 0; I != NumVars; ++I) 12309 Vars.push_back(Record.readSubExpr()); 12310 C->setRHSExprs(Vars); 12311 Vars.clear(); 12312 for (unsigned I = 0; I != NumVars; ++I) 12313 Vars.push_back(Record.readSubExpr()); 12314 C->setReductionOps(Vars); 12315 } 12316 12317 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) { 12318 VisitOMPClauseWithPostUpdate(C); 12319 C->setLParenLoc(Record.readSourceLocation()); 12320 C->setColonLoc(Record.readSourceLocation()); 12321 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12322 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12323 C->setQualifierLoc(NNSL); 12324 C->setNameInfo(DNI); 12325 12326 unsigned NumVars = C->varlist_size(); 12327 SmallVector<Expr *, 16> Vars; 12328 Vars.reserve(NumVars); 12329 for (unsigned I = 0; I != NumVars; ++I) 12330 Vars.push_back(Record.readSubExpr()); 12331 C->setVarRefs(Vars); 12332 Vars.clear(); 12333 for (unsigned I = 0; I != NumVars; ++I) 12334 Vars.push_back(Record.readSubExpr()); 12335 C->setPrivates(Vars); 12336 Vars.clear(); 12337 for (unsigned I = 0; I != NumVars; ++I) 12338 Vars.push_back(Record.readSubExpr()); 12339 C->setLHSExprs(Vars); 12340 Vars.clear(); 12341 for (unsigned I = 0; I != NumVars; ++I) 12342 Vars.push_back(Record.readSubExpr()); 12343 C->setRHSExprs(Vars); 12344 Vars.clear(); 12345 for (unsigned I = 0; I != NumVars; ++I) 12346 Vars.push_back(Record.readSubExpr()); 12347 C->setReductionOps(Vars); 12348 Vars.clear(); 12349 for (unsigned I = 0; I != NumVars; ++I) 12350 Vars.push_back(Record.readSubExpr()); 12351 C->setTaskgroupDescriptors(Vars); 12352 } 12353 12354 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) { 12355 VisitOMPClauseWithPostUpdate(C); 12356 C->setLParenLoc(Record.readSourceLocation()); 12357 C->setColonLoc(Record.readSourceLocation()); 12358 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt())); 12359 C->setModifierLoc(Record.readSourceLocation()); 12360 unsigned NumVars = C->varlist_size(); 12361 SmallVector<Expr *, 16> Vars; 12362 Vars.reserve(NumVars); 12363 for (unsigned i = 0; i != NumVars; ++i) 12364 Vars.push_back(Record.readSubExpr()); 12365 C->setVarRefs(Vars); 12366 Vars.clear(); 12367 for (unsigned i = 0; i != NumVars; ++i) 12368 Vars.push_back(Record.readSubExpr()); 12369 C->setPrivates(Vars); 12370 Vars.clear(); 12371 for (unsigned i = 0; i != NumVars; ++i) 12372 Vars.push_back(Record.readSubExpr()); 12373 C->setInits(Vars); 12374 Vars.clear(); 12375 for (unsigned i = 0; i != NumVars; ++i) 12376 Vars.push_back(Record.readSubExpr()); 12377 C->setUpdates(Vars); 12378 Vars.clear(); 12379 for (unsigned i = 0; i != NumVars; ++i) 12380 Vars.push_back(Record.readSubExpr()); 12381 C->setFinals(Vars); 12382 C->setStep(Record.readSubExpr()); 12383 C->setCalcStep(Record.readSubExpr()); 12384 Vars.clear(); 12385 for (unsigned I = 0; I != NumVars + 1; ++I) 12386 Vars.push_back(Record.readSubExpr()); 12387 C->setUsedExprs(Vars); 12388 } 12389 12390 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) { 12391 C->setLParenLoc(Record.readSourceLocation()); 12392 C->setColonLoc(Record.readSourceLocation()); 12393 unsigned NumVars = C->varlist_size(); 12394 SmallVector<Expr *, 16> Vars; 12395 Vars.reserve(NumVars); 12396 for (unsigned i = 0; i != NumVars; ++i) 12397 Vars.push_back(Record.readSubExpr()); 12398 C->setVarRefs(Vars); 12399 C->setAlignment(Record.readSubExpr()); 12400 } 12401 12402 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) { 12403 C->setLParenLoc(Record.readSourceLocation()); 12404 unsigned NumVars = C->varlist_size(); 12405 SmallVector<Expr *, 16> Exprs; 12406 Exprs.reserve(NumVars); 12407 for (unsigned i = 0; i != NumVars; ++i) 12408 Exprs.push_back(Record.readSubExpr()); 12409 C->setVarRefs(Exprs); 12410 Exprs.clear(); 12411 for (unsigned i = 0; i != NumVars; ++i) 12412 Exprs.push_back(Record.readSubExpr()); 12413 C->setSourceExprs(Exprs); 12414 Exprs.clear(); 12415 for (unsigned i = 0; i != NumVars; ++i) 12416 Exprs.push_back(Record.readSubExpr()); 12417 C->setDestinationExprs(Exprs); 12418 Exprs.clear(); 12419 for (unsigned i = 0; i != NumVars; ++i) 12420 Exprs.push_back(Record.readSubExpr()); 12421 C->setAssignmentOps(Exprs); 12422 } 12423 12424 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) { 12425 C->setLParenLoc(Record.readSourceLocation()); 12426 unsigned NumVars = C->varlist_size(); 12427 SmallVector<Expr *, 16> Exprs; 12428 Exprs.reserve(NumVars); 12429 for (unsigned i = 0; i != NumVars; ++i) 12430 Exprs.push_back(Record.readSubExpr()); 12431 C->setVarRefs(Exprs); 12432 Exprs.clear(); 12433 for (unsigned i = 0; i != NumVars; ++i) 12434 Exprs.push_back(Record.readSubExpr()); 12435 C->setSourceExprs(Exprs); 12436 Exprs.clear(); 12437 for (unsigned i = 0; i != NumVars; ++i) 12438 Exprs.push_back(Record.readSubExpr()); 12439 C->setDestinationExprs(Exprs); 12440 Exprs.clear(); 12441 for (unsigned i = 0; i != NumVars; ++i) 12442 Exprs.push_back(Record.readSubExpr()); 12443 C->setAssignmentOps(Exprs); 12444 } 12445 12446 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) { 12447 C->setLParenLoc(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 } 12455 12456 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) { 12457 C->setDepobj(Record.readSubExpr()); 12458 C->setLParenLoc(Record.readSourceLocation()); 12459 } 12460 12461 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) { 12462 C->setLParenLoc(Record.readSourceLocation()); 12463 C->setModifier(Record.readSubExpr()); 12464 C->setDependencyKind( 12465 static_cast<OpenMPDependClauseKind>(Record.readInt())); 12466 C->setDependencyLoc(Record.readSourceLocation()); 12467 C->setColonLoc(Record.readSourceLocation()); 12468 unsigned NumVars = C->varlist_size(); 12469 SmallVector<Expr *, 16> Vars; 12470 Vars.reserve(NumVars); 12471 for (unsigned I = 0; I != NumVars; ++I) 12472 Vars.push_back(Record.readSubExpr()); 12473 C->setVarRefs(Vars); 12474 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I) 12475 C->setLoopData(I, Record.readSubExpr()); 12476 } 12477 12478 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) { 12479 VisitOMPClauseWithPreInit(C); 12480 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>()); 12481 C->setDevice(Record.readSubExpr()); 12482 C->setModifierLoc(Record.readSourceLocation()); 12483 C->setLParenLoc(Record.readSourceLocation()); 12484 } 12485 12486 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) { 12487 C->setLParenLoc(Record.readSourceLocation()); 12488 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) { 12489 C->setMapTypeModifier( 12490 I, static_cast<OpenMPMapModifierKind>(Record.readInt())); 12491 C->setMapTypeModifierLoc(I, Record.readSourceLocation()); 12492 } 12493 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12494 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12495 C->setMapType( 12496 static_cast<OpenMPMapClauseKind>(Record.readInt())); 12497 C->setMapLoc(Record.readSourceLocation()); 12498 C->setColonLoc(Record.readSourceLocation()); 12499 auto NumVars = C->varlist_size(); 12500 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12501 auto TotalLists = C->getTotalComponentListNum(); 12502 auto TotalComponents = C->getTotalComponentsNum(); 12503 12504 SmallVector<Expr *, 16> Vars; 12505 Vars.reserve(NumVars); 12506 for (unsigned i = 0; i != NumVars; ++i) 12507 Vars.push_back(Record.readExpr()); 12508 C->setVarRefs(Vars); 12509 12510 SmallVector<Expr *, 16> UDMappers; 12511 UDMappers.reserve(NumVars); 12512 for (unsigned I = 0; I < NumVars; ++I) 12513 UDMappers.push_back(Record.readExpr()); 12514 C->setUDMapperRefs(UDMappers); 12515 12516 SmallVector<ValueDecl *, 16> Decls; 12517 Decls.reserve(UniqueDecls); 12518 for (unsigned i = 0; i < UniqueDecls; ++i) 12519 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12520 C->setUniqueDecls(Decls); 12521 12522 SmallVector<unsigned, 16> ListsPerDecl; 12523 ListsPerDecl.reserve(UniqueDecls); 12524 for (unsigned i = 0; i < UniqueDecls; ++i) 12525 ListsPerDecl.push_back(Record.readInt()); 12526 C->setDeclNumLists(ListsPerDecl); 12527 12528 SmallVector<unsigned, 32> ListSizes; 12529 ListSizes.reserve(TotalLists); 12530 for (unsigned i = 0; i < TotalLists; ++i) 12531 ListSizes.push_back(Record.readInt()); 12532 C->setComponentListSizes(ListSizes); 12533 12534 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12535 Components.reserve(TotalComponents); 12536 for (unsigned i = 0; i < TotalComponents; ++i) { 12537 Expr *AssociatedExpr = Record.readExpr(); 12538 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12539 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12540 AssociatedExpr, AssociatedDecl)); 12541 } 12542 C->setComponents(Components, ListSizes); 12543 } 12544 12545 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) { 12546 C->setLParenLoc(Record.readSourceLocation()); 12547 C->setColonLoc(Record.readSourceLocation()); 12548 C->setAllocator(Record.readSubExpr()); 12549 unsigned NumVars = C->varlist_size(); 12550 SmallVector<Expr *, 16> Vars; 12551 Vars.reserve(NumVars); 12552 for (unsigned i = 0; i != NumVars; ++i) 12553 Vars.push_back(Record.readSubExpr()); 12554 C->setVarRefs(Vars); 12555 } 12556 12557 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) { 12558 VisitOMPClauseWithPreInit(C); 12559 C->setNumTeams(Record.readSubExpr()); 12560 C->setLParenLoc(Record.readSourceLocation()); 12561 } 12562 12563 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) { 12564 VisitOMPClauseWithPreInit(C); 12565 C->setThreadLimit(Record.readSubExpr()); 12566 C->setLParenLoc(Record.readSourceLocation()); 12567 } 12568 12569 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) { 12570 VisitOMPClauseWithPreInit(C); 12571 C->setPriority(Record.readSubExpr()); 12572 C->setLParenLoc(Record.readSourceLocation()); 12573 } 12574 12575 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) { 12576 VisitOMPClauseWithPreInit(C); 12577 C->setGrainsize(Record.readSubExpr()); 12578 C->setLParenLoc(Record.readSourceLocation()); 12579 } 12580 12581 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) { 12582 VisitOMPClauseWithPreInit(C); 12583 C->setNumTasks(Record.readSubExpr()); 12584 C->setLParenLoc(Record.readSourceLocation()); 12585 } 12586 12587 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) { 12588 C->setHint(Record.readSubExpr()); 12589 C->setLParenLoc(Record.readSourceLocation()); 12590 } 12591 12592 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) { 12593 VisitOMPClauseWithPreInit(C); 12594 C->setDistScheduleKind( 12595 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt())); 12596 C->setChunkSize(Record.readSubExpr()); 12597 C->setLParenLoc(Record.readSourceLocation()); 12598 C->setDistScheduleKindLoc(Record.readSourceLocation()); 12599 C->setCommaLoc(Record.readSourceLocation()); 12600 } 12601 12602 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) { 12603 C->setDefaultmapKind( 12604 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt())); 12605 C->setDefaultmapModifier( 12606 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt())); 12607 C->setLParenLoc(Record.readSourceLocation()); 12608 C->setDefaultmapModifierLoc(Record.readSourceLocation()); 12609 C->setDefaultmapKindLoc(Record.readSourceLocation()); 12610 } 12611 12612 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) { 12613 C->setLParenLoc(Record.readSourceLocation()); 12614 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) { 12615 C->setMotionModifier( 12616 I, static_cast<OpenMPMotionModifierKind>(Record.readInt())); 12617 C->setMotionModifierLoc(I, Record.readSourceLocation()); 12618 } 12619 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12620 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12621 C->setColonLoc(Record.readSourceLocation()); 12622 auto NumVars = C->varlist_size(); 12623 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12624 auto TotalLists = C->getTotalComponentListNum(); 12625 auto TotalComponents = C->getTotalComponentsNum(); 12626 12627 SmallVector<Expr *, 16> Vars; 12628 Vars.reserve(NumVars); 12629 for (unsigned i = 0; i != NumVars; ++i) 12630 Vars.push_back(Record.readSubExpr()); 12631 C->setVarRefs(Vars); 12632 12633 SmallVector<Expr *, 16> UDMappers; 12634 UDMappers.reserve(NumVars); 12635 for (unsigned I = 0; I < NumVars; ++I) 12636 UDMappers.push_back(Record.readSubExpr()); 12637 C->setUDMapperRefs(UDMappers); 12638 12639 SmallVector<ValueDecl *, 16> Decls; 12640 Decls.reserve(UniqueDecls); 12641 for (unsigned i = 0; i < UniqueDecls; ++i) 12642 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12643 C->setUniqueDecls(Decls); 12644 12645 SmallVector<unsigned, 16> ListsPerDecl; 12646 ListsPerDecl.reserve(UniqueDecls); 12647 for (unsigned i = 0; i < UniqueDecls; ++i) 12648 ListsPerDecl.push_back(Record.readInt()); 12649 C->setDeclNumLists(ListsPerDecl); 12650 12651 SmallVector<unsigned, 32> ListSizes; 12652 ListSizes.reserve(TotalLists); 12653 for (unsigned i = 0; i < TotalLists; ++i) 12654 ListSizes.push_back(Record.readInt()); 12655 C->setComponentListSizes(ListSizes); 12656 12657 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12658 Components.reserve(TotalComponents); 12659 for (unsigned i = 0; i < TotalComponents; ++i) { 12660 Expr *AssociatedExpr = Record.readSubExpr(); 12661 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12662 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12663 AssociatedExpr, AssociatedDecl)); 12664 } 12665 C->setComponents(Components, ListSizes); 12666 } 12667 12668 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) { 12669 C->setLParenLoc(Record.readSourceLocation()); 12670 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) { 12671 C->setMotionModifier( 12672 I, static_cast<OpenMPMotionModifierKind>(Record.readInt())); 12673 C->setMotionModifierLoc(I, Record.readSourceLocation()); 12674 } 12675 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12676 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12677 C->setColonLoc(Record.readSourceLocation()); 12678 auto NumVars = C->varlist_size(); 12679 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12680 auto TotalLists = C->getTotalComponentListNum(); 12681 auto TotalComponents = C->getTotalComponentsNum(); 12682 12683 SmallVector<Expr *, 16> Vars; 12684 Vars.reserve(NumVars); 12685 for (unsigned i = 0; i != NumVars; ++i) 12686 Vars.push_back(Record.readSubExpr()); 12687 C->setVarRefs(Vars); 12688 12689 SmallVector<Expr *, 16> UDMappers; 12690 UDMappers.reserve(NumVars); 12691 for (unsigned I = 0; I < NumVars; ++I) 12692 UDMappers.push_back(Record.readSubExpr()); 12693 C->setUDMapperRefs(UDMappers); 12694 12695 SmallVector<ValueDecl *, 16> Decls; 12696 Decls.reserve(UniqueDecls); 12697 for (unsigned i = 0; i < UniqueDecls; ++i) 12698 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12699 C->setUniqueDecls(Decls); 12700 12701 SmallVector<unsigned, 16> ListsPerDecl; 12702 ListsPerDecl.reserve(UniqueDecls); 12703 for (unsigned i = 0; i < UniqueDecls; ++i) 12704 ListsPerDecl.push_back(Record.readInt()); 12705 C->setDeclNumLists(ListsPerDecl); 12706 12707 SmallVector<unsigned, 32> ListSizes; 12708 ListSizes.reserve(TotalLists); 12709 for (unsigned i = 0; i < TotalLists; ++i) 12710 ListSizes.push_back(Record.readInt()); 12711 C->setComponentListSizes(ListSizes); 12712 12713 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12714 Components.reserve(TotalComponents); 12715 for (unsigned i = 0; i < TotalComponents; ++i) { 12716 Expr *AssociatedExpr = Record.readSubExpr(); 12717 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12718 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12719 AssociatedExpr, AssociatedDecl)); 12720 } 12721 C->setComponents(Components, ListSizes); 12722 } 12723 12724 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) { 12725 C->setLParenLoc(Record.readSourceLocation()); 12726 auto NumVars = C->varlist_size(); 12727 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12728 auto TotalLists = C->getTotalComponentListNum(); 12729 auto TotalComponents = C->getTotalComponentsNum(); 12730 12731 SmallVector<Expr *, 16> Vars; 12732 Vars.reserve(NumVars); 12733 for (unsigned i = 0; i != NumVars; ++i) 12734 Vars.push_back(Record.readSubExpr()); 12735 C->setVarRefs(Vars); 12736 Vars.clear(); 12737 for (unsigned i = 0; i != NumVars; ++i) 12738 Vars.push_back(Record.readSubExpr()); 12739 C->setPrivateCopies(Vars); 12740 Vars.clear(); 12741 for (unsigned i = 0; i != NumVars; ++i) 12742 Vars.push_back(Record.readSubExpr()); 12743 C->setInits(Vars); 12744 12745 SmallVector<ValueDecl *, 16> Decls; 12746 Decls.reserve(UniqueDecls); 12747 for (unsigned i = 0; i < UniqueDecls; ++i) 12748 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12749 C->setUniqueDecls(Decls); 12750 12751 SmallVector<unsigned, 16> ListsPerDecl; 12752 ListsPerDecl.reserve(UniqueDecls); 12753 for (unsigned i = 0; i < UniqueDecls; ++i) 12754 ListsPerDecl.push_back(Record.readInt()); 12755 C->setDeclNumLists(ListsPerDecl); 12756 12757 SmallVector<unsigned, 32> ListSizes; 12758 ListSizes.reserve(TotalLists); 12759 for (unsigned i = 0; i < TotalLists; ++i) 12760 ListSizes.push_back(Record.readInt()); 12761 C->setComponentListSizes(ListSizes); 12762 12763 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12764 Components.reserve(TotalComponents); 12765 for (unsigned i = 0; i < TotalComponents; ++i) { 12766 Expr *AssociatedExpr = Record.readSubExpr(); 12767 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12768 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12769 AssociatedExpr, AssociatedDecl)); 12770 } 12771 C->setComponents(Components, ListSizes); 12772 } 12773 12774 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) { 12775 C->setLParenLoc(Record.readSourceLocation()); 12776 auto NumVars = C->varlist_size(); 12777 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12778 auto TotalLists = C->getTotalComponentListNum(); 12779 auto TotalComponents = C->getTotalComponentsNum(); 12780 12781 SmallVector<Expr *, 16> Vars; 12782 Vars.reserve(NumVars); 12783 for (unsigned i = 0; i != NumVars; ++i) 12784 Vars.push_back(Record.readSubExpr()); 12785 C->setVarRefs(Vars); 12786 12787 SmallVector<ValueDecl *, 16> Decls; 12788 Decls.reserve(UniqueDecls); 12789 for (unsigned i = 0; i < UniqueDecls; ++i) 12790 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12791 C->setUniqueDecls(Decls); 12792 12793 SmallVector<unsigned, 16> ListsPerDecl; 12794 ListsPerDecl.reserve(UniqueDecls); 12795 for (unsigned i = 0; i < UniqueDecls; ++i) 12796 ListsPerDecl.push_back(Record.readInt()); 12797 C->setDeclNumLists(ListsPerDecl); 12798 12799 SmallVector<unsigned, 32> ListSizes; 12800 ListSizes.reserve(TotalLists); 12801 for (unsigned i = 0; i < TotalLists; ++i) 12802 ListSizes.push_back(Record.readInt()); 12803 C->setComponentListSizes(ListSizes); 12804 12805 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12806 Components.reserve(TotalComponents); 12807 for (unsigned i = 0; i < TotalComponents; ++i) { 12808 Expr *AssociatedExpr = Record.readSubExpr(); 12809 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12810 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12811 AssociatedExpr, AssociatedDecl)); 12812 } 12813 C->setComponents(Components, ListSizes); 12814 } 12815 12816 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) { 12817 C->setLParenLoc(Record.readSourceLocation()); 12818 auto NumVars = C->varlist_size(); 12819 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12820 auto TotalLists = C->getTotalComponentListNum(); 12821 auto TotalComponents = C->getTotalComponentsNum(); 12822 12823 SmallVector<Expr *, 16> Vars; 12824 Vars.reserve(NumVars); 12825 for (unsigned i = 0; i != NumVars; ++i) 12826 Vars.push_back(Record.readSubExpr()); 12827 C->setVarRefs(Vars); 12828 Vars.clear(); 12829 12830 SmallVector<ValueDecl *, 16> Decls; 12831 Decls.reserve(UniqueDecls); 12832 for (unsigned i = 0; i < UniqueDecls; ++i) 12833 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12834 C->setUniqueDecls(Decls); 12835 12836 SmallVector<unsigned, 16> ListsPerDecl; 12837 ListsPerDecl.reserve(UniqueDecls); 12838 for (unsigned i = 0; i < UniqueDecls; ++i) 12839 ListsPerDecl.push_back(Record.readInt()); 12840 C->setDeclNumLists(ListsPerDecl); 12841 12842 SmallVector<unsigned, 32> ListSizes; 12843 ListSizes.reserve(TotalLists); 12844 for (unsigned i = 0; i < TotalLists; ++i) 12845 ListSizes.push_back(Record.readInt()); 12846 C->setComponentListSizes(ListSizes); 12847 12848 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12849 Components.reserve(TotalComponents); 12850 for (unsigned i = 0; i < TotalComponents; ++i) { 12851 Expr *AssociatedExpr = Record.readSubExpr(); 12852 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12853 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12854 AssociatedExpr, AssociatedDecl)); 12855 } 12856 C->setComponents(Components, ListSizes); 12857 } 12858 12859 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) { 12860 C->setLParenLoc(Record.readSourceLocation()); 12861 unsigned NumVars = C->varlist_size(); 12862 SmallVector<Expr *, 16> Vars; 12863 Vars.reserve(NumVars); 12864 for (unsigned i = 0; i != NumVars; ++i) 12865 Vars.push_back(Record.readSubExpr()); 12866 C->setVarRefs(Vars); 12867 Vars.clear(); 12868 Vars.reserve(NumVars); 12869 for (unsigned i = 0; i != NumVars; ++i) 12870 Vars.push_back(Record.readSubExpr()); 12871 C->setPrivateRefs(Vars); 12872 } 12873 12874 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) { 12875 C->setLParenLoc(Record.readSourceLocation()); 12876 unsigned NumVars = C->varlist_size(); 12877 SmallVector<Expr *, 16> Vars; 12878 Vars.reserve(NumVars); 12879 for (unsigned i = 0; i != NumVars; ++i) 12880 Vars.push_back(Record.readSubExpr()); 12881 C->setVarRefs(Vars); 12882 } 12883 12884 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) { 12885 C->setLParenLoc(Record.readSourceLocation()); 12886 unsigned NumVars = C->varlist_size(); 12887 SmallVector<Expr *, 16> Vars; 12888 Vars.reserve(NumVars); 12889 for (unsigned i = 0; i != NumVars; ++i) 12890 Vars.push_back(Record.readSubExpr()); 12891 C->setVarRefs(Vars); 12892 } 12893 12894 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) { 12895 C->setLParenLoc(Record.readSourceLocation()); 12896 unsigned NumOfAllocators = C->getNumberOfAllocators(); 12897 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data; 12898 Data.reserve(NumOfAllocators); 12899 for (unsigned I = 0; I != NumOfAllocators; ++I) { 12900 OMPUsesAllocatorsClause::Data &D = Data.emplace_back(); 12901 D.Allocator = Record.readSubExpr(); 12902 D.AllocatorTraits = Record.readSubExpr(); 12903 D.LParenLoc = Record.readSourceLocation(); 12904 D.RParenLoc = Record.readSourceLocation(); 12905 } 12906 C->setAllocatorsData(Data); 12907 } 12908 12909 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) { 12910 C->setLParenLoc(Record.readSourceLocation()); 12911 C->setModifier(Record.readSubExpr()); 12912 C->setColonLoc(Record.readSourceLocation()); 12913 unsigned NumOfLocators = C->varlist_size(); 12914 SmallVector<Expr *, 4> Locators; 12915 Locators.reserve(NumOfLocators); 12916 for (unsigned I = 0; I != NumOfLocators; ++I) 12917 Locators.push_back(Record.readSubExpr()); 12918 C->setVarRefs(Locators); 12919 } 12920 12921 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) { 12922 C->setKind(Record.readEnum<OpenMPOrderClauseKind>()); 12923 C->setLParenLoc(Record.readSourceLocation()); 12924 C->setKindKwLoc(Record.readSourceLocation()); 12925 } 12926 12927 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() { 12928 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo(); 12929 TI.Sets.resize(readUInt32()); 12930 for (auto &Set : TI.Sets) { 12931 Set.Kind = readEnum<llvm::omp::TraitSet>(); 12932 Set.Selectors.resize(readUInt32()); 12933 for (auto &Selector : Set.Selectors) { 12934 Selector.Kind = readEnum<llvm::omp::TraitSelector>(); 12935 Selector.ScoreOrCondition = nullptr; 12936 if (readBool()) 12937 Selector.ScoreOrCondition = readExprRef(); 12938 Selector.Properties.resize(readUInt32()); 12939 for (auto &Property : Selector.Properties) 12940 Property.Kind = readEnum<llvm::omp::TraitProperty>(); 12941 } 12942 } 12943 return &TI; 12944 } 12945 12946 void ASTRecordReader::readOMPChildren(OMPChildren *Data) { 12947 if (!Data) 12948 return; 12949 if (Reader->ReadingKind == ASTReader::Read_Stmt) { 12950 // Skip NumClauses, NumChildren and HasAssociatedStmt fields. 12951 skipInts(3); 12952 } 12953 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses()); 12954 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I) 12955 Clauses[I] = readOMPClause(); 12956 Data->setClauses(Clauses); 12957 if (Data->hasAssociatedStmt()) 12958 Data->setAssociatedStmt(readStmt()); 12959 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I) 12960 Data->getChildren()[I] = readStmt(); 12961 } 12962