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 CHECK_TARGET_OPT(TuneCPU, "tune CPU"); 396 } 397 398 #undef CHECK_TARGET_OPT 399 400 // Compare feature sets. 401 SmallVector<StringRef, 4> ExistingFeatures( 402 ExistingTargetOpts.FeaturesAsWritten.begin(), 403 ExistingTargetOpts.FeaturesAsWritten.end()); 404 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(), 405 TargetOpts.FeaturesAsWritten.end()); 406 llvm::sort(ExistingFeatures); 407 llvm::sort(ReadFeatures); 408 409 // We compute the set difference in both directions explicitly so that we can 410 // diagnose the differences differently. 411 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures; 412 std::set_difference( 413 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(), 414 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures)); 415 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(), 416 ExistingFeatures.begin(), ExistingFeatures.end(), 417 std::back_inserter(UnmatchedReadFeatures)); 418 419 // If we are allowing compatible differences and the read feature set is 420 // a strict subset of the existing feature set, there is nothing to diagnose. 421 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty()) 422 return false; 423 424 if (Diags) { 425 for (StringRef Feature : UnmatchedReadFeatures) 426 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 427 << /* is-existing-feature */ false << Feature; 428 for (StringRef Feature : UnmatchedExistingFeatures) 429 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 430 << /* is-existing-feature */ true << Feature; 431 } 432 433 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty(); 434 } 435 436 bool 437 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts, 438 bool Complain, 439 bool AllowCompatibleDifferences) { 440 const LangOptions &ExistingLangOpts = PP.getLangOpts(); 441 return checkLanguageOptions(LangOpts, ExistingLangOpts, 442 Complain ? &Reader.Diags : nullptr, 443 AllowCompatibleDifferences); 444 } 445 446 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts, 447 bool Complain, 448 bool AllowCompatibleDifferences) { 449 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts(); 450 return checkTargetOptions(TargetOpts, ExistingTargetOpts, 451 Complain ? &Reader.Diags : nullptr, 452 AllowCompatibleDifferences); 453 } 454 455 namespace { 456 457 using MacroDefinitionsMap = 458 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>; 459 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>; 460 461 } // namespace 462 463 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags, 464 DiagnosticsEngine &Diags, 465 bool Complain) { 466 using Level = DiagnosticsEngine::Level; 467 468 // Check current mappings for new -Werror mappings, and the stored mappings 469 // for cases that were explicitly mapped to *not* be errors that are now 470 // errors because of options like -Werror. 471 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags }; 472 473 for (DiagnosticsEngine *MappingSource : MappingSources) { 474 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) { 475 diag::kind DiagID = DiagIDMappingPair.first; 476 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation()); 477 if (CurLevel < DiagnosticsEngine::Error) 478 continue; // not significant 479 Level StoredLevel = 480 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation()); 481 if (StoredLevel < DiagnosticsEngine::Error) { 482 if (Complain) 483 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" + 484 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str(); 485 return true; 486 } 487 } 488 } 489 490 return false; 491 } 492 493 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) { 494 diag::Severity Ext = Diags.getExtensionHandlingBehavior(); 495 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors()) 496 return true; 497 return Ext >= diag::Severity::Error; 498 } 499 500 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags, 501 DiagnosticsEngine &Diags, 502 bool IsSystem, bool Complain) { 503 // Top-level options 504 if (IsSystem) { 505 if (Diags.getSuppressSystemWarnings()) 506 return false; 507 // If -Wsystem-headers was not enabled before, be conservative 508 if (StoredDiags.getSuppressSystemWarnings()) { 509 if (Complain) 510 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers"; 511 return true; 512 } 513 } 514 515 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) { 516 if (Complain) 517 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror"; 518 return true; 519 } 520 521 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() && 522 !StoredDiags.getEnableAllWarnings()) { 523 if (Complain) 524 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror"; 525 return true; 526 } 527 528 if (isExtHandlingFromDiagsError(Diags) && 529 !isExtHandlingFromDiagsError(StoredDiags)) { 530 if (Complain) 531 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors"; 532 return true; 533 } 534 535 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain); 536 } 537 538 /// Return the top import module if it is implicit, nullptr otherwise. 539 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr, 540 Preprocessor &PP) { 541 // If the original import came from a file explicitly generated by the user, 542 // don't check the diagnostic mappings. 543 // FIXME: currently this is approximated by checking whether this is not a 544 // module import of an implicitly-loaded module file. 545 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in 546 // the transitive closure of its imports, since unrelated modules cannot be 547 // imported until after this module finishes validation. 548 ModuleFile *TopImport = &*ModuleMgr.rbegin(); 549 while (!TopImport->ImportedBy.empty()) 550 TopImport = TopImport->ImportedBy[0]; 551 if (TopImport->Kind != MK_ImplicitModule) 552 return nullptr; 553 554 StringRef ModuleName = TopImport->ModuleName; 555 assert(!ModuleName.empty() && "diagnostic options read before module name"); 556 557 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName); 558 assert(M && "missing module"); 559 return M; 560 } 561 562 bool PCHValidator::ReadDiagnosticOptions( 563 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 564 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics(); 565 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs()); 566 IntrusiveRefCntPtr<DiagnosticsEngine> Diags( 567 new DiagnosticsEngine(DiagIDs, DiagOpts.get())); 568 // This should never fail, because we would have processed these options 569 // before writing them to an ASTFile. 570 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false); 571 572 ModuleManager &ModuleMgr = Reader.getModuleManager(); 573 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then"); 574 575 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP); 576 if (!TopM) 577 return false; 578 579 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that 580 // contains the union of their flags. 581 return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem, 582 Complain); 583 } 584 585 /// Collect the macro definitions provided by the given preprocessor 586 /// options. 587 static void 588 collectMacroDefinitions(const PreprocessorOptions &PPOpts, 589 MacroDefinitionsMap &Macros, 590 SmallVectorImpl<StringRef> *MacroNames = nullptr) { 591 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) { 592 StringRef Macro = PPOpts.Macros[I].first; 593 bool IsUndef = PPOpts.Macros[I].second; 594 595 std::pair<StringRef, StringRef> MacroPair = Macro.split('='); 596 StringRef MacroName = MacroPair.first; 597 StringRef MacroBody = MacroPair.second; 598 599 // For an #undef'd macro, we only care about the name. 600 if (IsUndef) { 601 if (MacroNames && !Macros.count(MacroName)) 602 MacroNames->push_back(MacroName); 603 604 Macros[MacroName] = std::make_pair("", true); 605 continue; 606 } 607 608 // For a #define'd macro, figure out the actual definition. 609 if (MacroName.size() == Macro.size()) 610 MacroBody = "1"; 611 else { 612 // Note: GCC drops anything following an end-of-line character. 613 StringRef::size_type End = MacroBody.find_first_of("\n\r"); 614 MacroBody = MacroBody.substr(0, End); 615 } 616 617 if (MacroNames && !Macros.count(MacroName)) 618 MacroNames->push_back(MacroName); 619 Macros[MacroName] = std::make_pair(MacroBody, false); 620 } 621 } 622 623 /// Check the preprocessor options deserialized from the control block 624 /// against the preprocessor options in an existing preprocessor. 625 /// 626 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 627 /// \param Validate If true, validate preprocessor options. If false, allow 628 /// macros defined by \p ExistingPPOpts to override those defined by 629 /// \p PPOpts in SuggestedPredefines. 630 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts, 631 const PreprocessorOptions &ExistingPPOpts, 632 DiagnosticsEngine *Diags, 633 FileManager &FileMgr, 634 std::string &SuggestedPredefines, 635 const LangOptions &LangOpts, 636 bool Validate = true) { 637 // Check macro definitions. 638 MacroDefinitionsMap ASTFileMacros; 639 collectMacroDefinitions(PPOpts, ASTFileMacros); 640 MacroDefinitionsMap ExistingMacros; 641 SmallVector<StringRef, 4> ExistingMacroNames; 642 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames); 643 644 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) { 645 // Dig out the macro definition in the existing preprocessor options. 646 StringRef MacroName = ExistingMacroNames[I]; 647 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName]; 648 649 // Check whether we know anything about this macro name or not. 650 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known = 651 ASTFileMacros.find(MacroName); 652 if (!Validate || Known == ASTFileMacros.end()) { 653 // FIXME: Check whether this identifier was referenced anywhere in the 654 // AST file. If so, we should reject the AST file. Unfortunately, this 655 // information isn't in the control block. What shall we do about it? 656 657 if (Existing.second) { 658 SuggestedPredefines += "#undef "; 659 SuggestedPredefines += MacroName.str(); 660 SuggestedPredefines += '\n'; 661 } else { 662 SuggestedPredefines += "#define "; 663 SuggestedPredefines += MacroName.str(); 664 SuggestedPredefines += ' '; 665 SuggestedPredefines += Existing.first.str(); 666 SuggestedPredefines += '\n'; 667 } 668 continue; 669 } 670 671 // If the macro was defined in one but undef'd in the other, we have a 672 // conflict. 673 if (Existing.second != Known->second.second) { 674 if (Diags) { 675 Diags->Report(diag::err_pch_macro_def_undef) 676 << MacroName << Known->second.second; 677 } 678 return true; 679 } 680 681 // If the macro was #undef'd in both, or if the macro bodies are identical, 682 // it's fine. 683 if (Existing.second || Existing.first == Known->second.first) 684 continue; 685 686 // The macro bodies differ; complain. 687 if (Diags) { 688 Diags->Report(diag::err_pch_macro_def_conflict) 689 << MacroName << Known->second.first << Existing.first; 690 } 691 return true; 692 } 693 694 // Check whether we're using predefines. 695 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) { 696 if (Diags) { 697 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines; 698 } 699 return true; 700 } 701 702 // Detailed record is important since it is used for the module cache hash. 703 if (LangOpts.Modules && 704 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) { 705 if (Diags) { 706 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord; 707 } 708 return true; 709 } 710 711 // Compute the #include and #include_macros lines we need. 712 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) { 713 StringRef File = ExistingPPOpts.Includes[I]; 714 715 if (!ExistingPPOpts.ImplicitPCHInclude.empty() && 716 !ExistingPPOpts.PCHThroughHeader.empty()) { 717 // In case the through header is an include, we must add all the includes 718 // to the predefines so the start point can be determined. 719 SuggestedPredefines += "#include \""; 720 SuggestedPredefines += File; 721 SuggestedPredefines += "\"\n"; 722 continue; 723 } 724 725 if (File == ExistingPPOpts.ImplicitPCHInclude) 726 continue; 727 728 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File) 729 != PPOpts.Includes.end()) 730 continue; 731 732 SuggestedPredefines += "#include \""; 733 SuggestedPredefines += File; 734 SuggestedPredefines += "\"\n"; 735 } 736 737 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) { 738 StringRef File = ExistingPPOpts.MacroIncludes[I]; 739 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(), 740 File) 741 != PPOpts.MacroIncludes.end()) 742 continue; 743 744 SuggestedPredefines += "#__include_macros \""; 745 SuggestedPredefines += File; 746 SuggestedPredefines += "\"\n##\n"; 747 } 748 749 return false; 750 } 751 752 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 753 bool Complain, 754 std::string &SuggestedPredefines) { 755 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts(); 756 757 return checkPreprocessorOptions(PPOpts, ExistingPPOpts, 758 Complain? &Reader.Diags : nullptr, 759 PP.getFileManager(), 760 SuggestedPredefines, 761 PP.getLangOpts()); 762 } 763 764 bool SimpleASTReaderListener::ReadPreprocessorOptions( 765 const PreprocessorOptions &PPOpts, 766 bool Complain, 767 std::string &SuggestedPredefines) { 768 return checkPreprocessorOptions(PPOpts, 769 PP.getPreprocessorOpts(), 770 nullptr, 771 PP.getFileManager(), 772 SuggestedPredefines, 773 PP.getLangOpts(), 774 false); 775 } 776 777 /// Check the header search options deserialized from the control block 778 /// against the header search options in an existing preprocessor. 779 /// 780 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 781 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 782 StringRef SpecificModuleCachePath, 783 StringRef ExistingModuleCachePath, 784 DiagnosticsEngine *Diags, 785 const LangOptions &LangOpts) { 786 if (LangOpts.Modules) { 787 if (SpecificModuleCachePath != ExistingModuleCachePath) { 788 if (Diags) 789 Diags->Report(diag::err_pch_modulecache_mismatch) 790 << SpecificModuleCachePath << ExistingModuleCachePath; 791 return true; 792 } 793 } 794 795 return false; 796 } 797 798 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 799 StringRef SpecificModuleCachePath, 800 bool Complain) { 801 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 802 PP.getHeaderSearchInfo().getModuleCachePath(), 803 Complain ? &Reader.Diags : nullptr, 804 PP.getLangOpts()); 805 } 806 807 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) { 808 PP.setCounterValue(Value); 809 } 810 811 //===----------------------------------------------------------------------===// 812 // AST reader implementation 813 //===----------------------------------------------------------------------===// 814 815 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener, 816 bool TakeOwnership) { 817 DeserializationListener = Listener; 818 OwnsDeserializationListener = TakeOwnership; 819 } 820 821 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) { 822 return serialization::ComputeHash(Sel); 823 } 824 825 std::pair<unsigned, unsigned> 826 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 827 using namespace llvm::support; 828 829 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 830 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 831 return std::make_pair(KeyLen, DataLen); 832 } 833 834 ASTSelectorLookupTrait::internal_key_type 835 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) { 836 using namespace llvm::support; 837 838 SelectorTable &SelTable = Reader.getContext().Selectors; 839 unsigned N = endian::readNext<uint16_t, little, unaligned>(d); 840 IdentifierInfo *FirstII = Reader.getLocalIdentifier( 841 F, endian::readNext<uint32_t, little, unaligned>(d)); 842 if (N == 0) 843 return SelTable.getNullarySelector(FirstII); 844 else if (N == 1) 845 return SelTable.getUnarySelector(FirstII); 846 847 SmallVector<IdentifierInfo *, 16> Args; 848 Args.push_back(FirstII); 849 for (unsigned I = 1; I != N; ++I) 850 Args.push_back(Reader.getLocalIdentifier( 851 F, endian::readNext<uint32_t, little, unaligned>(d))); 852 853 return SelTable.getSelector(N, Args.data()); 854 } 855 856 ASTSelectorLookupTrait::data_type 857 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d, 858 unsigned DataLen) { 859 using namespace llvm::support; 860 861 data_type Result; 862 863 Result.ID = Reader.getGlobalSelectorID( 864 F, endian::readNext<uint32_t, little, unaligned>(d)); 865 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d); 866 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d); 867 Result.InstanceBits = FullInstanceBits & 0x3; 868 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1; 869 Result.FactoryBits = FullFactoryBits & 0x3; 870 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1; 871 unsigned NumInstanceMethods = FullInstanceBits >> 3; 872 unsigned NumFactoryMethods = FullFactoryBits >> 3; 873 874 // Load instance methods 875 for (unsigned I = 0; I != NumInstanceMethods; ++I) { 876 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 877 F, endian::readNext<uint32_t, little, unaligned>(d))) 878 Result.Instance.push_back(Method); 879 } 880 881 // Load factory methods 882 for (unsigned I = 0; I != NumFactoryMethods; ++I) { 883 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 884 F, endian::readNext<uint32_t, little, unaligned>(d))) 885 Result.Factory.push_back(Method); 886 } 887 888 return Result; 889 } 890 891 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) { 892 return llvm::djbHash(a); 893 } 894 895 std::pair<unsigned, unsigned> 896 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) { 897 using namespace llvm::support; 898 899 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 900 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 901 return std::make_pair(KeyLen, DataLen); 902 } 903 904 ASTIdentifierLookupTraitBase::internal_key_type 905 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) { 906 assert(n >= 2 && d[n-1] == '\0'); 907 return StringRef((const char*) d, n-1); 908 } 909 910 /// Whether the given identifier is "interesting". 911 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II, 912 bool IsModule) { 913 return II.hadMacroDefinition() || 914 II.isPoisoned() || 915 (IsModule ? II.hasRevertedBuiltin() : II.getObjCOrBuiltinID()) || 916 II.hasRevertedTokenIDToIdentifier() || 917 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) && 918 II.getFETokenInfo()); 919 } 920 921 static bool readBit(unsigned &Bits) { 922 bool Value = Bits & 0x1; 923 Bits >>= 1; 924 return Value; 925 } 926 927 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) { 928 using namespace llvm::support; 929 930 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 931 return Reader.getGlobalIdentifierID(F, RawID >> 1); 932 } 933 934 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) { 935 if (!II.isFromAST()) { 936 II.setIsFromAST(); 937 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr; 938 if (isInterestingIdentifier(Reader, II, IsModule)) 939 II.setChangedSinceDeserialization(); 940 } 941 } 942 943 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k, 944 const unsigned char* d, 945 unsigned DataLen) { 946 using namespace llvm::support; 947 948 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 949 bool IsInteresting = RawID & 0x01; 950 951 // Wipe out the "is interesting" bit. 952 RawID = RawID >> 1; 953 954 // Build the IdentifierInfo and link the identifier ID with it. 955 IdentifierInfo *II = KnownII; 956 if (!II) { 957 II = &Reader.getIdentifierTable().getOwn(k); 958 KnownII = II; 959 } 960 markIdentifierFromAST(Reader, *II); 961 Reader.markIdentifierUpToDate(II); 962 963 IdentID ID = Reader.getGlobalIdentifierID(F, RawID); 964 if (!IsInteresting) { 965 // For uninteresting identifiers, there's nothing else to do. Just notify 966 // the reader that we've finished loading this identifier. 967 Reader.SetIdentifierInfo(ID, II); 968 return II; 969 } 970 971 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d); 972 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d); 973 bool CPlusPlusOperatorKeyword = readBit(Bits); 974 bool HasRevertedTokenIDToIdentifier = readBit(Bits); 975 bool HasRevertedBuiltin = readBit(Bits); 976 bool Poisoned = readBit(Bits); 977 bool ExtensionToken = readBit(Bits); 978 bool HadMacroDefinition = readBit(Bits); 979 980 assert(Bits == 0 && "Extra bits in the identifier?"); 981 DataLen -= 8; 982 983 // Set or check the various bits in the IdentifierInfo structure. 984 // Token IDs are read-only. 985 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier) 986 II->revertTokenIDToIdentifier(); 987 if (!F.isModule()) 988 II->setObjCOrBuiltinID(ObjCOrBuiltinID); 989 else if (HasRevertedBuiltin && II->getBuiltinID()) { 990 II->revertBuiltin(); 991 assert((II->hasRevertedBuiltin() || 992 II->getObjCOrBuiltinID() == ObjCOrBuiltinID) && 993 "Incorrect ObjC keyword or builtin ID"); 994 } 995 assert(II->isExtensionToken() == ExtensionToken && 996 "Incorrect extension token flag"); 997 (void)ExtensionToken; 998 if (Poisoned) 999 II->setIsPoisoned(true); 1000 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword && 1001 "Incorrect C++ operator keyword flag"); 1002 (void)CPlusPlusOperatorKeyword; 1003 1004 // If this identifier is a macro, deserialize the macro 1005 // definition. 1006 if (HadMacroDefinition) { 1007 uint32_t MacroDirectivesOffset = 1008 endian::readNext<uint32_t, little, unaligned>(d); 1009 DataLen -= 4; 1010 1011 Reader.addPendingMacro(II, &F, MacroDirectivesOffset); 1012 } 1013 1014 Reader.SetIdentifierInfo(ID, II); 1015 1016 // Read all of the declarations visible at global scope with this 1017 // name. 1018 if (DataLen > 0) { 1019 SmallVector<uint32_t, 4> DeclIDs; 1020 for (; DataLen > 0; DataLen -= 4) 1021 DeclIDs.push_back(Reader.getGlobalDeclID( 1022 F, endian::readNext<uint32_t, little, unaligned>(d))); 1023 Reader.SetGloballyVisibleDecls(II, DeclIDs); 1024 } 1025 1026 return II; 1027 } 1028 1029 DeclarationNameKey::DeclarationNameKey(DeclarationName Name) 1030 : Kind(Name.getNameKind()) { 1031 switch (Kind) { 1032 case DeclarationName::Identifier: 1033 Data = (uint64_t)Name.getAsIdentifierInfo(); 1034 break; 1035 case DeclarationName::ObjCZeroArgSelector: 1036 case DeclarationName::ObjCOneArgSelector: 1037 case DeclarationName::ObjCMultiArgSelector: 1038 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr(); 1039 break; 1040 case DeclarationName::CXXOperatorName: 1041 Data = Name.getCXXOverloadedOperator(); 1042 break; 1043 case DeclarationName::CXXLiteralOperatorName: 1044 Data = (uint64_t)Name.getCXXLiteralIdentifier(); 1045 break; 1046 case DeclarationName::CXXDeductionGuideName: 1047 Data = (uint64_t)Name.getCXXDeductionGuideTemplate() 1048 ->getDeclName().getAsIdentifierInfo(); 1049 break; 1050 case DeclarationName::CXXConstructorName: 1051 case DeclarationName::CXXDestructorName: 1052 case DeclarationName::CXXConversionFunctionName: 1053 case DeclarationName::CXXUsingDirective: 1054 Data = 0; 1055 break; 1056 } 1057 } 1058 1059 unsigned DeclarationNameKey::getHash() const { 1060 llvm::FoldingSetNodeID ID; 1061 ID.AddInteger(Kind); 1062 1063 switch (Kind) { 1064 case DeclarationName::Identifier: 1065 case DeclarationName::CXXLiteralOperatorName: 1066 case DeclarationName::CXXDeductionGuideName: 1067 ID.AddString(((IdentifierInfo*)Data)->getName()); 1068 break; 1069 case DeclarationName::ObjCZeroArgSelector: 1070 case DeclarationName::ObjCOneArgSelector: 1071 case DeclarationName::ObjCMultiArgSelector: 1072 ID.AddInteger(serialization::ComputeHash(Selector(Data))); 1073 break; 1074 case DeclarationName::CXXOperatorName: 1075 ID.AddInteger((OverloadedOperatorKind)Data); 1076 break; 1077 case DeclarationName::CXXConstructorName: 1078 case DeclarationName::CXXDestructorName: 1079 case DeclarationName::CXXConversionFunctionName: 1080 case DeclarationName::CXXUsingDirective: 1081 break; 1082 } 1083 1084 return ID.ComputeHash(); 1085 } 1086 1087 ModuleFile * 1088 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) { 1089 using namespace llvm::support; 1090 1091 uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d); 1092 return Reader.getLocalModuleFile(F, ModuleFileID); 1093 } 1094 1095 std::pair<unsigned, unsigned> 1096 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) { 1097 using namespace llvm::support; 1098 1099 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 1100 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 1101 return std::make_pair(KeyLen, DataLen); 1102 } 1103 1104 ASTDeclContextNameLookupTrait::internal_key_type 1105 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) { 1106 using namespace llvm::support; 1107 1108 auto Kind = (DeclarationName::NameKind)*d++; 1109 uint64_t Data; 1110 switch (Kind) { 1111 case DeclarationName::Identifier: 1112 case DeclarationName::CXXLiteralOperatorName: 1113 case DeclarationName::CXXDeductionGuideName: 1114 Data = (uint64_t)Reader.getLocalIdentifier( 1115 F, endian::readNext<uint32_t, little, unaligned>(d)); 1116 break; 1117 case DeclarationName::ObjCZeroArgSelector: 1118 case DeclarationName::ObjCOneArgSelector: 1119 case DeclarationName::ObjCMultiArgSelector: 1120 Data = 1121 (uint64_t)Reader.getLocalSelector( 1122 F, endian::readNext<uint32_t, little, unaligned>( 1123 d)).getAsOpaquePtr(); 1124 break; 1125 case DeclarationName::CXXOperatorName: 1126 Data = *d++; // OverloadedOperatorKind 1127 break; 1128 case DeclarationName::CXXConstructorName: 1129 case DeclarationName::CXXDestructorName: 1130 case DeclarationName::CXXConversionFunctionName: 1131 case DeclarationName::CXXUsingDirective: 1132 Data = 0; 1133 break; 1134 } 1135 1136 return DeclarationNameKey(Kind, Data); 1137 } 1138 1139 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type, 1140 const unsigned char *d, 1141 unsigned DataLen, 1142 data_type_builder &Val) { 1143 using namespace llvm::support; 1144 1145 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) { 1146 uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d); 1147 Val.insert(Reader.getGlobalDeclID(F, LocalID)); 1148 } 1149 } 1150 1151 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M, 1152 BitstreamCursor &Cursor, 1153 uint64_t Offset, 1154 DeclContext *DC) { 1155 assert(Offset != 0); 1156 1157 SavedStreamPosition SavedPosition(Cursor); 1158 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1159 Error(std::move(Err)); 1160 return true; 1161 } 1162 1163 RecordData Record; 1164 StringRef Blob; 1165 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1166 if (!MaybeCode) { 1167 Error(MaybeCode.takeError()); 1168 return true; 1169 } 1170 unsigned Code = MaybeCode.get(); 1171 1172 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1173 if (!MaybeRecCode) { 1174 Error(MaybeRecCode.takeError()); 1175 return true; 1176 } 1177 unsigned RecCode = MaybeRecCode.get(); 1178 if (RecCode != DECL_CONTEXT_LEXICAL) { 1179 Error("Expected lexical block"); 1180 return true; 1181 } 1182 1183 assert(!isa<TranslationUnitDecl>(DC) && 1184 "expected a TU_UPDATE_LEXICAL record for TU"); 1185 // If we are handling a C++ class template instantiation, we can see multiple 1186 // lexical updates for the same record. It's important that we select only one 1187 // of them, so that field numbering works properly. Just pick the first one we 1188 // see. 1189 auto &Lex = LexicalDecls[DC]; 1190 if (!Lex.first) { 1191 Lex = std::make_pair( 1192 &M, llvm::makeArrayRef( 1193 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 1194 Blob.data()), 1195 Blob.size() / 4)); 1196 } 1197 DC->setHasExternalLexicalStorage(true); 1198 return false; 1199 } 1200 1201 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M, 1202 BitstreamCursor &Cursor, 1203 uint64_t Offset, 1204 DeclID ID) { 1205 assert(Offset != 0); 1206 1207 SavedStreamPosition SavedPosition(Cursor); 1208 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1209 Error(std::move(Err)); 1210 return true; 1211 } 1212 1213 RecordData Record; 1214 StringRef Blob; 1215 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1216 if (!MaybeCode) { 1217 Error(MaybeCode.takeError()); 1218 return true; 1219 } 1220 unsigned Code = MaybeCode.get(); 1221 1222 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1223 if (!MaybeRecCode) { 1224 Error(MaybeRecCode.takeError()); 1225 return true; 1226 } 1227 unsigned RecCode = MaybeRecCode.get(); 1228 if (RecCode != DECL_CONTEXT_VISIBLE) { 1229 Error("Expected visible lookup table block"); 1230 return true; 1231 } 1232 1233 // We can't safely determine the primary context yet, so delay attaching the 1234 // lookup table until we're done with recursive deserialization. 1235 auto *Data = (const unsigned char*)Blob.data(); 1236 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data}); 1237 return false; 1238 } 1239 1240 void ASTReader::Error(StringRef Msg) const { 1241 Error(diag::err_fe_pch_malformed, Msg); 1242 if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() && 1243 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) { 1244 Diag(diag::note_module_cache_path) 1245 << PP.getHeaderSearchInfo().getModuleCachePath(); 1246 } 1247 } 1248 1249 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2, 1250 StringRef Arg3) const { 1251 if (Diags.isDiagnosticInFlight()) 1252 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3); 1253 else 1254 Diag(DiagID) << Arg1 << Arg2 << Arg3; 1255 } 1256 1257 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2, 1258 unsigned Select) const { 1259 if (!Diags.isDiagnosticInFlight()) 1260 Diag(DiagID) << Arg1 << Arg2 << Select; 1261 } 1262 1263 void ASTReader::Error(llvm::Error &&Err) const { 1264 Error(toString(std::move(Err))); 1265 } 1266 1267 //===----------------------------------------------------------------------===// 1268 // Source Manager Deserialization 1269 //===----------------------------------------------------------------------===// 1270 1271 /// Read the line table in the source manager block. 1272 /// \returns true if there was an error. 1273 bool ASTReader::ParseLineTable(ModuleFile &F, 1274 const RecordData &Record) { 1275 unsigned Idx = 0; 1276 LineTableInfo &LineTable = SourceMgr.getLineTable(); 1277 1278 // Parse the file names 1279 std::map<int, int> FileIDs; 1280 FileIDs[-1] = -1; // For unspecified filenames. 1281 for (unsigned I = 0; Record[Idx]; ++I) { 1282 // Extract the file name 1283 auto Filename = ReadPath(F, Record, Idx); 1284 FileIDs[I] = LineTable.getLineTableFilenameID(Filename); 1285 } 1286 ++Idx; 1287 1288 // Parse the line entries 1289 std::vector<LineEntry> Entries; 1290 while (Idx < Record.size()) { 1291 int FID = Record[Idx++]; 1292 assert(FID >= 0 && "Serialized line entries for non-local file."); 1293 // Remap FileID from 1-based old view. 1294 FID += F.SLocEntryBaseID - 1; 1295 1296 // Extract the line entries 1297 unsigned NumEntries = Record[Idx++]; 1298 assert(NumEntries && "no line entries for file ID"); 1299 Entries.clear(); 1300 Entries.reserve(NumEntries); 1301 for (unsigned I = 0; I != NumEntries; ++I) { 1302 unsigned FileOffset = Record[Idx++]; 1303 unsigned LineNo = Record[Idx++]; 1304 int FilenameID = FileIDs[Record[Idx++]]; 1305 SrcMgr::CharacteristicKind FileKind 1306 = (SrcMgr::CharacteristicKind)Record[Idx++]; 1307 unsigned IncludeOffset = Record[Idx++]; 1308 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, 1309 FileKind, IncludeOffset)); 1310 } 1311 LineTable.AddEntry(FileID::get(FID), Entries); 1312 } 1313 1314 return false; 1315 } 1316 1317 /// Read a source manager block 1318 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) { 1319 using namespace SrcMgr; 1320 1321 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor; 1322 1323 // Set the source-location entry cursor to the current position in 1324 // the stream. This cursor will be used to read the contents of the 1325 // source manager block initially, and then lazily read 1326 // source-location entries as needed. 1327 SLocEntryCursor = F.Stream; 1328 1329 // The stream itself is going to skip over the source manager block. 1330 if (llvm::Error Err = F.Stream.SkipBlock()) { 1331 Error(std::move(Err)); 1332 return true; 1333 } 1334 1335 // Enter the source manager block. 1336 if (llvm::Error Err = 1337 SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) { 1338 Error(std::move(Err)); 1339 return true; 1340 } 1341 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo(); 1342 1343 RecordData Record; 1344 while (true) { 1345 Expected<llvm::BitstreamEntry> MaybeE = 1346 SLocEntryCursor.advanceSkippingSubblocks(); 1347 if (!MaybeE) { 1348 Error(MaybeE.takeError()); 1349 return true; 1350 } 1351 llvm::BitstreamEntry E = MaybeE.get(); 1352 1353 switch (E.Kind) { 1354 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1355 case llvm::BitstreamEntry::Error: 1356 Error("malformed block record in AST file"); 1357 return true; 1358 case llvm::BitstreamEntry::EndBlock: 1359 return false; 1360 case llvm::BitstreamEntry::Record: 1361 // The interesting case. 1362 break; 1363 } 1364 1365 // Read a record. 1366 Record.clear(); 1367 StringRef Blob; 1368 Expected<unsigned> MaybeRecord = 1369 SLocEntryCursor.readRecord(E.ID, Record, &Blob); 1370 if (!MaybeRecord) { 1371 Error(MaybeRecord.takeError()); 1372 return true; 1373 } 1374 switch (MaybeRecord.get()) { 1375 default: // Default behavior: ignore. 1376 break; 1377 1378 case SM_SLOC_FILE_ENTRY: 1379 case SM_SLOC_BUFFER_ENTRY: 1380 case SM_SLOC_EXPANSION_ENTRY: 1381 // Once we hit one of the source location entries, we're done. 1382 return false; 1383 } 1384 } 1385 } 1386 1387 /// If a header file is not found at the path that we expect it to be 1388 /// and the PCH file was moved from its original location, try to resolve the 1389 /// file by assuming that header+PCH were moved together and the header is in 1390 /// the same place relative to the PCH. 1391 static std::string 1392 resolveFileRelativeToOriginalDir(const std::string &Filename, 1393 const std::string &OriginalDir, 1394 const std::string &CurrDir) { 1395 assert(OriginalDir != CurrDir && 1396 "No point trying to resolve the file if the PCH dir didn't change"); 1397 1398 using namespace llvm::sys; 1399 1400 SmallString<128> filePath(Filename); 1401 fs::make_absolute(filePath); 1402 assert(path::is_absolute(OriginalDir)); 1403 SmallString<128> currPCHPath(CurrDir); 1404 1405 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)), 1406 fileDirE = path::end(path::parent_path(filePath)); 1407 path::const_iterator origDirI = path::begin(OriginalDir), 1408 origDirE = path::end(OriginalDir); 1409 // Skip the common path components from filePath and OriginalDir. 1410 while (fileDirI != fileDirE && origDirI != origDirE && 1411 *fileDirI == *origDirI) { 1412 ++fileDirI; 1413 ++origDirI; 1414 } 1415 for (; origDirI != origDirE; ++origDirI) 1416 path::append(currPCHPath, ".."); 1417 path::append(currPCHPath, fileDirI, fileDirE); 1418 path::append(currPCHPath, path::filename(Filename)); 1419 return std::string(currPCHPath.str()); 1420 } 1421 1422 bool ASTReader::ReadSLocEntry(int ID) { 1423 if (ID == 0) 1424 return false; 1425 1426 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1427 Error("source location entry ID out-of-range for AST file"); 1428 return true; 1429 } 1430 1431 // Local helper to read the (possibly-compressed) buffer data following the 1432 // entry record. 1433 auto ReadBuffer = [this]( 1434 BitstreamCursor &SLocEntryCursor, 1435 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> { 1436 RecordData Record; 1437 StringRef Blob; 1438 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode(); 1439 if (!MaybeCode) { 1440 Error(MaybeCode.takeError()); 1441 return nullptr; 1442 } 1443 unsigned Code = MaybeCode.get(); 1444 1445 Expected<unsigned> MaybeRecCode = 1446 SLocEntryCursor.readRecord(Code, Record, &Blob); 1447 if (!MaybeRecCode) { 1448 Error(MaybeRecCode.takeError()); 1449 return nullptr; 1450 } 1451 unsigned RecCode = MaybeRecCode.get(); 1452 1453 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) { 1454 if (!llvm::zlib::isAvailable()) { 1455 Error("zlib is not available"); 1456 return nullptr; 1457 } 1458 SmallString<0> Uncompressed; 1459 if (llvm::Error E = 1460 llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) { 1461 Error("could not decompress embedded file contents: " + 1462 llvm::toString(std::move(E))); 1463 return nullptr; 1464 } 1465 return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name); 1466 } else if (RecCode == SM_SLOC_BUFFER_BLOB) { 1467 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true); 1468 } else { 1469 Error("AST record has invalid code"); 1470 return nullptr; 1471 } 1472 }; 1473 1474 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second; 1475 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit( 1476 F->SLocEntryOffsetsBase + 1477 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) { 1478 Error(std::move(Err)); 1479 return true; 1480 } 1481 1482 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor; 1483 unsigned BaseOffset = F->SLocEntryBaseOffset; 1484 1485 ++NumSLocEntriesRead; 1486 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance(); 1487 if (!MaybeEntry) { 1488 Error(MaybeEntry.takeError()); 1489 return true; 1490 } 1491 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1492 1493 if (Entry.Kind != llvm::BitstreamEntry::Record) { 1494 Error("incorrectly-formatted source location entry in AST file"); 1495 return true; 1496 } 1497 1498 RecordData Record; 1499 StringRef Blob; 1500 Expected<unsigned> MaybeSLOC = 1501 SLocEntryCursor.readRecord(Entry.ID, Record, &Blob); 1502 if (!MaybeSLOC) { 1503 Error(MaybeSLOC.takeError()); 1504 return true; 1505 } 1506 switch (MaybeSLOC.get()) { 1507 default: 1508 Error("incorrectly-formatted source location entry in AST file"); 1509 return true; 1510 1511 case SM_SLOC_FILE_ENTRY: { 1512 // We will detect whether a file changed and return 'Failure' for it, but 1513 // we will also try to fail gracefully by setting up the SLocEntry. 1514 unsigned InputID = Record[4]; 1515 InputFile IF = getInputFile(*F, InputID); 1516 const FileEntry *File = IF.getFile(); 1517 bool OverriddenBuffer = IF.isOverridden(); 1518 1519 // Note that we only check if a File was returned. If it was out-of-date 1520 // we have complained but we will continue creating a FileID to recover 1521 // gracefully. 1522 if (!File) 1523 return true; 1524 1525 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1526 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) { 1527 // This is the module's main file. 1528 IncludeLoc = getImportLocation(F); 1529 } 1530 SrcMgr::CharacteristicKind 1531 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1532 // FIXME: The FileID should be created from the FileEntryRef. 1533 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter, 1534 ID, BaseOffset + Record[0]); 1535 SrcMgr::FileInfo &FileInfo = 1536 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()); 1537 FileInfo.NumCreatedFIDs = Record[5]; 1538 if (Record[3]) 1539 FileInfo.setHasLineDirectives(); 1540 1541 unsigned NumFileDecls = Record[7]; 1542 if (NumFileDecls && ContextObj) { 1543 const DeclID *FirstDecl = F->FileSortedDecls + Record[6]; 1544 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?"); 1545 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl, 1546 NumFileDecls)); 1547 } 1548 1549 const SrcMgr::ContentCache *ContentCache 1550 = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter)); 1551 if (OverriddenBuffer && !ContentCache->BufferOverridden && 1552 ContentCache->ContentsEntry == ContentCache->OrigEntry && 1553 !ContentCache->getRawBuffer()) { 1554 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName()); 1555 if (!Buffer) 1556 return true; 1557 SourceMgr.overrideFileContents(File, std::move(Buffer)); 1558 } 1559 1560 break; 1561 } 1562 1563 case SM_SLOC_BUFFER_ENTRY: { 1564 const char *Name = Blob.data(); 1565 unsigned Offset = Record[0]; 1566 SrcMgr::CharacteristicKind 1567 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1568 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1569 if (IncludeLoc.isInvalid() && F->isModule()) { 1570 IncludeLoc = getImportLocation(F); 1571 } 1572 1573 auto Buffer = ReadBuffer(SLocEntryCursor, Name); 1574 if (!Buffer) 1575 return true; 1576 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID, 1577 BaseOffset + Offset, IncludeLoc); 1578 break; 1579 } 1580 1581 case SM_SLOC_EXPANSION_ENTRY: { 1582 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]); 1583 SourceMgr.createExpansionLoc(SpellingLoc, 1584 ReadSourceLocation(*F, Record[2]), 1585 ReadSourceLocation(*F, Record[3]), 1586 Record[5], 1587 Record[4], 1588 ID, 1589 BaseOffset + Record[0]); 1590 break; 1591 } 1592 } 1593 1594 return false; 1595 } 1596 1597 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) { 1598 if (ID == 0) 1599 return std::make_pair(SourceLocation(), ""); 1600 1601 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1602 Error("source location entry ID out-of-range for AST file"); 1603 return std::make_pair(SourceLocation(), ""); 1604 } 1605 1606 // Find which module file this entry lands in. 1607 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second; 1608 if (!M->isModule()) 1609 return std::make_pair(SourceLocation(), ""); 1610 1611 // FIXME: Can we map this down to a particular submodule? That would be 1612 // ideal. 1613 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName)); 1614 } 1615 1616 /// Find the location where the module F is imported. 1617 SourceLocation ASTReader::getImportLocation(ModuleFile *F) { 1618 if (F->ImportLoc.isValid()) 1619 return F->ImportLoc; 1620 1621 // Otherwise we have a PCH. It's considered to be "imported" at the first 1622 // location of its includer. 1623 if (F->ImportedBy.empty() || !F->ImportedBy[0]) { 1624 // Main file is the importer. 1625 assert(SourceMgr.getMainFileID().isValid() && "missing main file"); 1626 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); 1627 } 1628 return F->ImportedBy[0]->FirstLoc; 1629 } 1630 1631 /// Enter a subblock of the specified BlockID with the specified cursor. Read 1632 /// the abbreviations that are at the top of the block and then leave the cursor 1633 /// pointing into the block. 1634 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID, 1635 uint64_t *StartOfBlockOffset) { 1636 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) { 1637 // FIXME this drops errors on the floor. 1638 consumeError(std::move(Err)); 1639 return true; 1640 } 1641 1642 if (StartOfBlockOffset) 1643 *StartOfBlockOffset = Cursor.GetCurrentBitNo(); 1644 1645 while (true) { 1646 uint64_t Offset = Cursor.GetCurrentBitNo(); 1647 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1648 if (!MaybeCode) { 1649 // FIXME this drops errors on the floor. 1650 consumeError(MaybeCode.takeError()); 1651 return true; 1652 } 1653 unsigned Code = MaybeCode.get(); 1654 1655 // We expect all abbrevs to be at the start of the block. 1656 if (Code != llvm::bitc::DEFINE_ABBREV) { 1657 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1658 // FIXME this drops errors on the floor. 1659 consumeError(std::move(Err)); 1660 return true; 1661 } 1662 return false; 1663 } 1664 if (llvm::Error Err = Cursor.ReadAbbrevRecord()) { 1665 // FIXME this drops errors on the floor. 1666 consumeError(std::move(Err)); 1667 return true; 1668 } 1669 } 1670 } 1671 1672 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record, 1673 unsigned &Idx) { 1674 Token Tok; 1675 Tok.startToken(); 1676 Tok.setLocation(ReadSourceLocation(F, Record, Idx)); 1677 Tok.setLength(Record[Idx++]); 1678 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++])) 1679 Tok.setIdentifierInfo(II); 1680 Tok.setKind((tok::TokenKind)Record[Idx++]); 1681 Tok.setFlag((Token::TokenFlags)Record[Idx++]); 1682 return Tok; 1683 } 1684 1685 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) { 1686 BitstreamCursor &Stream = F.MacroCursor; 1687 1688 // Keep track of where we are in the stream, then jump back there 1689 // after reading this macro. 1690 SavedStreamPosition SavedPosition(Stream); 1691 1692 if (llvm::Error Err = Stream.JumpToBit(Offset)) { 1693 // FIXME this drops errors on the floor. 1694 consumeError(std::move(Err)); 1695 return nullptr; 1696 } 1697 RecordData Record; 1698 SmallVector<IdentifierInfo*, 16> MacroParams; 1699 MacroInfo *Macro = nullptr; 1700 1701 while (true) { 1702 // Advance to the next record, but if we get to the end of the block, don't 1703 // pop it (removing all the abbreviations from the cursor) since we want to 1704 // be able to reseek within the block and read entries. 1705 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd; 1706 Expected<llvm::BitstreamEntry> MaybeEntry = 1707 Stream.advanceSkippingSubblocks(Flags); 1708 if (!MaybeEntry) { 1709 Error(MaybeEntry.takeError()); 1710 return Macro; 1711 } 1712 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1713 1714 switch (Entry.Kind) { 1715 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1716 case llvm::BitstreamEntry::Error: 1717 Error("malformed block record in AST file"); 1718 return Macro; 1719 case llvm::BitstreamEntry::EndBlock: 1720 return Macro; 1721 case llvm::BitstreamEntry::Record: 1722 // The interesting case. 1723 break; 1724 } 1725 1726 // Read a record. 1727 Record.clear(); 1728 PreprocessorRecordTypes RecType; 1729 if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record)) 1730 RecType = (PreprocessorRecordTypes)MaybeRecType.get(); 1731 else { 1732 Error(MaybeRecType.takeError()); 1733 return Macro; 1734 } 1735 switch (RecType) { 1736 case PP_MODULE_MACRO: 1737 case PP_MACRO_DIRECTIVE_HISTORY: 1738 return Macro; 1739 1740 case PP_MACRO_OBJECT_LIKE: 1741 case PP_MACRO_FUNCTION_LIKE: { 1742 // If we already have a macro, that means that we've hit the end 1743 // of the definition of the macro we were looking for. We're 1744 // done. 1745 if (Macro) 1746 return Macro; 1747 1748 unsigned NextIndex = 1; // Skip identifier ID. 1749 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex); 1750 MacroInfo *MI = PP.AllocateMacroInfo(Loc); 1751 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex)); 1752 MI->setIsUsed(Record[NextIndex++]); 1753 MI->setUsedForHeaderGuard(Record[NextIndex++]); 1754 1755 if (RecType == PP_MACRO_FUNCTION_LIKE) { 1756 // Decode function-like macro info. 1757 bool isC99VarArgs = Record[NextIndex++]; 1758 bool isGNUVarArgs = Record[NextIndex++]; 1759 bool hasCommaPasting = Record[NextIndex++]; 1760 MacroParams.clear(); 1761 unsigned NumArgs = Record[NextIndex++]; 1762 for (unsigned i = 0; i != NumArgs; ++i) 1763 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++])); 1764 1765 // Install function-like macro info. 1766 MI->setIsFunctionLike(); 1767 if (isC99VarArgs) MI->setIsC99Varargs(); 1768 if (isGNUVarArgs) MI->setIsGNUVarargs(); 1769 if (hasCommaPasting) MI->setHasCommaPasting(); 1770 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator()); 1771 } 1772 1773 // Remember that we saw this macro last so that we add the tokens that 1774 // form its body to it. 1775 Macro = MI; 1776 1777 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() && 1778 Record[NextIndex]) { 1779 // We have a macro definition. Register the association 1780 PreprocessedEntityID 1781 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]); 1782 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 1783 PreprocessingRecord::PPEntityID PPID = 1784 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true); 1785 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>( 1786 PPRec.getPreprocessedEntity(PPID)); 1787 if (PPDef) 1788 PPRec.RegisterMacroDefinition(Macro, PPDef); 1789 } 1790 1791 ++NumMacrosRead; 1792 break; 1793 } 1794 1795 case PP_TOKEN: { 1796 // If we see a TOKEN before a PP_MACRO_*, then the file is 1797 // erroneous, just pretend we didn't see this. 1798 if (!Macro) break; 1799 1800 unsigned Idx = 0; 1801 Token Tok = ReadToken(F, Record, Idx); 1802 Macro->AddTokenToBody(Tok); 1803 break; 1804 } 1805 } 1806 } 1807 } 1808 1809 PreprocessedEntityID 1810 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, 1811 unsigned LocalID) const { 1812 if (!M.ModuleOffsetMap.empty()) 1813 ReadModuleOffsetMap(M); 1814 1815 ContinuousRangeMap<uint32_t, int, 2>::const_iterator 1816 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS); 1817 assert(I != M.PreprocessedEntityRemap.end() 1818 && "Invalid index into preprocessed entity index remap"); 1819 1820 return LocalID + I->second; 1821 } 1822 1823 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) { 1824 return llvm::hash_combine(ikey.Size, ikey.ModTime); 1825 } 1826 1827 HeaderFileInfoTrait::internal_key_type 1828 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) { 1829 internal_key_type ikey = {FE->getSize(), 1830 M.HasTimestamps ? FE->getModificationTime() : 0, 1831 FE->getName(), /*Imported*/ false}; 1832 return ikey; 1833 } 1834 1835 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) { 1836 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime)) 1837 return false; 1838 1839 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename) 1840 return true; 1841 1842 // Determine whether the actual files are equivalent. 1843 FileManager &FileMgr = Reader.getFileManager(); 1844 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* { 1845 if (!Key.Imported) { 1846 if (auto File = FileMgr.getFile(Key.Filename)) 1847 return *File; 1848 return nullptr; 1849 } 1850 1851 std::string Resolved = std::string(Key.Filename); 1852 Reader.ResolveImportedPath(M, Resolved); 1853 if (auto File = FileMgr.getFile(Resolved)) 1854 return *File; 1855 return nullptr; 1856 }; 1857 1858 const FileEntry *FEA = GetFile(a); 1859 const FileEntry *FEB = GetFile(b); 1860 return FEA && FEA == FEB; 1861 } 1862 1863 std::pair<unsigned, unsigned> 1864 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) { 1865 using namespace llvm::support; 1866 1867 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d); 1868 unsigned DataLen = (unsigned) *d++; 1869 return std::make_pair(KeyLen, DataLen); 1870 } 1871 1872 HeaderFileInfoTrait::internal_key_type 1873 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) { 1874 using namespace llvm::support; 1875 1876 internal_key_type ikey; 1877 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d)); 1878 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d)); 1879 ikey.Filename = (const char *)d; 1880 ikey.Imported = true; 1881 return ikey; 1882 } 1883 1884 HeaderFileInfoTrait::data_type 1885 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d, 1886 unsigned DataLen) { 1887 using namespace llvm::support; 1888 1889 const unsigned char *End = d + DataLen; 1890 HeaderFileInfo HFI; 1891 unsigned Flags = *d++; 1892 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp. 1893 HFI.isImport |= (Flags >> 5) & 0x01; 1894 HFI.isPragmaOnce |= (Flags >> 4) & 0x01; 1895 HFI.DirInfo = (Flags >> 1) & 0x07; 1896 HFI.IndexHeaderMapHeader = Flags & 0x01; 1897 // FIXME: Find a better way to handle this. Maybe just store a 1898 // "has been included" flag? 1899 HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d), 1900 HFI.NumIncludes); 1901 HFI.ControllingMacroID = Reader.getGlobalIdentifierID( 1902 M, endian::readNext<uint32_t, little, unaligned>(d)); 1903 if (unsigned FrameworkOffset = 1904 endian::readNext<uint32_t, little, unaligned>(d)) { 1905 // The framework offset is 1 greater than the actual offset, 1906 // since 0 is used as an indicator for "no framework name". 1907 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1); 1908 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName); 1909 } 1910 1911 assert((End - d) % 4 == 0 && 1912 "Wrong data length in HeaderFileInfo deserialization"); 1913 while (d != End) { 1914 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d); 1915 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3); 1916 LocalSMID >>= 2; 1917 1918 // This header is part of a module. Associate it with the module to enable 1919 // implicit module import. 1920 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID); 1921 Module *Mod = Reader.getSubmodule(GlobalSMID); 1922 FileManager &FileMgr = Reader.getFileManager(); 1923 ModuleMap &ModMap = 1924 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap(); 1925 1926 std::string Filename = std::string(key.Filename); 1927 if (key.Imported) 1928 Reader.ResolveImportedPath(M, Filename); 1929 // FIXME: This is not always the right filename-as-written, but we're not 1930 // going to use this information to rebuild the module, so it doesn't make 1931 // a lot of difference. 1932 Module::Header H = {std::string(key.Filename), *FileMgr.getFile(Filename)}; 1933 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true); 1934 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader); 1935 } 1936 1937 // This HeaderFileInfo was externally loaded. 1938 HFI.External = true; 1939 HFI.IsValid = true; 1940 return HFI; 1941 } 1942 1943 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M, 1944 uint32_t MacroDirectivesOffset) { 1945 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard"); 1946 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset)); 1947 } 1948 1949 void ASTReader::ReadDefinedMacros() { 1950 // Note that we are loading defined macros. 1951 Deserializing Macros(this); 1952 1953 for (ModuleFile &I : llvm::reverse(ModuleMgr)) { 1954 BitstreamCursor &MacroCursor = I.MacroCursor; 1955 1956 // If there was no preprocessor block, skip this file. 1957 if (MacroCursor.getBitcodeBytes().empty()) 1958 continue; 1959 1960 BitstreamCursor Cursor = MacroCursor; 1961 if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) { 1962 Error(std::move(Err)); 1963 return; 1964 } 1965 1966 RecordData Record; 1967 while (true) { 1968 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks(); 1969 if (!MaybeE) { 1970 Error(MaybeE.takeError()); 1971 return; 1972 } 1973 llvm::BitstreamEntry E = MaybeE.get(); 1974 1975 switch (E.Kind) { 1976 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1977 case llvm::BitstreamEntry::Error: 1978 Error("malformed block record in AST file"); 1979 return; 1980 case llvm::BitstreamEntry::EndBlock: 1981 goto NextCursor; 1982 1983 case llvm::BitstreamEntry::Record: { 1984 Record.clear(); 1985 Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record); 1986 if (!MaybeRecord) { 1987 Error(MaybeRecord.takeError()); 1988 return; 1989 } 1990 switch (MaybeRecord.get()) { 1991 default: // Default behavior: ignore. 1992 break; 1993 1994 case PP_MACRO_OBJECT_LIKE: 1995 case PP_MACRO_FUNCTION_LIKE: { 1996 IdentifierInfo *II = getLocalIdentifier(I, Record[0]); 1997 if (II->isOutOfDate()) 1998 updateOutOfDateIdentifier(*II); 1999 break; 2000 } 2001 2002 case PP_TOKEN: 2003 // Ignore tokens. 2004 break; 2005 } 2006 break; 2007 } 2008 } 2009 } 2010 NextCursor: ; 2011 } 2012 } 2013 2014 namespace { 2015 2016 /// Visitor class used to look up identifirs in an AST file. 2017 class IdentifierLookupVisitor { 2018 StringRef Name; 2019 unsigned NameHash; 2020 unsigned PriorGeneration; 2021 unsigned &NumIdentifierLookups; 2022 unsigned &NumIdentifierLookupHits; 2023 IdentifierInfo *Found = nullptr; 2024 2025 public: 2026 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration, 2027 unsigned &NumIdentifierLookups, 2028 unsigned &NumIdentifierLookupHits) 2029 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)), 2030 PriorGeneration(PriorGeneration), 2031 NumIdentifierLookups(NumIdentifierLookups), 2032 NumIdentifierLookupHits(NumIdentifierLookupHits) {} 2033 2034 bool operator()(ModuleFile &M) { 2035 // If we've already searched this module file, skip it now. 2036 if (M.Generation <= PriorGeneration) 2037 return true; 2038 2039 ASTIdentifierLookupTable *IdTable 2040 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable; 2041 if (!IdTable) 2042 return false; 2043 2044 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M, 2045 Found); 2046 ++NumIdentifierLookups; 2047 ASTIdentifierLookupTable::iterator Pos = 2048 IdTable->find_hashed(Name, NameHash, &Trait); 2049 if (Pos == IdTable->end()) 2050 return false; 2051 2052 // Dereferencing the iterator has the effect of building the 2053 // IdentifierInfo node and populating it with the various 2054 // declarations it needs. 2055 ++NumIdentifierLookupHits; 2056 Found = *Pos; 2057 return true; 2058 } 2059 2060 // Retrieve the identifier info found within the module 2061 // files. 2062 IdentifierInfo *getIdentifierInfo() const { return Found; } 2063 }; 2064 2065 } // namespace 2066 2067 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) { 2068 // Note that we are loading an identifier. 2069 Deserializing AnIdentifier(this); 2070 2071 unsigned PriorGeneration = 0; 2072 if (getContext().getLangOpts().Modules) 2073 PriorGeneration = IdentifierGeneration[&II]; 2074 2075 // If there is a global index, look there first to determine which modules 2076 // provably do not have any results for this identifier. 2077 GlobalModuleIndex::HitSet Hits; 2078 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 2079 if (!loadGlobalIndex()) { 2080 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) { 2081 HitsPtr = &Hits; 2082 } 2083 } 2084 2085 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration, 2086 NumIdentifierLookups, 2087 NumIdentifierLookupHits); 2088 ModuleMgr.visit(Visitor, HitsPtr); 2089 markIdentifierUpToDate(&II); 2090 } 2091 2092 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) { 2093 if (!II) 2094 return; 2095 2096 II->setOutOfDate(false); 2097 2098 // Update the generation for this identifier. 2099 if (getContext().getLangOpts().Modules) 2100 IdentifierGeneration[II] = getGeneration(); 2101 } 2102 2103 void ASTReader::resolvePendingMacro(IdentifierInfo *II, 2104 const PendingMacroInfo &PMInfo) { 2105 ModuleFile &M = *PMInfo.M; 2106 2107 BitstreamCursor &Cursor = M.MacroCursor; 2108 SavedStreamPosition SavedPosition(Cursor); 2109 if (llvm::Error Err = 2110 Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) { 2111 Error(std::move(Err)); 2112 return; 2113 } 2114 2115 struct ModuleMacroRecord { 2116 SubmoduleID SubModID; 2117 MacroInfo *MI; 2118 SmallVector<SubmoduleID, 8> Overrides; 2119 }; 2120 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros; 2121 2122 // We expect to see a sequence of PP_MODULE_MACRO records listing exported 2123 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete 2124 // macro histroy. 2125 RecordData Record; 2126 while (true) { 2127 Expected<llvm::BitstreamEntry> MaybeEntry = 2128 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 2129 if (!MaybeEntry) { 2130 Error(MaybeEntry.takeError()); 2131 return; 2132 } 2133 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2134 2135 if (Entry.Kind != llvm::BitstreamEntry::Record) { 2136 Error("malformed block record in AST file"); 2137 return; 2138 } 2139 2140 Record.clear(); 2141 Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record); 2142 if (!MaybePP) { 2143 Error(MaybePP.takeError()); 2144 return; 2145 } 2146 switch ((PreprocessorRecordTypes)MaybePP.get()) { 2147 case PP_MACRO_DIRECTIVE_HISTORY: 2148 break; 2149 2150 case PP_MODULE_MACRO: { 2151 ModuleMacros.push_back(ModuleMacroRecord()); 2152 auto &Info = ModuleMacros.back(); 2153 Info.SubModID = getGlobalSubmoduleID(M, Record[0]); 2154 Info.MI = getMacro(getGlobalMacroID(M, Record[1])); 2155 for (int I = 2, N = Record.size(); I != N; ++I) 2156 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I])); 2157 continue; 2158 } 2159 2160 default: 2161 Error("malformed block record in AST file"); 2162 return; 2163 } 2164 2165 // We found the macro directive history; that's the last record 2166 // for this macro. 2167 break; 2168 } 2169 2170 // Module macros are listed in reverse dependency order. 2171 { 2172 std::reverse(ModuleMacros.begin(), ModuleMacros.end()); 2173 llvm::SmallVector<ModuleMacro*, 8> Overrides; 2174 for (auto &MMR : ModuleMacros) { 2175 Overrides.clear(); 2176 for (unsigned ModID : MMR.Overrides) { 2177 Module *Mod = getSubmodule(ModID); 2178 auto *Macro = PP.getModuleMacro(Mod, II); 2179 assert(Macro && "missing definition for overridden macro"); 2180 Overrides.push_back(Macro); 2181 } 2182 2183 bool Inserted = false; 2184 Module *Owner = getSubmodule(MMR.SubModID); 2185 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted); 2186 } 2187 } 2188 2189 // Don't read the directive history for a module; we don't have anywhere 2190 // to put it. 2191 if (M.isModule()) 2192 return; 2193 2194 // Deserialize the macro directives history in reverse source-order. 2195 MacroDirective *Latest = nullptr, *Earliest = nullptr; 2196 unsigned Idx = 0, N = Record.size(); 2197 while (Idx < N) { 2198 MacroDirective *MD = nullptr; 2199 SourceLocation Loc = ReadSourceLocation(M, Record, Idx); 2200 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++]; 2201 switch (K) { 2202 case MacroDirective::MD_Define: { 2203 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++])); 2204 MD = PP.AllocateDefMacroDirective(MI, Loc); 2205 break; 2206 } 2207 case MacroDirective::MD_Undefine: 2208 MD = PP.AllocateUndefMacroDirective(Loc); 2209 break; 2210 case MacroDirective::MD_Visibility: 2211 bool isPublic = Record[Idx++]; 2212 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic); 2213 break; 2214 } 2215 2216 if (!Latest) 2217 Latest = MD; 2218 if (Earliest) 2219 Earliest->setPrevious(MD); 2220 Earliest = MD; 2221 } 2222 2223 if (Latest) 2224 PP.setLoadedMacroDirective(II, Earliest, Latest); 2225 } 2226 2227 ASTReader::InputFileInfo 2228 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) { 2229 // Go find this input file. 2230 BitstreamCursor &Cursor = F.InputFilesCursor; 2231 SavedStreamPosition SavedPosition(Cursor); 2232 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2233 // FIXME this drops errors on the floor. 2234 consumeError(std::move(Err)); 2235 } 2236 2237 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 2238 if (!MaybeCode) { 2239 // FIXME this drops errors on the floor. 2240 consumeError(MaybeCode.takeError()); 2241 } 2242 unsigned Code = MaybeCode.get(); 2243 RecordData Record; 2244 StringRef Blob; 2245 2246 if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob)) 2247 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE && 2248 "invalid record type for input file"); 2249 else { 2250 // FIXME this drops errors on the floor. 2251 consumeError(Maybe.takeError()); 2252 } 2253 2254 assert(Record[0] == ID && "Bogus stored ID or offset"); 2255 InputFileInfo R; 2256 R.StoredSize = static_cast<off_t>(Record[1]); 2257 R.StoredTime = static_cast<time_t>(Record[2]); 2258 R.Overridden = static_cast<bool>(Record[3]); 2259 R.Transient = static_cast<bool>(Record[4]); 2260 R.TopLevelModuleMap = static_cast<bool>(Record[5]); 2261 R.Filename = std::string(Blob); 2262 ResolveImportedPath(F, R.Filename); 2263 2264 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 2265 if (!MaybeEntry) // FIXME this drops errors on the floor. 2266 consumeError(MaybeEntry.takeError()); 2267 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2268 assert(Entry.Kind == llvm::BitstreamEntry::Record && 2269 "expected record type for input file hash"); 2270 2271 Record.clear(); 2272 if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record)) 2273 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH && 2274 "invalid record type for input file hash"); 2275 else { 2276 // FIXME this drops errors on the floor. 2277 consumeError(Maybe.takeError()); 2278 } 2279 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) | 2280 static_cast<uint64_t>(Record[0]); 2281 return R; 2282 } 2283 2284 static unsigned moduleKindForDiagnostic(ModuleKind Kind); 2285 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) { 2286 // If this ID is bogus, just return an empty input file. 2287 if (ID == 0 || ID > F.InputFilesLoaded.size()) 2288 return InputFile(); 2289 2290 // If we've already loaded this input file, return it. 2291 if (F.InputFilesLoaded[ID-1].getFile()) 2292 return F.InputFilesLoaded[ID-1]; 2293 2294 if (F.InputFilesLoaded[ID-1].isNotFound()) 2295 return InputFile(); 2296 2297 // Go find this input file. 2298 BitstreamCursor &Cursor = F.InputFilesCursor; 2299 SavedStreamPosition SavedPosition(Cursor); 2300 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2301 // FIXME this drops errors on the floor. 2302 consumeError(std::move(Err)); 2303 } 2304 2305 InputFileInfo FI = readInputFileInfo(F, ID); 2306 off_t StoredSize = FI.StoredSize; 2307 time_t StoredTime = FI.StoredTime; 2308 bool Overridden = FI.Overridden; 2309 bool Transient = FI.Transient; 2310 StringRef Filename = FI.Filename; 2311 uint64_t StoredContentHash = FI.ContentHash; 2312 2313 const FileEntry *File = nullptr; 2314 if (auto FE = FileMgr.getFile(Filename, /*OpenFile=*/false)) 2315 File = *FE; 2316 2317 // If we didn't find the file, resolve it relative to the 2318 // original directory from which this AST file was created. 2319 if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() && 2320 F.OriginalDir != F.BaseDirectory) { 2321 std::string Resolved = resolveFileRelativeToOriginalDir( 2322 std::string(Filename), F.OriginalDir, F.BaseDirectory); 2323 if (!Resolved.empty()) 2324 if (auto FE = FileMgr.getFile(Resolved)) 2325 File = *FE; 2326 } 2327 2328 // For an overridden file, create a virtual file with the stored 2329 // size/timestamp. 2330 if ((Overridden || Transient) && File == nullptr) 2331 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime); 2332 2333 if (File == nullptr) { 2334 if (Complain) { 2335 std::string ErrorStr = "could not find file '"; 2336 ErrorStr += Filename; 2337 ErrorStr += "' referenced by AST file '"; 2338 ErrorStr += F.FileName; 2339 ErrorStr += "'"; 2340 Error(ErrorStr); 2341 } 2342 // Record that we didn't find the file. 2343 F.InputFilesLoaded[ID-1] = InputFile::getNotFound(); 2344 return InputFile(); 2345 } 2346 2347 // Check if there was a request to override the contents of the file 2348 // that was part of the precompiled header. Overriding such a file 2349 // can lead to problems when lexing using the source locations from the 2350 // PCH. 2351 SourceManager &SM = getSourceManager(); 2352 // FIXME: Reject if the overrides are different. 2353 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) { 2354 if (Complain) 2355 Error(diag::err_fe_pch_file_overridden, Filename); 2356 2357 // After emitting the diagnostic, bypass the overriding file to recover 2358 // (this creates a separate FileEntry). 2359 File = SM.bypassFileContentsOverride(*File); 2360 if (!File) { 2361 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound(); 2362 return InputFile(); 2363 } 2364 } 2365 2366 enum ModificationType { 2367 Size, 2368 ModTime, 2369 Content, 2370 None, 2371 }; 2372 auto HasInputFileChanged = [&]() { 2373 if (StoredSize != File->getSize()) 2374 return ModificationType::Size; 2375 if (!DisableValidation && StoredTime && 2376 StoredTime != File->getModificationTime()) { 2377 // In case the modification time changes but not the content, 2378 // accept the cached file as legit. 2379 if (ValidateASTInputFilesContent && 2380 StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) { 2381 auto MemBuffOrError = FileMgr.getBufferForFile(File); 2382 if (!MemBuffOrError) { 2383 if (!Complain) 2384 return ModificationType::ModTime; 2385 std::string ErrorStr = "could not get buffer for file '"; 2386 ErrorStr += File->getName(); 2387 ErrorStr += "'"; 2388 Error(ErrorStr); 2389 return ModificationType::ModTime; 2390 } 2391 2392 auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer()); 2393 if (StoredContentHash == static_cast<uint64_t>(ContentHash)) 2394 return ModificationType::None; 2395 return ModificationType::Content; 2396 } 2397 return ModificationType::ModTime; 2398 } 2399 return ModificationType::None; 2400 }; 2401 2402 bool IsOutOfDate = false; 2403 auto FileChange = HasInputFileChanged(); 2404 // For an overridden file, there is nothing to validate. 2405 if (!Overridden && FileChange != ModificationType::None) { 2406 if (Complain) { 2407 // Build a list of the PCH imports that got us here (in reverse). 2408 SmallVector<ModuleFile *, 4> ImportStack(1, &F); 2409 while (!ImportStack.back()->ImportedBy.empty()) 2410 ImportStack.push_back(ImportStack.back()->ImportedBy[0]); 2411 2412 // The top-level PCH is stale. 2413 StringRef TopLevelPCHName(ImportStack.back()->FileName); 2414 unsigned DiagnosticKind = 2415 moduleKindForDiagnostic(ImportStack.back()->Kind); 2416 if (DiagnosticKind == 0) 2417 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName, 2418 (unsigned)FileChange); 2419 else if (DiagnosticKind == 1) 2420 Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName, 2421 (unsigned)FileChange); 2422 else 2423 Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName, 2424 (unsigned)FileChange); 2425 2426 // Print the import stack. 2427 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) { 2428 Diag(diag::note_pch_required_by) 2429 << Filename << ImportStack[0]->FileName; 2430 for (unsigned I = 1; I < ImportStack.size(); ++I) 2431 Diag(diag::note_pch_required_by) 2432 << ImportStack[I-1]->FileName << ImportStack[I]->FileName; 2433 } 2434 2435 if (!Diags.isDiagnosticInFlight()) 2436 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName; 2437 } 2438 2439 IsOutOfDate = true; 2440 } 2441 // FIXME: If the file is overridden and we've already opened it, 2442 // issue an error (or split it into a separate FileEntry). 2443 2444 InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate); 2445 2446 // Note that we've loaded this input file. 2447 F.InputFilesLoaded[ID-1] = IF; 2448 return IF; 2449 } 2450 2451 /// If we are loading a relocatable PCH or module file, and the filename 2452 /// is not an absolute path, add the system or module root to the beginning of 2453 /// the file name. 2454 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) { 2455 // Resolve relative to the base directory, if we have one. 2456 if (!M.BaseDirectory.empty()) 2457 return ResolveImportedPath(Filename, M.BaseDirectory); 2458 } 2459 2460 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) { 2461 if (Filename.empty() || llvm::sys::path::is_absolute(Filename)) 2462 return; 2463 2464 SmallString<128> Buffer; 2465 llvm::sys::path::append(Buffer, Prefix, Filename); 2466 Filename.assign(Buffer.begin(), Buffer.end()); 2467 } 2468 2469 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) { 2470 switch (ARR) { 2471 case ASTReader::Failure: return true; 2472 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing); 2473 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate); 2474 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch); 2475 case ASTReader::ConfigurationMismatch: 2476 return !(Caps & ASTReader::ARR_ConfigurationMismatch); 2477 case ASTReader::HadErrors: return true; 2478 case ASTReader::Success: return false; 2479 } 2480 2481 llvm_unreachable("unknown ASTReadResult"); 2482 } 2483 2484 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock( 2485 BitstreamCursor &Stream, unsigned ClientLoadCapabilities, 2486 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener, 2487 std::string &SuggestedPredefines) { 2488 if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) { 2489 // FIXME this drops errors on the floor. 2490 consumeError(std::move(Err)); 2491 return Failure; 2492 } 2493 2494 // Read all of the records in the options block. 2495 RecordData Record; 2496 ASTReadResult Result = Success; 2497 while (true) { 2498 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2499 if (!MaybeEntry) { 2500 // FIXME this drops errors on the floor. 2501 consumeError(MaybeEntry.takeError()); 2502 return Failure; 2503 } 2504 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2505 2506 switch (Entry.Kind) { 2507 case llvm::BitstreamEntry::Error: 2508 case llvm::BitstreamEntry::SubBlock: 2509 return Failure; 2510 2511 case llvm::BitstreamEntry::EndBlock: 2512 return Result; 2513 2514 case llvm::BitstreamEntry::Record: 2515 // The interesting case. 2516 break; 2517 } 2518 2519 // Read and process a record. 2520 Record.clear(); 2521 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record); 2522 if (!MaybeRecordType) { 2523 // FIXME this drops errors on the floor. 2524 consumeError(MaybeRecordType.takeError()); 2525 return Failure; 2526 } 2527 switch ((OptionsRecordTypes)MaybeRecordType.get()) { 2528 case LANGUAGE_OPTIONS: { 2529 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2530 if (ParseLanguageOptions(Record, Complain, Listener, 2531 AllowCompatibleConfigurationMismatch)) 2532 Result = ConfigurationMismatch; 2533 break; 2534 } 2535 2536 case TARGET_OPTIONS: { 2537 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2538 if (ParseTargetOptions(Record, Complain, Listener, 2539 AllowCompatibleConfigurationMismatch)) 2540 Result = ConfigurationMismatch; 2541 break; 2542 } 2543 2544 case FILE_SYSTEM_OPTIONS: { 2545 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2546 if (!AllowCompatibleConfigurationMismatch && 2547 ParseFileSystemOptions(Record, Complain, Listener)) 2548 Result = ConfigurationMismatch; 2549 break; 2550 } 2551 2552 case HEADER_SEARCH_OPTIONS: { 2553 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2554 if (!AllowCompatibleConfigurationMismatch && 2555 ParseHeaderSearchOptions(Record, Complain, Listener)) 2556 Result = ConfigurationMismatch; 2557 break; 2558 } 2559 2560 case PREPROCESSOR_OPTIONS: 2561 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2562 if (!AllowCompatibleConfigurationMismatch && 2563 ParsePreprocessorOptions(Record, Complain, Listener, 2564 SuggestedPredefines)) 2565 Result = ConfigurationMismatch; 2566 break; 2567 } 2568 } 2569 } 2570 2571 ASTReader::ASTReadResult 2572 ASTReader::ReadControlBlock(ModuleFile &F, 2573 SmallVectorImpl<ImportedModule> &Loaded, 2574 const ModuleFile *ImportedBy, 2575 unsigned ClientLoadCapabilities) { 2576 BitstreamCursor &Stream = F.Stream; 2577 2578 if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) { 2579 Error(std::move(Err)); 2580 return Failure; 2581 } 2582 2583 // Lambda to read the unhashed control block the first time it's called. 2584 // 2585 // For PCM files, the unhashed control block cannot be read until after the 2586 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still 2587 // need to look ahead before reading the IMPORTS record. For consistency, 2588 // this block is always read somehow (see BitstreamEntry::EndBlock). 2589 bool HasReadUnhashedControlBlock = false; 2590 auto readUnhashedControlBlockOnce = [&]() { 2591 if (!HasReadUnhashedControlBlock) { 2592 HasReadUnhashedControlBlock = true; 2593 if (ASTReadResult Result = 2594 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities)) 2595 return Result; 2596 } 2597 return Success; 2598 }; 2599 2600 // Read all of the records and blocks in the control block. 2601 RecordData Record; 2602 unsigned NumInputs = 0; 2603 unsigned NumUserInputs = 0; 2604 StringRef BaseDirectoryAsWritten; 2605 while (true) { 2606 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2607 if (!MaybeEntry) { 2608 Error(MaybeEntry.takeError()); 2609 return Failure; 2610 } 2611 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2612 2613 switch (Entry.Kind) { 2614 case llvm::BitstreamEntry::Error: 2615 Error("malformed block record in AST file"); 2616 return Failure; 2617 case llvm::BitstreamEntry::EndBlock: { 2618 // Validate the module before returning. This call catches an AST with 2619 // no module name and no imports. 2620 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2621 return Result; 2622 2623 // Validate input files. 2624 const HeaderSearchOptions &HSOpts = 2625 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 2626 2627 // All user input files reside at the index range [0, NumUserInputs), and 2628 // system input files reside at [NumUserInputs, NumInputs). For explicitly 2629 // loaded module files, ignore missing inputs. 2630 if (!DisableValidation && F.Kind != MK_ExplicitModule && 2631 F.Kind != MK_PrebuiltModule) { 2632 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 2633 2634 // If we are reading a module, we will create a verification timestamp, 2635 // so we verify all input files. Otherwise, verify only user input 2636 // files. 2637 2638 unsigned N = NumUserInputs; 2639 if (ValidateSystemInputs || 2640 (HSOpts.ModulesValidateOncePerBuildSession && 2641 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp && 2642 F.Kind == MK_ImplicitModule)) 2643 N = NumInputs; 2644 2645 for (unsigned I = 0; I < N; ++I) { 2646 InputFile IF = getInputFile(F, I+1, Complain); 2647 if (!IF.getFile() || IF.isOutOfDate()) 2648 return OutOfDate; 2649 } 2650 } 2651 2652 if (Listener) 2653 Listener->visitModuleFile(F.FileName, F.Kind); 2654 2655 if (Listener && Listener->needsInputFileVisitation()) { 2656 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs 2657 : NumUserInputs; 2658 for (unsigned I = 0; I < N; ++I) { 2659 bool IsSystem = I >= NumUserInputs; 2660 InputFileInfo FI = readInputFileInfo(F, I+1); 2661 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden, 2662 F.Kind == MK_ExplicitModule || 2663 F.Kind == MK_PrebuiltModule); 2664 } 2665 } 2666 2667 return Success; 2668 } 2669 2670 case llvm::BitstreamEntry::SubBlock: 2671 switch (Entry.ID) { 2672 case INPUT_FILES_BLOCK_ID: 2673 F.InputFilesCursor = Stream; 2674 if (llvm::Error Err = Stream.SkipBlock()) { 2675 Error(std::move(Err)); 2676 return Failure; 2677 } 2678 if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) { 2679 Error("malformed block record in AST file"); 2680 return Failure; 2681 } 2682 continue; 2683 2684 case OPTIONS_BLOCK_ID: 2685 // If we're reading the first module for this group, check its options 2686 // are compatible with ours. For modules it imports, no further checking 2687 // is required, because we checked them when we built it. 2688 if (Listener && !ImportedBy) { 2689 // Should we allow the configuration of the module file to differ from 2690 // the configuration of the current translation unit in a compatible 2691 // way? 2692 // 2693 // FIXME: Allow this for files explicitly specified with -include-pch. 2694 bool AllowCompatibleConfigurationMismatch = 2695 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 2696 2697 ASTReadResult Result = 2698 ReadOptionsBlock(Stream, ClientLoadCapabilities, 2699 AllowCompatibleConfigurationMismatch, *Listener, 2700 SuggestedPredefines); 2701 if (Result == Failure) { 2702 Error("malformed block record in AST file"); 2703 return Result; 2704 } 2705 2706 if (DisableValidation || 2707 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 2708 Result = Success; 2709 2710 // If we can't load the module, exit early since we likely 2711 // will rebuild the module anyway. The stream may be in the 2712 // middle of a block. 2713 if (Result != Success) 2714 return Result; 2715 } else if (llvm::Error Err = Stream.SkipBlock()) { 2716 Error(std::move(Err)); 2717 return Failure; 2718 } 2719 continue; 2720 2721 default: 2722 if (llvm::Error Err = Stream.SkipBlock()) { 2723 Error(std::move(Err)); 2724 return Failure; 2725 } 2726 continue; 2727 } 2728 2729 case llvm::BitstreamEntry::Record: 2730 // The interesting case. 2731 break; 2732 } 2733 2734 // Read and process a record. 2735 Record.clear(); 2736 StringRef Blob; 2737 Expected<unsigned> MaybeRecordType = 2738 Stream.readRecord(Entry.ID, Record, &Blob); 2739 if (!MaybeRecordType) { 2740 Error(MaybeRecordType.takeError()); 2741 return Failure; 2742 } 2743 switch ((ControlRecordTypes)MaybeRecordType.get()) { 2744 case METADATA: { 2745 if (Record[0] != VERSION_MAJOR && !DisableValidation) { 2746 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2747 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old 2748 : diag::err_pch_version_too_new); 2749 return VersionMismatch; 2750 } 2751 2752 bool hasErrors = Record[6]; 2753 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) { 2754 Diag(diag::err_pch_with_compiler_errors); 2755 return HadErrors; 2756 } 2757 if (hasErrors) { 2758 Diags.ErrorOccurred = true; 2759 Diags.UncompilableErrorOccurred = true; 2760 Diags.UnrecoverableErrorOccurred = true; 2761 } 2762 2763 F.RelocatablePCH = Record[4]; 2764 // Relative paths in a relocatable PCH are relative to our sysroot. 2765 if (F.RelocatablePCH) 2766 F.BaseDirectory = isysroot.empty() ? "/" : isysroot; 2767 2768 F.HasTimestamps = Record[5]; 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 getContext().getLangOpts().BuildingPCHWithObjectFile) 3239 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3240 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3241 break; 3242 3243 case SPECIAL_TYPES: 3244 if (SpecialTypes.empty()) { 3245 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3246 SpecialTypes.push_back(getGlobalTypeID(F, Record[I])); 3247 break; 3248 } 3249 3250 if (SpecialTypes.size() != Record.size()) { 3251 Error("invalid special-types record"); 3252 return Failure; 3253 } 3254 3255 for (unsigned I = 0, N = Record.size(); I != N; ++I) { 3256 serialization::TypeID ID = getGlobalTypeID(F, Record[I]); 3257 if (!SpecialTypes[I]) 3258 SpecialTypes[I] = ID; 3259 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate 3260 // merge step? 3261 } 3262 break; 3263 3264 case STATISTICS: 3265 TotalNumStatements += Record[0]; 3266 TotalNumMacros += Record[1]; 3267 TotalLexicalDeclContexts += Record[2]; 3268 TotalVisibleDeclContexts += Record[3]; 3269 break; 3270 3271 case UNUSED_FILESCOPED_DECLS: 3272 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3273 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I])); 3274 break; 3275 3276 case DELEGATING_CTORS: 3277 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3278 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I])); 3279 break; 3280 3281 case WEAK_UNDECLARED_IDENTIFIERS: 3282 if (Record.size() % 4 != 0) { 3283 Error("invalid weak identifiers record"); 3284 return Failure; 3285 } 3286 3287 // FIXME: Ignore weak undeclared identifiers from non-original PCH 3288 // files. This isn't the way to do it :) 3289 WeakUndeclaredIdentifiers.clear(); 3290 3291 // Translate the weak, undeclared identifiers into global IDs. 3292 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) { 3293 WeakUndeclaredIdentifiers.push_back( 3294 getGlobalIdentifierID(F, Record[I++])); 3295 WeakUndeclaredIdentifiers.push_back( 3296 getGlobalIdentifierID(F, Record[I++])); 3297 WeakUndeclaredIdentifiers.push_back( 3298 ReadSourceLocation(F, Record, I).getRawEncoding()); 3299 WeakUndeclaredIdentifiers.push_back(Record[I++]); 3300 } 3301 break; 3302 3303 case SELECTOR_OFFSETS: { 3304 F.SelectorOffsets = (const uint32_t *)Blob.data(); 3305 F.LocalNumSelectors = Record[0]; 3306 unsigned LocalBaseSelectorID = Record[1]; 3307 F.BaseSelectorID = getTotalNumSelectors(); 3308 3309 if (F.LocalNumSelectors > 0) { 3310 // Introduce the global -> local mapping for selectors within this 3311 // module. 3312 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F)); 3313 3314 // Introduce the local -> global mapping for selectors within this 3315 // module. 3316 F.SelectorRemap.insertOrReplace( 3317 std::make_pair(LocalBaseSelectorID, 3318 F.BaseSelectorID - LocalBaseSelectorID)); 3319 3320 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors); 3321 } 3322 break; 3323 } 3324 3325 case METHOD_POOL: 3326 F.SelectorLookupTableData = (const unsigned char *)Blob.data(); 3327 if (Record[0]) 3328 F.SelectorLookupTable 3329 = ASTSelectorLookupTable::Create( 3330 F.SelectorLookupTableData + Record[0], 3331 F.SelectorLookupTableData, 3332 ASTSelectorLookupTrait(*this, F)); 3333 TotalNumMethodPoolEntries += Record[1]; 3334 break; 3335 3336 case REFERENCED_SELECTOR_POOL: 3337 if (!Record.empty()) { 3338 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) { 3339 ReferencedSelectorsData.push_back(getGlobalSelectorID(F, 3340 Record[Idx++])); 3341 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx). 3342 getRawEncoding()); 3343 } 3344 } 3345 break; 3346 3347 case PP_CONDITIONAL_STACK: 3348 if (!Record.empty()) { 3349 unsigned Idx = 0, End = Record.size() - 1; 3350 bool ReachedEOFWhileSkipping = Record[Idx++]; 3351 llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo; 3352 if (ReachedEOFWhileSkipping) { 3353 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx); 3354 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx); 3355 bool FoundNonSkipPortion = Record[Idx++]; 3356 bool FoundElse = Record[Idx++]; 3357 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx); 3358 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion, 3359 FoundElse, ElseLoc); 3360 } 3361 SmallVector<PPConditionalInfo, 4> ConditionalStack; 3362 while (Idx < End) { 3363 auto Loc = ReadSourceLocation(F, Record, Idx); 3364 bool WasSkipping = Record[Idx++]; 3365 bool FoundNonSkip = Record[Idx++]; 3366 bool FoundElse = Record[Idx++]; 3367 ConditionalStack.push_back( 3368 {Loc, WasSkipping, FoundNonSkip, FoundElse}); 3369 } 3370 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo); 3371 } 3372 break; 3373 3374 case PP_COUNTER_VALUE: 3375 if (!Record.empty() && Listener) 3376 Listener->ReadCounter(F, Record[0]); 3377 break; 3378 3379 case FILE_SORTED_DECLS: 3380 F.FileSortedDecls = (const DeclID *)Blob.data(); 3381 F.NumFileSortedDecls = Record[0]; 3382 break; 3383 3384 case SOURCE_LOCATION_OFFSETS: { 3385 F.SLocEntryOffsets = (const uint32_t *)Blob.data(); 3386 F.LocalNumSLocEntries = Record[0]; 3387 unsigned SLocSpaceSize = Record[1]; 3388 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset; 3389 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) = 3390 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, 3391 SLocSpaceSize); 3392 if (!F.SLocEntryBaseID) { 3393 Error("ran out of source locations"); 3394 break; 3395 } 3396 // Make our entry in the range map. BaseID is negative and growing, so 3397 // we invert it. Because we invert it, though, we need the other end of 3398 // the range. 3399 unsigned RangeStart = 3400 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1; 3401 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F)); 3402 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset); 3403 3404 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing. 3405 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0); 3406 GlobalSLocOffsetMap.insert( 3407 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset 3408 - SLocSpaceSize,&F)); 3409 3410 // Initialize the remapping table. 3411 // Invalid stays invalid. 3412 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0)); 3413 // This module. Base was 2 when being compiled. 3414 F.SLocRemap.insertOrReplace(std::make_pair(2U, 3415 static_cast<int>(F.SLocEntryBaseOffset - 2))); 3416 3417 TotalNumSLocEntries += F.LocalNumSLocEntries; 3418 break; 3419 } 3420 3421 case MODULE_OFFSET_MAP: 3422 F.ModuleOffsetMap = Blob; 3423 break; 3424 3425 case SOURCE_MANAGER_LINE_TABLE: 3426 if (ParseLineTable(F, Record)) { 3427 Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file"); 3428 return Failure; 3429 } 3430 break; 3431 3432 case SOURCE_LOCATION_PRELOADS: { 3433 // Need to transform from the local view (1-based IDs) to the global view, 3434 // which is based off F.SLocEntryBaseID. 3435 if (!F.PreloadSLocEntries.empty()) { 3436 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file"); 3437 return Failure; 3438 } 3439 3440 F.PreloadSLocEntries.swap(Record); 3441 break; 3442 } 3443 3444 case EXT_VECTOR_DECLS: 3445 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3446 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I])); 3447 break; 3448 3449 case VTABLE_USES: 3450 if (Record.size() % 3 != 0) { 3451 Error("Invalid VTABLE_USES record"); 3452 return Failure; 3453 } 3454 3455 // Later tables overwrite earlier ones. 3456 // FIXME: Modules will have some trouble with this. This is clearly not 3457 // the right way to do this. 3458 VTableUses.clear(); 3459 3460 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) { 3461 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++])); 3462 VTableUses.push_back( 3463 ReadSourceLocation(F, Record, Idx).getRawEncoding()); 3464 VTableUses.push_back(Record[Idx++]); 3465 } 3466 break; 3467 3468 case PENDING_IMPLICIT_INSTANTIATIONS: 3469 if (PendingInstantiations.size() % 2 != 0) { 3470 Error("Invalid existing PendingInstantiations"); 3471 return Failure; 3472 } 3473 3474 if (Record.size() % 2 != 0) { 3475 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block"); 3476 return Failure; 3477 } 3478 3479 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3480 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++])); 3481 PendingInstantiations.push_back( 3482 ReadSourceLocation(F, Record, I).getRawEncoding()); 3483 } 3484 break; 3485 3486 case SEMA_DECL_REFS: 3487 if (Record.size() != 3) { 3488 Error("Invalid SEMA_DECL_REFS block"); 3489 return Failure; 3490 } 3491 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3492 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3493 break; 3494 3495 case PPD_ENTITIES_OFFSETS: { 3496 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data(); 3497 assert(Blob.size() % sizeof(PPEntityOffset) == 0); 3498 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset); 3499 3500 unsigned LocalBasePreprocessedEntityID = Record[0]; 3501 3502 unsigned StartingID; 3503 if (!PP.getPreprocessingRecord()) 3504 PP.createPreprocessingRecord(); 3505 if (!PP.getPreprocessingRecord()->getExternalSource()) 3506 PP.getPreprocessingRecord()->SetExternalSource(*this); 3507 StartingID 3508 = PP.getPreprocessingRecord() 3509 ->allocateLoadedEntities(F.NumPreprocessedEntities); 3510 F.BasePreprocessedEntityID = StartingID; 3511 3512 if (F.NumPreprocessedEntities > 0) { 3513 // Introduce the global -> local mapping for preprocessed entities in 3514 // this module. 3515 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F)); 3516 3517 // Introduce the local -> global mapping for preprocessed entities in 3518 // this module. 3519 F.PreprocessedEntityRemap.insertOrReplace( 3520 std::make_pair(LocalBasePreprocessedEntityID, 3521 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID)); 3522 } 3523 3524 break; 3525 } 3526 3527 case PPD_SKIPPED_RANGES: { 3528 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data(); 3529 assert(Blob.size() % sizeof(PPSkippedRange) == 0); 3530 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange); 3531 3532 if (!PP.getPreprocessingRecord()) 3533 PP.createPreprocessingRecord(); 3534 if (!PP.getPreprocessingRecord()->getExternalSource()) 3535 PP.getPreprocessingRecord()->SetExternalSource(*this); 3536 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord() 3537 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges); 3538 3539 if (F.NumPreprocessedSkippedRanges > 0) 3540 GlobalSkippedRangeMap.insert( 3541 std::make_pair(F.BasePreprocessedSkippedRangeID, &F)); 3542 break; 3543 } 3544 3545 case DECL_UPDATE_OFFSETS: 3546 if (Record.size() % 2 != 0) { 3547 Error("invalid DECL_UPDATE_OFFSETS block in AST file"); 3548 return Failure; 3549 } 3550 for (unsigned I = 0, N = Record.size(); I != N; I += 2) { 3551 GlobalDeclID ID = getGlobalDeclID(F, Record[I]); 3552 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1])); 3553 3554 // If we've already loaded the decl, perform the updates when we finish 3555 // loading this block. 3556 if (Decl *D = GetExistingDecl(ID)) 3557 PendingUpdateRecords.push_back( 3558 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3559 } 3560 break; 3561 3562 case OBJC_CATEGORIES_MAP: 3563 if (F.LocalNumObjCCategoriesInMap != 0) { 3564 Error("duplicate OBJC_CATEGORIES_MAP record in AST file"); 3565 return Failure; 3566 } 3567 3568 F.LocalNumObjCCategoriesInMap = Record[0]; 3569 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data(); 3570 break; 3571 3572 case OBJC_CATEGORIES: 3573 F.ObjCCategories.swap(Record); 3574 break; 3575 3576 case CUDA_SPECIAL_DECL_REFS: 3577 // Later tables overwrite earlier ones. 3578 // FIXME: Modules will have trouble with this. 3579 CUDASpecialDeclRefs.clear(); 3580 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3581 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3582 break; 3583 3584 case HEADER_SEARCH_TABLE: 3585 F.HeaderFileInfoTableData = Blob.data(); 3586 F.LocalNumHeaderFileInfos = Record[1]; 3587 if (Record[0]) { 3588 F.HeaderFileInfoTable 3589 = HeaderFileInfoLookupTable::Create( 3590 (const unsigned char *)F.HeaderFileInfoTableData + Record[0], 3591 (const unsigned char *)F.HeaderFileInfoTableData, 3592 HeaderFileInfoTrait(*this, F, 3593 &PP.getHeaderSearchInfo(), 3594 Blob.data() + Record[2])); 3595 3596 PP.getHeaderSearchInfo().SetExternalSource(this); 3597 if (!PP.getHeaderSearchInfo().getExternalLookup()) 3598 PP.getHeaderSearchInfo().SetExternalLookup(this); 3599 } 3600 break; 3601 3602 case FP_PRAGMA_OPTIONS: 3603 // Later tables overwrite earlier ones. 3604 FPPragmaOptions.swap(Record); 3605 break; 3606 3607 case OPENCL_EXTENSIONS: 3608 for (unsigned I = 0, E = Record.size(); I != E; ) { 3609 auto Name = ReadString(Record, I); 3610 auto &Opt = OpenCLExtensions.OptMap[Name]; 3611 Opt.Supported = Record[I++] != 0; 3612 Opt.Enabled = Record[I++] != 0; 3613 Opt.Avail = Record[I++]; 3614 Opt.Core = Record[I++]; 3615 } 3616 break; 3617 3618 case OPENCL_EXTENSION_TYPES: 3619 for (unsigned I = 0, E = Record.size(); I != E;) { 3620 auto TypeID = static_cast<::TypeID>(Record[I++]); 3621 auto *Type = GetType(TypeID).getTypePtr(); 3622 auto NumExt = static_cast<unsigned>(Record[I++]); 3623 for (unsigned II = 0; II != NumExt; ++II) { 3624 auto Ext = ReadString(Record, I); 3625 OpenCLTypeExtMap[Type].insert(Ext); 3626 } 3627 } 3628 break; 3629 3630 case OPENCL_EXTENSION_DECLS: 3631 for (unsigned I = 0, E = Record.size(); I != E;) { 3632 auto DeclID = static_cast<::DeclID>(Record[I++]); 3633 auto *Decl = GetDecl(DeclID); 3634 auto NumExt = static_cast<unsigned>(Record[I++]); 3635 for (unsigned II = 0; II != NumExt; ++II) { 3636 auto Ext = ReadString(Record, I); 3637 OpenCLDeclExtMap[Decl].insert(Ext); 3638 } 3639 } 3640 break; 3641 3642 case TENTATIVE_DEFINITIONS: 3643 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3644 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I])); 3645 break; 3646 3647 case KNOWN_NAMESPACES: 3648 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3649 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I])); 3650 break; 3651 3652 case UNDEFINED_BUT_USED: 3653 if (UndefinedButUsed.size() % 2 != 0) { 3654 Error("Invalid existing UndefinedButUsed"); 3655 return Failure; 3656 } 3657 3658 if (Record.size() % 2 != 0) { 3659 Error("invalid undefined-but-used record"); 3660 return Failure; 3661 } 3662 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3663 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++])); 3664 UndefinedButUsed.push_back( 3665 ReadSourceLocation(F, Record, I).getRawEncoding()); 3666 } 3667 break; 3668 3669 case DELETE_EXPRS_TO_ANALYZE: 3670 for (unsigned I = 0, N = Record.size(); I != N;) { 3671 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++])); 3672 const uint64_t Count = Record[I++]; 3673 DelayedDeleteExprs.push_back(Count); 3674 for (uint64_t C = 0; C < Count; ++C) { 3675 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding()); 3676 bool IsArrayForm = Record[I++] == 1; 3677 DelayedDeleteExprs.push_back(IsArrayForm); 3678 } 3679 } 3680 break; 3681 3682 case IMPORTED_MODULES: 3683 if (!F.isModule()) { 3684 // If we aren't loading a module (which has its own exports), make 3685 // all of the imported modules visible. 3686 // FIXME: Deal with macros-only imports. 3687 for (unsigned I = 0, N = Record.size(); I != N; /**/) { 3688 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]); 3689 SourceLocation Loc = ReadSourceLocation(F, Record, I); 3690 if (GlobalID) { 3691 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc)); 3692 if (DeserializationListener) 3693 DeserializationListener->ModuleImportRead(GlobalID, Loc); 3694 } 3695 } 3696 } 3697 break; 3698 3699 case MACRO_OFFSET: { 3700 if (F.LocalNumMacros != 0) { 3701 Error("duplicate MACRO_OFFSET record in AST file"); 3702 return Failure; 3703 } 3704 F.MacroOffsets = (const uint32_t *)Blob.data(); 3705 F.LocalNumMacros = Record[0]; 3706 unsigned LocalBaseMacroID = Record[1]; 3707 F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset; 3708 F.BaseMacroID = getTotalNumMacros(); 3709 3710 if (F.LocalNumMacros > 0) { 3711 // Introduce the global -> local mapping for macros within this module. 3712 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F)); 3713 3714 // Introduce the local -> global mapping for macros within this module. 3715 F.MacroRemap.insertOrReplace( 3716 std::make_pair(LocalBaseMacroID, 3717 F.BaseMacroID - LocalBaseMacroID)); 3718 3719 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros); 3720 } 3721 break; 3722 } 3723 3724 case LATE_PARSED_TEMPLATE: 3725 LateParsedTemplates.append(Record.begin(), Record.end()); 3726 break; 3727 3728 case OPTIMIZE_PRAGMA_OPTIONS: 3729 if (Record.size() != 1) { 3730 Error("invalid pragma optimize record"); 3731 return Failure; 3732 } 3733 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]); 3734 break; 3735 3736 case MSSTRUCT_PRAGMA_OPTIONS: 3737 if (Record.size() != 1) { 3738 Error("invalid pragma ms_struct record"); 3739 return Failure; 3740 } 3741 PragmaMSStructState = Record[0]; 3742 break; 3743 3744 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS: 3745 if (Record.size() != 2) { 3746 Error("invalid pragma ms_struct record"); 3747 return Failure; 3748 } 3749 PragmaMSPointersToMembersState = Record[0]; 3750 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]); 3751 break; 3752 3753 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES: 3754 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3755 UnusedLocalTypedefNameCandidates.push_back( 3756 getGlobalDeclID(F, Record[I])); 3757 break; 3758 3759 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH: 3760 if (Record.size() != 1) { 3761 Error("invalid cuda pragma options record"); 3762 return Failure; 3763 } 3764 ForceCUDAHostDeviceDepth = Record[0]; 3765 break; 3766 3767 case PACK_PRAGMA_OPTIONS: { 3768 if (Record.size() < 3) { 3769 Error("invalid pragma pack record"); 3770 return Failure; 3771 } 3772 PragmaPackCurrentValue = Record[0]; 3773 PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]); 3774 unsigned NumStackEntries = Record[2]; 3775 unsigned Idx = 3; 3776 // Reset the stack when importing a new module. 3777 PragmaPackStack.clear(); 3778 for (unsigned I = 0; I < NumStackEntries; ++I) { 3779 PragmaPackStackEntry Entry; 3780 Entry.Value = Record[Idx++]; 3781 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3782 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3783 PragmaPackStrings.push_back(ReadString(Record, Idx)); 3784 Entry.SlotLabel = PragmaPackStrings.back(); 3785 PragmaPackStack.push_back(Entry); 3786 } 3787 break; 3788 } 3789 3790 case FLOAT_CONTROL_PRAGMA_OPTIONS: { 3791 if (Record.size() < 3) { 3792 Error("invalid pragma pack record"); 3793 return Failure; 3794 } 3795 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]); 3796 FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]); 3797 unsigned NumStackEntries = Record[2]; 3798 unsigned Idx = 3; 3799 // Reset the stack when importing a new module. 3800 FpPragmaStack.clear(); 3801 for (unsigned I = 0; I < NumStackEntries; ++I) { 3802 FpPragmaStackEntry Entry; 3803 Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]); 3804 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3805 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3806 FpPragmaStrings.push_back(ReadString(Record, Idx)); 3807 Entry.SlotLabel = FpPragmaStrings.back(); 3808 FpPragmaStack.push_back(Entry); 3809 } 3810 break; 3811 } 3812 3813 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS: 3814 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3815 DeclsToCheckForDeferredDiags.push_back(getGlobalDeclID(F, Record[I])); 3816 break; 3817 } 3818 } 3819 } 3820 3821 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const { 3822 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read"); 3823 3824 // Additional remapping information. 3825 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data(); 3826 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size(); 3827 F.ModuleOffsetMap = StringRef(); 3828 3829 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders. 3830 if (F.SLocRemap.find(0) == F.SLocRemap.end()) { 3831 F.SLocRemap.insert(std::make_pair(0U, 0)); 3832 F.SLocRemap.insert(std::make_pair(2U, 1)); 3833 } 3834 3835 // Continuous range maps we may be updating in our module. 3836 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder; 3837 RemapBuilder SLocRemap(F.SLocRemap); 3838 RemapBuilder IdentifierRemap(F.IdentifierRemap); 3839 RemapBuilder MacroRemap(F.MacroRemap); 3840 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap); 3841 RemapBuilder SubmoduleRemap(F.SubmoduleRemap); 3842 RemapBuilder SelectorRemap(F.SelectorRemap); 3843 RemapBuilder DeclRemap(F.DeclRemap); 3844 RemapBuilder TypeRemap(F.TypeRemap); 3845 3846 while (Data < DataEnd) { 3847 // FIXME: Looking up dependency modules by filename is horrible. Let's 3848 // start fixing this with prebuilt, explicit and implicit modules and see 3849 // how it goes... 3850 using namespace llvm::support; 3851 ModuleKind Kind = static_cast<ModuleKind>( 3852 endian::readNext<uint8_t, little, unaligned>(Data)); 3853 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data); 3854 StringRef Name = StringRef((const char*)Data, Len); 3855 Data += Len; 3856 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule || 3857 Kind == MK_ImplicitModule 3858 ? ModuleMgr.lookupByModuleName(Name) 3859 : ModuleMgr.lookupByFileName(Name)); 3860 if (!OM) { 3861 std::string Msg = 3862 "SourceLocation remap refers to unknown module, cannot find "; 3863 Msg.append(std::string(Name)); 3864 Error(Msg); 3865 return; 3866 } 3867 3868 uint32_t SLocOffset = 3869 endian::readNext<uint32_t, little, unaligned>(Data); 3870 uint32_t IdentifierIDOffset = 3871 endian::readNext<uint32_t, little, unaligned>(Data); 3872 uint32_t MacroIDOffset = 3873 endian::readNext<uint32_t, little, unaligned>(Data); 3874 uint32_t PreprocessedEntityIDOffset = 3875 endian::readNext<uint32_t, little, unaligned>(Data); 3876 uint32_t SubmoduleIDOffset = 3877 endian::readNext<uint32_t, little, unaligned>(Data); 3878 uint32_t SelectorIDOffset = 3879 endian::readNext<uint32_t, little, unaligned>(Data); 3880 uint32_t DeclIDOffset = 3881 endian::readNext<uint32_t, little, unaligned>(Data); 3882 uint32_t TypeIndexOffset = 3883 endian::readNext<uint32_t, little, unaligned>(Data); 3884 3885 uint32_t None = std::numeric_limits<uint32_t>::max(); 3886 3887 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset, 3888 RemapBuilder &Remap) { 3889 if (Offset != None) 3890 Remap.insert(std::make_pair(Offset, 3891 static_cast<int>(BaseOffset - Offset))); 3892 }; 3893 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap); 3894 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap); 3895 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap); 3896 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID, 3897 PreprocessedEntityRemap); 3898 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap); 3899 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap); 3900 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap); 3901 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap); 3902 3903 // Global -> local mappings. 3904 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset; 3905 } 3906 } 3907 3908 ASTReader::ASTReadResult 3909 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F, 3910 const ModuleFile *ImportedBy, 3911 unsigned ClientLoadCapabilities) { 3912 unsigned Idx = 0; 3913 F.ModuleMapPath = ReadPath(F, Record, Idx); 3914 3915 // Try to resolve ModuleName in the current header search context and 3916 // verify that it is found in the same module map file as we saved. If the 3917 // top-level AST file is a main file, skip this check because there is no 3918 // usable header search context. 3919 assert(!F.ModuleName.empty() && 3920 "MODULE_NAME should come before MODULE_MAP_FILE"); 3921 if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) { 3922 // An implicitly-loaded module file should have its module listed in some 3923 // module map file that we've already loaded. 3924 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName); 3925 auto &Map = PP.getHeaderSearchInfo().getModuleMap(); 3926 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr; 3927 // Don't emit module relocation error if we have -fno-validate-pch 3928 if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) { 3929 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) { 3930 if (auto *ASTFE = M ? M->getASTFile() : nullptr) { 3931 // This module was defined by an imported (explicit) module. 3932 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName 3933 << ASTFE->getName(); 3934 } else { 3935 // This module was built with a different module map. 3936 Diag(diag::err_imported_module_not_found) 3937 << F.ModuleName << F.FileName 3938 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath 3939 << !ImportedBy; 3940 // In case it was imported by a PCH, there's a chance the user is 3941 // just missing to include the search path to the directory containing 3942 // the modulemap. 3943 if (ImportedBy && ImportedBy->Kind == MK_PCH) 3944 Diag(diag::note_imported_by_pch_module_not_found) 3945 << llvm::sys::path::parent_path(F.ModuleMapPath); 3946 } 3947 } 3948 return OutOfDate; 3949 } 3950 3951 assert(M->Name == F.ModuleName && "found module with different name"); 3952 3953 // Check the primary module map file. 3954 auto StoredModMap = FileMgr.getFile(F.ModuleMapPath); 3955 if (!StoredModMap || *StoredModMap != ModMap) { 3956 assert(ModMap && "found module is missing module map file"); 3957 assert((ImportedBy || F.Kind == MK_ImplicitModule) && 3958 "top-level import should be verified"); 3959 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy; 3960 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3961 Diag(diag::err_imported_module_modmap_changed) 3962 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName) 3963 << ModMap->getName() << F.ModuleMapPath << NotImported; 3964 return OutOfDate; 3965 } 3966 3967 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps; 3968 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) { 3969 // FIXME: we should use input files rather than storing names. 3970 std::string Filename = ReadPath(F, Record, Idx); 3971 auto F = FileMgr.getFile(Filename, false, false); 3972 if (!F) { 3973 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3974 Error("could not find file '" + Filename +"' referenced by AST file"); 3975 return OutOfDate; 3976 } 3977 AdditionalStoredMaps.insert(*F); 3978 } 3979 3980 // Check any additional module map files (e.g. module.private.modulemap) 3981 // that are not in the pcm. 3982 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) { 3983 for (const FileEntry *ModMap : *AdditionalModuleMaps) { 3984 // Remove files that match 3985 // Note: SmallPtrSet::erase is really remove 3986 if (!AdditionalStoredMaps.erase(ModMap)) { 3987 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3988 Diag(diag::err_module_different_modmap) 3989 << F.ModuleName << /*new*/0 << ModMap->getName(); 3990 return OutOfDate; 3991 } 3992 } 3993 } 3994 3995 // Check any additional module map files that are in the pcm, but not 3996 // found in header search. Cases that match are already removed. 3997 for (const FileEntry *ModMap : AdditionalStoredMaps) { 3998 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3999 Diag(diag::err_module_different_modmap) 4000 << F.ModuleName << /*not new*/1 << ModMap->getName(); 4001 return OutOfDate; 4002 } 4003 } 4004 4005 if (Listener) 4006 Listener->ReadModuleMapFile(F.ModuleMapPath); 4007 return Success; 4008 } 4009 4010 /// Move the given method to the back of the global list of methods. 4011 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) { 4012 // Find the entry for this selector in the method pool. 4013 Sema::GlobalMethodPool::iterator Known 4014 = S.MethodPool.find(Method->getSelector()); 4015 if (Known == S.MethodPool.end()) 4016 return; 4017 4018 // Retrieve the appropriate method list. 4019 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first 4020 : Known->second.second; 4021 bool Found = false; 4022 for (ObjCMethodList *List = &Start; List; List = List->getNext()) { 4023 if (!Found) { 4024 if (List->getMethod() == Method) { 4025 Found = true; 4026 } else { 4027 // Keep searching. 4028 continue; 4029 } 4030 } 4031 4032 if (List->getNext()) 4033 List->setMethod(List->getNext()->getMethod()); 4034 else 4035 List->setMethod(Method); 4036 } 4037 } 4038 4039 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) { 4040 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?"); 4041 for (Decl *D : Names) { 4042 bool wasHidden = !D->isUnconditionallyVisible(); 4043 D->setVisibleDespiteOwningModule(); 4044 4045 if (wasHidden && SemaObj) { 4046 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) { 4047 moveMethodToBackOfGlobalList(*SemaObj, Method); 4048 } 4049 } 4050 } 4051 } 4052 4053 void ASTReader::makeModuleVisible(Module *Mod, 4054 Module::NameVisibilityKind NameVisibility, 4055 SourceLocation ImportLoc) { 4056 llvm::SmallPtrSet<Module *, 4> Visited; 4057 SmallVector<Module *, 4> Stack; 4058 Stack.push_back(Mod); 4059 while (!Stack.empty()) { 4060 Mod = Stack.pop_back_val(); 4061 4062 if (NameVisibility <= Mod->NameVisibility) { 4063 // This module already has this level of visibility (or greater), so 4064 // there is nothing more to do. 4065 continue; 4066 } 4067 4068 if (Mod->isUnimportable()) { 4069 // Modules that aren't importable cannot be made visible. 4070 continue; 4071 } 4072 4073 // Update the module's name visibility. 4074 Mod->NameVisibility = NameVisibility; 4075 4076 // If we've already deserialized any names from this module, 4077 // mark them as visible. 4078 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod); 4079 if (Hidden != HiddenNamesMap.end()) { 4080 auto HiddenNames = std::move(*Hidden); 4081 HiddenNamesMap.erase(Hidden); 4082 makeNamesVisible(HiddenNames.second, HiddenNames.first); 4083 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() && 4084 "making names visible added hidden names"); 4085 } 4086 4087 // Push any exported modules onto the stack to be marked as visible. 4088 SmallVector<Module *, 16> Exports; 4089 Mod->getExportedModules(Exports); 4090 for (SmallVectorImpl<Module *>::iterator 4091 I = Exports.begin(), E = Exports.end(); I != E; ++I) { 4092 Module *Exported = *I; 4093 if (Visited.insert(Exported).second) 4094 Stack.push_back(Exported); 4095 } 4096 } 4097 } 4098 4099 /// We've merged the definition \p MergedDef into the existing definition 4100 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made 4101 /// visible. 4102 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def, 4103 NamedDecl *MergedDef) { 4104 if (!Def->isUnconditionallyVisible()) { 4105 // If MergedDef is visible or becomes visible, make the definition visible. 4106 if (MergedDef->isUnconditionallyVisible()) 4107 Def->setVisibleDespiteOwningModule(); 4108 else { 4109 getContext().mergeDefinitionIntoModule( 4110 Def, MergedDef->getImportedOwningModule(), 4111 /*NotifyListeners*/ false); 4112 PendingMergedDefinitionsToDeduplicate.insert(Def); 4113 } 4114 } 4115 } 4116 4117 bool ASTReader::loadGlobalIndex() { 4118 if (GlobalIndex) 4119 return false; 4120 4121 if (TriedLoadingGlobalIndex || !UseGlobalIndex || 4122 !PP.getLangOpts().Modules) 4123 return true; 4124 4125 // Try to load the global index. 4126 TriedLoadingGlobalIndex = true; 4127 StringRef ModuleCachePath 4128 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath(); 4129 std::pair<GlobalModuleIndex *, llvm::Error> Result = 4130 GlobalModuleIndex::readIndex(ModuleCachePath); 4131 if (llvm::Error Err = std::move(Result.second)) { 4132 assert(!Result.first); 4133 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 4134 return true; 4135 } 4136 4137 GlobalIndex.reset(Result.first); 4138 ModuleMgr.setGlobalIndex(GlobalIndex.get()); 4139 return false; 4140 } 4141 4142 bool ASTReader::isGlobalIndexUnavailable() const { 4143 return PP.getLangOpts().Modules && UseGlobalIndex && 4144 !hasGlobalIndex() && TriedLoadingGlobalIndex; 4145 } 4146 4147 static void updateModuleTimestamp(ModuleFile &MF) { 4148 // Overwrite the timestamp file contents so that file's mtime changes. 4149 std::string TimestampFilename = MF.getTimestampFilename(); 4150 std::error_code EC; 4151 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text); 4152 if (EC) 4153 return; 4154 OS << "Timestamp file\n"; 4155 OS.close(); 4156 OS.clear_error(); // Avoid triggering a fatal error. 4157 } 4158 4159 /// Given a cursor at the start of an AST file, scan ahead and drop the 4160 /// cursor into the start of the given block ID, returning false on success and 4161 /// true on failure. 4162 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) { 4163 while (true) { 4164 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 4165 if (!MaybeEntry) { 4166 // FIXME this drops errors on the floor. 4167 consumeError(MaybeEntry.takeError()); 4168 return true; 4169 } 4170 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4171 4172 switch (Entry.Kind) { 4173 case llvm::BitstreamEntry::Error: 4174 case llvm::BitstreamEntry::EndBlock: 4175 return true; 4176 4177 case llvm::BitstreamEntry::Record: 4178 // Ignore top-level records. 4179 if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID)) 4180 break; 4181 else { 4182 // FIXME this drops errors on the floor. 4183 consumeError(Skipped.takeError()); 4184 return true; 4185 } 4186 4187 case llvm::BitstreamEntry::SubBlock: 4188 if (Entry.ID == BlockID) { 4189 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) { 4190 // FIXME this drops the error on the floor. 4191 consumeError(std::move(Err)); 4192 return true; 4193 } 4194 // Found it! 4195 return false; 4196 } 4197 4198 if (llvm::Error Err = Cursor.SkipBlock()) { 4199 // FIXME this drops the error on the floor. 4200 consumeError(std::move(Err)); 4201 return true; 4202 } 4203 } 4204 } 4205 } 4206 4207 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, 4208 ModuleKind Type, 4209 SourceLocation ImportLoc, 4210 unsigned ClientLoadCapabilities, 4211 SmallVectorImpl<ImportedSubmodule> *Imported) { 4212 llvm::SaveAndRestore<SourceLocation> 4213 SetCurImportLocRAII(CurrentImportLoc, ImportLoc); 4214 4215 // Defer any pending actions until we get to the end of reading the AST file. 4216 Deserializing AnASTFile(this); 4217 4218 // Bump the generation number. 4219 unsigned PreviousGeneration = 0; 4220 if (ContextObj) 4221 PreviousGeneration = incrementGeneration(*ContextObj); 4222 4223 unsigned NumModules = ModuleMgr.size(); 4224 auto removeModulesAndReturn = [&](ASTReadResult ReadResult) { 4225 assert(ReadResult && "expected to return error"); 4226 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, 4227 PP.getLangOpts().Modules 4228 ? &PP.getHeaderSearchInfo().getModuleMap() 4229 : nullptr); 4230 4231 // If we find that any modules are unusable, the global index is going 4232 // to be out-of-date. Just remove it. 4233 GlobalIndex.reset(); 4234 ModuleMgr.setGlobalIndex(nullptr); 4235 return ReadResult; 4236 }; 4237 4238 SmallVector<ImportedModule, 4> Loaded; 4239 switch (ASTReadResult ReadResult = 4240 ReadASTCore(FileName, Type, ImportLoc, 4241 /*ImportedBy=*/nullptr, Loaded, 0, 0, 4242 ASTFileSignature(), ClientLoadCapabilities)) { 4243 case Failure: 4244 case Missing: 4245 case OutOfDate: 4246 case VersionMismatch: 4247 case ConfigurationMismatch: 4248 case HadErrors: 4249 return removeModulesAndReturn(ReadResult); 4250 case Success: 4251 break; 4252 } 4253 4254 // Here comes stuff that we only do once the entire chain is loaded. 4255 4256 // Load the AST blocks of all of the modules that we loaded. We can still 4257 // hit errors parsing the ASTs at this point. 4258 for (ImportedModule &M : Loaded) { 4259 ModuleFile &F = *M.Mod; 4260 4261 // Read the AST block. 4262 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities)) 4263 return removeModulesAndReturn(Result); 4264 4265 // The AST block should always have a definition for the main module. 4266 if (F.isModule() && !F.DidReadTopLevelSubmodule) { 4267 Error(diag::err_module_file_missing_top_level_submodule, F.FileName); 4268 return removeModulesAndReturn(Failure); 4269 } 4270 4271 // Read the extension blocks. 4272 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) { 4273 if (ASTReadResult Result = ReadExtensionBlock(F)) 4274 return removeModulesAndReturn(Result); 4275 } 4276 4277 // Once read, set the ModuleFile bit base offset and update the size in 4278 // bits of all files we've seen. 4279 F.GlobalBitOffset = TotalModulesSizeInBits; 4280 TotalModulesSizeInBits += F.SizeInBits; 4281 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F)); 4282 } 4283 4284 // Preload source locations and interesting indentifiers. 4285 for (ImportedModule &M : Loaded) { 4286 ModuleFile &F = *M.Mod; 4287 4288 // Preload SLocEntries. 4289 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) { 4290 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID; 4291 // Load it through the SourceManager and don't call ReadSLocEntry() 4292 // directly because the entry may have already been loaded in which case 4293 // calling ReadSLocEntry() directly would trigger an assertion in 4294 // SourceManager. 4295 SourceMgr.getLoadedSLocEntryByID(Index); 4296 } 4297 4298 // Map the original source file ID into the ID space of the current 4299 // compilation. 4300 if (F.OriginalSourceFileID.isValid()) { 4301 F.OriginalSourceFileID = FileID::get( 4302 F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1); 4303 } 4304 4305 // Preload all the pending interesting identifiers by marking them out of 4306 // date. 4307 for (auto Offset : F.PreloadIdentifierOffsets) { 4308 const unsigned char *Data = reinterpret_cast<const unsigned char *>( 4309 F.IdentifierTableData + Offset); 4310 4311 ASTIdentifierLookupTrait Trait(*this, F); 4312 auto KeyDataLen = Trait.ReadKeyDataLength(Data); 4313 auto Key = Trait.ReadKey(Data, KeyDataLen.first); 4314 auto &II = PP.getIdentifierTable().getOwn(Key); 4315 II.setOutOfDate(true); 4316 4317 // Mark this identifier as being from an AST file so that we can track 4318 // whether we need to serialize it. 4319 markIdentifierFromAST(*this, II); 4320 4321 // Associate the ID with the identifier so that the writer can reuse it. 4322 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first); 4323 SetIdentifierInfo(ID, &II); 4324 } 4325 } 4326 4327 // Setup the import locations and notify the module manager that we've 4328 // committed to these module files. 4329 for (ImportedModule &M : Loaded) { 4330 ModuleFile &F = *M.Mod; 4331 4332 ModuleMgr.moduleFileAccepted(&F); 4333 4334 // Set the import location. 4335 F.DirectImportLoc = ImportLoc; 4336 // FIXME: We assume that locations from PCH / preamble do not need 4337 // any translation. 4338 if (!M.ImportedBy) 4339 F.ImportLoc = M.ImportLoc; 4340 else 4341 F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc); 4342 } 4343 4344 if (!PP.getLangOpts().CPlusPlus || 4345 (Type != MK_ImplicitModule && Type != MK_ExplicitModule && 4346 Type != MK_PrebuiltModule)) { 4347 // Mark all of the identifiers in the identifier table as being out of date, 4348 // so that various accessors know to check the loaded modules when the 4349 // identifier is used. 4350 // 4351 // For C++ modules, we don't need information on many identifiers (just 4352 // those that provide macros or are poisoned), so we mark all of 4353 // the interesting ones via PreloadIdentifierOffsets. 4354 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(), 4355 IdEnd = PP.getIdentifierTable().end(); 4356 Id != IdEnd; ++Id) 4357 Id->second->setOutOfDate(true); 4358 } 4359 // Mark selectors as out of date. 4360 for (auto Sel : SelectorGeneration) 4361 SelectorOutOfDate[Sel.first] = true; 4362 4363 // Resolve any unresolved module exports. 4364 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) { 4365 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I]; 4366 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID); 4367 Module *ResolvedMod = getSubmodule(GlobalID); 4368 4369 switch (Unresolved.Kind) { 4370 case UnresolvedModuleRef::Conflict: 4371 if (ResolvedMod) { 4372 Module::Conflict Conflict; 4373 Conflict.Other = ResolvedMod; 4374 Conflict.Message = Unresolved.String.str(); 4375 Unresolved.Mod->Conflicts.push_back(Conflict); 4376 } 4377 continue; 4378 4379 case UnresolvedModuleRef::Import: 4380 if (ResolvedMod) 4381 Unresolved.Mod->Imports.insert(ResolvedMod); 4382 continue; 4383 4384 case UnresolvedModuleRef::Export: 4385 if (ResolvedMod || Unresolved.IsWildcard) 4386 Unresolved.Mod->Exports.push_back( 4387 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard)); 4388 continue; 4389 } 4390 } 4391 UnresolvedModuleRefs.clear(); 4392 4393 if (Imported) 4394 Imported->append(ImportedModules.begin(), 4395 ImportedModules.end()); 4396 4397 // FIXME: How do we load the 'use'd modules? They may not be submodules. 4398 // Might be unnecessary as use declarations are only used to build the 4399 // module itself. 4400 4401 if (ContextObj) 4402 InitializeContext(); 4403 4404 if (SemaObj) 4405 UpdateSema(); 4406 4407 if (DeserializationListener) 4408 DeserializationListener->ReaderInitialized(this); 4409 4410 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule(); 4411 if (PrimaryModule.OriginalSourceFileID.isValid()) { 4412 // If this AST file is a precompiled preamble, then set the 4413 // preamble file ID of the source manager to the file source file 4414 // from which the preamble was built. 4415 if (Type == MK_Preamble) { 4416 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID); 4417 } else if (Type == MK_MainFile) { 4418 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID); 4419 } 4420 } 4421 4422 // For any Objective-C class definitions we have already loaded, make sure 4423 // that we load any additional categories. 4424 if (ContextObj) { 4425 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) { 4426 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(), 4427 ObjCClassesLoaded[I], 4428 PreviousGeneration); 4429 } 4430 } 4431 4432 if (PP.getHeaderSearchInfo() 4433 .getHeaderSearchOpts() 4434 .ModulesValidateOncePerBuildSession) { 4435 // Now we are certain that the module and all modules it depends on are 4436 // up to date. Create or update timestamp files for modules that are 4437 // located in the module cache (not for PCH files that could be anywhere 4438 // in the filesystem). 4439 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) { 4440 ImportedModule &M = Loaded[I]; 4441 if (M.Mod->Kind == MK_ImplicitModule) { 4442 updateModuleTimestamp(*M.Mod); 4443 } 4444 } 4445 } 4446 4447 return Success; 4448 } 4449 4450 static ASTFileSignature readASTFileSignature(StringRef PCH); 4451 4452 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'. 4453 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) { 4454 // FIXME checking magic headers is done in other places such as 4455 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't 4456 // always done the same. Unify it all with a helper. 4457 if (!Stream.canSkipToPos(4)) 4458 return llvm::createStringError(std::errc::illegal_byte_sequence, 4459 "file too small to contain AST file magic"); 4460 for (unsigned C : {'C', 'P', 'C', 'H'}) 4461 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) { 4462 if (Res.get() != C) 4463 return llvm::createStringError( 4464 std::errc::illegal_byte_sequence, 4465 "file doesn't start with AST file magic"); 4466 } else 4467 return Res.takeError(); 4468 return llvm::Error::success(); 4469 } 4470 4471 static unsigned moduleKindForDiagnostic(ModuleKind Kind) { 4472 switch (Kind) { 4473 case MK_PCH: 4474 return 0; // PCH 4475 case MK_ImplicitModule: 4476 case MK_ExplicitModule: 4477 case MK_PrebuiltModule: 4478 return 1; // module 4479 case MK_MainFile: 4480 case MK_Preamble: 4481 return 2; // main source file 4482 } 4483 llvm_unreachable("unknown module kind"); 4484 } 4485 4486 ASTReader::ASTReadResult 4487 ASTReader::ReadASTCore(StringRef FileName, 4488 ModuleKind Type, 4489 SourceLocation ImportLoc, 4490 ModuleFile *ImportedBy, 4491 SmallVectorImpl<ImportedModule> &Loaded, 4492 off_t ExpectedSize, time_t ExpectedModTime, 4493 ASTFileSignature ExpectedSignature, 4494 unsigned ClientLoadCapabilities) { 4495 ModuleFile *M; 4496 std::string ErrorStr; 4497 ModuleManager::AddModuleResult AddResult 4498 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy, 4499 getGeneration(), ExpectedSize, ExpectedModTime, 4500 ExpectedSignature, readASTFileSignature, 4501 M, ErrorStr); 4502 4503 switch (AddResult) { 4504 case ModuleManager::AlreadyLoaded: 4505 Diag(diag::remark_module_import) 4506 << M->ModuleName << M->FileName << (ImportedBy ? true : false) 4507 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 4508 return Success; 4509 4510 case ModuleManager::NewlyLoaded: 4511 // Load module file below. 4512 break; 4513 4514 case ModuleManager::Missing: 4515 // The module file was missing; if the client can handle that, return 4516 // it. 4517 if (ClientLoadCapabilities & ARR_Missing) 4518 return Missing; 4519 4520 // Otherwise, return an error. 4521 Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type) 4522 << FileName << !ErrorStr.empty() 4523 << ErrorStr; 4524 return Failure; 4525 4526 case ModuleManager::OutOfDate: 4527 // We couldn't load the module file because it is out-of-date. If the 4528 // client can handle out-of-date, return it. 4529 if (ClientLoadCapabilities & ARR_OutOfDate) 4530 return OutOfDate; 4531 4532 // Otherwise, return an error. 4533 Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type) 4534 << FileName << !ErrorStr.empty() 4535 << ErrorStr; 4536 return Failure; 4537 } 4538 4539 assert(M && "Missing module file"); 4540 4541 bool ShouldFinalizePCM = false; 4542 auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() { 4543 auto &MC = getModuleManager().getModuleCache(); 4544 if (ShouldFinalizePCM) 4545 MC.finalizePCM(FileName); 4546 else 4547 MC.tryToDropPCM(FileName); 4548 }); 4549 ModuleFile &F = *M; 4550 BitstreamCursor &Stream = F.Stream; 4551 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer)); 4552 F.SizeInBits = F.Buffer->getBufferSize() * 8; 4553 4554 // Sniff for the signature. 4555 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4556 Diag(diag::err_module_file_invalid) 4557 << moduleKindForDiagnostic(Type) << FileName << std::move(Err); 4558 return Failure; 4559 } 4560 4561 // This is used for compatibility with older PCH formats. 4562 bool HaveReadControlBlock = false; 4563 while (true) { 4564 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4565 if (!MaybeEntry) { 4566 Error(MaybeEntry.takeError()); 4567 return Failure; 4568 } 4569 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4570 4571 switch (Entry.Kind) { 4572 case llvm::BitstreamEntry::Error: 4573 case llvm::BitstreamEntry::Record: 4574 case llvm::BitstreamEntry::EndBlock: 4575 Error("invalid record at top-level of AST file"); 4576 return Failure; 4577 4578 case llvm::BitstreamEntry::SubBlock: 4579 break; 4580 } 4581 4582 switch (Entry.ID) { 4583 case CONTROL_BLOCK_ID: 4584 HaveReadControlBlock = true; 4585 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) { 4586 case Success: 4587 // Check that we didn't try to load a non-module AST file as a module. 4588 // 4589 // FIXME: Should we also perform the converse check? Loading a module as 4590 // a PCH file sort of works, but it's a bit wonky. 4591 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule || 4592 Type == MK_PrebuiltModule) && 4593 F.ModuleName.empty()) { 4594 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure; 4595 if (Result != OutOfDate || 4596 (ClientLoadCapabilities & ARR_OutOfDate) == 0) 4597 Diag(diag::err_module_file_not_module) << FileName; 4598 return Result; 4599 } 4600 break; 4601 4602 case Failure: return Failure; 4603 case Missing: return Missing; 4604 case OutOfDate: return OutOfDate; 4605 case VersionMismatch: return VersionMismatch; 4606 case ConfigurationMismatch: return ConfigurationMismatch; 4607 case HadErrors: return HadErrors; 4608 } 4609 break; 4610 4611 case AST_BLOCK_ID: 4612 if (!HaveReadControlBlock) { 4613 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 4614 Diag(diag::err_pch_version_too_old); 4615 return VersionMismatch; 4616 } 4617 4618 // Record that we've loaded this module. 4619 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc)); 4620 ShouldFinalizePCM = true; 4621 return Success; 4622 4623 case UNHASHED_CONTROL_BLOCK_ID: 4624 // This block is handled using look-ahead during ReadControlBlock. We 4625 // shouldn't get here! 4626 Error("malformed block record in AST file"); 4627 return Failure; 4628 4629 default: 4630 if (llvm::Error Err = Stream.SkipBlock()) { 4631 Error(std::move(Err)); 4632 return Failure; 4633 } 4634 break; 4635 } 4636 } 4637 4638 llvm_unreachable("unexpected break; expected return"); 4639 } 4640 4641 ASTReader::ASTReadResult 4642 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy, 4643 unsigned ClientLoadCapabilities) { 4644 const HeaderSearchOptions &HSOpts = 4645 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 4646 bool AllowCompatibleConfigurationMismatch = 4647 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 4648 4649 ASTReadResult Result = readUnhashedControlBlockImpl( 4650 &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch, 4651 Listener.get(), 4652 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions); 4653 4654 // If F was directly imported by another module, it's implicitly validated by 4655 // the importing module. 4656 if (DisableValidation || WasImportedBy || 4657 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 4658 return Success; 4659 4660 if (Result == Failure) { 4661 Error("malformed block record in AST file"); 4662 return Failure; 4663 } 4664 4665 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) { 4666 // If this module has already been finalized in the ModuleCache, we're stuck 4667 // with it; we can only load a single version of each module. 4668 // 4669 // This can happen when a module is imported in two contexts: in one, as a 4670 // user module; in another, as a system module (due to an import from 4671 // another module marked with the [system] flag). It usually indicates a 4672 // bug in the module map: this module should also be marked with [system]. 4673 // 4674 // If -Wno-system-headers (the default), and the first import is as a 4675 // system module, then validation will fail during the as-user import, 4676 // since -Werror flags won't have been validated. However, it's reasonable 4677 // to treat this consistently as a system module. 4678 // 4679 // If -Wsystem-headers, the PCM on disk was built with 4680 // -Wno-system-headers, and the first import is as a user module, then 4681 // validation will fail during the as-system import since the PCM on disk 4682 // doesn't guarantee that -Werror was respected. However, the -Werror 4683 // flags were checked during the initial as-user import. 4684 if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) { 4685 Diag(diag::warn_module_system_bit_conflict) << F.FileName; 4686 return Success; 4687 } 4688 } 4689 4690 return Result; 4691 } 4692 4693 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl( 4694 ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities, 4695 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener, 4696 bool ValidateDiagnosticOptions) { 4697 // Initialize a stream. 4698 BitstreamCursor Stream(StreamData); 4699 4700 // Sniff for the signature. 4701 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4702 // FIXME this drops the error on the floor. 4703 consumeError(std::move(Err)); 4704 return Failure; 4705 } 4706 4707 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 4708 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 4709 return Failure; 4710 4711 // Read all of the records in the options block. 4712 RecordData Record; 4713 ASTReadResult Result = Success; 4714 while (true) { 4715 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4716 if (!MaybeEntry) { 4717 // FIXME this drops the error on the floor. 4718 consumeError(MaybeEntry.takeError()); 4719 return Failure; 4720 } 4721 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4722 4723 switch (Entry.Kind) { 4724 case llvm::BitstreamEntry::Error: 4725 case llvm::BitstreamEntry::SubBlock: 4726 return Failure; 4727 4728 case llvm::BitstreamEntry::EndBlock: 4729 return Result; 4730 4731 case llvm::BitstreamEntry::Record: 4732 // The interesting case. 4733 break; 4734 } 4735 4736 // Read and process a record. 4737 Record.clear(); 4738 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record); 4739 if (!MaybeRecordType) { 4740 // FIXME this drops the error. 4741 return Failure; 4742 } 4743 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) { 4744 case SIGNATURE: 4745 if (F) 4746 F->Signature = ASTFileSignature::create(Record.begin(), Record.end()); 4747 break; 4748 case AST_BLOCK_HASH: 4749 if (F) 4750 F->ASTBlockHash = 4751 ASTFileSignature::create(Record.begin(), Record.end()); 4752 break; 4753 case DIAGNOSTIC_OPTIONS: { 4754 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 4755 if (Listener && ValidateDiagnosticOptions && 4756 !AllowCompatibleConfigurationMismatch && 4757 ParseDiagnosticOptions(Record, Complain, *Listener)) 4758 Result = OutOfDate; // Don't return early. Read the signature. 4759 break; 4760 } 4761 case DIAG_PRAGMA_MAPPINGS: 4762 if (!F) 4763 break; 4764 if (F->PragmaDiagMappings.empty()) 4765 F->PragmaDiagMappings.swap(Record); 4766 else 4767 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(), 4768 Record.begin(), Record.end()); 4769 break; 4770 } 4771 } 4772 } 4773 4774 /// Parse a record and blob containing module file extension metadata. 4775 static bool parseModuleFileExtensionMetadata( 4776 const SmallVectorImpl<uint64_t> &Record, 4777 StringRef Blob, 4778 ModuleFileExtensionMetadata &Metadata) { 4779 if (Record.size() < 4) return true; 4780 4781 Metadata.MajorVersion = Record[0]; 4782 Metadata.MinorVersion = Record[1]; 4783 4784 unsigned BlockNameLen = Record[2]; 4785 unsigned UserInfoLen = Record[3]; 4786 4787 if (BlockNameLen + UserInfoLen > Blob.size()) return true; 4788 4789 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen); 4790 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen, 4791 Blob.data() + BlockNameLen + UserInfoLen); 4792 return false; 4793 } 4794 4795 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) { 4796 BitstreamCursor &Stream = F.Stream; 4797 4798 RecordData Record; 4799 while (true) { 4800 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4801 if (!MaybeEntry) { 4802 Error(MaybeEntry.takeError()); 4803 return Failure; 4804 } 4805 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4806 4807 switch (Entry.Kind) { 4808 case llvm::BitstreamEntry::SubBlock: 4809 if (llvm::Error Err = Stream.SkipBlock()) { 4810 Error(std::move(Err)); 4811 return Failure; 4812 } 4813 continue; 4814 4815 case llvm::BitstreamEntry::EndBlock: 4816 return Success; 4817 4818 case llvm::BitstreamEntry::Error: 4819 return HadErrors; 4820 4821 case llvm::BitstreamEntry::Record: 4822 break; 4823 } 4824 4825 Record.clear(); 4826 StringRef Blob; 4827 Expected<unsigned> MaybeRecCode = 4828 Stream.readRecord(Entry.ID, Record, &Blob); 4829 if (!MaybeRecCode) { 4830 Error(MaybeRecCode.takeError()); 4831 return Failure; 4832 } 4833 switch (MaybeRecCode.get()) { 4834 case EXTENSION_METADATA: { 4835 ModuleFileExtensionMetadata Metadata; 4836 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) { 4837 Error("malformed EXTENSION_METADATA in AST file"); 4838 return Failure; 4839 } 4840 4841 // Find a module file extension with this block name. 4842 auto Known = ModuleFileExtensions.find(Metadata.BlockName); 4843 if (Known == ModuleFileExtensions.end()) break; 4844 4845 // Form a reader. 4846 if (auto Reader = Known->second->createExtensionReader(Metadata, *this, 4847 F, Stream)) { 4848 F.ExtensionReaders.push_back(std::move(Reader)); 4849 } 4850 4851 break; 4852 } 4853 } 4854 } 4855 4856 return Success; 4857 } 4858 4859 void ASTReader::InitializeContext() { 4860 assert(ContextObj && "no context to initialize"); 4861 ASTContext &Context = *ContextObj; 4862 4863 // If there's a listener, notify them that we "read" the translation unit. 4864 if (DeserializationListener) 4865 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, 4866 Context.getTranslationUnitDecl()); 4867 4868 // FIXME: Find a better way to deal with collisions between these 4869 // built-in types. Right now, we just ignore the problem. 4870 4871 // Load the special types. 4872 if (SpecialTypes.size() >= NumSpecialTypeIDs) { 4873 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) { 4874 if (!Context.CFConstantStringTypeDecl) 4875 Context.setCFConstantStringType(GetType(String)); 4876 } 4877 4878 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) { 4879 QualType FileType = GetType(File); 4880 if (FileType.isNull()) { 4881 Error("FILE type is NULL"); 4882 return; 4883 } 4884 4885 if (!Context.FILEDecl) { 4886 if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) 4887 Context.setFILEDecl(Typedef->getDecl()); 4888 else { 4889 const TagType *Tag = FileType->getAs<TagType>(); 4890 if (!Tag) { 4891 Error("Invalid FILE type in AST file"); 4892 return; 4893 } 4894 Context.setFILEDecl(Tag->getDecl()); 4895 } 4896 } 4897 } 4898 4899 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) { 4900 QualType Jmp_bufType = GetType(Jmp_buf); 4901 if (Jmp_bufType.isNull()) { 4902 Error("jmp_buf type is NULL"); 4903 return; 4904 } 4905 4906 if (!Context.jmp_bufDecl) { 4907 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) 4908 Context.setjmp_bufDecl(Typedef->getDecl()); 4909 else { 4910 const TagType *Tag = Jmp_bufType->getAs<TagType>(); 4911 if (!Tag) { 4912 Error("Invalid jmp_buf type in AST file"); 4913 return; 4914 } 4915 Context.setjmp_bufDecl(Tag->getDecl()); 4916 } 4917 } 4918 } 4919 4920 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) { 4921 QualType Sigjmp_bufType = GetType(Sigjmp_buf); 4922 if (Sigjmp_bufType.isNull()) { 4923 Error("sigjmp_buf type is NULL"); 4924 return; 4925 } 4926 4927 if (!Context.sigjmp_bufDecl) { 4928 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) 4929 Context.setsigjmp_bufDecl(Typedef->getDecl()); 4930 else { 4931 const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); 4932 assert(Tag && "Invalid sigjmp_buf type in AST file"); 4933 Context.setsigjmp_bufDecl(Tag->getDecl()); 4934 } 4935 } 4936 } 4937 4938 if (unsigned ObjCIdRedef 4939 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) { 4940 if (Context.ObjCIdRedefinitionType.isNull()) 4941 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef); 4942 } 4943 4944 if (unsigned ObjCClassRedef 4945 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) { 4946 if (Context.ObjCClassRedefinitionType.isNull()) 4947 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef); 4948 } 4949 4950 if (unsigned ObjCSelRedef 4951 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) { 4952 if (Context.ObjCSelRedefinitionType.isNull()) 4953 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef); 4954 } 4955 4956 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) { 4957 QualType Ucontext_tType = GetType(Ucontext_t); 4958 if (Ucontext_tType.isNull()) { 4959 Error("ucontext_t type is NULL"); 4960 return; 4961 } 4962 4963 if (!Context.ucontext_tDecl) { 4964 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>()) 4965 Context.setucontext_tDecl(Typedef->getDecl()); 4966 else { 4967 const TagType *Tag = Ucontext_tType->getAs<TagType>(); 4968 assert(Tag && "Invalid ucontext_t type in AST file"); 4969 Context.setucontext_tDecl(Tag->getDecl()); 4970 } 4971 } 4972 } 4973 } 4974 4975 ReadPragmaDiagnosticMappings(Context.getDiagnostics()); 4976 4977 // If there were any CUDA special declarations, deserialize them. 4978 if (!CUDASpecialDeclRefs.empty()) { 4979 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!"); 4980 Context.setcudaConfigureCallDecl( 4981 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0]))); 4982 } 4983 4984 // Re-export any modules that were imported by a non-module AST file. 4985 // FIXME: This does not make macro-only imports visible again. 4986 for (auto &Import : ImportedModules) { 4987 if (Module *Imported = getSubmodule(Import.ID)) { 4988 makeModuleVisible(Imported, Module::AllVisible, 4989 /*ImportLoc=*/Import.ImportLoc); 4990 if (Import.ImportLoc.isValid()) 4991 PP.makeModuleVisible(Imported, Import.ImportLoc); 4992 // This updates visibility for Preprocessor only. For Sema, which can be 4993 // nullptr here, we do the same later, in UpdateSema(). 4994 } 4995 } 4996 } 4997 4998 void ASTReader::finalizeForWriting() { 4999 // Nothing to do for now. 5000 } 5001 5002 /// Reads and return the signature record from \p PCH's control block, or 5003 /// else returns 0. 5004 static ASTFileSignature readASTFileSignature(StringRef PCH) { 5005 BitstreamCursor Stream(PCH); 5006 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5007 // FIXME this drops the error on the floor. 5008 consumeError(std::move(Err)); 5009 return ASTFileSignature(); 5010 } 5011 5012 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 5013 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 5014 return ASTFileSignature(); 5015 5016 // Scan for SIGNATURE inside the diagnostic options block. 5017 ASTReader::RecordData Record; 5018 while (true) { 5019 Expected<llvm::BitstreamEntry> MaybeEntry = 5020 Stream.advanceSkippingSubblocks(); 5021 if (!MaybeEntry) { 5022 // FIXME this drops the error on the floor. 5023 consumeError(MaybeEntry.takeError()); 5024 return ASTFileSignature(); 5025 } 5026 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5027 5028 if (Entry.Kind != llvm::BitstreamEntry::Record) 5029 return ASTFileSignature(); 5030 5031 Record.clear(); 5032 StringRef Blob; 5033 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 5034 if (!MaybeRecord) { 5035 // FIXME this drops the error on the floor. 5036 consumeError(MaybeRecord.takeError()); 5037 return ASTFileSignature(); 5038 } 5039 if (SIGNATURE == MaybeRecord.get()) 5040 return ASTFileSignature::create(Record.begin(), 5041 Record.begin() + ASTFileSignature::size); 5042 } 5043 } 5044 5045 /// Retrieve the name of the original source file name 5046 /// directly from the AST file, without actually loading the AST 5047 /// file. 5048 std::string ASTReader::getOriginalSourceFile( 5049 const std::string &ASTFileName, FileManager &FileMgr, 5050 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) { 5051 // Open the AST file. 5052 auto Buffer = FileMgr.getBufferForFile(ASTFileName); 5053 if (!Buffer) { 5054 Diags.Report(diag::err_fe_unable_to_read_pch_file) 5055 << ASTFileName << Buffer.getError().message(); 5056 return std::string(); 5057 } 5058 5059 // Initialize the stream 5060 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer)); 5061 5062 // Sniff for the signature. 5063 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5064 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err); 5065 return std::string(); 5066 } 5067 5068 // Scan for the CONTROL_BLOCK_ID block. 5069 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) { 5070 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 5071 return std::string(); 5072 } 5073 5074 // Scan for ORIGINAL_FILE inside the control block. 5075 RecordData Record; 5076 while (true) { 5077 Expected<llvm::BitstreamEntry> MaybeEntry = 5078 Stream.advanceSkippingSubblocks(); 5079 if (!MaybeEntry) { 5080 // FIXME this drops errors on the floor. 5081 consumeError(MaybeEntry.takeError()); 5082 return std::string(); 5083 } 5084 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5085 5086 if (Entry.Kind == llvm::BitstreamEntry::EndBlock) 5087 return std::string(); 5088 5089 if (Entry.Kind != llvm::BitstreamEntry::Record) { 5090 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 5091 return std::string(); 5092 } 5093 5094 Record.clear(); 5095 StringRef Blob; 5096 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 5097 if (!MaybeRecord) { 5098 // FIXME this drops the errors on the floor. 5099 consumeError(MaybeRecord.takeError()); 5100 return std::string(); 5101 } 5102 if (ORIGINAL_FILE == MaybeRecord.get()) 5103 return Blob.str(); 5104 } 5105 } 5106 5107 namespace { 5108 5109 class SimplePCHValidator : public ASTReaderListener { 5110 const LangOptions &ExistingLangOpts; 5111 const TargetOptions &ExistingTargetOpts; 5112 const PreprocessorOptions &ExistingPPOpts; 5113 std::string ExistingModuleCachePath; 5114 FileManager &FileMgr; 5115 5116 public: 5117 SimplePCHValidator(const LangOptions &ExistingLangOpts, 5118 const TargetOptions &ExistingTargetOpts, 5119 const PreprocessorOptions &ExistingPPOpts, 5120 StringRef ExistingModuleCachePath, FileManager &FileMgr) 5121 : ExistingLangOpts(ExistingLangOpts), 5122 ExistingTargetOpts(ExistingTargetOpts), 5123 ExistingPPOpts(ExistingPPOpts), 5124 ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {} 5125 5126 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain, 5127 bool AllowCompatibleDifferences) override { 5128 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr, 5129 AllowCompatibleDifferences); 5130 } 5131 5132 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain, 5133 bool AllowCompatibleDifferences) override { 5134 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr, 5135 AllowCompatibleDifferences); 5136 } 5137 5138 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 5139 StringRef SpecificModuleCachePath, 5140 bool Complain) override { 5141 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5142 ExistingModuleCachePath, 5143 nullptr, ExistingLangOpts); 5144 } 5145 5146 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 5147 bool Complain, 5148 std::string &SuggestedPredefines) override { 5149 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr, 5150 SuggestedPredefines, ExistingLangOpts); 5151 } 5152 }; 5153 5154 } // namespace 5155 5156 bool ASTReader::readASTFileControlBlock( 5157 StringRef Filename, FileManager &FileMgr, 5158 const PCHContainerReader &PCHContainerRdr, 5159 bool FindModuleFileExtensions, 5160 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) { 5161 // Open the AST file. 5162 // FIXME: This allows use of the VFS; we do not allow use of the 5163 // VFS when actually loading a module. 5164 auto Buffer = FileMgr.getBufferForFile(Filename); 5165 if (!Buffer) { 5166 return true; 5167 } 5168 5169 // Initialize the stream 5170 StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer); 5171 BitstreamCursor Stream(Bytes); 5172 5173 // Sniff for the signature. 5174 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5175 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 5176 return true; 5177 } 5178 5179 // Scan for the CONTROL_BLOCK_ID block. 5180 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) 5181 return true; 5182 5183 bool NeedsInputFiles = Listener.needsInputFileVisitation(); 5184 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation(); 5185 bool NeedsImports = Listener.needsImportVisitation(); 5186 BitstreamCursor InputFilesCursor; 5187 5188 RecordData Record; 5189 std::string ModuleDir; 5190 bool DoneWithControlBlock = false; 5191 while (!DoneWithControlBlock) { 5192 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5193 if (!MaybeEntry) { 5194 // FIXME this drops the error on the floor. 5195 consumeError(MaybeEntry.takeError()); 5196 return true; 5197 } 5198 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5199 5200 switch (Entry.Kind) { 5201 case llvm::BitstreamEntry::SubBlock: { 5202 switch (Entry.ID) { 5203 case OPTIONS_BLOCK_ID: { 5204 std::string IgnoredSuggestedPredefines; 5205 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate, 5206 /*AllowCompatibleConfigurationMismatch*/ false, 5207 Listener, IgnoredSuggestedPredefines) != Success) 5208 return true; 5209 break; 5210 } 5211 5212 case INPUT_FILES_BLOCK_ID: 5213 InputFilesCursor = Stream; 5214 if (llvm::Error Err = Stream.SkipBlock()) { 5215 // FIXME this drops the error on the floor. 5216 consumeError(std::move(Err)); 5217 return true; 5218 } 5219 if (NeedsInputFiles && 5220 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID)) 5221 return true; 5222 break; 5223 5224 default: 5225 if (llvm::Error Err = Stream.SkipBlock()) { 5226 // FIXME this drops the error on the floor. 5227 consumeError(std::move(Err)); 5228 return true; 5229 } 5230 break; 5231 } 5232 5233 continue; 5234 } 5235 5236 case llvm::BitstreamEntry::EndBlock: 5237 DoneWithControlBlock = true; 5238 break; 5239 5240 case llvm::BitstreamEntry::Error: 5241 return true; 5242 5243 case llvm::BitstreamEntry::Record: 5244 break; 5245 } 5246 5247 if (DoneWithControlBlock) break; 5248 5249 Record.clear(); 5250 StringRef Blob; 5251 Expected<unsigned> MaybeRecCode = 5252 Stream.readRecord(Entry.ID, Record, &Blob); 5253 if (!MaybeRecCode) { 5254 // FIXME this drops the error. 5255 return Failure; 5256 } 5257 switch ((ControlRecordTypes)MaybeRecCode.get()) { 5258 case METADATA: 5259 if (Record[0] != VERSION_MAJOR) 5260 return true; 5261 if (Listener.ReadFullVersionInformation(Blob)) 5262 return true; 5263 break; 5264 case MODULE_NAME: 5265 Listener.ReadModuleName(Blob); 5266 break; 5267 case MODULE_DIRECTORY: 5268 ModuleDir = std::string(Blob); 5269 break; 5270 case MODULE_MAP_FILE: { 5271 unsigned Idx = 0; 5272 auto Path = ReadString(Record, Idx); 5273 ResolveImportedPath(Path, ModuleDir); 5274 Listener.ReadModuleMapFile(Path); 5275 break; 5276 } 5277 case INPUT_FILE_OFFSETS: { 5278 if (!NeedsInputFiles) 5279 break; 5280 5281 unsigned NumInputFiles = Record[0]; 5282 unsigned NumUserFiles = Record[1]; 5283 const llvm::support::unaligned_uint64_t *InputFileOffs = 5284 (const llvm::support::unaligned_uint64_t *)Blob.data(); 5285 for (unsigned I = 0; I != NumInputFiles; ++I) { 5286 // Go find this input file. 5287 bool isSystemFile = I >= NumUserFiles; 5288 5289 if (isSystemFile && !NeedsSystemInputFiles) 5290 break; // the rest are system input files 5291 5292 BitstreamCursor &Cursor = InputFilesCursor; 5293 SavedStreamPosition SavedPosition(Cursor); 5294 if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) { 5295 // FIXME this drops errors on the floor. 5296 consumeError(std::move(Err)); 5297 } 5298 5299 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 5300 if (!MaybeCode) { 5301 // FIXME this drops errors on the floor. 5302 consumeError(MaybeCode.takeError()); 5303 } 5304 unsigned Code = MaybeCode.get(); 5305 5306 RecordData Record; 5307 StringRef Blob; 5308 bool shouldContinue = false; 5309 Expected<unsigned> MaybeRecordType = 5310 Cursor.readRecord(Code, Record, &Blob); 5311 if (!MaybeRecordType) { 5312 // FIXME this drops errors on the floor. 5313 consumeError(MaybeRecordType.takeError()); 5314 } 5315 switch ((InputFileRecordTypes)MaybeRecordType.get()) { 5316 case INPUT_FILE_HASH: 5317 break; 5318 case INPUT_FILE: 5319 bool Overridden = static_cast<bool>(Record[3]); 5320 std::string Filename = std::string(Blob); 5321 ResolveImportedPath(Filename, ModuleDir); 5322 shouldContinue = Listener.visitInputFile( 5323 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false); 5324 break; 5325 } 5326 if (!shouldContinue) 5327 break; 5328 } 5329 break; 5330 } 5331 5332 case IMPORTS: { 5333 if (!NeedsImports) 5334 break; 5335 5336 unsigned Idx = 0, N = Record.size(); 5337 while (Idx < N) { 5338 // Read information about the AST file. 5339 Idx += 5340 1 + 1 + 1 + 1 + 5341 ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature 5342 std::string ModuleName = ReadString(Record, Idx); 5343 std::string Filename = ReadString(Record, Idx); 5344 ResolveImportedPath(Filename, ModuleDir); 5345 Listener.visitImport(ModuleName, Filename); 5346 } 5347 break; 5348 } 5349 5350 default: 5351 // No other validation to perform. 5352 break; 5353 } 5354 } 5355 5356 // Look for module file extension blocks, if requested. 5357 if (FindModuleFileExtensions) { 5358 BitstreamCursor SavedStream = Stream; 5359 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) { 5360 bool DoneWithExtensionBlock = false; 5361 while (!DoneWithExtensionBlock) { 5362 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5363 if (!MaybeEntry) { 5364 // FIXME this drops the error. 5365 return true; 5366 } 5367 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5368 5369 switch (Entry.Kind) { 5370 case llvm::BitstreamEntry::SubBlock: 5371 if (llvm::Error Err = Stream.SkipBlock()) { 5372 // FIXME this drops the error on the floor. 5373 consumeError(std::move(Err)); 5374 return true; 5375 } 5376 continue; 5377 5378 case llvm::BitstreamEntry::EndBlock: 5379 DoneWithExtensionBlock = true; 5380 continue; 5381 5382 case llvm::BitstreamEntry::Error: 5383 return true; 5384 5385 case llvm::BitstreamEntry::Record: 5386 break; 5387 } 5388 5389 Record.clear(); 5390 StringRef Blob; 5391 Expected<unsigned> MaybeRecCode = 5392 Stream.readRecord(Entry.ID, Record, &Blob); 5393 if (!MaybeRecCode) { 5394 // FIXME this drops the error. 5395 return true; 5396 } 5397 switch (MaybeRecCode.get()) { 5398 case EXTENSION_METADATA: { 5399 ModuleFileExtensionMetadata Metadata; 5400 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) 5401 return true; 5402 5403 Listener.readModuleFileExtension(Metadata); 5404 break; 5405 } 5406 } 5407 } 5408 } 5409 Stream = SavedStream; 5410 } 5411 5412 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 5413 if (readUnhashedControlBlockImpl( 5414 nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate, 5415 /*AllowCompatibleConfigurationMismatch*/ false, &Listener, 5416 ValidateDiagnosticOptions) != Success) 5417 return true; 5418 5419 return false; 5420 } 5421 5422 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr, 5423 const PCHContainerReader &PCHContainerRdr, 5424 const LangOptions &LangOpts, 5425 const TargetOptions &TargetOpts, 5426 const PreprocessorOptions &PPOpts, 5427 StringRef ExistingModuleCachePath) { 5428 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, 5429 ExistingModuleCachePath, FileMgr); 5430 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr, 5431 /*FindModuleFileExtensions=*/false, 5432 validator, 5433 /*ValidateDiagnosticOptions=*/true); 5434 } 5435 5436 ASTReader::ASTReadResult 5437 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) { 5438 // Enter the submodule block. 5439 if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) { 5440 Error(std::move(Err)); 5441 return Failure; 5442 } 5443 5444 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap(); 5445 bool First = true; 5446 Module *CurrentModule = nullptr; 5447 RecordData Record; 5448 while (true) { 5449 Expected<llvm::BitstreamEntry> MaybeEntry = 5450 F.Stream.advanceSkippingSubblocks(); 5451 if (!MaybeEntry) { 5452 Error(MaybeEntry.takeError()); 5453 return Failure; 5454 } 5455 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5456 5457 switch (Entry.Kind) { 5458 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 5459 case llvm::BitstreamEntry::Error: 5460 Error("malformed block record in AST file"); 5461 return Failure; 5462 case llvm::BitstreamEntry::EndBlock: 5463 return Success; 5464 case llvm::BitstreamEntry::Record: 5465 // The interesting case. 5466 break; 5467 } 5468 5469 // Read a record. 5470 StringRef Blob; 5471 Record.clear(); 5472 Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob); 5473 if (!MaybeKind) { 5474 Error(MaybeKind.takeError()); 5475 return Failure; 5476 } 5477 unsigned Kind = MaybeKind.get(); 5478 5479 if ((Kind == SUBMODULE_METADATA) != First) { 5480 Error("submodule metadata record should be at beginning of block"); 5481 return Failure; 5482 } 5483 First = false; 5484 5485 // Submodule information is only valid if we have a current module. 5486 // FIXME: Should we error on these cases? 5487 if (!CurrentModule && Kind != SUBMODULE_METADATA && 5488 Kind != SUBMODULE_DEFINITION) 5489 continue; 5490 5491 switch (Kind) { 5492 default: // Default behavior: ignore. 5493 break; 5494 5495 case SUBMODULE_DEFINITION: { 5496 if (Record.size() < 12) { 5497 Error("malformed module definition"); 5498 return Failure; 5499 } 5500 5501 StringRef Name = Blob; 5502 unsigned Idx = 0; 5503 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]); 5504 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]); 5505 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++]; 5506 bool IsFramework = Record[Idx++]; 5507 bool IsExplicit = Record[Idx++]; 5508 bool IsSystem = Record[Idx++]; 5509 bool IsExternC = Record[Idx++]; 5510 bool InferSubmodules = Record[Idx++]; 5511 bool InferExplicitSubmodules = Record[Idx++]; 5512 bool InferExportWildcard = Record[Idx++]; 5513 bool ConfigMacrosExhaustive = Record[Idx++]; 5514 bool ModuleMapIsPrivate = Record[Idx++]; 5515 5516 Module *ParentModule = nullptr; 5517 if (Parent) 5518 ParentModule = getSubmodule(Parent); 5519 5520 // Retrieve this (sub)module from the module map, creating it if 5521 // necessary. 5522 CurrentModule = 5523 ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit) 5524 .first; 5525 5526 // FIXME: set the definition loc for CurrentModule, or call 5527 // ModMap.setInferredModuleAllowedBy() 5528 5529 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS; 5530 if (GlobalIndex >= SubmodulesLoaded.size() || 5531 SubmodulesLoaded[GlobalIndex]) { 5532 Error("too many submodules"); 5533 return Failure; 5534 } 5535 5536 if (!ParentModule) { 5537 if (const FileEntry *CurFile = CurrentModule->getASTFile()) { 5538 // Don't emit module relocation error if we have -fno-validate-pch 5539 if (!PP.getPreprocessorOpts().DisablePCHValidation && 5540 CurFile != F.File) { 5541 Error(diag::err_module_file_conflict, 5542 CurrentModule->getTopLevelModuleName(), CurFile->getName(), 5543 F.File->getName()); 5544 return Failure; 5545 } 5546 } 5547 5548 F.DidReadTopLevelSubmodule = true; 5549 CurrentModule->setASTFile(F.File); 5550 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath; 5551 } 5552 5553 CurrentModule->Kind = Kind; 5554 CurrentModule->Signature = F.Signature; 5555 CurrentModule->IsFromModuleFile = true; 5556 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem; 5557 CurrentModule->IsExternC = IsExternC; 5558 CurrentModule->InferSubmodules = InferSubmodules; 5559 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules; 5560 CurrentModule->InferExportWildcard = InferExportWildcard; 5561 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive; 5562 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate; 5563 if (DeserializationListener) 5564 DeserializationListener->ModuleRead(GlobalID, CurrentModule); 5565 5566 SubmodulesLoaded[GlobalIndex] = CurrentModule; 5567 5568 // Clear out data that will be replaced by what is in the module file. 5569 CurrentModule->LinkLibraries.clear(); 5570 CurrentModule->ConfigMacros.clear(); 5571 CurrentModule->UnresolvedConflicts.clear(); 5572 CurrentModule->Conflicts.clear(); 5573 5574 // The module is available unless it's missing a requirement; relevant 5575 // requirements will be (re-)added by SUBMODULE_REQUIRES records. 5576 // Missing headers that were present when the module was built do not 5577 // make it unavailable -- if we got this far, this must be an explicitly 5578 // imported module file. 5579 CurrentModule->Requirements.clear(); 5580 CurrentModule->MissingHeaders.clear(); 5581 CurrentModule->IsUnimportable = 5582 ParentModule && ParentModule->IsUnimportable; 5583 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable; 5584 break; 5585 } 5586 5587 case SUBMODULE_UMBRELLA_HEADER: { 5588 std::string Filename = std::string(Blob); 5589 ResolveImportedPath(F, Filename); 5590 if (auto Umbrella = PP.getFileManager().getFile(Filename)) { 5591 if (!CurrentModule->getUmbrellaHeader()) 5592 ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob); 5593 else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) { 5594 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 5595 Error("mismatched umbrella headers in submodule"); 5596 return OutOfDate; 5597 } 5598 } 5599 break; 5600 } 5601 5602 case SUBMODULE_HEADER: 5603 case SUBMODULE_EXCLUDED_HEADER: 5604 case SUBMODULE_PRIVATE_HEADER: 5605 // We lazily associate headers with their modules via the HeaderInfo table. 5606 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead 5607 // of complete filenames or remove it entirely. 5608 break; 5609 5610 case SUBMODULE_TEXTUAL_HEADER: 5611 case SUBMODULE_PRIVATE_TEXTUAL_HEADER: 5612 // FIXME: Textual headers are not marked in the HeaderInfo table. Load 5613 // them here. 5614 break; 5615 5616 case SUBMODULE_TOPHEADER: 5617 CurrentModule->addTopHeaderFilename(Blob); 5618 break; 5619 5620 case SUBMODULE_UMBRELLA_DIR: { 5621 std::string Dirname = std::string(Blob); 5622 ResolveImportedPath(F, Dirname); 5623 if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) { 5624 if (!CurrentModule->getUmbrellaDir()) 5625 ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob); 5626 else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) { 5627 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 5628 Error("mismatched umbrella directories in submodule"); 5629 return OutOfDate; 5630 } 5631 } 5632 break; 5633 } 5634 5635 case SUBMODULE_METADATA: { 5636 F.BaseSubmoduleID = getTotalNumSubmodules(); 5637 F.LocalNumSubmodules = Record[0]; 5638 unsigned LocalBaseSubmoduleID = Record[1]; 5639 if (F.LocalNumSubmodules > 0) { 5640 // Introduce the global -> local mapping for submodules within this 5641 // module. 5642 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F)); 5643 5644 // Introduce the local -> global mapping for submodules within this 5645 // module. 5646 F.SubmoduleRemap.insertOrReplace( 5647 std::make_pair(LocalBaseSubmoduleID, 5648 F.BaseSubmoduleID - LocalBaseSubmoduleID)); 5649 5650 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules); 5651 } 5652 break; 5653 } 5654 5655 case SUBMODULE_IMPORTS: 5656 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) { 5657 UnresolvedModuleRef Unresolved; 5658 Unresolved.File = &F; 5659 Unresolved.Mod = CurrentModule; 5660 Unresolved.ID = Record[Idx]; 5661 Unresolved.Kind = UnresolvedModuleRef::Import; 5662 Unresolved.IsWildcard = false; 5663 UnresolvedModuleRefs.push_back(Unresolved); 5664 } 5665 break; 5666 5667 case SUBMODULE_EXPORTS: 5668 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) { 5669 UnresolvedModuleRef Unresolved; 5670 Unresolved.File = &F; 5671 Unresolved.Mod = CurrentModule; 5672 Unresolved.ID = Record[Idx]; 5673 Unresolved.Kind = UnresolvedModuleRef::Export; 5674 Unresolved.IsWildcard = Record[Idx + 1]; 5675 UnresolvedModuleRefs.push_back(Unresolved); 5676 } 5677 5678 // Once we've loaded the set of exports, there's no reason to keep 5679 // the parsed, unresolved exports around. 5680 CurrentModule->UnresolvedExports.clear(); 5681 break; 5682 5683 case SUBMODULE_REQUIRES: 5684 CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(), 5685 PP.getTargetInfo()); 5686 break; 5687 5688 case SUBMODULE_LINK_LIBRARY: 5689 ModMap.resolveLinkAsDependencies(CurrentModule); 5690 CurrentModule->LinkLibraries.push_back( 5691 Module::LinkLibrary(std::string(Blob), Record[0])); 5692 break; 5693 5694 case SUBMODULE_CONFIG_MACRO: 5695 CurrentModule->ConfigMacros.push_back(Blob.str()); 5696 break; 5697 5698 case SUBMODULE_CONFLICT: { 5699 UnresolvedModuleRef Unresolved; 5700 Unresolved.File = &F; 5701 Unresolved.Mod = CurrentModule; 5702 Unresolved.ID = Record[0]; 5703 Unresolved.Kind = UnresolvedModuleRef::Conflict; 5704 Unresolved.IsWildcard = false; 5705 Unresolved.String = Blob; 5706 UnresolvedModuleRefs.push_back(Unresolved); 5707 break; 5708 } 5709 5710 case SUBMODULE_INITIALIZERS: { 5711 if (!ContextObj) 5712 break; 5713 SmallVector<uint32_t, 16> Inits; 5714 for (auto &ID : Record) 5715 Inits.push_back(getGlobalDeclID(F, ID)); 5716 ContextObj->addLazyModuleInitializers(CurrentModule, Inits); 5717 break; 5718 } 5719 5720 case SUBMODULE_EXPORT_AS: 5721 CurrentModule->ExportAsModule = Blob.str(); 5722 ModMap.addLinkAsDependency(CurrentModule); 5723 break; 5724 } 5725 } 5726 } 5727 5728 /// Parse the record that corresponds to a LangOptions data 5729 /// structure. 5730 /// 5731 /// This routine parses the language options from the AST file and then gives 5732 /// them to the AST listener if one is set. 5733 /// 5734 /// \returns true if the listener deems the file unacceptable, false otherwise. 5735 bool ASTReader::ParseLanguageOptions(const RecordData &Record, 5736 bool Complain, 5737 ASTReaderListener &Listener, 5738 bool AllowCompatibleDifferences) { 5739 LangOptions LangOpts; 5740 unsigned Idx = 0; 5741 #define LANGOPT(Name, Bits, Default, Description) \ 5742 LangOpts.Name = Record[Idx++]; 5743 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 5744 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++])); 5745 #include "clang/Basic/LangOptions.def" 5746 #define SANITIZER(NAME, ID) \ 5747 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]); 5748 #include "clang/Basic/Sanitizers.def" 5749 5750 for (unsigned N = Record[Idx++]; N; --N) 5751 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx)); 5752 5753 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++]; 5754 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx); 5755 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion); 5756 5757 LangOpts.CurrentModule = ReadString(Record, Idx); 5758 5759 // Comment options. 5760 for (unsigned N = Record[Idx++]; N; --N) { 5761 LangOpts.CommentOpts.BlockCommandNames.push_back( 5762 ReadString(Record, Idx)); 5763 } 5764 LangOpts.CommentOpts.ParseAllComments = Record[Idx++]; 5765 5766 // OpenMP offloading options. 5767 for (unsigned N = Record[Idx++]; N; --N) { 5768 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx))); 5769 } 5770 5771 LangOpts.OMPHostIRFile = ReadString(Record, Idx); 5772 5773 return Listener.ReadLanguageOptions(LangOpts, Complain, 5774 AllowCompatibleDifferences); 5775 } 5776 5777 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain, 5778 ASTReaderListener &Listener, 5779 bool AllowCompatibleDifferences) { 5780 unsigned Idx = 0; 5781 TargetOptions TargetOpts; 5782 TargetOpts.Triple = ReadString(Record, Idx); 5783 TargetOpts.CPU = ReadString(Record, Idx); 5784 TargetOpts.TuneCPU = ReadString(Record, Idx); 5785 TargetOpts.ABI = ReadString(Record, Idx); 5786 for (unsigned N = Record[Idx++]; N; --N) { 5787 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx)); 5788 } 5789 for (unsigned N = Record[Idx++]; N; --N) { 5790 TargetOpts.Features.push_back(ReadString(Record, Idx)); 5791 } 5792 5793 return Listener.ReadTargetOptions(TargetOpts, Complain, 5794 AllowCompatibleDifferences); 5795 } 5796 5797 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain, 5798 ASTReaderListener &Listener) { 5799 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions); 5800 unsigned Idx = 0; 5801 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++]; 5802 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \ 5803 DiagOpts->set##Name(static_cast<Type>(Record[Idx++])); 5804 #include "clang/Basic/DiagnosticOptions.def" 5805 5806 for (unsigned N = Record[Idx++]; N; --N) 5807 DiagOpts->Warnings.push_back(ReadString(Record, Idx)); 5808 for (unsigned N = Record[Idx++]; N; --N) 5809 DiagOpts->Remarks.push_back(ReadString(Record, Idx)); 5810 5811 return Listener.ReadDiagnosticOptions(DiagOpts, Complain); 5812 } 5813 5814 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain, 5815 ASTReaderListener &Listener) { 5816 FileSystemOptions FSOpts; 5817 unsigned Idx = 0; 5818 FSOpts.WorkingDir = ReadString(Record, Idx); 5819 return Listener.ReadFileSystemOptions(FSOpts, Complain); 5820 } 5821 5822 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record, 5823 bool Complain, 5824 ASTReaderListener &Listener) { 5825 HeaderSearchOptions HSOpts; 5826 unsigned Idx = 0; 5827 HSOpts.Sysroot = ReadString(Record, Idx); 5828 5829 // Include entries. 5830 for (unsigned N = Record[Idx++]; N; --N) { 5831 std::string Path = ReadString(Record, Idx); 5832 frontend::IncludeDirGroup Group 5833 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]); 5834 bool IsFramework = Record[Idx++]; 5835 bool IgnoreSysRoot = Record[Idx++]; 5836 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework, 5837 IgnoreSysRoot); 5838 } 5839 5840 // System header prefixes. 5841 for (unsigned N = Record[Idx++]; N; --N) { 5842 std::string Prefix = ReadString(Record, Idx); 5843 bool IsSystemHeader = Record[Idx++]; 5844 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader); 5845 } 5846 5847 HSOpts.ResourceDir = ReadString(Record, Idx); 5848 HSOpts.ModuleCachePath = ReadString(Record, Idx); 5849 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx); 5850 HSOpts.DisableModuleHash = Record[Idx++]; 5851 HSOpts.ImplicitModuleMaps = Record[Idx++]; 5852 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++]; 5853 HSOpts.UseBuiltinIncludes = Record[Idx++]; 5854 HSOpts.UseStandardSystemIncludes = Record[Idx++]; 5855 HSOpts.UseStandardCXXIncludes = Record[Idx++]; 5856 HSOpts.UseLibcxx = Record[Idx++]; 5857 std::string SpecificModuleCachePath = ReadString(Record, Idx); 5858 5859 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5860 Complain); 5861 } 5862 5863 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record, 5864 bool Complain, 5865 ASTReaderListener &Listener, 5866 std::string &SuggestedPredefines) { 5867 PreprocessorOptions PPOpts; 5868 unsigned Idx = 0; 5869 5870 // Macro definitions/undefs 5871 for (unsigned N = Record[Idx++]; N; --N) { 5872 std::string Macro = ReadString(Record, Idx); 5873 bool IsUndef = Record[Idx++]; 5874 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef)); 5875 } 5876 5877 // Includes 5878 for (unsigned N = Record[Idx++]; N; --N) { 5879 PPOpts.Includes.push_back(ReadString(Record, Idx)); 5880 } 5881 5882 // Macro Includes 5883 for (unsigned N = Record[Idx++]; N; --N) { 5884 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx)); 5885 } 5886 5887 PPOpts.UsePredefines = Record[Idx++]; 5888 PPOpts.DetailedRecord = Record[Idx++]; 5889 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx); 5890 PPOpts.ObjCXXARCStandardLibrary = 5891 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]); 5892 SuggestedPredefines.clear(); 5893 return Listener.ReadPreprocessorOptions(PPOpts, Complain, 5894 SuggestedPredefines); 5895 } 5896 5897 std::pair<ModuleFile *, unsigned> 5898 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) { 5899 GlobalPreprocessedEntityMapType::iterator 5900 I = GlobalPreprocessedEntityMap.find(GlobalIndex); 5901 assert(I != GlobalPreprocessedEntityMap.end() && 5902 "Corrupted global preprocessed entity map"); 5903 ModuleFile *M = I->second; 5904 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID; 5905 return std::make_pair(M, LocalIndex); 5906 } 5907 5908 llvm::iterator_range<PreprocessingRecord::iterator> 5909 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const { 5910 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord()) 5911 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID, 5912 Mod.NumPreprocessedEntities); 5913 5914 return llvm::make_range(PreprocessingRecord::iterator(), 5915 PreprocessingRecord::iterator()); 5916 } 5917 5918 llvm::iterator_range<ASTReader::ModuleDeclIterator> 5919 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) { 5920 return llvm::make_range( 5921 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls), 5922 ModuleDeclIterator(this, &Mod, 5923 Mod.FileSortedDecls + Mod.NumFileSortedDecls)); 5924 } 5925 5926 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) { 5927 auto I = GlobalSkippedRangeMap.find(GlobalIndex); 5928 assert(I != GlobalSkippedRangeMap.end() && 5929 "Corrupted global skipped range map"); 5930 ModuleFile *M = I->second; 5931 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID; 5932 assert(LocalIndex < M->NumPreprocessedSkippedRanges); 5933 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex]; 5934 SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()), 5935 TranslateSourceLocation(*M, RawRange.getEnd())); 5936 assert(Range.isValid()); 5937 return Range; 5938 } 5939 5940 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) { 5941 PreprocessedEntityID PPID = Index+1; 5942 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 5943 ModuleFile &M = *PPInfo.first; 5944 unsigned LocalIndex = PPInfo.second; 5945 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 5946 5947 if (!PP.getPreprocessingRecord()) { 5948 Error("no preprocessing record"); 5949 return nullptr; 5950 } 5951 5952 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor); 5953 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit( 5954 M.MacroOffsetsBase + PPOffs.BitOffset)) { 5955 Error(std::move(Err)); 5956 return nullptr; 5957 } 5958 5959 Expected<llvm::BitstreamEntry> MaybeEntry = 5960 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 5961 if (!MaybeEntry) { 5962 Error(MaybeEntry.takeError()); 5963 return nullptr; 5964 } 5965 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5966 5967 if (Entry.Kind != llvm::BitstreamEntry::Record) 5968 return nullptr; 5969 5970 // Read the record. 5971 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()), 5972 TranslateSourceLocation(M, PPOffs.getEnd())); 5973 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 5974 StringRef Blob; 5975 RecordData Record; 5976 Expected<unsigned> MaybeRecType = 5977 M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob); 5978 if (!MaybeRecType) { 5979 Error(MaybeRecType.takeError()); 5980 return nullptr; 5981 } 5982 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) { 5983 case PPD_MACRO_EXPANSION: { 5984 bool isBuiltin = Record[0]; 5985 IdentifierInfo *Name = nullptr; 5986 MacroDefinitionRecord *Def = nullptr; 5987 if (isBuiltin) 5988 Name = getLocalIdentifier(M, Record[1]); 5989 else { 5990 PreprocessedEntityID GlobalID = 5991 getGlobalPreprocessedEntityID(M, Record[1]); 5992 Def = cast<MacroDefinitionRecord>( 5993 PPRec.getLoadedPreprocessedEntity(GlobalID - 1)); 5994 } 5995 5996 MacroExpansion *ME; 5997 if (isBuiltin) 5998 ME = new (PPRec) MacroExpansion(Name, Range); 5999 else 6000 ME = new (PPRec) MacroExpansion(Def, Range); 6001 6002 return ME; 6003 } 6004 6005 case PPD_MACRO_DEFINITION: { 6006 // Decode the identifier info and then check again; if the macro is 6007 // still defined and associated with the identifier, 6008 IdentifierInfo *II = getLocalIdentifier(M, Record[0]); 6009 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range); 6010 6011 if (DeserializationListener) 6012 DeserializationListener->MacroDefinitionRead(PPID, MD); 6013 6014 return MD; 6015 } 6016 6017 case PPD_INCLUSION_DIRECTIVE: { 6018 const char *FullFileNameStart = Blob.data() + Record[0]; 6019 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]); 6020 const FileEntry *File = nullptr; 6021 if (!FullFileName.empty()) 6022 if (auto FE = PP.getFileManager().getFile(FullFileName)) 6023 File = *FE; 6024 6025 // FIXME: Stable encoding 6026 InclusionDirective::InclusionKind Kind 6027 = static_cast<InclusionDirective::InclusionKind>(Record[2]); 6028 InclusionDirective *ID 6029 = new (PPRec) InclusionDirective(PPRec, Kind, 6030 StringRef(Blob.data(), Record[0]), 6031 Record[1], Record[3], 6032 File, 6033 Range); 6034 return ID; 6035 } 6036 } 6037 6038 llvm_unreachable("Invalid PreprocessorDetailRecordTypes"); 6039 } 6040 6041 /// Find the next module that contains entities and return the ID 6042 /// of the first entry. 6043 /// 6044 /// \param SLocMapI points at a chunk of a module that contains no 6045 /// preprocessed entities or the entities it contains are not the ones we are 6046 /// looking for. 6047 PreprocessedEntityID ASTReader::findNextPreprocessedEntity( 6048 GlobalSLocOffsetMapType::const_iterator SLocMapI) const { 6049 ++SLocMapI; 6050 for (GlobalSLocOffsetMapType::const_iterator 6051 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) { 6052 ModuleFile &M = *SLocMapI->second; 6053 if (M.NumPreprocessedEntities) 6054 return M.BasePreprocessedEntityID; 6055 } 6056 6057 return getTotalNumPreprocessedEntities(); 6058 } 6059 6060 namespace { 6061 6062 struct PPEntityComp { 6063 const ASTReader &Reader; 6064 ModuleFile &M; 6065 6066 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {} 6067 6068 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const { 6069 SourceLocation LHS = getLoc(L); 6070 SourceLocation RHS = getLoc(R); 6071 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6072 } 6073 6074 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const { 6075 SourceLocation LHS = getLoc(L); 6076 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6077 } 6078 6079 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const { 6080 SourceLocation RHS = getLoc(R); 6081 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6082 } 6083 6084 SourceLocation getLoc(const PPEntityOffset &PPE) const { 6085 return Reader.TranslateSourceLocation(M, PPE.getBegin()); 6086 } 6087 }; 6088 6089 } // namespace 6090 6091 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc, 6092 bool EndsAfter) const { 6093 if (SourceMgr.isLocalSourceLocation(Loc)) 6094 return getTotalNumPreprocessedEntities(); 6095 6096 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find( 6097 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1); 6098 assert(SLocMapI != GlobalSLocOffsetMap.end() && 6099 "Corrupted global sloc offset map"); 6100 6101 if (SLocMapI->second->NumPreprocessedEntities == 0) 6102 return findNextPreprocessedEntity(SLocMapI); 6103 6104 ModuleFile &M = *SLocMapI->second; 6105 6106 using pp_iterator = const PPEntityOffset *; 6107 6108 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 6109 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 6110 6111 size_t Count = M.NumPreprocessedEntities; 6112 size_t Half; 6113 pp_iterator First = pp_begin; 6114 pp_iterator PPI; 6115 6116 if (EndsAfter) { 6117 PPI = std::upper_bound(pp_begin, pp_end, Loc, 6118 PPEntityComp(*this, M)); 6119 } else { 6120 // Do a binary search manually instead of using std::lower_bound because 6121 // The end locations of entities may be unordered (when a macro expansion 6122 // is inside another macro argument), but for this case it is not important 6123 // whether we get the first macro expansion or its containing macro. 6124 while (Count > 0) { 6125 Half = Count / 2; 6126 PPI = First; 6127 std::advance(PPI, Half); 6128 if (SourceMgr.isBeforeInTranslationUnit( 6129 TranslateSourceLocation(M, PPI->getEnd()), Loc)) { 6130 First = PPI; 6131 ++First; 6132 Count = Count - Half - 1; 6133 } else 6134 Count = Half; 6135 } 6136 } 6137 6138 if (PPI == pp_end) 6139 return findNextPreprocessedEntity(SLocMapI); 6140 6141 return M.BasePreprocessedEntityID + (PPI - pp_begin); 6142 } 6143 6144 /// Returns a pair of [Begin, End) indices of preallocated 6145 /// preprocessed entities that \arg Range encompasses. 6146 std::pair<unsigned, unsigned> 6147 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) { 6148 if (Range.isInvalid()) 6149 return std::make_pair(0,0); 6150 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); 6151 6152 PreprocessedEntityID BeginID = 6153 findPreprocessedEntity(Range.getBegin(), false); 6154 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true); 6155 return std::make_pair(BeginID, EndID); 6156 } 6157 6158 /// Optionally returns true or false if the preallocated preprocessed 6159 /// entity with index \arg Index came from file \arg FID. 6160 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index, 6161 FileID FID) { 6162 if (FID.isInvalid()) 6163 return false; 6164 6165 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 6166 ModuleFile &M = *PPInfo.first; 6167 unsigned LocalIndex = PPInfo.second; 6168 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 6169 6170 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin()); 6171 if (Loc.isInvalid()) 6172 return false; 6173 6174 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID)) 6175 return true; 6176 else 6177 return false; 6178 } 6179 6180 namespace { 6181 6182 /// Visitor used to search for information about a header file. 6183 class HeaderFileInfoVisitor { 6184 const FileEntry *FE; 6185 Optional<HeaderFileInfo> HFI; 6186 6187 public: 6188 explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {} 6189 6190 bool operator()(ModuleFile &M) { 6191 HeaderFileInfoLookupTable *Table 6192 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable); 6193 if (!Table) 6194 return false; 6195 6196 // Look in the on-disk hash table for an entry for this file name. 6197 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE); 6198 if (Pos == Table->end()) 6199 return false; 6200 6201 HFI = *Pos; 6202 return true; 6203 } 6204 6205 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; } 6206 }; 6207 6208 } // namespace 6209 6210 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) { 6211 HeaderFileInfoVisitor Visitor(FE); 6212 ModuleMgr.visit(Visitor); 6213 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) 6214 return *HFI; 6215 6216 return HeaderFileInfo(); 6217 } 6218 6219 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) { 6220 using DiagState = DiagnosticsEngine::DiagState; 6221 SmallVector<DiagState *, 32> DiagStates; 6222 6223 for (ModuleFile &F : ModuleMgr) { 6224 unsigned Idx = 0; 6225 auto &Record = F.PragmaDiagMappings; 6226 if (Record.empty()) 6227 continue; 6228 6229 DiagStates.clear(); 6230 6231 auto ReadDiagState = 6232 [&](const DiagState &BasedOn, SourceLocation Loc, 6233 bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * { 6234 unsigned BackrefID = Record[Idx++]; 6235 if (BackrefID != 0) 6236 return DiagStates[BackrefID - 1]; 6237 6238 // A new DiagState was created here. 6239 Diag.DiagStates.push_back(BasedOn); 6240 DiagState *NewState = &Diag.DiagStates.back(); 6241 DiagStates.push_back(NewState); 6242 unsigned Size = Record[Idx++]; 6243 assert(Idx + Size * 2 <= Record.size() && 6244 "Invalid data, not enough diag/map pairs"); 6245 while (Size--) { 6246 unsigned DiagID = Record[Idx++]; 6247 DiagnosticMapping NewMapping = 6248 DiagnosticMapping::deserialize(Record[Idx++]); 6249 if (!NewMapping.isPragma() && !IncludeNonPragmaStates) 6250 continue; 6251 6252 DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID); 6253 6254 // If this mapping was specified as a warning but the severity was 6255 // upgraded due to diagnostic settings, simulate the current diagnostic 6256 // settings (and use a warning). 6257 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) { 6258 NewMapping.setSeverity(diag::Severity::Warning); 6259 NewMapping.setUpgradedFromWarning(false); 6260 } 6261 6262 Mapping = NewMapping; 6263 } 6264 return NewState; 6265 }; 6266 6267 // Read the first state. 6268 DiagState *FirstState; 6269 if (F.Kind == MK_ImplicitModule) { 6270 // Implicitly-built modules are reused with different diagnostic 6271 // settings. Use the initial diagnostic state from Diag to simulate this 6272 // compilation's diagnostic settings. 6273 FirstState = Diag.DiagStatesByLoc.FirstDiagState; 6274 DiagStates.push_back(FirstState); 6275 6276 // Skip the initial diagnostic state from the serialized module. 6277 assert(Record[1] == 0 && 6278 "Invalid data, unexpected backref in initial state"); 6279 Idx = 3 + Record[2] * 2; 6280 assert(Idx < Record.size() && 6281 "Invalid data, not enough state change pairs in initial state"); 6282 } else if (F.isModule()) { 6283 // For an explicit module, preserve the flags from the module build 6284 // command line (-w, -Weverything, -Werror, ...) along with any explicit 6285 // -Wblah flags. 6286 unsigned Flags = Record[Idx++]; 6287 DiagState Initial; 6288 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1; 6289 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1; 6290 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1; 6291 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1; 6292 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1; 6293 Initial.ExtBehavior = (diag::Severity)Flags; 6294 FirstState = ReadDiagState(Initial, SourceLocation(), true); 6295 6296 assert(F.OriginalSourceFileID.isValid()); 6297 6298 // Set up the root buffer of the module to start with the initial 6299 // diagnostic state of the module itself, to cover files that contain no 6300 // explicit transitions (for which we did not serialize anything). 6301 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID] 6302 .StateTransitions.push_back({FirstState, 0}); 6303 } else { 6304 // For prefix ASTs, start with whatever the user configured on the 6305 // command line. 6306 Idx++; // Skip flags. 6307 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, 6308 SourceLocation(), false); 6309 } 6310 6311 // Read the state transitions. 6312 unsigned NumLocations = Record[Idx++]; 6313 while (NumLocations--) { 6314 assert(Idx < Record.size() && 6315 "Invalid data, missing pragma diagnostic states"); 6316 SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]); 6317 auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc); 6318 assert(IDAndOffset.first.isValid() && "invalid FileID for transition"); 6319 assert(IDAndOffset.second == 0 && "not a start location for a FileID"); 6320 unsigned Transitions = Record[Idx++]; 6321 6322 // Note that we don't need to set up Parent/ParentOffset here, because 6323 // we won't be changing the diagnostic state within imported FileIDs 6324 // (other than perhaps appending to the main source file, which has no 6325 // parent). 6326 auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first]; 6327 F.StateTransitions.reserve(F.StateTransitions.size() + Transitions); 6328 for (unsigned I = 0; I != Transitions; ++I) { 6329 unsigned Offset = Record[Idx++]; 6330 auto *State = 6331 ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false); 6332 F.StateTransitions.push_back({State, Offset}); 6333 } 6334 } 6335 6336 // Read the final state. 6337 assert(Idx < Record.size() && 6338 "Invalid data, missing final pragma diagnostic state"); 6339 SourceLocation CurStateLoc = 6340 ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]); 6341 auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false); 6342 6343 if (!F.isModule()) { 6344 Diag.DiagStatesByLoc.CurDiagState = CurState; 6345 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc; 6346 6347 // Preserve the property that the imaginary root file describes the 6348 // current state. 6349 FileID NullFile; 6350 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions; 6351 if (T.empty()) 6352 T.push_back({CurState, 0}); 6353 else 6354 T[0].State = CurState; 6355 } 6356 6357 // Don't try to read these mappings again. 6358 Record.clear(); 6359 } 6360 } 6361 6362 /// Get the correct cursor and offset for loading a type. 6363 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) { 6364 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index); 6365 assert(I != GlobalTypeMap.end() && "Corrupted global type map"); 6366 ModuleFile *M = I->second; 6367 return RecordLocation( 6368 M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() + 6369 M->DeclsBlockStartOffset); 6370 } 6371 6372 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) { 6373 switch (code) { 6374 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \ 6375 case TYPE_##CODE_ID: return Type::CLASS_ID; 6376 #include "clang/Serialization/TypeBitCodes.def" 6377 default: return llvm::None; 6378 } 6379 } 6380 6381 /// Read and return the type with the given index.. 6382 /// 6383 /// The index is the type ID, shifted and minus the number of predefs. This 6384 /// routine actually reads the record corresponding to the type at the given 6385 /// location. It is a helper routine for GetType, which deals with reading type 6386 /// IDs. 6387 QualType ASTReader::readTypeRecord(unsigned Index) { 6388 assert(ContextObj && "reading type with no AST context"); 6389 ASTContext &Context = *ContextObj; 6390 RecordLocation Loc = TypeCursorForIndex(Index); 6391 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 6392 6393 // Keep track of where we are in the stream, then jump back there 6394 // after reading this type. 6395 SavedStreamPosition SavedPosition(DeclsCursor); 6396 6397 ReadingKindTracker ReadingKind(Read_Type, *this); 6398 6399 // Note that we are loading a type record. 6400 Deserializing AType(this); 6401 6402 if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) { 6403 Error(std::move(Err)); 6404 return QualType(); 6405 } 6406 Expected<unsigned> RawCode = DeclsCursor.ReadCode(); 6407 if (!RawCode) { 6408 Error(RawCode.takeError()); 6409 return QualType(); 6410 } 6411 6412 ASTRecordReader Record(*this, *Loc.F); 6413 Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get()); 6414 if (!Code) { 6415 Error(Code.takeError()); 6416 return QualType(); 6417 } 6418 if (Code.get() == TYPE_EXT_QUAL) { 6419 QualType baseType = Record.readQualType(); 6420 Qualifiers quals = Record.readQualifiers(); 6421 return Context.getQualifiedType(baseType, quals); 6422 } 6423 6424 auto maybeClass = getTypeClassForCode((TypeCode) Code.get()); 6425 if (!maybeClass) { 6426 Error("Unexpected code for type"); 6427 return QualType(); 6428 } 6429 6430 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record); 6431 return TypeReader.read(*maybeClass); 6432 } 6433 6434 namespace clang { 6435 6436 class TypeLocReader : public TypeLocVisitor<TypeLocReader> { 6437 ASTRecordReader &Reader; 6438 6439 SourceLocation readSourceLocation() { 6440 return Reader.readSourceLocation(); 6441 } 6442 6443 TypeSourceInfo *GetTypeSourceInfo() { 6444 return Reader.readTypeSourceInfo(); 6445 } 6446 6447 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() { 6448 return Reader.readNestedNameSpecifierLoc(); 6449 } 6450 6451 Attr *ReadAttr() { 6452 return Reader.readAttr(); 6453 } 6454 6455 public: 6456 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {} 6457 6458 // We want compile-time assurance that we've enumerated all of 6459 // these, so unfortunately we have to declare them first, then 6460 // define them out-of-line. 6461 #define ABSTRACT_TYPELOC(CLASS, PARENT) 6462 #define TYPELOC(CLASS, PARENT) \ 6463 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 6464 #include "clang/AST/TypeLocNodes.def" 6465 6466 void VisitFunctionTypeLoc(FunctionTypeLoc); 6467 void VisitArrayTypeLoc(ArrayTypeLoc); 6468 }; 6469 6470 } // namespace clang 6471 6472 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 6473 // nothing to do 6474 } 6475 6476 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 6477 TL.setBuiltinLoc(readSourceLocation()); 6478 if (TL.needsExtraLocalData()) { 6479 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt())); 6480 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Reader.readInt())); 6481 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Reader.readInt())); 6482 TL.setModeAttr(Reader.readInt()); 6483 } 6484 } 6485 6486 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { 6487 TL.setNameLoc(readSourceLocation()); 6488 } 6489 6490 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { 6491 TL.setStarLoc(readSourceLocation()); 6492 } 6493 6494 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) { 6495 // nothing to do 6496 } 6497 6498 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { 6499 // nothing to do 6500 } 6501 6502 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) { 6503 TL.setExpansionLoc(readSourceLocation()); 6504 } 6505 6506 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 6507 TL.setCaretLoc(readSourceLocation()); 6508 } 6509 6510 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 6511 TL.setAmpLoc(readSourceLocation()); 6512 } 6513 6514 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 6515 TL.setAmpAmpLoc(readSourceLocation()); 6516 } 6517 6518 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 6519 TL.setStarLoc(readSourceLocation()); 6520 TL.setClassTInfo(GetTypeSourceInfo()); 6521 } 6522 6523 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { 6524 TL.setLBracketLoc(readSourceLocation()); 6525 TL.setRBracketLoc(readSourceLocation()); 6526 if (Reader.readBool()) 6527 TL.setSizeExpr(Reader.readExpr()); 6528 else 6529 TL.setSizeExpr(nullptr); 6530 } 6531 6532 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 6533 VisitArrayTypeLoc(TL); 6534 } 6535 6536 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 6537 VisitArrayTypeLoc(TL); 6538 } 6539 6540 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 6541 VisitArrayTypeLoc(TL); 6542 } 6543 6544 void TypeLocReader::VisitDependentSizedArrayTypeLoc( 6545 DependentSizedArrayTypeLoc TL) { 6546 VisitArrayTypeLoc(TL); 6547 } 6548 6549 void TypeLocReader::VisitDependentAddressSpaceTypeLoc( 6550 DependentAddressSpaceTypeLoc TL) { 6551 6552 TL.setAttrNameLoc(readSourceLocation()); 6553 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6554 TL.setAttrExprOperand(Reader.readExpr()); 6555 } 6556 6557 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( 6558 DependentSizedExtVectorTypeLoc TL) { 6559 TL.setNameLoc(readSourceLocation()); 6560 } 6561 6562 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { 6563 TL.setNameLoc(readSourceLocation()); 6564 } 6565 6566 void TypeLocReader::VisitDependentVectorTypeLoc( 6567 DependentVectorTypeLoc TL) { 6568 TL.setNameLoc(readSourceLocation()); 6569 } 6570 6571 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 6572 TL.setNameLoc(readSourceLocation()); 6573 } 6574 6575 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) { 6576 TL.setAttrNameLoc(readSourceLocation()); 6577 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6578 TL.setAttrRowOperand(Reader.readExpr()); 6579 TL.setAttrColumnOperand(Reader.readExpr()); 6580 } 6581 6582 void TypeLocReader::VisitDependentSizedMatrixTypeLoc( 6583 DependentSizedMatrixTypeLoc TL) { 6584 TL.setAttrNameLoc(readSourceLocation()); 6585 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6586 TL.setAttrRowOperand(Reader.readExpr()); 6587 TL.setAttrColumnOperand(Reader.readExpr()); 6588 } 6589 6590 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 6591 TL.setLocalRangeBegin(readSourceLocation()); 6592 TL.setLParenLoc(readSourceLocation()); 6593 TL.setRParenLoc(readSourceLocation()); 6594 TL.setExceptionSpecRange(Reader.readSourceRange()); 6595 TL.setLocalRangeEnd(readSourceLocation()); 6596 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) { 6597 TL.setParam(i, Reader.readDeclAs<ParmVarDecl>()); 6598 } 6599 } 6600 6601 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 6602 VisitFunctionTypeLoc(TL); 6603 } 6604 6605 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 6606 VisitFunctionTypeLoc(TL); 6607 } 6608 6609 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 6610 TL.setNameLoc(readSourceLocation()); 6611 } 6612 6613 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 6614 TL.setNameLoc(readSourceLocation()); 6615 } 6616 6617 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 6618 TL.setTypeofLoc(readSourceLocation()); 6619 TL.setLParenLoc(readSourceLocation()); 6620 TL.setRParenLoc(readSourceLocation()); 6621 } 6622 6623 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 6624 TL.setTypeofLoc(readSourceLocation()); 6625 TL.setLParenLoc(readSourceLocation()); 6626 TL.setRParenLoc(readSourceLocation()); 6627 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6628 } 6629 6630 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 6631 TL.setNameLoc(readSourceLocation()); 6632 } 6633 6634 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 6635 TL.setKWLoc(readSourceLocation()); 6636 TL.setLParenLoc(readSourceLocation()); 6637 TL.setRParenLoc(readSourceLocation()); 6638 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6639 } 6640 6641 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) { 6642 TL.setNameLoc(readSourceLocation()); 6643 if (Reader.readBool()) { 6644 TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc()); 6645 TL.setTemplateKWLoc(readSourceLocation()); 6646 TL.setConceptNameLoc(readSourceLocation()); 6647 TL.setFoundDecl(Reader.readDeclAs<NamedDecl>()); 6648 TL.setLAngleLoc(readSourceLocation()); 6649 TL.setRAngleLoc(readSourceLocation()); 6650 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6651 TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo( 6652 TL.getTypePtr()->getArg(i).getKind())); 6653 } 6654 } 6655 6656 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc( 6657 DeducedTemplateSpecializationTypeLoc TL) { 6658 TL.setTemplateNameLoc(readSourceLocation()); 6659 } 6660 6661 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { 6662 TL.setNameLoc(readSourceLocation()); 6663 } 6664 6665 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { 6666 TL.setNameLoc(readSourceLocation()); 6667 } 6668 6669 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 6670 TL.setAttr(ReadAttr()); 6671 } 6672 6673 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 6674 TL.setNameLoc(readSourceLocation()); 6675 } 6676 6677 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( 6678 SubstTemplateTypeParmTypeLoc TL) { 6679 TL.setNameLoc(readSourceLocation()); 6680 } 6681 6682 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc( 6683 SubstTemplateTypeParmPackTypeLoc TL) { 6684 TL.setNameLoc(readSourceLocation()); 6685 } 6686 6687 void TypeLocReader::VisitTemplateSpecializationTypeLoc( 6688 TemplateSpecializationTypeLoc TL) { 6689 TL.setTemplateKeywordLoc(readSourceLocation()); 6690 TL.setTemplateNameLoc(readSourceLocation()); 6691 TL.setLAngleLoc(readSourceLocation()); 6692 TL.setRAngleLoc(readSourceLocation()); 6693 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6694 TL.setArgLocInfo( 6695 i, 6696 Reader.readTemplateArgumentLocInfo( 6697 TL.getTypePtr()->getArg(i).getKind())); 6698 } 6699 6700 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) { 6701 TL.setLParenLoc(readSourceLocation()); 6702 TL.setRParenLoc(readSourceLocation()); 6703 } 6704 6705 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 6706 TL.setElaboratedKeywordLoc(readSourceLocation()); 6707 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6708 } 6709 6710 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 6711 TL.setNameLoc(readSourceLocation()); 6712 } 6713 6714 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 6715 TL.setElaboratedKeywordLoc(readSourceLocation()); 6716 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6717 TL.setNameLoc(readSourceLocation()); 6718 } 6719 6720 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( 6721 DependentTemplateSpecializationTypeLoc TL) { 6722 TL.setElaboratedKeywordLoc(readSourceLocation()); 6723 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6724 TL.setTemplateKeywordLoc(readSourceLocation()); 6725 TL.setTemplateNameLoc(readSourceLocation()); 6726 TL.setLAngleLoc(readSourceLocation()); 6727 TL.setRAngleLoc(readSourceLocation()); 6728 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 6729 TL.setArgLocInfo( 6730 I, 6731 Reader.readTemplateArgumentLocInfo( 6732 TL.getTypePtr()->getArg(I).getKind())); 6733 } 6734 6735 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 6736 TL.setEllipsisLoc(readSourceLocation()); 6737 } 6738 6739 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 6740 TL.setNameLoc(readSourceLocation()); 6741 } 6742 6743 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) { 6744 if (TL.getNumProtocols()) { 6745 TL.setProtocolLAngleLoc(readSourceLocation()); 6746 TL.setProtocolRAngleLoc(readSourceLocation()); 6747 } 6748 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6749 TL.setProtocolLoc(i, readSourceLocation()); 6750 } 6751 6752 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 6753 TL.setHasBaseTypeAsWritten(Reader.readBool()); 6754 TL.setTypeArgsLAngleLoc(readSourceLocation()); 6755 TL.setTypeArgsRAngleLoc(readSourceLocation()); 6756 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i) 6757 TL.setTypeArgTInfo(i, GetTypeSourceInfo()); 6758 TL.setProtocolLAngleLoc(readSourceLocation()); 6759 TL.setProtocolRAngleLoc(readSourceLocation()); 6760 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6761 TL.setProtocolLoc(i, readSourceLocation()); 6762 } 6763 6764 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 6765 TL.setStarLoc(readSourceLocation()); 6766 } 6767 6768 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) { 6769 TL.setKWLoc(readSourceLocation()); 6770 TL.setLParenLoc(readSourceLocation()); 6771 TL.setRParenLoc(readSourceLocation()); 6772 } 6773 6774 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) { 6775 TL.setKWLoc(readSourceLocation()); 6776 } 6777 6778 void TypeLocReader::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) { 6779 TL.setNameLoc(readSourceLocation()); 6780 } 6781 void TypeLocReader::VisitDependentExtIntTypeLoc( 6782 clang::DependentExtIntTypeLoc TL) { 6783 TL.setNameLoc(readSourceLocation()); 6784 } 6785 6786 6787 void ASTRecordReader::readTypeLoc(TypeLoc TL) { 6788 TypeLocReader TLR(*this); 6789 for (; !TL.isNull(); TL = TL.getNextTypeLoc()) 6790 TLR.Visit(TL); 6791 } 6792 6793 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() { 6794 QualType InfoTy = readType(); 6795 if (InfoTy.isNull()) 6796 return nullptr; 6797 6798 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy); 6799 readTypeLoc(TInfo->getTypeLoc()); 6800 return TInfo; 6801 } 6802 6803 QualType ASTReader::GetType(TypeID ID) { 6804 assert(ContextObj && "reading type with no AST context"); 6805 ASTContext &Context = *ContextObj; 6806 6807 unsigned FastQuals = ID & Qualifiers::FastMask; 6808 unsigned Index = ID >> Qualifiers::FastWidth; 6809 6810 if (Index < NUM_PREDEF_TYPE_IDS) { 6811 QualType T; 6812 switch ((PredefinedTypeIDs)Index) { 6813 case PREDEF_TYPE_NULL_ID: 6814 return QualType(); 6815 case PREDEF_TYPE_VOID_ID: 6816 T = Context.VoidTy; 6817 break; 6818 case PREDEF_TYPE_BOOL_ID: 6819 T = Context.BoolTy; 6820 break; 6821 case PREDEF_TYPE_CHAR_U_ID: 6822 case PREDEF_TYPE_CHAR_S_ID: 6823 // FIXME: Check that the signedness of CharTy is correct! 6824 T = Context.CharTy; 6825 break; 6826 case PREDEF_TYPE_UCHAR_ID: 6827 T = Context.UnsignedCharTy; 6828 break; 6829 case PREDEF_TYPE_USHORT_ID: 6830 T = Context.UnsignedShortTy; 6831 break; 6832 case PREDEF_TYPE_UINT_ID: 6833 T = Context.UnsignedIntTy; 6834 break; 6835 case PREDEF_TYPE_ULONG_ID: 6836 T = Context.UnsignedLongTy; 6837 break; 6838 case PREDEF_TYPE_ULONGLONG_ID: 6839 T = Context.UnsignedLongLongTy; 6840 break; 6841 case PREDEF_TYPE_UINT128_ID: 6842 T = Context.UnsignedInt128Ty; 6843 break; 6844 case PREDEF_TYPE_SCHAR_ID: 6845 T = Context.SignedCharTy; 6846 break; 6847 case PREDEF_TYPE_WCHAR_ID: 6848 T = Context.WCharTy; 6849 break; 6850 case PREDEF_TYPE_SHORT_ID: 6851 T = Context.ShortTy; 6852 break; 6853 case PREDEF_TYPE_INT_ID: 6854 T = Context.IntTy; 6855 break; 6856 case PREDEF_TYPE_LONG_ID: 6857 T = Context.LongTy; 6858 break; 6859 case PREDEF_TYPE_LONGLONG_ID: 6860 T = Context.LongLongTy; 6861 break; 6862 case PREDEF_TYPE_INT128_ID: 6863 T = Context.Int128Ty; 6864 break; 6865 case PREDEF_TYPE_BFLOAT16_ID: 6866 T = Context.BFloat16Ty; 6867 break; 6868 case PREDEF_TYPE_HALF_ID: 6869 T = Context.HalfTy; 6870 break; 6871 case PREDEF_TYPE_FLOAT_ID: 6872 T = Context.FloatTy; 6873 break; 6874 case PREDEF_TYPE_DOUBLE_ID: 6875 T = Context.DoubleTy; 6876 break; 6877 case PREDEF_TYPE_LONGDOUBLE_ID: 6878 T = Context.LongDoubleTy; 6879 break; 6880 case PREDEF_TYPE_SHORT_ACCUM_ID: 6881 T = Context.ShortAccumTy; 6882 break; 6883 case PREDEF_TYPE_ACCUM_ID: 6884 T = Context.AccumTy; 6885 break; 6886 case PREDEF_TYPE_LONG_ACCUM_ID: 6887 T = Context.LongAccumTy; 6888 break; 6889 case PREDEF_TYPE_USHORT_ACCUM_ID: 6890 T = Context.UnsignedShortAccumTy; 6891 break; 6892 case PREDEF_TYPE_UACCUM_ID: 6893 T = Context.UnsignedAccumTy; 6894 break; 6895 case PREDEF_TYPE_ULONG_ACCUM_ID: 6896 T = Context.UnsignedLongAccumTy; 6897 break; 6898 case PREDEF_TYPE_SHORT_FRACT_ID: 6899 T = Context.ShortFractTy; 6900 break; 6901 case PREDEF_TYPE_FRACT_ID: 6902 T = Context.FractTy; 6903 break; 6904 case PREDEF_TYPE_LONG_FRACT_ID: 6905 T = Context.LongFractTy; 6906 break; 6907 case PREDEF_TYPE_USHORT_FRACT_ID: 6908 T = Context.UnsignedShortFractTy; 6909 break; 6910 case PREDEF_TYPE_UFRACT_ID: 6911 T = Context.UnsignedFractTy; 6912 break; 6913 case PREDEF_TYPE_ULONG_FRACT_ID: 6914 T = Context.UnsignedLongFractTy; 6915 break; 6916 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID: 6917 T = Context.SatShortAccumTy; 6918 break; 6919 case PREDEF_TYPE_SAT_ACCUM_ID: 6920 T = Context.SatAccumTy; 6921 break; 6922 case PREDEF_TYPE_SAT_LONG_ACCUM_ID: 6923 T = Context.SatLongAccumTy; 6924 break; 6925 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID: 6926 T = Context.SatUnsignedShortAccumTy; 6927 break; 6928 case PREDEF_TYPE_SAT_UACCUM_ID: 6929 T = Context.SatUnsignedAccumTy; 6930 break; 6931 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID: 6932 T = Context.SatUnsignedLongAccumTy; 6933 break; 6934 case PREDEF_TYPE_SAT_SHORT_FRACT_ID: 6935 T = Context.SatShortFractTy; 6936 break; 6937 case PREDEF_TYPE_SAT_FRACT_ID: 6938 T = Context.SatFractTy; 6939 break; 6940 case PREDEF_TYPE_SAT_LONG_FRACT_ID: 6941 T = Context.SatLongFractTy; 6942 break; 6943 case PREDEF_TYPE_SAT_USHORT_FRACT_ID: 6944 T = Context.SatUnsignedShortFractTy; 6945 break; 6946 case PREDEF_TYPE_SAT_UFRACT_ID: 6947 T = Context.SatUnsignedFractTy; 6948 break; 6949 case PREDEF_TYPE_SAT_ULONG_FRACT_ID: 6950 T = Context.SatUnsignedLongFractTy; 6951 break; 6952 case PREDEF_TYPE_FLOAT16_ID: 6953 T = Context.Float16Ty; 6954 break; 6955 case PREDEF_TYPE_FLOAT128_ID: 6956 T = Context.Float128Ty; 6957 break; 6958 case PREDEF_TYPE_OVERLOAD_ID: 6959 T = Context.OverloadTy; 6960 break; 6961 case PREDEF_TYPE_BOUND_MEMBER: 6962 T = Context.BoundMemberTy; 6963 break; 6964 case PREDEF_TYPE_PSEUDO_OBJECT: 6965 T = Context.PseudoObjectTy; 6966 break; 6967 case PREDEF_TYPE_DEPENDENT_ID: 6968 T = Context.DependentTy; 6969 break; 6970 case PREDEF_TYPE_UNKNOWN_ANY: 6971 T = Context.UnknownAnyTy; 6972 break; 6973 case PREDEF_TYPE_NULLPTR_ID: 6974 T = Context.NullPtrTy; 6975 break; 6976 case PREDEF_TYPE_CHAR8_ID: 6977 T = Context.Char8Ty; 6978 break; 6979 case PREDEF_TYPE_CHAR16_ID: 6980 T = Context.Char16Ty; 6981 break; 6982 case PREDEF_TYPE_CHAR32_ID: 6983 T = Context.Char32Ty; 6984 break; 6985 case PREDEF_TYPE_OBJC_ID: 6986 T = Context.ObjCBuiltinIdTy; 6987 break; 6988 case PREDEF_TYPE_OBJC_CLASS: 6989 T = Context.ObjCBuiltinClassTy; 6990 break; 6991 case PREDEF_TYPE_OBJC_SEL: 6992 T = Context.ObjCBuiltinSelTy; 6993 break; 6994 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 6995 case PREDEF_TYPE_##Id##_ID: \ 6996 T = Context.SingletonId; \ 6997 break; 6998 #include "clang/Basic/OpenCLImageTypes.def" 6999 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ 7000 case PREDEF_TYPE_##Id##_ID: \ 7001 T = Context.Id##Ty; \ 7002 break; 7003 #include "clang/Basic/OpenCLExtensionTypes.def" 7004 case PREDEF_TYPE_SAMPLER_ID: 7005 T = Context.OCLSamplerTy; 7006 break; 7007 case PREDEF_TYPE_EVENT_ID: 7008 T = Context.OCLEventTy; 7009 break; 7010 case PREDEF_TYPE_CLK_EVENT_ID: 7011 T = Context.OCLClkEventTy; 7012 break; 7013 case PREDEF_TYPE_QUEUE_ID: 7014 T = Context.OCLQueueTy; 7015 break; 7016 case PREDEF_TYPE_RESERVE_ID_ID: 7017 T = Context.OCLReserveIDTy; 7018 break; 7019 case PREDEF_TYPE_AUTO_DEDUCT: 7020 T = Context.getAutoDeductType(); 7021 break; 7022 case PREDEF_TYPE_AUTO_RREF_DEDUCT: 7023 T = Context.getAutoRRefDeductType(); 7024 break; 7025 case PREDEF_TYPE_ARC_UNBRIDGED_CAST: 7026 T = Context.ARCUnbridgedCastTy; 7027 break; 7028 case PREDEF_TYPE_BUILTIN_FN: 7029 T = Context.BuiltinFnTy; 7030 break; 7031 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX: 7032 T = Context.IncompleteMatrixIdxTy; 7033 break; 7034 case PREDEF_TYPE_OMP_ARRAY_SECTION: 7035 T = Context.OMPArraySectionTy; 7036 break; 7037 case PREDEF_TYPE_OMP_ARRAY_SHAPING: 7038 T = Context.OMPArraySectionTy; 7039 break; 7040 case PREDEF_TYPE_OMP_ITERATOR: 7041 T = Context.OMPIteratorTy; 7042 break; 7043 #define SVE_TYPE(Name, Id, SingletonId) \ 7044 case PREDEF_TYPE_##Id##_ID: \ 7045 T = Context.SingletonId; \ 7046 break; 7047 #include "clang/Basic/AArch64SVEACLETypes.def" 7048 } 7049 7050 assert(!T.isNull() && "Unknown predefined type"); 7051 return T.withFastQualifiers(FastQuals); 7052 } 7053 7054 Index -= NUM_PREDEF_TYPE_IDS; 7055 assert(Index < TypesLoaded.size() && "Type index out-of-range"); 7056 if (TypesLoaded[Index].isNull()) { 7057 TypesLoaded[Index] = readTypeRecord(Index); 7058 if (TypesLoaded[Index].isNull()) 7059 return QualType(); 7060 7061 TypesLoaded[Index]->setFromAST(); 7062 if (DeserializationListener) 7063 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID), 7064 TypesLoaded[Index]); 7065 } 7066 7067 return TypesLoaded[Index].withFastQualifiers(FastQuals); 7068 } 7069 7070 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) { 7071 return GetType(getGlobalTypeID(F, LocalID)); 7072 } 7073 7074 serialization::TypeID 7075 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const { 7076 unsigned FastQuals = LocalID & Qualifiers::FastMask; 7077 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth; 7078 7079 if (LocalIndex < NUM_PREDEF_TYPE_IDS) 7080 return LocalID; 7081 7082 if (!F.ModuleOffsetMap.empty()) 7083 ReadModuleOffsetMap(F); 7084 7085 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7086 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS); 7087 assert(I != F.TypeRemap.end() && "Invalid index into type index remap"); 7088 7089 unsigned GlobalIndex = LocalIndex + I->second; 7090 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals; 7091 } 7092 7093 TemplateArgumentLocInfo 7094 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) { 7095 switch (Kind) { 7096 case TemplateArgument::Expression: 7097 return readExpr(); 7098 case TemplateArgument::Type: 7099 return readTypeSourceInfo(); 7100 case TemplateArgument::Template: { 7101 NestedNameSpecifierLoc QualifierLoc = 7102 readNestedNameSpecifierLoc(); 7103 SourceLocation TemplateNameLoc = readSourceLocation(); 7104 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 7105 SourceLocation()); 7106 } 7107 case TemplateArgument::TemplateExpansion: { 7108 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc(); 7109 SourceLocation TemplateNameLoc = readSourceLocation(); 7110 SourceLocation EllipsisLoc = readSourceLocation(); 7111 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 7112 EllipsisLoc); 7113 } 7114 case TemplateArgument::Null: 7115 case TemplateArgument::Integral: 7116 case TemplateArgument::Declaration: 7117 case TemplateArgument::NullPtr: 7118 case TemplateArgument::Pack: 7119 // FIXME: Is this right? 7120 return TemplateArgumentLocInfo(); 7121 } 7122 llvm_unreachable("unexpected template argument loc"); 7123 } 7124 7125 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() { 7126 TemplateArgument Arg = readTemplateArgument(); 7127 7128 if (Arg.getKind() == TemplateArgument::Expression) { 7129 if (readBool()) // bool InfoHasSameExpr. 7130 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); 7131 } 7132 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind())); 7133 } 7134 7135 const ASTTemplateArgumentListInfo * 7136 ASTRecordReader::readASTTemplateArgumentListInfo() { 7137 SourceLocation LAngleLoc = readSourceLocation(); 7138 SourceLocation RAngleLoc = readSourceLocation(); 7139 unsigned NumArgsAsWritten = readInt(); 7140 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc); 7141 for (unsigned i = 0; i != NumArgsAsWritten; ++i) 7142 TemplArgsInfo.addArgument(readTemplateArgumentLoc()); 7143 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo); 7144 } 7145 7146 Decl *ASTReader::GetExternalDecl(uint32_t ID) { 7147 return GetDecl(ID); 7148 } 7149 7150 void ASTReader::CompleteRedeclChain(const Decl *D) { 7151 if (NumCurrentElementsDeserializing) { 7152 // We arrange to not care about the complete redeclaration chain while we're 7153 // deserializing. Just remember that the AST has marked this one as complete 7154 // but that it's not actually complete yet, so we know we still need to 7155 // complete it later. 7156 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D)); 7157 return; 7158 } 7159 7160 const DeclContext *DC = D->getDeclContext()->getRedeclContext(); 7161 7162 // If this is a named declaration, complete it by looking it up 7163 // within its context. 7164 // 7165 // FIXME: Merging a function definition should merge 7166 // all mergeable entities within it. 7167 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) || 7168 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) { 7169 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) { 7170 if (!getContext().getLangOpts().CPlusPlus && 7171 isa<TranslationUnitDecl>(DC)) { 7172 // Outside of C++, we don't have a lookup table for the TU, so update 7173 // the identifier instead. (For C++ modules, we don't store decls 7174 // in the serialized identifier table, so we do the lookup in the TU.) 7175 auto *II = Name.getAsIdentifierInfo(); 7176 assert(II && "non-identifier name in C?"); 7177 if (II->isOutOfDate()) 7178 updateOutOfDateIdentifier(*II); 7179 } else 7180 DC->lookup(Name); 7181 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) { 7182 // Find all declarations of this kind from the relevant context. 7183 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) { 7184 auto *DC = cast<DeclContext>(DCDecl); 7185 SmallVector<Decl*, 8> Decls; 7186 FindExternalLexicalDecls( 7187 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls); 7188 } 7189 } 7190 } 7191 7192 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) 7193 CTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7194 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) 7195 VTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7196 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 7197 if (auto *Template = FD->getPrimaryTemplate()) 7198 Template->LoadLazySpecializations(); 7199 } 7200 } 7201 7202 CXXCtorInitializer ** 7203 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) { 7204 RecordLocation Loc = getLocalBitOffset(Offset); 7205 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7206 SavedStreamPosition SavedPosition(Cursor); 7207 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7208 Error(std::move(Err)); 7209 return nullptr; 7210 } 7211 ReadingKindTracker ReadingKind(Read_Decl, *this); 7212 7213 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7214 if (!MaybeCode) { 7215 Error(MaybeCode.takeError()); 7216 return nullptr; 7217 } 7218 unsigned Code = MaybeCode.get(); 7219 7220 ASTRecordReader Record(*this, *Loc.F); 7221 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7222 if (!MaybeRecCode) { 7223 Error(MaybeRecCode.takeError()); 7224 return nullptr; 7225 } 7226 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) { 7227 Error("malformed AST file: missing C++ ctor initializers"); 7228 return nullptr; 7229 } 7230 7231 return Record.readCXXCtorInitializers(); 7232 } 7233 7234 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) { 7235 assert(ContextObj && "reading base specifiers with no AST context"); 7236 ASTContext &Context = *ContextObj; 7237 7238 RecordLocation Loc = getLocalBitOffset(Offset); 7239 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7240 SavedStreamPosition SavedPosition(Cursor); 7241 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7242 Error(std::move(Err)); 7243 return nullptr; 7244 } 7245 ReadingKindTracker ReadingKind(Read_Decl, *this); 7246 7247 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7248 if (!MaybeCode) { 7249 Error(MaybeCode.takeError()); 7250 return nullptr; 7251 } 7252 unsigned Code = MaybeCode.get(); 7253 7254 ASTRecordReader Record(*this, *Loc.F); 7255 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7256 if (!MaybeRecCode) { 7257 Error(MaybeCode.takeError()); 7258 return nullptr; 7259 } 7260 unsigned RecCode = MaybeRecCode.get(); 7261 7262 if (RecCode != DECL_CXX_BASE_SPECIFIERS) { 7263 Error("malformed AST file: missing C++ base specifiers"); 7264 return nullptr; 7265 } 7266 7267 unsigned NumBases = Record.readInt(); 7268 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases); 7269 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases]; 7270 for (unsigned I = 0; I != NumBases; ++I) 7271 Bases[I] = Record.readCXXBaseSpecifier(); 7272 return Bases; 7273 } 7274 7275 serialization::DeclID 7276 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const { 7277 if (LocalID < NUM_PREDEF_DECL_IDS) 7278 return LocalID; 7279 7280 if (!F.ModuleOffsetMap.empty()) 7281 ReadModuleOffsetMap(F); 7282 7283 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7284 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS); 7285 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap"); 7286 7287 return LocalID + I->second; 7288 } 7289 7290 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID, 7291 ModuleFile &M) const { 7292 // Predefined decls aren't from any module. 7293 if (ID < NUM_PREDEF_DECL_IDS) 7294 return false; 7295 7296 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID && 7297 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls; 7298 } 7299 7300 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) { 7301 if (!D->isFromASTFile()) 7302 return nullptr; 7303 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID()); 7304 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7305 return I->second; 7306 } 7307 7308 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) { 7309 if (ID < NUM_PREDEF_DECL_IDS) 7310 return SourceLocation(); 7311 7312 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7313 7314 if (Index > DeclsLoaded.size()) { 7315 Error("declaration ID out-of-range for AST file"); 7316 return SourceLocation(); 7317 } 7318 7319 if (Decl *D = DeclsLoaded[Index]) 7320 return D->getLocation(); 7321 7322 SourceLocation Loc; 7323 DeclCursorForID(ID, Loc); 7324 return Loc; 7325 } 7326 7327 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) { 7328 switch (ID) { 7329 case PREDEF_DECL_NULL_ID: 7330 return nullptr; 7331 7332 case PREDEF_DECL_TRANSLATION_UNIT_ID: 7333 return Context.getTranslationUnitDecl(); 7334 7335 case PREDEF_DECL_OBJC_ID_ID: 7336 return Context.getObjCIdDecl(); 7337 7338 case PREDEF_DECL_OBJC_SEL_ID: 7339 return Context.getObjCSelDecl(); 7340 7341 case PREDEF_DECL_OBJC_CLASS_ID: 7342 return Context.getObjCClassDecl(); 7343 7344 case PREDEF_DECL_OBJC_PROTOCOL_ID: 7345 return Context.getObjCProtocolDecl(); 7346 7347 case PREDEF_DECL_INT_128_ID: 7348 return Context.getInt128Decl(); 7349 7350 case PREDEF_DECL_UNSIGNED_INT_128_ID: 7351 return Context.getUInt128Decl(); 7352 7353 case PREDEF_DECL_OBJC_INSTANCETYPE_ID: 7354 return Context.getObjCInstanceTypeDecl(); 7355 7356 case PREDEF_DECL_BUILTIN_VA_LIST_ID: 7357 return Context.getBuiltinVaListDecl(); 7358 7359 case PREDEF_DECL_VA_LIST_TAG: 7360 return Context.getVaListTagDecl(); 7361 7362 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID: 7363 return Context.getBuiltinMSVaListDecl(); 7364 7365 case PREDEF_DECL_BUILTIN_MS_GUID_ID: 7366 return Context.getMSGuidTagDecl(); 7367 7368 case PREDEF_DECL_EXTERN_C_CONTEXT_ID: 7369 return Context.getExternCContextDecl(); 7370 7371 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID: 7372 return Context.getMakeIntegerSeqDecl(); 7373 7374 case PREDEF_DECL_CF_CONSTANT_STRING_ID: 7375 return Context.getCFConstantStringDecl(); 7376 7377 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID: 7378 return Context.getCFConstantStringTagDecl(); 7379 7380 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID: 7381 return Context.getTypePackElementDecl(); 7382 } 7383 llvm_unreachable("PredefinedDeclIDs unknown enum value"); 7384 } 7385 7386 Decl *ASTReader::GetExistingDecl(DeclID ID) { 7387 assert(ContextObj && "reading decl with no AST context"); 7388 if (ID < NUM_PREDEF_DECL_IDS) { 7389 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID); 7390 if (D) { 7391 // Track that we have merged the declaration with ID \p ID into the 7392 // pre-existing predefined declaration \p D. 7393 auto &Merged = KeyDecls[D->getCanonicalDecl()]; 7394 if (Merged.empty()) 7395 Merged.push_back(ID); 7396 } 7397 return D; 7398 } 7399 7400 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7401 7402 if (Index >= DeclsLoaded.size()) { 7403 assert(0 && "declaration ID out-of-range for AST file"); 7404 Error("declaration ID out-of-range for AST file"); 7405 return nullptr; 7406 } 7407 7408 return DeclsLoaded[Index]; 7409 } 7410 7411 Decl *ASTReader::GetDecl(DeclID ID) { 7412 if (ID < NUM_PREDEF_DECL_IDS) 7413 return GetExistingDecl(ID); 7414 7415 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7416 7417 if (Index >= DeclsLoaded.size()) { 7418 assert(0 && "declaration ID out-of-range for AST file"); 7419 Error("declaration ID out-of-range for AST file"); 7420 return nullptr; 7421 } 7422 7423 if (!DeclsLoaded[Index]) { 7424 ReadDeclRecord(ID); 7425 if (DeserializationListener) 7426 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); 7427 } 7428 7429 return DeclsLoaded[Index]; 7430 } 7431 7432 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M, 7433 DeclID GlobalID) { 7434 if (GlobalID < NUM_PREDEF_DECL_IDS) 7435 return GlobalID; 7436 7437 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID); 7438 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7439 ModuleFile *Owner = I->second; 7440 7441 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos 7442 = M.GlobalToLocalDeclIDs.find(Owner); 7443 if (Pos == M.GlobalToLocalDeclIDs.end()) 7444 return 0; 7445 7446 return GlobalID - Owner->BaseDeclID + Pos->second; 7447 } 7448 7449 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F, 7450 const RecordData &Record, 7451 unsigned &Idx) { 7452 if (Idx >= Record.size()) { 7453 Error("Corrupted AST file"); 7454 return 0; 7455 } 7456 7457 return getGlobalDeclID(F, Record[Idx++]); 7458 } 7459 7460 /// Resolve the offset of a statement into a statement. 7461 /// 7462 /// This operation will read a new statement from the external 7463 /// source each time it is called, and is meant to be used via a 7464 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 7465 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) { 7466 // Switch case IDs are per Decl. 7467 ClearSwitchCaseIDs(); 7468 7469 // Offset here is a global offset across the entire chain. 7470 RecordLocation Loc = getLocalBitOffset(Offset); 7471 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) { 7472 Error(std::move(Err)); 7473 return nullptr; 7474 } 7475 assert(NumCurrentElementsDeserializing == 0 && 7476 "should not be called while already deserializing"); 7477 Deserializing D(this); 7478 return ReadStmtFromStream(*Loc.F); 7479 } 7480 7481 void ASTReader::FindExternalLexicalDecls( 7482 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant, 7483 SmallVectorImpl<Decl *> &Decls) { 7484 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {}; 7485 7486 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) { 7487 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries"); 7488 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) { 7489 auto K = (Decl::Kind)+LexicalDecls[I]; 7490 if (!IsKindWeWant(K)) 7491 continue; 7492 7493 auto ID = (serialization::DeclID)+LexicalDecls[I + 1]; 7494 7495 // Don't add predefined declarations to the lexical context more 7496 // than once. 7497 if (ID < NUM_PREDEF_DECL_IDS) { 7498 if (PredefsVisited[ID]) 7499 continue; 7500 7501 PredefsVisited[ID] = true; 7502 } 7503 7504 if (Decl *D = GetLocalDecl(*M, ID)) { 7505 assert(D->getKind() == K && "wrong kind for lexical decl"); 7506 if (!DC->isDeclInLexicalTraversal(D)) 7507 Decls.push_back(D); 7508 } 7509 } 7510 }; 7511 7512 if (isa<TranslationUnitDecl>(DC)) { 7513 for (auto Lexical : TULexicalDecls) 7514 Visit(Lexical.first, Lexical.second); 7515 } else { 7516 auto I = LexicalDecls.find(DC); 7517 if (I != LexicalDecls.end()) 7518 Visit(I->second.first, I->second.second); 7519 } 7520 7521 ++NumLexicalDeclContextsRead; 7522 } 7523 7524 namespace { 7525 7526 class DeclIDComp { 7527 ASTReader &Reader; 7528 ModuleFile &Mod; 7529 7530 public: 7531 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {} 7532 7533 bool operator()(LocalDeclID L, LocalDeclID R) const { 7534 SourceLocation LHS = getLocation(L); 7535 SourceLocation RHS = getLocation(R); 7536 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7537 } 7538 7539 bool operator()(SourceLocation LHS, LocalDeclID R) const { 7540 SourceLocation RHS = getLocation(R); 7541 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7542 } 7543 7544 bool operator()(LocalDeclID L, SourceLocation RHS) const { 7545 SourceLocation LHS = getLocation(L); 7546 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7547 } 7548 7549 SourceLocation getLocation(LocalDeclID ID) const { 7550 return Reader.getSourceManager().getFileLoc( 7551 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID))); 7552 } 7553 }; 7554 7555 } // namespace 7556 7557 void ASTReader::FindFileRegionDecls(FileID File, 7558 unsigned Offset, unsigned Length, 7559 SmallVectorImpl<Decl *> &Decls) { 7560 SourceManager &SM = getSourceManager(); 7561 7562 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File); 7563 if (I == FileDeclIDs.end()) 7564 return; 7565 7566 FileDeclsInfo &DInfo = I->second; 7567 if (DInfo.Decls.empty()) 7568 return; 7569 7570 SourceLocation 7571 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset); 7572 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length); 7573 7574 DeclIDComp DIDComp(*this, *DInfo.Mod); 7575 ArrayRef<serialization::LocalDeclID>::iterator BeginIt = 7576 llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp); 7577 if (BeginIt != DInfo.Decls.begin()) 7578 --BeginIt; 7579 7580 // If we are pointing at a top-level decl inside an objc container, we need 7581 // to backtrack until we find it otherwise we will fail to report that the 7582 // region overlaps with an objc container. 7583 while (BeginIt != DInfo.Decls.begin() && 7584 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt)) 7585 ->isTopLevelDeclInObjCContainer()) 7586 --BeginIt; 7587 7588 ArrayRef<serialization::LocalDeclID>::iterator EndIt = 7589 llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp); 7590 if (EndIt != DInfo.Decls.end()) 7591 ++EndIt; 7592 7593 for (ArrayRef<serialization::LocalDeclID>::iterator 7594 DIt = BeginIt; DIt != EndIt; ++DIt) 7595 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt))); 7596 } 7597 7598 bool 7599 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, 7600 DeclarationName Name) { 7601 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() && 7602 "DeclContext has no visible decls in storage"); 7603 if (!Name) 7604 return false; 7605 7606 auto It = Lookups.find(DC); 7607 if (It == Lookups.end()) 7608 return false; 7609 7610 Deserializing LookupResults(this); 7611 7612 // Load the list of declarations. 7613 SmallVector<NamedDecl *, 64> Decls; 7614 for (DeclID ID : It->second.Table.find(Name)) { 7615 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7616 if (ND->getDeclName() == Name) 7617 Decls.push_back(ND); 7618 } 7619 7620 ++NumVisibleDeclContextsRead; 7621 SetExternalVisibleDeclsForName(DC, Name, Decls); 7622 return !Decls.empty(); 7623 } 7624 7625 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) { 7626 if (!DC->hasExternalVisibleStorage()) 7627 return; 7628 7629 auto It = Lookups.find(DC); 7630 assert(It != Lookups.end() && 7631 "have external visible storage but no lookup tables"); 7632 7633 DeclsMap Decls; 7634 7635 for (DeclID ID : It->second.Table.findAll()) { 7636 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7637 Decls[ND->getDeclName()].push_back(ND); 7638 } 7639 7640 ++NumVisibleDeclContextsRead; 7641 7642 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) { 7643 SetExternalVisibleDeclsForName(DC, I->first, I->second); 7644 } 7645 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false); 7646 } 7647 7648 const serialization::reader::DeclContextLookupTable * 7649 ASTReader::getLoadedLookupTables(DeclContext *Primary) const { 7650 auto I = Lookups.find(Primary); 7651 return I == Lookups.end() ? nullptr : &I->second; 7652 } 7653 7654 /// Under non-PCH compilation the consumer receives the objc methods 7655 /// before receiving the implementation, and codegen depends on this. 7656 /// We simulate this by deserializing and passing to consumer the methods of the 7657 /// implementation before passing the deserialized implementation decl. 7658 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD, 7659 ASTConsumer *Consumer) { 7660 assert(ImplD && Consumer); 7661 7662 for (auto *I : ImplD->methods()) 7663 Consumer->HandleInterestingDecl(DeclGroupRef(I)); 7664 7665 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD)); 7666 } 7667 7668 void ASTReader::PassInterestingDeclToConsumer(Decl *D) { 7669 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) 7670 PassObjCImplDeclToConsumer(ImplD, Consumer); 7671 else 7672 Consumer->HandleInterestingDecl(DeclGroupRef(D)); 7673 } 7674 7675 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) { 7676 this->Consumer = Consumer; 7677 7678 if (Consumer) 7679 PassInterestingDeclsToConsumer(); 7680 7681 if (DeserializationListener) 7682 DeserializationListener->ReaderInitialized(this); 7683 } 7684 7685 void ASTReader::PrintStats() { 7686 std::fprintf(stderr, "*** AST File Statistics:\n"); 7687 7688 unsigned NumTypesLoaded 7689 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), 7690 QualType()); 7691 unsigned NumDeclsLoaded 7692 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), 7693 (Decl *)nullptr); 7694 unsigned NumIdentifiersLoaded 7695 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), 7696 IdentifiersLoaded.end(), 7697 (IdentifierInfo *)nullptr); 7698 unsigned NumMacrosLoaded 7699 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(), 7700 MacrosLoaded.end(), 7701 (MacroInfo *)nullptr); 7702 unsigned NumSelectorsLoaded 7703 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), 7704 SelectorsLoaded.end(), 7705 Selector()); 7706 7707 if (unsigned TotalNumSLocEntries = getTotalNumSLocs()) 7708 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", 7709 NumSLocEntriesRead, TotalNumSLocEntries, 7710 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); 7711 if (!TypesLoaded.empty()) 7712 std::fprintf(stderr, " %u/%u types read (%f%%)\n", 7713 NumTypesLoaded, (unsigned)TypesLoaded.size(), 7714 ((float)NumTypesLoaded/TypesLoaded.size() * 100)); 7715 if (!DeclsLoaded.empty()) 7716 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", 7717 NumDeclsLoaded, (unsigned)DeclsLoaded.size(), 7718 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); 7719 if (!IdentifiersLoaded.empty()) 7720 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", 7721 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), 7722 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); 7723 if (!MacrosLoaded.empty()) 7724 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7725 NumMacrosLoaded, (unsigned)MacrosLoaded.size(), 7726 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100)); 7727 if (!SelectorsLoaded.empty()) 7728 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", 7729 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), 7730 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); 7731 if (TotalNumStatements) 7732 std::fprintf(stderr, " %u/%u statements read (%f%%)\n", 7733 NumStatementsRead, TotalNumStatements, 7734 ((float)NumStatementsRead/TotalNumStatements * 100)); 7735 if (TotalNumMacros) 7736 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7737 NumMacrosRead, TotalNumMacros, 7738 ((float)NumMacrosRead/TotalNumMacros * 100)); 7739 if (TotalLexicalDeclContexts) 7740 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", 7741 NumLexicalDeclContextsRead, TotalLexicalDeclContexts, 7742 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts 7743 * 100)); 7744 if (TotalVisibleDeclContexts) 7745 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", 7746 NumVisibleDeclContextsRead, TotalVisibleDeclContexts, 7747 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts 7748 * 100)); 7749 if (TotalNumMethodPoolEntries) 7750 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", 7751 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, 7752 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries 7753 * 100)); 7754 if (NumMethodPoolLookups) 7755 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n", 7756 NumMethodPoolHits, NumMethodPoolLookups, 7757 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0)); 7758 if (NumMethodPoolTableLookups) 7759 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n", 7760 NumMethodPoolTableHits, NumMethodPoolTableLookups, 7761 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups 7762 * 100.0)); 7763 if (NumIdentifierLookupHits) 7764 std::fprintf(stderr, 7765 " %u / %u identifier table lookups succeeded (%f%%)\n", 7766 NumIdentifierLookupHits, NumIdentifierLookups, 7767 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups); 7768 7769 if (GlobalIndex) { 7770 std::fprintf(stderr, "\n"); 7771 GlobalIndex->printStats(); 7772 } 7773 7774 std::fprintf(stderr, "\n"); 7775 dump(); 7776 std::fprintf(stderr, "\n"); 7777 } 7778 7779 template<typename Key, typename ModuleFile, unsigned InitialCapacity> 7780 LLVM_DUMP_METHOD static void 7781 dumpModuleIDMap(StringRef Name, 7782 const ContinuousRangeMap<Key, ModuleFile *, 7783 InitialCapacity> &Map) { 7784 if (Map.begin() == Map.end()) 7785 return; 7786 7787 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>; 7788 7789 llvm::errs() << Name << ":\n"; 7790 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end(); 7791 I != IEnd; ++I) { 7792 llvm::errs() << " " << I->first << " -> " << I->second->FileName 7793 << "\n"; 7794 } 7795 } 7796 7797 LLVM_DUMP_METHOD void ASTReader::dump() { 7798 llvm::errs() << "*** PCH/ModuleFile Remappings:\n"; 7799 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap); 7800 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap); 7801 dumpModuleIDMap("Global type map", GlobalTypeMap); 7802 dumpModuleIDMap("Global declaration map", GlobalDeclMap); 7803 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap); 7804 dumpModuleIDMap("Global macro map", GlobalMacroMap); 7805 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap); 7806 dumpModuleIDMap("Global selector map", GlobalSelectorMap); 7807 dumpModuleIDMap("Global preprocessed entity map", 7808 GlobalPreprocessedEntityMap); 7809 7810 llvm::errs() << "\n*** PCH/Modules Loaded:"; 7811 for (ModuleFile &M : ModuleMgr) 7812 M.dump(); 7813 } 7814 7815 /// Return the amount of memory used by memory buffers, breaking down 7816 /// by heap-backed versus mmap'ed memory. 7817 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const { 7818 for (ModuleFile &I : ModuleMgr) { 7819 if (llvm::MemoryBuffer *buf = I.Buffer) { 7820 size_t bytes = buf->getBufferSize(); 7821 switch (buf->getBufferKind()) { 7822 case llvm::MemoryBuffer::MemoryBuffer_Malloc: 7823 sizes.malloc_bytes += bytes; 7824 break; 7825 case llvm::MemoryBuffer::MemoryBuffer_MMap: 7826 sizes.mmap_bytes += bytes; 7827 break; 7828 } 7829 } 7830 } 7831 } 7832 7833 void ASTReader::InitializeSema(Sema &S) { 7834 SemaObj = &S; 7835 S.addExternalSource(this); 7836 7837 // Makes sure any declarations that were deserialized "too early" 7838 // still get added to the identifier's declaration chains. 7839 for (uint64_t ID : PreloadedDeclIDs) { 7840 NamedDecl *D = cast<NamedDecl>(GetDecl(ID)); 7841 pushExternalDeclIntoScope(D, D->getDeclName()); 7842 } 7843 PreloadedDeclIDs.clear(); 7844 7845 // FIXME: What happens if these are changed by a module import? 7846 if (!FPPragmaOptions.empty()) { 7847 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS"); 7848 FPOptionsOverride NewOverrides = 7849 FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]); 7850 SemaObj->CurFPFeatures = 7851 NewOverrides.applyOverrides(SemaObj->getLangOpts()); 7852 } 7853 7854 SemaObj->OpenCLFeatures.copy(OpenCLExtensions); 7855 SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap; 7856 SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap; 7857 7858 UpdateSema(); 7859 } 7860 7861 void ASTReader::UpdateSema() { 7862 assert(SemaObj && "no Sema to update"); 7863 7864 // Load the offsets of the declarations that Sema references. 7865 // They will be lazily deserialized when needed. 7866 if (!SemaDeclRefs.empty()) { 7867 assert(SemaDeclRefs.size() % 3 == 0); 7868 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) { 7869 if (!SemaObj->StdNamespace) 7870 SemaObj->StdNamespace = SemaDeclRefs[I]; 7871 if (!SemaObj->StdBadAlloc) 7872 SemaObj->StdBadAlloc = SemaDeclRefs[I+1]; 7873 if (!SemaObj->StdAlignValT) 7874 SemaObj->StdAlignValT = SemaDeclRefs[I+2]; 7875 } 7876 SemaDeclRefs.clear(); 7877 } 7878 7879 // Update the state of pragmas. Use the same API as if we had encountered the 7880 // pragma in the source. 7881 if(OptimizeOffPragmaLocation.isValid()) 7882 SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation); 7883 if (PragmaMSStructState != -1) 7884 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState); 7885 if (PointersToMembersPragmaLocation.isValid()) { 7886 SemaObj->ActOnPragmaMSPointersToMembers( 7887 (LangOptions::PragmaMSPointersToMembersKind) 7888 PragmaMSPointersToMembersState, 7889 PointersToMembersPragmaLocation); 7890 } 7891 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth; 7892 7893 if (PragmaPackCurrentValue) { 7894 // The bottom of the stack might have a default value. It must be adjusted 7895 // to the current value to ensure that the packing state is preserved after 7896 // popping entries that were included/imported from a PCH/module. 7897 bool DropFirst = false; 7898 if (!PragmaPackStack.empty() && 7899 PragmaPackStack.front().Location.isInvalid()) { 7900 assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue && 7901 "Expected a default alignment value"); 7902 SemaObj->PackStack.Stack.emplace_back( 7903 PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue, 7904 SemaObj->PackStack.CurrentPragmaLocation, 7905 PragmaPackStack.front().PushLocation); 7906 DropFirst = true; 7907 } 7908 for (const auto &Entry : 7909 llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0)) 7910 SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value, 7911 Entry.Location, Entry.PushLocation); 7912 if (PragmaPackCurrentLocation.isInvalid()) { 7913 assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue && 7914 "Expected a default alignment value"); 7915 // Keep the current values. 7916 } else { 7917 SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue; 7918 SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation; 7919 } 7920 } 7921 if (FpPragmaCurrentValue) { 7922 // The bottom of the stack might have a default value. It must be adjusted 7923 // to the current value to ensure that fp-pragma state is preserved after 7924 // popping entries that were included/imported from a PCH/module. 7925 bool DropFirst = false; 7926 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) { 7927 assert(FpPragmaStack.front().Value == 7928 SemaObj->FpPragmaStack.DefaultValue && 7929 "Expected a default pragma float_control value"); 7930 SemaObj->FpPragmaStack.Stack.emplace_back( 7931 FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue, 7932 SemaObj->FpPragmaStack.CurrentPragmaLocation, 7933 FpPragmaStack.front().PushLocation); 7934 DropFirst = true; 7935 } 7936 for (const auto &Entry : 7937 llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0)) 7938 SemaObj->FpPragmaStack.Stack.emplace_back( 7939 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation); 7940 if (FpPragmaCurrentLocation.isInvalid()) { 7941 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue && 7942 "Expected a default pragma float_control value"); 7943 // Keep the current values. 7944 } else { 7945 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue; 7946 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation; 7947 } 7948 } 7949 7950 // For non-modular AST files, restore visiblity of modules. 7951 for (auto &Import : ImportedModules) { 7952 if (Import.ImportLoc.isInvalid()) 7953 continue; 7954 if (Module *Imported = getSubmodule(Import.ID)) { 7955 SemaObj->makeModuleVisible(Imported, Import.ImportLoc); 7956 } 7957 } 7958 } 7959 7960 IdentifierInfo *ASTReader::get(StringRef Name) { 7961 // Note that we are loading an identifier. 7962 Deserializing AnIdentifier(this); 7963 7964 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0, 7965 NumIdentifierLookups, 7966 NumIdentifierLookupHits); 7967 7968 // We don't need to do identifier table lookups in C++ modules (we preload 7969 // all interesting declarations, and don't need to use the scope for name 7970 // lookups). Perform the lookup in PCH files, though, since we don't build 7971 // a complete initial identifier table if we're carrying on from a PCH. 7972 if (PP.getLangOpts().CPlusPlus) { 7973 for (auto F : ModuleMgr.pch_modules()) 7974 if (Visitor(*F)) 7975 break; 7976 } else { 7977 // If there is a global index, look there first to determine which modules 7978 // provably do not have any results for this identifier. 7979 GlobalModuleIndex::HitSet Hits; 7980 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 7981 if (!loadGlobalIndex()) { 7982 if (GlobalIndex->lookupIdentifier(Name, Hits)) { 7983 HitsPtr = &Hits; 7984 } 7985 } 7986 7987 ModuleMgr.visit(Visitor, HitsPtr); 7988 } 7989 7990 IdentifierInfo *II = Visitor.getIdentifierInfo(); 7991 markIdentifierUpToDate(II); 7992 return II; 7993 } 7994 7995 namespace clang { 7996 7997 /// An identifier-lookup iterator that enumerates all of the 7998 /// identifiers stored within a set of AST files. 7999 class ASTIdentifierIterator : public IdentifierIterator { 8000 /// The AST reader whose identifiers are being enumerated. 8001 const ASTReader &Reader; 8002 8003 /// The current index into the chain of AST files stored in 8004 /// the AST reader. 8005 unsigned Index; 8006 8007 /// The current position within the identifier lookup table 8008 /// of the current AST file. 8009 ASTIdentifierLookupTable::key_iterator Current; 8010 8011 /// The end position within the identifier lookup table of 8012 /// the current AST file. 8013 ASTIdentifierLookupTable::key_iterator End; 8014 8015 /// Whether to skip any modules in the ASTReader. 8016 bool SkipModules; 8017 8018 public: 8019 explicit ASTIdentifierIterator(const ASTReader &Reader, 8020 bool SkipModules = false); 8021 8022 StringRef Next() override; 8023 }; 8024 8025 } // namespace clang 8026 8027 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader, 8028 bool SkipModules) 8029 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) { 8030 } 8031 8032 StringRef ASTIdentifierIterator::Next() { 8033 while (Current == End) { 8034 // If we have exhausted all of our AST files, we're done. 8035 if (Index == 0) 8036 return StringRef(); 8037 8038 --Index; 8039 ModuleFile &F = Reader.ModuleMgr[Index]; 8040 if (SkipModules && F.isModule()) 8041 continue; 8042 8043 ASTIdentifierLookupTable *IdTable = 8044 (ASTIdentifierLookupTable *)F.IdentifierLookupTable; 8045 Current = IdTable->key_begin(); 8046 End = IdTable->key_end(); 8047 } 8048 8049 // We have any identifiers remaining in the current AST file; return 8050 // the next one. 8051 StringRef Result = *Current; 8052 ++Current; 8053 return Result; 8054 } 8055 8056 namespace { 8057 8058 /// A utility for appending two IdentifierIterators. 8059 class ChainedIdentifierIterator : public IdentifierIterator { 8060 std::unique_ptr<IdentifierIterator> Current; 8061 std::unique_ptr<IdentifierIterator> Queued; 8062 8063 public: 8064 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First, 8065 std::unique_ptr<IdentifierIterator> Second) 8066 : Current(std::move(First)), Queued(std::move(Second)) {} 8067 8068 StringRef Next() override { 8069 if (!Current) 8070 return StringRef(); 8071 8072 StringRef result = Current->Next(); 8073 if (!result.empty()) 8074 return result; 8075 8076 // Try the queued iterator, which may itself be empty. 8077 Current.reset(); 8078 std::swap(Current, Queued); 8079 return Next(); 8080 } 8081 }; 8082 8083 } // namespace 8084 8085 IdentifierIterator *ASTReader::getIdentifiers() { 8086 if (!loadGlobalIndex()) { 8087 std::unique_ptr<IdentifierIterator> ReaderIter( 8088 new ASTIdentifierIterator(*this, /*SkipModules=*/true)); 8089 std::unique_ptr<IdentifierIterator> ModulesIter( 8090 GlobalIndex->createIdentifierIterator()); 8091 return new ChainedIdentifierIterator(std::move(ReaderIter), 8092 std::move(ModulesIter)); 8093 } 8094 8095 return new ASTIdentifierIterator(*this); 8096 } 8097 8098 namespace clang { 8099 namespace serialization { 8100 8101 class ReadMethodPoolVisitor { 8102 ASTReader &Reader; 8103 Selector Sel; 8104 unsigned PriorGeneration; 8105 unsigned InstanceBits = 0; 8106 unsigned FactoryBits = 0; 8107 bool InstanceHasMoreThanOneDecl = false; 8108 bool FactoryHasMoreThanOneDecl = false; 8109 SmallVector<ObjCMethodDecl *, 4> InstanceMethods; 8110 SmallVector<ObjCMethodDecl *, 4> FactoryMethods; 8111 8112 public: 8113 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel, 8114 unsigned PriorGeneration) 8115 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {} 8116 8117 bool operator()(ModuleFile &M) { 8118 if (!M.SelectorLookupTable) 8119 return false; 8120 8121 // If we've already searched this module file, skip it now. 8122 if (M.Generation <= PriorGeneration) 8123 return true; 8124 8125 ++Reader.NumMethodPoolTableLookups; 8126 ASTSelectorLookupTable *PoolTable 8127 = (ASTSelectorLookupTable*)M.SelectorLookupTable; 8128 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel); 8129 if (Pos == PoolTable->end()) 8130 return false; 8131 8132 ++Reader.NumMethodPoolTableHits; 8133 ++Reader.NumSelectorsRead; 8134 // FIXME: Not quite happy with the statistics here. We probably should 8135 // disable this tracking when called via LoadSelector. 8136 // Also, should entries without methods count as misses? 8137 ++Reader.NumMethodPoolEntriesRead; 8138 ASTSelectorLookupTrait::data_type Data = *Pos; 8139 if (Reader.DeserializationListener) 8140 Reader.DeserializationListener->SelectorRead(Data.ID, Sel); 8141 8142 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end()); 8143 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end()); 8144 InstanceBits = Data.InstanceBits; 8145 FactoryBits = Data.FactoryBits; 8146 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl; 8147 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl; 8148 return true; 8149 } 8150 8151 /// Retrieve the instance methods found by this visitor. 8152 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const { 8153 return InstanceMethods; 8154 } 8155 8156 /// Retrieve the instance methods found by this visitor. 8157 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const { 8158 return FactoryMethods; 8159 } 8160 8161 unsigned getInstanceBits() const { return InstanceBits; } 8162 unsigned getFactoryBits() const { return FactoryBits; } 8163 8164 bool instanceHasMoreThanOneDecl() const { 8165 return InstanceHasMoreThanOneDecl; 8166 } 8167 8168 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; } 8169 }; 8170 8171 } // namespace serialization 8172 } // namespace clang 8173 8174 /// Add the given set of methods to the method list. 8175 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods, 8176 ObjCMethodList &List) { 8177 for (unsigned I = 0, N = Methods.size(); I != N; ++I) { 8178 S.addMethodToGlobalList(&List, Methods[I]); 8179 } 8180 } 8181 8182 void ASTReader::ReadMethodPool(Selector Sel) { 8183 // Get the selector generation and update it to the current generation. 8184 unsigned &Generation = SelectorGeneration[Sel]; 8185 unsigned PriorGeneration = Generation; 8186 Generation = getGeneration(); 8187 SelectorOutOfDate[Sel] = false; 8188 8189 // Search for methods defined with this selector. 8190 ++NumMethodPoolLookups; 8191 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration); 8192 ModuleMgr.visit(Visitor); 8193 8194 if (Visitor.getInstanceMethods().empty() && 8195 Visitor.getFactoryMethods().empty()) 8196 return; 8197 8198 ++NumMethodPoolHits; 8199 8200 if (!getSema()) 8201 return; 8202 8203 Sema &S = *getSema(); 8204 Sema::GlobalMethodPool::iterator Pos 8205 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first; 8206 8207 Pos->second.first.setBits(Visitor.getInstanceBits()); 8208 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl()); 8209 Pos->second.second.setBits(Visitor.getFactoryBits()); 8210 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl()); 8211 8212 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since 8213 // when building a module we keep every method individually and may need to 8214 // update hasMoreThanOneDecl as we add the methods. 8215 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first); 8216 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second); 8217 } 8218 8219 void ASTReader::updateOutOfDateSelector(Selector Sel) { 8220 if (SelectorOutOfDate[Sel]) 8221 ReadMethodPool(Sel); 8222 } 8223 8224 void ASTReader::ReadKnownNamespaces( 8225 SmallVectorImpl<NamespaceDecl *> &Namespaces) { 8226 Namespaces.clear(); 8227 8228 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) { 8229 if (NamespaceDecl *Namespace 8230 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I]))) 8231 Namespaces.push_back(Namespace); 8232 } 8233 } 8234 8235 void ASTReader::ReadUndefinedButUsed( 8236 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) { 8237 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) { 8238 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++])); 8239 SourceLocation Loc = 8240 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]); 8241 Undefined.insert(std::make_pair(D, Loc)); 8242 } 8243 } 8244 8245 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector< 8246 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> & 8247 Exprs) { 8248 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) { 8249 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++])); 8250 uint64_t Count = DelayedDeleteExprs[Idx++]; 8251 for (uint64_t C = 0; C < Count; ++C) { 8252 SourceLocation DeleteLoc = 8253 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]); 8254 const bool IsArrayForm = DelayedDeleteExprs[Idx++]; 8255 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm)); 8256 } 8257 } 8258 } 8259 8260 void ASTReader::ReadTentativeDefinitions( 8261 SmallVectorImpl<VarDecl *> &TentativeDefs) { 8262 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { 8263 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I])); 8264 if (Var) 8265 TentativeDefs.push_back(Var); 8266 } 8267 TentativeDefinitions.clear(); 8268 } 8269 8270 void ASTReader::ReadUnusedFileScopedDecls( 8271 SmallVectorImpl<const DeclaratorDecl *> &Decls) { 8272 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) { 8273 DeclaratorDecl *D 8274 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I])); 8275 if (D) 8276 Decls.push_back(D); 8277 } 8278 UnusedFileScopedDecls.clear(); 8279 } 8280 8281 void ASTReader::ReadDelegatingConstructors( 8282 SmallVectorImpl<CXXConstructorDecl *> &Decls) { 8283 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) { 8284 CXXConstructorDecl *D 8285 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I])); 8286 if (D) 8287 Decls.push_back(D); 8288 } 8289 DelegatingCtorDecls.clear(); 8290 } 8291 8292 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) { 8293 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) { 8294 TypedefNameDecl *D 8295 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])); 8296 if (D) 8297 Decls.push_back(D); 8298 } 8299 ExtVectorDecls.clear(); 8300 } 8301 8302 void ASTReader::ReadUnusedLocalTypedefNameCandidates( 8303 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) { 8304 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N; 8305 ++I) { 8306 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>( 8307 GetDecl(UnusedLocalTypedefNameCandidates[I])); 8308 if (D) 8309 Decls.insert(D); 8310 } 8311 UnusedLocalTypedefNameCandidates.clear(); 8312 } 8313 8314 void ASTReader::ReadDeclsToCheckForDeferredDiags( 8315 llvm::SmallVector<Decl *, 4> &Decls) { 8316 for (unsigned I = 0, N = DeclsToCheckForDeferredDiags.size(); I != N; 8317 ++I) { 8318 auto *D = dyn_cast_or_null<Decl>( 8319 GetDecl(DeclsToCheckForDeferredDiags[I])); 8320 if (D) 8321 Decls.push_back(D); 8322 } 8323 DeclsToCheckForDeferredDiags.clear(); 8324 } 8325 8326 8327 void ASTReader::ReadReferencedSelectors( 8328 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) { 8329 if (ReferencedSelectorsData.empty()) 8330 return; 8331 8332 // If there are @selector references added them to its pool. This is for 8333 // implementation of -Wselector. 8334 unsigned int DataSize = ReferencedSelectorsData.size()-1; 8335 unsigned I = 0; 8336 while (I < DataSize) { 8337 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]); 8338 SourceLocation SelLoc 8339 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]); 8340 Sels.push_back(std::make_pair(Sel, SelLoc)); 8341 } 8342 ReferencedSelectorsData.clear(); 8343 } 8344 8345 void ASTReader::ReadWeakUndeclaredIdentifiers( 8346 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) { 8347 if (WeakUndeclaredIdentifiers.empty()) 8348 return; 8349 8350 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) { 8351 IdentifierInfo *WeakId 8352 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8353 IdentifierInfo *AliasId 8354 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8355 SourceLocation Loc 8356 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]); 8357 bool Used = WeakUndeclaredIdentifiers[I++]; 8358 WeakInfo WI(AliasId, Loc); 8359 WI.setUsed(Used); 8360 WeakIDs.push_back(std::make_pair(WeakId, WI)); 8361 } 8362 WeakUndeclaredIdentifiers.clear(); 8363 } 8364 8365 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) { 8366 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) { 8367 ExternalVTableUse VT; 8368 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); 8369 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]); 8370 VT.DefinitionRequired = VTableUses[Idx++]; 8371 VTables.push_back(VT); 8372 } 8373 8374 VTableUses.clear(); 8375 } 8376 8377 void ASTReader::ReadPendingInstantiations( 8378 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) { 8379 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) { 8380 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++])); 8381 SourceLocation Loc 8382 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]); 8383 8384 Pending.push_back(std::make_pair(D, Loc)); 8385 } 8386 PendingInstantiations.clear(); 8387 } 8388 8389 void ASTReader::ReadLateParsedTemplates( 8390 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>> 8391 &LPTMap) { 8392 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N; 8393 /* In loop */) { 8394 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++])); 8395 8396 auto LT = std::make_unique<LateParsedTemplate>(); 8397 LT->D = GetDecl(LateParsedTemplates[Idx++]); 8398 8399 ModuleFile *F = getOwningModuleFile(LT->D); 8400 assert(F && "No module"); 8401 8402 unsigned TokN = LateParsedTemplates[Idx++]; 8403 LT->Toks.reserve(TokN); 8404 for (unsigned T = 0; T < TokN; ++T) 8405 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx)); 8406 8407 LPTMap.insert(std::make_pair(FD, std::move(LT))); 8408 } 8409 8410 LateParsedTemplates.clear(); 8411 } 8412 8413 void ASTReader::LoadSelector(Selector Sel) { 8414 // It would be complicated to avoid reading the methods anyway. So don't. 8415 ReadMethodPool(Sel); 8416 } 8417 8418 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) { 8419 assert(ID && "Non-zero identifier ID required"); 8420 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); 8421 IdentifiersLoaded[ID - 1] = II; 8422 if (DeserializationListener) 8423 DeserializationListener->IdentifierRead(ID, II); 8424 } 8425 8426 /// Set the globally-visible declarations associated with the given 8427 /// identifier. 8428 /// 8429 /// If the AST reader is currently in a state where the given declaration IDs 8430 /// cannot safely be resolved, they are queued until it is safe to resolve 8431 /// them. 8432 /// 8433 /// \param II an IdentifierInfo that refers to one or more globally-visible 8434 /// declarations. 8435 /// 8436 /// \param DeclIDs the set of declaration IDs with the name @p II that are 8437 /// visible at global scope. 8438 /// 8439 /// \param Decls if non-null, this vector will be populated with the set of 8440 /// deserialized declarations. These declarations will not be pushed into 8441 /// scope. 8442 void 8443 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, 8444 const SmallVectorImpl<uint32_t> &DeclIDs, 8445 SmallVectorImpl<Decl *> *Decls) { 8446 if (NumCurrentElementsDeserializing && !Decls) { 8447 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end()); 8448 return; 8449 } 8450 8451 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { 8452 if (!SemaObj) { 8453 // Queue this declaration so that it will be added to the 8454 // translation unit scope and identifier's declaration chain 8455 // once a Sema object is known. 8456 PreloadedDeclIDs.push_back(DeclIDs[I]); 8457 continue; 8458 } 8459 8460 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); 8461 8462 // If we're simply supposed to record the declarations, do so now. 8463 if (Decls) { 8464 Decls->push_back(D); 8465 continue; 8466 } 8467 8468 // Introduce this declaration into the translation-unit scope 8469 // and add it to the declaration chain for this identifier, so 8470 // that (unqualified) name lookup will find it. 8471 pushExternalDeclIntoScope(D, II); 8472 } 8473 } 8474 8475 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) { 8476 if (ID == 0) 8477 return nullptr; 8478 8479 if (IdentifiersLoaded.empty()) { 8480 Error("no identifier table in AST file"); 8481 return nullptr; 8482 } 8483 8484 ID -= 1; 8485 if (!IdentifiersLoaded[ID]) { 8486 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1); 8487 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map"); 8488 ModuleFile *M = I->second; 8489 unsigned Index = ID - M->BaseIdentifierID; 8490 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index]; 8491 8492 // All of the strings in the AST file are preceded by a 16-bit length. 8493 // Extract that 16-bit length to avoid having to execute strlen(). 8494 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as 8495 // unsigned integers. This is important to avoid integer overflow when 8496 // we cast them to 'unsigned'. 8497 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2; 8498 unsigned StrLen = (((unsigned) StrLenPtr[0]) 8499 | (((unsigned) StrLenPtr[1]) << 8)) - 1; 8500 auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen)); 8501 IdentifiersLoaded[ID] = &II; 8502 markIdentifierFromAST(*this, II); 8503 if (DeserializationListener) 8504 DeserializationListener->IdentifierRead(ID + 1, &II); 8505 } 8506 8507 return IdentifiersLoaded[ID]; 8508 } 8509 8510 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) { 8511 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID)); 8512 } 8513 8514 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) { 8515 if (LocalID < NUM_PREDEF_IDENT_IDS) 8516 return LocalID; 8517 8518 if (!M.ModuleOffsetMap.empty()) 8519 ReadModuleOffsetMap(M); 8520 8521 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8522 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS); 8523 assert(I != M.IdentifierRemap.end() 8524 && "Invalid index into identifier index remap"); 8525 8526 return LocalID + I->second; 8527 } 8528 8529 MacroInfo *ASTReader::getMacro(MacroID ID) { 8530 if (ID == 0) 8531 return nullptr; 8532 8533 if (MacrosLoaded.empty()) { 8534 Error("no macro table in AST file"); 8535 return nullptr; 8536 } 8537 8538 ID -= NUM_PREDEF_MACRO_IDS; 8539 if (!MacrosLoaded[ID]) { 8540 GlobalMacroMapType::iterator I 8541 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS); 8542 assert(I != GlobalMacroMap.end() && "Corrupted global macro map"); 8543 ModuleFile *M = I->second; 8544 unsigned Index = ID - M->BaseMacroID; 8545 MacrosLoaded[ID] = 8546 ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]); 8547 8548 if (DeserializationListener) 8549 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS, 8550 MacrosLoaded[ID]); 8551 } 8552 8553 return MacrosLoaded[ID]; 8554 } 8555 8556 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) { 8557 if (LocalID < NUM_PREDEF_MACRO_IDS) 8558 return LocalID; 8559 8560 if (!M.ModuleOffsetMap.empty()) 8561 ReadModuleOffsetMap(M); 8562 8563 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8564 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS); 8565 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap"); 8566 8567 return LocalID + I->second; 8568 } 8569 8570 serialization::SubmoduleID 8571 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) { 8572 if (LocalID < NUM_PREDEF_SUBMODULE_IDS) 8573 return LocalID; 8574 8575 if (!M.ModuleOffsetMap.empty()) 8576 ReadModuleOffsetMap(M); 8577 8578 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8579 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS); 8580 assert(I != M.SubmoduleRemap.end() 8581 && "Invalid index into submodule index remap"); 8582 8583 return LocalID + I->second; 8584 } 8585 8586 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) { 8587 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) { 8588 assert(GlobalID == 0 && "Unhandled global submodule ID"); 8589 return nullptr; 8590 } 8591 8592 if (GlobalID > SubmodulesLoaded.size()) { 8593 Error("submodule ID out of range in AST file"); 8594 return nullptr; 8595 } 8596 8597 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS]; 8598 } 8599 8600 Module *ASTReader::getModule(unsigned ID) { 8601 return getSubmodule(ID); 8602 } 8603 8604 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) { 8605 if (ID & 1) { 8606 // It's a module, look it up by submodule ID. 8607 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1)); 8608 return I == GlobalSubmoduleMap.end() ? nullptr : I->second; 8609 } else { 8610 // It's a prefix (preamble, PCH, ...). Look it up by index. 8611 unsigned IndexFromEnd = ID >> 1; 8612 assert(IndexFromEnd && "got reference to unknown module file"); 8613 return getModuleManager().pch_modules().end()[-IndexFromEnd]; 8614 } 8615 } 8616 8617 unsigned ASTReader::getModuleFileID(ModuleFile *F) { 8618 if (!F) 8619 return 1; 8620 8621 // For a file representing a module, use the submodule ID of the top-level 8622 // module as the file ID. For any other kind of file, the number of such 8623 // files loaded beforehand will be the same on reload. 8624 // FIXME: Is this true even if we have an explicit module file and a PCH? 8625 if (F->isModule()) 8626 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1; 8627 8628 auto PCHModules = getModuleManager().pch_modules(); 8629 auto I = llvm::find(PCHModules, F); 8630 assert(I != PCHModules.end() && "emitting reference to unknown file"); 8631 return (I - PCHModules.end()) << 1; 8632 } 8633 8634 llvm::Optional<ASTSourceDescriptor> 8635 ASTReader::getSourceDescriptor(unsigned ID) { 8636 if (Module *M = getSubmodule(ID)) 8637 return ASTSourceDescriptor(*M); 8638 8639 // If there is only a single PCH, return it instead. 8640 // Chained PCH are not supported. 8641 const auto &PCHChain = ModuleMgr.pch_modules(); 8642 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) { 8643 ModuleFile &MF = ModuleMgr.getPrimaryModule(); 8644 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName); 8645 StringRef FileName = llvm::sys::path::filename(MF.FileName); 8646 return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName, 8647 MF.Signature); 8648 } 8649 return None; 8650 } 8651 8652 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) { 8653 auto I = DefinitionSource.find(FD); 8654 if (I == DefinitionSource.end()) 8655 return EK_ReplyHazy; 8656 return I->second ? EK_Never : EK_Always; 8657 } 8658 8659 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) { 8660 return DecodeSelector(getGlobalSelectorID(M, LocalID)); 8661 } 8662 8663 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) { 8664 if (ID == 0) 8665 return Selector(); 8666 8667 if (ID > SelectorsLoaded.size()) { 8668 Error("selector ID out of range in AST file"); 8669 return Selector(); 8670 } 8671 8672 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) { 8673 // Load this selector from the selector table. 8674 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID); 8675 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map"); 8676 ModuleFile &M = *I->second; 8677 ASTSelectorLookupTrait Trait(*this, M); 8678 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS; 8679 SelectorsLoaded[ID - 1] = 8680 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0); 8681 if (DeserializationListener) 8682 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 8683 } 8684 8685 return SelectorsLoaded[ID - 1]; 8686 } 8687 8688 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) { 8689 return DecodeSelector(ID); 8690 } 8691 8692 uint32_t ASTReader::GetNumExternalSelectors() { 8693 // ID 0 (the null selector) is considered an external selector. 8694 return getTotalNumSelectors() + 1; 8695 } 8696 8697 serialization::SelectorID 8698 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const { 8699 if (LocalID < NUM_PREDEF_SELECTOR_IDS) 8700 return LocalID; 8701 8702 if (!M.ModuleOffsetMap.empty()) 8703 ReadModuleOffsetMap(M); 8704 8705 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8706 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS); 8707 assert(I != M.SelectorRemap.end() 8708 && "Invalid index into selector index remap"); 8709 8710 return LocalID + I->second; 8711 } 8712 8713 DeclarationNameLoc 8714 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) { 8715 DeclarationNameLoc DNLoc; 8716 switch (Name.getNameKind()) { 8717 case DeclarationName::CXXConstructorName: 8718 case DeclarationName::CXXDestructorName: 8719 case DeclarationName::CXXConversionFunctionName: 8720 DNLoc.NamedType.TInfo = readTypeSourceInfo(); 8721 break; 8722 8723 case DeclarationName::CXXOperatorName: 8724 DNLoc.CXXOperatorName.BeginOpNameLoc 8725 = readSourceLocation().getRawEncoding(); 8726 DNLoc.CXXOperatorName.EndOpNameLoc 8727 = readSourceLocation().getRawEncoding(); 8728 break; 8729 8730 case DeclarationName::CXXLiteralOperatorName: 8731 DNLoc.CXXLiteralOperatorName.OpNameLoc 8732 = readSourceLocation().getRawEncoding(); 8733 break; 8734 8735 case DeclarationName::Identifier: 8736 case DeclarationName::ObjCZeroArgSelector: 8737 case DeclarationName::ObjCOneArgSelector: 8738 case DeclarationName::ObjCMultiArgSelector: 8739 case DeclarationName::CXXUsingDirective: 8740 case DeclarationName::CXXDeductionGuideName: 8741 break; 8742 } 8743 return DNLoc; 8744 } 8745 8746 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() { 8747 DeclarationNameInfo NameInfo; 8748 NameInfo.setName(readDeclarationName()); 8749 NameInfo.setLoc(readSourceLocation()); 8750 NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName())); 8751 return NameInfo; 8752 } 8753 8754 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) { 8755 Info.QualifierLoc = readNestedNameSpecifierLoc(); 8756 unsigned NumTPLists = readInt(); 8757 Info.NumTemplParamLists = NumTPLists; 8758 if (NumTPLists) { 8759 Info.TemplParamLists = 8760 new (getContext()) TemplateParameterList *[NumTPLists]; 8761 for (unsigned i = 0; i != NumTPLists; ++i) 8762 Info.TemplParamLists[i] = readTemplateParameterList(); 8763 } 8764 } 8765 8766 TemplateParameterList * 8767 ASTRecordReader::readTemplateParameterList() { 8768 SourceLocation TemplateLoc = readSourceLocation(); 8769 SourceLocation LAngleLoc = readSourceLocation(); 8770 SourceLocation RAngleLoc = readSourceLocation(); 8771 8772 unsigned NumParams = readInt(); 8773 SmallVector<NamedDecl *, 16> Params; 8774 Params.reserve(NumParams); 8775 while (NumParams--) 8776 Params.push_back(readDeclAs<NamedDecl>()); 8777 8778 bool HasRequiresClause = readBool(); 8779 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr; 8780 8781 TemplateParameterList *TemplateParams = TemplateParameterList::Create( 8782 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause); 8783 return TemplateParams; 8784 } 8785 8786 void ASTRecordReader::readTemplateArgumentList( 8787 SmallVectorImpl<TemplateArgument> &TemplArgs, 8788 bool Canonicalize) { 8789 unsigned NumTemplateArgs = readInt(); 8790 TemplArgs.reserve(NumTemplateArgs); 8791 while (NumTemplateArgs--) 8792 TemplArgs.push_back(readTemplateArgument(Canonicalize)); 8793 } 8794 8795 /// Read a UnresolvedSet structure. 8796 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) { 8797 unsigned NumDecls = readInt(); 8798 Set.reserve(getContext(), NumDecls); 8799 while (NumDecls--) { 8800 DeclID ID = readDeclID(); 8801 AccessSpecifier AS = (AccessSpecifier) readInt(); 8802 Set.addLazyDecl(getContext(), ID, AS); 8803 } 8804 } 8805 8806 CXXBaseSpecifier 8807 ASTRecordReader::readCXXBaseSpecifier() { 8808 bool isVirtual = readBool(); 8809 bool isBaseOfClass = readBool(); 8810 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt()); 8811 bool inheritConstructors = readBool(); 8812 TypeSourceInfo *TInfo = readTypeSourceInfo(); 8813 SourceRange Range = readSourceRange(); 8814 SourceLocation EllipsisLoc = readSourceLocation(); 8815 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo, 8816 EllipsisLoc); 8817 Result.setInheritConstructors(inheritConstructors); 8818 return Result; 8819 } 8820 8821 CXXCtorInitializer ** 8822 ASTRecordReader::readCXXCtorInitializers() { 8823 ASTContext &Context = getContext(); 8824 unsigned NumInitializers = readInt(); 8825 assert(NumInitializers && "wrote ctor initializers but have no inits"); 8826 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers]; 8827 for (unsigned i = 0; i != NumInitializers; ++i) { 8828 TypeSourceInfo *TInfo = nullptr; 8829 bool IsBaseVirtual = false; 8830 FieldDecl *Member = nullptr; 8831 IndirectFieldDecl *IndirectMember = nullptr; 8832 8833 CtorInitializerType Type = (CtorInitializerType) readInt(); 8834 switch (Type) { 8835 case CTOR_INITIALIZER_BASE: 8836 TInfo = readTypeSourceInfo(); 8837 IsBaseVirtual = readBool(); 8838 break; 8839 8840 case CTOR_INITIALIZER_DELEGATING: 8841 TInfo = readTypeSourceInfo(); 8842 break; 8843 8844 case CTOR_INITIALIZER_MEMBER: 8845 Member = readDeclAs<FieldDecl>(); 8846 break; 8847 8848 case CTOR_INITIALIZER_INDIRECT_MEMBER: 8849 IndirectMember = readDeclAs<IndirectFieldDecl>(); 8850 break; 8851 } 8852 8853 SourceLocation MemberOrEllipsisLoc = readSourceLocation(); 8854 Expr *Init = readExpr(); 8855 SourceLocation LParenLoc = readSourceLocation(); 8856 SourceLocation RParenLoc = readSourceLocation(); 8857 8858 CXXCtorInitializer *BOMInit; 8859 if (Type == CTOR_INITIALIZER_BASE) 8860 BOMInit = new (Context) 8861 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init, 8862 RParenLoc, MemberOrEllipsisLoc); 8863 else if (Type == CTOR_INITIALIZER_DELEGATING) 8864 BOMInit = new (Context) 8865 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc); 8866 else if (Member) 8867 BOMInit = new (Context) 8868 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc, 8869 Init, RParenLoc); 8870 else 8871 BOMInit = new (Context) 8872 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc, 8873 LParenLoc, Init, RParenLoc); 8874 8875 if (/*IsWritten*/readBool()) { 8876 unsigned SourceOrder = readInt(); 8877 BOMInit->setSourceOrder(SourceOrder); 8878 } 8879 8880 CtorInitializers[i] = BOMInit; 8881 } 8882 8883 return CtorInitializers; 8884 } 8885 8886 NestedNameSpecifierLoc 8887 ASTRecordReader::readNestedNameSpecifierLoc() { 8888 ASTContext &Context = getContext(); 8889 unsigned N = readInt(); 8890 NestedNameSpecifierLocBuilder Builder; 8891 for (unsigned I = 0; I != N; ++I) { 8892 auto Kind = readNestedNameSpecifierKind(); 8893 switch (Kind) { 8894 case NestedNameSpecifier::Identifier: { 8895 IdentifierInfo *II = readIdentifier(); 8896 SourceRange Range = readSourceRange(); 8897 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd()); 8898 break; 8899 } 8900 8901 case NestedNameSpecifier::Namespace: { 8902 NamespaceDecl *NS = readDeclAs<NamespaceDecl>(); 8903 SourceRange Range = readSourceRange(); 8904 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd()); 8905 break; 8906 } 8907 8908 case NestedNameSpecifier::NamespaceAlias: { 8909 NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>(); 8910 SourceRange Range = readSourceRange(); 8911 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd()); 8912 break; 8913 } 8914 8915 case NestedNameSpecifier::TypeSpec: 8916 case NestedNameSpecifier::TypeSpecWithTemplate: { 8917 bool Template = readBool(); 8918 TypeSourceInfo *T = readTypeSourceInfo(); 8919 if (!T) 8920 return NestedNameSpecifierLoc(); 8921 SourceLocation ColonColonLoc = readSourceLocation(); 8922 8923 // FIXME: 'template' keyword location not saved anywhere, so we fake it. 8924 Builder.Extend(Context, 8925 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(), 8926 T->getTypeLoc(), ColonColonLoc); 8927 break; 8928 } 8929 8930 case NestedNameSpecifier::Global: { 8931 SourceLocation ColonColonLoc = readSourceLocation(); 8932 Builder.MakeGlobal(Context, ColonColonLoc); 8933 break; 8934 } 8935 8936 case NestedNameSpecifier::Super: { 8937 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>(); 8938 SourceRange Range = readSourceRange(); 8939 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd()); 8940 break; 8941 } 8942 } 8943 } 8944 8945 return Builder.getWithLocInContext(Context); 8946 } 8947 8948 SourceRange 8949 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record, 8950 unsigned &Idx) { 8951 SourceLocation beg = ReadSourceLocation(F, Record, Idx); 8952 SourceLocation end = ReadSourceLocation(F, Record, Idx); 8953 return SourceRange(beg, end); 8954 } 8955 8956 static llvm::FixedPointSemantics 8957 ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record, 8958 unsigned &Idx) { 8959 unsigned Width = Record[Idx++]; 8960 unsigned Scale = Record[Idx++]; 8961 uint64_t Tmp = Record[Idx++]; 8962 bool IsSigned = Tmp & 0x1; 8963 bool IsSaturated = Tmp & 0x2; 8964 bool HasUnsignedPadding = Tmp & 0x4; 8965 return llvm::FixedPointSemantics(Width, Scale, IsSigned, IsSaturated, 8966 HasUnsignedPadding); 8967 } 8968 8969 static const llvm::fltSemantics & 8970 readAPFloatSemantics(ASTRecordReader &reader) { 8971 return llvm::APFloatBase::EnumToSemantics( 8972 static_cast<llvm::APFloatBase::Semantics>(reader.readInt())); 8973 } 8974 8975 APValue ASTRecordReader::readAPValue() { 8976 unsigned Kind = readInt(); 8977 switch ((APValue::ValueKind) Kind) { 8978 case APValue::None: 8979 return APValue(); 8980 case APValue::Indeterminate: 8981 return APValue::IndeterminateValue(); 8982 case APValue::Int: 8983 return APValue(readAPSInt()); 8984 case APValue::Float: { 8985 const llvm::fltSemantics &FloatSema = readAPFloatSemantics(*this); 8986 return APValue(readAPFloat(FloatSema)); 8987 } 8988 case APValue::FixedPoint: { 8989 llvm::FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx); 8990 return APValue(llvm::APFixedPoint(readAPInt(), FPSema)); 8991 } 8992 case APValue::ComplexInt: { 8993 llvm::APSInt First = readAPSInt(); 8994 return APValue(std::move(First), readAPSInt()); 8995 } 8996 case APValue::ComplexFloat: { 8997 const llvm::fltSemantics &FloatSema1 = readAPFloatSemantics(*this); 8998 llvm::APFloat First = readAPFloat(FloatSema1); 8999 const llvm::fltSemantics &FloatSema2 = readAPFloatSemantics(*this); 9000 return APValue(std::move(First), readAPFloat(FloatSema2)); 9001 } 9002 case APValue::LValue: 9003 case APValue::Vector: 9004 case APValue::Array: 9005 case APValue::Struct: 9006 case APValue::Union: 9007 case APValue::MemberPointer: 9008 case APValue::AddrLabelDiff: 9009 // TODO : Handle all these APValue::ValueKind. 9010 return APValue(); 9011 } 9012 llvm_unreachable("Invalid APValue::ValueKind"); 9013 } 9014 9015 /// Read a floating-point value 9016 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) { 9017 return llvm::APFloat(Sem, readAPInt()); 9018 } 9019 9020 // Read a string 9021 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) { 9022 unsigned Len = Record[Idx++]; 9023 std::string Result(Record.data() + Idx, Record.data() + Idx + Len); 9024 Idx += Len; 9025 return Result; 9026 } 9027 9028 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record, 9029 unsigned &Idx) { 9030 std::string Filename = ReadString(Record, Idx); 9031 ResolveImportedPath(F, Filename); 9032 return Filename; 9033 } 9034 9035 std::string ASTReader::ReadPath(StringRef BaseDirectory, 9036 const RecordData &Record, unsigned &Idx) { 9037 std::string Filename = ReadString(Record, Idx); 9038 if (!BaseDirectory.empty()) 9039 ResolveImportedPath(Filename, BaseDirectory); 9040 return Filename; 9041 } 9042 9043 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record, 9044 unsigned &Idx) { 9045 unsigned Major = Record[Idx++]; 9046 unsigned Minor = Record[Idx++]; 9047 unsigned Subminor = Record[Idx++]; 9048 if (Minor == 0) 9049 return VersionTuple(Major); 9050 if (Subminor == 0) 9051 return VersionTuple(Major, Minor - 1); 9052 return VersionTuple(Major, Minor - 1, Subminor - 1); 9053 } 9054 9055 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F, 9056 const RecordData &Record, 9057 unsigned &Idx) { 9058 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx); 9059 return CXXTemporary::Create(getContext(), Decl); 9060 } 9061 9062 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const { 9063 return Diag(CurrentImportLoc, DiagID); 9064 } 9065 9066 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const { 9067 return Diags.Report(Loc, DiagID); 9068 } 9069 9070 /// Retrieve the identifier table associated with the 9071 /// preprocessor. 9072 IdentifierTable &ASTReader::getIdentifierTable() { 9073 return PP.getIdentifierTable(); 9074 } 9075 9076 /// Record that the given ID maps to the given switch-case 9077 /// statement. 9078 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { 9079 assert((*CurrSwitchCaseStmts)[ID] == nullptr && 9080 "Already have a SwitchCase with this ID"); 9081 (*CurrSwitchCaseStmts)[ID] = SC; 9082 } 9083 9084 /// Retrieve the switch-case statement with the given ID. 9085 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) { 9086 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID"); 9087 return (*CurrSwitchCaseStmts)[ID]; 9088 } 9089 9090 void ASTReader::ClearSwitchCaseIDs() { 9091 CurrSwitchCaseStmts->clear(); 9092 } 9093 9094 void ASTReader::ReadComments() { 9095 ASTContext &Context = getContext(); 9096 std::vector<RawComment *> Comments; 9097 for (SmallVectorImpl<std::pair<BitstreamCursor, 9098 serialization::ModuleFile *>>::iterator 9099 I = CommentsCursors.begin(), 9100 E = CommentsCursors.end(); 9101 I != E; ++I) { 9102 Comments.clear(); 9103 BitstreamCursor &Cursor = I->first; 9104 serialization::ModuleFile &F = *I->second; 9105 SavedStreamPosition SavedPosition(Cursor); 9106 9107 RecordData Record; 9108 while (true) { 9109 Expected<llvm::BitstreamEntry> MaybeEntry = 9110 Cursor.advanceSkippingSubblocks( 9111 BitstreamCursor::AF_DontPopBlockAtEnd); 9112 if (!MaybeEntry) { 9113 Error(MaybeEntry.takeError()); 9114 return; 9115 } 9116 llvm::BitstreamEntry Entry = MaybeEntry.get(); 9117 9118 switch (Entry.Kind) { 9119 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 9120 case llvm::BitstreamEntry::Error: 9121 Error("malformed block record in AST file"); 9122 return; 9123 case llvm::BitstreamEntry::EndBlock: 9124 goto NextCursor; 9125 case llvm::BitstreamEntry::Record: 9126 // The interesting case. 9127 break; 9128 } 9129 9130 // Read a record. 9131 Record.clear(); 9132 Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record); 9133 if (!MaybeComment) { 9134 Error(MaybeComment.takeError()); 9135 return; 9136 } 9137 switch ((CommentRecordTypes)MaybeComment.get()) { 9138 case COMMENTS_RAW_COMMENT: { 9139 unsigned Idx = 0; 9140 SourceRange SR = ReadSourceRange(F, Record, Idx); 9141 RawComment::CommentKind Kind = 9142 (RawComment::CommentKind) Record[Idx++]; 9143 bool IsTrailingComment = Record[Idx++]; 9144 bool IsAlmostTrailingComment = Record[Idx++]; 9145 Comments.push_back(new (Context) RawComment( 9146 SR, Kind, IsTrailingComment, IsAlmostTrailingComment)); 9147 break; 9148 } 9149 } 9150 } 9151 NextCursor: 9152 llvm::DenseMap<FileID, std::map<unsigned, RawComment *>> 9153 FileToOffsetToComment; 9154 for (RawComment *C : Comments) { 9155 SourceLocation CommentLoc = C->getBeginLoc(); 9156 if (CommentLoc.isValid()) { 9157 std::pair<FileID, unsigned> Loc = 9158 SourceMgr.getDecomposedLoc(CommentLoc); 9159 if (Loc.first.isValid()) 9160 Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C); 9161 } 9162 } 9163 } 9164 } 9165 9166 void ASTReader::visitInputFiles(serialization::ModuleFile &MF, 9167 bool IncludeSystem, bool Complain, 9168 llvm::function_ref<void(const serialization::InputFile &IF, 9169 bool isSystem)> Visitor) { 9170 unsigned NumUserInputs = MF.NumUserInputFiles; 9171 unsigned NumInputs = MF.InputFilesLoaded.size(); 9172 assert(NumUserInputs <= NumInputs); 9173 unsigned N = IncludeSystem ? NumInputs : NumUserInputs; 9174 for (unsigned I = 0; I < N; ++I) { 9175 bool IsSystem = I >= NumUserInputs; 9176 InputFile IF = getInputFile(MF, I+1, Complain); 9177 Visitor(IF, IsSystem); 9178 } 9179 } 9180 9181 void ASTReader::visitTopLevelModuleMaps( 9182 serialization::ModuleFile &MF, 9183 llvm::function_ref<void(const FileEntry *FE)> Visitor) { 9184 unsigned NumInputs = MF.InputFilesLoaded.size(); 9185 for (unsigned I = 0; I < NumInputs; ++I) { 9186 InputFileInfo IFI = readInputFileInfo(MF, I + 1); 9187 if (IFI.TopLevelModuleMap) 9188 // FIXME: This unnecessarily re-reads the InputFileInfo. 9189 if (auto *FE = getInputFile(MF, I + 1).getFile()) 9190 Visitor(FE); 9191 } 9192 } 9193 9194 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) { 9195 // If we know the owning module, use it. 9196 if (Module *M = D->getImportedOwningModule()) 9197 return M->getFullModuleName(); 9198 9199 // Otherwise, use the name of the top-level module the decl is within. 9200 if (ModuleFile *M = getOwningModuleFile(D)) 9201 return M->ModuleName; 9202 9203 // Not from a module. 9204 return {}; 9205 } 9206 9207 void ASTReader::finishPendingActions() { 9208 while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() || 9209 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() || 9210 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() || 9211 !PendingUpdateRecords.empty()) { 9212 // If any identifiers with corresponding top-level declarations have 9213 // been loaded, load those declarations now. 9214 using TopLevelDeclsMap = 9215 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>; 9216 TopLevelDeclsMap TopLevelDecls; 9217 9218 while (!PendingIdentifierInfos.empty()) { 9219 IdentifierInfo *II = PendingIdentifierInfos.back().first; 9220 SmallVector<uint32_t, 4> DeclIDs = 9221 std::move(PendingIdentifierInfos.back().second); 9222 PendingIdentifierInfos.pop_back(); 9223 9224 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]); 9225 } 9226 9227 // Load each function type that we deferred loading because it was a 9228 // deduced type that might refer to a local type declared within itself. 9229 for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) { 9230 auto *FD = PendingFunctionTypes[I].first; 9231 FD->setType(GetType(PendingFunctionTypes[I].second)); 9232 9233 // If we gave a function a deduced return type, remember that we need to 9234 // propagate that along the redeclaration chain. 9235 auto *DT = FD->getReturnType()->getContainedDeducedType(); 9236 if (DT && DT->isDeduced()) 9237 PendingDeducedTypeUpdates.insert( 9238 {FD->getCanonicalDecl(), FD->getReturnType()}); 9239 } 9240 PendingFunctionTypes.clear(); 9241 9242 // For each decl chain that we wanted to complete while deserializing, mark 9243 // it as "still needs to be completed". 9244 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) { 9245 markIncompleteDeclChain(PendingIncompleteDeclChains[I]); 9246 } 9247 PendingIncompleteDeclChains.clear(); 9248 9249 // Load pending declaration chains. 9250 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) 9251 loadPendingDeclChain(PendingDeclChains[I].first, 9252 PendingDeclChains[I].second); 9253 PendingDeclChains.clear(); 9254 9255 // Make the most recent of the top-level declarations visible. 9256 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(), 9257 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) { 9258 IdentifierInfo *II = TLD->first; 9259 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) { 9260 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II); 9261 } 9262 } 9263 9264 // Load any pending macro definitions. 9265 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) { 9266 IdentifierInfo *II = PendingMacroIDs.begin()[I].first; 9267 SmallVector<PendingMacroInfo, 2> GlobalIDs; 9268 GlobalIDs.swap(PendingMacroIDs.begin()[I].second); 9269 // Initialize the macro history from chained-PCHs ahead of 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 // Handle module imports. 9277 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9278 ++IDIdx) { 9279 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9280 if (Info.M->isModule()) 9281 resolvePendingMacro(II, Info); 9282 } 9283 } 9284 PendingMacroIDs.clear(); 9285 9286 // Wire up the DeclContexts for Decls that we delayed setting until 9287 // recursive loading is completed. 9288 while (!PendingDeclContextInfos.empty()) { 9289 PendingDeclContextInfo Info = PendingDeclContextInfos.front(); 9290 PendingDeclContextInfos.pop_front(); 9291 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC)); 9292 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC)); 9293 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext()); 9294 } 9295 9296 // Perform any pending declaration updates. 9297 while (!PendingUpdateRecords.empty()) { 9298 auto Update = PendingUpdateRecords.pop_back_val(); 9299 ReadingKindTracker ReadingKind(Read_Decl, *this); 9300 loadDeclUpdateRecords(Update); 9301 } 9302 } 9303 9304 // At this point, all update records for loaded decls are in place, so any 9305 // fake class definitions should have become real. 9306 assert(PendingFakeDefinitionData.empty() && 9307 "faked up a class definition but never saw the real one"); 9308 9309 // If we deserialized any C++ or Objective-C class definitions, any 9310 // Objective-C protocol definitions, or any redeclarable templates, make sure 9311 // that all redeclarations point to the definitions. Note that this can only 9312 // happen now, after the redeclaration chains have been fully wired. 9313 for (Decl *D : PendingDefinitions) { 9314 if (TagDecl *TD = dyn_cast<TagDecl>(D)) { 9315 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) { 9316 // Make sure that the TagType points at the definition. 9317 const_cast<TagType*>(TagT)->decl = TD; 9318 } 9319 9320 if (auto RD = dyn_cast<CXXRecordDecl>(D)) { 9321 for (auto *R = getMostRecentExistingDecl(RD); R; 9322 R = R->getPreviousDecl()) { 9323 assert((R == D) == 9324 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() && 9325 "declaration thinks it's the definition but it isn't"); 9326 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData; 9327 } 9328 } 9329 9330 continue; 9331 } 9332 9333 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) { 9334 // Make sure that the ObjCInterfaceType points at the definition. 9335 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl)) 9336 ->Decl = ID; 9337 9338 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl()) 9339 cast<ObjCInterfaceDecl>(R)->Data = ID->Data; 9340 9341 continue; 9342 } 9343 9344 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) { 9345 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl()) 9346 cast<ObjCProtocolDecl>(R)->Data = PD->Data; 9347 9348 continue; 9349 } 9350 9351 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl(); 9352 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl()) 9353 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common; 9354 } 9355 PendingDefinitions.clear(); 9356 9357 // Load the bodies of any functions or methods we've encountered. We do 9358 // this now (delayed) so that we can be sure that the declaration chains 9359 // have been fully wired up (hasBody relies on this). 9360 // FIXME: We shouldn't require complete redeclaration chains here. 9361 for (PendingBodiesMap::iterator PB = PendingBodies.begin(), 9362 PBEnd = PendingBodies.end(); 9363 PB != PBEnd; ++PB) { 9364 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) { 9365 // For a function defined inline within a class template, force the 9366 // canonical definition to be the one inside the canonical definition of 9367 // the template. This ensures that we instantiate from a correct view 9368 // of the template. 9369 // 9370 // Sadly we can't do this more generally: we can't be sure that all 9371 // copies of an arbitrary class definition will have the same members 9372 // defined (eg, some member functions may not be instantiated, and some 9373 // special members may or may not have been implicitly defined). 9374 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent())) 9375 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition()) 9376 continue; 9377 9378 // FIXME: Check for =delete/=default? 9379 // FIXME: Complain about ODR violations here? 9380 const FunctionDecl *Defn = nullptr; 9381 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) { 9382 FD->setLazyBody(PB->second); 9383 } else { 9384 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn); 9385 mergeDefinitionVisibility(NonConstDefn, FD); 9386 9387 if (!FD->isLateTemplateParsed() && 9388 !NonConstDefn->isLateTemplateParsed() && 9389 FD->getODRHash() != NonConstDefn->getODRHash()) { 9390 if (!isa<CXXMethodDecl>(FD)) { 9391 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9392 } else if (FD->getLexicalParent()->isFileContext() && 9393 NonConstDefn->getLexicalParent()->isFileContext()) { 9394 // Only diagnose out-of-line method definitions. If they are 9395 // in class definitions, then an error will be generated when 9396 // processing the class bodies. 9397 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9398 } 9399 } 9400 } 9401 continue; 9402 } 9403 9404 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first); 9405 if (!getContext().getLangOpts().Modules || !MD->hasBody()) 9406 MD->setLazyBody(PB->second); 9407 } 9408 PendingBodies.clear(); 9409 9410 // Do some cleanup. 9411 for (auto *ND : PendingMergedDefinitionsToDeduplicate) 9412 getContext().deduplicateMergedDefinitonsFor(ND); 9413 PendingMergedDefinitionsToDeduplicate.clear(); 9414 } 9415 9416 void ASTReader::diagnoseOdrViolations() { 9417 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() && 9418 PendingFunctionOdrMergeFailures.empty() && 9419 PendingEnumOdrMergeFailures.empty()) 9420 return; 9421 9422 // Trigger the import of the full definition of each class that had any 9423 // odr-merging problems, so we can produce better diagnostics for them. 9424 // These updates may in turn find and diagnose some ODR failures, so take 9425 // ownership of the set first. 9426 auto OdrMergeFailures = std::move(PendingOdrMergeFailures); 9427 PendingOdrMergeFailures.clear(); 9428 for (auto &Merge : OdrMergeFailures) { 9429 Merge.first->buildLookup(); 9430 Merge.first->decls_begin(); 9431 Merge.first->bases_begin(); 9432 Merge.first->vbases_begin(); 9433 for (auto &RecordPair : Merge.second) { 9434 auto *RD = RecordPair.first; 9435 RD->decls_begin(); 9436 RD->bases_begin(); 9437 RD->vbases_begin(); 9438 } 9439 } 9440 9441 // Trigger the import of functions. 9442 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures); 9443 PendingFunctionOdrMergeFailures.clear(); 9444 for (auto &Merge : FunctionOdrMergeFailures) { 9445 Merge.first->buildLookup(); 9446 Merge.first->decls_begin(); 9447 Merge.first->getBody(); 9448 for (auto &FD : Merge.second) { 9449 FD->buildLookup(); 9450 FD->decls_begin(); 9451 FD->getBody(); 9452 } 9453 } 9454 9455 // Trigger the import of enums. 9456 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures); 9457 PendingEnumOdrMergeFailures.clear(); 9458 for (auto &Merge : EnumOdrMergeFailures) { 9459 Merge.first->decls_begin(); 9460 for (auto &Enum : Merge.second) { 9461 Enum->decls_begin(); 9462 } 9463 } 9464 9465 // For each declaration from a merged context, check that the canonical 9466 // definition of that context also contains a declaration of the same 9467 // entity. 9468 // 9469 // Caution: this loop does things that might invalidate iterators into 9470 // PendingOdrMergeChecks. Don't turn this into a range-based for loop! 9471 while (!PendingOdrMergeChecks.empty()) { 9472 NamedDecl *D = PendingOdrMergeChecks.pop_back_val(); 9473 9474 // FIXME: Skip over implicit declarations for now. This matters for things 9475 // like implicitly-declared special member functions. This isn't entirely 9476 // correct; we can end up with multiple unmerged declarations of the same 9477 // implicit entity. 9478 if (D->isImplicit()) 9479 continue; 9480 9481 DeclContext *CanonDef = D->getDeclContext(); 9482 9483 bool Found = false; 9484 const Decl *DCanon = D->getCanonicalDecl(); 9485 9486 for (auto RI : D->redecls()) { 9487 if (RI->getLexicalDeclContext() == CanonDef) { 9488 Found = true; 9489 break; 9490 } 9491 } 9492 if (Found) 9493 continue; 9494 9495 // Quick check failed, time to do the slow thing. Note, we can't just 9496 // look up the name of D in CanonDef here, because the member that is 9497 // in CanonDef might not be found by name lookup (it might have been 9498 // replaced by a more recent declaration in the lookup table), and we 9499 // can't necessarily find it in the redeclaration chain because it might 9500 // be merely mergeable, not redeclarable. 9501 llvm::SmallVector<const NamedDecl*, 4> Candidates; 9502 for (auto *CanonMember : CanonDef->decls()) { 9503 if (CanonMember->getCanonicalDecl() == DCanon) { 9504 // This can happen if the declaration is merely mergeable and not 9505 // actually redeclarable (we looked for redeclarations earlier). 9506 // 9507 // FIXME: We should be able to detect this more efficiently, without 9508 // pulling in all of the members of CanonDef. 9509 Found = true; 9510 break; 9511 } 9512 if (auto *ND = dyn_cast<NamedDecl>(CanonMember)) 9513 if (ND->getDeclName() == D->getDeclName()) 9514 Candidates.push_back(ND); 9515 } 9516 9517 if (!Found) { 9518 // The AST doesn't like TagDecls becoming invalid after they've been 9519 // completed. We only really need to mark FieldDecls as invalid here. 9520 if (!isa<TagDecl>(D)) 9521 D->setInvalidDecl(); 9522 9523 // Ensure we don't accidentally recursively enter deserialization while 9524 // we're producing our diagnostic. 9525 Deserializing RecursionGuard(this); 9526 9527 std::string CanonDefModule = 9528 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef)); 9529 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl) 9530 << D << getOwningModuleNameForDiagnostic(D) 9531 << CanonDef << CanonDefModule.empty() << CanonDefModule; 9532 9533 if (Candidates.empty()) 9534 Diag(cast<Decl>(CanonDef)->getLocation(), 9535 diag::note_module_odr_violation_no_possible_decls) << D; 9536 else { 9537 for (unsigned I = 0, N = Candidates.size(); I != N; ++I) 9538 Diag(Candidates[I]->getLocation(), 9539 diag::note_module_odr_violation_possible_decl) 9540 << Candidates[I]; 9541 } 9542 9543 DiagnosedOdrMergeFailures.insert(CanonDef); 9544 } 9545 } 9546 9547 if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() && 9548 EnumOdrMergeFailures.empty()) 9549 return; 9550 9551 // Ensure we don't accidentally recursively enter deserialization while 9552 // we're producing our diagnostics. 9553 Deserializing RecursionGuard(this); 9554 9555 // Common code for hashing helpers. 9556 ODRHash Hash; 9557 auto ComputeQualTypeODRHash = [&Hash](QualType Ty) { 9558 Hash.clear(); 9559 Hash.AddQualType(Ty); 9560 return Hash.CalculateHash(); 9561 }; 9562 9563 auto ComputeODRHash = [&Hash](const Stmt *S) { 9564 assert(S); 9565 Hash.clear(); 9566 Hash.AddStmt(S); 9567 return Hash.CalculateHash(); 9568 }; 9569 9570 auto ComputeSubDeclODRHash = [&Hash](const Decl *D) { 9571 assert(D); 9572 Hash.clear(); 9573 Hash.AddSubDecl(D); 9574 return Hash.CalculateHash(); 9575 }; 9576 9577 auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) { 9578 Hash.clear(); 9579 Hash.AddTemplateArgument(TA); 9580 return Hash.CalculateHash(); 9581 }; 9582 9583 auto ComputeTemplateParameterListODRHash = 9584 [&Hash](const TemplateParameterList *TPL) { 9585 assert(TPL); 9586 Hash.clear(); 9587 Hash.AddTemplateParameterList(TPL); 9588 return Hash.CalculateHash(); 9589 }; 9590 9591 // Used with err_module_odr_violation_mismatch_decl and 9592 // note_module_odr_violation_mismatch_decl 9593 // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed 9594 enum ODRMismatchDecl { 9595 EndOfClass, 9596 PublicSpecifer, 9597 PrivateSpecifer, 9598 ProtectedSpecifer, 9599 StaticAssert, 9600 Field, 9601 CXXMethod, 9602 TypeAlias, 9603 TypeDef, 9604 Var, 9605 Friend, 9606 FunctionTemplate, 9607 Other 9608 }; 9609 9610 // Used with err_module_odr_violation_mismatch_decl_diff and 9611 // note_module_odr_violation_mismatch_decl_diff 9612 enum ODRMismatchDeclDifference { 9613 StaticAssertCondition, 9614 StaticAssertMessage, 9615 StaticAssertOnlyMessage, 9616 FieldName, 9617 FieldTypeName, 9618 FieldSingleBitField, 9619 FieldDifferentWidthBitField, 9620 FieldSingleMutable, 9621 FieldSingleInitializer, 9622 FieldDifferentInitializers, 9623 MethodName, 9624 MethodDeleted, 9625 MethodDefaulted, 9626 MethodVirtual, 9627 MethodStatic, 9628 MethodVolatile, 9629 MethodConst, 9630 MethodInline, 9631 MethodNumberParameters, 9632 MethodParameterType, 9633 MethodParameterName, 9634 MethodParameterSingleDefaultArgument, 9635 MethodParameterDifferentDefaultArgument, 9636 MethodNoTemplateArguments, 9637 MethodDifferentNumberTemplateArguments, 9638 MethodDifferentTemplateArgument, 9639 MethodSingleBody, 9640 MethodDifferentBody, 9641 TypedefName, 9642 TypedefType, 9643 VarName, 9644 VarType, 9645 VarSingleInitializer, 9646 VarDifferentInitializer, 9647 VarConstexpr, 9648 FriendTypeFunction, 9649 FriendType, 9650 FriendFunction, 9651 FunctionTemplateDifferentNumberParameters, 9652 FunctionTemplateParameterDifferentKind, 9653 FunctionTemplateParameterName, 9654 FunctionTemplateParameterSingleDefaultArgument, 9655 FunctionTemplateParameterDifferentDefaultArgument, 9656 FunctionTemplateParameterDifferentType, 9657 FunctionTemplatePackParameter, 9658 }; 9659 9660 // These lambdas have the common portions of the ODR diagnostics. This 9661 // has the same return as Diag(), so addition parameters can be passed 9662 // in with operator<< 9663 auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule, 9664 SourceLocation Loc, SourceRange Range, 9665 ODRMismatchDeclDifference DiffType) { 9666 return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff) 9667 << FirstRecord << FirstModule.empty() << FirstModule << Range 9668 << DiffType; 9669 }; 9670 auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc, 9671 SourceRange Range, ODRMismatchDeclDifference DiffType) { 9672 return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff) 9673 << SecondModule << Range << DiffType; 9674 }; 9675 9676 auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote, 9677 &ComputeQualTypeODRHash, &ComputeODRHash]( 9678 NamedDecl *FirstRecord, StringRef FirstModule, 9679 StringRef SecondModule, FieldDecl *FirstField, 9680 FieldDecl *SecondField) { 9681 IdentifierInfo *FirstII = FirstField->getIdentifier(); 9682 IdentifierInfo *SecondII = SecondField->getIdentifier(); 9683 if (FirstII->getName() != SecondII->getName()) { 9684 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9685 FirstField->getSourceRange(), FieldName) 9686 << FirstII; 9687 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9688 SecondField->getSourceRange(), FieldName) 9689 << SecondII; 9690 9691 return true; 9692 } 9693 9694 assert(getContext().hasSameType(FirstField->getType(), 9695 SecondField->getType())); 9696 9697 QualType FirstType = FirstField->getType(); 9698 QualType SecondType = SecondField->getType(); 9699 if (ComputeQualTypeODRHash(FirstType) != 9700 ComputeQualTypeODRHash(SecondType)) { 9701 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9702 FirstField->getSourceRange(), FieldTypeName) 9703 << FirstII << FirstType; 9704 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9705 SecondField->getSourceRange(), FieldTypeName) 9706 << SecondII << SecondType; 9707 9708 return true; 9709 } 9710 9711 const bool IsFirstBitField = FirstField->isBitField(); 9712 const bool IsSecondBitField = SecondField->isBitField(); 9713 if (IsFirstBitField != IsSecondBitField) { 9714 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9715 FirstField->getSourceRange(), FieldSingleBitField) 9716 << FirstII << IsFirstBitField; 9717 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9718 SecondField->getSourceRange(), FieldSingleBitField) 9719 << SecondII << IsSecondBitField; 9720 return true; 9721 } 9722 9723 if (IsFirstBitField && IsSecondBitField) { 9724 unsigned FirstBitWidthHash = 9725 ComputeODRHash(FirstField->getBitWidth()); 9726 unsigned SecondBitWidthHash = 9727 ComputeODRHash(SecondField->getBitWidth()); 9728 if (FirstBitWidthHash != SecondBitWidthHash) { 9729 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9730 FirstField->getSourceRange(), 9731 FieldDifferentWidthBitField) 9732 << FirstII << FirstField->getBitWidth()->getSourceRange(); 9733 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9734 SecondField->getSourceRange(), 9735 FieldDifferentWidthBitField) 9736 << SecondII << SecondField->getBitWidth()->getSourceRange(); 9737 return true; 9738 } 9739 } 9740 9741 if (!PP.getLangOpts().CPlusPlus) 9742 return false; 9743 9744 const bool IsFirstMutable = FirstField->isMutable(); 9745 const bool IsSecondMutable = SecondField->isMutable(); 9746 if (IsFirstMutable != IsSecondMutable) { 9747 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9748 FirstField->getSourceRange(), FieldSingleMutable) 9749 << FirstII << IsFirstMutable; 9750 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9751 SecondField->getSourceRange(), FieldSingleMutable) 9752 << SecondII << IsSecondMutable; 9753 return true; 9754 } 9755 9756 const Expr *FirstInitializer = FirstField->getInClassInitializer(); 9757 const Expr *SecondInitializer = SecondField->getInClassInitializer(); 9758 if ((!FirstInitializer && SecondInitializer) || 9759 (FirstInitializer && !SecondInitializer)) { 9760 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9761 FirstField->getSourceRange(), FieldSingleInitializer) 9762 << FirstII << (FirstInitializer != nullptr); 9763 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9764 SecondField->getSourceRange(), FieldSingleInitializer) 9765 << SecondII << (SecondInitializer != nullptr); 9766 return true; 9767 } 9768 9769 if (FirstInitializer && SecondInitializer) { 9770 unsigned FirstInitHash = ComputeODRHash(FirstInitializer); 9771 unsigned SecondInitHash = ComputeODRHash(SecondInitializer); 9772 if (FirstInitHash != SecondInitHash) { 9773 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9774 FirstField->getSourceRange(), 9775 FieldDifferentInitializers) 9776 << FirstII << FirstInitializer->getSourceRange(); 9777 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9778 SecondField->getSourceRange(), 9779 FieldDifferentInitializers) 9780 << SecondII << SecondInitializer->getSourceRange(); 9781 return true; 9782 } 9783 } 9784 9785 return false; 9786 }; 9787 9788 auto ODRDiagTypeDefOrAlias = 9789 [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash]( 9790 NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule, 9791 TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD, 9792 bool IsTypeAlias) { 9793 auto FirstName = FirstTD->getDeclName(); 9794 auto SecondName = SecondTD->getDeclName(); 9795 if (FirstName != SecondName) { 9796 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(), 9797 FirstTD->getSourceRange(), TypedefName) 9798 << IsTypeAlias << FirstName; 9799 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(), 9800 SecondTD->getSourceRange(), TypedefName) 9801 << IsTypeAlias << SecondName; 9802 return true; 9803 } 9804 9805 QualType FirstType = FirstTD->getUnderlyingType(); 9806 QualType SecondType = SecondTD->getUnderlyingType(); 9807 if (ComputeQualTypeODRHash(FirstType) != 9808 ComputeQualTypeODRHash(SecondType)) { 9809 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(), 9810 FirstTD->getSourceRange(), TypedefType) 9811 << IsTypeAlias << FirstName << FirstType; 9812 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(), 9813 SecondTD->getSourceRange(), TypedefType) 9814 << IsTypeAlias << SecondName << SecondType; 9815 return true; 9816 } 9817 9818 return false; 9819 }; 9820 9821 auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote, 9822 &ComputeQualTypeODRHash, &ComputeODRHash, 9823 this](NamedDecl *FirstRecord, StringRef FirstModule, 9824 StringRef SecondModule, VarDecl *FirstVD, 9825 VarDecl *SecondVD) { 9826 auto FirstName = FirstVD->getDeclName(); 9827 auto SecondName = SecondVD->getDeclName(); 9828 if (FirstName != SecondName) { 9829 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9830 FirstVD->getSourceRange(), VarName) 9831 << FirstName; 9832 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9833 SecondVD->getSourceRange(), VarName) 9834 << SecondName; 9835 return true; 9836 } 9837 9838 QualType FirstType = FirstVD->getType(); 9839 QualType SecondType = SecondVD->getType(); 9840 if (ComputeQualTypeODRHash(FirstType) != 9841 ComputeQualTypeODRHash(SecondType)) { 9842 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9843 FirstVD->getSourceRange(), VarType) 9844 << FirstName << FirstType; 9845 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9846 SecondVD->getSourceRange(), VarType) 9847 << SecondName << SecondType; 9848 return true; 9849 } 9850 9851 if (!PP.getLangOpts().CPlusPlus) 9852 return false; 9853 9854 const Expr *FirstInit = FirstVD->getInit(); 9855 const Expr *SecondInit = SecondVD->getInit(); 9856 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 9857 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9858 FirstVD->getSourceRange(), VarSingleInitializer) 9859 << FirstName << (FirstInit == nullptr) 9860 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 9861 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9862 SecondVD->getSourceRange(), VarSingleInitializer) 9863 << SecondName << (SecondInit == nullptr) 9864 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 9865 return true; 9866 } 9867 9868 if (FirstInit && SecondInit && 9869 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 9870 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9871 FirstVD->getSourceRange(), VarDifferentInitializer) 9872 << FirstName << FirstInit->getSourceRange(); 9873 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9874 SecondVD->getSourceRange(), VarDifferentInitializer) 9875 << SecondName << SecondInit->getSourceRange(); 9876 return true; 9877 } 9878 9879 const bool FirstIsConstexpr = FirstVD->isConstexpr(); 9880 const bool SecondIsConstexpr = SecondVD->isConstexpr(); 9881 if (FirstIsConstexpr != SecondIsConstexpr) { 9882 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9883 FirstVD->getSourceRange(), VarConstexpr) 9884 << FirstName << FirstIsConstexpr; 9885 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9886 SecondVD->getSourceRange(), VarConstexpr) 9887 << SecondName << SecondIsConstexpr; 9888 return true; 9889 } 9890 return false; 9891 }; 9892 9893 auto DifferenceSelector = [](Decl *D) { 9894 assert(D && "valid Decl required"); 9895 switch (D->getKind()) { 9896 default: 9897 return Other; 9898 case Decl::AccessSpec: 9899 switch (D->getAccess()) { 9900 case AS_public: 9901 return PublicSpecifer; 9902 case AS_private: 9903 return PrivateSpecifer; 9904 case AS_protected: 9905 return ProtectedSpecifer; 9906 case AS_none: 9907 break; 9908 } 9909 llvm_unreachable("Invalid access specifier"); 9910 case Decl::StaticAssert: 9911 return StaticAssert; 9912 case Decl::Field: 9913 return Field; 9914 case Decl::CXXMethod: 9915 case Decl::CXXConstructor: 9916 case Decl::CXXDestructor: 9917 return CXXMethod; 9918 case Decl::TypeAlias: 9919 return TypeAlias; 9920 case Decl::Typedef: 9921 return TypeDef; 9922 case Decl::Var: 9923 return Var; 9924 case Decl::Friend: 9925 return Friend; 9926 case Decl::FunctionTemplate: 9927 return FunctionTemplate; 9928 } 9929 }; 9930 9931 using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>; 9932 auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes, 9933 RecordDecl *Record, 9934 const DeclContext *DC) { 9935 for (auto *D : Record->decls()) { 9936 if (!ODRHash::isDeclToBeProcessed(D, DC)) 9937 continue; 9938 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 9939 } 9940 }; 9941 9942 struct DiffResult { 9943 Decl *FirstDecl = nullptr, *SecondDecl = nullptr; 9944 ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other; 9945 }; 9946 9947 // If there is a diagnoseable difference, FirstDiffType and 9948 // SecondDiffType will not be Other and FirstDecl and SecondDecl will be 9949 // filled in if not EndOfClass. 9950 auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes, 9951 DeclHashes &SecondHashes) { 9952 DiffResult DR; 9953 auto FirstIt = FirstHashes.begin(); 9954 auto SecondIt = SecondHashes.begin(); 9955 while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) { 9956 if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() && 9957 FirstIt->second == SecondIt->second) { 9958 ++FirstIt; 9959 ++SecondIt; 9960 continue; 9961 } 9962 9963 DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first; 9964 DR.SecondDecl = 9965 SecondIt == SecondHashes.end() ? nullptr : SecondIt->first; 9966 9967 DR.FirstDiffType = 9968 DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass; 9969 DR.SecondDiffType = 9970 DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass; 9971 return DR; 9972 } 9973 return DR; 9974 }; 9975 9976 // Use this to diagnose that an unexpected Decl was encountered 9977 // or no difference was detected. This causes a generic error 9978 // message to be emitted. 9979 auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord, 9980 StringRef FirstModule, 9981 NamedDecl *SecondRecord, 9982 StringRef SecondModule) { 9983 Diag(FirstRecord->getLocation(), 9984 diag::err_module_odr_violation_different_definitions) 9985 << FirstRecord << FirstModule.empty() << FirstModule; 9986 9987 if (DR.FirstDecl) { 9988 Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference) 9989 << FirstRecord << DR.FirstDecl->getSourceRange(); 9990 } 9991 9992 Diag(SecondRecord->getLocation(), 9993 diag::note_module_odr_violation_different_definitions) 9994 << SecondModule; 9995 9996 if (DR.SecondDecl) { 9997 Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference) 9998 << DR.SecondDecl->getSourceRange(); 9999 } 10000 }; 10001 10002 auto DiagnoseODRMismatch = 10003 [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule, 10004 NamedDecl *SecondRecord, StringRef SecondModule) { 10005 SourceLocation FirstLoc; 10006 SourceRange FirstRange; 10007 auto *FirstTag = dyn_cast<TagDecl>(FirstRecord); 10008 if (DR.FirstDiffType == EndOfClass && FirstTag) { 10009 FirstLoc = FirstTag->getBraceRange().getEnd(); 10010 } else { 10011 FirstLoc = DR.FirstDecl->getLocation(); 10012 FirstRange = DR.FirstDecl->getSourceRange(); 10013 } 10014 Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl) 10015 << FirstRecord << FirstModule.empty() << FirstModule << FirstRange 10016 << DR.FirstDiffType; 10017 10018 SourceLocation SecondLoc; 10019 SourceRange SecondRange; 10020 auto *SecondTag = dyn_cast<TagDecl>(SecondRecord); 10021 if (DR.SecondDiffType == EndOfClass && SecondTag) { 10022 SecondLoc = SecondTag->getBraceRange().getEnd(); 10023 } else { 10024 SecondLoc = DR.SecondDecl->getLocation(); 10025 SecondRange = DR.SecondDecl->getSourceRange(); 10026 } 10027 Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl) 10028 << SecondModule << SecondRange << DR.SecondDiffType; 10029 }; 10030 10031 // Issue any pending ODR-failure diagnostics. 10032 for (auto &Merge : OdrMergeFailures) { 10033 // If we've already pointed out a specific problem with this class, don't 10034 // bother issuing a general "something's different" diagnostic. 10035 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 10036 continue; 10037 10038 bool Diagnosed = false; 10039 CXXRecordDecl *FirstRecord = Merge.first; 10040 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord); 10041 for (auto &RecordPair : Merge.second) { 10042 CXXRecordDecl *SecondRecord = RecordPair.first; 10043 // Multiple different declarations got merged together; tell the user 10044 // where they came from. 10045 if (FirstRecord == SecondRecord) 10046 continue; 10047 10048 std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord); 10049 10050 auto *FirstDD = FirstRecord->DefinitionData; 10051 auto *SecondDD = RecordPair.second; 10052 10053 assert(FirstDD && SecondDD && "Definitions without DefinitionData"); 10054 10055 // Diagnostics from DefinitionData are emitted here. 10056 if (FirstDD != SecondDD) { 10057 enum ODRDefinitionDataDifference { 10058 NumBases, 10059 NumVBases, 10060 BaseType, 10061 BaseVirtual, 10062 BaseAccess, 10063 }; 10064 auto ODRDiagBaseError = [FirstRecord, &FirstModule, 10065 this](SourceLocation Loc, SourceRange Range, 10066 ODRDefinitionDataDifference DiffType) { 10067 return Diag(Loc, diag::err_module_odr_violation_definition_data) 10068 << FirstRecord << FirstModule.empty() << FirstModule << Range 10069 << DiffType; 10070 }; 10071 auto ODRDiagBaseNote = [&SecondModule, 10072 this](SourceLocation Loc, SourceRange Range, 10073 ODRDefinitionDataDifference DiffType) { 10074 return Diag(Loc, diag::note_module_odr_violation_definition_data) 10075 << SecondModule << Range << DiffType; 10076 }; 10077 10078 unsigned FirstNumBases = FirstDD->NumBases; 10079 unsigned FirstNumVBases = FirstDD->NumVBases; 10080 unsigned SecondNumBases = SecondDD->NumBases; 10081 unsigned SecondNumVBases = SecondDD->NumVBases; 10082 10083 auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) { 10084 unsigned NumBases = DD->NumBases; 10085 if (NumBases == 0) return SourceRange(); 10086 auto bases = DD->bases(); 10087 return SourceRange(bases[0].getBeginLoc(), 10088 bases[NumBases - 1].getEndLoc()); 10089 }; 10090 10091 if (FirstNumBases != SecondNumBases) { 10092 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10093 NumBases) 10094 << FirstNumBases; 10095 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10096 NumBases) 10097 << SecondNumBases; 10098 Diagnosed = true; 10099 break; 10100 } 10101 10102 if (FirstNumVBases != SecondNumVBases) { 10103 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10104 NumVBases) 10105 << FirstNumVBases; 10106 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10107 NumVBases) 10108 << SecondNumVBases; 10109 Diagnosed = true; 10110 break; 10111 } 10112 10113 auto FirstBases = FirstDD->bases(); 10114 auto SecondBases = SecondDD->bases(); 10115 unsigned i = 0; 10116 for (i = 0; i < FirstNumBases; ++i) { 10117 auto FirstBase = FirstBases[i]; 10118 auto SecondBase = SecondBases[i]; 10119 if (ComputeQualTypeODRHash(FirstBase.getType()) != 10120 ComputeQualTypeODRHash(SecondBase.getType())) { 10121 ODRDiagBaseError(FirstRecord->getLocation(), 10122 FirstBase.getSourceRange(), BaseType) 10123 << (i + 1) << FirstBase.getType(); 10124 ODRDiagBaseNote(SecondRecord->getLocation(), 10125 SecondBase.getSourceRange(), BaseType) 10126 << (i + 1) << SecondBase.getType(); 10127 break; 10128 } 10129 10130 if (FirstBase.isVirtual() != SecondBase.isVirtual()) { 10131 ODRDiagBaseError(FirstRecord->getLocation(), 10132 FirstBase.getSourceRange(), BaseVirtual) 10133 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType(); 10134 ODRDiagBaseNote(SecondRecord->getLocation(), 10135 SecondBase.getSourceRange(), BaseVirtual) 10136 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType(); 10137 break; 10138 } 10139 10140 if (FirstBase.getAccessSpecifierAsWritten() != 10141 SecondBase.getAccessSpecifierAsWritten()) { 10142 ODRDiagBaseError(FirstRecord->getLocation(), 10143 FirstBase.getSourceRange(), BaseAccess) 10144 << (i + 1) << FirstBase.getType() 10145 << (int)FirstBase.getAccessSpecifierAsWritten(); 10146 ODRDiagBaseNote(SecondRecord->getLocation(), 10147 SecondBase.getSourceRange(), BaseAccess) 10148 << (i + 1) << SecondBase.getType() 10149 << (int)SecondBase.getAccessSpecifierAsWritten(); 10150 break; 10151 } 10152 } 10153 10154 if (i != FirstNumBases) { 10155 Diagnosed = true; 10156 break; 10157 } 10158 } 10159 10160 const ClassTemplateDecl *FirstTemplate = 10161 FirstRecord->getDescribedClassTemplate(); 10162 const ClassTemplateDecl *SecondTemplate = 10163 SecondRecord->getDescribedClassTemplate(); 10164 10165 assert(!FirstTemplate == !SecondTemplate && 10166 "Both pointers should be null or non-null"); 10167 10168 enum ODRTemplateDifference { 10169 ParamEmptyName, 10170 ParamName, 10171 ParamSingleDefaultArgument, 10172 ParamDifferentDefaultArgument, 10173 }; 10174 10175 if (FirstTemplate && SecondTemplate) { 10176 DeclHashes FirstTemplateHashes; 10177 DeclHashes SecondTemplateHashes; 10178 10179 auto PopulateTemplateParameterHashs = 10180 [&ComputeSubDeclODRHash](DeclHashes &Hashes, 10181 const ClassTemplateDecl *TD) { 10182 for (auto *D : TD->getTemplateParameters()->asArray()) { 10183 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 10184 } 10185 }; 10186 10187 PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate); 10188 PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate); 10189 10190 assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() && 10191 "Number of template parameters should be equal."); 10192 10193 auto FirstIt = FirstTemplateHashes.begin(); 10194 auto FirstEnd = FirstTemplateHashes.end(); 10195 auto SecondIt = SecondTemplateHashes.begin(); 10196 for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) { 10197 if (FirstIt->second == SecondIt->second) 10198 continue; 10199 10200 auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this]( 10201 SourceLocation Loc, SourceRange Range, 10202 ODRTemplateDifference DiffType) { 10203 return Diag(Loc, diag::err_module_odr_violation_template_parameter) 10204 << FirstRecord << FirstModule.empty() << FirstModule << Range 10205 << DiffType; 10206 }; 10207 auto ODRDiagTemplateNote = [&SecondModule, this]( 10208 SourceLocation Loc, SourceRange Range, 10209 ODRTemplateDifference DiffType) { 10210 return Diag(Loc, diag::note_module_odr_violation_template_parameter) 10211 << SecondModule << Range << DiffType; 10212 }; 10213 10214 const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first); 10215 const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first); 10216 10217 assert(FirstDecl->getKind() == SecondDecl->getKind() && 10218 "Parameter Decl's should be the same kind."); 10219 10220 DeclarationName FirstName = FirstDecl->getDeclName(); 10221 DeclarationName SecondName = SecondDecl->getDeclName(); 10222 10223 if (FirstName != SecondName) { 10224 const bool FirstNameEmpty = 10225 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo(); 10226 const bool SecondNameEmpty = 10227 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo(); 10228 assert((!FirstNameEmpty || !SecondNameEmpty) && 10229 "Both template parameters cannot be unnamed."); 10230 ODRDiagTemplateError(FirstDecl->getLocation(), 10231 FirstDecl->getSourceRange(), 10232 FirstNameEmpty ? ParamEmptyName : ParamName) 10233 << FirstName; 10234 ODRDiagTemplateNote(SecondDecl->getLocation(), 10235 SecondDecl->getSourceRange(), 10236 SecondNameEmpty ? ParamEmptyName : ParamName) 10237 << SecondName; 10238 break; 10239 } 10240 10241 switch (FirstDecl->getKind()) { 10242 default: 10243 llvm_unreachable("Invalid template parameter type."); 10244 case Decl::TemplateTypeParm: { 10245 const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl); 10246 const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl); 10247 const bool HasFirstDefaultArgument = 10248 FirstParam->hasDefaultArgument() && 10249 !FirstParam->defaultArgumentWasInherited(); 10250 const bool HasSecondDefaultArgument = 10251 SecondParam->hasDefaultArgument() && 10252 !SecondParam->defaultArgumentWasInherited(); 10253 10254 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10255 ODRDiagTemplateError(FirstDecl->getLocation(), 10256 FirstDecl->getSourceRange(), 10257 ParamSingleDefaultArgument) 10258 << HasFirstDefaultArgument; 10259 ODRDiagTemplateNote(SecondDecl->getLocation(), 10260 SecondDecl->getSourceRange(), 10261 ParamSingleDefaultArgument) 10262 << HasSecondDefaultArgument; 10263 break; 10264 } 10265 10266 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10267 "Expecting default arguments."); 10268 10269 ODRDiagTemplateError(FirstDecl->getLocation(), 10270 FirstDecl->getSourceRange(), 10271 ParamDifferentDefaultArgument); 10272 ODRDiagTemplateNote(SecondDecl->getLocation(), 10273 SecondDecl->getSourceRange(), 10274 ParamDifferentDefaultArgument); 10275 10276 break; 10277 } 10278 case Decl::NonTypeTemplateParm: { 10279 const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl); 10280 const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl); 10281 const bool HasFirstDefaultArgument = 10282 FirstParam->hasDefaultArgument() && 10283 !FirstParam->defaultArgumentWasInherited(); 10284 const bool HasSecondDefaultArgument = 10285 SecondParam->hasDefaultArgument() && 10286 !SecondParam->defaultArgumentWasInherited(); 10287 10288 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10289 ODRDiagTemplateError(FirstDecl->getLocation(), 10290 FirstDecl->getSourceRange(), 10291 ParamSingleDefaultArgument) 10292 << HasFirstDefaultArgument; 10293 ODRDiagTemplateNote(SecondDecl->getLocation(), 10294 SecondDecl->getSourceRange(), 10295 ParamSingleDefaultArgument) 10296 << HasSecondDefaultArgument; 10297 break; 10298 } 10299 10300 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10301 "Expecting default arguments."); 10302 10303 ODRDiagTemplateError(FirstDecl->getLocation(), 10304 FirstDecl->getSourceRange(), 10305 ParamDifferentDefaultArgument); 10306 ODRDiagTemplateNote(SecondDecl->getLocation(), 10307 SecondDecl->getSourceRange(), 10308 ParamDifferentDefaultArgument); 10309 10310 break; 10311 } 10312 case Decl::TemplateTemplateParm: { 10313 const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl); 10314 const auto *SecondParam = 10315 cast<TemplateTemplateParmDecl>(SecondDecl); 10316 const bool HasFirstDefaultArgument = 10317 FirstParam->hasDefaultArgument() && 10318 !FirstParam->defaultArgumentWasInherited(); 10319 const bool HasSecondDefaultArgument = 10320 SecondParam->hasDefaultArgument() && 10321 !SecondParam->defaultArgumentWasInherited(); 10322 10323 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10324 ODRDiagTemplateError(FirstDecl->getLocation(), 10325 FirstDecl->getSourceRange(), 10326 ParamSingleDefaultArgument) 10327 << HasFirstDefaultArgument; 10328 ODRDiagTemplateNote(SecondDecl->getLocation(), 10329 SecondDecl->getSourceRange(), 10330 ParamSingleDefaultArgument) 10331 << HasSecondDefaultArgument; 10332 break; 10333 } 10334 10335 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10336 "Expecting default arguments."); 10337 10338 ODRDiagTemplateError(FirstDecl->getLocation(), 10339 FirstDecl->getSourceRange(), 10340 ParamDifferentDefaultArgument); 10341 ODRDiagTemplateNote(SecondDecl->getLocation(), 10342 SecondDecl->getSourceRange(), 10343 ParamDifferentDefaultArgument); 10344 10345 break; 10346 } 10347 } 10348 10349 break; 10350 } 10351 10352 if (FirstIt != FirstEnd) { 10353 Diagnosed = true; 10354 break; 10355 } 10356 } 10357 10358 DeclHashes FirstHashes; 10359 DeclHashes SecondHashes; 10360 const DeclContext *DC = FirstRecord; 10361 PopulateHashes(FirstHashes, FirstRecord, DC); 10362 PopulateHashes(SecondHashes, SecondRecord, DC); 10363 10364 auto DR = FindTypeDiffs(FirstHashes, SecondHashes); 10365 ODRMismatchDecl FirstDiffType = DR.FirstDiffType; 10366 ODRMismatchDecl SecondDiffType = DR.SecondDiffType; 10367 Decl *FirstDecl = DR.FirstDecl; 10368 Decl *SecondDecl = DR.SecondDecl; 10369 10370 if (FirstDiffType == Other || SecondDiffType == Other) { 10371 DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord, 10372 SecondModule); 10373 Diagnosed = true; 10374 break; 10375 } 10376 10377 if (FirstDiffType != SecondDiffType) { 10378 DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord, 10379 SecondModule); 10380 Diagnosed = true; 10381 break; 10382 } 10383 10384 assert(FirstDiffType == SecondDiffType); 10385 10386 switch (FirstDiffType) { 10387 case Other: 10388 case EndOfClass: 10389 case PublicSpecifer: 10390 case PrivateSpecifer: 10391 case ProtectedSpecifer: 10392 llvm_unreachable("Invalid diff type"); 10393 10394 case StaticAssert: { 10395 StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl); 10396 StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl); 10397 10398 Expr *FirstExpr = FirstSA->getAssertExpr(); 10399 Expr *SecondExpr = SecondSA->getAssertExpr(); 10400 unsigned FirstODRHash = ComputeODRHash(FirstExpr); 10401 unsigned SecondODRHash = ComputeODRHash(SecondExpr); 10402 if (FirstODRHash != SecondODRHash) { 10403 ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(), 10404 FirstExpr->getSourceRange(), StaticAssertCondition); 10405 ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(), 10406 SecondExpr->getSourceRange(), StaticAssertCondition); 10407 Diagnosed = true; 10408 break; 10409 } 10410 10411 StringLiteral *FirstStr = FirstSA->getMessage(); 10412 StringLiteral *SecondStr = SecondSA->getMessage(); 10413 assert((FirstStr || SecondStr) && "Both messages cannot be empty"); 10414 if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) { 10415 SourceLocation FirstLoc, SecondLoc; 10416 SourceRange FirstRange, SecondRange; 10417 if (FirstStr) { 10418 FirstLoc = FirstStr->getBeginLoc(); 10419 FirstRange = FirstStr->getSourceRange(); 10420 } else { 10421 FirstLoc = FirstSA->getBeginLoc(); 10422 FirstRange = FirstSA->getSourceRange(); 10423 } 10424 if (SecondStr) { 10425 SecondLoc = SecondStr->getBeginLoc(); 10426 SecondRange = SecondStr->getSourceRange(); 10427 } else { 10428 SecondLoc = SecondSA->getBeginLoc(); 10429 SecondRange = SecondSA->getSourceRange(); 10430 } 10431 ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange, 10432 StaticAssertOnlyMessage) 10433 << (FirstStr == nullptr); 10434 ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange, 10435 StaticAssertOnlyMessage) 10436 << (SecondStr == nullptr); 10437 Diagnosed = true; 10438 break; 10439 } 10440 10441 if (FirstStr && SecondStr && 10442 FirstStr->getString() != SecondStr->getString()) { 10443 ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(), 10444 FirstStr->getSourceRange(), StaticAssertMessage); 10445 ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(), 10446 SecondStr->getSourceRange(), StaticAssertMessage); 10447 Diagnosed = true; 10448 break; 10449 } 10450 break; 10451 } 10452 case Field: { 10453 Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule, 10454 cast<FieldDecl>(FirstDecl), 10455 cast<FieldDecl>(SecondDecl)); 10456 break; 10457 } 10458 case CXXMethod: { 10459 enum { 10460 DiagMethod, 10461 DiagConstructor, 10462 DiagDestructor, 10463 } FirstMethodType, 10464 SecondMethodType; 10465 auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) { 10466 if (isa<CXXConstructorDecl>(D)) return DiagConstructor; 10467 if (isa<CXXDestructorDecl>(D)) return DiagDestructor; 10468 return DiagMethod; 10469 }; 10470 const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl); 10471 const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl); 10472 FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod); 10473 SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod); 10474 auto FirstName = FirstMethod->getDeclName(); 10475 auto SecondName = SecondMethod->getDeclName(); 10476 if (FirstMethodType != SecondMethodType || FirstName != SecondName) { 10477 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10478 FirstMethod->getSourceRange(), MethodName) 10479 << FirstMethodType << FirstName; 10480 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10481 SecondMethod->getSourceRange(), MethodName) 10482 << SecondMethodType << SecondName; 10483 10484 Diagnosed = true; 10485 break; 10486 } 10487 10488 const bool FirstDeleted = FirstMethod->isDeletedAsWritten(); 10489 const bool SecondDeleted = SecondMethod->isDeletedAsWritten(); 10490 if (FirstDeleted != SecondDeleted) { 10491 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10492 FirstMethod->getSourceRange(), MethodDeleted) 10493 << FirstMethodType << FirstName << FirstDeleted; 10494 10495 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10496 SecondMethod->getSourceRange(), MethodDeleted) 10497 << SecondMethodType << SecondName << SecondDeleted; 10498 Diagnosed = true; 10499 break; 10500 } 10501 10502 const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted(); 10503 const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted(); 10504 if (FirstDefaulted != SecondDefaulted) { 10505 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10506 FirstMethod->getSourceRange(), MethodDefaulted) 10507 << FirstMethodType << FirstName << FirstDefaulted; 10508 10509 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10510 SecondMethod->getSourceRange(), MethodDefaulted) 10511 << SecondMethodType << SecondName << SecondDefaulted; 10512 Diagnosed = true; 10513 break; 10514 } 10515 10516 const bool FirstVirtual = FirstMethod->isVirtualAsWritten(); 10517 const bool SecondVirtual = SecondMethod->isVirtualAsWritten(); 10518 const bool FirstPure = FirstMethod->isPure(); 10519 const bool SecondPure = SecondMethod->isPure(); 10520 if ((FirstVirtual || SecondVirtual) && 10521 (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) { 10522 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10523 FirstMethod->getSourceRange(), MethodVirtual) 10524 << FirstMethodType << FirstName << FirstPure << FirstVirtual; 10525 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10526 SecondMethod->getSourceRange(), MethodVirtual) 10527 << SecondMethodType << SecondName << SecondPure << SecondVirtual; 10528 Diagnosed = true; 10529 break; 10530 } 10531 10532 // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging, 10533 // FirstDecl is the canonical Decl of SecondDecl, so the storage 10534 // class needs to be checked instead. 10535 const auto FirstStorage = FirstMethod->getStorageClass(); 10536 const auto SecondStorage = SecondMethod->getStorageClass(); 10537 const bool FirstStatic = FirstStorage == SC_Static; 10538 const bool SecondStatic = SecondStorage == SC_Static; 10539 if (FirstStatic != SecondStatic) { 10540 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10541 FirstMethod->getSourceRange(), MethodStatic) 10542 << FirstMethodType << FirstName << FirstStatic; 10543 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10544 SecondMethod->getSourceRange(), MethodStatic) 10545 << SecondMethodType << SecondName << SecondStatic; 10546 Diagnosed = true; 10547 break; 10548 } 10549 10550 const bool FirstVolatile = FirstMethod->isVolatile(); 10551 const bool SecondVolatile = SecondMethod->isVolatile(); 10552 if (FirstVolatile != SecondVolatile) { 10553 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10554 FirstMethod->getSourceRange(), MethodVolatile) 10555 << FirstMethodType << FirstName << FirstVolatile; 10556 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10557 SecondMethod->getSourceRange(), MethodVolatile) 10558 << SecondMethodType << SecondName << SecondVolatile; 10559 Diagnosed = true; 10560 break; 10561 } 10562 10563 const bool FirstConst = FirstMethod->isConst(); 10564 const bool SecondConst = SecondMethod->isConst(); 10565 if (FirstConst != SecondConst) { 10566 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10567 FirstMethod->getSourceRange(), MethodConst) 10568 << FirstMethodType << FirstName << FirstConst; 10569 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10570 SecondMethod->getSourceRange(), MethodConst) 10571 << SecondMethodType << SecondName << SecondConst; 10572 Diagnosed = true; 10573 break; 10574 } 10575 10576 const bool FirstInline = FirstMethod->isInlineSpecified(); 10577 const bool SecondInline = SecondMethod->isInlineSpecified(); 10578 if (FirstInline != SecondInline) { 10579 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10580 FirstMethod->getSourceRange(), MethodInline) 10581 << FirstMethodType << FirstName << FirstInline; 10582 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10583 SecondMethod->getSourceRange(), MethodInline) 10584 << SecondMethodType << SecondName << SecondInline; 10585 Diagnosed = true; 10586 break; 10587 } 10588 10589 const unsigned FirstNumParameters = FirstMethod->param_size(); 10590 const unsigned SecondNumParameters = SecondMethod->param_size(); 10591 if (FirstNumParameters != SecondNumParameters) { 10592 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10593 FirstMethod->getSourceRange(), 10594 MethodNumberParameters) 10595 << FirstMethodType << FirstName << FirstNumParameters; 10596 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10597 SecondMethod->getSourceRange(), 10598 MethodNumberParameters) 10599 << SecondMethodType << SecondName << SecondNumParameters; 10600 Diagnosed = true; 10601 break; 10602 } 10603 10604 // Need this status boolean to know when break out of the switch. 10605 bool ParameterMismatch = false; 10606 for (unsigned I = 0; I < FirstNumParameters; ++I) { 10607 const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I); 10608 const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I); 10609 10610 QualType FirstParamType = FirstParam->getType(); 10611 QualType SecondParamType = SecondParam->getType(); 10612 if (FirstParamType != SecondParamType && 10613 ComputeQualTypeODRHash(FirstParamType) != 10614 ComputeQualTypeODRHash(SecondParamType)) { 10615 if (const DecayedType *ParamDecayedType = 10616 FirstParamType->getAs<DecayedType>()) { 10617 ODRDiagDeclError( 10618 FirstRecord, FirstModule, FirstMethod->getLocation(), 10619 FirstMethod->getSourceRange(), MethodParameterType) 10620 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10621 << true << ParamDecayedType->getOriginalType(); 10622 } else { 10623 ODRDiagDeclError( 10624 FirstRecord, FirstModule, FirstMethod->getLocation(), 10625 FirstMethod->getSourceRange(), MethodParameterType) 10626 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10627 << false; 10628 } 10629 10630 if (const DecayedType *ParamDecayedType = 10631 SecondParamType->getAs<DecayedType>()) { 10632 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10633 SecondMethod->getSourceRange(), 10634 MethodParameterType) 10635 << SecondMethodType << SecondName << (I + 1) 10636 << SecondParamType << true 10637 << ParamDecayedType->getOriginalType(); 10638 } else { 10639 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10640 SecondMethod->getSourceRange(), 10641 MethodParameterType) 10642 << SecondMethodType << SecondName << (I + 1) 10643 << SecondParamType << false; 10644 } 10645 ParameterMismatch = true; 10646 break; 10647 } 10648 10649 DeclarationName FirstParamName = FirstParam->getDeclName(); 10650 DeclarationName SecondParamName = SecondParam->getDeclName(); 10651 if (FirstParamName != SecondParamName) { 10652 ODRDiagDeclError(FirstRecord, FirstModule, 10653 FirstMethod->getLocation(), 10654 FirstMethod->getSourceRange(), MethodParameterName) 10655 << FirstMethodType << FirstName << (I + 1) << FirstParamName; 10656 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10657 SecondMethod->getSourceRange(), MethodParameterName) 10658 << SecondMethodType << SecondName << (I + 1) << SecondParamName; 10659 ParameterMismatch = true; 10660 break; 10661 } 10662 10663 const Expr *FirstInit = FirstParam->getInit(); 10664 const Expr *SecondInit = SecondParam->getInit(); 10665 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 10666 ODRDiagDeclError(FirstRecord, FirstModule, 10667 FirstMethod->getLocation(), 10668 FirstMethod->getSourceRange(), 10669 MethodParameterSingleDefaultArgument) 10670 << FirstMethodType << FirstName << (I + 1) 10671 << (FirstInit == nullptr) 10672 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 10673 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10674 SecondMethod->getSourceRange(), 10675 MethodParameterSingleDefaultArgument) 10676 << SecondMethodType << SecondName << (I + 1) 10677 << (SecondInit == nullptr) 10678 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 10679 ParameterMismatch = true; 10680 break; 10681 } 10682 10683 if (FirstInit && SecondInit && 10684 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 10685 ODRDiagDeclError(FirstRecord, FirstModule, 10686 FirstMethod->getLocation(), 10687 FirstMethod->getSourceRange(), 10688 MethodParameterDifferentDefaultArgument) 10689 << FirstMethodType << FirstName << (I + 1) 10690 << FirstInit->getSourceRange(); 10691 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10692 SecondMethod->getSourceRange(), 10693 MethodParameterDifferentDefaultArgument) 10694 << SecondMethodType << SecondName << (I + 1) 10695 << SecondInit->getSourceRange(); 10696 ParameterMismatch = true; 10697 break; 10698 10699 } 10700 } 10701 10702 if (ParameterMismatch) { 10703 Diagnosed = true; 10704 break; 10705 } 10706 10707 const auto *FirstTemplateArgs = 10708 FirstMethod->getTemplateSpecializationArgs(); 10709 const auto *SecondTemplateArgs = 10710 SecondMethod->getTemplateSpecializationArgs(); 10711 10712 if ((FirstTemplateArgs && !SecondTemplateArgs) || 10713 (!FirstTemplateArgs && SecondTemplateArgs)) { 10714 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10715 FirstMethod->getSourceRange(), 10716 MethodNoTemplateArguments) 10717 << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr); 10718 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10719 SecondMethod->getSourceRange(), 10720 MethodNoTemplateArguments) 10721 << SecondMethodType << SecondName 10722 << (SecondTemplateArgs != nullptr); 10723 10724 Diagnosed = true; 10725 break; 10726 } 10727 10728 if (FirstTemplateArgs && SecondTemplateArgs) { 10729 // Remove pack expansions from argument list. 10730 auto ExpandTemplateArgumentList = 10731 [](const TemplateArgumentList *TAL) { 10732 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList; 10733 for (const TemplateArgument &TA : TAL->asArray()) { 10734 if (TA.getKind() != TemplateArgument::Pack) { 10735 ExpandedList.push_back(&TA); 10736 continue; 10737 } 10738 for (const TemplateArgument &PackTA : TA.getPackAsArray()) { 10739 ExpandedList.push_back(&PackTA); 10740 } 10741 } 10742 return ExpandedList; 10743 }; 10744 llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList = 10745 ExpandTemplateArgumentList(FirstTemplateArgs); 10746 llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList = 10747 ExpandTemplateArgumentList(SecondTemplateArgs); 10748 10749 if (FirstExpandedList.size() != SecondExpandedList.size()) { 10750 ODRDiagDeclError(FirstRecord, FirstModule, 10751 FirstMethod->getLocation(), 10752 FirstMethod->getSourceRange(), 10753 MethodDifferentNumberTemplateArguments) 10754 << FirstMethodType << FirstName 10755 << (unsigned)FirstExpandedList.size(); 10756 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10757 SecondMethod->getSourceRange(), 10758 MethodDifferentNumberTemplateArguments) 10759 << SecondMethodType << SecondName 10760 << (unsigned)SecondExpandedList.size(); 10761 10762 Diagnosed = true; 10763 break; 10764 } 10765 10766 bool TemplateArgumentMismatch = false; 10767 for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) { 10768 const TemplateArgument &FirstTA = *FirstExpandedList[i], 10769 &SecondTA = *SecondExpandedList[i]; 10770 if (ComputeTemplateArgumentODRHash(FirstTA) == 10771 ComputeTemplateArgumentODRHash(SecondTA)) { 10772 continue; 10773 } 10774 10775 ODRDiagDeclError( 10776 FirstRecord, FirstModule, FirstMethod->getLocation(), 10777 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument) 10778 << FirstMethodType << FirstName << FirstTA << i + 1; 10779 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10780 SecondMethod->getSourceRange(), 10781 MethodDifferentTemplateArgument) 10782 << SecondMethodType << SecondName << SecondTA << i + 1; 10783 10784 TemplateArgumentMismatch = true; 10785 break; 10786 } 10787 10788 if (TemplateArgumentMismatch) { 10789 Diagnosed = true; 10790 break; 10791 } 10792 } 10793 10794 // Compute the hash of the method as if it has no body. 10795 auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) { 10796 Hash.clear(); 10797 Hash.AddFunctionDecl(D, true /*SkipBody*/); 10798 return Hash.CalculateHash(); 10799 }; 10800 10801 // Compare the hash generated to the hash stored. A difference means 10802 // that a body was present in the original source. Due to merging, 10803 // the stardard way of detecting a body will not work. 10804 const bool HasFirstBody = 10805 ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash(); 10806 const bool HasSecondBody = 10807 ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash(); 10808 10809 if (HasFirstBody != HasSecondBody) { 10810 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10811 FirstMethod->getSourceRange(), MethodSingleBody) 10812 << FirstMethodType << FirstName << HasFirstBody; 10813 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10814 SecondMethod->getSourceRange(), MethodSingleBody) 10815 << SecondMethodType << SecondName << HasSecondBody; 10816 Diagnosed = true; 10817 break; 10818 } 10819 10820 if (HasFirstBody && HasSecondBody) { 10821 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10822 FirstMethod->getSourceRange(), MethodDifferentBody) 10823 << FirstMethodType << FirstName; 10824 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10825 SecondMethod->getSourceRange(), MethodDifferentBody) 10826 << SecondMethodType << SecondName; 10827 Diagnosed = true; 10828 break; 10829 } 10830 10831 break; 10832 } 10833 case TypeAlias: 10834 case TypeDef: { 10835 Diagnosed = ODRDiagTypeDefOrAlias( 10836 FirstRecord, FirstModule, SecondModule, 10837 cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl), 10838 FirstDiffType == TypeAlias); 10839 break; 10840 } 10841 case Var: { 10842 Diagnosed = 10843 ODRDiagVar(FirstRecord, FirstModule, SecondModule, 10844 cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl)); 10845 break; 10846 } 10847 case Friend: { 10848 FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl); 10849 FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl); 10850 10851 NamedDecl *FirstND = FirstFriend->getFriendDecl(); 10852 NamedDecl *SecondND = SecondFriend->getFriendDecl(); 10853 10854 TypeSourceInfo *FirstTSI = FirstFriend->getFriendType(); 10855 TypeSourceInfo *SecondTSI = SecondFriend->getFriendType(); 10856 10857 if (FirstND && SecondND) { 10858 ODRDiagDeclError(FirstRecord, FirstModule, 10859 FirstFriend->getFriendLoc(), 10860 FirstFriend->getSourceRange(), FriendFunction) 10861 << FirstND; 10862 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10863 SecondFriend->getSourceRange(), FriendFunction) 10864 << SecondND; 10865 10866 Diagnosed = true; 10867 break; 10868 } 10869 10870 if (FirstTSI && SecondTSI) { 10871 QualType FirstFriendType = FirstTSI->getType(); 10872 QualType SecondFriendType = SecondTSI->getType(); 10873 assert(ComputeQualTypeODRHash(FirstFriendType) != 10874 ComputeQualTypeODRHash(SecondFriendType)); 10875 ODRDiagDeclError(FirstRecord, FirstModule, 10876 FirstFriend->getFriendLoc(), 10877 FirstFriend->getSourceRange(), FriendType) 10878 << FirstFriendType; 10879 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10880 SecondFriend->getSourceRange(), FriendType) 10881 << SecondFriendType; 10882 Diagnosed = true; 10883 break; 10884 } 10885 10886 ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(), 10887 FirstFriend->getSourceRange(), FriendTypeFunction) 10888 << (FirstTSI == nullptr); 10889 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10890 SecondFriend->getSourceRange(), FriendTypeFunction) 10891 << (SecondTSI == nullptr); 10892 10893 Diagnosed = true; 10894 break; 10895 } 10896 case FunctionTemplate: { 10897 FunctionTemplateDecl *FirstTemplate = 10898 cast<FunctionTemplateDecl>(FirstDecl); 10899 FunctionTemplateDecl *SecondTemplate = 10900 cast<FunctionTemplateDecl>(SecondDecl); 10901 10902 TemplateParameterList *FirstTPL = 10903 FirstTemplate->getTemplateParameters(); 10904 TemplateParameterList *SecondTPL = 10905 SecondTemplate->getTemplateParameters(); 10906 10907 if (FirstTPL->size() != SecondTPL->size()) { 10908 ODRDiagDeclError(FirstRecord, FirstModule, 10909 FirstTemplate->getLocation(), 10910 FirstTemplate->getSourceRange(), 10911 FunctionTemplateDifferentNumberParameters) 10912 << FirstTemplate << FirstTPL->size(); 10913 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10914 SecondTemplate->getSourceRange(), 10915 FunctionTemplateDifferentNumberParameters) 10916 << SecondTemplate << SecondTPL->size(); 10917 10918 Diagnosed = true; 10919 break; 10920 } 10921 10922 bool ParameterMismatch = false; 10923 for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) { 10924 NamedDecl *FirstParam = FirstTPL->getParam(i); 10925 NamedDecl *SecondParam = SecondTPL->getParam(i); 10926 10927 if (FirstParam->getKind() != SecondParam->getKind()) { 10928 enum { 10929 TemplateTypeParameter, 10930 NonTypeTemplateParameter, 10931 TemplateTemplateParameter, 10932 }; 10933 auto GetParamType = [](NamedDecl *D) { 10934 switch (D->getKind()) { 10935 default: 10936 llvm_unreachable("Unexpected template parameter type"); 10937 case Decl::TemplateTypeParm: 10938 return TemplateTypeParameter; 10939 case Decl::NonTypeTemplateParm: 10940 return NonTypeTemplateParameter; 10941 case Decl::TemplateTemplateParm: 10942 return TemplateTemplateParameter; 10943 } 10944 }; 10945 10946 ODRDiagDeclError(FirstRecord, FirstModule, 10947 FirstTemplate->getLocation(), 10948 FirstTemplate->getSourceRange(), 10949 FunctionTemplateParameterDifferentKind) 10950 << FirstTemplate << (i + 1) << GetParamType(FirstParam); 10951 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10952 SecondTemplate->getSourceRange(), 10953 FunctionTemplateParameterDifferentKind) 10954 << SecondTemplate << (i + 1) << GetParamType(SecondParam); 10955 10956 ParameterMismatch = true; 10957 break; 10958 } 10959 10960 if (FirstParam->getName() != SecondParam->getName()) { 10961 ODRDiagDeclError( 10962 FirstRecord, FirstModule, FirstTemplate->getLocation(), 10963 FirstTemplate->getSourceRange(), FunctionTemplateParameterName) 10964 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier() 10965 << FirstParam; 10966 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10967 SecondTemplate->getSourceRange(), 10968 FunctionTemplateParameterName) 10969 << SecondTemplate << (i + 1) 10970 << (bool)SecondParam->getIdentifier() << SecondParam; 10971 ParameterMismatch = true; 10972 break; 10973 } 10974 10975 if (isa<TemplateTypeParmDecl>(FirstParam) && 10976 isa<TemplateTypeParmDecl>(SecondParam)) { 10977 TemplateTypeParmDecl *FirstTTPD = 10978 cast<TemplateTypeParmDecl>(FirstParam); 10979 TemplateTypeParmDecl *SecondTTPD = 10980 cast<TemplateTypeParmDecl>(SecondParam); 10981 bool HasFirstDefaultArgument = 10982 FirstTTPD->hasDefaultArgument() && 10983 !FirstTTPD->defaultArgumentWasInherited(); 10984 bool HasSecondDefaultArgument = 10985 SecondTTPD->hasDefaultArgument() && 10986 !SecondTTPD->defaultArgumentWasInherited(); 10987 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10988 ODRDiagDeclError(FirstRecord, FirstModule, 10989 FirstTemplate->getLocation(), 10990 FirstTemplate->getSourceRange(), 10991 FunctionTemplateParameterSingleDefaultArgument) 10992 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 10993 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10994 SecondTemplate->getSourceRange(), 10995 FunctionTemplateParameterSingleDefaultArgument) 10996 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 10997 ParameterMismatch = true; 10998 break; 10999 } 11000 11001 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11002 QualType FirstType = FirstTTPD->getDefaultArgument(); 11003 QualType SecondType = SecondTTPD->getDefaultArgument(); 11004 if (ComputeQualTypeODRHash(FirstType) != 11005 ComputeQualTypeODRHash(SecondType)) { 11006 ODRDiagDeclError( 11007 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11008 FirstTemplate->getSourceRange(), 11009 FunctionTemplateParameterDifferentDefaultArgument) 11010 << FirstTemplate << (i + 1) << FirstType; 11011 ODRDiagDeclNote( 11012 SecondModule, SecondTemplate->getLocation(), 11013 SecondTemplate->getSourceRange(), 11014 FunctionTemplateParameterDifferentDefaultArgument) 11015 << SecondTemplate << (i + 1) << SecondType; 11016 ParameterMismatch = true; 11017 break; 11018 } 11019 } 11020 11021 if (FirstTTPD->isParameterPack() != 11022 SecondTTPD->isParameterPack()) { 11023 ODRDiagDeclError(FirstRecord, FirstModule, 11024 FirstTemplate->getLocation(), 11025 FirstTemplate->getSourceRange(), 11026 FunctionTemplatePackParameter) 11027 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 11028 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11029 SecondTemplate->getSourceRange(), 11030 FunctionTemplatePackParameter) 11031 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 11032 ParameterMismatch = true; 11033 break; 11034 } 11035 } 11036 11037 if (isa<TemplateTemplateParmDecl>(FirstParam) && 11038 isa<TemplateTemplateParmDecl>(SecondParam)) { 11039 TemplateTemplateParmDecl *FirstTTPD = 11040 cast<TemplateTemplateParmDecl>(FirstParam); 11041 TemplateTemplateParmDecl *SecondTTPD = 11042 cast<TemplateTemplateParmDecl>(SecondParam); 11043 11044 TemplateParameterList *FirstTPL = 11045 FirstTTPD->getTemplateParameters(); 11046 TemplateParameterList *SecondTPL = 11047 SecondTTPD->getTemplateParameters(); 11048 11049 if (ComputeTemplateParameterListODRHash(FirstTPL) != 11050 ComputeTemplateParameterListODRHash(SecondTPL)) { 11051 ODRDiagDeclError(FirstRecord, FirstModule, 11052 FirstTemplate->getLocation(), 11053 FirstTemplate->getSourceRange(), 11054 FunctionTemplateParameterDifferentType) 11055 << FirstTemplate << (i + 1); 11056 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11057 SecondTemplate->getSourceRange(), 11058 FunctionTemplateParameterDifferentType) 11059 << SecondTemplate << (i + 1); 11060 ParameterMismatch = true; 11061 break; 11062 } 11063 11064 bool HasFirstDefaultArgument = 11065 FirstTTPD->hasDefaultArgument() && 11066 !FirstTTPD->defaultArgumentWasInherited(); 11067 bool HasSecondDefaultArgument = 11068 SecondTTPD->hasDefaultArgument() && 11069 !SecondTTPD->defaultArgumentWasInherited(); 11070 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11071 ODRDiagDeclError(FirstRecord, FirstModule, 11072 FirstTemplate->getLocation(), 11073 FirstTemplate->getSourceRange(), 11074 FunctionTemplateParameterSingleDefaultArgument) 11075 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11076 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11077 SecondTemplate->getSourceRange(), 11078 FunctionTemplateParameterSingleDefaultArgument) 11079 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11080 ParameterMismatch = true; 11081 break; 11082 } 11083 11084 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11085 TemplateArgument FirstTA = 11086 FirstTTPD->getDefaultArgument().getArgument(); 11087 TemplateArgument SecondTA = 11088 SecondTTPD->getDefaultArgument().getArgument(); 11089 if (ComputeTemplateArgumentODRHash(FirstTA) != 11090 ComputeTemplateArgumentODRHash(SecondTA)) { 11091 ODRDiagDeclError( 11092 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11093 FirstTemplate->getSourceRange(), 11094 FunctionTemplateParameterDifferentDefaultArgument) 11095 << FirstTemplate << (i + 1) << FirstTA; 11096 ODRDiagDeclNote( 11097 SecondModule, SecondTemplate->getLocation(), 11098 SecondTemplate->getSourceRange(), 11099 FunctionTemplateParameterDifferentDefaultArgument) 11100 << SecondTemplate << (i + 1) << SecondTA; 11101 ParameterMismatch = true; 11102 break; 11103 } 11104 } 11105 11106 if (FirstTTPD->isParameterPack() != 11107 SecondTTPD->isParameterPack()) { 11108 ODRDiagDeclError(FirstRecord, FirstModule, 11109 FirstTemplate->getLocation(), 11110 FirstTemplate->getSourceRange(), 11111 FunctionTemplatePackParameter) 11112 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 11113 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11114 SecondTemplate->getSourceRange(), 11115 FunctionTemplatePackParameter) 11116 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 11117 ParameterMismatch = true; 11118 break; 11119 } 11120 } 11121 11122 if (isa<NonTypeTemplateParmDecl>(FirstParam) && 11123 isa<NonTypeTemplateParmDecl>(SecondParam)) { 11124 NonTypeTemplateParmDecl *FirstNTTPD = 11125 cast<NonTypeTemplateParmDecl>(FirstParam); 11126 NonTypeTemplateParmDecl *SecondNTTPD = 11127 cast<NonTypeTemplateParmDecl>(SecondParam); 11128 11129 QualType FirstType = FirstNTTPD->getType(); 11130 QualType SecondType = SecondNTTPD->getType(); 11131 if (ComputeQualTypeODRHash(FirstType) != 11132 ComputeQualTypeODRHash(SecondType)) { 11133 ODRDiagDeclError(FirstRecord, FirstModule, 11134 FirstTemplate->getLocation(), 11135 FirstTemplate->getSourceRange(), 11136 FunctionTemplateParameterDifferentType) 11137 << FirstTemplate << (i + 1); 11138 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11139 SecondTemplate->getSourceRange(), 11140 FunctionTemplateParameterDifferentType) 11141 << SecondTemplate << (i + 1); 11142 ParameterMismatch = true; 11143 break; 11144 } 11145 11146 bool HasFirstDefaultArgument = 11147 FirstNTTPD->hasDefaultArgument() && 11148 !FirstNTTPD->defaultArgumentWasInherited(); 11149 bool HasSecondDefaultArgument = 11150 SecondNTTPD->hasDefaultArgument() && 11151 !SecondNTTPD->defaultArgumentWasInherited(); 11152 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11153 ODRDiagDeclError(FirstRecord, FirstModule, 11154 FirstTemplate->getLocation(), 11155 FirstTemplate->getSourceRange(), 11156 FunctionTemplateParameterSingleDefaultArgument) 11157 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11158 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11159 SecondTemplate->getSourceRange(), 11160 FunctionTemplateParameterSingleDefaultArgument) 11161 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11162 ParameterMismatch = true; 11163 break; 11164 } 11165 11166 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11167 Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument(); 11168 Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument(); 11169 if (ComputeODRHash(FirstDefaultArgument) != 11170 ComputeODRHash(SecondDefaultArgument)) { 11171 ODRDiagDeclError( 11172 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11173 FirstTemplate->getSourceRange(), 11174 FunctionTemplateParameterDifferentDefaultArgument) 11175 << FirstTemplate << (i + 1) << FirstDefaultArgument; 11176 ODRDiagDeclNote( 11177 SecondModule, SecondTemplate->getLocation(), 11178 SecondTemplate->getSourceRange(), 11179 FunctionTemplateParameterDifferentDefaultArgument) 11180 << SecondTemplate << (i + 1) << SecondDefaultArgument; 11181 ParameterMismatch = true; 11182 break; 11183 } 11184 } 11185 11186 if (FirstNTTPD->isParameterPack() != 11187 SecondNTTPD->isParameterPack()) { 11188 ODRDiagDeclError(FirstRecord, FirstModule, 11189 FirstTemplate->getLocation(), 11190 FirstTemplate->getSourceRange(), 11191 FunctionTemplatePackParameter) 11192 << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack(); 11193 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11194 SecondTemplate->getSourceRange(), 11195 FunctionTemplatePackParameter) 11196 << SecondTemplate << (i + 1) 11197 << SecondNTTPD->isParameterPack(); 11198 ParameterMismatch = true; 11199 break; 11200 } 11201 } 11202 } 11203 11204 if (ParameterMismatch) { 11205 Diagnosed = true; 11206 break; 11207 } 11208 11209 break; 11210 } 11211 } 11212 11213 if (Diagnosed) 11214 continue; 11215 11216 Diag(FirstDecl->getLocation(), 11217 diag::err_module_odr_violation_mismatch_decl_unknown) 11218 << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType 11219 << FirstDecl->getSourceRange(); 11220 Diag(SecondDecl->getLocation(), 11221 diag::note_module_odr_violation_mismatch_decl_unknown) 11222 << SecondModule << FirstDiffType << SecondDecl->getSourceRange(); 11223 Diagnosed = true; 11224 } 11225 11226 if (!Diagnosed) { 11227 // All definitions are updates to the same declaration. This happens if a 11228 // module instantiates the declaration of a class template specialization 11229 // and two or more other modules instantiate its definition. 11230 // 11231 // FIXME: Indicate which modules had instantiations of this definition. 11232 // FIXME: How can this even happen? 11233 Diag(Merge.first->getLocation(), 11234 diag::err_module_odr_violation_different_instantiations) 11235 << Merge.first; 11236 } 11237 } 11238 11239 // Issue ODR failures diagnostics for functions. 11240 for (auto &Merge : FunctionOdrMergeFailures) { 11241 enum ODRFunctionDifference { 11242 ReturnType, 11243 ParameterName, 11244 ParameterType, 11245 ParameterSingleDefaultArgument, 11246 ParameterDifferentDefaultArgument, 11247 FunctionBody, 11248 }; 11249 11250 FunctionDecl *FirstFunction = Merge.first; 11251 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction); 11252 11253 bool Diagnosed = false; 11254 for (auto &SecondFunction : Merge.second) { 11255 11256 if (FirstFunction == SecondFunction) 11257 continue; 11258 11259 std::string SecondModule = 11260 getOwningModuleNameForDiagnostic(SecondFunction); 11261 11262 auto ODRDiagError = [FirstFunction, &FirstModule, 11263 this](SourceLocation Loc, SourceRange Range, 11264 ODRFunctionDifference DiffType) { 11265 return Diag(Loc, diag::err_module_odr_violation_function) 11266 << FirstFunction << FirstModule.empty() << FirstModule << Range 11267 << DiffType; 11268 }; 11269 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11270 SourceRange Range, 11271 ODRFunctionDifference DiffType) { 11272 return Diag(Loc, diag::note_module_odr_violation_function) 11273 << SecondModule << Range << DiffType; 11274 }; 11275 11276 if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) != 11277 ComputeQualTypeODRHash(SecondFunction->getReturnType())) { 11278 ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(), 11279 FirstFunction->getReturnTypeSourceRange(), ReturnType) 11280 << FirstFunction->getReturnType(); 11281 ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(), 11282 SecondFunction->getReturnTypeSourceRange(), ReturnType) 11283 << SecondFunction->getReturnType(); 11284 Diagnosed = true; 11285 break; 11286 } 11287 11288 assert(FirstFunction->param_size() == SecondFunction->param_size() && 11289 "Merged functions with different number of parameters"); 11290 11291 auto ParamSize = FirstFunction->param_size(); 11292 bool ParameterMismatch = false; 11293 for (unsigned I = 0; I < ParamSize; ++I) { 11294 auto *FirstParam = FirstFunction->getParamDecl(I); 11295 auto *SecondParam = SecondFunction->getParamDecl(I); 11296 11297 assert(getContext().hasSameType(FirstParam->getType(), 11298 SecondParam->getType()) && 11299 "Merged function has different parameter types."); 11300 11301 if (FirstParam->getDeclName() != SecondParam->getDeclName()) { 11302 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11303 ParameterName) 11304 << I + 1 << FirstParam->getDeclName(); 11305 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11306 ParameterName) 11307 << I + 1 << SecondParam->getDeclName(); 11308 ParameterMismatch = true; 11309 break; 11310 }; 11311 11312 QualType FirstParamType = FirstParam->getType(); 11313 QualType SecondParamType = SecondParam->getType(); 11314 if (FirstParamType != SecondParamType && 11315 ComputeQualTypeODRHash(FirstParamType) != 11316 ComputeQualTypeODRHash(SecondParamType)) { 11317 if (const DecayedType *ParamDecayedType = 11318 FirstParamType->getAs<DecayedType>()) { 11319 ODRDiagError(FirstParam->getLocation(), 11320 FirstParam->getSourceRange(), ParameterType) 11321 << (I + 1) << FirstParamType << true 11322 << ParamDecayedType->getOriginalType(); 11323 } else { 11324 ODRDiagError(FirstParam->getLocation(), 11325 FirstParam->getSourceRange(), ParameterType) 11326 << (I + 1) << FirstParamType << false; 11327 } 11328 11329 if (const DecayedType *ParamDecayedType = 11330 SecondParamType->getAs<DecayedType>()) { 11331 ODRDiagNote(SecondParam->getLocation(), 11332 SecondParam->getSourceRange(), ParameterType) 11333 << (I + 1) << SecondParamType << true 11334 << ParamDecayedType->getOriginalType(); 11335 } else { 11336 ODRDiagNote(SecondParam->getLocation(), 11337 SecondParam->getSourceRange(), ParameterType) 11338 << (I + 1) << SecondParamType << false; 11339 } 11340 ParameterMismatch = true; 11341 break; 11342 } 11343 11344 const Expr *FirstInit = FirstParam->getInit(); 11345 const Expr *SecondInit = SecondParam->getInit(); 11346 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 11347 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11348 ParameterSingleDefaultArgument) 11349 << (I + 1) << (FirstInit == nullptr) 11350 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 11351 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11352 ParameterSingleDefaultArgument) 11353 << (I + 1) << (SecondInit == nullptr) 11354 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 11355 ParameterMismatch = true; 11356 break; 11357 } 11358 11359 if (FirstInit && SecondInit && 11360 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11361 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11362 ParameterDifferentDefaultArgument) 11363 << (I + 1) << FirstInit->getSourceRange(); 11364 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11365 ParameterDifferentDefaultArgument) 11366 << (I + 1) << SecondInit->getSourceRange(); 11367 ParameterMismatch = true; 11368 break; 11369 } 11370 11371 assert(ComputeSubDeclODRHash(FirstParam) == 11372 ComputeSubDeclODRHash(SecondParam) && 11373 "Undiagnosed parameter difference."); 11374 } 11375 11376 if (ParameterMismatch) { 11377 Diagnosed = true; 11378 break; 11379 } 11380 11381 // If no error has been generated before now, assume the problem is in 11382 // the body and generate a message. 11383 ODRDiagError(FirstFunction->getLocation(), 11384 FirstFunction->getSourceRange(), FunctionBody); 11385 ODRDiagNote(SecondFunction->getLocation(), 11386 SecondFunction->getSourceRange(), FunctionBody); 11387 Diagnosed = true; 11388 break; 11389 } 11390 (void)Diagnosed; 11391 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11392 } 11393 11394 // Issue ODR failures diagnostics for enums. 11395 for (auto &Merge : EnumOdrMergeFailures) { 11396 enum ODREnumDifference { 11397 SingleScopedEnum, 11398 EnumTagKeywordMismatch, 11399 SingleSpecifiedType, 11400 DifferentSpecifiedTypes, 11401 DifferentNumberEnumConstants, 11402 EnumConstantName, 11403 EnumConstantSingleInitilizer, 11404 EnumConstantDifferentInitilizer, 11405 }; 11406 11407 // If we've already pointed out a specific problem with this enum, don't 11408 // bother issuing a general "something's different" diagnostic. 11409 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 11410 continue; 11411 11412 EnumDecl *FirstEnum = Merge.first; 11413 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum); 11414 11415 using DeclHashes = 11416 llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>; 11417 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum]( 11418 DeclHashes &Hashes, EnumDecl *Enum) { 11419 for (auto *D : Enum->decls()) { 11420 // Due to decl merging, the first EnumDecl is the parent of 11421 // Decls in both records. 11422 if (!ODRHash::isDeclToBeProcessed(D, FirstEnum)) 11423 continue; 11424 assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind"); 11425 Hashes.emplace_back(cast<EnumConstantDecl>(D), 11426 ComputeSubDeclODRHash(D)); 11427 } 11428 }; 11429 DeclHashes FirstHashes; 11430 PopulateHashes(FirstHashes, FirstEnum); 11431 bool Diagnosed = false; 11432 for (auto &SecondEnum : Merge.second) { 11433 11434 if (FirstEnum == SecondEnum) 11435 continue; 11436 11437 std::string SecondModule = 11438 getOwningModuleNameForDiagnostic(SecondEnum); 11439 11440 auto ODRDiagError = [FirstEnum, &FirstModule, 11441 this](SourceLocation Loc, SourceRange Range, 11442 ODREnumDifference DiffType) { 11443 return Diag(Loc, diag::err_module_odr_violation_enum) 11444 << FirstEnum << FirstModule.empty() << FirstModule << Range 11445 << DiffType; 11446 }; 11447 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11448 SourceRange Range, 11449 ODREnumDifference DiffType) { 11450 return Diag(Loc, diag::note_module_odr_violation_enum) 11451 << SecondModule << Range << DiffType; 11452 }; 11453 11454 if (FirstEnum->isScoped() != SecondEnum->isScoped()) { 11455 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11456 SingleScopedEnum) 11457 << FirstEnum->isScoped(); 11458 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11459 SingleScopedEnum) 11460 << SecondEnum->isScoped(); 11461 Diagnosed = true; 11462 continue; 11463 } 11464 11465 if (FirstEnum->isScoped() && SecondEnum->isScoped()) { 11466 if (FirstEnum->isScopedUsingClassTag() != 11467 SecondEnum->isScopedUsingClassTag()) { 11468 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11469 EnumTagKeywordMismatch) 11470 << FirstEnum->isScopedUsingClassTag(); 11471 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11472 EnumTagKeywordMismatch) 11473 << SecondEnum->isScopedUsingClassTag(); 11474 Diagnosed = true; 11475 continue; 11476 } 11477 } 11478 11479 QualType FirstUnderlyingType = 11480 FirstEnum->getIntegerTypeSourceInfo() 11481 ? FirstEnum->getIntegerTypeSourceInfo()->getType() 11482 : QualType(); 11483 QualType SecondUnderlyingType = 11484 SecondEnum->getIntegerTypeSourceInfo() 11485 ? SecondEnum->getIntegerTypeSourceInfo()->getType() 11486 : QualType(); 11487 if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) { 11488 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11489 SingleSpecifiedType) 11490 << !FirstUnderlyingType.isNull(); 11491 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11492 SingleSpecifiedType) 11493 << !SecondUnderlyingType.isNull(); 11494 Diagnosed = true; 11495 continue; 11496 } 11497 11498 if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) { 11499 if (ComputeQualTypeODRHash(FirstUnderlyingType) != 11500 ComputeQualTypeODRHash(SecondUnderlyingType)) { 11501 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11502 DifferentSpecifiedTypes) 11503 << FirstUnderlyingType; 11504 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11505 DifferentSpecifiedTypes) 11506 << SecondUnderlyingType; 11507 Diagnosed = true; 11508 continue; 11509 } 11510 } 11511 11512 DeclHashes SecondHashes; 11513 PopulateHashes(SecondHashes, SecondEnum); 11514 11515 if (FirstHashes.size() != SecondHashes.size()) { 11516 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11517 DifferentNumberEnumConstants) 11518 << (int)FirstHashes.size(); 11519 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11520 DifferentNumberEnumConstants) 11521 << (int)SecondHashes.size(); 11522 Diagnosed = true; 11523 continue; 11524 } 11525 11526 for (unsigned I = 0; I < FirstHashes.size(); ++I) { 11527 if (FirstHashes[I].second == SecondHashes[I].second) 11528 continue; 11529 const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first; 11530 const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first; 11531 11532 if (FirstEnumConstant->getDeclName() != 11533 SecondEnumConstant->getDeclName()) { 11534 11535 ODRDiagError(FirstEnumConstant->getLocation(), 11536 FirstEnumConstant->getSourceRange(), EnumConstantName) 11537 << I + 1 << FirstEnumConstant; 11538 ODRDiagNote(SecondEnumConstant->getLocation(), 11539 SecondEnumConstant->getSourceRange(), EnumConstantName) 11540 << I + 1 << SecondEnumConstant; 11541 Diagnosed = true; 11542 break; 11543 } 11544 11545 const Expr *FirstInit = FirstEnumConstant->getInitExpr(); 11546 const Expr *SecondInit = SecondEnumConstant->getInitExpr(); 11547 if (!FirstInit && !SecondInit) 11548 continue; 11549 11550 if (!FirstInit || !SecondInit) { 11551 ODRDiagError(FirstEnumConstant->getLocation(), 11552 FirstEnumConstant->getSourceRange(), 11553 EnumConstantSingleInitilizer) 11554 << I + 1 << FirstEnumConstant << (FirstInit != nullptr); 11555 ODRDiagNote(SecondEnumConstant->getLocation(), 11556 SecondEnumConstant->getSourceRange(), 11557 EnumConstantSingleInitilizer) 11558 << I + 1 << SecondEnumConstant << (SecondInit != nullptr); 11559 Diagnosed = true; 11560 break; 11561 } 11562 11563 if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11564 ODRDiagError(FirstEnumConstant->getLocation(), 11565 FirstEnumConstant->getSourceRange(), 11566 EnumConstantDifferentInitilizer) 11567 << I + 1 << FirstEnumConstant; 11568 ODRDiagNote(SecondEnumConstant->getLocation(), 11569 SecondEnumConstant->getSourceRange(), 11570 EnumConstantDifferentInitilizer) 11571 << I + 1 << SecondEnumConstant; 11572 Diagnosed = true; 11573 break; 11574 } 11575 } 11576 } 11577 11578 (void)Diagnosed; 11579 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11580 } 11581 } 11582 11583 void ASTReader::StartedDeserializing() { 11584 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get()) 11585 ReadTimer->startTimer(); 11586 } 11587 11588 void ASTReader::FinishedDeserializing() { 11589 assert(NumCurrentElementsDeserializing && 11590 "FinishedDeserializing not paired with StartedDeserializing"); 11591 if (NumCurrentElementsDeserializing == 1) { 11592 // We decrease NumCurrentElementsDeserializing only after pending actions 11593 // are finished, to avoid recursively re-calling finishPendingActions(). 11594 finishPendingActions(); 11595 } 11596 --NumCurrentElementsDeserializing; 11597 11598 if (NumCurrentElementsDeserializing == 0) { 11599 // Propagate exception specification and deduced type updates along 11600 // redeclaration chains. 11601 // 11602 // We do this now rather than in finishPendingActions because we want to 11603 // be able to walk the complete redeclaration chains of the updated decls. 11604 while (!PendingExceptionSpecUpdates.empty() || 11605 !PendingDeducedTypeUpdates.empty()) { 11606 auto ESUpdates = std::move(PendingExceptionSpecUpdates); 11607 PendingExceptionSpecUpdates.clear(); 11608 for (auto Update : ESUpdates) { 11609 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11610 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>(); 11611 auto ESI = FPT->getExtProtoInfo().ExceptionSpec; 11612 if (auto *Listener = getContext().getASTMutationListener()) 11613 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second)); 11614 for (auto *Redecl : Update.second->redecls()) 11615 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI); 11616 } 11617 11618 auto DTUpdates = std::move(PendingDeducedTypeUpdates); 11619 PendingDeducedTypeUpdates.clear(); 11620 for (auto Update : DTUpdates) { 11621 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11622 // FIXME: If the return type is already deduced, check that it matches. 11623 getContext().adjustDeducedFunctionResultType(Update.first, 11624 Update.second); 11625 } 11626 } 11627 11628 if (ReadTimer) 11629 ReadTimer->stopTimer(); 11630 11631 diagnoseOdrViolations(); 11632 11633 // We are not in recursive loading, so it's safe to pass the "interesting" 11634 // decls to the consumer. 11635 if (Consumer) 11636 PassInterestingDeclsToConsumer(); 11637 } 11638 } 11639 11640 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) { 11641 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) { 11642 // Remove any fake results before adding any real ones. 11643 auto It = PendingFakeLookupResults.find(II); 11644 if (It != PendingFakeLookupResults.end()) { 11645 for (auto *ND : It->second) 11646 SemaObj->IdResolver.RemoveDecl(ND); 11647 // FIXME: this works around module+PCH performance issue. 11648 // Rather than erase the result from the map, which is O(n), just clear 11649 // the vector of NamedDecls. 11650 It->second.clear(); 11651 } 11652 } 11653 11654 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) { 11655 SemaObj->TUScope->AddDecl(D); 11656 } else if (SemaObj->TUScope) { 11657 // Adding the decl to IdResolver may have failed because it was already in 11658 // (even though it was not added in scope). If it is already in, make sure 11659 // it gets in the scope as well. 11660 if (std::find(SemaObj->IdResolver.begin(Name), 11661 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end()) 11662 SemaObj->TUScope->AddDecl(D); 11663 } 11664 } 11665 11666 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache, 11667 ASTContext *Context, 11668 const PCHContainerReader &PCHContainerRdr, 11669 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, 11670 StringRef isysroot, bool DisableValidation, 11671 bool AllowASTWithCompilerErrors, 11672 bool AllowConfigurationMismatch, bool ValidateSystemInputs, 11673 bool ValidateASTInputFilesContent, bool UseGlobalIndex, 11674 std::unique_ptr<llvm::Timer> ReadTimer) 11675 : Listener(DisableValidation 11676 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP)) 11677 : cast<ASTReaderListener>(new PCHValidator(PP, *this))), 11678 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), 11679 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP), 11680 ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache, 11681 PCHContainerRdr, PP.getHeaderSearchInfo()), 11682 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot), 11683 DisableValidation(DisableValidation), 11684 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors), 11685 AllowConfigurationMismatch(AllowConfigurationMismatch), 11686 ValidateSystemInputs(ValidateSystemInputs), 11687 ValidateASTInputFilesContent(ValidateASTInputFilesContent), 11688 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) { 11689 SourceMgr.setExternalSLocEntrySource(this); 11690 11691 for (const auto &Ext : Extensions) { 11692 auto BlockName = Ext->getExtensionMetadata().BlockName; 11693 auto Known = ModuleFileExtensions.find(BlockName); 11694 if (Known != ModuleFileExtensions.end()) { 11695 Diags.Report(diag::warn_duplicate_module_file_extension) 11696 << BlockName; 11697 continue; 11698 } 11699 11700 ModuleFileExtensions.insert({BlockName, Ext}); 11701 } 11702 } 11703 11704 ASTReader::~ASTReader() { 11705 if (OwnsDeserializationListener) 11706 delete DeserializationListener; 11707 } 11708 11709 IdentifierResolver &ASTReader::getIdResolver() { 11710 return SemaObj ? SemaObj->IdResolver : DummyIdResolver; 11711 } 11712 11713 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor, 11714 unsigned AbbrevID) { 11715 Idx = 0; 11716 Record.clear(); 11717 return Cursor.readRecord(AbbrevID, Record); 11718 } 11719 //===----------------------------------------------------------------------===// 11720 //// OMPClauseReader implementation 11721 ////===----------------------------------------------------------------------===// 11722 11723 // This has to be in namespace clang because it's friended by all 11724 // of the OMP clauses. 11725 namespace clang { 11726 11727 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> { 11728 ASTRecordReader &Record; 11729 ASTContext &Context; 11730 11731 public: 11732 OMPClauseReader(ASTRecordReader &Record) 11733 : Record(Record), Context(Record.getContext()) {} 11734 11735 #define OMP_CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C); 11736 #include "llvm/Frontend/OpenMP/OMPKinds.def" 11737 OMPClause *readClause(); 11738 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C); 11739 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C); 11740 }; 11741 11742 } // end namespace clang 11743 11744 OMPClause *ASTRecordReader::readOMPClause() { 11745 return OMPClauseReader(*this).readClause(); 11746 } 11747 11748 OMPClause *OMPClauseReader::readClause() { 11749 OMPClause *C = nullptr; 11750 switch (llvm::omp::Clause(Record.readInt())) { 11751 case llvm::omp::OMPC_if: 11752 C = new (Context) OMPIfClause(); 11753 break; 11754 case llvm::omp::OMPC_final: 11755 C = new (Context) OMPFinalClause(); 11756 break; 11757 case llvm::omp::OMPC_num_threads: 11758 C = new (Context) OMPNumThreadsClause(); 11759 break; 11760 case llvm::omp::OMPC_safelen: 11761 C = new (Context) OMPSafelenClause(); 11762 break; 11763 case llvm::omp::OMPC_simdlen: 11764 C = new (Context) OMPSimdlenClause(); 11765 break; 11766 case llvm::omp::OMPC_allocator: 11767 C = new (Context) OMPAllocatorClause(); 11768 break; 11769 case llvm::omp::OMPC_collapse: 11770 C = new (Context) OMPCollapseClause(); 11771 break; 11772 case llvm::omp::OMPC_default: 11773 C = new (Context) OMPDefaultClause(); 11774 break; 11775 case llvm::omp::OMPC_proc_bind: 11776 C = new (Context) OMPProcBindClause(); 11777 break; 11778 case llvm::omp::OMPC_schedule: 11779 C = new (Context) OMPScheduleClause(); 11780 break; 11781 case llvm::omp::OMPC_ordered: 11782 C = OMPOrderedClause::CreateEmpty(Context, Record.readInt()); 11783 break; 11784 case llvm::omp::OMPC_nowait: 11785 C = new (Context) OMPNowaitClause(); 11786 break; 11787 case llvm::omp::OMPC_untied: 11788 C = new (Context) OMPUntiedClause(); 11789 break; 11790 case llvm::omp::OMPC_mergeable: 11791 C = new (Context) OMPMergeableClause(); 11792 break; 11793 case llvm::omp::OMPC_read: 11794 C = new (Context) OMPReadClause(); 11795 break; 11796 case llvm::omp::OMPC_write: 11797 C = new (Context) OMPWriteClause(); 11798 break; 11799 case llvm::omp::OMPC_update: 11800 C = OMPUpdateClause::CreateEmpty(Context, Record.readInt()); 11801 break; 11802 case llvm::omp::OMPC_capture: 11803 C = new (Context) OMPCaptureClause(); 11804 break; 11805 case llvm::omp::OMPC_seq_cst: 11806 C = new (Context) OMPSeqCstClause(); 11807 break; 11808 case llvm::omp::OMPC_acq_rel: 11809 C = new (Context) OMPAcqRelClause(); 11810 break; 11811 case llvm::omp::OMPC_acquire: 11812 C = new (Context) OMPAcquireClause(); 11813 break; 11814 case llvm::omp::OMPC_release: 11815 C = new (Context) OMPReleaseClause(); 11816 break; 11817 case llvm::omp::OMPC_relaxed: 11818 C = new (Context) OMPRelaxedClause(); 11819 break; 11820 case llvm::omp::OMPC_threads: 11821 C = new (Context) OMPThreadsClause(); 11822 break; 11823 case llvm::omp::OMPC_simd: 11824 C = new (Context) OMPSIMDClause(); 11825 break; 11826 case llvm::omp::OMPC_nogroup: 11827 C = new (Context) OMPNogroupClause(); 11828 break; 11829 case llvm::omp::OMPC_unified_address: 11830 C = new (Context) OMPUnifiedAddressClause(); 11831 break; 11832 case llvm::omp::OMPC_unified_shared_memory: 11833 C = new (Context) OMPUnifiedSharedMemoryClause(); 11834 break; 11835 case llvm::omp::OMPC_reverse_offload: 11836 C = new (Context) OMPReverseOffloadClause(); 11837 break; 11838 case llvm::omp::OMPC_dynamic_allocators: 11839 C = new (Context) OMPDynamicAllocatorsClause(); 11840 break; 11841 case llvm::omp::OMPC_atomic_default_mem_order: 11842 C = new (Context) OMPAtomicDefaultMemOrderClause(); 11843 break; 11844 case llvm::omp::OMPC_private: 11845 C = OMPPrivateClause::CreateEmpty(Context, Record.readInt()); 11846 break; 11847 case llvm::omp::OMPC_firstprivate: 11848 C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt()); 11849 break; 11850 case llvm::omp::OMPC_lastprivate: 11851 C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt()); 11852 break; 11853 case llvm::omp::OMPC_shared: 11854 C = OMPSharedClause::CreateEmpty(Context, Record.readInt()); 11855 break; 11856 case llvm::omp::OMPC_reduction: { 11857 unsigned N = Record.readInt(); 11858 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>(); 11859 C = OMPReductionClause::CreateEmpty(Context, N, Modifier); 11860 break; 11861 } 11862 case llvm::omp::OMPC_task_reduction: 11863 C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt()); 11864 break; 11865 case llvm::omp::OMPC_in_reduction: 11866 C = OMPInReductionClause::CreateEmpty(Context, Record.readInt()); 11867 break; 11868 case llvm::omp::OMPC_linear: 11869 C = OMPLinearClause::CreateEmpty(Context, Record.readInt()); 11870 break; 11871 case llvm::omp::OMPC_aligned: 11872 C = OMPAlignedClause::CreateEmpty(Context, Record.readInt()); 11873 break; 11874 case llvm::omp::OMPC_copyin: 11875 C = OMPCopyinClause::CreateEmpty(Context, Record.readInt()); 11876 break; 11877 case llvm::omp::OMPC_copyprivate: 11878 C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt()); 11879 break; 11880 case llvm::omp::OMPC_flush: 11881 C = OMPFlushClause::CreateEmpty(Context, Record.readInt()); 11882 break; 11883 case llvm::omp::OMPC_depobj: 11884 C = OMPDepobjClause::CreateEmpty(Context); 11885 break; 11886 case llvm::omp::OMPC_depend: { 11887 unsigned NumVars = Record.readInt(); 11888 unsigned NumLoops = Record.readInt(); 11889 C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops); 11890 break; 11891 } 11892 case llvm::omp::OMPC_device: 11893 C = new (Context) OMPDeviceClause(); 11894 break; 11895 case llvm::omp::OMPC_map: { 11896 OMPMappableExprListSizeTy Sizes; 11897 Sizes.NumVars = Record.readInt(); 11898 Sizes.NumUniqueDeclarations = Record.readInt(); 11899 Sizes.NumComponentLists = Record.readInt(); 11900 Sizes.NumComponents = Record.readInt(); 11901 C = OMPMapClause::CreateEmpty(Context, Sizes); 11902 break; 11903 } 11904 case llvm::omp::OMPC_num_teams: 11905 C = new (Context) OMPNumTeamsClause(); 11906 break; 11907 case llvm::omp::OMPC_thread_limit: 11908 C = new (Context) OMPThreadLimitClause(); 11909 break; 11910 case llvm::omp::OMPC_priority: 11911 C = new (Context) OMPPriorityClause(); 11912 break; 11913 case llvm::omp::OMPC_grainsize: 11914 C = new (Context) OMPGrainsizeClause(); 11915 break; 11916 case llvm::omp::OMPC_num_tasks: 11917 C = new (Context) OMPNumTasksClause(); 11918 break; 11919 case llvm::omp::OMPC_hint: 11920 C = new (Context) OMPHintClause(); 11921 break; 11922 case llvm::omp::OMPC_dist_schedule: 11923 C = new (Context) OMPDistScheduleClause(); 11924 break; 11925 case llvm::omp::OMPC_defaultmap: 11926 C = new (Context) OMPDefaultmapClause(); 11927 break; 11928 case llvm::omp::OMPC_to: { 11929 OMPMappableExprListSizeTy Sizes; 11930 Sizes.NumVars = Record.readInt(); 11931 Sizes.NumUniqueDeclarations = Record.readInt(); 11932 Sizes.NumComponentLists = Record.readInt(); 11933 Sizes.NumComponents = Record.readInt(); 11934 C = OMPToClause::CreateEmpty(Context, Sizes); 11935 break; 11936 } 11937 case llvm::omp::OMPC_from: { 11938 OMPMappableExprListSizeTy Sizes; 11939 Sizes.NumVars = Record.readInt(); 11940 Sizes.NumUniqueDeclarations = Record.readInt(); 11941 Sizes.NumComponentLists = Record.readInt(); 11942 Sizes.NumComponents = Record.readInt(); 11943 C = OMPFromClause::CreateEmpty(Context, Sizes); 11944 break; 11945 } 11946 case llvm::omp::OMPC_use_device_ptr: { 11947 OMPMappableExprListSizeTy Sizes; 11948 Sizes.NumVars = Record.readInt(); 11949 Sizes.NumUniqueDeclarations = Record.readInt(); 11950 Sizes.NumComponentLists = Record.readInt(); 11951 Sizes.NumComponents = Record.readInt(); 11952 C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes); 11953 break; 11954 } 11955 case llvm::omp::OMPC_use_device_addr: { 11956 OMPMappableExprListSizeTy Sizes; 11957 Sizes.NumVars = Record.readInt(); 11958 Sizes.NumUniqueDeclarations = Record.readInt(); 11959 Sizes.NumComponentLists = Record.readInt(); 11960 Sizes.NumComponents = Record.readInt(); 11961 C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes); 11962 break; 11963 } 11964 case llvm::omp::OMPC_is_device_ptr: { 11965 OMPMappableExprListSizeTy Sizes; 11966 Sizes.NumVars = Record.readInt(); 11967 Sizes.NumUniqueDeclarations = Record.readInt(); 11968 Sizes.NumComponentLists = Record.readInt(); 11969 Sizes.NumComponents = Record.readInt(); 11970 C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes); 11971 break; 11972 } 11973 case llvm::omp::OMPC_allocate: 11974 C = OMPAllocateClause::CreateEmpty(Context, Record.readInt()); 11975 break; 11976 case llvm::omp::OMPC_nontemporal: 11977 C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt()); 11978 break; 11979 case llvm::omp::OMPC_inclusive: 11980 C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt()); 11981 break; 11982 case llvm::omp::OMPC_exclusive: 11983 C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt()); 11984 break; 11985 case llvm::omp::OMPC_order: 11986 C = new (Context) OMPOrderClause(); 11987 break; 11988 case llvm::omp::OMPC_destroy: 11989 C = new (Context) OMPDestroyClause(); 11990 break; 11991 case llvm::omp::OMPC_detach: 11992 C = new (Context) OMPDetachClause(); 11993 break; 11994 case llvm::omp::OMPC_uses_allocators: 11995 C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt()); 11996 break; 11997 case llvm::omp::OMPC_affinity: 11998 C = OMPAffinityClause::CreateEmpty(Context, Record.readInt()); 11999 break; 12000 #define OMP_CLAUSE_NO_CLASS(Enum, Str) \ 12001 case llvm::omp::Enum: \ 12002 break; 12003 #include "llvm/Frontend/OpenMP/OMPKinds.def" 12004 default: 12005 break; 12006 } 12007 assert(C && "Unknown OMPClause type"); 12008 12009 Visit(C); 12010 C->setLocStart(Record.readSourceLocation()); 12011 C->setLocEnd(Record.readSourceLocation()); 12012 12013 return C; 12014 } 12015 12016 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) { 12017 C->setPreInitStmt(Record.readSubStmt(), 12018 static_cast<OpenMPDirectiveKind>(Record.readInt())); 12019 } 12020 12021 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) { 12022 VisitOMPClauseWithPreInit(C); 12023 C->setPostUpdateExpr(Record.readSubExpr()); 12024 } 12025 12026 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) { 12027 VisitOMPClauseWithPreInit(C); 12028 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt())); 12029 C->setNameModifierLoc(Record.readSourceLocation()); 12030 C->setColonLoc(Record.readSourceLocation()); 12031 C->setCondition(Record.readSubExpr()); 12032 C->setLParenLoc(Record.readSourceLocation()); 12033 } 12034 12035 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) { 12036 VisitOMPClauseWithPreInit(C); 12037 C->setCondition(Record.readSubExpr()); 12038 C->setLParenLoc(Record.readSourceLocation()); 12039 } 12040 12041 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) { 12042 VisitOMPClauseWithPreInit(C); 12043 C->setNumThreads(Record.readSubExpr()); 12044 C->setLParenLoc(Record.readSourceLocation()); 12045 } 12046 12047 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) { 12048 C->setSafelen(Record.readSubExpr()); 12049 C->setLParenLoc(Record.readSourceLocation()); 12050 } 12051 12052 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) { 12053 C->setSimdlen(Record.readSubExpr()); 12054 C->setLParenLoc(Record.readSourceLocation()); 12055 } 12056 12057 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) { 12058 C->setAllocator(Record.readExpr()); 12059 C->setLParenLoc(Record.readSourceLocation()); 12060 } 12061 12062 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) { 12063 C->setNumForLoops(Record.readSubExpr()); 12064 C->setLParenLoc(Record.readSourceLocation()); 12065 } 12066 12067 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) { 12068 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt())); 12069 C->setLParenLoc(Record.readSourceLocation()); 12070 C->setDefaultKindKwLoc(Record.readSourceLocation()); 12071 } 12072 12073 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) { 12074 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt())); 12075 C->setLParenLoc(Record.readSourceLocation()); 12076 C->setProcBindKindKwLoc(Record.readSourceLocation()); 12077 } 12078 12079 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) { 12080 VisitOMPClauseWithPreInit(C); 12081 C->setScheduleKind( 12082 static_cast<OpenMPScheduleClauseKind>(Record.readInt())); 12083 C->setFirstScheduleModifier( 12084 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 12085 C->setSecondScheduleModifier( 12086 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 12087 C->setChunkSize(Record.readSubExpr()); 12088 C->setLParenLoc(Record.readSourceLocation()); 12089 C->setFirstScheduleModifierLoc(Record.readSourceLocation()); 12090 C->setSecondScheduleModifierLoc(Record.readSourceLocation()); 12091 C->setScheduleKindLoc(Record.readSourceLocation()); 12092 C->setCommaLoc(Record.readSourceLocation()); 12093 } 12094 12095 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) { 12096 C->setNumForLoops(Record.readSubExpr()); 12097 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 12098 C->setLoopNumIterations(I, Record.readSubExpr()); 12099 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 12100 C->setLoopCounter(I, Record.readSubExpr()); 12101 C->setLParenLoc(Record.readSourceLocation()); 12102 } 12103 12104 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) { 12105 C->setEventHandler(Record.readSubExpr()); 12106 C->setLParenLoc(Record.readSourceLocation()); 12107 } 12108 12109 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {} 12110 12111 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {} 12112 12113 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {} 12114 12115 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {} 12116 12117 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {} 12118 12119 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) { 12120 if (C->isExtended()) { 12121 C->setLParenLoc(Record.readSourceLocation()); 12122 C->setArgumentLoc(Record.readSourceLocation()); 12123 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>()); 12124 } 12125 } 12126 12127 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {} 12128 12129 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {} 12130 12131 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {} 12132 12133 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {} 12134 12135 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {} 12136 12137 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {} 12138 12139 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {} 12140 12141 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {} 12142 12143 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {} 12144 12145 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *) {} 12146 12147 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {} 12148 12149 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause( 12150 OMPUnifiedSharedMemoryClause *) {} 12151 12152 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {} 12153 12154 void 12155 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) { 12156 } 12157 12158 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause( 12159 OMPAtomicDefaultMemOrderClause *C) { 12160 C->setAtomicDefaultMemOrderKind( 12161 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt())); 12162 C->setLParenLoc(Record.readSourceLocation()); 12163 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation()); 12164 } 12165 12166 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) { 12167 C->setLParenLoc(Record.readSourceLocation()); 12168 unsigned NumVars = C->varlist_size(); 12169 SmallVector<Expr *, 16> Vars; 12170 Vars.reserve(NumVars); 12171 for (unsigned i = 0; i != NumVars; ++i) 12172 Vars.push_back(Record.readSubExpr()); 12173 C->setVarRefs(Vars); 12174 Vars.clear(); 12175 for (unsigned i = 0; i != NumVars; ++i) 12176 Vars.push_back(Record.readSubExpr()); 12177 C->setPrivateCopies(Vars); 12178 } 12179 12180 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) { 12181 VisitOMPClauseWithPreInit(C); 12182 C->setLParenLoc(Record.readSourceLocation()); 12183 unsigned NumVars = C->varlist_size(); 12184 SmallVector<Expr *, 16> Vars; 12185 Vars.reserve(NumVars); 12186 for (unsigned i = 0; i != NumVars; ++i) 12187 Vars.push_back(Record.readSubExpr()); 12188 C->setVarRefs(Vars); 12189 Vars.clear(); 12190 for (unsigned i = 0; i != NumVars; ++i) 12191 Vars.push_back(Record.readSubExpr()); 12192 C->setPrivateCopies(Vars); 12193 Vars.clear(); 12194 for (unsigned i = 0; i != NumVars; ++i) 12195 Vars.push_back(Record.readSubExpr()); 12196 C->setInits(Vars); 12197 } 12198 12199 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) { 12200 VisitOMPClauseWithPostUpdate(C); 12201 C->setLParenLoc(Record.readSourceLocation()); 12202 C->setKind(Record.readEnum<OpenMPLastprivateModifier>()); 12203 C->setKindLoc(Record.readSourceLocation()); 12204 C->setColonLoc(Record.readSourceLocation()); 12205 unsigned NumVars = C->varlist_size(); 12206 SmallVector<Expr *, 16> Vars; 12207 Vars.reserve(NumVars); 12208 for (unsigned i = 0; i != NumVars; ++i) 12209 Vars.push_back(Record.readSubExpr()); 12210 C->setVarRefs(Vars); 12211 Vars.clear(); 12212 for (unsigned i = 0; i != NumVars; ++i) 12213 Vars.push_back(Record.readSubExpr()); 12214 C->setPrivateCopies(Vars); 12215 Vars.clear(); 12216 for (unsigned i = 0; i != NumVars; ++i) 12217 Vars.push_back(Record.readSubExpr()); 12218 C->setSourceExprs(Vars); 12219 Vars.clear(); 12220 for (unsigned i = 0; i != NumVars; ++i) 12221 Vars.push_back(Record.readSubExpr()); 12222 C->setDestinationExprs(Vars); 12223 Vars.clear(); 12224 for (unsigned i = 0; i != NumVars; ++i) 12225 Vars.push_back(Record.readSubExpr()); 12226 C->setAssignmentOps(Vars); 12227 } 12228 12229 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) { 12230 C->setLParenLoc(Record.readSourceLocation()); 12231 unsigned NumVars = C->varlist_size(); 12232 SmallVector<Expr *, 16> Vars; 12233 Vars.reserve(NumVars); 12234 for (unsigned i = 0; i != NumVars; ++i) 12235 Vars.push_back(Record.readSubExpr()); 12236 C->setVarRefs(Vars); 12237 } 12238 12239 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) { 12240 VisitOMPClauseWithPostUpdate(C); 12241 C->setLParenLoc(Record.readSourceLocation()); 12242 C->setModifierLoc(Record.readSourceLocation()); 12243 C->setColonLoc(Record.readSourceLocation()); 12244 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12245 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12246 C->setQualifierLoc(NNSL); 12247 C->setNameInfo(DNI); 12248 12249 unsigned NumVars = C->varlist_size(); 12250 SmallVector<Expr *, 16> Vars; 12251 Vars.reserve(NumVars); 12252 for (unsigned i = 0; i != NumVars; ++i) 12253 Vars.push_back(Record.readSubExpr()); 12254 C->setVarRefs(Vars); 12255 Vars.clear(); 12256 for (unsigned i = 0; i != NumVars; ++i) 12257 Vars.push_back(Record.readSubExpr()); 12258 C->setPrivates(Vars); 12259 Vars.clear(); 12260 for (unsigned i = 0; i != NumVars; ++i) 12261 Vars.push_back(Record.readSubExpr()); 12262 C->setLHSExprs(Vars); 12263 Vars.clear(); 12264 for (unsigned i = 0; i != NumVars; ++i) 12265 Vars.push_back(Record.readSubExpr()); 12266 C->setRHSExprs(Vars); 12267 Vars.clear(); 12268 for (unsigned i = 0; i != NumVars; ++i) 12269 Vars.push_back(Record.readSubExpr()); 12270 C->setReductionOps(Vars); 12271 if (C->getModifier() == OMPC_REDUCTION_inscan) { 12272 Vars.clear(); 12273 for (unsigned i = 0; i != NumVars; ++i) 12274 Vars.push_back(Record.readSubExpr()); 12275 C->setInscanCopyOps(Vars); 12276 Vars.clear(); 12277 for (unsigned i = 0; i != NumVars; ++i) 12278 Vars.push_back(Record.readSubExpr()); 12279 C->setInscanCopyArrayTemps(Vars); 12280 Vars.clear(); 12281 for (unsigned i = 0; i != NumVars; ++i) 12282 Vars.push_back(Record.readSubExpr()); 12283 C->setInscanCopyArrayElems(Vars); 12284 } 12285 } 12286 12287 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) { 12288 VisitOMPClauseWithPostUpdate(C); 12289 C->setLParenLoc(Record.readSourceLocation()); 12290 C->setColonLoc(Record.readSourceLocation()); 12291 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12292 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12293 C->setQualifierLoc(NNSL); 12294 C->setNameInfo(DNI); 12295 12296 unsigned NumVars = C->varlist_size(); 12297 SmallVector<Expr *, 16> Vars; 12298 Vars.reserve(NumVars); 12299 for (unsigned I = 0; I != NumVars; ++I) 12300 Vars.push_back(Record.readSubExpr()); 12301 C->setVarRefs(Vars); 12302 Vars.clear(); 12303 for (unsigned I = 0; I != NumVars; ++I) 12304 Vars.push_back(Record.readSubExpr()); 12305 C->setPrivates(Vars); 12306 Vars.clear(); 12307 for (unsigned I = 0; I != NumVars; ++I) 12308 Vars.push_back(Record.readSubExpr()); 12309 C->setLHSExprs(Vars); 12310 Vars.clear(); 12311 for (unsigned I = 0; I != NumVars; ++I) 12312 Vars.push_back(Record.readSubExpr()); 12313 C->setRHSExprs(Vars); 12314 Vars.clear(); 12315 for (unsigned I = 0; I != NumVars; ++I) 12316 Vars.push_back(Record.readSubExpr()); 12317 C->setReductionOps(Vars); 12318 } 12319 12320 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) { 12321 VisitOMPClauseWithPostUpdate(C); 12322 C->setLParenLoc(Record.readSourceLocation()); 12323 C->setColonLoc(Record.readSourceLocation()); 12324 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12325 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12326 C->setQualifierLoc(NNSL); 12327 C->setNameInfo(DNI); 12328 12329 unsigned NumVars = C->varlist_size(); 12330 SmallVector<Expr *, 16> Vars; 12331 Vars.reserve(NumVars); 12332 for (unsigned I = 0; I != NumVars; ++I) 12333 Vars.push_back(Record.readSubExpr()); 12334 C->setVarRefs(Vars); 12335 Vars.clear(); 12336 for (unsigned I = 0; I != NumVars; ++I) 12337 Vars.push_back(Record.readSubExpr()); 12338 C->setPrivates(Vars); 12339 Vars.clear(); 12340 for (unsigned I = 0; I != NumVars; ++I) 12341 Vars.push_back(Record.readSubExpr()); 12342 C->setLHSExprs(Vars); 12343 Vars.clear(); 12344 for (unsigned I = 0; I != NumVars; ++I) 12345 Vars.push_back(Record.readSubExpr()); 12346 C->setRHSExprs(Vars); 12347 Vars.clear(); 12348 for (unsigned I = 0; I != NumVars; ++I) 12349 Vars.push_back(Record.readSubExpr()); 12350 C->setReductionOps(Vars); 12351 Vars.clear(); 12352 for (unsigned I = 0; I != NumVars; ++I) 12353 Vars.push_back(Record.readSubExpr()); 12354 C->setTaskgroupDescriptors(Vars); 12355 } 12356 12357 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) { 12358 VisitOMPClauseWithPostUpdate(C); 12359 C->setLParenLoc(Record.readSourceLocation()); 12360 C->setColonLoc(Record.readSourceLocation()); 12361 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt())); 12362 C->setModifierLoc(Record.readSourceLocation()); 12363 unsigned NumVars = C->varlist_size(); 12364 SmallVector<Expr *, 16> Vars; 12365 Vars.reserve(NumVars); 12366 for (unsigned i = 0; i != NumVars; ++i) 12367 Vars.push_back(Record.readSubExpr()); 12368 C->setVarRefs(Vars); 12369 Vars.clear(); 12370 for (unsigned i = 0; i != NumVars; ++i) 12371 Vars.push_back(Record.readSubExpr()); 12372 C->setPrivates(Vars); 12373 Vars.clear(); 12374 for (unsigned i = 0; i != NumVars; ++i) 12375 Vars.push_back(Record.readSubExpr()); 12376 C->setInits(Vars); 12377 Vars.clear(); 12378 for (unsigned i = 0; i != NumVars; ++i) 12379 Vars.push_back(Record.readSubExpr()); 12380 C->setUpdates(Vars); 12381 Vars.clear(); 12382 for (unsigned i = 0; i != NumVars; ++i) 12383 Vars.push_back(Record.readSubExpr()); 12384 C->setFinals(Vars); 12385 C->setStep(Record.readSubExpr()); 12386 C->setCalcStep(Record.readSubExpr()); 12387 Vars.clear(); 12388 for (unsigned I = 0; I != NumVars + 1; ++I) 12389 Vars.push_back(Record.readSubExpr()); 12390 C->setUsedExprs(Vars); 12391 } 12392 12393 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) { 12394 C->setLParenLoc(Record.readSourceLocation()); 12395 C->setColonLoc(Record.readSourceLocation()); 12396 unsigned NumVars = C->varlist_size(); 12397 SmallVector<Expr *, 16> Vars; 12398 Vars.reserve(NumVars); 12399 for (unsigned i = 0; i != NumVars; ++i) 12400 Vars.push_back(Record.readSubExpr()); 12401 C->setVarRefs(Vars); 12402 C->setAlignment(Record.readSubExpr()); 12403 } 12404 12405 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) { 12406 C->setLParenLoc(Record.readSourceLocation()); 12407 unsigned NumVars = C->varlist_size(); 12408 SmallVector<Expr *, 16> Exprs; 12409 Exprs.reserve(NumVars); 12410 for (unsigned i = 0; i != NumVars; ++i) 12411 Exprs.push_back(Record.readSubExpr()); 12412 C->setVarRefs(Exprs); 12413 Exprs.clear(); 12414 for (unsigned i = 0; i != NumVars; ++i) 12415 Exprs.push_back(Record.readSubExpr()); 12416 C->setSourceExprs(Exprs); 12417 Exprs.clear(); 12418 for (unsigned i = 0; i != NumVars; ++i) 12419 Exprs.push_back(Record.readSubExpr()); 12420 C->setDestinationExprs(Exprs); 12421 Exprs.clear(); 12422 for (unsigned i = 0; i != NumVars; ++i) 12423 Exprs.push_back(Record.readSubExpr()); 12424 C->setAssignmentOps(Exprs); 12425 } 12426 12427 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) { 12428 C->setLParenLoc(Record.readSourceLocation()); 12429 unsigned NumVars = C->varlist_size(); 12430 SmallVector<Expr *, 16> Exprs; 12431 Exprs.reserve(NumVars); 12432 for (unsigned i = 0; i != NumVars; ++i) 12433 Exprs.push_back(Record.readSubExpr()); 12434 C->setVarRefs(Exprs); 12435 Exprs.clear(); 12436 for (unsigned i = 0; i != NumVars; ++i) 12437 Exprs.push_back(Record.readSubExpr()); 12438 C->setSourceExprs(Exprs); 12439 Exprs.clear(); 12440 for (unsigned i = 0; i != NumVars; ++i) 12441 Exprs.push_back(Record.readSubExpr()); 12442 C->setDestinationExprs(Exprs); 12443 Exprs.clear(); 12444 for (unsigned i = 0; i != NumVars; ++i) 12445 Exprs.push_back(Record.readSubExpr()); 12446 C->setAssignmentOps(Exprs); 12447 } 12448 12449 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) { 12450 C->setLParenLoc(Record.readSourceLocation()); 12451 unsigned NumVars = C->varlist_size(); 12452 SmallVector<Expr *, 16> Vars; 12453 Vars.reserve(NumVars); 12454 for (unsigned i = 0; i != NumVars; ++i) 12455 Vars.push_back(Record.readSubExpr()); 12456 C->setVarRefs(Vars); 12457 } 12458 12459 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) { 12460 C->setDepobj(Record.readSubExpr()); 12461 C->setLParenLoc(Record.readSourceLocation()); 12462 } 12463 12464 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) { 12465 C->setLParenLoc(Record.readSourceLocation()); 12466 C->setModifier(Record.readSubExpr()); 12467 C->setDependencyKind( 12468 static_cast<OpenMPDependClauseKind>(Record.readInt())); 12469 C->setDependencyLoc(Record.readSourceLocation()); 12470 C->setColonLoc(Record.readSourceLocation()); 12471 unsigned NumVars = C->varlist_size(); 12472 SmallVector<Expr *, 16> Vars; 12473 Vars.reserve(NumVars); 12474 for (unsigned I = 0; I != NumVars; ++I) 12475 Vars.push_back(Record.readSubExpr()); 12476 C->setVarRefs(Vars); 12477 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I) 12478 C->setLoopData(I, Record.readSubExpr()); 12479 } 12480 12481 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) { 12482 VisitOMPClauseWithPreInit(C); 12483 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>()); 12484 C->setDevice(Record.readSubExpr()); 12485 C->setModifierLoc(Record.readSourceLocation()); 12486 C->setLParenLoc(Record.readSourceLocation()); 12487 } 12488 12489 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) { 12490 C->setLParenLoc(Record.readSourceLocation()); 12491 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) { 12492 C->setMapTypeModifier( 12493 I, static_cast<OpenMPMapModifierKind>(Record.readInt())); 12494 C->setMapTypeModifierLoc(I, Record.readSourceLocation()); 12495 } 12496 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12497 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12498 C->setMapType( 12499 static_cast<OpenMPMapClauseKind>(Record.readInt())); 12500 C->setMapLoc(Record.readSourceLocation()); 12501 C->setColonLoc(Record.readSourceLocation()); 12502 auto NumVars = C->varlist_size(); 12503 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12504 auto TotalLists = C->getTotalComponentListNum(); 12505 auto TotalComponents = C->getTotalComponentsNum(); 12506 12507 SmallVector<Expr *, 16> Vars; 12508 Vars.reserve(NumVars); 12509 for (unsigned i = 0; i != NumVars; ++i) 12510 Vars.push_back(Record.readExpr()); 12511 C->setVarRefs(Vars); 12512 12513 SmallVector<Expr *, 16> UDMappers; 12514 UDMappers.reserve(NumVars); 12515 for (unsigned I = 0; I < NumVars; ++I) 12516 UDMappers.push_back(Record.readExpr()); 12517 C->setUDMapperRefs(UDMappers); 12518 12519 SmallVector<ValueDecl *, 16> Decls; 12520 Decls.reserve(UniqueDecls); 12521 for (unsigned i = 0; i < UniqueDecls; ++i) 12522 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12523 C->setUniqueDecls(Decls); 12524 12525 SmallVector<unsigned, 16> ListsPerDecl; 12526 ListsPerDecl.reserve(UniqueDecls); 12527 for (unsigned i = 0; i < UniqueDecls; ++i) 12528 ListsPerDecl.push_back(Record.readInt()); 12529 C->setDeclNumLists(ListsPerDecl); 12530 12531 SmallVector<unsigned, 32> ListSizes; 12532 ListSizes.reserve(TotalLists); 12533 for (unsigned i = 0; i < TotalLists; ++i) 12534 ListSizes.push_back(Record.readInt()); 12535 C->setComponentListSizes(ListSizes); 12536 12537 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12538 Components.reserve(TotalComponents); 12539 for (unsigned i = 0; i < TotalComponents; ++i) { 12540 Expr *AssociatedExpr = Record.readExpr(); 12541 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12542 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12543 AssociatedExpr, AssociatedDecl)); 12544 } 12545 C->setComponents(Components, ListSizes); 12546 } 12547 12548 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) { 12549 C->setLParenLoc(Record.readSourceLocation()); 12550 C->setColonLoc(Record.readSourceLocation()); 12551 C->setAllocator(Record.readSubExpr()); 12552 unsigned NumVars = C->varlist_size(); 12553 SmallVector<Expr *, 16> Vars; 12554 Vars.reserve(NumVars); 12555 for (unsigned i = 0; i != NumVars; ++i) 12556 Vars.push_back(Record.readSubExpr()); 12557 C->setVarRefs(Vars); 12558 } 12559 12560 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) { 12561 VisitOMPClauseWithPreInit(C); 12562 C->setNumTeams(Record.readSubExpr()); 12563 C->setLParenLoc(Record.readSourceLocation()); 12564 } 12565 12566 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) { 12567 VisitOMPClauseWithPreInit(C); 12568 C->setThreadLimit(Record.readSubExpr()); 12569 C->setLParenLoc(Record.readSourceLocation()); 12570 } 12571 12572 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) { 12573 VisitOMPClauseWithPreInit(C); 12574 C->setPriority(Record.readSubExpr()); 12575 C->setLParenLoc(Record.readSourceLocation()); 12576 } 12577 12578 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) { 12579 VisitOMPClauseWithPreInit(C); 12580 C->setGrainsize(Record.readSubExpr()); 12581 C->setLParenLoc(Record.readSourceLocation()); 12582 } 12583 12584 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) { 12585 VisitOMPClauseWithPreInit(C); 12586 C->setNumTasks(Record.readSubExpr()); 12587 C->setLParenLoc(Record.readSourceLocation()); 12588 } 12589 12590 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) { 12591 C->setHint(Record.readSubExpr()); 12592 C->setLParenLoc(Record.readSourceLocation()); 12593 } 12594 12595 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) { 12596 VisitOMPClauseWithPreInit(C); 12597 C->setDistScheduleKind( 12598 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt())); 12599 C->setChunkSize(Record.readSubExpr()); 12600 C->setLParenLoc(Record.readSourceLocation()); 12601 C->setDistScheduleKindLoc(Record.readSourceLocation()); 12602 C->setCommaLoc(Record.readSourceLocation()); 12603 } 12604 12605 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) { 12606 C->setDefaultmapKind( 12607 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt())); 12608 C->setDefaultmapModifier( 12609 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt())); 12610 C->setLParenLoc(Record.readSourceLocation()); 12611 C->setDefaultmapModifierLoc(Record.readSourceLocation()); 12612 C->setDefaultmapKindLoc(Record.readSourceLocation()); 12613 } 12614 12615 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) { 12616 C->setLParenLoc(Record.readSourceLocation()); 12617 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) { 12618 C->setMotionModifier( 12619 I, static_cast<OpenMPMotionModifierKind>(Record.readInt())); 12620 C->setMotionModifierLoc(I, Record.readSourceLocation()); 12621 } 12622 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12623 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12624 C->setColonLoc(Record.readSourceLocation()); 12625 auto NumVars = C->varlist_size(); 12626 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12627 auto TotalLists = C->getTotalComponentListNum(); 12628 auto TotalComponents = C->getTotalComponentsNum(); 12629 12630 SmallVector<Expr *, 16> Vars; 12631 Vars.reserve(NumVars); 12632 for (unsigned i = 0; i != NumVars; ++i) 12633 Vars.push_back(Record.readSubExpr()); 12634 C->setVarRefs(Vars); 12635 12636 SmallVector<Expr *, 16> UDMappers; 12637 UDMappers.reserve(NumVars); 12638 for (unsigned I = 0; I < NumVars; ++I) 12639 UDMappers.push_back(Record.readSubExpr()); 12640 C->setUDMapperRefs(UDMappers); 12641 12642 SmallVector<ValueDecl *, 16> Decls; 12643 Decls.reserve(UniqueDecls); 12644 for (unsigned i = 0; i < UniqueDecls; ++i) 12645 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12646 C->setUniqueDecls(Decls); 12647 12648 SmallVector<unsigned, 16> ListsPerDecl; 12649 ListsPerDecl.reserve(UniqueDecls); 12650 for (unsigned i = 0; i < UniqueDecls; ++i) 12651 ListsPerDecl.push_back(Record.readInt()); 12652 C->setDeclNumLists(ListsPerDecl); 12653 12654 SmallVector<unsigned, 32> ListSizes; 12655 ListSizes.reserve(TotalLists); 12656 for (unsigned i = 0; i < TotalLists; ++i) 12657 ListSizes.push_back(Record.readInt()); 12658 C->setComponentListSizes(ListSizes); 12659 12660 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12661 Components.reserve(TotalComponents); 12662 for (unsigned i = 0; i < TotalComponents; ++i) { 12663 Expr *AssociatedExpr = Record.readSubExpr(); 12664 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12665 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12666 AssociatedExpr, AssociatedDecl)); 12667 } 12668 C->setComponents(Components, ListSizes); 12669 } 12670 12671 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) { 12672 C->setLParenLoc(Record.readSourceLocation()); 12673 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) { 12674 C->setMotionModifier( 12675 I, static_cast<OpenMPMotionModifierKind>(Record.readInt())); 12676 C->setMotionModifierLoc(I, Record.readSourceLocation()); 12677 } 12678 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12679 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12680 C->setColonLoc(Record.readSourceLocation()); 12681 auto NumVars = C->varlist_size(); 12682 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12683 auto TotalLists = C->getTotalComponentListNum(); 12684 auto TotalComponents = C->getTotalComponentsNum(); 12685 12686 SmallVector<Expr *, 16> Vars; 12687 Vars.reserve(NumVars); 12688 for (unsigned i = 0; i != NumVars; ++i) 12689 Vars.push_back(Record.readSubExpr()); 12690 C->setVarRefs(Vars); 12691 12692 SmallVector<Expr *, 16> UDMappers; 12693 UDMappers.reserve(NumVars); 12694 for (unsigned I = 0; I < NumVars; ++I) 12695 UDMappers.push_back(Record.readSubExpr()); 12696 C->setUDMapperRefs(UDMappers); 12697 12698 SmallVector<ValueDecl *, 16> Decls; 12699 Decls.reserve(UniqueDecls); 12700 for (unsigned i = 0; i < UniqueDecls; ++i) 12701 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12702 C->setUniqueDecls(Decls); 12703 12704 SmallVector<unsigned, 16> ListsPerDecl; 12705 ListsPerDecl.reserve(UniqueDecls); 12706 for (unsigned i = 0; i < UniqueDecls; ++i) 12707 ListsPerDecl.push_back(Record.readInt()); 12708 C->setDeclNumLists(ListsPerDecl); 12709 12710 SmallVector<unsigned, 32> ListSizes; 12711 ListSizes.reserve(TotalLists); 12712 for (unsigned i = 0; i < TotalLists; ++i) 12713 ListSizes.push_back(Record.readInt()); 12714 C->setComponentListSizes(ListSizes); 12715 12716 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12717 Components.reserve(TotalComponents); 12718 for (unsigned i = 0; i < TotalComponents; ++i) { 12719 Expr *AssociatedExpr = Record.readSubExpr(); 12720 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12721 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12722 AssociatedExpr, AssociatedDecl)); 12723 } 12724 C->setComponents(Components, ListSizes); 12725 } 12726 12727 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) { 12728 C->setLParenLoc(Record.readSourceLocation()); 12729 auto NumVars = C->varlist_size(); 12730 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12731 auto TotalLists = C->getTotalComponentListNum(); 12732 auto TotalComponents = C->getTotalComponentsNum(); 12733 12734 SmallVector<Expr *, 16> Vars; 12735 Vars.reserve(NumVars); 12736 for (unsigned i = 0; i != NumVars; ++i) 12737 Vars.push_back(Record.readSubExpr()); 12738 C->setVarRefs(Vars); 12739 Vars.clear(); 12740 for (unsigned i = 0; i != NumVars; ++i) 12741 Vars.push_back(Record.readSubExpr()); 12742 C->setPrivateCopies(Vars); 12743 Vars.clear(); 12744 for (unsigned i = 0; i != NumVars; ++i) 12745 Vars.push_back(Record.readSubExpr()); 12746 C->setInits(Vars); 12747 12748 SmallVector<ValueDecl *, 16> Decls; 12749 Decls.reserve(UniqueDecls); 12750 for (unsigned i = 0; i < UniqueDecls; ++i) 12751 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12752 C->setUniqueDecls(Decls); 12753 12754 SmallVector<unsigned, 16> ListsPerDecl; 12755 ListsPerDecl.reserve(UniqueDecls); 12756 for (unsigned i = 0; i < UniqueDecls; ++i) 12757 ListsPerDecl.push_back(Record.readInt()); 12758 C->setDeclNumLists(ListsPerDecl); 12759 12760 SmallVector<unsigned, 32> ListSizes; 12761 ListSizes.reserve(TotalLists); 12762 for (unsigned i = 0; i < TotalLists; ++i) 12763 ListSizes.push_back(Record.readInt()); 12764 C->setComponentListSizes(ListSizes); 12765 12766 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12767 Components.reserve(TotalComponents); 12768 for (unsigned i = 0; i < TotalComponents; ++i) { 12769 Expr *AssociatedExpr = Record.readSubExpr(); 12770 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12771 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12772 AssociatedExpr, AssociatedDecl)); 12773 } 12774 C->setComponents(Components, ListSizes); 12775 } 12776 12777 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) { 12778 C->setLParenLoc(Record.readSourceLocation()); 12779 auto NumVars = C->varlist_size(); 12780 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12781 auto TotalLists = C->getTotalComponentListNum(); 12782 auto TotalComponents = C->getTotalComponentsNum(); 12783 12784 SmallVector<Expr *, 16> Vars; 12785 Vars.reserve(NumVars); 12786 for (unsigned i = 0; i != NumVars; ++i) 12787 Vars.push_back(Record.readSubExpr()); 12788 C->setVarRefs(Vars); 12789 12790 SmallVector<ValueDecl *, 16> Decls; 12791 Decls.reserve(UniqueDecls); 12792 for (unsigned i = 0; i < UniqueDecls; ++i) 12793 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12794 C->setUniqueDecls(Decls); 12795 12796 SmallVector<unsigned, 16> ListsPerDecl; 12797 ListsPerDecl.reserve(UniqueDecls); 12798 for (unsigned i = 0; i < UniqueDecls; ++i) 12799 ListsPerDecl.push_back(Record.readInt()); 12800 C->setDeclNumLists(ListsPerDecl); 12801 12802 SmallVector<unsigned, 32> ListSizes; 12803 ListSizes.reserve(TotalLists); 12804 for (unsigned i = 0; i < TotalLists; ++i) 12805 ListSizes.push_back(Record.readInt()); 12806 C->setComponentListSizes(ListSizes); 12807 12808 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12809 Components.reserve(TotalComponents); 12810 for (unsigned i = 0; i < TotalComponents; ++i) { 12811 Expr *AssociatedExpr = Record.readSubExpr(); 12812 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12813 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12814 AssociatedExpr, AssociatedDecl)); 12815 } 12816 C->setComponents(Components, ListSizes); 12817 } 12818 12819 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) { 12820 C->setLParenLoc(Record.readSourceLocation()); 12821 auto NumVars = C->varlist_size(); 12822 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12823 auto TotalLists = C->getTotalComponentListNum(); 12824 auto TotalComponents = C->getTotalComponentsNum(); 12825 12826 SmallVector<Expr *, 16> Vars; 12827 Vars.reserve(NumVars); 12828 for (unsigned i = 0; i != NumVars; ++i) 12829 Vars.push_back(Record.readSubExpr()); 12830 C->setVarRefs(Vars); 12831 Vars.clear(); 12832 12833 SmallVector<ValueDecl *, 16> Decls; 12834 Decls.reserve(UniqueDecls); 12835 for (unsigned i = 0; i < UniqueDecls; ++i) 12836 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12837 C->setUniqueDecls(Decls); 12838 12839 SmallVector<unsigned, 16> ListsPerDecl; 12840 ListsPerDecl.reserve(UniqueDecls); 12841 for (unsigned i = 0; i < UniqueDecls; ++i) 12842 ListsPerDecl.push_back(Record.readInt()); 12843 C->setDeclNumLists(ListsPerDecl); 12844 12845 SmallVector<unsigned, 32> ListSizes; 12846 ListSizes.reserve(TotalLists); 12847 for (unsigned i = 0; i < TotalLists; ++i) 12848 ListSizes.push_back(Record.readInt()); 12849 C->setComponentListSizes(ListSizes); 12850 12851 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12852 Components.reserve(TotalComponents); 12853 for (unsigned i = 0; i < TotalComponents; ++i) { 12854 Expr *AssociatedExpr = Record.readSubExpr(); 12855 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12856 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12857 AssociatedExpr, AssociatedDecl)); 12858 } 12859 C->setComponents(Components, ListSizes); 12860 } 12861 12862 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) { 12863 C->setLParenLoc(Record.readSourceLocation()); 12864 unsigned NumVars = C->varlist_size(); 12865 SmallVector<Expr *, 16> Vars; 12866 Vars.reserve(NumVars); 12867 for (unsigned i = 0; i != NumVars; ++i) 12868 Vars.push_back(Record.readSubExpr()); 12869 C->setVarRefs(Vars); 12870 Vars.clear(); 12871 Vars.reserve(NumVars); 12872 for (unsigned i = 0; i != NumVars; ++i) 12873 Vars.push_back(Record.readSubExpr()); 12874 C->setPrivateRefs(Vars); 12875 } 12876 12877 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) { 12878 C->setLParenLoc(Record.readSourceLocation()); 12879 unsigned NumVars = C->varlist_size(); 12880 SmallVector<Expr *, 16> Vars; 12881 Vars.reserve(NumVars); 12882 for (unsigned i = 0; i != NumVars; ++i) 12883 Vars.push_back(Record.readSubExpr()); 12884 C->setVarRefs(Vars); 12885 } 12886 12887 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) { 12888 C->setLParenLoc(Record.readSourceLocation()); 12889 unsigned NumVars = C->varlist_size(); 12890 SmallVector<Expr *, 16> Vars; 12891 Vars.reserve(NumVars); 12892 for (unsigned i = 0; i != NumVars; ++i) 12893 Vars.push_back(Record.readSubExpr()); 12894 C->setVarRefs(Vars); 12895 } 12896 12897 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) { 12898 C->setLParenLoc(Record.readSourceLocation()); 12899 unsigned NumOfAllocators = C->getNumberOfAllocators(); 12900 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data; 12901 Data.reserve(NumOfAllocators); 12902 for (unsigned I = 0; I != NumOfAllocators; ++I) { 12903 OMPUsesAllocatorsClause::Data &D = Data.emplace_back(); 12904 D.Allocator = Record.readSubExpr(); 12905 D.AllocatorTraits = Record.readSubExpr(); 12906 D.LParenLoc = Record.readSourceLocation(); 12907 D.RParenLoc = Record.readSourceLocation(); 12908 } 12909 C->setAllocatorsData(Data); 12910 } 12911 12912 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) { 12913 C->setLParenLoc(Record.readSourceLocation()); 12914 C->setModifier(Record.readSubExpr()); 12915 C->setColonLoc(Record.readSourceLocation()); 12916 unsigned NumOfLocators = C->varlist_size(); 12917 SmallVector<Expr *, 4> Locators; 12918 Locators.reserve(NumOfLocators); 12919 for (unsigned I = 0; I != NumOfLocators; ++I) 12920 Locators.push_back(Record.readSubExpr()); 12921 C->setVarRefs(Locators); 12922 } 12923 12924 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) { 12925 C->setKind(Record.readEnum<OpenMPOrderClauseKind>()); 12926 C->setLParenLoc(Record.readSourceLocation()); 12927 C->setKindKwLoc(Record.readSourceLocation()); 12928 } 12929 12930 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() { 12931 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo(); 12932 TI.Sets.resize(readUInt32()); 12933 for (auto &Set : TI.Sets) { 12934 Set.Kind = readEnum<llvm::omp::TraitSet>(); 12935 Set.Selectors.resize(readUInt32()); 12936 for (auto &Selector : Set.Selectors) { 12937 Selector.Kind = readEnum<llvm::omp::TraitSelector>(); 12938 Selector.ScoreOrCondition = nullptr; 12939 if (readBool()) 12940 Selector.ScoreOrCondition = readExprRef(); 12941 Selector.Properties.resize(readUInt32()); 12942 for (auto &Property : Selector.Properties) 12943 Property.Kind = readEnum<llvm::omp::TraitProperty>(); 12944 } 12945 } 12946 return &TI; 12947 } 12948 12949 void ASTRecordReader::readOMPChildren(OMPChildren *Data) { 12950 if (!Data) 12951 return; 12952 if (Reader->ReadingKind == ASTReader::Read_Stmt) { 12953 // Skip NumClauses, NumChildren and HasAssociatedStmt fields. 12954 skipInts(3); 12955 } 12956 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses()); 12957 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I) 12958 Clauses[I] = readOMPClause(); 12959 Data->setClauses(Clauses); 12960 if (Data->hasAssociatedStmt()) 12961 Data->setAssociatedStmt(readStmt()); 12962 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I) 12963 Data->getChildren()[I] = readStmt(); 12964 } 12965