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